Skip to content

Commit

Permalink
[lexical][@lexical/selection] Feature: Unify $selectAll Implementatio…
Browse files Browse the repository at this point in the history
…ns (#6902)

Co-authored-by: Hadi Elghoul <[email protected]>
  • Loading branch information
elgh0ul and Hadi Elghoul authored Dec 6, 2024
1 parent 97481c9 commit 55ef7ca
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 40 deletions.
4 changes: 2 additions & 2 deletions packages/lexical-selection/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import {
$isParentElementRTL,
$moveCaretSelection,
$moveCharacter,
$selectAll,
$setBlocksType,
$shouldOverrideDefaultCharacterSelection,
$wrapNodes,
Expand All @@ -32,7 +31,9 @@ import {

export {
/** @deprecated moved to the lexical package */ $cloneWithProperties,
/** @deprecated moved to the lexical package */ $selectAll,
} from 'lexical';

export {
$addNodeStyle,
$isAtNodeEnd,
Expand All @@ -48,7 +49,6 @@ export {
$isParentElementRTL,
$moveCaretSelection,
$moveCharacter,
$selectAll,
$setBlocksType,
$shouldOverrideDefaultCharacterSelection,
$wrapNodes,
Expand Down
35 changes: 0 additions & 35 deletions packages/lexical-selection/src/range-selection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -452,41 +452,6 @@ export function $moveCharacter(
);
}

/**
* Expands the current Selection to cover all of the content in the editor.
* @param selection - The current selection.
*/
export function $selectAll(selection: RangeSelection): void {
const anchor = selection.anchor;
const focus = selection.focus;
const anchorNode = anchor.getNode();
const topParent = anchorNode.getTopLevelElementOrThrow();
const root = topParent.getParentOrThrow();
let firstNode = root.getFirstDescendant();
let lastNode = root.getLastDescendant();
let firstType: 'element' | 'text' = 'element';
let lastType: 'element' | 'text' = 'element';
let lastOffset = 0;

if ($isTextNode(firstNode)) {
firstType = 'text';
} else if (!$isElementNode(firstNode) && firstNode !== null) {
firstNode = firstNode.getParentOrThrow();
}

if ($isTextNode(lastNode)) {
lastType = 'text';
lastOffset = lastNode.getTextContentSize();
} else if (!$isElementNode(lastNode) && lastNode !== null) {
lastNode = lastNode.getParentOrThrow();
}

if (firstNode && lastNode) {
anchor.set(firstNode.getKey(), 0, firstType);
focus.set(lastNode.getKey(), lastOffset, lastType);
}
}

/**
* Returns the current value of a CSS property for Nodes, if set. If not set, it returns the defaultValue.
* @param node - The node whose style value to get.
Expand Down
21 changes: 18 additions & 3 deletions packages/lexical/src/LexicalUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1084,10 +1084,25 @@ export function isSelectAll(
return key.toLowerCase() === 'a' && controlOrMeta(metaKey, ctrlKey);
}

export function $selectAll(): void {
export function $selectAll(selection?: RangeSelection | null): RangeSelection {
const root = $getRoot();
const selection = root.select(0, root.getChildrenSize());
$setSelection($normalizeSelection(selection));

if ($isRangeSelection(selection)) {
const anchor = selection.anchor;
const focus = selection.focus;
const anchorNode = anchor.getNode();
const topParent = anchorNode.getTopLevelElementOrThrow();
const rootNode = topParent.getParentOrThrow();
anchor.set(rootNode.getKey(), 0, 'element');
focus.set(rootNode.getKey(), rootNode.getChildrenSize(), 'element');
$normalizeSelection(selection);
return selection;
} else {
// Create a new RangeSelection
const newSelection = root.select(0, root.getChildrenSize());
$setSelection($normalizeSelection(newSelection));
return newSelection;
}
}

export function getCachedClassNameArray(
Expand Down

0 comments on commit 55ef7ca

Please sign in to comment.