1 import * as Dates from "../services/dates";
2 import {onSelect} from "../services/dom";
3 import {Component} from "./component";
5 export class PageEditor extends Component {
8 this.draftsEnabled = this.$opts.draftsEnabled === 'true';
9 this.editorType = this.$opts.editorType;
10 this.pageId = Number(this.$opts.pageId);
11 this.isNewDraft = this.$opts.pageNewDraft === 'true';
12 this.hasDefaultTitle = this.$opts.hasDefaultTitle || false;
15 this.container = this.$el;
16 this.titleElem = this.$refs.titleContainer.querySelector('input');
17 this.saveDraftButton = this.$refs.saveDraft;
18 this.discardDraftButton = this.$refs.discardDraft;
19 this.discardDraftWrap = this.$refs.discardDraftWrap;
20 this.draftDisplay = this.$refs.draftDisplay;
21 this.draftDisplayIcon = this.$refs.draftDisplayIcon;
22 this.changelogInput = this.$refs.changelogInput;
23 this.changelogDisplay = this.$refs.changelogDisplay;
24 this.changeEditorButtons = this.$manyRefs.changeEditor;
25 this.switchDialogContainer = this.$refs.switchDialog;
28 this.draftText = this.$opts.draftText;
29 this.autosaveFailText = this.$opts.autosaveFailText;
30 this.editingPageText = this.$opts.editingPageText;
31 this.draftDiscardedText = this.$opts.draftDiscardedText;
32 this.setChangelogText = this.$opts.setChangelogText;
36 this.editorMarkdown = '';
42 this.shownWarningsCache = new Set();
44 if (this.pageId !== 0 && this.draftsEnabled) {
45 window.setTimeout(() => {
49 this.draftDisplay.innerHTML = this.draftText;
51 this.setupListeners();
52 this.setInitialFocus();
56 // Listen to save events from editor
57 window.$events.listen('editor-save-draft', this.saveDraft.bind(this));
58 window.$events.listen('editor-save-page', this.savePage.bind(this));
60 // Listen to content changes from the editor
61 window.$events.listen('editor-html-change', html => {
62 this.editorHTML = html;
64 window.$events.listen('editor-markdown-change', markdown => {
65 this.editorMarkdown = markdown;
69 this.changelogInput.addEventListener('change', this.updateChangelogDisplay.bind(this));
72 onSelect(this.saveDraftButton, this.saveDraft.bind(this));
73 onSelect(this.discardDraftButton, this.discardDraft.bind(this));
75 // Change editor controls
76 onSelect(this.changeEditorButtons, this.changeEditor.bind(this));
80 if (this.hasDefaultTitle) {
81 return this.titleElem.select();
84 window.setTimeout(() => {
85 window.$events.emit('editor::focus', '');
90 let lastContent = this.titleElem.value.trim() + '::' + this.editorHTML;
91 this.autoSaveInterval = window.setInterval(() => {
92 // Stop if manually saved recently to prevent bombarding the server
93 let savedRecently = (Date.now() - this.autoSave.last < (this.autoSave.frequency)/2);
94 if (savedRecently) return;
95 const newContent = this.titleElem.value.trim() + '::' + this.editorHTML;
96 if (newContent !== lastContent) {
97 lastContent = newContent;
101 }, this.autoSave.frequency);
105 this.container.closest('form').submit();
110 name: this.titleElem.value.trim(),
111 html: this.editorHTML,
114 if (this.editorType === 'markdown') {
115 data.markdown = this.editorMarkdown;
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);
134 // Save the editor content in LocalStorage as a last resort, just in case.
136 const saveKey = `draft-save-fail-${(new Date()).toISOString()}`;
137 window.localStorage.setItem(saveKey, JSON.stringify(data));
140 window.$events.emit('error', this.autosaveFailText);
146 draftNotifyChange(text) {
147 this.draftDisplay.innerText = text;
148 this.draftDisplayIcon.classList.add('visible');
149 window.setTimeout(() => {
150 this.draftDisplayIcon.classList.remove('visible');
154 async discardDraft() {
157 response = await window.$http.get(`/ajax/page/${this.pageId}`);
159 return console.error(e);
162 if (this.autoSave.interval) {
163 window.clearInterval(this.autoSave.interval);
166 this.draftDisplay.innerText = this.editingPageText;
167 this.toggleDiscardDraftVisibility(false);
168 window.$events.emit('editor::replace', {
169 html: response.data.html,
170 markdown: response.data.markdown,
173 this.titleElem.value = response.data.name;
174 window.setTimeout(() => {
175 this.startAutoSave();
177 window.$events.emit('success', this.draftDiscardedText);
181 updateChangelogDisplay() {
182 let summary = this.changelogInput.value.trim();
183 if (summary.length === 0) {
184 summary = this.setChangelogText;
185 } else if (summary.length > 16) {
186 summary = summary.slice(0, 16) + '...';
188 this.changelogDisplay.innerText = summary;
191 toggleDiscardDraftVisibility(show) {
192 this.discardDraftWrap.classList.toggle('hidden', !show);
195 async changeEditor(event) {
196 event.preventDefault();
198 const link = event.target.closest('a').href;
199 /** @var {ConfirmDialog} **/
200 const dialog = window.$components.firstOnElement(this.switchDialogContainer, 'confirm-dialog');
201 const [saved, confirmed] = await Promise.all([this.saveDraft(), dialog.show()]);
203 if (saved && confirmed) {
204 window.location = link;