From 168f26c86285b3005f6ed75d2699fb5c22733318 Mon Sep 17 00:00:00 2001 From: PatricB Date: Mon, 9 Nov 2020 03:05:50 +0100 Subject: [PATCH] make block position definable in the list of blocks #185 Signed-off-by: PatricB --- src/components/Editor.vue | 5 ++--- src/components/Plugins.js | 25 ++++++++++++++++++++++--- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/src/components/Editor.vue b/src/components/Editor.vue index 9984cb1..3642cf9 100755 --- a/src/components/Editor.vue +++ b/src/components/Editor.vue @@ -102,9 +102,8 @@ export default { }, beforeCreate() { - Object.keys(window.editor.blocks).forEach(key => { - - const block = window.editor.blocks[key]; + Object.keys(window.editor.blocks).forEach(index => { + const [key, block] = window.editor.blocks[index]; if (block.extends && window.editor.blocks[block.extends]) { block.extends = window.editor.blocks[block.extends]; diff --git a/src/components/Plugins.js b/src/components/Plugins.js index dfc79b0..3ed8861 100755 --- a/src/components/Plugins.js +++ b/src/components/Plugins.js @@ -1,10 +1,11 @@ window.editor = { - blocks: {}, - block(type, params) { + blocks: [], + block(type, params, position) { const defaults = { type: type, icon: "page", }; + const positionMatch = /(after|before):(\S+)/.exec(position); // extend the params with the defaults params = { @@ -21,6 +22,24 @@ window.editor = { placeholder: params.placeholder, }; - this.blocks[type] = params; + // add block to blocks array + if (position && positionMatch !== null && this.blocks.findIndex(b => b[0] === positionMatch[2]) !== -1) { + // add after/before a given type if position parameter is given and sibling block isset + // find position of siblings type + let siblingsIndex = this.blocks.findIndex(b => b[0] === positionMatch[2]); + + // add new block before/after sibling + switch (positionMatch[1]) { + case 'before': + this.blocks.splice(siblingsIndex, 0, [type,params]) + break; + case 'after': + default: + this.blocks.splice(siblingsIndex + 1, 0, [type,params]) + } + } else { + // add block add the end of blocks + this.blocks.push([type, params]); + } } };