From 38092a051c013728cddf62787988a51275ce52b5 Mon Sep 17 00:00:00 2001 From: Diego Najar Date: Thu, 9 May 2019 19:31:55 +0200 Subject: [PATCH] Autosave and preview function migrated to fetch function --- bl-kernel/admin/views/edit-content.php | 12 ++-- bl-kernel/admin/views/new-content.php | 19 ++++-- bl-kernel/ajax/save-as-draft.php | 31 ++++++---- bl-kernel/js/bludit-ajax.php | 83 +++++++++++++++----------- 4 files changed, 88 insertions(+), 57 deletions(-) diff --git a/bl-kernel/admin/views/edit-content.php b/bl-kernel/admin/views/edit-content.php index 1c5f8bc6..7977aae8 100644 --- a/bl-kernel/admin/views/edit-content.php +++ b/bl-kernel/admin/views/edit-content.php @@ -400,8 +400,9 @@ $(document).ready(function() { var title = $("#jstitle").val(); var content = editorGetContent(); var ajax = new bluditAjax(); - ajax.autosave(uuid, title, content, false); - window.open("uuid().'?preview='.md5('autosave-'.$page->uuid()) ?>", "_blank"); + bluditAjax.preview(uuid, title, content).then(function(data) { + window.open("", "_blank"); + }); }); // Button Save @@ -444,8 +445,11 @@ $(document).ready(function() { // Call autosave only when the user change the content if (currentContent!=content) { currentContent = content; - // showAlert is the function to display an alert defined in alert.php - ajax.autosave(uuid, title, content, showAlert); + bluditAjax.autosave(uuid, title, content).then(function(data) { + if (data.status==0) { + showAlert("Autosave success"); + } + }); } },1000*60*AUTOSAVE_INTERVAL); diff --git a/bl-kernel/admin/views/new-content.php b/bl-kernel/admin/views/new-content.php index 763f0d80..3ff92b04 100644 --- a/bl-kernel/admin/views/new-content.php +++ b/bl-kernel/admin/views/new-content.php @@ -349,9 +349,9 @@ $(document).ready(function() { var uuid = $("#jsuuid").val(); var title = $("#jstitle").val(); var content = editorGetContent(); - var ajax = new bluditAjax(); - ajax.autosave(uuid, title, content, false); - window.open("", "_blank"); + bluditAjax.preview(uuid, title, content).then(function(data) { + window.open("", "_blank"); + }); }); // Button Save @@ -373,13 +373,20 @@ $(document).ready(function() { // Autosave // Autosave works when the content of the page is bigger than 100 characters + var currentContent = editorGetContent(); setInterval(function() { var uuid = $("#jsuuid").val(); var title = $("#jstitle").val(); var content = editorGetContent(); - var ajax = new bluditAjax(); - // showAlert is the function to display an alert defined in alert.php - ajax.autosave(uuid, title, content, showAlert); + // Call autosave only when the user change the content + if (currentContent!=content) { + currentContent = content; + bluditAjax.autosave(uuid, title, content).then(function(data) { + if (data.status==0) { + showAlert("Autosave success"); + } + }); + } },1000*60*AUTOSAVE_INTERVAL); }); diff --git a/bl-kernel/ajax/save-as-draft.php b/bl-kernel/ajax/save-as-draft.php index ed9c3933..39b6d301 100644 --- a/bl-kernel/ajax/save-as-draft.php +++ b/bl-kernel/ajax/save-as-draft.php @@ -1,33 +1,40 @@ $autosaveUUID, - 'key'=>$autosaveUUID, - 'slug'=>$autosaveUUID, - 'title'=>$title.' [ Autosave ] ', + 'uuid'=>$uuid, + 'key'=>$uuid, + 'slug'=>$uuid, + 'title'=>$title, 'content'=>$content, 'type'=>'draft' ); // Get the page key by the UUID -$pageKey = $pages->getByUUID($autosaveUUID); +$pageKey = $pages->getByUUID($uuid); // if pageKey is empty means the autosave page doesn't exist if (empty($pageKey)) { @@ -36,8 +43,8 @@ if (empty($pageKey)) { editPage($page); } -ajaxResponse(0, 'Autosave successfully.', array( - 'uuid'=>$autosaveUUID +ajaxResponse(0, 'Save as draft successfully.', array( + 'uuid'=>$uuid )); ?> \ No newline at end of file diff --git a/bl-kernel/js/bludit-ajax.php b/bl-kernel/js/bludit-ajax.php index f8580ab6..5158ca41 100644 --- a/bl-kernel/js/bludit-ajax.php +++ b/bl-kernel/js/bludit-ajax.php @@ -1,45 +1,58 @@ class bluditAjax { - // Autosave works only when the content has more than 100 characters - // callBack function need to be showAlert(), this function is for display alerts to the user, defined in alert.php - autosave(uuid, title, content, callBack) { - var ajaxRequest; - if (ajaxRequest) { - ajaxRequest.abort(); + static async preview(uuid, title, content) { + let url = HTML_PATH_ADMIN_ROOT+"ajax/save-as-draft" + try { + const response = await fetch(url, { + credentials: 'same-origin', + method: "POST", + headers: new Headers({ + 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8' + }), + body: new URLSearchParams({ + 'tokenCSRF': tokenCSRF, + 'uuid': "autosave-" + uuid, + 'title': title, + 'content': content + }), + }); + const json = await response.json(); + return json; } + catch (err) { + console.log(err); + return true; + } + } - if ((content.length<100) && callBack) { + // Autosave works only when the content has more than 100 characters + static async autosave(uuid, title, content) { + if ((content.length<100)) { return false; } - ajaxRequest = $.ajax({ - type: "POST", - data: { - tokenCSRF: tokenCSRF, // token from env variables - uuid: uuid, - title: title, - content: content - }, - url: HTML_PATH_ADMIN_ROOT+"ajax/save-as-draft" - }); - - ajaxRequest.done(function (response, textStatus, jqXHR) { - console.log("Bludit AJAX: autosave(): done handler"); - if (callBack) { - callBack("Autosave success"); - } - }); - - ajaxRequest.fail(function (jqXHR, textStatus, errorThrown) { - console.log("Bludit AJAX: autosave(): fail handler"); - if (callBack) { - callBack("Autosave failure"); - } - }); - - ajaxRequest.always(function () { - console.log("Bludit AJAX: autosave(): always handler"); - }); + let url = HTML_PATH_ADMIN_ROOT+"ajax/save-as-draft" + try { + const response = await fetch(url, { + credentials: 'same-origin', + method: "POST", + headers: new Headers({ + 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8' + }), + body: new URLSearchParams({ + 'tokenCSRF': tokenCSRF, + 'uuid': "autosave-" + uuid, + 'title': title+" [Autosave]", + 'content': content + }), + }); + const json = await response.json(); + return json; + } + catch (err) { + console.log(err); + return true; + } } // Alert the user when the user is not logged