From 057856a2d2aa1a1d234e7e1032aa09e57579ad62 Mon Sep 17 00:00:00 2001 From: Ian Gulliver Date: Thu, 11 Jul 2019 04:25:06 +0000 Subject: [PATCH] Auto-populate groups based on highlighted entries --- Editor.js | 14 ++++++++------ EditorEntryBase.js | 8 ++++---- EditorGroup.js | 10 ++++++++-- EditorNode.js | 6 +++++- List.js | 14 ++++++++++++++ 5 files changed, 39 insertions(+), 13 deletions(-) diff --git a/Editor.js b/Editor.js index 8fb3219..11f7594 100644 --- a/Editor.js +++ b/Editor.js @@ -60,15 +60,15 @@ class Editor extends List { return this.getEntries(type).length > limits[0]; } - addNodeAfter() { + addNodeAfter(...rest) { if (this.mayAdd(EditorNode)) { - EditorNode.addAfter(this.container_, this.getSelected()); + EditorNode.addAfter(this.container_, this.getSelected(), ...rest); } } - addNodeBefore() { + addNodeBefore(...rest) { if (this.mayAdd(EditorNode)) { - EditorNode.addBefore(this.container_, this.getSelected()); + EditorNode.addBefore(this.container_, this.getSelected(), ...rest); } } @@ -104,13 +104,15 @@ class Editor extends List { addGroupAfter() { if (this.mayAdd(EditorGroup)) { - EditorGroup.addAfter(this.container_, this.getSelected()); + EditorGroup.addAfter(this.container_, this.getSelected(), + this.queryEntries('.highlight', EditorNode)); } } addGroupBefore() { if (this.mayAdd(EditorGroup)) { - EditorGroup.addBefore(this.container_, this.getSelected()); + EditorGroup.addBefore(this.container_, this.getSelected(), + this.queryEntries('.highlight', EditorNode)); } } diff --git a/EditorEntryBase.js b/EditorEntryBase.js index 769dde4..f9b6162 100644 --- a/EditorEntryBase.js +++ b/EditorEntryBase.js @@ -47,14 +47,14 @@ class EditorEntryBase extends ListenUtils { afterDomAdd() { } - static addBefore(container, elem) { - let entry = new this(); + static addBefore(container, elem, ...rest) { + let entry = new this(...rest); container.insertBefore(entry.getElement(), elem); entry.afterDomAdd(); } - static addAfter(container, elem) { - let entry = new this(); + static addAfter(container, elem, ...rest) { + let entry = new this(...rest); container.insertBefore(entry.getElement(), elem ? elem.nextSibling : null); entry.afterDomAdd(); } diff --git a/EditorGroup.js b/EditorGroup.js index 9a9dd09..387f090 100644 --- a/EditorGroup.js +++ b/EditorGroup.js @@ -1,5 +1,5 @@ class EditorGroup extends EditorEntryBase { - constructor() { + constructor(entries) { super(); this.elem_.innerText = '□'; @@ -10,7 +10,13 @@ class EditorGroup extends EditorEntryBase { [EditorNode, [1, Number.POSITIVE_INFINITY]], [EditorLabel, [0, 1]], ]); - this.nodes_.addNodeAfter(); + if (entries && entries.length) { + for (let entry of entries) { + this.nodes_.addNodeAfter(entry.getLabel()); + } + } else { + this.nodes_.addNodeAfter(); + } this.elem_.appendChild(nodeList); } diff --git a/EditorNode.js b/EditorNode.js index cfec01b..c6492a2 100644 --- a/EditorNode.js +++ b/EditorNode.js @@ -1,5 +1,5 @@ class EditorNode extends EditorEntryBase { - constructor() { + constructor(label) { super(); this.elem_.classList.add('node'); @@ -10,6 +10,10 @@ class EditorNode extends EditorEntryBase { this.listen(this.input_, 'keydown', (e) => this.onInputKeyDown(e)); this.listen(this.input_, 'input', (e) => this.onInput()); this.elem_.appendChild(this.input_); + + if (label) { + this.setLabel(label); + } } afterDomAdd() { diff --git a/List.js b/List.js index 5b34c1c..bd53134 100644 --- a/List.js +++ b/List.js @@ -14,6 +14,20 @@ class List { return ret; } + queryEntries(query, type) { + let ret = []; + for (let elem of this.container_.querySelectorAll(query)) { + if (!elem.xArchObj) { + continue; + } + if (type && !(elem.xArchObj instanceof type)) { + continue; + } + ret.push(elem.xArchObj); + } + return ret; + } + getSelected() { let iter = document.activeElement; while (iter) {