Skip to content

Commit 720541e

Browse files
committed
fix: End a keyboard move if the user moves focus off the block
1 parent a0bf76f commit 720541e

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

src/actions/mover.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,16 +125,21 @@ export class Mover {
125125
);
126126
if (!DraggerClass) throw new Error('no Dragger registered');
127127
const dragger = new DraggerClass(block, workspace);
128+
// Set up a blur listener to end the move if the user clicks away
129+
const blurListener = () => {
130+
this.finishMove(workspace);
131+
}
128132
// Record that a move is in progress and start dragging.
129133
workspace.setKeyboardMoveInProgress(true);
130-
const info = new MoveInfo(block, dragger);
134+
const info = new MoveInfo(block, dragger, blurListener);
131135
this.moves.set(workspace, info);
132136
// Begin drag.
133137
dragger.onDragStart(info.fakePointerEvent('pointerdown'));
134138
info.updateTotalDelta();
135139
// In case the block is detached, ensure that it still retains focus
136140
// (otherwise dragging will break).
137141
getFocusManager().focusNode(block);
142+
block.getFocusableElement().addEventListener('blur', blurListener)
138143
return true;
139144
}
140145

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

160+
// Remove the blur listener before ending the drag
161+
info.block.getFocusableElement().removeEventListener('blur', info.blurListener);
162+
155163
info.dragger.onDragEnd(
156164
info.fakePointerEvent('pointerup'),
157165
new utils.Coordinate(0, 0),
@@ -341,6 +349,7 @@ export class MoveInfo {
341349
constructor(
342350
readonly block: BlockSvg,
343351
readonly dragger: IDragger,
352+
readonly blurListener: EventListener,
344353
) {
345354
this.parentNext = block.previousConnection?.targetConnection ?? null;
346355
this.parentInput = block.outputConnection?.targetConnection ?? null;

0 commit comments

Comments
 (0)