Skip to content

Commit a6115c2

Browse files
committed
refactor: Export KeyboardMover class with a static instance
1 parent c591750 commit a6115c2

5 files changed

Lines changed: 35 additions & 27 deletions

File tree

packages/blockly/core/bubbles/mini_workspace_bubble.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,9 @@ export class MiniWorkspaceBubble extends Bubble {
155155
*/
156156
private bumpBlocksIntoBounds() {
157157
// Only bump for mouse-driven drags.
158-
if (this.miniWorkspace.isDragging() && !KeyboardMover.isMoving()) return;
158+
if (this.miniWorkspace.isDragging() && !KeyboardMover.mover.isMoving()) {
159+
return;
160+
}
159161

160162
const MARGIN = 20;
161163

@@ -187,11 +189,13 @@ export class MiniWorkspaceBubble extends Bubble {
187189
* mini workspace.
188190
*/
189191
private updateBubbleSize() {
190-
if (this.miniWorkspace.isDragging() && !KeyboardMover.isMoving()) return;
192+
if (this.miniWorkspace.isDragging() && !KeyboardMover.mover.isMoving()) {
193+
return;
194+
}
191195

192196
// Disable autolayout if a keyboard move is in progress to prevent the
193197
// mutator bubble from jumping around.
194-
this.autoLayout &&= !KeyboardMover.isMoving();
198+
this.autoLayout &&= !KeyboardMover.mover.isMoving();
195199

196200
const currSize = this.getSize();
197201
const newSize = this.calculateWorkspaceSize();

packages/blockly/core/keyboard_nav/keyboard_mover.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ const COMMIT_MOVE_SHORTCUT = 'commitMove';
3232
* Class responsible for coordinating keyboard-driven moves with the workspace
3333
* and dragging system.
3434
*/
35-
class KeyboardMoverImplementation {
35+
export class KeyboardMover {
3636
/**
3737
* Object responsible for dragging workspace elements in response to move
3838
* commands.
@@ -70,6 +70,11 @@ class KeyboardMoverImplementation {
7070
this.abortMove();
7171
};
7272

73+
static mover = new KeyboardMover();
74+
75+
// Constructor is private to keep this class a singleton.
76+
private constructor() {}
77+
7378
/**
7479
* Returns true iff the given draggable is allowed to be moved.
7580
*
@@ -304,6 +309,3 @@ class KeyboardMoverImplementation {
304309
);
305310
}
306311
}
307-
308-
const KeyboardMover = new KeyboardMoverImplementation();
309-
export {KeyboardMover};

packages/blockly/core/shortcut_items.ts

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ export function registerMovementShortcuts() {
394394
name: 'start_move',
395395
preconditionFn: (workspace) => {
396396
const startDraggable = getCurrentDraggable(workspace);
397-
return !!startDraggable && KeyboardMover.canMove(startDraggable);
397+
return !!startDraggable && KeyboardMover.mover.canMove(startDraggable);
398398
},
399399
callback: (workspace, e) => {
400400
keyboardNavigationController.setIsActive(true);
@@ -406,31 +406,33 @@ export function registerMovementShortcuts() {
406406
}
407407
return (
408408
!!startDraggable &&
409-
KeyboardMover.startMove(startDraggable, e as KeyboardEvent)
409+
KeyboardMover.mover.startMove(startDraggable, e as KeyboardEvent)
410410
);
411411
},
412412
keyCodes: [KeyCodes.M],
413413
},
414414
{
415415
name: 'finish_move',
416-
preconditionFn: () => KeyboardMover.isMoving(),
417-
callback: (_workspace, e) => KeyboardMover.finishMove(e as KeyboardEvent),
416+
preconditionFn: () => KeyboardMover.mover.isMoving(),
417+
callback: (_workspace, e) =>
418+
KeyboardMover.mover.finishMove(e as KeyboardEvent),
418419
keyCodes: [KeyCodes.ENTER, KeyCodes.SPACE],
419420
allowCollision: true,
420421
},
421422
{
422423
name: 'abort_move',
423-
preconditionFn: () => KeyboardMover.isMoving(),
424-
callback: (_workspace, e) => KeyboardMover.abortMove(e as KeyboardEvent),
424+
preconditionFn: () => KeyboardMover.mover.isMoving(),
425+
callback: (_workspace, e) =>
426+
KeyboardMover.mover.abortMove(e as KeyboardEvent),
425427
keyCodes: [KeyCodes.ESC],
426428
allowCollision: true,
427429
},
428430
{
429431
name: 'move_left',
430-
preconditionFn: () => KeyboardMover.isMoving(),
432+
preconditionFn: () => KeyboardMover.mover.isMoving(),
431433
callback: (_workspace, e) => {
432434
e.preventDefault();
433-
return KeyboardMover.move(Direction.LEFT, e as KeyboardEvent);
435+
return KeyboardMover.mover.move(Direction.LEFT, e as KeyboardEvent);
434436
},
435437
keyCodes: [
436438
KeyCodes.LEFT,
@@ -445,10 +447,10 @@ export function registerMovementShortcuts() {
445447
},
446448
{
447449
name: 'move_right',
448-
preconditionFn: () => KeyboardMover.isMoving(),
450+
preconditionFn: () => KeyboardMover.mover.isMoving(),
449451
callback: (_workspace, e) => {
450452
e.preventDefault();
451-
return KeyboardMover.move(Direction.RIGHT, e as KeyboardEvent);
453+
return KeyboardMover.mover.move(Direction.RIGHT, e as KeyboardEvent);
452454
},
453455
keyCodes: [
454456
KeyCodes.RIGHT,
@@ -463,10 +465,10 @@ export function registerMovementShortcuts() {
463465
},
464466
{
465467
name: 'move_up',
466-
preconditionFn: () => KeyboardMover.isMoving(),
468+
preconditionFn: () => KeyboardMover.mover.isMoving(),
467469
callback: (_workspace, e) => {
468470
e.preventDefault();
469-
return KeyboardMover.move(Direction.UP, e as KeyboardEvent);
471+
return KeyboardMover.mover.move(Direction.UP, e as KeyboardEvent);
470472
},
471473
keyCodes: [
472474
KeyCodes.UP,
@@ -481,10 +483,10 @@ export function registerMovementShortcuts() {
481483
},
482484
{
483485
name: 'move_down',
484-
preconditionFn: () => KeyboardMover.isMoving(),
486+
preconditionFn: () => KeyboardMover.mover.isMoving(),
485487
callback: (_workspace, e) => {
486488
e.preventDefault();
487-
return KeyboardMover.move(Direction.DOWN, e as KeyboardEvent);
489+
return KeyboardMover.mover.move(Direction.DOWN, e as KeyboardEvent);
488490
},
489491
keyCodes: [
490492
KeyCodes.DOWN,

packages/blockly/core/workspace_svg.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1490,7 +1490,7 @@ export class WorkspaceSvg
14901490
*/
14911491
isDragging(): boolean {
14921492
return (
1493-
KeyboardMover.isMoving() ||
1493+
KeyboardMover.mover.isMoving() ||
14941494
(this.currentGesture_ !== null && this.currentGesture_.isDragging())
14951495
);
14961496
}
@@ -2449,7 +2449,7 @@ export class WorkspaceSvg
24492449
getGesture(e?: PointerEvent): Gesture | null {
24502450
// Ignore and cancel events that would start a gesture during a
24512451
// keyboard-driven move.
2452-
if (KeyboardMover.isMoving()) {
2452+
if (KeyboardMover.mover.isMoving()) {
24532453
e?.preventDefault();
24542454
e?.stopPropagation();
24552455
return null;

packages/blockly/tests/mocha/keyboard_movement_test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ suite('Keyboard-driven movement', function () {
2323
const toolbox = document.getElementById('toolbox-simple');
2424
this.workspace = Blockly.inject('blocklyDiv', {toolbox: toolbox});
2525
Blockly.common.defineBlocks(p5blocks);
26-
Blockly.KeyboardMover.setMoveDistance(20);
26+
Blockly.KeyboardMover.mover.setMoveDistance(20);
2727
});
2828

2929
teardown(function () {
@@ -320,7 +320,7 @@ suite('Keyboard-driven movement', function () {
320320
startMove(this.workspace);
321321
const steps = [100, 20, 0, -20, -100];
322322
for (const step of steps) {
323-
Blockly.KeyboardMover.setMoveDistance(step);
323+
Blockly.KeyboardMover.mover.setMoveDistance(step);
324324
const oldLeft = this.element.getBoundingRectangle().left;
325325
moveRight(this.workspace, this.modifiers);
326326
const newLeft = this.element.getBoundingRectangle().left;
@@ -335,15 +335,15 @@ suite('Keyboard-driven movement', function () {
335335
const oldBounds = this.element.getBoundingRectangle();
336336
Blockly.getFocusManager().focusNode(this.element);
337337
startMove(this.workspace);
338-
assert.isTrue(Blockly.KeyboardMover.isMoving());
338+
assert.isTrue(Blockly.KeyboardMover.mover.isMoving());
339339
moveRight(this.workspace, this.modifiers);
340340
moveRight(this.workspace, this.modifiers);
341341

342342
const event = createKeyDownEvent(Blockly.utils.KeyCodes.C, [
343343
Blockly.utils.KeyCodes.META,
344344
]);
345345
this.workspace.getInjectionDiv().dispatchEvent(event);
346-
assert.isFalse(Blockly.KeyboardMover.isMoving());
346+
assert.isFalse(Blockly.KeyboardMover.mover.isMoving());
347347

348348
const newBounds = this.element.getBoundingRectangle();
349349
oldBounds.left += 40;

0 commit comments

Comments
 (0)