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

Support select all for node selection #4233

Merged
merged 1 commit into from
Mar 31, 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
17 changes: 17 additions & 0 deletions packages/lexical-playground/__tests__/e2e/Selection.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
moveToLineBeginning,
moveToPrevWord,
pressShiftEnter,
selectAll,
} from '../keyboardShortcuts/index.mjs';
import {
assertHTML,
Expand Down Expand Up @@ -264,4 +265,20 @@ test.describe('Selection', () => {
`,
);
});

test('Can select all with node selection', async ({page, isPlainText}) => {
test.skip(isPlainText);
await focusEditor(page);
await page.keyboard.type('Text before');
await insertSampleImage(page);
await page.keyboard.type('Text after');
await selectAll(page);
await page.keyboard.press('Delete');
await assertHTML(
page,
html`
<p class="PlaygroundEditorTheme__paragraph"><br /></p>
`,
);
});
});
8 changes: 8 additions & 0 deletions packages/lexical/src/LexicalEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ import {
isOpenLineBreak,
isParagraph,
isRedo,
isSelectAll,
isSelectionWithinEditor,
isSpace,
isTab,
Expand Down Expand Up @@ -180,6 +181,7 @@ let collapsedSelectionFormat: [number, string, number, NodeKey, number] = [
// work as intended between different browsers and across word, line and character
// boundary/formats. It also is important for text replacement, node schemas and
// composition mechanics.

function $shouldPreventDefaultAndInsertText(
selection: RangeSelection,
domTargetRange: null | StaticRange,
Expand Down Expand Up @@ -977,6 +979,12 @@ function onKeyDown(event: KeyboardEvent, editor: LexicalEditor): void {
} else if (isCut(keyCode, shiftKey, metaKey, ctrlKey)) {
event.preventDefault();
dispatchCommand(editor, CUT_COMMAND, event);
} else if (isSelectAll(keyCode, metaKey, ctrlKey)) {
event.preventDefault();
editor.update(() => {
const root = $getRoot();
root.select(0, root.getChildrenSize());
});
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions packages/lexical/src/LexicalUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1031,6 +1031,14 @@ export function isDelete(keyCode: number): boolean {
return keyCode === 46;
}

export function isSelectAll(
keyCode: number,
metaKey: boolean,
ctrlKey: boolean,
): boolean {
return keyCode === 65 && controlOrMeta(metaKey, ctrlKey);
}

export function getCachedClassNameArray(
classNamesTheme: EditorThemeClasses,
classNameThemeType: string,
Expand Down