2018-09-19 01:47:32 +03:00
|
|
|
/**
|
|
|
|
* Module for pages create/edit
|
|
|
|
*/
|
2018-10-04 22:08:21 +03:00
|
|
|
/**
|
|
|
|
* @typedef {object} editorData
|
|
|
|
* @property {{type, data}[]} blocks - array of Blocks
|
|
|
|
* @property {string} version - used Editor version
|
|
|
|
* @property {number} time - saving time
|
|
|
|
*/
|
|
|
|
|
2018-10-03 12:26:41 +03:00
|
|
|
export default class Writing {
|
2018-10-04 22:08:21 +03:00
|
|
|
/**
|
|
|
|
* Creates base properties
|
|
|
|
*/
|
2018-09-19 01:47:32 +03:00
|
|
|
constructor(){
|
2018-10-03 12:26:41 +03:00
|
|
|
this.editor = null;
|
2018-10-04 22:08:21 +03:00
|
|
|
this.nodes = {
|
|
|
|
editorWrapper: null,
|
|
|
|
saveButton: null,
|
|
|
|
parentIdSelector: null,
|
|
|
|
}
|
2018-09-19 01:47:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called by ModuleDispatcher to initialize module from DOM
|
2018-10-04 22:08:21 +03:00
|
|
|
* @param {object} settings - module settings
|
|
|
|
* @param {HTMLElement} moduleEl - module element
|
2018-09-19 01:47:32 +03:00
|
|
|
*/
|
2018-10-04 22:08:21 +03:00
|
|
|
init(settings, moduleEl) {
|
|
|
|
/**
|
|
|
|
* Create Editor
|
|
|
|
*/
|
|
|
|
this.nodes.editorWrapper = document.createElement('div');
|
|
|
|
this.nodes.editorWrapper.id = 'codex-editor';
|
2018-09-19 01:47:32 +03:00
|
|
|
|
2018-10-04 22:08:21 +03:00
|
|
|
moduleEl.appendChild(this.nodes.editorWrapper);
|
2018-09-19 01:47:32 +03:00
|
|
|
|
2018-10-03 12:26:41 +03:00
|
|
|
this.loadEditor().then((editor) => {
|
|
|
|
this.editor = editor;
|
2018-10-04 22:08:21 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Activate form elements
|
|
|
|
*/
|
|
|
|
this.nodes.saveButton = moduleEl.querySelector('[name="js-submit"]');
|
|
|
|
this.nodes.saveButton.addEventListener('click', () => {
|
|
|
|
this.saveButtonClicked();
|
|
|
|
});
|
|
|
|
this.nodes.parentIdSelector = moduleEl.querySelector('[name="parent"]');
|
2018-09-19 01:47:32 +03:00
|
|
|
};
|
|
|
|
|
2018-10-04 22:08:21 +03:00
|
|
|
/**
|
|
|
|
* Loads class for working with Editor
|
|
|
|
* @return {Promise<Editor>}
|
|
|
|
*/
|
2018-10-03 12:26:41 +03:00
|
|
|
async loadEditor(){
|
|
|
|
const {default: Editor} = await import(/* webpackChunkName: "editor" */ './../classes/editor');
|
|
|
|
|
|
|
|
return new Editor();
|
2018-09-19 01:47:32 +03:00
|
|
|
}
|
2018-10-04 22:08:21 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns all writing form data
|
|
|
|
* @throws {Error} - validation error
|
|
|
|
* @return {Promise.<{parent: string, body: {editorData}}>}
|
|
|
|
*/
|
|
|
|
async getData(){
|
|
|
|
const editorData = await this.editor.save();
|
|
|
|
const firstBlock = editorData.blocks.length ? editorData.blocks[0] : null;
|
|
|
|
const title = firstBlock && firstBlock.type === 'header' ? firstBlock.data.text : null;
|
|
|
|
|
|
|
|
if (!title) {
|
|
|
|
throw new Error('Entry should start with Header');
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
parent: this.nodes.parentIdSelector.value,
|
|
|
|
body: editorData
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handler for clicks on the Save button
|
|
|
|
*/
|
|
|
|
async saveButtonClicked(){
|
|
|
|
try {
|
|
|
|
const writingData = await this.getData();
|
|
|
|
|
|
|
|
try {
|
|
|
|
let response = await fetch('/page', {
|
|
|
|
method: 'PUT',
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json; charset=utf-8",
|
|
|
|
},
|
|
|
|
body: JSON.stringify(writingData),
|
|
|
|
});
|
|
|
|
|
|
|
|
response = await response.json();
|
|
|
|
|
|
|
|
if (response.success){
|
|
|
|
document.location = '/page/' + response.result._id;
|
|
|
|
} else {
|
|
|
|
alert(response.error);
|
|
|
|
console.log('Validation failed:', response.error);
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (sendingError) {
|
|
|
|
console.log('Saving request failed:', sendingError);
|
|
|
|
}
|
|
|
|
} catch (savingError){
|
|
|
|
alert(savingError);
|
|
|
|
console.log('Saving error: ', savingError);
|
|
|
|
}
|
|
|
|
}
|
2018-10-03 12:26:41 +03:00
|
|
|
}
|