|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2025 Google LLC |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +import { |
| 8 | + BlockSvg, |
| 9 | + clipboard, |
| 10 | + ContextMenuRegistry, |
| 11 | + ICopyable, |
| 12 | + ShortcutRegistry, |
| 13 | + utils, |
| 14 | + comments, |
| 15 | + ICopyData, |
| 16 | +} from 'blockly'; |
| 17 | +import * as Constants from '../constants'; |
| 18 | +import {getMenuItem} from '../shortcut_formatting'; |
| 19 | + |
| 20 | +/** |
| 21 | + * Duplicate action that adds a keyboard shortcut for duplicate and overrides |
| 22 | + * the context menu item to show it if the context menu item is registered. |
| 23 | + */ |
| 24 | +export class DuplicateAction { |
| 25 | + private duplicateShortcut: ShortcutRegistry.KeyboardShortcut | null = null; |
| 26 | + private uninstallHandlers: Array<() => void> = []; |
| 27 | + |
| 28 | + /** |
| 29 | + * Install the shortcuts and override context menu entries. |
| 30 | + * |
| 31 | + * No change is made if there's already a 'duplicate' shortcut. |
| 32 | + */ |
| 33 | + install() { |
| 34 | + this.duplicateShortcut = this.registerDuplicateShortcut(); |
| 35 | + if (this.duplicateShortcut) { |
| 36 | + this.uninstallHandlers.push( |
| 37 | + overrideContextMenuItemForShortcutText( |
| 38 | + 'blockDuplicate', |
| 39 | + Constants.SHORTCUT_NAMES.DUPLICATE, |
| 40 | + ), |
| 41 | + ); |
| 42 | + this.uninstallHandlers.push( |
| 43 | + overrideContextMenuItemForShortcutText( |
| 44 | + 'commentDuplicate', |
| 45 | + Constants.SHORTCUT_NAMES.DUPLICATE, |
| 46 | + ), |
| 47 | + ); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Unregister the shortcut and reinstate the original context menu entries. |
| 53 | + */ |
| 54 | + uninstall() { |
| 55 | + this.uninstallHandlers.forEach((handler) => handler()); |
| 56 | + this.uninstallHandlers.length = 0; |
| 57 | + if (this.duplicateShortcut) { |
| 58 | + ShortcutRegistry.registry.unregister(this.duplicateShortcut.name); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Create and register the keyboard shortcut for the duplicate action. |
| 64 | + * Same behaviour as for the core context menu. |
| 65 | + * Skipped if there is a shortcut with a matching name already. |
| 66 | + */ |
| 67 | + private registerDuplicateShortcut(): ShortcutRegistry.KeyboardShortcut | null { |
| 68 | + if ( |
| 69 | + ShortcutRegistry.registry.getRegistry()[ |
| 70 | + Constants.SHORTCUT_NAMES.DUPLICATE |
| 71 | + ] |
| 72 | + ) { |
| 73 | + return null; |
| 74 | + } |
| 75 | + |
| 76 | + const shortcut: ShortcutRegistry.KeyboardShortcut = { |
| 77 | + name: Constants.SHORTCUT_NAMES.DUPLICATE, |
| 78 | + // Equivalent to the core context menu entry. |
| 79 | + preconditionFn(workspace, scope) { |
| 80 | + const {focusedNode} = scope; |
| 81 | + if (focusedNode instanceof BlockSvg) { |
| 82 | + return ( |
| 83 | + !focusedNode.isInFlyout && |
| 84 | + focusedNode.isDeletable() && |
| 85 | + focusedNode.isMovable() && |
| 86 | + focusedNode.isDuplicatable() |
| 87 | + ); |
| 88 | + } else if (focusedNode instanceof comments.RenderedWorkspaceComment) { |
| 89 | + return focusedNode.isMovable(); |
| 90 | + } |
| 91 | + return false; |
| 92 | + }, |
| 93 | + callback(workspace, e, shortcut, scope) { |
| 94 | + const copyable = scope.focusedNode as ICopyable<ICopyData>; |
| 95 | + const data = copyable.toCopyData(); |
| 96 | + if (!data) return false; |
| 97 | + return !!clipboard.paste(data, workspace); |
| 98 | + }, |
| 99 | + keyCodes: [utils.KeyCodes.D], |
| 100 | + }; |
| 101 | + ShortcutRegistry.registry.register(shortcut); |
| 102 | + return shortcut; |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +/** |
| 107 | + * Replace a context menu item to add shortcut text to its displayText. |
| 108 | + * |
| 109 | + * Nothing happens if there is not a matching context menu item registered. |
| 110 | + * |
| 111 | + * @param registryId Context menu registry id to replace if present. |
| 112 | + * @param shortcutName The corresponding shortcut name. |
| 113 | + * @returns A function to reinstate the original context menu entry. |
| 114 | + */ |
| 115 | +function overrideContextMenuItemForShortcutText( |
| 116 | + registryId: string, |
| 117 | + shortcutName: string, |
| 118 | +): () => void { |
| 119 | + const original = ContextMenuRegistry.registry.getItem(registryId); |
| 120 | + if (!original || 'separator' in original) { |
| 121 | + return () => {}; |
| 122 | + } |
| 123 | + |
| 124 | + const override: ContextMenuRegistry.RegistryItem = { |
| 125 | + ...original, |
| 126 | + displayText: (scope: ContextMenuRegistry.Scope) => { |
| 127 | + const displayText = |
| 128 | + typeof original.displayText === 'function' |
| 129 | + ? original.displayText(scope) |
| 130 | + : original.displayText; |
| 131 | + if (displayText instanceof HTMLElement) { |
| 132 | + // We can't cope in this scenario. |
| 133 | + return displayText; |
| 134 | + } |
| 135 | + return getMenuItem(displayText, shortcutName); |
| 136 | + }, |
| 137 | + }; |
| 138 | + ContextMenuRegistry.registry.unregister(registryId); |
| 139 | + ContextMenuRegistry.registry.register(override); |
| 140 | + |
| 141 | + return () => { |
| 142 | + ContextMenuRegistry.registry.unregister(registryId); |
| 143 | + ContextMenuRegistry.registry.register(original); |
| 144 | + }; |
| 145 | +} |
0 commit comments