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

Commit 168f26c

Browse files
committed
make block position definable in the list of blocks #185
Signed-off-by: PatricB <lspeck@gmx.de>
1 parent 1d19e9c commit 168f26c

2 files changed

Lines changed: 24 additions & 6 deletions

File tree

src/components/Editor.vue

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,8 @@ export default {
102102
},
103103
beforeCreate() {
104104
105-
Object.keys(window.editor.blocks).forEach(key => {
106-
107-
const block = window.editor.blocks[key];
105+
Object.keys(window.editor.blocks).forEach(index => {
106+
const [key, block] = window.editor.blocks[index];
108107
109108
if (block.extends && window.editor.blocks[block.extends]) {
110109
block.extends = window.editor.blocks[block.extends];

src/components/Plugins.js

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
window.editor = {
2-
blocks: {},
3-
block(type, params) {
2+
blocks: [],
3+
block(type, params, position) {
44
const defaults = {
55
type: type,
66
icon: "page",
77
};
8+
const positionMatch = /(after|before):(\S+)/.exec(position);
89

910
// extend the params with the defaults
1011
params = {
@@ -21,6 +22,24 @@ window.editor = {
2122
placeholder: params.placeholder,
2223
};
2324

24-
this.blocks[type] = params;
25+
// add block to blocks array
26+
if (position && positionMatch !== null && this.blocks.findIndex(b => b[0] === positionMatch[2]) !== -1) {
27+
// add after/before a given type if position parameter is given and sibling block isset
28+
// find position of siblings type
29+
let siblingsIndex = this.blocks.findIndex(b => b[0] === positionMatch[2]);
30+
31+
// add new block before/after sibling
32+
switch (positionMatch[1]) {
33+
case 'before':
34+
this.blocks.splice(siblingsIndex, 0, [type,params])
35+
break;
36+
case 'after':
37+
default:
38+
this.blocks.splice(siblingsIndex + 1, 0, [type,params])
39+
}
40+
} else {
41+
// add block add the end of blocks
42+
this.blocks.push([type, params]);
43+
}
2544
}
2645
};

0 commit comments

Comments
 (0)