internalMarkNodeAsDirty,
removeFromParent,
} from './LexicalUtils';
-import {$insertAndSelectNewEmptyAdjacentNode} from "../../utils/nodes";
export type NodeMap = Map<NodeKey, LexicalNode>;
const prevSibling = this.getPreviousSibling();
const parent = this.getParentOrThrow();
if (prevSibling === null) {
- return $insertAndSelectNewEmptyAdjacentNode(this, false);
+ return parent.select(0, 0);
}
if ($isElementNode(prevSibling)) {
return prevSibling.select();
const nextSibling = this.getNextSibling();
const parent = this.getParentOrThrow();
if (nextSibling === null) {
- return $insertAndSelectNewEmptyAdjacentNode(this, true);
+ return parent.select();
}
if ($isElementNode(nextSibling)) {
return nextSibling.select(0, 0);
import {$isTableRowNode} from './LexicalTableRowNode';
import {$isTableSelection} from './LexicalTableSelection';
import {$computeTableMap, $getNodeTriplet} from './LexicalTableUtils';
+import {$selectOrCreateAdjacent} from "../../utils/nodes";
const LEXICAL_ELEMENT_KEY = '__lexicalTableSelection';
false,
);
} else {
- tableNode.selectPrevious();
+ $selectOrCreateAdjacent(tableNode, false);
}
return true;
true,
);
} else {
- tableNode.selectNext();
+ $selectOrCreateAdjacent(tableNode, true);
}
return true;
import {$isImageNode} from "@lexical/rich-text/LexicalImageNode";
import {$isMediaNode} from "@lexical/rich-text/LexicalMediaNode";
import {getLastSelection} from "../utils/selection";
-import {$getNearestNodeBlockParent, $getParentOfType} from "../utils/nodes";
+import {$getNearestNodeBlockParent, $getParentOfType, $selectOrCreateAdjacent} from "../utils/nodes";
import {$setInsetForSelection} from "../utils/lists";
import {$isListItemNode} from "@lexical/list";
import {$isDetailsNode, DetailsNode} from "@lexical/rich-text/LexicalDetailsNode";
event?.preventDefault();
const node = selectionNodes[0];
-
editor.update(() => {
- if (after) {
- node.selectNext();
- } else {
- node.selectPrevious();
- }
+ $selectOrCreateAdjacent(node, after);
});
return true;
return sorted;
}
-export function $insertAndSelectNewEmptyAdjacentNode(node: LexicalNode, after: boolean): RangeSelection {
- const target = $createParagraphNode();
- if (after) {
- node.insertAfter(target)
- } else {
- node.insertBefore(target);
+export function $selectOrCreateAdjacent(node: LexicalNode, after: boolean): RangeSelection {
+ const nearestBlock = $getNearestNodeBlockParent(node) || node;
+ let target = after ? nearestBlock.getNextSibling() : nearestBlock.getPreviousSibling()
+
+ if (!target) {
+ target = $createParagraphNode();
+ if (after) {
+ node.insertAfter(target)
+ } else {
+ node.insertBefore(target);
+ }
}
- return target.select();
+ return after ? target.selectStart() : target.selectEnd();
}
export function nodeHasAlignment(node: object): node is NodeHasAlignment {