-
Notifications
You must be signed in to change notification settings - Fork 13
feat: Edit option in block context menu #274
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 9 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
f05269f
refactor: Rename shortcuts to match arrow keys
cpcallen 380abb9
chore: Sort inputs in navigation_controller.ts
cpcallen fa6d3e4
feat: Edit action
cpcallen 9957282
feat: Only show Edit option when useful
cpcallen 9d9fbfd
fix: Adjust weights to put edit between insert and delete
cpcallen 3f04bfb
fix: typo in src/constants.ts
cpcallen 860cdb9
fix: Revert f05269f for PR #274.
cpcallen 03f3f34
chore: Merge branch 'main' into feat/edit
cpcallen 608ae08
fix: JSDoc tweaks for PR #274
cpcallen 0dba720
Merge branch 'main' into feat/edit
rachel-fenichel f96211c
fix: remove dead import
rachel-fenichel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| /** | ||
| * @license | ||
| * Copyright 2025 Google LLC | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| import { | ||
| Connection, | ||
| ContextMenuRegistry, | ||
| ShortcutRegistry, | ||
| comments, | ||
| utils as BlocklyUtils, | ||
| } from 'blockly'; | ||
| import * as Constants from '../constants'; | ||
| import type {BlockSvg, WorkspaceSvg} from 'blockly'; | ||
| import {LineCursor} from '../line_cursor'; | ||
| import {NavigationController} from '../navigation_controller'; | ||
|
|
||
| const KeyCodes = BlocklyUtils.KeyCodes; | ||
|
|
||
| /** | ||
| * Action to edit a block. This just moves the cursor to the first | ||
| * field or input (if there is one), and exists as an aid to | ||
| * navigational discoverability: | ||
| * | ||
| * Any time there is a cursor position that can be accessed by | ||
| * pressing the right-arrow key, which isn't accessible by pressing | ||
| * the down-arrow key (these positions are typically fields and value | ||
| * inputs), a context menu item "Edit Block contents (→︎)" will be | ||
| * shown in the block context menu. | ||
| * | ||
| * N.B.: This item is shown any time the cursor is on a block and not | ||
| * in the rightmost position 'on the current line'; that means that | ||
| * sometimes the label ("Edit block contents") is possibly misleading, | ||
| * because it might not be the contents of the _current_ block that's | ||
| * being edited, but rather that of a sibling or parent block. | ||
| * | ||
| * This action registers itself only as a context menu item, as there | ||
| * is already a corresponding "right" shortcut item. | ||
| */ | ||
| export class EditAction { | ||
| constructor(private canCurrentlyNavigate: (ws: WorkspaceSvg) => boolean) {} | ||
|
|
||
| /** | ||
| * Install this action as a context menu item. | ||
| */ | ||
| install() { | ||
| this.registerContextMenuAction(); | ||
| } | ||
|
|
||
| /** | ||
| * Uninstall this action as both a keyboard shortcut and a context menu item. | ||
| * Reinstall the original context menu action if possible. | ||
| */ | ||
| uninstall() { | ||
| ContextMenuRegistry.registry.unregister('edit'); | ||
| } | ||
|
|
||
| /** | ||
| * Register the edit block action as a context menu item on blocks. | ||
| */ | ||
| private registerContextMenuAction() { | ||
| const editAboveItem: ContextMenuRegistry.RegistryItem = { | ||
| displayText: 'Edit Block contents (→︎)', | ||
| preconditionFn: (scope: ContextMenuRegistry.Scope) => { | ||
| const workspace = scope.block?.workspace; | ||
| if (!workspace || !this.canCurrentlyNavigate(workspace)) { | ||
| return 'disabled'; | ||
| } | ||
| const cursor = workspace.getCursor() as LineCursor | null; | ||
| if (!cursor) return 'disabled'; | ||
| return cursor.atEndOfLine() ? 'hidden' : 'enabled'; | ||
| }, | ||
| callback: (scope: ContextMenuRegistry.Scope) => { | ||
| const workspace = scope.block?.workspace; | ||
| if (!workspace) return false; | ||
| workspace.getCursor()?.in(); | ||
| return true; | ||
| }, | ||
| scopeType: ContextMenuRegistry.ScopeType.BLOCK, | ||
| id: 'edit', | ||
| weight: 10, | ||
| }; | ||
|
|
||
| ContextMenuRegistry.registry.register(editAboveItem); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does it perhaps make sense to consolidate this with the shortcut for workspace contexts? It seems to be doing roughly the same thing, and it feels a bit odd to have an inconsistency here as compared to other action classes (where those manage both context menu and shortcut triggers).
It wouldn't seem odd, I think, for there to be a workspace-only shortcut handled here either since the context menu callback and precondition are already assuming workspace only. I suppose the only aspects I'm unsure of is having multiple actions bound for the same key (RIGHT) or whether splitting that into multiple locations is wise.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this code could be merged in to
actions/ws_movement.ts, now that has been separated out in #281.The precondition for showing the "edit" option is a bit different from the precondition for allowing the right arrow to work, though: the right arrow key is practically always enabled, wheras the edit option is only shown in some places.
(And the edit option will need to be updated once the cursor properly supports RTL, since in that case it will do the same thing as the left arrow key, rather than the right arrow key.)
Unfortunately I won't have time to do such a refactor before I leave today, and I won't be back for a couple of weeks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's fine to merge this as-is for the benefits of the clean-up. Will approve, thanks!