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

make block position definable in the list of blocks #185 #270

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]);
}
}
};