1
0
Fork 0
mirror of https://github.com/codex-team/codex.docs.git synced 2025-07-21 06:09:41 +02:00

Page showing, page edit, move API to /api/ (#10)

This commit is contained in:
Peter Savchenko 2018-10-15 22:06:01 +03:00 committed by GitHub
parent ff09f695d0
commit 730eff7995
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
18 changed files with 316 additions and 133 deletions

View file

@ -7,6 +7,12 @@
* @property {string} version - used Editor version
* @property {number} time - saving time
*/
/**
* @typedef {object} writingSettings
* @property {{_id, _parent, title, body: editorData}} [page] - page data for editing
*/
/**
* @class Writing
* @classdesc Class for create/edit pages
@ -17,6 +23,7 @@ export default class Writing {
*/
constructor() {
this.editor = null;
this.page = null; // stores Page on editing
this.nodes = {
editorWrapper: null,
saveButton: null,
@ -26,10 +33,10 @@ export default class Writing {
/**
* Called by ModuleDispatcher to initialize module from DOM
* @param {object} settings - module settings
* @param {writingSettings} settings - module settings
* @param {HTMLElement} moduleEl - module element
*/
init(settings, moduleEl) {
init(settings = {}, moduleEl) {
/**
* Create Editor
*/
@ -38,6 +45,10 @@ export default class Writing {
moduleEl.appendChild(this.nodes.editorWrapper);
if (settings.page){
this.page = settings.page;
}
this.loadEditor().then((editor) => {
this.editor = editor;
});
@ -59,7 +70,9 @@ export default class Writing {
async loadEditor() {
const {default: Editor} = await import(/* webpackChunkName: "editor" */ './../classes/editor');
return new Editor();
return new Editor({
initialData: this.page ? this.page.body : null
});
}
/**
@ -88,10 +101,11 @@ export default class Writing {
async saveButtonClicked() {
try {
const writingData = await this.getData();
const endpoint = this.page ? '/api/page/' + this.page._id : '/api/page';
try {
let response = await fetch('/page', {
method: 'PUT',
let response = await fetch(endpoint, {
method: this.page ? 'POST' : 'PUT',
headers: {
'Content-Type': 'application/json; charset=utf-8'
},