@lobehub/editor 1.8.6 → 1.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -23,3 +23,10 @@ export declare function isDocumentFragment(x: unknown): x is DocumentFragment;
23
23
  export declare function getParentElement(node: Node): HTMLElement | null;
24
24
  export declare function $getNearestNodeFromDOMNode(startingDOM: Node, editor: LexicalEditor, editorState?: ReturnType<LexicalEditor['getEditorState']>): LexicalNode | null;
25
25
  export declare function getKernelFromEditor(editor: LexicalEditor): IEditorKernel;
26
+ /**
27
+ *
28
+ * @param nodeA
29
+ * @param nodeB
30
+ * @returns
31
+ */
32
+ export declare function compareNodeOrder(nodeA: LexicalNode, nodeB: LexicalNode): number;
@@ -68,4 +68,44 @@ export function getKernelFromEditor(editor) {
68
68
  var _editor$_createEditor;
69
69
  // @ts-expect-error __kernel is injected into the lexical editor instance
70
70
  return ((_editor$_createEditor = editor._createEditorArgs) === null || _editor$_createEditor === void 0 ? void 0 : _editor$_createEditor.__kernel) || editor._kernel;
71
+ }
72
+
73
+ /**
74
+ *
75
+ * @param nodeA
76
+ * @param nodeB
77
+ * @returns
78
+ */
79
+ export function compareNodeOrder(nodeA, nodeB) {
80
+ if (nodeA === nodeB) {
81
+ return 0;
82
+ }
83
+ var pathA = [];
84
+ var pathB = [];
85
+ var currentA = nodeA;
86
+ var currentB = nodeB;
87
+ while (currentA) {
88
+ pathA.unshift(currentA);
89
+ currentA = currentA.getParent();
90
+ }
91
+ while (currentB) {
92
+ pathB.unshift(currentB);
93
+ currentB = currentB.getParent();
94
+ }
95
+ var minLength = Math.min(pathA.length, pathB.length);
96
+ for (var i = 0; i < minLength; i++) {
97
+ if (pathA[i] !== pathB[i]) {
98
+ var _pathA$i$getParent;
99
+ var siblings = ((_pathA$i$getParent = pathA[i].getParent()) === null || _pathA$i$getParent === void 0 ? void 0 : _pathA$i$getParent.getChildren()) || [];
100
+ return siblings.indexOf(pathA[i]) - siblings.indexOf(pathB[i]);
101
+ }
102
+ }
103
+
104
+ // If all nodes in the shorter path are the same, the shorter path's node comes first
105
+ if (pathA.length !== pathB.length) {
106
+ return pathA.length - pathB.length;
107
+ }
108
+
109
+ // This case should not happen as we checked for equality at the start
110
+ return 0;
71
111
  }
@@ -202,7 +202,7 @@ export function registerRichKeydown(editor, kernel, options) {
202
202
  });
203
203
  event.preventDefault();
204
204
  return true;
205
- } else if (possibleNode) {
205
+ } else if (possibleNode && possibleNode.getType() !== 'linebreak') {
206
206
  possibleNode === null || possibleNode === void 0 || possibleNode.selectEnd();
207
207
  event.preventDefault();
208
208
  return true;
@@ -254,7 +254,7 @@ export function registerRichKeydown(editor, kernel, options) {
254
254
  });
255
255
  event.preventDefault();
256
256
  return true;
257
- } else if (possibleNode) {
257
+ } else if (possibleNode && possibleNode.getType() !== 'linebreak') {
258
258
  possibleNode === null || possibleNode === void 0 || possibleNode.selectStart();
259
259
  event.preventDefault();
260
260
  return true;
@@ -5,10 +5,11 @@ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len
5
5
  function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
6
6
  function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
7
7
  import { mergeRegister } from '@lexical/utils';
8
- import { $getSelection, $isElementNode, $isNodeSelection, $isRangeSelection, $isTextNode } from 'lexical';
8
+ import { $getNodeByKey, $getSelection, $isNodeSelection, $isRangeSelection } from 'lexical';
9
9
  import { memo, useCallback, useEffect, useRef, useState } from 'react';
10
10
  import { useLexicalComposerContext, useLexicalEditor } from "../../../../editor-kernel/react";
11
11
  import PortalAnchor from "../../../../editor-kernel/react/PortalAnchor";
12
+ import { compareNodeOrder } from "../../../../editor-kernel/utils";
12
13
  import { SELECT_MATH_SIDE_COMMAND, UPDATE_MATH_COMMAND } from "../../command";
13
14
  import { $isMathNode, MathBlockNode } from "../../node";
14
15
  import MathEditorContainer from "./MathEditorContainer";
@@ -64,8 +65,10 @@ var MathEdit = /*#__PURE__*/memo(function (_ref) {
64
65
  return lexicalEditor.getElementByKey(mathNode.getKey());
65
66
  });
66
67
  if (currentNode) {
67
- var writableNode = mathNode.getWritable();
68
- writableNode.__code = value;
68
+ lexicalEditor.update(function () {
69
+ var writableNode = mathNode.getWritable();
70
+ writableNode.__code = value;
71
+ });
69
72
  }
70
73
  return;
71
74
  }
@@ -81,8 +84,8 @@ var MathEdit = /*#__PURE__*/memo(function (_ref) {
81
84
  return lexicalEditor.getElementByKey(mathNode.getKey());
82
85
  });
83
86
  if (currentNode) {
84
- var _writableNode = mathNode.getWritable();
85
- _writableNode.__code = value;
87
+ var writableNode = mathNode.getWritable();
88
+ writableNode.__code = value;
86
89
  }
87
90
  });
88
91
  // 延迟重置更新标志,确保更新监听器不会立即触发
@@ -204,25 +207,23 @@ var MathEdit = /*#__PURE__*/memo(function (_ref) {
204
207
  return node;
205
208
  });
206
209
  if (canEdit) {
207
- var node = prevEditorState.read(function () {
210
+ var anchorNodeKey = prevEditorState.read(function () {
208
211
  var sel = prevEditorState._selection;
209
- if (!$isRangeSelection(sel) || !sel.isCollapsed()) {
212
+ if (!$isRangeSelection(sel)) {
210
213
  return false;
211
214
  }
212
- var node = sel.anchor.getNode();
213
- if ($isTextNode(node)) {
214
- return node.getNextSibling();
215
- }
216
- if (!$isElementNode(node)) {
217
- return false;
215
+ var node = sel.anchor.key;
216
+ return node;
217
+ });
218
+ editor.read(function () {
219
+ var anchorNode = anchorNodeKey ? $getNodeByKey(anchorNodeKey) : null;
220
+ var isPrev = anchorNode && compareNodeOrder(anchorNode, canEdit) < 0;
221
+ if (isPrev) {
222
+ setPrev(true);
223
+ } else {
224
+ setPrev(false);
218
225
  }
219
- return node.getChildAtIndex(sel.anchor.offset);
220
226
  });
221
- if (canEdit === node) {
222
- setPrev(true);
223
- } else {
224
- setPrev(false);
225
- }
226
227
  setIsOpen(true);
227
228
  }
228
229
  if (!canEdit) {
@@ -252,8 +253,9 @@ var MathEdit = /*#__PURE__*/memo(function (_ref) {
252
253
  lastValidRef.current = value;
253
254
  }
254
255
  },
255
- onValueChange: setValue,
256
- prev: prev,
256
+ onValueChange: setValue
257
+ // prev={prev}
258
+ ,
257
259
  value: value
258
260
  });
259
261
 
@@ -16,6 +16,8 @@ var MathEditorContainer = /*#__PURE__*/memo(function (_ref) {
16
16
  return updatePosition({
17
17
  callback: function callback() {
18
18
  if (!divRef.current || !mathDOM) return;
19
+ // 展示的时候聚焦
20
+ onFocus === null || onFocus === void 0 || onFocus();
19
21
  if (!isBlockMode) {
20
22
  divRef.current.style.width = '';
21
23
  return;
@@ -21,7 +21,6 @@ export interface MathEditorContentProps {
21
21
  /** 值变化回调 */
22
22
  onValueChange: (value: string) => void;
23
23
  /** 是否从前一个位置进入 */
24
- prev: boolean;
25
24
  /** 当前输入值 */
26
25
  value: string;
27
26
  }
@@ -25,7 +25,6 @@ var MathEditorContent = /*#__PURE__*/memo(function (_ref) {
25
25
  onSubmit = _ref.onSubmit,
26
26
  onValidationChange = _ref.onValidationChange,
27
27
  onValueChange = _ref.onValueChange,
28
- prev = _ref.prev,
29
28
  value = _ref.value;
30
29
  var t = useTranslation();
31
30
  var textareaRef = useRef(null);
@@ -41,16 +40,15 @@ var MathEditorContent = /*#__PURE__*/memo(function (_ref) {
41
40
  focusRef === null || focusRef === void 0 || focusRef(textareaRef.current);
42
41
  }, [focusRef]);
43
42
 
44
- // 聚焦和光标位置处理
45
- useEffect(function () {
46
- if (textareaRef.current) {
47
- textareaRef.current.focus();
48
- if (prev) {
49
- var _textareaRef$current$;
50
- (_textareaRef$current$ = textareaRef.current.resizableTextArea) === null || _textareaRef$current$ === void 0 || (_textareaRef$current$ = _textareaRef$current$.textArea) === null || _textareaRef$current$ === void 0 || _textareaRef$current$.setSelectionRange(0, 0);
51
- }
52
- }
53
- }, [prev]);
43
+ // // 聚焦和光标位置处理
44
+ // useEffect(() => {
45
+ // if (textareaRef.current) {
46
+ // textareaRef.current.focus();
47
+ // if (prev) {
48
+ // textareaRef.current.resizableTextArea?.textArea?.setSelectionRange(0, 0);
49
+ // }
50
+ // }
51
+ // }, [prev]);
54
52
 
55
53
  // 实时验证 LaTeX 语法
56
54
  useEffect(function () {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lobehub/editor",
3
- "version": "1.8.6",
3
+ "version": "1.9.0",
4
4
  "description": "A powerful and extensible rich text editor built on Meta's Lexical framework, providing a modern editing experience with React integration.",
5
5
  "keywords": [
6
6
  "lobehub",