1 import * as Dates from '../services/dates';
2 import {onSelect} from '../services/dom';
3 import {debounce} from '../services/util';
4 import {Component} from './component';
6 export class PageEditor extends Component {
10 this.draftsEnabled = this.$opts.draftsEnabled === 'true';
11 this.editorType = this.$opts.editorType;
12 this.pageId = Number(this.$opts.pageId);
13 this.isNewDraft = this.$opts.pageNewDraft === 'true';
14 this.hasDefaultTitle = this.$opts.hasDefaultTitle || false;
17 this.container = this.$el;
18 this.titleElem = this.$refs.titleContainer.querySelector('input');
19 this.saveDraftButton = this.$refs.saveDraft;
20 this.discardDraftButton = this.$refs.discardDraft;
21 this.discardDraftWrap = this.$refs.discardDraftWrap;
22 this.draftDisplay = this.$refs.draftDisplay;
23 this.draftDisplayIcon = this.$refs.draftDisplayIcon;
24 this.changelogInput = this.$refs.changelogInput;
25 this.changelogDisplay = this.$refs.changelogDisplay;
26 this.changeEditorButtons = this.$manyRefs.changeEditor || [];
27 this.switchDialogContainer = this.$refs.switchDialog;
30 this.draftText = this.$opts.draftText;
31 this.autosaveFailText = this.$opts.autosaveFailText;
32 this.editingPageText = this.$opts.editingPageText;
33 this.draftDiscardedText = this.$opts.draftDiscardedText;
34 this.setChangelogText = this.$opts.setChangelogText;
43 this.shownWarningsCache = new Set();
45 if (this.pageId !== 0 && this.draftsEnabled) {
46 window.setTimeout(() => {
50 this.draftDisplay.innerHTML = this.draftText;
52 this.setupListeners();
53 this.setInitialFocus();
57 // Listen to save events from editor
58 window.$events.listen('editor-save-draft', this.saveDraft.bind(this));
59 window.$events.listen('editor-save-page', this.savePage.bind(this));
61 // Listen to content changes from the editor
62 const onContentChange = () => {
63 this.autoSave.pendingChange = true;
65 window.$events.listen('editor-html-change', onContentChange);
66 window.$events.listen('editor-markdown-change', onContentChange);
68 // Listen to changes on the title input
69 this.titleElem.addEventListener('input', onContentChange);
72 const updateChangelogDebounced = debounce(this.updateChangelogDisplay.bind(this), 300, false);
73 this.changelogInput.addEventListener('input', updateChangelogDebounced);
76 onSelect(this.saveDraftButton, this.saveDraft.bind(this));
77 onSelect(this.discardDraftButton, this.discardDraft.bind(this));
79 // Change editor controls
80 onSelect(this.changeEditorButtons, this.changeEditor.bind(this));
84 if (this.hasDefaultTitle) {
85 this.titleElem.select();
89 window.setTimeout(() => {
90 window.$events.emit('editor::focus', '');
95 this.autoSave.interval = window.setInterval(this.runAutoSave.bind(this), this.autoSave.frequency);
99 // Stop if manually saved recently to prevent bombarding the server
100 const savedRecently = (Date.now() - this.autoSave.last < (this.autoSave.frequency) / 2);
101 if (savedRecently || !this.autoSave.pendingChange) {
109 this.container.closest('form').submit();
113 const data = {name: this.titleElem.value.trim()};
115 const editorContent = this.getEditorComponent().getContent();
116 Object.assign(data, editorContent);
120 const resp = await window.$http.put(`/ajax/page/${this.pageId}/save-draft`, data);
121 if (!this.isNewDraft) {
122 this.toggleDiscardDraftVisibility(true);
125 this.draftNotifyChange(`${resp.data.message} ${Dates.utcTimeStampToLocalTime(resp.data.timestamp)}`);
126 this.autoSave.last = Date.now();
127 if (resp.data.warning && !this.shownWarningsCache.has(resp.data.warning)) {
128 window.$events.emit('warning', resp.data.warning);
129 this.shownWarningsCache.add(resp.data.warning);
133 this.autoSave.pendingChange = false;
135 // Save the editor content in LocalStorage as a last resort, just in case.
137 const saveKey = `draft-save-fail-${(new Date()).toISOString()}`;
138 window.localStorage.setItem(saveKey, JSON.stringify(data));
140 console.error(lsErr);
143 window.$events.emit('error', this.autosaveFailText);
149 draftNotifyChange(text) {
150 this.draftDisplay.innerText = text;
151 this.draftDisplayIcon.classList.add('visible');
152 window.setTimeout(() => {
153 this.draftDisplayIcon.classList.remove('visible');
157 async discardDraft() {
160 response = await window.$http.get(`/ajax/page/${this.pageId}`);
166 if (this.autoSave.interval) {
167 window.clearInterval(this.autoSave.interval);
170 this.draftDisplay.innerText = this.editingPageText;
171 this.toggleDiscardDraftVisibility(false);
172 window.$events.emit('editor::replace', {
173 html: response.data.html,
174 markdown: response.data.markdown,
177 this.titleElem.value = response.data.name;
178 window.setTimeout(() => {
179 this.startAutoSave();
181 window.$events.emit('success', this.draftDiscardedText);
184 updateChangelogDisplay() {
185 let summary = this.changelogInput.value.trim();
186 if (summary.length === 0) {
187 summary = this.setChangelogText;
188 } else if (summary.length > 16) {
189 summary = `${summary.slice(0, 16)}...`;
191 this.changelogDisplay.innerText = summary;
194 toggleDiscardDraftVisibility(show) {
195 this.discardDraftWrap.classList.toggle('hidden', !show);
198 async changeEditor(event) {
199 event.preventDefault();
201 const link = event.target.closest('a').href;
202 /** @var {ConfirmDialog} * */
203 const dialog = window.$components.firstOnElement(this.switchDialogContainer, 'confirm-dialog');
204 const [saved, confirmed] = await Promise.all([this.saveDraft(), dialog.show()]);
206 if (saved && confirmed) {
207 window.location = link;
212 * @return MarkdownEditor|WysiwygEditor
214 getEditorComponent() {
215 return window.$components.first('markdown-editor') || window.$components.first('wysiwyg-editor');