]> BookStack Code Mirror - bookstack/blobdiff - resources/js/components/page-editor.js
Added webhook_call_before theme event hook
[bookstack] / resources / js / components / page-editor.js
index 4fb472e7eb71d3c40e338e1e0a204e5788d1431b..5f35e64992c50b793bdebf33edcb46a634fea4b9 100644 (file)
@@ -12,7 +12,7 @@ class PageEditor {
         this.editorType = this.$opts.editorType;
         this.pageId = Number(this.$opts.pageId);
         this.isNewDraft = this.$opts.pageNewDraft === 'true';
-        this.hasDefaultTitle = this.$opts.isDefaultTitle || false;
+        this.hasDefaultTitle = this.$opts.hasDefaultTitle || false;
 
         // Elements
         this.container = this.$el;
@@ -40,7 +40,7 @@ class PageEditor {
             frequency: 30000,
             last: 0,
         };
-        this.draftHasError = false;
+        this.shownWarningsCache = new Set();
 
         if (this.pageId !== 0 && this.draftsEnabled) {
             window.setTimeout(() => {
@@ -115,17 +115,23 @@ class PageEditor {
 
         try {
             const resp = await window.$http.put(`/ajax/page/${this.pageId}/save-draft`, data);
-            this.draftHasError = false;
             if (!this.isNewDraft) {
                 this.toggleDiscardDraftVisibility(true);
             }
             this.draftNotifyChange(`${resp.data.message} ${Dates.utcTimeStampToLocalTime(resp.data.timestamp)}`);
             this.autoSave.last = Date.now();
-        } catch (err) {
-            if (!this.draftHasError) {
-                this.draftHasError = true;
-                window.$events.emit('error', this.autosaveFailText);
+            if (resp.data.warning && !this.shownWarningsCache.has(resp.data.warning)) {
+                window.$events.emit('warning', resp.data.warning);
+                this.shownWarningsCache.add(resp.data.warning);
             }
+        } catch (err) {
+            // Save the editor content in LocalStorage as a last resort, just in case.
+            try {
+                const saveKey = `draft-save-fail-${(new Date()).toISOString()}`;
+                window.localStorage.setItem(saveKey, JSON.stringify(data));
+            } catch (err) {}
+
+            window.$events.emit('error', this.autosaveFailText);
         }
 
     }