Skip to content

Commit

Permalink
Unify implementations and deprecate @lexical/selection
Browse files Browse the repository at this point in the history
  • Loading branch information
Hadi Elghoul authored and Hadi Elghoul committed Dec 3, 2024
1 parent b7fa4cf commit 6113603
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 37 deletions.
5 changes: 3 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 @@ -30,6 +29,9 @@ import {
getStyleObjectFromCSS,
} from './utils';

export {
/** @deprecated moved to the lexical package */ $selectAll,
} from 'lexical';
export {
/** @deprecated moved to the lexical package */ $cloneWithProperties,
} from 'lexical';
Expand All @@ -48,7 +50,6 @@ export {
$isParentElementRTL,
$moveCaretSelection,
$moveCharacter,
$selectAll,
$setBlocksType,
$shouldOverrideDefaultCharacterSelection,
$wrapNodes,
Expand Down
34 changes: 2 additions & 32 deletions packages/lexical-selection/src/range-selection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -453,39 +453,9 @@ export function $moveCharacter(
}

/**
* Expands the current Selection to cover all of the content in the editor.
* @param selection - The current selection.
* @deprecated This function has been moved to the lexical package. Use the $selectAll function from the lexical package instead.
*/
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);
}
}
export {$selectAll} from 'lexical';

/**
* Returns the current value of a CSS property for Nodes, if set. If not set, it returns the defaultValue.
Expand Down
45 changes: 42 additions & 3 deletions packages/lexical/src/LexicalUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1084,10 +1084,49 @@ 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 (selection && $isRangeSelection(selection)) {
// Expand the existing RangeSelection
const anchor = selection.anchor;
const focus = selection.focus;
const anchorNode = anchor.getNode();
const topParent = anchorNode.getTopLevelElementOrThrow();
const rootNode = topParent.getParentOrThrow();
let firstNode = rootNode.getFirstDescendant();
let lastNode = rootNode.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);
}

// Normalize and set the updated selection
$setSelection($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 6113603

Please sign in to comment.