Skip to content

Commit

Permalink
selectEnd/start as a method of LexicalNode instead of ElementNode (#5205
Browse files Browse the repository at this point in the history
)

Co-authored-by: EgonBolton <[email protected]>
  • Loading branch information
GermanJablo and GermanJablo authored Dec 1, 2023
1 parent edbb1e4 commit 0238ea2
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 32 deletions.
2 changes: 1 addition & 1 deletion packages/lexical-website/docs/concepts/selection.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ editor.update(() => {
someNode.selectPrevious();
someNode.selectNext();

// On element nodes, you can use these.
// You can use this on any node.
someNode.selectStart();
someNode.selectEnd();

Expand Down
8 changes: 8 additions & 0 deletions packages/lexical/src/LexicalNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1024,6 +1024,14 @@ export class LexicalNode {
return $createParagraphNode();
}

selectStart(): RangeSelection {
return this.selectPrevious();
}

selectEnd(): RangeSelection {
return this.selectNext(0, 0);
}

/**
* Moves selection to the previous sibling of this node, at the specified offsets.
*
Expand Down
19 changes: 4 additions & 15 deletions packages/lexical/src/LexicalSelection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1556,6 +1556,7 @@ export class RangeSelection implements BaseSelection {
return selection.insertNodes(nodes);
}
const firstBlock = $getAncestor(this.anchor.getNode(), INTERNAL_$isBlock)!;
const last = nodes[nodes.length - 1]!;

// CASE 1: insert inside a code block
if ('__language' in firstBlock) {
Expand All @@ -1564,10 +1565,7 @@ export class RangeSelection implements BaseSelection {
} else {
const index = removeTextAndSplitBlock(this);
firstBlock.splice(index, 0, nodes);
const last = nodes[nodes.length - 1]!;
if (last.select) {
last.select();
} else last.selectNext(0, 0);
last.selectEnd();
}
return;
}
Expand All @@ -1576,22 +1574,13 @@ export class RangeSelection implements BaseSelection {
const notInline = (node: LexicalNode) =>
($isElementNode(node) || $isDecoratorNode(node)) && !node.isInline();

const last = nodes[nodes.length - 1]!;
const nodeToSelect = $isElementNode(last)
? last.getLastDescendant() || last
: last;
const nodeToSelectSize = nodeToSelect.getTextContentSize();
function restoreSelection() {
if (nodeToSelect.select) {
nodeToSelect.select(nodeToSelectSize, nodeToSelectSize);
} else {
nodeToSelect.selectNext(0, 0);
}
}
if (!nodes.some(notInline)) {
const index = removeTextAndSplitBlock(this);
firstBlock.splice(index, 0, nodes);
restoreSelection();
nodeToSelect.selectEnd();
return;
}

Expand Down Expand Up @@ -1631,7 +1620,7 @@ export class RangeSelection implements BaseSelection {
firstBlock.remove();
}

restoreSelection();
nodeToSelect.selectEnd();

// To understand this take a look at the test "can wrap post-linebreak nodes into new element"
const lastChild = $isElementNode(firstBlock)
Expand Down
18 changes: 2 additions & 16 deletions packages/lexical/src/nodes/LexicalElementNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,25 +339,11 @@ export class ElementNode extends LexicalNode {
}
selectStart(): RangeSelection {
const firstNode = this.getFirstDescendant();
if ($isElementNode(firstNode) || $isTextNode(firstNode)) {
return firstNode.select(0, 0);
}
// Decorator or LineBreak
if (firstNode !== null) {
return firstNode.selectPrevious();
}
return this.select(0, 0);
return firstNode ? firstNode.selectStart() : this.select();
}
selectEnd(): RangeSelection {
const lastNode = this.getLastDescendant();
if ($isElementNode(lastNode) || $isTextNode(lastNode)) {
return lastNode.select();
}
// Decorator or LineBreak
if (lastNode !== null) {
return lastNode.selectNext();
}
return this.select();
return lastNode ? lastNode.selectEnd() : this.select();
}
clear(): this {
const writableSelf = this.getWritable();
Expand Down
9 changes: 9 additions & 0 deletions packages/lexical/src/nodes/LexicalTextNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -807,6 +807,15 @@ export class TextNode extends LexicalNode {
return selection;
}

selectStart(): RangeSelection {
return this.select(0, 0);
}

selectEnd(): RangeSelection {
const size = this.getTextContentSize();
return this.select(size, size);
}

/**
* Inserts the provided text into this TextNode at the provided offset, deleting the number of characters
* specified. Can optionally calculate a new selection after the operation is complete.
Expand Down

2 comments on commit 0238ea2

@vercel
Copy link

@vercel vercel bot commented on 0238ea2 Dec 1, 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
lexicaljs.com
lexicaljs.org
www.lexical.dev
lexical-fbopensource.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 0238ea2 Dec 1, 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-git-main-fbopensource.vercel.app
lexical-playground.vercel.app
lexical-playground-fbopensource.vercel.app
playground.lexical.dev

Please sign in to comment.