Lexical: Changed table esacpe handling

Avoids misuse of selectPrevious/Next as per prior commit which was then
causing problems elsewhere, and is probably best to avoid creation in
those select methods anyway.
This commit is contained in:
Dan Brown 2025-05-26 18:47:51 +01:00
parent a43a1832f5
commit 2e718c12e1
No known key found for this signature in database
GPG Key ID: 46D9F943C24A2EF9
4 changed files with 19 additions and 19 deletions

View File

@ -48,7 +48,6 @@ import {
internalMarkNodeAsDirty, internalMarkNodeAsDirty,
removeFromParent, removeFromParent,
} from './LexicalUtils'; } from './LexicalUtils';
import {$insertAndSelectNewEmptyAdjacentNode} from "../../utils/nodes";
export type NodeMap = Map<NodeKey, LexicalNode>; export type NodeMap = Map<NodeKey, LexicalNode>;
@ -1131,7 +1130,7 @@ export class LexicalNode {
const prevSibling = this.getPreviousSibling(); const prevSibling = this.getPreviousSibling();
const parent = this.getParentOrThrow(); const parent = this.getParentOrThrow();
if (prevSibling === null) { if (prevSibling === null) {
return $insertAndSelectNewEmptyAdjacentNode(this, false); return parent.select(0, 0);
} }
if ($isElementNode(prevSibling)) { if ($isElementNode(prevSibling)) {
return prevSibling.select(); return prevSibling.select();
@ -1153,7 +1152,7 @@ export class LexicalNode {
const nextSibling = this.getNextSibling(); const nextSibling = this.getNextSibling();
const parent = this.getParentOrThrow(); const parent = this.getParentOrThrow();
if (nextSibling === null) { if (nextSibling === null) {
return $insertAndSelectNewEmptyAdjacentNode(this, true); return parent.select();
} }
if ($isElementNode(nextSibling)) { if ($isElementNode(nextSibling)) {
return nextSibling.select(0, 0); return nextSibling.select(0, 0);

View File

@ -71,6 +71,7 @@ import {TableDOMTable, TableObserver} from './LexicalTableObserver';
import {$isTableRowNode} from './LexicalTableRowNode'; import {$isTableRowNode} from './LexicalTableRowNode';
import {$isTableSelection} from './LexicalTableSelection'; import {$isTableSelection} from './LexicalTableSelection';
import {$computeTableMap, $getNodeTriplet} from './LexicalTableUtils'; import {$computeTableMap, $getNodeTriplet} from './LexicalTableUtils';
import {$selectOrCreateAdjacent} from "../../utils/nodes";
const LEXICAL_ELEMENT_KEY = '__lexicalTableSelection'; const LEXICAL_ELEMENT_KEY = '__lexicalTableSelection';
@ -1113,7 +1114,7 @@ const selectTableNodeInDirection = (
false, false,
); );
} else { } else {
tableNode.selectPrevious(); $selectOrCreateAdjacent(tableNode, false);
} }
return true; return true;
@ -1125,7 +1126,7 @@ const selectTableNodeInDirection = (
true, true,
); );
} else { } else {
tableNode.selectNext(); $selectOrCreateAdjacent(tableNode, true);
} }
return true; return true;

View File

@ -13,7 +13,7 @@ import {
import {$isImageNode} from "@lexical/rich-text/LexicalImageNode"; import {$isImageNode} from "@lexical/rich-text/LexicalImageNode";
import {$isMediaNode} from "@lexical/rich-text/LexicalMediaNode"; import {$isMediaNode} from "@lexical/rich-text/LexicalMediaNode";
import {getLastSelection} from "../utils/selection"; import {getLastSelection} from "../utils/selection";
import {$getNearestNodeBlockParent, $getParentOfType} from "../utils/nodes"; import {$getNearestNodeBlockParent, $getParentOfType, $selectOrCreateAdjacent} from "../utils/nodes";
import {$setInsetForSelection} from "../utils/lists"; import {$setInsetForSelection} from "../utils/lists";
import {$isListItemNode} from "@lexical/list"; import {$isListItemNode} from "@lexical/list";
import {$isDetailsNode, DetailsNode} from "@lexical/rich-text/LexicalDetailsNode"; import {$isDetailsNode, DetailsNode} from "@lexical/rich-text/LexicalDetailsNode";
@ -81,13 +81,8 @@ function focusAdjacentOrInsertForSingleSelectNode(editor: LexicalEditor, event:
event?.preventDefault(); event?.preventDefault();
const node = selectionNodes[0]; const node = selectionNodes[0];
editor.update(() => { editor.update(() => {
if (after) { $selectOrCreateAdjacent(node, after);
node.selectNext();
} else {
node.selectPrevious();
}
}); });
return true; return true;

View File

@ -118,15 +118,20 @@ export function $sortNodes(nodes: LexicalNode[]): LexicalNode[] {
return sorted; return sorted;
} }
export function $insertAndSelectNewEmptyAdjacentNode(node: LexicalNode, after: boolean): RangeSelection { export function $selectOrCreateAdjacent(node: LexicalNode, after: boolean): RangeSelection {
const target = $createParagraphNode(); const nearestBlock = $getNearestNodeBlockParent(node) || node;
if (after) { let target = after ? nearestBlock.getNextSibling() : nearestBlock.getPreviousSibling()
node.insertAfter(target)
} else { if (!target) {
node.insertBefore(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 { export function nodeHasAlignment(node: object): node is NodeHasAlignment {