From 5aa123068952d3194c1042045b41618fcaa655a3 Mon Sep 17 00:00:00 2001 From: Chris Montrois Date: Mon, 7 Aug 2023 17:52:33 -0600 Subject: [PATCH] Extract "select all" behavior to new `SELECT_ALL_COMMAND` (#4818) --- packages/lexical-plain-text/src/index.ts | 11 +++++++++++ packages/lexical-rich-text/src/index.ts | 11 +++++++++++ packages/lexical/src/LexicalCommands.ts | 2 ++ packages/lexical/src/LexicalEvents.ts | 11 +++-------- packages/lexical/src/index.ts | 2 ++ 5 files changed, 29 insertions(+), 8 deletions(-) diff --git a/packages/lexical-plain-text/src/index.ts b/packages/lexical-plain-text/src/index.ts index 37e47b38510..de335c1727a 100644 --- a/packages/lexical-plain-text/src/index.ts +++ b/packages/lexical-plain-text/src/index.ts @@ -21,6 +21,7 @@ import {mergeRegister} from '@lexical/utils'; import { $getSelection, $isRangeSelection, + $selectAll, COMMAND_PRIORITY_EDITOR, CONTROLLED_TEXT_INSERTION_COMMAND, COPY_COMMAND, @@ -39,6 +40,7 @@ import { KEY_ENTER_COMMAND, PASTE_COMMAND, REMOVE_TEXT_COMMAND, + SELECT_ALL_COMMAND, } from 'lexical'; import { CAN_USE_BEFORE_INPUT, @@ -322,6 +324,15 @@ export function registerPlainText(editor: LexicalEditor): () => void { }, COMMAND_PRIORITY_EDITOR, ), + editor.registerCommand( + SELECT_ALL_COMMAND, + () => { + $selectAll(); + + return true; + }, + COMMAND_PRIORITY_EDITOR, + ), editor.registerCommand( COPY_COMMAND, (event) => { diff --git a/packages/lexical-rich-text/src/index.ts b/packages/lexical-rich-text/src/index.ts index 432e48b3ea4..95cef5d051d 100644 --- a/packages/lexical-rich-text/src/index.ts +++ b/packages/lexical-rich-text/src/index.ts @@ -58,6 +58,7 @@ import { $isRootNode, $isTextNode, $normalizeSelection__EXPERIMENTAL, + $selectAll, $setSelection, CLICK_COMMAND, COMMAND_PRIORITY_EDITOR, @@ -91,6 +92,7 @@ import { OUTDENT_CONTENT_COMMAND, PASTE_COMMAND, REMOVE_TEXT_COMMAND, + SELECT_ALL_COMMAND, } from 'lexical'; import caretFromPoint from 'shared/caretFromPoint'; import { @@ -990,6 +992,15 @@ export function registerRichText(editor: LexicalEditor): () => void { }, COMMAND_PRIORITY_EDITOR, ), + editor.registerCommand( + SELECT_ALL_COMMAND, + () => { + $selectAll(); + + return true; + }, + COMMAND_PRIORITY_EDITOR, + ), editor.registerCommand( COPY_COMMAND, (event) => { diff --git a/packages/lexical/src/LexicalCommands.ts b/packages/lexical/src/LexicalCommands.ts index 33e0fa1af8d..45a3722c336 100644 --- a/packages/lexical/src/LexicalCommands.ts +++ b/packages/lexical/src/LexicalCommands.ts @@ -95,6 +95,8 @@ export const COPY_COMMAND: LexicalCommand< export const CUT_COMMAND: LexicalCommand< ClipboardEvent | KeyboardEvent | null > = createCommand('CUT_COMMAND'); +export const SELECT_ALL_COMMAND: LexicalCommand = + createCommand('SELECT_ALL_COMMAND'); export const CLEAR_EDITOR_COMMAND: LexicalCommand = createCommand( 'CLEAR_EDITOR_COMMAND', ); diff --git a/packages/lexical/src/LexicalEvents.ts b/packages/lexical/src/LexicalEvents.ts index 17056260b75..52ddd4c68a3 100644 --- a/packages/lexical/src/LexicalEvents.ts +++ b/packages/lexical/src/LexicalEvents.ts @@ -64,7 +64,7 @@ import { SELECTION_CHANGE_COMMAND, UNDO_COMMAND, } from '.'; -import {KEY_MODIFIER_COMMAND} from './LexicalCommands'; +import {KEY_MODIFIER_COMMAND, SELECT_ALL_COMMAND} from './LexicalCommands'; import { COMPOSITION_START_CHAR, DOM_ELEMENT_TYPE, @@ -79,7 +79,6 @@ import { $getNodeByKey, $isSelectionCapturedInDecorator, $isTokenOrSegmented, - $selectAll, $setSelection, $shouldInsertTextAfterOrBeforeTextNode, $updateSelectedTextFromDOM, @@ -1018,16 +1017,12 @@ function onKeyDown(event: KeyboardEvent, editor: LexicalEditor): void { dispatchCommand(editor, CUT_COMMAND, event); } else if (isSelectAll(keyCode, metaKey, ctrlKey)) { event.preventDefault(); - editor.update(() => { - $selectAll(); - }); + dispatchCommand(editor, SELECT_ALL_COMMAND, event); } // FF does it well (no need to override behavior) } else if (!IS_FIREFOX && isSelectAll(keyCode, metaKey, ctrlKey)) { event.preventDefault(); - editor.update(() => { - $selectAll(); - }); + dispatchCommand(editor, SELECT_ALL_COMMAND, event); } } diff --git a/packages/lexical/src/index.ts b/packages/lexical/src/index.ts index 552f3e62903..2aa8b332044 100644 --- a/packages/lexical/src/index.ts +++ b/packages/lexical/src/index.ts @@ -105,6 +105,7 @@ export { PASTE_COMMAND, REDO_COMMAND, REMOVE_TEXT_COMMAND, + SELECT_ALL_COMMAND, SELECTION_CHANGE_COMMAND, UNDO_COMMAND, } from './LexicalCommands'; @@ -149,6 +150,7 @@ export { $isLeafNode, $isRootOrShadowRoot, $nodesOfType, + $selectAll, $setCompositionKey, $setSelection, $splitNode,