Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Commit

Permalink
make block position definable in the list of blocks #185
Browse files Browse the repository at this point in the history
Signed-off-by: PatricB <lspeck@gmx.de>
  • Loading branch information
PatricB committed Nov 9, 2020
1 parent 1d19e9c commit 168f26c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
5 changes: 2 additions & 3 deletions src/components/Editor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
25 changes: 22 additions & 3 deletions src/components/Plugins.js
Original file line number Diff line number Diff line change
@@ -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 = {
Expand All @@ -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]);
}
}
};

0 comments on commit 168f26c

Please sign in to comment.