1 const moment = require('moment');
2 require('moment/locale/en-gb');
3 moment.locale('en-gb');
5 let autoSaveFrequency = 30;
8 let draftErroring = false;
10 let currentContent = {
19 this.draftsEnabled = elem.getAttribute('drafts-enabled') === 'true';
20 this.editorType = elem.getAttribute('editor-type');
21 this.pageId= Number(elem.getAttribute('page-id'));
22 this.isNewDraft = Number(elem.getAttribute('page-new-draft')) === 1;
23 this.isUpdateDraft = Number(elem.getAttribute('page-update-draft')) === 1;
25 if (this.pageId !== 0 && this.draftsEnabled) {
26 window.setTimeout(() => {
31 if (this.isUpdateDraft || this.isNewDraft) {
32 this.draftText = trans('entities.pages_editing_draft');
34 this.draftText = trans('entities.pages_editing_page');
37 // Listen to save draft events from editor
38 window.$events.listen('editor-save-draft', this.saveDraft);
40 // Listen to content changes from the editor
41 window.$events.listen('editor-html-change', html => {
42 this.editorHTML = html;
44 window.$events.listen('editor-markdown-change', markdown => {
45 this.editorMarkdown = markdown;
51 editorType: 'wysiwyg',
67 currentContent.title = document.getElementById('name').value.trim();
68 currentContent.html = this.editorHTML;
70 autoSave = window.setInterval(() => {
71 // Return if manually saved recently to prevent bombarding the server
72 if (Date.now() - lastSave < (1000 * autoSaveFrequency)/2) return;
73 let newTitle = document.getElementById('name').value.trim();
74 let newHtml = this.editorHTML;
76 if (newTitle !== currentContent.title || newHtml !== currentContent.html) {
77 currentContent.html = newHtml;
78 currentContent.title = newTitle;
82 }, 1000 * autoSaveFrequency);
86 if (!this.draftsEnabled) return;
89 name: document.getElementById('name').value.trim(),
93 if (this.editorType === 'markdown') data.markdown = this.editorMarkdown;
95 let url = window.baseUrl(`/ajax/page/${this.pageId}/save-draft`);
96 window.$http.put(url, data).then(response => {
97 draftErroring = false;
98 let updateTime = moment.utc(moment.unix(response.data.timestamp)).toDate();
99 if (!this.isNewPageDraft) this.isUpdateDraft = true;
100 this.draftNotifyChange(response.data.message + moment(updateTime).format('HH:mm'));
101 lastSave = Date.now();
103 if (draftErroring) return;
104 window.$events('error', trans('errors.page_draft_autosave_fail'));
105 draftErroring = true;
110 draftNotifyChange(text) {
111 this.draftText = text;
112 this.draftUpdated = true;
113 window.setTimeout(() => {
114 this.draftUpdated = false;
119 let url = window.baseUrl(`/ajax/page/${this.pageId}`);
120 window.$http.get(url).then(response => {
121 if (autoSave) window.clearInterval(autoSave);
123 this.draftText = trans('entities.pages_editing_page');
124 this.isUpdateDraft = false;
125 window.$events.emit('editor-html-update', response.data.html);
126 window.$events.emit('editor-markdown-update', response.data.markdown || response.data.html);
128 document.getElementById('name').value = response.data.name;
129 window.setTimeout(() => {
130 this.startAutoSave();
132 window.$events.emit('success', trans('entities.pages_draft_discarded'));
139 changeSummaryShort() {
140 let len = this.changeSummary.length;
141 if (len === 0) return trans('entities.pages_edit_set_changelog');
142 if (len <= 16) return this.changeSummary;
143 return this.changeSummary.slice(0, 16) + '...';
148 mounted, data, methods, computed,