2018-04-01 09:10:12 -04:00
|
|
|
import * as Dates from "../services/dates";
|
2017-09-30 08:26:48 -04:00
|
|
|
|
|
|
|
let autoSaveFrequency = 30;
|
|
|
|
|
|
|
|
let autoSave = false;
|
|
|
|
let draftErroring = false;
|
|
|
|
|
|
|
|
let currentContent = {
|
|
|
|
title: false,
|
|
|
|
html: false
|
|
|
|
};
|
|
|
|
|
|
|
|
let lastSave = 0;
|
|
|
|
|
|
|
|
function mounted() {
|
|
|
|
let elem = this.$el;
|
|
|
|
this.draftsEnabled = elem.getAttribute('drafts-enabled') === 'true';
|
|
|
|
this.editorType = elem.getAttribute('editor-type');
|
|
|
|
this.pageId= Number(elem.getAttribute('page-id'));
|
|
|
|
this.isNewDraft = Number(elem.getAttribute('page-new-draft')) === 1;
|
|
|
|
this.isUpdateDraft = Number(elem.getAttribute('page-update-draft')) === 1;
|
|
|
|
|
|
|
|
if (this.pageId !== 0 && this.draftsEnabled) {
|
|
|
|
window.setTimeout(() => {
|
|
|
|
this.startAutoSave();
|
|
|
|
}, 1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.isUpdateDraft || this.isNewDraft) {
|
|
|
|
this.draftText = trans('entities.pages_editing_draft');
|
|
|
|
} else {
|
|
|
|
this.draftText = trans('entities.pages_editing_page');
|
|
|
|
}
|
|
|
|
|
2017-12-07 13:44:20 -05:00
|
|
|
// Listen to save events from editor
|
2017-09-30 08:26:48 -04:00
|
|
|
window.$events.listen('editor-save-draft', this.saveDraft);
|
2017-12-07 13:44:20 -05:00
|
|
|
window.$events.listen('editor-save-page', this.savePage);
|
2017-09-30 08:26:48 -04:00
|
|
|
|
|
|
|
// Listen to content changes from the editor
|
|
|
|
window.$events.listen('editor-html-change', html => {
|
|
|
|
this.editorHTML = html;
|
|
|
|
});
|
|
|
|
window.$events.listen('editor-markdown-change', markdown => {
|
|
|
|
this.editorMarkdown = markdown;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
let data = {
|
|
|
|
draftsEnabled: false,
|
|
|
|
editorType: 'wysiwyg',
|
|
|
|
pagedId: 0,
|
|
|
|
isNewDraft: false,
|
|
|
|
isUpdateDraft: false,
|
|
|
|
|
|
|
|
draftText: '',
|
|
|
|
draftUpdated : false,
|
|
|
|
changeSummary: '',
|
|
|
|
|
|
|
|
editorHTML: '',
|
|
|
|
editorMarkdown: '',
|
|
|
|
};
|
|
|
|
|
|
|
|
let methods = {
|
|
|
|
|
|
|
|
startAutoSave() {
|
|
|
|
currentContent.title = document.getElementById('name').value.trim();
|
|
|
|
currentContent.html = this.editorHTML;
|
|
|
|
|
|
|
|
autoSave = window.setInterval(() => {
|
|
|
|
// Return if manually saved recently to prevent bombarding the server
|
|
|
|
if (Date.now() - lastSave < (1000 * autoSaveFrequency)/2) return;
|
2019-06-16 06:55:01 -04:00
|
|
|
const newTitle = document.getElementById('name').value.trim();
|
|
|
|
const newHtml = this.editorHTML;
|
2017-09-30 08:26:48 -04:00
|
|
|
|
|
|
|
if (newTitle !== currentContent.title || newHtml !== currentContent.html) {
|
|
|
|
currentContent.html = newHtml;
|
|
|
|
currentContent.title = newTitle;
|
|
|
|
this.saveDraft();
|
|
|
|
}
|
|
|
|
|
|
|
|
}, 1000 * autoSaveFrequency);
|
|
|
|
},
|
|
|
|
|
|
|
|
saveDraft() {
|
|
|
|
if (!this.draftsEnabled) return;
|
|
|
|
|
2019-06-16 06:55:01 -04:00
|
|
|
const data = {
|
2017-09-30 08:26:48 -04:00
|
|
|
name: document.getElementById('name').value.trim(),
|
|
|
|
html: this.editorHTML
|
|
|
|
};
|
|
|
|
|
|
|
|
if (this.editorType === 'markdown') data.markdown = this.editorMarkdown;
|
|
|
|
|
2019-06-16 06:55:01 -04:00
|
|
|
const url = window.baseUrl(`/ajax/page/${this.pageId}/save-draft`);
|
2017-09-30 08:26:48 -04:00
|
|
|
window.$http.put(url, data).then(response => {
|
|
|
|
draftErroring = false;
|
2017-10-01 13:11:24 -04:00
|
|
|
if (!this.isNewDraft) this.isUpdateDraft = true;
|
2019-06-16 06:55:01 -04:00
|
|
|
this.draftNotifyChange(`${response.data.message} ${Dates.utcTimeStampToLocalTime(response.data.timestamp)}`);
|
2017-09-30 08:26:48 -04:00
|
|
|
lastSave = Date.now();
|
|
|
|
}, errorRes => {
|
|
|
|
if (draftErroring) return;
|
2018-05-19 12:01:33 -04:00
|
|
|
window.$events.emit('error', trans('errors.page_draft_autosave_fail'));
|
2017-09-30 08:26:48 -04:00
|
|
|
draftErroring = true;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2017-12-07 13:44:20 -05:00
|
|
|
savePage() {
|
|
|
|
this.$el.closest('form').submit();
|
|
|
|
},
|
2017-09-30 08:26:48 -04:00
|
|
|
|
|
|
|
draftNotifyChange(text) {
|
|
|
|
this.draftText = text;
|
|
|
|
this.draftUpdated = true;
|
|
|
|
window.setTimeout(() => {
|
|
|
|
this.draftUpdated = false;
|
|
|
|
}, 2000);
|
|
|
|
},
|
|
|
|
|
|
|
|
discardDraft() {
|
|
|
|
let url = window.baseUrl(`/ajax/page/${this.pageId}`);
|
|
|
|
window.$http.get(url).then(response => {
|
|
|
|
if (autoSave) window.clearInterval(autoSave);
|
|
|
|
|
|
|
|
this.draftText = trans('entities.pages_editing_page');
|
|
|
|
this.isUpdateDraft = false;
|
|
|
|
window.$events.emit('editor-html-update', response.data.html);
|
|
|
|
window.$events.emit('editor-markdown-update', response.data.markdown || response.data.html);
|
|
|
|
|
|
|
|
document.getElementById('name').value = response.data.name;
|
|
|
|
window.setTimeout(() => {
|
|
|
|
this.startAutoSave();
|
|
|
|
}, 1000);
|
|
|
|
window.$events.emit('success', trans('entities.pages_draft_discarded'));
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
let computed = {
|
|
|
|
changeSummaryShort() {
|
|
|
|
let len = this.changeSummary.length;
|
|
|
|
if (len === 0) return trans('entities.pages_edit_set_changelog');
|
|
|
|
if (len <= 16) return this.changeSummary;
|
|
|
|
return this.changeSummary.slice(0, 16) + '...';
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-11-09 16:17:35 -05:00
|
|
|
export default {
|
2017-09-30 08:26:48 -04:00
|
|
|
mounted, data, methods, computed,
|
|
|
|
};
|