Skip to content

Commit

Permalink
Extract "select all" behavior to new SELECT_ALL_COMMAND (#4818)
Browse files Browse the repository at this point in the history
  • Loading branch information
montlebalm authored Aug 7, 2023
1 parent 565d580 commit 5aa1230
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 8 deletions.
11 changes: 11 additions & 0 deletions packages/lexical-plain-text/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {mergeRegister} from '@lexical/utils';
import {
$getSelection,
$isRangeSelection,
$selectAll,
COMMAND_PRIORITY_EDITOR,
CONTROLLED_TEXT_INSERTION_COMMAND,
COPY_COMMAND,
Expand All @@ -39,6 +40,7 @@ import {
KEY_ENTER_COMMAND,
PASTE_COMMAND,
REMOVE_TEXT_COMMAND,
SELECT_ALL_COMMAND,
} from 'lexical';
import {
CAN_USE_BEFORE_INPUT,
Expand Down Expand Up @@ -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) => {
Expand Down
11 changes: 11 additions & 0 deletions packages/lexical-rich-text/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ import {
$isRootNode,
$isTextNode,
$normalizeSelection__EXPERIMENTAL,
$selectAll,
$setSelection,
CLICK_COMMAND,
COMMAND_PRIORITY_EDITOR,
Expand Down Expand Up @@ -91,6 +92,7 @@ import {
OUTDENT_CONTENT_COMMAND,
PASTE_COMMAND,
REMOVE_TEXT_COMMAND,
SELECT_ALL_COMMAND,
} from 'lexical';
import caretFromPoint from 'shared/caretFromPoint';
import {
Expand Down Expand Up @@ -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) => {
Expand Down
2 changes: 2 additions & 0 deletions packages/lexical/src/LexicalCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<KeyboardEvent> =
createCommand('SELECT_ALL_COMMAND');
export const CLEAR_EDITOR_COMMAND: LexicalCommand<void> = createCommand(
'CLEAR_EDITOR_COMMAND',
);
Expand Down
11 changes: 3 additions & 8 deletions packages/lexical/src/LexicalEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -79,7 +79,6 @@ import {
$getNodeByKey,
$isSelectionCapturedInDecorator,
$isTokenOrSegmented,
$selectAll,
$setSelection,
$shouldInsertTextAfterOrBeforeTextNode,
$updateSelectedTextFromDOM,
Expand Down Expand Up @@ -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);
}
}

Expand Down
2 changes: 2 additions & 0 deletions packages/lexical/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ export {
PASTE_COMMAND,
REDO_COMMAND,
REMOVE_TEXT_COMMAND,
SELECT_ALL_COMMAND,
SELECTION_CHANGE_COMMAND,
UNDO_COMMAND,
} from './LexicalCommands';
Expand Down Expand Up @@ -149,6 +150,7 @@ export {
$isLeafNode,
$isRootOrShadowRoot,
$nodesOfType,
$selectAll,
$setCompositionKey,
$setSelection,
$splitNode,
Expand Down

2 comments on commit 5aa1230

@vercel
Copy link

@vercel vercel bot commented on 5aa1230 Aug 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

lexical – ./packages/lexical-website

lexical-fbopensource.vercel.app
lexical-git-main-fbopensource.vercel.app
lexical.dev
www.lexical.dev
lexicaljs.com
lexicaljs.org

@vercel
Copy link

@vercel vercel bot commented on 5aa1230 Aug 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

lexical-playground – ./packages/lexical-playground

lexical-playground-fbopensource.vercel.app
lexical-playground.vercel.app
playground.lexical.dev
lexical-playground-git-main-fbopensource.vercel.app

Please sign in to comment.