2018-10-23 09:34:00 +02:00
|
|
|
/**
|
|
|
|
* Codex Editor Saver
|
|
|
|
*
|
|
|
|
* @module Saver
|
|
|
|
* @author Codex Team
|
|
|
|
* @version 2.0.0
|
|
|
|
*/
|
2018-11-23 17:11:50 +01:00
|
|
|
import Module from '../__module';
|
|
|
|
import {OutputData} from '../../../types';
|
2018-12-05 15:02:09 +01:00
|
|
|
import Block from '../block';
|
2018-10-23 09:34:00 +02:00
|
|
|
|
2018-11-23 17:11:50 +01:00
|
|
|
declare const VERSION: string;
|
2018-10-23 09:34:00 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @classdesc This method reduces all Blocks asyncronically and calls Block's save method to extract data
|
|
|
|
*
|
|
|
|
* @typedef {Saver} Saver
|
|
|
|
* @property {Element} html - Editor HTML content
|
|
|
|
* @property {String} json - Editor JSON output
|
|
|
|
*/
|
|
|
|
export default class Saver extends Module {
|
|
|
|
/**
|
|
|
|
* Composes new chain of Promises to fire them alternatelly
|
2018-11-23 17:11:50 +01:00
|
|
|
* @return {OutputData}
|
2018-10-23 09:34:00 +02:00
|
|
|
*/
|
2018-11-23 17:11:50 +01:00
|
|
|
public async save(): Promise<OutputData> {
|
2018-12-20 21:47:27 +01:00
|
|
|
const {BlockManager, Sanitizer, ModificationsObserver} = this.Editor;
|
|
|
|
const blocks = BlockManager.blocks,
|
2018-10-23 09:34:00 +02:00
|
|
|
chainData = [];
|
|
|
|
|
2018-12-20 21:47:27 +01:00
|
|
|
/**
|
|
|
|
* Disable modifications observe while saving
|
|
|
|
*/
|
|
|
|
ModificationsObserver.disable();
|
|
|
|
|
2018-12-05 15:02:09 +01:00
|
|
|
blocks.forEach((block: Block) => {
|
2018-11-30 19:57:30 +01:00
|
|
|
chainData.push(block.save());
|
2018-10-23 09:34:00 +02:00
|
|
|
});
|
|
|
|
|
2018-11-23 17:11:50 +01:00
|
|
|
const extractedData = await Promise.all(chainData);
|
2018-12-20 21:47:27 +01:00
|
|
|
const sanitizedData = await Sanitizer.sanitizeBlocks(extractedData);
|
|
|
|
|
|
|
|
ModificationsObserver.enable();
|
2018-11-30 19:57:30 +01:00
|
|
|
|
2018-11-23 17:11:50 +01:00
|
|
|
return this.makeOutput(sanitizedData);
|
2018-10-23 09:34:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates output object with saved data, time and version of editor
|
|
|
|
* @param {Object} allExtractedData
|
2018-11-23 17:11:50 +01:00
|
|
|
* @return {OutputData}
|
2018-10-23 09:34:00 +02:00
|
|
|
*/
|
2018-11-23 17:11:50 +01:00
|
|
|
private makeOutput(allExtractedData): OutputData {
|
2018-10-23 09:34:00 +02:00
|
|
|
let totalTime = 0;
|
|
|
|
const blocks = [];
|
|
|
|
|
|
|
|
console.groupCollapsed('[CodexEditor saving]:');
|
|
|
|
|
|
|
|
allExtractedData.forEach((extraction) => {
|
|
|
|
/** Group process info */
|
|
|
|
console.log(`«${extraction.tool}» saving info`, extraction);
|
|
|
|
totalTime += extraction.time;
|
2018-12-05 15:02:09 +01:00
|
|
|
|
|
|
|
/** If it was stub Block, get original data */
|
|
|
|
if (extraction.tool === this.Editor.Tools.stubTool) {
|
|
|
|
blocks.push(extraction.data);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-10-23 09:34:00 +02:00
|
|
|
blocks.push({
|
|
|
|
type: extraction.tool,
|
|
|
|
data: extraction.data,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
console.log('Total', totalTime);
|
|
|
|
console.groupEnd();
|
|
|
|
|
|
|
|
return {
|
|
|
|
time: +new Date(),
|
|
|
|
blocks,
|
2018-11-23 17:11:50 +01:00
|
|
|
version: VERSION,
|
2018-10-23 09:34:00 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|