1 import * as Dates from "../services/dates";
3 let autoSaveFrequency = 30;
6 let draftErroring = false;
17 this.draftsEnabled = elem.getAttribute('drafts-enabled') === 'true';
18 this.editorType = elem.getAttribute('editor-type');
19 this.pageId= Number(elem.getAttribute('page-id'));
20 this.isNewDraft = Number(elem.getAttribute('page-new-draft')) === 1;
21 this.isUpdateDraft = Number(elem.getAttribute('page-update-draft')) === 1;
23 if (this.pageId !== 0 && this.draftsEnabled) {
24 window.setTimeout(() => {
29 if (this.isUpdateDraft || this.isNewDraft) {
30 this.draftText = trans('entities.pages_editing_draft');
32 this.draftText = trans('entities.pages_editing_page');
35 // Listen to save events from editor
36 window.$events.listen('editor-save-draft', this.saveDraft);
37 window.$events.listen('editor-save-page', this.savePage);
39 // Listen to content changes from the editor
40 window.$events.listen('editor-html-change', html => {
41 this.editorHTML = html;
43 window.$events.listen('editor-markdown-change', markdown => {
44 this.editorMarkdown = markdown;
47 const scrollToText = window.location.hash ? window.location.hash.substr(1) : '';
50 window.$events.emit('editor-scroll-to-text', scrollToText);
57 editorType: 'wysiwyg',
73 currentContent.title = document.getElementById('name').value.trim();
74 currentContent.html = this.editorHTML;
76 autoSave = window.setInterval(() => {
77 // Return if manually saved recently to prevent bombarding the server
78 if (Date.now() - lastSave < (1000 * autoSaveFrequency)/2) return;
79 let newTitle = document.getElementById('name').value.trim();
80 let newHtml = this.editorHTML;
82 if (newTitle !== currentContent.title || newHtml !== currentContent.html) {
83 currentContent.html = newHtml;
84 currentContent.title = newTitle;
88 }, 1000 * autoSaveFrequency);
92 if (!this.draftsEnabled) return;
95 name: document.getElementById('name').value.trim(),
99 if (this.editorType === 'markdown') data.markdown = this.editorMarkdown;
101 let url = window.baseUrl(`/ajax/page/${this.pageId}/save-draft`);
102 window.$http.put(url, data).then(response => {
103 draftErroring = false;
104 if (!this.isNewDraft) this.isUpdateDraft = true;
105 this.draftNotifyChange(`${response.data.message } ${Dates.utcTimeStampToLocalTime(response.data.timestamp)}`);
106 lastSave = Date.now();
108 if (draftErroring) return;
109 window.$events.emit('error', trans('errors.page_draft_autosave_fail'));
110 draftErroring = true;
115 this.$el.closest('form').submit();
118 draftNotifyChange(text) {
119 this.draftText = text;
120 this.draftUpdated = true;
121 window.setTimeout(() => {
122 this.draftUpdated = false;
127 let url = window.baseUrl(`/ajax/page/${this.pageId}`);
128 window.$http.get(url).then(response => {
129 if (autoSave) window.clearInterval(autoSave);
131 this.draftText = trans('entities.pages_editing_page');
132 this.isUpdateDraft = false;
133 window.$events.emit('editor-html-update', response.data.html);
134 window.$events.emit('editor-markdown-update', response.data.markdown || response.data.html);
136 document.getElementById('name').value = response.data.name;
137 window.setTimeout(() => {
138 this.startAutoSave();
140 window.$events.emit('success', trans('entities.pages_draft_discarded'));
147 changeSummaryShort() {
148 let len = this.changeSummary.length;
149 if (len === 0) return trans('entities.pages_edit_set_changelog');
150 if (len <= 16) return this.changeSummary;
151 return this.changeSummary.slice(0, 16) + '...';
156 mounted, data, methods, computed,