* Insert data passed as application/x-editor-js JSON * * @param {Array} blocks — Blocks' data to insert * @returns {void}
(blocks: Pick<SavedData, 'id' | 'data' | 'tool'>[])
| 868 | * @returns {void} |
| 869 | */ |
| 870 | private insertEditorJSData(blocks: Pick<SavedData, 'id' | 'data' | 'tool'>[]): void { |
| 871 | const { BlockManager, Caret, Tools } = this.Editor; |
| 872 | const sanitizedBlocks = sanitizeBlocks(blocks, (name) => |
| 873 | Tools.blockTools.get(name).sanitizeConfig |
| 874 | ); |
| 875 | |
| 876 | sanitizedBlocks.forEach(({ tool, data }, i) => { |
| 877 | let needToReplaceCurrentBlock = false; |
| 878 | |
| 879 | if (i === 0) { |
| 880 | const isCurrentBlockDefault = BlockManager.currentBlock && BlockManager.currentBlock.tool.isDefault; |
| 881 | |
| 882 | needToReplaceCurrentBlock = isCurrentBlockDefault && BlockManager.currentBlock.isEmpty; |
| 883 | } |
| 884 | |
| 885 | const block = BlockManager.insert({ |
| 886 | tool, |
| 887 | data, |
| 888 | replace: needToReplaceCurrentBlock, |
| 889 | }); |
| 890 | |
| 891 | Caret.setToBlock(block, Caret.positions.END); |
| 892 | }); |
| 893 | } |
| 894 | |
| 895 | /** |
| 896 | * Fetch nodes from Element node |
no test coverage detected