2023-04-18 17:20:02 -04:00
|
|
|
import * as Dates from '../services/dates';
|
|
|
|
import {onSelect} from '../services/dom';
|
|
|
|
import {debounce} from '../services/util';
|
|
|
|
import {Component} from './component';
|
2020-07-05 16:18:17 -04:00
|
|
|
|
2022-11-16 08:04:22 -05:00
|
|
|
export class PageEditor extends Component {
|
2023-04-18 17:20:02 -04:00
|
|
|
|
2020-07-05 16:18:17 -04:00
|
|
|
setup() {
|
|
|
|
// Options
|
|
|
|
this.draftsEnabled = this.$opts.draftsEnabled === 'true';
|
|
|
|
this.editorType = this.$opts.editorType;
|
|
|
|
this.pageId = Number(this.$opts.pageId);
|
|
|
|
this.isNewDraft = this.$opts.pageNewDraft === 'true';
|
2021-01-03 17:29:58 -05:00
|
|
|
this.hasDefaultTitle = this.$opts.hasDefaultTitle || false;
|
2020-07-05 16:18:17 -04:00
|
|
|
|
|
|
|
// Elements
|
|
|
|
this.container = this.$el;
|
|
|
|
this.titleElem = this.$refs.titleContainer.querySelector('input');
|
|
|
|
this.saveDraftButton = this.$refs.saveDraft;
|
|
|
|
this.discardDraftButton = this.$refs.discardDraft;
|
|
|
|
this.discardDraftWrap = this.$refs.discardDraftWrap;
|
|
|
|
this.draftDisplay = this.$refs.draftDisplay;
|
|
|
|
this.draftDisplayIcon = this.$refs.draftDisplayIcon;
|
|
|
|
this.changelogInput = this.$refs.changelogInput;
|
|
|
|
this.changelogDisplay = this.$refs.changelogDisplay;
|
2023-04-18 09:21:22 -04:00
|
|
|
this.changeEditorButtons = this.$manyRefs.changeEditor || [];
|
2022-04-20 13:21:21 -04:00
|
|
|
this.switchDialogContainer = this.$refs.switchDialog;
|
2020-07-05 16:18:17 -04:00
|
|
|
|
|
|
|
// Translations
|
|
|
|
this.draftText = this.$opts.draftText;
|
|
|
|
this.autosaveFailText = this.$opts.autosaveFailText;
|
|
|
|
this.editingPageText = this.$opts.editingPageText;
|
|
|
|
this.draftDiscardedText = this.$opts.draftDiscardedText;
|
|
|
|
this.setChangelogText = this.$opts.setChangelogText;
|
|
|
|
|
|
|
|
// State data
|
|
|
|
this.autoSave = {
|
|
|
|
interval: null,
|
|
|
|
frequency: 30000,
|
|
|
|
last: 0,
|
2023-02-23 07:30:27 -05:00
|
|
|
pendingChange: false,
|
2020-07-05 16:18:17 -04:00
|
|
|
};
|
2021-10-04 15:26:55 -04:00
|
|
|
this.shownWarningsCache = new Set();
|
2020-07-05 16:18:17 -04:00
|
|
|
|
|
|
|
if (this.pageId !== 0 && this.draftsEnabled) {
|
|
|
|
window.setTimeout(() => {
|
|
|
|
this.startAutoSave();
|
|
|
|
}, 1000);
|
|
|
|
}
|
|
|
|
this.draftDisplay.innerHTML = this.draftText;
|
|
|
|
|
|
|
|
this.setupListeners();
|
|
|
|
this.setInitialFocus();
|
|
|
|
}
|
|
|
|
|
|
|
|
setupListeners() {
|
|
|
|
// Listen to save events from editor
|
|
|
|
window.$events.listen('editor-save-draft', this.saveDraft.bind(this));
|
|
|
|
window.$events.listen('editor-save-page', this.savePage.bind(this));
|
|
|
|
|
|
|
|
// Listen to content changes from the editor
|
2023-04-19 10:20:04 -04:00
|
|
|
const onContentChange = () => {
|
|
|
|
this.autoSave.pendingChange = true;
|
|
|
|
};
|
2023-02-23 07:30:27 -05:00
|
|
|
window.$events.listen('editor-html-change', onContentChange);
|
|
|
|
window.$events.listen('editor-markdown-change', onContentChange);
|
|
|
|
|
|
|
|
// Listen to changes on the title input
|
|
|
|
this.titleElem.addEventListener('input', onContentChange);
|
2020-07-05 16:18:17 -04:00
|
|
|
|
|
|
|
// Changelog controls
|
2022-11-21 12:35:19 -05:00
|
|
|
const updateChangelogDebounced = debounce(this.updateChangelogDisplay.bind(this), 300, false);
|
|
|
|
this.changelogInput.addEventListener('input', updateChangelogDebounced);
|
2020-07-05 16:18:17 -04:00
|
|
|
|
|
|
|
// Draft Controls
|
|
|
|
onSelect(this.saveDraftButton, this.saveDraft.bind(this));
|
|
|
|
onSelect(this.discardDraftButton, this.discardDraft.bind(this));
|
2022-04-20 13:21:21 -04:00
|
|
|
|
|
|
|
// Change editor controls
|
|
|
|
onSelect(this.changeEditorButtons, this.changeEditor.bind(this));
|
2020-07-05 16:18:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
setInitialFocus() {
|
|
|
|
if (this.hasDefaultTitle) {
|
2023-04-19 10:20:04 -04:00
|
|
|
this.titleElem.select();
|
|
|
|
return;
|
2020-07-05 16:18:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
window.setTimeout(() => {
|
|
|
|
window.$events.emit('editor::focus', '');
|
|
|
|
}, 500);
|
|
|
|
}
|
|
|
|
|
|
|
|
startAutoSave() {
|
2023-02-23 07:30:27 -05:00
|
|
|
this.autoSave.interval = window.setInterval(this.runAutoSave.bind(this), this.autoSave.frequency);
|
|
|
|
}
|
2020-07-05 16:18:17 -04:00
|
|
|
|
2023-02-23 07:30:27 -05:00
|
|
|
runAutoSave() {
|
|
|
|
// Stop if manually saved recently to prevent bombarding the server
|
2023-04-18 17:20:02 -04:00
|
|
|
const savedRecently = (Date.now() - this.autoSave.last < (this.autoSave.frequency) / 2);
|
2023-02-23 07:30:27 -05:00
|
|
|
if (savedRecently || !this.autoSave.pendingChange) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-04-18 17:20:02 -04:00
|
|
|
this.saveDraft();
|
2020-07-05 16:18:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
savePage() {
|
|
|
|
this.container.closest('form').submit();
|
|
|
|
}
|
|
|
|
|
|
|
|
async saveDraft() {
|
2023-02-23 07:30:27 -05:00
|
|
|
const data = {name: this.titleElem.value.trim()};
|
2020-07-05 16:18:17 -04:00
|
|
|
|
2023-02-23 07:30:27 -05:00
|
|
|
const editorContent = this.getEditorComponent().getContent();
|
|
|
|
Object.assign(data, editorContent);
|
2020-07-05 16:18:17 -04:00
|
|
|
|
2022-04-20 13:21:21 -04:00
|
|
|
let didSave = false;
|
2020-07-05 16:18:17 -04:00
|
|
|
try {
|
|
|
|
const resp = await window.$http.put(`/ajax/page/${this.pageId}/save-draft`, data);
|
|
|
|
if (!this.isNewDraft) {
|
|
|
|
this.toggleDiscardDraftVisibility(true);
|
|
|
|
}
|
2022-04-20 13:21:21 -04:00
|
|
|
|
2020-07-05 16:18:17 -04:00
|
|
|
this.draftNotifyChange(`${resp.data.message} ${Dates.utcTimeStampToLocalTime(resp.data.timestamp)}`);
|
|
|
|
this.autoSave.last = Date.now();
|
2021-10-04 15:26:55 -04:00
|
|
|
if (resp.data.warning && !this.shownWarningsCache.has(resp.data.warning)) {
|
2021-08-08 13:20:15 -04:00
|
|
|
window.$events.emit('warning', resp.data.warning);
|
2021-10-04 15:26:55 -04:00
|
|
|
this.shownWarningsCache.add(resp.data.warning);
|
2021-08-08 13:20:15 -04:00
|
|
|
}
|
2022-04-20 13:21:21 -04:00
|
|
|
|
|
|
|
didSave = true;
|
2023-02-23 07:30:27 -05:00
|
|
|
this.autoSave.pendingChange = false;
|
2020-07-05 16:18:17 -04:00
|
|
|
} catch (err) {
|
2020-09-13 14:32:45 -04:00
|
|
|
// Save the editor content in LocalStorage as a last resort, just in case.
|
|
|
|
try {
|
|
|
|
const saveKey = `draft-save-fail-${(new Date()).toISOString()}`;
|
|
|
|
window.localStorage.setItem(saveKey, JSON.stringify(data));
|
2023-04-19 10:20:04 -04:00
|
|
|
} catch (lsErr) {
|
|
|
|
console.error(lsErr);
|
|
|
|
}
|
2020-09-13 14:32:45 -04:00
|
|
|
|
|
|
|
window.$events.emit('error', this.autosaveFailText);
|
2020-07-05 16:18:17 -04:00
|
|
|
}
|
|
|
|
|
2022-04-20 13:21:21 -04:00
|
|
|
return didSave;
|
2020-07-05 16:18:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
draftNotifyChange(text) {
|
|
|
|
this.draftDisplay.innerText = text;
|
|
|
|
this.draftDisplayIcon.classList.add('visible');
|
|
|
|
window.setTimeout(() => {
|
|
|
|
this.draftDisplayIcon.classList.remove('visible');
|
|
|
|
}, 2000);
|
|
|
|
}
|
|
|
|
|
|
|
|
async discardDraft() {
|
|
|
|
let response;
|
|
|
|
try {
|
|
|
|
response = await window.$http.get(`/ajax/page/${this.pageId}`);
|
|
|
|
} catch (e) {
|
2023-04-19 10:20:04 -04:00
|
|
|
console.error(e);
|
|
|
|
return;
|
2020-07-05 16:18:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (this.autoSave.interval) {
|
|
|
|
window.clearInterval(this.autoSave.interval);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.draftDisplay.innerText = this.editingPageText;
|
|
|
|
this.toggleDiscardDraftVisibility(false);
|
2022-02-07 20:01:37 -05:00
|
|
|
window.$events.emit('editor::replace', {
|
|
|
|
html: response.data.html,
|
|
|
|
markdown: response.data.markdown,
|
|
|
|
});
|
2020-07-05 16:18:17 -04:00
|
|
|
|
|
|
|
this.titleElem.value = response.data.name;
|
|
|
|
window.setTimeout(() => {
|
|
|
|
this.startAutoSave();
|
|
|
|
}, 1000);
|
|
|
|
window.$events.emit('success', this.draftDiscardedText);
|
|
|
|
}
|
|
|
|
|
|
|
|
updateChangelogDisplay() {
|
|
|
|
let summary = this.changelogInput.value.trim();
|
|
|
|
if (summary.length === 0) {
|
|
|
|
summary = this.setChangelogText;
|
|
|
|
} else if (summary.length > 16) {
|
2023-04-18 17:20:02 -04:00
|
|
|
summary = `${summary.slice(0, 16)}...`;
|
2020-07-05 16:18:17 -04:00
|
|
|
}
|
|
|
|
this.changelogDisplay.innerText = summary;
|
|
|
|
}
|
|
|
|
|
|
|
|
toggleDiscardDraftVisibility(show) {
|
|
|
|
this.discardDraftWrap.classList.toggle('hidden', !show);
|
|
|
|
}
|
|
|
|
|
2022-04-20 13:21:21 -04:00
|
|
|
async changeEditor(event) {
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
const link = event.target.closest('a').href;
|
2023-04-18 17:20:02 -04:00
|
|
|
/** @var {ConfirmDialog} * */
|
2022-11-16 10:46:41 -05:00
|
|
|
const dialog = window.$components.firstOnElement(this.switchDialogContainer, 'confirm-dialog');
|
2022-04-20 13:21:21 -04:00
|
|
|
const [saved, confirmed] = await Promise.all([this.saveDraft(), dialog.show()]);
|
|
|
|
|
|
|
|
if (saved && confirmed) {
|
|
|
|
window.location = link;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-23 07:30:27 -05:00
|
|
|
/**
|
|
|
|
* @return MarkdownEditor|WysiwygEditor
|
|
|
|
*/
|
|
|
|
getEditorComponent() {
|
|
|
|
return window.$components.first('markdown-editor') || window.$components.first('wysiwyg-editor');
|
|
|
|
}
|
|
|
|
|
2020-07-05 16:18:17 -04:00
|
|
|
}
|