File tree

4 files changed

+19
-19
lines changed

4 files changed

+19
-19
lines changed
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ import {
4848
internalMarkNodeAsDirty,
4949
removeFromParent,
5050
} from './LexicalUtils';
51-
import {$insertAndSelectNewEmptyAdjacentNode} from "../../utils/nodes";
5251

5352
export type NodeMap = Map<NodeKey, LexicalNode>;
5453

@@ -1131,7 +1130,7 @@ export class LexicalNode {
11311130
const prevSibling = this.getPreviousSibling();
11321131
const parent = this.getParentOrThrow();
11331132
if (prevSibling === null) {
1134-
return $insertAndSelectNewEmptyAdjacentNode(this, false);
1133+
return parent.select(0, 0);
11351134
}
11361135
if ($isElementNode(prevSibling)) {
11371136
return prevSibling.select();
@@ -1153,7 +1152,7 @@ export class LexicalNode {
11531152
const nextSibling = this.getNextSibling();
11541153
const parent = this.getParentOrThrow();
11551154
if (nextSibling === null) {
1156-
return $insertAndSelectNewEmptyAdjacentNode(this, true);
1155+
return parent.select();
11571156
}
11581157
if ($isElementNode(nextSibling)) {
11591158
return nextSibling.select(0, 0);
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ import {TableDOMTable, TableObserver} from './LexicalTableObserver';
7171
import {$isTableRowNode} from './LexicalTableRowNode';
7272
import {$isTableSelection} from './LexicalTableSelection';
7373
import {$computeTableMap, $getNodeTriplet} from './LexicalTableUtils';
74+
import {$selectOrCreateAdjacent} from "../../utils/nodes";
7475

7576
const LEXICAL_ELEMENT_KEY = '__lexicalTableSelection';
7677

@@ -1113,7 +1114,7 @@ const selectTableNodeInDirection = (
11131114
false,
11141115
);
11151116
} else {
1116-
tableNode.selectPrevious();
1117+
$selectOrCreateAdjacent(tableNode, false);
11171118
}
11181119

11191120
return true;
@@ -1125,7 +1126,7 @@ const selectTableNodeInDirection = (
11251126
true,
11261127
);
11271128
} else {
1128-
tableNode.selectNext();
1129+
$selectOrCreateAdjacent(tableNode, true);
11291130
}
11301131

11311132
return true;
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
import {$isImageNode} from "@lexical/rich-text/LexicalImageNode";
1414
import {$isMediaNode} from "@lexical/rich-text/LexicalMediaNode";
1515
import {getLastSelection} from "../utils/selection";
16-
import {$getNearestNodeBlockParent, $getParentOfType} from "../utils/nodes";
16+
import {$getNearestNodeBlockParent, $getParentOfType, $selectOrCreateAdjacent} from "../utils/nodes";
1717
import {$setInsetForSelection} from "../utils/lists";
1818
import {$isListItemNode} from "@lexical/list";
1919
import {$isDetailsNode, DetailsNode} from "@lexical/rich-text/LexicalDetailsNode";
@@ -81,13 +81,8 @@ function focusAdjacentOrInsertForSingleSelectNode(editor: LexicalEditor, event:
8181

8282
event?.preventDefault();
8383
const node = selectionNodes[0];
84-
8584
editor.update(() => {
86-
if (after) {
87-
node.selectNext();
88-
} else {
89-
node.selectPrevious();
90-
}
85+
$selectOrCreateAdjacent(node, after);
9186
});
9287

9388
return true;
Original file line numberDiff line numberDiff line change
@@ -118,15 +118,20 @@ export function $sortNodes(nodes: LexicalNode[]): LexicalNode[] {
118118
return sorted;
119119
}
120120

121-
export function $insertAndSelectNewEmptyAdjacentNode(node: LexicalNode, after: boolean): RangeSelection {
122-
const target = $createParagraphNode();
123-
if (after) {
124-
node.insertAfter(target)
125-
} else {
126-
node.insertBefore(target);
121+
export function $selectOrCreateAdjacent(node: LexicalNode, after: boolean): RangeSelection {
122+
const nearestBlock = $getNearestNodeBlockParent(node) || node;
123+
let target = after ? nearestBlock.getNextSibling() : nearestBlock.getPreviousSibling()
124+
125+
if (!target) {
126+
target = $createParagraphNode();
127+
if (after) {
128+
node.insertAfter(target)
129+
} else {
130+
node.insertBefore(target);
131+
}
127132
}
128133

129-
return target.select();
134+
return after ? target.selectStart() : target.selectEnd();
130135
}
131136

132137
export function nodeHasAlignment(node: object): node is NodeHasAlignment {

0 commit comments

Comments
 (0)