From fc61f9e547820a92a4346cf7b3cc96f161c21697 Mon Sep 17 00:00:00 2001 From: Paul Kaplan Date: Tue, 21 Nov 2017 11:45:11 -0500 Subject: [PATCH] Select target after drag end --- src/virtual-machine.js | 23 +++++++++++++++++---- test/unit/virtual-machine.js | 39 ++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 4 deletions(-) diff --git a/src/virtual-machine.js b/src/virtual-machine.js index 994de9860..263248b66 100644 --- a/src/virtual-machine.js +++ b/src/virtual-machine.js @@ -36,6 +36,13 @@ class VirtualMachine extends EventEmitter { * @type {Target} */ this.editingTarget = null; + + /** + * The currently dragging target, for redirecting IO data. + * @type {Target} + */ + this._dragTarget = null; + // Runtime emits are passed along as VM emits. this.runtime.on(Runtime.SCRIPT_GLOW_ON, glowData => { this.emit(Runtime.SCRIPT_GLOW_ON, glowData); @@ -692,8 +699,8 @@ class VirtualMachine extends EventEmitter { startDrag (targetId) { const target = this.runtime.getTargetById(targetId); if (target) { + this._dragTarget = target; target.startDrag(); - this.setEditingTarget(target.id); } } @@ -703,15 +710,23 @@ class VirtualMachine extends EventEmitter { */ stopDrag (targetId) { const target = this.runtime.getTargetById(targetId); - if (target) target.stopDrag(); + if (target) { + this._dragTarget = null; + target.stopDrag(); + this.setEditingTarget(target.id); + } } /** - * Post/edit sprite info for the current editing target. + * Post/edit sprite info for the current editing target or the drag target. * @param {object} data An object with sprite info data to set. */ postSpriteInfo (data) { - this.editingTarget.postSpriteInfo(data); + if (this._dragTarget) { + this._dragTarget.postSpriteInfo(data); + } else { + this.editingTarget.postSpriteInfo(data); + } } } diff --git a/test/unit/virtual-machine.js b/test/unit/virtual-machine.js index f0887039e..40bd6bb54 100644 --- a/test/unit/virtual-machine.js +++ b/test/unit/virtual-machine.js @@ -316,3 +316,42 @@ test('emitWorkspaceUpdate', t => { t.notEqual(xml.indexOf('blocks'), -1); t.end(); }); + +test('drag IO redirect', t => { + const vm = new VirtualMachine(); + const sprite1Info = []; + const sprite2Info = []; + vm.runtime.targets = [ + { + id: 'sprite1', + postSpriteInfo: data => sprite1Info.push(data) + }, { + id: 'sprite2', + postSpriteInfo: data => sprite2Info.push(data), + startDrag: () => {}, + stopDrag: () => {} + } + ]; + vm.editingTarget = vm.runtime.targets[0]; + // Stub emitWorkspace/TargetsUpdate, it needs data we don't care about here + vm.emitWorkspaceUpdate = () => null; + vm.emitTargetsUpdate = () => null; + + // postSpriteInfo should go to the editing target by default`` + vm.postSpriteInfo('sprite1 info'); + t.equal(sprite1Info[0], 'sprite1 info'); + + // postSprite info goes to the drag target if it exists + vm.startDrag('sprite2'); + vm.postSpriteInfo('sprite2 info'); + t.equal(sprite2Info[0], 'sprite2 info'); + + // stop drag should set the editing target + vm.stopDrag('sprite2'); + t.equal(vm.editingTarget.id, 'sprite2'); + + // Then postSpriteInfo should continue posting to the new editing target + vm.postSpriteInfo('sprite2 info 2'); + t.equal(sprite2Info[1], 'sprite2 info 2'); + t.end(); +});