]> BookStack Code Mirror - bookstack/commitdiff
Lexical: Changed table esacpe handling
authorDan Brown <redacted>
Mon, 26 May 2025 17:47:51 +0000 (18:47 +0100)
committerDan Brown <redacted>
Mon, 26 May 2025 17:47:51 +0000 (18:47 +0100)
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.

resources/js/wysiwyg/lexical/core/LexicalNode.ts
resources/js/wysiwyg/lexical/table/LexicalTableSelectionHelpers.ts
resources/js/wysiwyg/services/keyboard-handling.ts
resources/js/wysiwyg/utils/nodes.ts

index e54cd1066c2050322750dd8a654aefbd5a1fb554..7306e6bca2755a578826f4bf87f37d24935ed465 100644 (file)
@@ -48,7 +48,6 @@ import {
   internalMarkNodeAsDirty,
   removeFromParent,
 } from './LexicalUtils';
-import {$insertAndSelectNewEmptyAdjacentNode} from "../../utils/nodes";
 
 export type NodeMap = Map<NodeKey, LexicalNode>;
 
@@ -1131,7 +1130,7 @@ export class 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();
@@ -1153,7 +1152,7 @@ export class LexicalNode {
     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);
index d9164a778ce5094cf19285424dd7a25ec78dc7e1..44801966996cb1e14c4dc81a8183e6213b3c3dd9 100644 (file)
@@ -71,6 +71,7 @@ import {TableDOMTable, TableObserver} from './LexicalTableObserver';
 import {$isTableRowNode} from './LexicalTableRowNode';
 import {$isTableSelection} from './LexicalTableSelection';
 import {$computeTableMap, $getNodeTriplet} from './LexicalTableUtils';
+import {$selectOrCreateAdjacent} from "../../utils/nodes";
 
 const LEXICAL_ELEMENT_KEY = '__lexicalTableSelection';
 
@@ -1113,7 +1114,7 @@ const selectTableNodeInDirection = (
           false,
         );
       } else {
-        tableNode.selectPrevious();
+        $selectOrCreateAdjacent(tableNode, false);
       }
 
       return true;
@@ -1125,7 +1126,7 @@ const selectTableNodeInDirection = (
           true,
         );
       } else {
-        tableNode.selectNext();
+        $selectOrCreateAdjacent(tableNode, true);
       }
 
       return true;
index 39818acb09e167fccae454edccc27675298bf479..b4f546117bb9951959f7ea21e2f08b5ef5fd5bcf 100644 (file)
@@ -13,7 +13,7 @@ import {
 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";
@@ -81,13 +81,8 @@ function focusAdjacentOrInsertForSingleSelectNode(editor: LexicalEditor, event:
 
     event?.preventDefault();
     const node = selectionNodes[0];
-
     editor.update(() => {
-        if (after) {
-            node.selectNext();
-        } else {
-            node.selectPrevious();
-        }
+        $selectOrCreateAdjacent(node, after);
     });
 
     return true;
index ebf01e39ddef50a291a5b5b00558b7e7d1bf1d9b..778be5ba6e0a97d32c4b024c3011c228c32806a1 100644 (file)
@@ -118,15 +118,20 @@ export function $sortNodes(nodes: LexicalNode[]): LexicalNode[] {
     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 {