Skip to content
Merged
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
13 changes: 12 additions & 1 deletion src/actions/mover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,16 +125,21 @@ export class Mover {
);
if (!DraggerClass) throw new Error('no Dragger registered');
const dragger = new DraggerClass(block, workspace);
// Set up a blur listener to end the move if the user clicks away
const blurListener = () => {
this.finishMove(workspace);
};
// Record that a move is in progress and start dragging.
workspace.setKeyboardMoveInProgress(true);
const info = new MoveInfo(block, dragger);
const info = new MoveInfo(block, dragger, blurListener);
this.moves.set(workspace, info);
// Begin drag.
dragger.onDragStart(info.fakePointerEvent('pointerdown'));
info.updateTotalDelta();
// In case the block is detached, ensure that it still retains focus
// (otherwise dragging will break).
getFocusManager().focusNode(block);
block.getFocusableElement().addEventListener('blur', blurListener);
return true;
}

Expand All @@ -152,6 +157,11 @@ export class Mover {
const info = this.moves.get(workspace);
if (!info) throw new Error('no move info for workspace');

// Remove the blur listener before ending the drag
info.block
.getFocusableElement()
.removeEventListener('blur', info.blurListener);

info.dragger.onDragEnd(
info.fakePointerEvent('pointerup'),
new utils.Coordinate(0, 0),
Expand Down Expand Up @@ -341,6 +351,7 @@ export class MoveInfo {
constructor(
readonly block: BlockSvg,
readonly dragger: IDragger,
readonly blurListener: EventListener,
) {
this.parentNext = block.previousConnection?.targetConnection ?? null;
this.parentInput = block.outputConnection?.targetConnection ?? null;
Expand Down
16 changes: 16 additions & 0 deletions test/webdriverio/test/basic_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import * as chai from 'chai';
import * as Blockly from 'blockly';
import {
isDragging,
setCurrentCursorNodeById,
setCurrentCursorNodeByIdAndFieldName,
getCurrentFocusNodeId,
Expand Down Expand Up @@ -247,6 +248,21 @@ suite('Keyboard navigation on Blocks', function () {
.expect(await getCurrentFocusedBlockId(this.browser))
.equal('controls_repeat_1');
});

test('Losing focus cancels move', async function () {
await tabNavigateToWorkspace(this.browser);
await this.browser.pause(PAUSE_TIME);
await setCurrentCursorNodeById(this.browser, 'text_print_1');
await this.browser.keys('m');
await this.browser.pause(PAUSE_TIME);

chai.assert.isTrue(await isDragging(this.browser));

await this.browser.keys(Key.Tab);
await this.browser.pause(PAUSE_TIME);

chai.assert.isFalse(await isDragging(this.browser));
});
});

// TODO(#499) These tests fail because focusing on a field doesn't update the cursor
Expand Down
14 changes: 14 additions & 0 deletions test/webdriverio/test/test_setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -356,3 +356,17 @@ export async function tabNavigateForward(browser: WebdriverIO.Browser) {
await browser.keys(webdriverio.Key.Tab);
await browser.pause(PAUSE_TIME);
}

/**
* Returns whether there's a drag in progress on the main workspace.
*
* @param browser The active WebdriverIO Browser object.
*/
export async function isDragging(
browser: WebdriverIO.Browser,
): Promise<boolean> {
return await browser.execute(() => {
const workspaceSvg = Blockly.getMainWorkspace() as Blockly.WorkspaceSvg;
return workspaceSvg.isDragging();
});
}
Loading