X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/85b7b10c016ba7036309b598fc5b4d23c6f95fdc..3f5dc10cd4cf901b44b1cf8c9e2626bf0425d488:/resources/js/components/page-editor.js diff --git a/resources/js/components/page-editor.js b/resources/js/components/page-editor.js index 950a5a3b3..e7f4c0ba9 100644 --- a/resources/js/components/page-editor.js +++ b/resources/js/components/page-editor.js @@ -1,9 +1,10 @@ -import * as Dates from "../services/dates"; -import {onSelect} from "../services/dom"; -import {debounce} from "../services/util"; -import {Component} from "./component"; +import * as Dates from '../services/dates'; +import {onSelect} from '../services/dom'; +import {debounce} from '../services/util'; +import {Component} from './component'; export class PageEditor extends Component { + setup() { // Options this.draftsEnabled = this.$opts.draftsEnabled === 'true'; @@ -22,7 +23,7 @@ export class PageEditor extends Component { this.draftDisplayIcon = this.$refs.draftDisplayIcon; this.changelogInput = this.$refs.changelogInput; this.changelogDisplay = this.$refs.changelogDisplay; - this.changeEditorButtons = this.$manyRefs.changeEditor; + this.changeEditorButtons = this.$manyRefs.changeEditor || []; this.switchDialogContainer = this.$refs.switchDialog; // Translations @@ -33,12 +34,11 @@ export class PageEditor extends Component { this.setChangelogText = this.$opts.setChangelogText; // State data - this.editorHTML = ''; - this.editorMarkdown = ''; this.autoSave = { interval: null, frequency: 30000, last: 0, + pendingChange: false, }; this.shownWarningsCache = new Set(); @@ -59,12 +59,14 @@ export class PageEditor extends Component { window.$events.listen('editor-save-page', this.savePage.bind(this)); // Listen to content changes from the editor - window.$events.listen('editor-html-change', html => { - this.editorHTML = html; - }); - window.$events.listen('editor-markdown-change', markdown => { - this.editorMarkdown = markdown; - }); + const onContentChange = () => { + this.autoSave.pendingChange = true; + }; + window.$events.listen('editor-html-change', onContentChange); + window.$events.listen('editor-markdown-change', onContentChange); + + // Listen to changes on the title input + this.titleElem.addEventListener('input', onContentChange); // Changelog controls const updateChangelogDebounced = debounce(this.updateChangelogDisplay.bind(this), 300, false); @@ -80,7 +82,8 @@ export class PageEditor extends Component { setInitialFocus() { if (this.hasDefaultTitle) { - return this.titleElem.select(); + this.titleElem.select(); + return; } window.setTimeout(() => { @@ -89,18 +92,17 @@ export class PageEditor extends Component { } startAutoSave() { - let lastContent = this.titleElem.value.trim() + '::' + this.editorHTML; - this.autoSaveInterval = window.setInterval(() => { - // Stop if manually saved recently to prevent bombarding the server - let savedRecently = (Date.now() - this.autoSave.last < (this.autoSave.frequency)/2); - if (savedRecently) return; - const newContent = this.titleElem.value.trim() + '::' + this.editorHTML; - if (newContent !== lastContent) { - lastContent = newContent; - this.saveDraft(); - } + this.autoSave.interval = window.setInterval(this.runAutoSave.bind(this), this.autoSave.frequency); + } - }, this.autoSave.frequency); + runAutoSave() { + // Stop if manually saved recently to prevent bombarding the server + const savedRecently = (Date.now() - this.autoSave.last < (this.autoSave.frequency) / 2); + if (savedRecently || !this.autoSave.pendingChange) { + return; + } + + this.saveDraft(); } savePage() { @@ -108,14 +110,10 @@ export class PageEditor extends Component { } async saveDraft() { - const data = { - name: this.titleElem.value.trim(), - html: this.editorHTML, - }; + const data = {name: this.titleElem.value.trim()}; - if (this.editorType === 'markdown') { - data.markdown = this.editorMarkdown; - } + const editorContent = this.getEditorComponent().getContent(); + Object.assign(data, editorContent); let didSave = false; try { @@ -132,12 +130,15 @@ export class PageEditor extends Component { } didSave = true; + this.autoSave.pendingChange = false; } 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) {} + } catch (lsErr) { + console.error(lsErr); + } window.$events.emit('error', this.autosaveFailText); } @@ -158,7 +159,8 @@ export class PageEditor extends Component { try { response = await window.$http.get(`/ajax/page/${this.pageId}`); } catch (e) { - return console.error(e); + console.error(e); + return; } if (this.autoSave.interval) { @@ -177,7 +179,6 @@ export class PageEditor extends Component { this.startAutoSave(); }, 1000); window.$events.emit('success', this.draftDiscardedText); - } updateChangelogDisplay() { @@ -185,7 +186,7 @@ export class PageEditor extends Component { if (summary.length === 0) { summary = this.setChangelogText; } else if (summary.length > 16) { - summary = summary.slice(0, 16) + '...'; + summary = `${summary.slice(0, 16)}...`; } this.changelogDisplay.innerText = summary; } @@ -198,7 +199,7 @@ export class PageEditor extends Component { event.preventDefault(); const link = event.target.closest('a').href; - /** @var {ConfirmDialog} **/ + /** @var {ConfirmDialog} * */ const dialog = window.$components.firstOnElement(this.switchDialogContainer, 'confirm-dialog'); const [saved, confirmed] = await Promise.all([this.saveDraft(), dialog.show()]); @@ -207,4 +208,11 @@ export class PageEditor extends Component { } } + /** + * @return MarkdownEditor|WysiwygEditor + */ + getEditorComponent() { + return window.$components.first('markdown-editor') || window.$components.first('wysiwyg-editor'); + } + }