2017-01-10 19:22:40 +01:00
|
|
|
/**
|
|
|
|
* Codex Editor Saver
|
|
|
|
*
|
|
|
|
* @author Codex Team
|
2017-01-11 19:29:04 +01:00
|
|
|
* @version 1.0.2
|
2017-01-10 19:22:40 +01:00
|
|
|
*/
|
|
|
|
|
2017-02-01 18:25:59 +01:00
|
|
|
module.exports = (function (saver) {
|
2016-12-07 19:25:31 +01:00
|
|
|
|
2017-02-13 18:54:18 +01:00
|
|
|
let editor = codex.editor;
|
|
|
|
|
2016-12-07 19:25:31 +01:00
|
|
|
/**
|
|
|
|
* Saves blocks
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
saver.saveBlocks = function () {
|
|
|
|
|
|
|
|
/** Save html content of redactor to memory */
|
2017-02-01 18:25:59 +01:00
|
|
|
editor.state.html = editor.nodes.redactor.innerHTML;
|
2016-12-07 19:25:31 +01:00
|
|
|
|
|
|
|
/** Empty jsonOutput state */
|
2017-02-01 18:25:59 +01:00
|
|
|
editor.state.jsonOutput = [];
|
2016-12-07 19:25:31 +01:00
|
|
|
|
|
|
|
Promise.resolve()
|
|
|
|
|
2017-02-01 18:25:59 +01:00
|
|
|
.then(function () {
|
|
|
|
|
|
|
|
return editor.nodes.redactor.childNodes;
|
|
|
|
|
2016-12-07 19:25:31 +01:00
|
|
|
})
|
|
|
|
/** Making a sequence from separate blocks */
|
2017-02-01 18:25:59 +01:00
|
|
|
.then(editor.saver.makeQueue)
|
2016-12-07 19:25:31 +01:00
|
|
|
|
2017-02-01 18:25:59 +01:00
|
|
|
.then(function () {
|
|
|
|
// editor.nodes.textarea.innerHTML = editor.state.html;
|
2016-12-07 19:25:31 +01:00
|
|
|
})
|
|
|
|
|
2017-02-01 18:25:59 +01:00
|
|
|
.catch( function (error) {
|
|
|
|
|
|
|
|
editor.core.log(error);
|
|
|
|
|
2016-12-07 19:25:31 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2017-02-01 18:25:59 +01:00
|
|
|
saver.makeQueue = function (blocks) {
|
2016-12-07 19:25:31 +01:00
|
|
|
|
|
|
|
var queue = Promise.resolve();
|
|
|
|
|
|
|
|
for(var index = 0; index < blocks.length; index++) {
|
|
|
|
|
|
|
|
/** Add node to sequence at specified index */
|
2017-02-01 18:25:59 +01:00
|
|
|
editor.saver.getBlockData(queue, blocks, index);
|
2016-12-07 19:25:31 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Gets every block and makes From Data */
|
2017-02-01 18:25:59 +01:00
|
|
|
saver.getBlockData = function (queue, blocks, index) {
|
|
|
|
|
|
|
|
queue.then(function () {
|
|
|
|
|
|
|
|
return editor.saver.getNodeAsync(blocks, index);
|
2016-12-07 19:25:31 +01:00
|
|
|
|
|
|
|
})
|
|
|
|
|
2017-02-01 18:25:59 +01:00
|
|
|
.then(editor.saver.makeFormDataFromBlocks);
|
2016-12-07 19:25:31 +01:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Asynchronously returns block data from blocksList by index
|
|
|
|
* @return Promise to node
|
|
|
|
*/
|
|
|
|
saver.getNodeAsync = function (blocksList, index) {
|
|
|
|
|
2017-02-01 18:25:59 +01:00
|
|
|
return Promise.resolve().then(function () {
|
2016-12-07 19:25:31 +01:00
|
|
|
|
|
|
|
return blocksList[index];
|
|
|
|
|
|
|
|
});
|
2017-02-01 18:25:59 +01:00
|
|
|
|
2016-12-07 19:25:31 +01:00
|
|
|
};
|
|
|
|
|
2017-02-01 18:25:59 +01:00
|
|
|
saver.makeFormDataFromBlocks = function (block) {
|
2016-12-07 19:25:31 +01:00
|
|
|
|
2017-03-03 16:39:56 +01:00
|
|
|
var pluginName = block.dataset.tool;
|
2016-12-07 19:25:31 +01:00
|
|
|
|
|
|
|
/** Check for plugin existance */
|
2017-02-01 18:25:59 +01:00
|
|
|
if (!editor.tools[pluginName]) {
|
|
|
|
|
2016-12-07 19:25:31 +01:00
|
|
|
throw Error(`Plugin «${pluginName}» not found`);
|
2017-02-01 18:25:59 +01:00
|
|
|
|
2016-12-07 19:25:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Check for plugin having render method */
|
2017-02-01 18:25:59 +01:00
|
|
|
if (typeof editor.tools[pluginName].save != 'function') {
|
2016-12-07 19:25:31 +01:00
|
|
|
|
|
|
|
throw Error(`Plugin «${pluginName}» must have save method`);
|
2017-02-01 18:25:59 +01:00
|
|
|
|
2016-12-07 19:25:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Result saver */
|
|
|
|
var blockContent = block.childNodes[0],
|
|
|
|
pluginsContent = blockContent.childNodes[0],
|
2017-02-05 15:50:48 +01:00
|
|
|
savedData,
|
|
|
|
position,
|
2017-03-03 16:39:56 +01:00
|
|
|
output;
|
2016-12-07 19:25:31 +01:00
|
|
|
|
2017-02-05 15:50:48 +01:00
|
|
|
/** If plugin wasn't available then return data from cache */
|
|
|
|
if ( editor.tools[pluginName].available === false ) {
|
2016-12-07 19:25:31 +01:00
|
|
|
|
2017-02-05 15:50:48 +01:00
|
|
|
position = pluginsContent.dataset.inputPosition;
|
|
|
|
savedData = codex.editor.state.blocks.items[position].data;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
savedData = editor.tools[pluginName].save(pluginsContent);
|
2016-12-07 19:25:31 +01:00
|
|
|
|
2017-02-05 15:50:48 +01:00
|
|
|
if (editor.tools[pluginName].validate) {
|
2017-02-01 18:25:59 +01:00
|
|
|
|
2017-02-05 15:50:48 +01:00
|
|
|
var result = editor.tools[pluginName].validate(savedData);
|
2017-01-18 16:12:50 +01:00
|
|
|
|
2017-02-05 15:50:48 +01:00
|
|
|
/**
|
|
|
|
* Do not allow invalid data
|
|
|
|
*/
|
|
|
|
if (!result)
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
2017-02-01 18:25:59 +01:00
|
|
|
|
2017-01-18 16:12:50 +01:00
|
|
|
}
|
2017-02-01 18:25:59 +01:00
|
|
|
|
2017-02-05 15:50:48 +01:00
|
|
|
output = {
|
2017-02-08 07:39:07 +01:00
|
|
|
type : pluginName,
|
|
|
|
data : savedData
|
2017-02-05 15:50:48 +01:00
|
|
|
};
|
|
|
|
|
2017-02-01 18:25:59 +01:00
|
|
|
editor.state.jsonOutput.push(output);
|
2016-12-07 19:25:31 +01:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
return saver;
|
|
|
|
|
2017-02-01 18:25:59 +01:00
|
|
|
})({});
|