Skip to content
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

Extract "select all" behavior to new SELECT_ALL_COMMAND #4818

Merged
merged 1 commit into from
Aug 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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