-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Add Cmd/Ctrl + D shortcut for duplicating document blocks #8578 #8627
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
base: main
Are you sure you want to change the base?
Changes from all commits
63d5949
106f6ee
fe92347
d31e212
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| import 'dart:async'; | ||
|
|
||
| import 'package:appflowy/generated/locale_keys.g.dart'; | ||
| import 'package:appflowy/plugins/document/presentation/editor_notification.dart'; | ||
| import 'package:appflowy_editor/appflowy_editor.dart'; | ||
| import 'package:easy_localization/easy_localization.dart'; | ||
| import 'package:flutter/material.dart'; | ||
|
|
||
| /// Duplicate block(s). | ||
| /// | ||
| /// - support | ||
| /// - desktop | ||
| /// - web | ||
| final CommandShortcutEvent customDuplicateBlockCommand = CommandShortcutEvent( | ||
| key: 'duplicate selected block', | ||
| getDescription: () => LocaleKeys.document_plugins_optionAction_duplicate.tr(), | ||
| command: 'ctrl+d', | ||
| macOSCommand: 'cmd+d', | ||
| handler: _duplicateBlockCommandHandler, | ||
| ); | ||
|
|
||
| CommandShortcutEventHandler _duplicateBlockCommandHandler = (editorState) { | ||
| final selection = editorState.selection; | ||
|
Comment on lines
+14
to
+23
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: Consider updating the selection to target the newly duplicated block(s) after the command runs. Right now the selection stays on the original block(s), which can lead to accidental edits there and makes repeated Suggested implementation: To fully implement “selection moves to duplicated blocks”, you’ll need to update the body of
In summary, the handler should:
|
||
| if (selection == null) { | ||
| return KeyEventResult.ignored; | ||
| } | ||
|
|
||
| final transaction = editorState.transaction; | ||
| final normalizedSelection = selection.normalized; | ||
| final isMultiBlockSelection = | ||
| normalizedSelection.start.path != normalizedSelection.end.path; | ||
|
|
||
| if (editorState.selectionType == SelectionType.block || | ||
| isMultiBlockSelection) { | ||
| final nodes = editorState.getNodesInSelection(normalizedSelection); | ||
| if (nodes.isEmpty) { | ||
| return KeyEventResult.ignored; | ||
| } | ||
|
|
||
| transaction.insertNodes( | ||
| normalizedSelection.end.path.next, | ||
| nodes.map((node) => node.deepCopy()).toList(), | ||
| ); | ||
| } else { | ||
| final node = editorState.getNodeAtPath(normalizedSelection.end.path); | ||
| if (node == null) { | ||
| return KeyEventResult.ignored; | ||
| } | ||
|
|
||
| transaction.insertNode(node.path.next, node.deepCopy()); | ||
| } | ||
|
|
||
| unawaited( | ||
| editorState.apply(transaction).then((_) { | ||
| EditorNotification.paste().post(); | ||
| }), | ||
| ); | ||
|
|
||
| return KeyEventResult.handled; | ||
| }; | ||
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.
suggestion: Avoid hardcoding the shortcut label to keep it in sync with the actual keybinding configuration.
The
rightIconhardcodesCmd+D/Ctrl+Dwhile the actual shortcut is defined incustomDuplicateBlockCommand. If the binding changes or becomes configurable, the label will be wrong. Please derive this label from the same source as the shortcut (or a shared constant) so the UI shortcut hint always matches the real keybinding.Suggested implementation:
customDuplicateBlockCommandis accessible in this file:shortcutLabel(or similarly named) property or getter that returns a user-facing label likeCmd+D/Ctrl+Dbased on the current keybinding configuration and platform.customDuplicateBlockCommandis currently only defining the keybinding but not a label:String get shortcutLabelto that command (or a shared helper) that builds the label from its configured keybinding(s) instead of hardcoding platform-specific strings in the UI.universal_platformfrom this file.