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;
22 this.titleElem = elem.querySelector('input[name=name]');
23 this.hasDefaultTitle = this.titleElem.closest('[is-default-value]') !== null;
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 events from editor
38 window.$events.listen('editor-save-draft', this.saveDraft);
39 window.$events.listen('editor-save-page', this.savePage);
41 // Listen to content changes from the editor
42 window.$events.listen('editor-html-change', html => {
43 this.editorHTML = html;
45 window.$events.listen('editor-markdown-change', markdown => {
46 this.editorMarkdown = markdown;
49 this.setInitialFocus();
54 editorType: 'wysiwyg',
66 hasDefaultTitle: false,
73 if (this.hasDefaultTitle) {
74 this.titleElem.select();
76 window.setTimeout(() => {
77 this.$events.emit('editor::focus', '');
83 currentContent.title = this.titleElem.value.trim();
84 currentContent.html = this.editorHTML;
86 autoSave = window.setInterval(() => {
87 // Return if manually saved recently to prevent bombarding the server
88 if (Date.now() - lastSave < (1000 * autoSaveFrequency)/2) return;
89 const newTitle = this.titleElem.value.trim();
90 const newHtml = this.editorHTML;
92 if (newTitle !== currentContent.title || newHtml !== currentContent.html) {
93 currentContent.html = newHtml;
94 currentContent.title = newTitle;
98 }, 1000 * autoSaveFrequency);
102 if (!this.draftsEnabled) return;
105 name: this.titleElem.value.trim(),
106 html: this.editorHTML
109 if (this.editorType === 'markdown') data.markdown = this.editorMarkdown;
111 const url = window.baseUrl(`/ajax/page/${this.pageId}/save-draft`);
112 window.$http.put(url, data).then(response => {
113 draftErroring = false;
114 if (!this.isNewDraft) this.isUpdateDraft = true;
115 this.draftNotifyChange(`${response.data.message} ${Dates.utcTimeStampToLocalTime(response.data.timestamp)}`);
116 lastSave = Date.now();
118 if (draftErroring) return;
119 window.$events.emit('error', trans('errors.page_draft_autosave_fail'));
120 draftErroring = true;
125 this.$el.closest('form').submit();
128 draftNotifyChange(text) {
129 this.draftText = text;
130 this.draftUpdated = true;
131 window.setTimeout(() => {
132 this.draftUpdated = false;
137 let url = window.baseUrl(`/ajax/page/${this.pageId}`);
138 window.$http.get(url).then(response => {
139 if (autoSave) window.clearInterval(autoSave);
141 this.draftText = trans('entities.pages_editing_page');
142 this.isUpdateDraft = false;
143 window.$events.emit('editor-html-update', response.data.html);
144 window.$events.emit('editor-markdown-update', response.data.markdown || response.data.html);
146 this.titleElem.value = response.data.name;
147 window.setTimeout(() => {
148 this.startAutoSave();
150 window.$events.emit('success', trans('entities.pages_draft_discarded'));
157 changeSummaryShort() {
158 let len = this.changeSummary.length;
159 if (len === 0) return trans('entities.pages_edit_set_changelog');
160 if (len <= 16) return this.changeSummary;
161 return this.changeSummary.slice(0, 16) + '...';
166 mounted, data, methods, computed,