|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2025 Google LLC |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +import { |
| 8 | + ASTNode, |
| 9 | + RenderedConnection, |
| 10 | + ShortcutRegistry, |
| 11 | + utils as BlocklyUtils, |
| 12 | +} from 'blockly'; |
| 13 | +import * as Constants from '../constants'; |
| 14 | +import type {WorkspaceSvg} from 'blockly'; |
| 15 | +import {Navigation} from '../navigation'; |
| 16 | + |
| 17 | +const KeyCodes = BlocklyUtils.KeyCodes; |
| 18 | + |
| 19 | +/** |
| 20 | + * Action to insert a block into the workspace. |
| 21 | + * |
| 22 | + * This action registers itself as both a keyboard shortcut and a context menu |
| 23 | + * item. |
| 24 | + */ |
| 25 | +export class DisconnectAction { |
| 26 | + /** |
| 27 | + * Function provided by the navigation controller to say whether editing |
| 28 | + * is allowed. |
| 29 | + */ |
| 30 | + private canCurrentlyEdit: (ws: WorkspaceSvg) => boolean; |
| 31 | + |
| 32 | + /** |
| 33 | + * Registration name for the keyboard shortcut. |
| 34 | + */ |
| 35 | + private shortcutName = Constants.SHORTCUT_NAMES.DISCONNECT; |
| 36 | + |
| 37 | + constructor( |
| 38 | + private navigation: Navigation, |
| 39 | + canEdit: (ws: WorkspaceSvg) => boolean, |
| 40 | + ) { |
| 41 | + this.canCurrentlyEdit = canEdit; |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Install this action as both a keyboard shortcut and a context menu item. |
| 46 | + */ |
| 47 | + install() { |
| 48 | + this.registerShortcut(); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Uninstall this action as both a keyboard shortcut and a context menu item. |
| 53 | + * Reinstall the original context menu action if possible. |
| 54 | + */ |
| 55 | + uninstall() { |
| 56 | + ShortcutRegistry.registry.unregister(this.shortcutName); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Create and register the keyboard shortcut for this action. |
| 61 | + */ |
| 62 | + private registerShortcut() { |
| 63 | + const disconnectShortcut: ShortcutRegistry.KeyboardShortcut = { |
| 64 | + name: this.shortcutName, |
| 65 | + preconditionFn: (workspace) => this.canCurrentlyEdit(workspace), |
| 66 | + callback: (workspace) => { |
| 67 | + switch (this.navigation.getState(workspace)) { |
| 68 | + case Constants.STATE.WORKSPACE: |
| 69 | + this.disconnectBlocks(workspace); |
| 70 | + return true; |
| 71 | + default: |
| 72 | + return false; |
| 73 | + } |
| 74 | + }, |
| 75 | + keyCodes: [KeyCodes.X], |
| 76 | + }; |
| 77 | + ShortcutRegistry.registry.register(disconnectShortcut); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Disconnects the connection that the cursor is pointing to, and bump blocks. |
| 82 | + * This is a no-op if the connection cannot be broken or if the cursor is not |
| 83 | + * pointing to a connection. |
| 84 | + * |
| 85 | + * @param workspace The workspace. |
| 86 | + */ |
| 87 | + disconnectBlocks(workspace: WorkspaceSvg) { |
| 88 | + const cursor = workspace.getCursor(); |
| 89 | + if (!cursor) { |
| 90 | + return; |
| 91 | + } |
| 92 | + let curNode: ASTNode | null = cursor.getCurNode(); |
| 93 | + let wasVisitingConnection = true; |
| 94 | + while (curNode && !curNode.isConnection()) { |
| 95 | + curNode = curNode.out(); |
| 96 | + wasVisitingConnection = false; |
| 97 | + } |
| 98 | + if (!curNode) { |
| 99 | + console.log('Unable to find a connection to disconnect'); |
| 100 | + return; |
| 101 | + } |
| 102 | + const curConnection = curNode.getLocation() as RenderedConnection; |
| 103 | + if (!curConnection.isConnected()) { |
| 104 | + return; |
| 105 | + } |
| 106 | + const superiorConnection = curConnection.isSuperior() |
| 107 | + ? curConnection |
| 108 | + : curConnection.targetConnection!; |
| 109 | + |
| 110 | + const inferiorConnection = curConnection.isSuperior() |
| 111 | + ? curConnection.targetConnection! |
| 112 | + : curConnection; |
| 113 | + |
| 114 | + if (inferiorConnection.getSourceBlock().isShadow()) { |
| 115 | + return; |
| 116 | + } |
| 117 | + |
| 118 | + if (!inferiorConnection.getSourceBlock().isMovable()) { |
| 119 | + return; |
| 120 | + } |
| 121 | + |
| 122 | + superiorConnection.disconnect(); |
| 123 | + inferiorConnection.bumpAwayFrom(superiorConnection); |
| 124 | + |
| 125 | + const rootBlock = superiorConnection.getSourceBlock().getRootBlock(); |
| 126 | + rootBlock.bringToFront(); |
| 127 | + |
| 128 | + if (wasVisitingConnection) { |
| 129 | + const connectionNode = ASTNode.createConnectionNode(superiorConnection); |
| 130 | + workspace.getCursor()!.setCurNode(connectionNode!); |
| 131 | + } |
| 132 | + } |
| 133 | +} |
0 commit comments