Skip to content

Commit e3897e0

Browse files
fix: patch WorkspaceSvg.getGesture to handle keyboard drags (#400)
* fix: patch WorkspaceSvg.getGesture to handle keyboard drags * chore: fix typo
1 parent 6b98bde commit e3897e0

File tree

1 file changed

+30
-9
lines changed

1 file changed

+30
-9
lines changed

src/actions/mover.ts

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66

7-
import type {BlockSvg, IDragger, IDragStrategy} from 'blockly';
7+
import type {BlockSvg, IDragger, IDragStrategy, Gesture} from 'blockly';
88
import {
99
ASTNode,
1010
common,
@@ -43,6 +43,12 @@ export class Mover {
4343
*/
4444
oldIsDragging: (() => boolean) | null = null;
4545

46+
/**
47+
* The stashed getGesture function, which is replaced at the beginning
48+
* of a keyboard drag and reset at the end of a keyboard drag.
49+
*/
50+
oldGetGesture: ((e: PointerEvent) => Gesture | null) | null = null;
51+
4652
/**
4753
* The block's base drag strategy, which will be overridden during
4854
* keyboard drags and reset at the end of the drag.
@@ -100,7 +106,7 @@ export class Mover {
100106
common.setSelected(block);
101107
cursor.setCurNode(ASTNode.createBlockNode(block));
102108

103-
this.patchIsDragging(workspace);
109+
this.patchWorkspace(workspace);
104110
this.patchDragStrategy(block);
105111
// Begin dragging block.
106112
const DraggerClass = registry.getClassFromOptions(
@@ -136,7 +142,7 @@ export class Mover {
136142
new utils.Coordinate(0, 0),
137143
);
138144

139-
this.unpatchIsDragging(workspace);
145+
this.unpatchWorkspace(workspace);
140146
this.unpatchDragStrategy(info.block);
141147
this.moves.delete(workspace);
142148
return true;
@@ -167,7 +173,7 @@ export class Mover {
167173
new utils.Coordinate(0, 0),
168174
);
169175

170-
this.unpatchIsDragging(workspace);
176+
this.unpatchWorkspace(workspace);
171177
this.unpatchDragStrategy(info.block);
172178
this.moves.delete(workspace);
173179
return true;
@@ -243,27 +249,42 @@ export class Mover {
243249
}
244250

245251
/**
246-
* Monkeypatch over workspace.isDragging to return whether a keyboard
247-
* drag is in progress.
252+
* Monkeypatch over workspace functions to consider keyboard drags as
253+
* well as mouse/pointer drags.
248254
*
249255
* @param workspace The workspace to patch.
250256
*/
251-
private patchIsDragging(workspace: WorkspaceSvg) {
257+
private patchWorkspace(workspace: WorkspaceSvg) {
258+
// Keyboard drags are real drags.
252259
this.oldIsDragging = workspace.isDragging;
253260
// eslint-disable-next-line @typescript-eslint/no-explicit-any
254261
(workspace as any).isDragging = () => this.isMoving(workspace);
262+
263+
// Ignore mouse/pointer events during keyboard drags.
264+
this.oldGetGesture = workspace.getGesture;
265+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
266+
(workspace as any).getGesture = (e: PointerEvent) => {
267+
// Normally these would be called from Gesture.doStart.
268+
e.preventDefault();
269+
e.stopPropagation();
270+
return null;
271+
};
255272
}
256273

257274
/**
258-
* Remove the monkeypatch on workspace.isDragging.
275+
* Remove monkeypatches on the workspace.
259276
*
260277
* @param workspace The workspace to unpatch.
261278
*/
262-
private unpatchIsDragging(workspace: WorkspaceSvg) {
279+
private unpatchWorkspace(workspace: WorkspaceSvg) {
263280
if (this.oldIsDragging) {
264281
// eslint-disable-next-line @typescript-eslint/no-explicit-any
265282
(workspace as any).isDragging = this.oldIsDragging;
266283
}
284+
if (this.oldGetGesture) {
285+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
286+
(workspace as any).getGesture = this.oldGetGesture;
287+
}
267288
}
268289
/**
269290
* Monkeypatch: replace the block's drag strategy and cache the old value.

0 commit comments

Comments
 (0)