|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2025 Google LLC |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +import { |
| 8 | + Connection, |
| 9 | + ContextMenuRegistry, |
| 10 | + ShortcutRegistry, |
| 11 | + comments, |
| 12 | + utils as BlocklyUtils, |
| 13 | +} from 'blockly'; |
| 14 | +import * as Constants from '../constants'; |
| 15 | +import type {BlockSvg, WorkspaceSvg} from 'blockly'; |
| 16 | +import {LineCursor} from '../line_cursor'; |
| 17 | +import {NavigationController} from '../navigation_controller'; |
| 18 | + |
| 19 | +const KeyCodes = BlocklyUtils.KeyCodes; |
| 20 | + |
| 21 | +/** |
| 22 | + * Action to edit a block. This just moves the cursor to the first |
| 23 | + * field or input (if there is one), and exists as an aid to |
| 24 | + * navigational discoverability: |
| 25 | + * |
| 26 | + * Any time there is a cursor position that can be accessed by |
| 27 | + * pressing the right-arrow key, which isn't accessible by pressing |
| 28 | + * the down-arrow key (these positions are typically fields and value |
| 29 | + * inputs), a context menu item "Edit Block contents (→︎)" will be |
| 30 | + * shown in the block context menu. |
| 31 | + * |
| 32 | + * N.B.: This item is shown any time the cursor is on a block and not |
| 33 | + * in the rightmost position 'on the current line'; that means that |
| 34 | + * sometimes the label ("Edit block contents") is possibly misleading, |
| 35 | + * because it might not be the contents of the _current_ block that's |
| 36 | + * being edited, but rather that of a sibling or parent block. |
| 37 | + * |
| 38 | + * This action registers itself only as a context menu item, as there |
| 39 | + * is already a corresponding "right" shortcut item. |
| 40 | + */ |
| 41 | +export class EditAction { |
| 42 | + constructor(private canCurrentlyNavigate: (ws: WorkspaceSvg) => boolean) {} |
| 43 | + |
| 44 | + /** |
| 45 | + * Install this action as a context menu item. |
| 46 | + */ |
| 47 | + install() { |
| 48 | + this.registerContextMenuAction(); |
| 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 | + ContextMenuRegistry.registry.unregister('edit'); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Register the edit block action as a context menu item on blocks. |
| 61 | + */ |
| 62 | + private registerContextMenuAction() { |
| 63 | + const editAboveItem: ContextMenuRegistry.RegistryItem = { |
| 64 | + displayText: 'Edit Block contents (→︎)', |
| 65 | + preconditionFn: (scope: ContextMenuRegistry.Scope) => { |
| 66 | + const workspace = scope.block?.workspace; |
| 67 | + if (!workspace || !this.canCurrentlyNavigate(workspace)) { |
| 68 | + return 'disabled'; |
| 69 | + } |
| 70 | + const cursor = workspace.getCursor() as LineCursor | null; |
| 71 | + if (!cursor) return 'disabled'; |
| 72 | + return cursor.atEndOfLine() ? 'hidden' : 'enabled'; |
| 73 | + }, |
| 74 | + callback: (scope: ContextMenuRegistry.Scope) => { |
| 75 | + const workspace = scope.block?.workspace; |
| 76 | + if (!workspace) return false; |
| 77 | + workspace.getCursor()?.in(); |
| 78 | + return true; |
| 79 | + }, |
| 80 | + scopeType: ContextMenuRegistry.ScopeType.BLOCK, |
| 81 | + id: 'edit', |
| 82 | + weight: 10, |
| 83 | + }; |
| 84 | + |
| 85 | + ContextMenuRegistry.registry.register(editAboveItem); |
| 86 | + } |
| 87 | +} |
0 commit comments