Select target after drag end

This commit is contained in:
Paul Kaplan 2017-11-21 11:45:11 -05:00
parent 5fbfecb1a9
commit fc61f9e547
2 changed files with 58 additions and 4 deletions

View file

@ -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,16 +710,24 @@ 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) {
if (this._dragTarget) {
this._dragTarget.postSpriteInfo(data);
} else {
this.editingTarget.postSpriteInfo(data);
}
}
}
module.exports = VirtualMachine;

View file

@ -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();
});