From dc8d58dd886ab684a354a292862aadd8e9940c9f Mon Sep 17 00:00:00 2001 From: Vitaliy Stoliarov Date: Wed, 8 Aug 2018 15:16:49 +0300 Subject: [PATCH] accumulate selected nodes --- src/editor.js | 8 ++++---- src/view/drag.js | 11 ++++++----- src/view/node.js | 12 +++++++----- 3 files changed, 17 insertions(+), 14 deletions(-) diff --git a/src/editor.js b/src/editor.js index aeeca1df..4446d23c 100644 --- a/src/editor.js +++ b/src/editor.js @@ -24,7 +24,7 @@ export class NodeEditor extends Context { window.addEventListener('keyup', e => this.trigger('keyup', e)); this.on('nodecreated', node => this.getComponent(node.name).created(node)); this.on('noderemoved', node => this.getComponent(node.name).destroyed(node)); - this.on('selectnode', node => this.selectNode(node)); + this.on('selectnode', ({ node, accumulate }) => this.selectNode(node, accumulate)); } addNode(node: Node) { @@ -72,13 +72,13 @@ export class NodeEditor extends Context { } selectNode(node: Node, accumulate: boolean = false) { - if (this.nodes.indexOf(node) === -1) + if (this.nodes.indexOf(node) === -1) throw new Error('Node not exist in list'); if (!this.trigger('nodeselect', node)) return; - - this.selected.add(node, accumulate); + this.selected.add(node, accumulate); + this.trigger('nodeselected', node); } diff --git a/src/view/drag.js b/src/view/drag.js index 88910635..11e461e4 100644 --- a/src/view/drag.js +++ b/src/view/drag.js @@ -33,7 +33,7 @@ export class Drag { e.stopPropagation(); this.mouseStart = this.getCoords(e); - this.onStart(); + this.onStart(e); } move(e) { @@ -45,12 +45,13 @@ export class Drag { let delta = [x - this.mouseStart[0], y - this.mouseStart[1]]; let zoom = this.el.getBoundingClientRect().width / this.el.offsetWidth; - this.onTranslate(delta[0] / zoom, delta[1] / zoom); + this.onTranslate(delta[0] / zoom, delta[1] / zoom, e); } - up() { + up(e) { + if (!this.mouseStart) return; + this.mouseStart = null; - - this.onDrag(); + this.onDrag(e); } } \ No newline at end of file diff --git a/src/view/node.js b/src/view/node.js index f8de1c9a..d21a37d3 100644 --- a/src/view/node.js +++ b/src/view/node.js @@ -45,9 +45,9 @@ export class Node extends Emitter { return this.sockets.get(io).getPosition(this.node); } - onSelect() { + onSelect(e) { this._startPosition = [...this.node.position]; - this.trigger('selectnode', this.node); + this.trigger('selectnode', { node: this.node, accumulate: e.ctrlKey }); } onTranslate(dx, dy) { @@ -63,11 +63,13 @@ export class Node extends Emitter { if (!this.trigger('nodetranslate', params)) return; - this.node.position[0] = params.x; - this.node.position[1] = params.y; + const prev = [...node.position]; + + node.position[0] = params.x; + node.position[1] = params.y; this.update(); - this.trigger('nodetranslated', { node }); + this.trigger('nodetranslated', { node, prev }); } update() {