Skip to content

Commit

Permalink
Support select all for node selection (#4233)
Browse files Browse the repository at this point in the history
  • Loading branch information
fantactuka authored Mar 31, 2023
1 parent c1bdf82 commit 0dc89f8
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
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

2 comments on commit 0dc89f8

@vercel
Copy link

@vercel vercel bot commented on 0dc89f8 Mar 31, 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-git-main-fbopensource.vercel.app
lexical.dev
lexical-fbopensource.vercel.app
lexicaljs.org
www.lexical.dev
lexicaljs.com

@vercel
Copy link

@vercel vercel bot commented on 0dc89f8 Mar 31, 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.vercel.app
lexical-playground-git-main-fbopensource.vercel.app
lexical-playground-fbopensource.vercel.app
playground.lexical.dev

Please sign in to comment.