|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2025 Google LLC |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +import {ASTNode, ShortcutRegistry, utils as BlocklyUtils} from 'blockly'; |
| 8 | +import * as Constants from '../constants'; |
| 9 | +import type {WorkspaceSvg} from 'blockly'; |
| 10 | + |
| 11 | +const KeyCodes = BlocklyUtils.KeyCodes; |
| 12 | +const createSerializedKey = ShortcutRegistry.registry.createSerializedKey.bind( |
| 13 | + ShortcutRegistry.registry, |
| 14 | +); |
| 15 | + |
| 16 | +/** |
| 17 | + * Logic for free movement of the cursor on the workspace with keyboard |
| 18 | + * shortcuts. |
| 19 | + */ |
| 20 | +export class WorkspaceMovement { |
| 21 | + /** |
| 22 | + * Function provided by the navigation controller to say whether editing |
| 23 | + * is allowed. |
| 24 | + */ |
| 25 | + private canCurrentlyEdit: (ws: WorkspaceSvg) => boolean; |
| 26 | + |
| 27 | + /** |
| 28 | + * The distance to move the cursor when the cursor is on the workspace. |
| 29 | + */ |
| 30 | + WS_MOVE_DISTANCE = 40; |
| 31 | + |
| 32 | + constructor(canEdit: (ws: WorkspaceSvg) => boolean) { |
| 33 | + this.canCurrentlyEdit = canEdit; |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Install these actions as both keyboard shortcuts and context menu items. |
| 38 | + */ |
| 39 | + install() { |
| 40 | + const shortcutList: { |
| 41 | + [name: string]: ShortcutRegistry.KeyboardShortcut; |
| 42 | + } = { |
| 43 | + /** Move the cursor on the workspace to the left. */ |
| 44 | + wsMoveLeft: { |
| 45 | + name: Constants.SHORTCUT_NAMES.MOVE_WS_CURSOR_LEFT, |
| 46 | + preconditionFn: (workspace) => this.canCurrentlyEdit(workspace), |
| 47 | + callback: (workspace) => this.moveWSCursor(workspace, -1, 0), |
| 48 | + keyCodes: [createSerializedKey(KeyCodes.A, [KeyCodes.SHIFT])], |
| 49 | + }, |
| 50 | + /** Move the cursor on the workspace to the right. */ |
| 51 | + wsMoveRight: { |
| 52 | + name: Constants.SHORTCUT_NAMES.MOVE_WS_CURSOR_RIGHT, |
| 53 | + preconditionFn: (workspace) => this.canCurrentlyEdit(workspace), |
| 54 | + callback: (workspace) => this.moveWSCursor(workspace, 1, 0), |
| 55 | + keyCodes: [createSerializedKey(KeyCodes.D, [KeyCodes.SHIFT])], |
| 56 | + }, |
| 57 | + |
| 58 | + /** Move the cursor on the workspace up. */ |
| 59 | + wsMoveUp: { |
| 60 | + name: Constants.SHORTCUT_NAMES.MOVE_WS_CURSOR_UP, |
| 61 | + preconditionFn: (workspace) => this.canCurrentlyEdit(workspace), |
| 62 | + callback: (workspace) => this.moveWSCursor(workspace, 0, -1), |
| 63 | + keyCodes: [createSerializedKey(KeyCodes.W, [KeyCodes.SHIFT])], |
| 64 | + }, |
| 65 | + |
| 66 | + /** Move the cursor on the workspace down. */ |
| 67 | + wsMoveDown: { |
| 68 | + name: Constants.SHORTCUT_NAMES.MOVE_WS_CURSOR_DOWN, |
| 69 | + preconditionFn: (workspace) => this.canCurrentlyEdit(workspace), |
| 70 | + callback: (workspace) => this.moveWSCursor(workspace, 0, 1), |
| 71 | + keyCodes: [createSerializedKey(KeyCodes.S, [KeyCodes.SHIFT])], |
| 72 | + }, |
| 73 | + }; |
| 74 | + for (const shortcut of Object.values(shortcutList)) { |
| 75 | + ShortcutRegistry.registry.register(shortcut); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Uninstall these actions. |
| 81 | + */ |
| 82 | + uninstall() { |
| 83 | + ShortcutRegistry.registry.unregister( |
| 84 | + Constants.SHORTCUT_NAMES.MOVE_WS_CURSOR_LEFT, |
| 85 | + ); |
| 86 | + ShortcutRegistry.registry.unregister( |
| 87 | + Constants.SHORTCUT_NAMES.MOVE_WS_CURSOR_RIGHT, |
| 88 | + ); |
| 89 | + ShortcutRegistry.registry.unregister( |
| 90 | + Constants.SHORTCUT_NAMES.MOVE_WS_CURSOR_UP, |
| 91 | + ); |
| 92 | + ShortcutRegistry.registry.unregister( |
| 93 | + Constants.SHORTCUT_NAMES.MOVE_WS_CURSOR_DOWN, |
| 94 | + ); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Moves the workspace cursor in the given direction. |
| 99 | + * |
| 100 | + * @param workspace The workspace the cursor is on. |
| 101 | + * @param xDirection -1 to move cursor left. 1 to move cursor right. |
| 102 | + * @param yDirection -1 to move cursor up. 1 to move cursor down. |
| 103 | + * @returns True if the current node is a workspace, false |
| 104 | + * otherwise. |
| 105 | + */ |
| 106 | + moveWSCursor( |
| 107 | + workspace: WorkspaceSvg, |
| 108 | + xDirection: number, |
| 109 | + yDirection: number, |
| 110 | + ): boolean { |
| 111 | + const cursor = workspace.getCursor(); |
| 112 | + if (!cursor) { |
| 113 | + return false; |
| 114 | + } |
| 115 | + const curNode = cursor.getCurNode(); |
| 116 | + |
| 117 | + if (curNode.getType() !== ASTNode.types.WORKSPACE) { |
| 118 | + return false; |
| 119 | + } |
| 120 | + |
| 121 | + const wsCoord = curNode.getWsCoordinate(); |
| 122 | + const newX = xDirection * this.WS_MOVE_DISTANCE + wsCoord.x; |
| 123 | + const newY = yDirection * this.WS_MOVE_DISTANCE + wsCoord.y; |
| 124 | + |
| 125 | + cursor.setCurNode( |
| 126 | + ASTNode.createWorkspaceNode( |
| 127 | + workspace, |
| 128 | + new BlocklyUtils.Coordinate(newX, newY), |
| 129 | + )!, |
| 130 | + ); |
| 131 | + return true; |
| 132 | + } |
| 133 | +} |
0 commit comments