Skip to content

NOW MANAGED ON CODEBERG

Lexical: Changed table esacpe handling

Dan Brown2e718c12e120
diff --git a/resources/js/wysiwyg/lexical/core/LexicalNode.ts b/resources/js/wysiwyg/lexical/core/LexicalNode.ts
index e54cd1066..7306e6bca 100644
--- a/resources/js/wysiwyg/lexical/core/LexicalNode.ts
+++ b/resources/js/wysiwyg/lexical/core/LexicalNode.ts
@@ -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);
diff --git a/resources/js/wysiwyg/lexical/table/LexicalTableSelectionHelpers.ts b/resources/js/wysiwyg/lexical/table/LexicalTableSelectionHelpers.ts
index d9164a778..448019669 100644
--- a/resources/js/wysiwyg/lexical/table/LexicalTableSelectionHelpers.ts
+++ b/resources/js/wysiwyg/lexical/table/LexicalTableSelectionHelpers.ts
@@ -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;
diff --git a/resources/js/wysiwyg/services/keyboard-handling.ts b/resources/js/wysiwyg/services/keyboard-handling.ts
index 39818acb0..b4f546117 100644
--- a/resources/js/wysiwyg/services/keyboard-handling.ts
+++ b/resources/js/wysiwyg/services/keyboard-handling.ts
@@ -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;
diff --git a/resources/js/wysiwyg/utils/nodes.ts b/resources/js/wysiwyg/utils/nodes.ts
index ebf01e39d..778be5ba6 100644
--- a/resources/js/wysiwyg/utils/nodes.ts
+++ b/resources/js/wysiwyg/utils/nodes.ts
@@ -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 {