mirror of
https://github.com/codex-team/editor.js
synced 2026-03-16 15:45:47 +01:00
Saver module initial
This commit is contained in:
parent
c1721a8d20
commit
90c67ade02
2 changed files with 189 additions and 165 deletions
|
|
@ -1,165 +0,0 @@
|
|||
/**
|
||||
* Codex Editor Saver
|
||||
*
|
||||
* @author Codex Team
|
||||
* @version 1.1.0
|
||||
*/
|
||||
|
||||
module.exports = (function (saver) {
|
||||
|
||||
let editor = codex.editor;
|
||||
|
||||
/**
|
||||
* @public
|
||||
* Save blocks
|
||||
*/
|
||||
saver.save = function () {
|
||||
|
||||
/** Save html content of redactor to memory */
|
||||
editor.state.html = editor.nodes.redactor.innerHTML;
|
||||
|
||||
/** Clean jsonOutput state */
|
||||
editor.state.jsonOutput = [];
|
||||
|
||||
return saveBlocks(editor.nodes.redactor.childNodes);
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
* @private
|
||||
* Save each block data
|
||||
*
|
||||
* @param blocks
|
||||
* @returns {Promise.<TResult>}
|
||||
*/
|
||||
let saveBlocks = function (blocks) {
|
||||
|
||||
let data = [];
|
||||
|
||||
for(let index = 0; index < blocks.length; index++) {
|
||||
|
||||
data.push(getBlockData(blocks[index]));
|
||||
|
||||
}
|
||||
|
||||
return Promise.all(data)
|
||||
.then(makeOutput)
|
||||
.catch(editor.core.log);
|
||||
|
||||
};
|
||||
|
||||
/** Save and validate block data */
|
||||
let getBlockData = function (block) {
|
||||
|
||||
return saveBlockData(block)
|
||||
.then(validateBlockData)
|
||||
.catch(editor.core.log);
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
* @private
|
||||
* Call block`s plugin save method and return saved data
|
||||
*
|
||||
* @param block
|
||||
* @returns {Object}
|
||||
*/
|
||||
let saveBlockData = function (block) {
|
||||
|
||||
let pluginName = block.dataset.tool;
|
||||
|
||||
/** Check for plugin existence */
|
||||
if (!editor.tools[pluginName]) {
|
||||
|
||||
editor.core.log(`Plugin «${pluginName}» not found`, 'error');
|
||||
return {data: null, pluginName: null};
|
||||
|
||||
}
|
||||
|
||||
/** Check for plugin having save method */
|
||||
if (typeof editor.tools[pluginName].save !== 'function') {
|
||||
|
||||
editor.core.log(`Plugin «${pluginName}» must have save method`, 'error');
|
||||
return {data: null, pluginName: null};
|
||||
|
||||
}
|
||||
|
||||
/** Result saver */
|
||||
let blockContent = block.childNodes[0],
|
||||
pluginsContent = blockContent.childNodes[0],
|
||||
position = pluginsContent.dataset.inputPosition;
|
||||
|
||||
/** If plugin wasn't available then return data from cache */
|
||||
if ( editor.tools[pluginName].available === false ) {
|
||||
|
||||
return Promise.resolve({data: codex.editor.state.blocks.items[position].data, pluginName});
|
||||
|
||||
}
|
||||
|
||||
return Promise.resolve(pluginsContent)
|
||||
.then(editor.tools[pluginName].save)
|
||||
.then(data => Object({data, pluginName}));
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
* Call plugin`s validate method. Return false if validation failed
|
||||
*
|
||||
* @param data
|
||||
* @param pluginName
|
||||
* @returns {Object|Boolean}
|
||||
*/
|
||||
let validateBlockData = function ({data, pluginName}) {
|
||||
|
||||
if (!data || !pluginName) {
|
||||
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
if (editor.tools[pluginName].validate) {
|
||||
|
||||
let result = editor.tools[pluginName].validate(data);
|
||||
|
||||
/**
|
||||
* Do not allow invalid data
|
||||
*/
|
||||
if (!result) {
|
||||
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return {data, pluginName};
|
||||
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
* Compile article output
|
||||
*
|
||||
* @param savedData
|
||||
* @returns {{time: number, version, items: (*|Array)}}
|
||||
*/
|
||||
let makeOutput = function (savedData) {
|
||||
|
||||
savedData = savedData.filter(blockData => blockData);
|
||||
|
||||
let items = savedData.map(blockData => Object({type: blockData.pluginName, data: blockData.data}));
|
||||
|
||||
editor.state.jsonOutput = items;
|
||||
|
||||
return {
|
||||
id: editor.state.blocks.id || null,
|
||||
time: +new Date(),
|
||||
version: editor.version,
|
||||
items
|
||||
};
|
||||
|
||||
};
|
||||
|
||||
return saver;
|
||||
|
||||
})({});
|
||||
189
src/components/modules/saver.js
Normal file
189
src/components/modules/saver.js
Normal file
|
|
@ -0,0 +1,189 @@
|
|||
/**
|
||||
* Codex Editor Saver
|
||||
*
|
||||
* @author Codex Team
|
||||
* @version 2.0.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Saver class properties:
|
||||
* @property {DOMElement} html - Editor HTML content
|
||||
* @property {String} json - Editor JSON output
|
||||
*/
|
||||
export default class Saver extends Module {
|
||||
|
||||
/**
|
||||
* @constructor
|
||||
* @param config
|
||||
*/
|
||||
constructor(config) {
|
||||
super(config);
|
||||
|
||||
this.html = '';
|
||||
this.json = {};
|
||||
}
|
||||
|
||||
save() {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// module.exports = (function (saver) {
|
||||
//
|
||||
// let editor = codex.editor;
|
||||
//
|
||||
// /**
|
||||
// * @public
|
||||
// * Save blocks
|
||||
// */
|
||||
// saver.save = function () {
|
||||
//
|
||||
// /** Save html content of redactor to memory */
|
||||
// editor.state.html = editor.nodes.redactor.innerHTML;
|
||||
//
|
||||
// /** Clean jsonOutput state */
|
||||
// editor.state.jsonOutput = [];
|
||||
//
|
||||
// return saveBlocks(editor.nodes.redactor.childNodes);
|
||||
//
|
||||
// };
|
||||
//
|
||||
// /**
|
||||
// * @private
|
||||
// * Save each block data
|
||||
// *
|
||||
// * @param blocks
|
||||
// * @returns {Promise.<TResult>}
|
||||
// */
|
||||
// let saveBlocks = function (blocks) {
|
||||
//
|
||||
// let data = [];
|
||||
//
|
||||
// for(let index = 0; index < blocks.length; index++) {
|
||||
//
|
||||
// data.push(getBlockData(blocks[index]));
|
||||
//
|
||||
// }
|
||||
//
|
||||
// return Promise.all(data)
|
||||
// .then(makeOutput)
|
||||
// .catch(editor.core.log);
|
||||
//
|
||||
// };
|
||||
//
|
||||
// /** Save and validate block data */
|
||||
// let getBlockData = function (block) {
|
||||
//
|
||||
// return saveBlockData(block)
|
||||
// .then(validateBlockData)
|
||||
// .catch(editor.core.log);
|
||||
//
|
||||
// };
|
||||
//
|
||||
// /**
|
||||
// * @private
|
||||
// * Call block`s plugin save method and return saved data
|
||||
// *
|
||||
// * @param block
|
||||
// * @returns {Object}
|
||||
// */
|
||||
// let saveBlockData = function (block) {
|
||||
//
|
||||
// let pluginName = block.dataset.tool;
|
||||
//
|
||||
// /** Check for plugin existence */
|
||||
// if (!editor.tools[pluginName]) {
|
||||
//
|
||||
// editor.core.log(`Plugin «${pluginName}» not found`, 'error');
|
||||
// return {data: null, pluginName: null};
|
||||
//
|
||||
// }
|
||||
//
|
||||
// /** Check for plugin having save method */
|
||||
// if (typeof editor.tools[pluginName].save !== 'function') {
|
||||
//
|
||||
// editor.core.log(`Plugin «${pluginName}» must have save method`, 'error');
|
||||
// return {data: null, pluginName: null};
|
||||
//
|
||||
// }
|
||||
//
|
||||
// /** Result saver */
|
||||
// let blockContent = block.childNodes[0],
|
||||
// pluginsContent = blockContent.childNodes[0],
|
||||
// position = pluginsContent.dataset.inputPosition;
|
||||
//
|
||||
// /** If plugin wasn't available then return data from cache */
|
||||
// if ( editor.tools[pluginName].available === false ) {
|
||||
//
|
||||
// return Promise.resolve({data: codex.editor.state.blocks.items[position].data, pluginName});
|
||||
//
|
||||
// }
|
||||
//
|
||||
// return Promise.resolve(pluginsContent)
|
||||
// .then(editor.tools[pluginName].save)
|
||||
// .then(data => Object({data, pluginName}));
|
||||
//
|
||||
// };
|
||||
//
|
||||
// /**
|
||||
// * Call plugin`s validate method. Return false if validation failed
|
||||
// *
|
||||
// * @param data
|
||||
// * @param pluginName
|
||||
// * @returns {Object|Boolean}
|
||||
// */
|
||||
// let validateBlockData = function ({data, pluginName}) {
|
||||
//
|
||||
// if (!data || !pluginName) {
|
||||
//
|
||||
// return false;
|
||||
//
|
||||
// }
|
||||
//
|
||||
// if (editor.tools[pluginName].validate) {
|
||||
//
|
||||
// let result = editor.tools[pluginName].validate(data);
|
||||
//
|
||||
// /**
|
||||
// * Do not allow invalid data
|
||||
// */
|
||||
// if (!result) {
|
||||
//
|
||||
// return false;
|
||||
//
|
||||
// }
|
||||
//
|
||||
// }
|
||||
//
|
||||
// return {data, pluginName};
|
||||
//
|
||||
//
|
||||
// };
|
||||
//
|
||||
// /**
|
||||
// * Compile article output
|
||||
// *
|
||||
// * @param savedData
|
||||
// * @returns {{time: number, version, items: (*|Array)}}
|
||||
// */
|
||||
// let makeOutput = function (savedData) {
|
||||
//
|
||||
// savedData = savedData.filter(blockData => blockData);
|
||||
//
|
||||
// let items = savedData.map(blockData => Object({type: blockData.pluginName, data: blockData.data}));
|
||||
//
|
||||
// editor.state.jsonOutput = items;
|
||||
//
|
||||
// return {
|
||||
// id: editor.state.blocks.id || null,
|
||||
// time: +new Date(),
|
||||
// version: editor.version,
|
||||
// items
|
||||
// };
|
||||
//
|
||||
// };
|
||||
//
|
||||
// return saver;
|
||||
//
|
||||
// })({});
|
||||
Loading…
Add table
Add a link
Reference in a new issue