@fileverse-dev/fortune-react 1.3.13-create-3 → 1.3.13-create-4

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.
@@ -1,47 +1,77 @@
1
1
  import { useCallback, useRef } from "react";
2
- import _ from "lodash";
3
- import { escapeScriptTag, functionHTMLGenerate, escapeHTMLTag, handleFormulaInput } from "@fileverse-dev/fortune-core";
4
- import { setCursorPosition } from "../components/SheetOverlay/helper";
2
+ import { escapeScriptTag } from "@fileverse-dev/fortune-core";
3
+ import { getCursorPosition, getSelectionOffsets, setSelectionOffsets, setCursorPosition } from "../components/SheetOverlay/helper";
5
4
  var MAX_FORMULA_HISTORY = 100;
6
- export function useFormulaEditorHistory(primaryRef, cellInputRef, fxInputRef, setContext, primary) {
5
+ function normalizeEditorHtmlSnapshot(html) {
6
+ var stripped = (html !== null && html !== void 0 ? html : "").replace(/\u200B/g, "").trim();
7
+ if (stripped === "" || stripped === "<br>" || stripped === "<div><br></div>" || stripped === "<div></div>") {
8
+ return "";
9
+ }
10
+ return html !== null && html !== void 0 ? html : "";
11
+ }
12
+ function historyForLog(entries, index) {
13
+ return entries.map(function (e, i) {
14
+ var _a, _b;
15
+ return {
16
+ i: i,
17
+ active: i === index,
18
+ len: ((_a = e.html) !== null && _a !== void 0 ? _a : "").length,
19
+ preview: ((_b = e.html) !== null && _b !== void 0 ? _b : "").replace(/\s+/g, " ").slice(0, 24)
20
+ };
21
+ });
22
+ }
23
+ export function useFormulaEditorHistory(primaryRef, cellInputRef, fxInputRef, _setContext, primary) {
7
24
  var preTextRef = useRef("");
8
- var preFormulaSpanValuesRef = useRef(null);
25
+ var preHtmlRef = useRef("");
26
+ var preCaretRef = useRef(0);
27
+ var startedFromEmptyRef = useRef(true);
9
28
  var formulaHistoryRef = useRef({
10
29
  active: false,
11
30
  entries: [],
12
31
  index: -1
13
32
  });
33
+ var isApplyingHistoryRef = useRef(false);
14
34
  var resetFormulaHistory = useCallback(function () {
15
35
  formulaHistoryRef.current = {
16
36
  active: false,
17
37
  entries: [],
18
38
  index: -1
19
39
  };
20
- preFormulaSpanValuesRef.current = null;
40
+ preHtmlRef.current = "";
41
+ preCaretRef.current = 0;
42
+ startedFromEmptyRef.current = true;
21
43
  }, []);
22
44
  var pushFormulaHistoryEntry = useCallback(function (entry) {
23
45
  var history = formulaHistoryRef.current;
24
46
  var current = history.entries[history.index];
25
- if (current && current.spanValues.length > 0 && entry.spanValues.length > 0 && _.isEqual(current.spanValues, entry.spanValues)) {
26
- return;
27
- }
28
- if (current && current.spanValues.length === 0 && entry.spanValues.length === 0 && current.text === entry.text && current.caret === entry.caret) {
47
+ if (current && current.html === entry.html) {
48
+ console.log("[Hist:pushSkipDuplicate]", {
49
+ currentHtmlLen: current.html.length,
50
+ entryHtmlLen: entry.html.length,
51
+ index: history.index
52
+ });
29
53
  return;
30
54
  }
31
55
  var nextEntries = history.entries.slice(0, history.index + 1);
32
56
  nextEntries.push(entry);
33
- if (nextEntries.length > MAX_FORMULA_HISTORY) {
34
- nextEntries.shift();
35
- }
57
+ if (nextEntries.length > MAX_FORMULA_HISTORY) nextEntries.shift();
36
58
  history.entries = nextEntries;
37
59
  history.index = nextEntries.length - 1;
38
60
  history.active = true;
61
+ console.log("[Hist:push]", {
62
+ htmlLen: entry.html.length,
63
+ caret: entry.caret,
64
+ entriesLen: history.entries.length,
65
+ index: history.index,
66
+ entries: historyForLog(history.entries, history.index)
67
+ });
39
68
  }, []);
40
- var applyFormulaHistoryEntry = useCallback(function (entry) {
69
+ var applyFormulaHistoryEntry = useCallback(function (entry, preserveSelection) {
70
+ var _a, _b, _c, _d, _e;
41
71
  var primaryEl = primaryRef.current;
42
72
  if (!primaryEl) return;
43
- var safeText = escapeScriptTag(entry.text || "");
44
- var html = safeText.startsWith("=") ? functionHTMLGenerate(safeText) : escapeHTMLTag(safeText);
73
+ isApplyingHistoryRef.current = true;
74
+ var html = escapeScriptTag((_a = entry.html) !== null && _a !== void 0 ? _a : "");
45
75
  var cell = cellInputRef.current;
46
76
  var fx = fxInputRef.current;
47
77
  primaryEl.innerHTML = html;
@@ -50,70 +80,142 @@ export function useFormulaEditorHistory(primaryRef, cellInputRef, fxInputRef, se
50
80
  } else if (cell) {
51
81
  cell.innerHTML = html;
52
82
  }
53
- setCursorPosition(primaryEl, Math.min(entry.caret, entry.text.length));
54
- setContext(function (draftCtx) {
55
- if (primary === "cell") {
56
- if (!cellInputRef.current) return;
57
- handleFormulaInput(draftCtx, fxInputRef.current, cellInputRef.current, 0);
58
- } else {
59
- if (!fxInputRef.current) return;
60
- handleFormulaInput(draftCtx, cellInputRef.current, fxInputRef.current, 0);
61
- }
83
+ var textLen = (_c = (_b = primaryEl.innerText) === null || _b === void 0 ? void 0 : _b.length) !== null && _c !== void 0 ? _c : 0;
84
+ if (preserveSelection && preserveSelection.end > preserveSelection.start && textLen > 0) {
85
+ var start = Math.max(0, Math.min(preserveSelection.start, textLen));
86
+ var end = Math.max(start, Math.min(preserveSelection.end, textLen));
87
+ setSelectionOffsets(primaryEl, start, end);
88
+ } else {
89
+ setCursorPosition(primaryEl, Math.max(0, textLen));
90
+ }
91
+ console.log("[Hist:apply]", {
92
+ entryHtmlLen: ((_d = entry.html) !== null && _d !== void 0 ? _d : "").length,
93
+ afterHtmlLen: ((_e = primaryEl.innerHTML) !== null && _e !== void 0 ? _e : "").length,
94
+ afterTextLen: textLen
95
+ });
96
+ requestAnimationFrame(function () {
97
+ requestAnimationFrame(function () {
98
+ setTimeout(function () {
99
+ isApplyingHistoryRef.current = false;
100
+ }, 120);
101
+ });
62
102
  });
63
- }, [cellInputRef, fxInputRef, primary, primaryRef, setContext]);
103
+ }, [cellInputRef, fxInputRef, primary, primaryRef]);
64
104
  var handleFormulaHistoryUndoRedo = useCallback(function (isRedo) {
105
+ var _a, _b, _c, _d;
65
106
  var history = formulaHistoryRef.current;
66
107
  if (!history.active || history.entries.length === 0) return false;
67
108
  var nextIndex = isRedo ? history.index + 1 : history.index - 1;
68
- if (nextIndex < 0 || nextIndex >= history.entries.length) return true;
109
+ if (nextIndex < 0 || nextIndex >= history.entries.length) {
110
+ console.log("[Hist:undoRedoBoundary]", {
111
+ isRedo: isRedo,
112
+ index: history.index,
113
+ entriesLen: history.entries.length,
114
+ nextIndex: nextIndex,
115
+ startedFromEmpty: startedFromEmptyRef.current,
116
+ liveHtmlLen: normalizeEditorHtmlSnapshot((_b = (_a = primaryRef.current) === null || _a === void 0 ? void 0 : _a.innerHTML) !== null && _b !== void 0 ? _b : "").length,
117
+ entries: historyForLog(history.entries, history.index)
118
+ });
119
+ if (!isRedo && nextIndex < 0 && startedFromEmptyRef.current) {
120
+ var liveHtml = normalizeEditorHtmlSnapshot((_d = (_c = primaryRef.current) === null || _c === void 0 ? void 0 : _c.innerHTML) !== null && _d !== void 0 ? _d : "");
121
+ if (liveHtml !== "") {
122
+ history.entries[0] = {
123
+ html: "",
124
+ caret: 0
125
+ };
126
+ history.index = 0;
127
+ var currentSelection_1 = primaryRef.current ? getSelectionOffsets(primaryRef.current) : undefined;
128
+ applyFormulaHistoryEntry(history.entries[0], currentSelection_1);
129
+ console.log("[Hist:undoRedoForcedEmpty]", {
130
+ entriesLen: history.entries.length,
131
+ index: history.index,
132
+ entries: historyForLog(history.entries, history.index)
133
+ });
134
+ return true;
135
+ }
136
+ }
137
+ return false;
138
+ }
69
139
  history.index = nextIndex;
70
- applyFormulaHistoryEntry(history.entries[nextIndex]);
140
+ var entry = history.entries[nextIndex];
141
+ var currentSelection = primaryRef.current ? getSelectionOffsets(primaryRef.current) : undefined;
142
+ applyFormulaHistoryEntry(entry, currentSelection);
143
+ console.log("[Hist:undoRedoApplied]", {
144
+ isRedo: isRedo,
145
+ nextIndex: nextIndex,
146
+ entriesLen: history.entries.length,
147
+ entryHtmlLen: entry.html.length,
148
+ entries: historyForLog(history.entries, history.index)
149
+ });
71
150
  return true;
72
- }, [applyFormulaHistoryEntry]);
73
- var capturePreFormulaState = useCallback(function () {
151
+ }, [applyFormulaHistoryEntry, primaryRef]);
152
+ var capturePreEditorHistoryState = useCallback(function () {
153
+ var _a, _b, _c;
74
154
  var el = primaryRef.current;
75
155
  if (!el) return;
76
156
  preTextRef.current = el.innerText;
77
- preFormulaSpanValuesRef.current = Array.from(el.querySelectorAll("span.fortune-formula-functionrange-cell")).map(function (node) {
78
- var _a;
79
- return (_a = node.textContent) !== null && _a !== void 0 ? _a : "";
157
+ preHtmlRef.current = el.innerHTML;
158
+ preCaretRef.current = getCursorPosition(el);
159
+ if (!formulaHistoryRef.current.active) {
160
+ startedFromEmptyRef.current = normalizeEditorHtmlSnapshot((_a = preHtmlRef.current) !== null && _a !== void 0 ? _a : "") === "";
161
+ }
162
+ console.log("[Hist:capturePre]", {
163
+ preHtmlLen: ((_b = preHtmlRef.current) !== null && _b !== void 0 ? _b : "").length,
164
+ preTextLen: ((_c = preTextRef.current) !== null && _c !== void 0 ? _c : "").length,
165
+ preCaret: preCaretRef.current,
166
+ startedFromEmpty: startedFromEmptyRef.current,
167
+ historyActive: formulaHistoryRef.current.active,
168
+ entriesLen: formulaHistoryRef.current.entries.length,
169
+ index: formulaHistoryRef.current.index
80
170
  });
81
171
  }, [primaryRef]);
82
- var appendFormulaHistoryFromPrimaryEditor = useCallback(function (getCaret) {
172
+ var appendEditorHistoryFromPrimaryEditor = useCallback(function (getCaret) {
83
173
  var _a, _b;
174
+ if (isApplyingHistoryRef.current) return;
84
175
  var el = primaryRef.current;
85
176
  if (!el) return;
86
- var currentText = el.innerText || "";
87
- if (currentText.startsWith("=")) {
88
- var caret = getCaret();
89
- var spanValues = Array.from(el.querySelectorAll("span.fortune-formula-functionrange-cell")).map(function (node) {
90
- var _a;
91
- return (_a = node.textContent) !== null && _a !== void 0 ? _a : "";
92
- });
93
- if (!formulaHistoryRef.current.active) {
94
- var seedText = preTextRef.current || "";
95
- pushFormulaHistoryEntry({
96
- text: seedText,
97
- caret: Math.min(caret, seedText.length),
98
- spanValues: (_b = (_a = preFormulaSpanValuesRef.current) !== null && _a !== void 0 ? _a : spanValues) !== null && _b !== void 0 ? _b : []
99
- });
100
- }
177
+ var preHtmlSnapshot = normalizeEditorHtmlSnapshot((_a = preHtmlRef.current) !== null && _a !== void 0 ? _a : "");
178
+ var snapshotHtml = normalizeEditorHtmlSnapshot((_b = el.innerHTML) !== null && _b !== void 0 ? _b : "");
179
+ var caret = getCaret();
180
+ var history = formulaHistoryRef.current;
181
+ var wasInactive = !history.active || history.entries.length === 0;
182
+ console.log("[Hist:appendStart]", {
183
+ preHtmlLen: preHtmlSnapshot.length,
184
+ currentHtmlLen: snapshotHtml.length,
185
+ caret: caret,
186
+ wasInactive: wasInactive,
187
+ historyActive: history.active,
188
+ entriesLen: history.entries.length,
189
+ index: history.index,
190
+ entries: historyForLog(history.entries, history.index)
191
+ });
192
+ if (wasInactive) {
193
+ startedFromEmptyRef.current = preHtmlSnapshot === "";
194
+ var seedHtml = preHtmlSnapshot === snapshotHtml ? "" : preHtmlSnapshot !== null && preHtmlSnapshot !== void 0 ? preHtmlSnapshot : "";
101
195
  pushFormulaHistoryEntry({
102
- text: currentText,
103
- caret: caret,
104
- spanValues: spanValues
196
+ html: seedHtml,
197
+ caret: preCaretRef.current
198
+ });
199
+ } else {
200
+ console.log("[Hist:seedSkippedAlreadyActive]", {
201
+ index: history.index,
202
+ entriesLen: history.entries.length,
203
+ entries: historyForLog(history.entries, history.index)
105
204
  });
106
- } else if (formulaHistoryRef.current.active) {
107
- resetFormulaHistory();
108
205
  }
109
- }, [primaryRef, pushFormulaHistoryEntry, resetFormulaHistory]);
206
+ pushFormulaHistoryEntry({
207
+ html: snapshotHtml,
208
+ caret: caret
209
+ });
210
+ }, [primaryRef, pushFormulaHistoryEntry]);
110
211
  return {
111
212
  formulaHistoryRef: formulaHistoryRef,
112
213
  preTextRef: preTextRef,
113
- preFormulaSpanValuesRef: preFormulaSpanValuesRef,
114
214
  resetFormulaHistory: resetFormulaHistory,
115
215
  handleFormulaHistoryUndoRedo: handleFormulaHistoryUndoRedo,
116
- capturePreFormulaState: capturePreFormulaState,
117
- appendFormulaHistoryFromPrimaryEditor: appendFormulaHistoryFromPrimaryEditor
216
+ capturePreEditorHistoryState: capturePreEditorHistoryState,
217
+ appendEditorHistoryFromPrimaryEditor: appendEditorHistoryFromPrimaryEditor,
218
+ capturePreFormulaState: capturePreEditorHistoryState,
219
+ appendFormulaHistoryFromPrimaryEditor: appendEditorHistoryFromPrimaryEditor
118
220
  };
119
221
  }
@@ -41,12 +41,11 @@ var FxEditor = function FxEditor() {
41
41
  refs = _g.refs;
42
42
  var lastKeyDownEventRef = (0, _react.useRef)(null);
43
43
  var _h = (0, _useFormulaEditorHistory.useFormulaEditorHistory)(refs.fxInput, refs.cellInput, refs.fxInput, setContext, "fx"),
44
- formulaHistoryRef = _h.formulaHistoryRef,
45
44
  preTextRef = _h.preTextRef,
46
45
  resetFormulaHistory = _h.resetFormulaHistory,
47
46
  handleFormulaHistoryUndoRedo = _h.handleFormulaHistoryUndoRedo,
48
- capturePreFormulaState = _h.capturePreFormulaState,
49
- appendFormulaHistoryFromPrimaryEditor = _h.appendFormulaHistoryFromPrimaryEditor;
47
+ capturePreEditorHistoryState = _h.capturePreEditorHistoryState,
48
+ appendEditorHistoryFromPrimaryEditor = _h.appendEditorHistoryFromPrimaryEditor;
50
49
  var inputContainerRef = (0, _react.useRef)(null);
51
50
  var _j = (0, _react.useState)(false),
52
51
  isHidenRC = _j[0],
@@ -288,7 +287,20 @@ var FxEditor = function FxEditor() {
288
287
  var currentCommaCount = (0, _helper.countCommasBeforeCursor)((_a = refs.fxInput) === null || _a === void 0 ? void 0 : _a.current);
289
288
  setCommaCount(currentCommaCount);
290
289
  lastKeyDownEventRef.current = new KeyboardEvent(e.type, e.nativeEvent);
291
- capturePreFormulaState();
290
+ if (!(0, _helper.isEditorUndoRedoKeyEvent)(e.nativeEvent)) {
291
+ capturePreEditorHistoryState();
292
+ }
293
+ var isPotentialContentKey = !e.metaKey && !e.ctrlKey && !e.altKey && (e.key.length === 1 || e.key === "Backspace" || e.key === "Delete" || e.key === "Enter" || e.key === "Tab");
294
+ if (isPotentialContentKey) {
295
+ requestAnimationFrame(function () {
296
+ requestAnimationFrame(function () {
297
+ if ((0, _fortuneCore.getFormulaEditorOwner)(context) !== "fx") return;
298
+ appendEditorHistoryFromPrimaryEditor(function () {
299
+ return (0, _helper.getCursorPosition)(refs.fxInput.current);
300
+ });
301
+ });
302
+ });
303
+ }
292
304
  recentText.current = refs.fxInput.current.innerText;
293
305
  var key = e.key;
294
306
  var currentInputText = ((_c = (_b = refs.fxInput.current) === null || _b === void 0 ? void 0 : _b.innerText) === null || _c === void 0 ? void 0 : _c.trim()) || "";
@@ -340,14 +352,20 @@ var FxEditor = function FxEditor() {
340
352
  }
341
353
  if ((e.metaKey || e.ctrlKey) && context.luckysheetCellUpdate.length > 0) {
342
354
  if (e.code === "KeyZ" || e.code === "KeyY") {
343
- var shouldUseFormulaHistory = currentInputText.startsWith("=") || formulaHistoryRef.current.active;
344
- if (shouldUseFormulaHistory) {
345
- var handledByFormulaHistory = handleFormulaHistoryUndoRedo(e.code === "KeyY" || e.code === "KeyZ" && e.shiftKey);
346
- if (handledByFormulaHistory) {
347
- e.preventDefault();
348
- }
349
- }
355
+ var isRedo_1 = e.code === "KeyY" || e.code === "KeyZ" && e.shiftKey;
356
+ e.preventDefault();
350
357
  e.stopPropagation();
358
+ var _attempt_ = function attempt_1(triesLeft) {
359
+ var handled = handleFormulaHistoryUndoRedo(isRedo_1);
360
+ if (handled) return;
361
+ if (triesLeft <= 0) return;
362
+ requestAnimationFrame(function () {
363
+ requestAnimationFrame(function () {
364
+ return _attempt_(triesLeft - 1);
365
+ });
366
+ });
367
+ };
368
+ _attempt_(2);
351
369
  return;
352
370
  }
353
371
  }
@@ -440,7 +458,7 @@ var FxEditor = function FxEditor() {
440
458
  }
441
459
  }
442
460
  });
443
- }, [capturePreFormulaState, context.allowEdit, context.luckysheetCellUpdate, handleFormulaHistoryUndoRedo, refs.cellInput, refs.fxInput, setContext]);
461
+ }, [capturePreEditorHistoryState, context.allowEdit, context.luckysheetCellUpdate, handleFormulaHistoryUndoRedo, refs.cellInput, refs.fxInput, setContext]);
444
462
  var handleHideShowHint = function handleHideShowHint() {
445
463
  var _a, _b, _c, _d;
446
464
  var el = (_a = document.getElementsByClassName("cell-hint")) === null || _a === void 0 ? void 0 : _a[0];
@@ -497,17 +515,23 @@ var FxEditor = function FxEditor() {
497
515
  }
498
516
  return;
499
517
  }
518
+ if ((0, _helper.isEditorUndoRedoKeyEvent)(e)) {
519
+ return;
520
+ }
500
521
  var kcode = e.keyCode;
501
522
  if (!kcode) return;
502
- appendFormulaHistoryFromPrimaryEditor(function () {
503
- return (0, _helper.getCursorPosition)(refs.fxInput.current);
504
- });
505
523
  if (!(kcode >= 112 && kcode <= 123 || kcode <= 46 || kcode === 144 || kcode === 108 || e.ctrlKey || e.altKey || e.shiftKey && (kcode === 37 || kcode === 38 || kcode === 39 || kcode === 40)) || kcode === 8 || kcode === 32 || kcode === 46 || e.ctrlKey && kcode === 86) {
506
524
  setContext(function (draftCtx) {
507
525
  (0, _fortuneCore.handleFormulaInput)(draftCtx, refs.cellInput.current, refs.fxInput.current, kcode, recentText.current);
508
526
  });
509
527
  }
510
- }, [appendFormulaHistoryFromPrimaryEditor, context.isFlvReadOnly, context.luckysheetCellUpdate, refs.cellInput, refs.fxInput, setContext]);
528
+ requestAnimationFrame(function () {
529
+ if ((0, _fortuneCore.getFormulaEditorOwner)(context) !== "fx") return;
530
+ appendEditorHistoryFromPrimaryEditor(function () {
531
+ return (0, _helper.getCursorPosition)(refs.fxInput.current);
532
+ });
533
+ });
534
+ }, [appendEditorHistoryFromPrimaryEditor, context.isFlvReadOnly, context.luckysheetCellUpdate, refs.cellInput, refs.fxInput, setContext]);
511
535
  (0, _useRerenderOnFormulaCaret.useRerenderOnFormulaCaret)(refs.fxInput, context.luckysheetCellUpdate.length > 0);
512
536
  var getFunctionNameFromInput = (0, _react.useCallback)(function () {
513
537
  var _a, _b, _c, _d;
@@ -50,10 +50,11 @@ var ContentEditable = function ContentEditable(_a) {
50
50
  if (root.current != null) {
51
51
  html = root.current.innerHTML;
52
52
  }
53
- if (onChange && html !== lastHtml.current) {
54
- onChange(html || "", isBlur);
53
+ var htmlStr = html || "";
54
+ if (onChange && htmlStr !== lastHtml.current) {
55
+ onChange(htmlStr, isBlur);
55
56
  }
56
- lastHtml.current = html || "";
57
+ lastHtml.current = htmlStr;
57
58
  }, [root, onChange]);
58
59
  var innerRef = props.innerRef,
59
60
  _onBlur = props.onBlur;
@@ -87,12 +87,11 @@ var InputBox = function InputBox() {
87
87
  var skipNextAnchorSelectionSyncRef = (0, _react.useRef)(false);
88
88
  var lastHandledMouseDragSignatureRef = (0, _react.useRef)("");
89
89
  var _2 = (0, _useFormulaEditorHistory.useFormulaEditorHistory)(inputRef, refs.cellInput, refs.fxInput, setContext, "cell"),
90
- formulaHistoryRef = _2.formulaHistoryRef,
91
90
  preTextRef = _2.preTextRef,
92
91
  resetFormulaHistory = _2.resetFormulaHistory,
93
92
  handleFormulaHistoryUndoRedo = _2.handleFormulaHistoryUndoRedo,
94
- capturePreFormulaState = _2.capturePreFormulaState,
95
- appendFormulaHistoryFromPrimaryEditor = _2.appendFormulaHistoryFromPrimaryEditor;
93
+ capturePreEditorHistoryState = _2.capturePreEditorHistoryState,
94
+ appendEditorHistoryFromPrimaryEditor = _2.appendEditorHistoryFromPrimaryEditor;
96
95
  var ZWSP = "\u200B";
97
96
  var inputBoxInnerRef = (0, _react.useRef)(null);
98
97
  var _3 = (0, _react.useState)([]),
@@ -414,12 +413,25 @@ var InputBox = function InputBox() {
414
413
  });
415
414
  }, [formulaMouseDragSignature, context.formulaCache.func_selectedrange, refs.cellInput, setContext]);
416
415
  var onKeyDown = (0, _react.useCallback)(function (e) {
417
- var _a, _b, _c;
416
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
418
417
  setContext(function (draftCtx) {
419
418
  (0, _fortuneCore.setFormulaEditorOwner)(draftCtx, "cell");
420
419
  });
421
420
  lastKeyDownEventRef.current = new KeyboardEvent(e.type, e.nativeEvent);
422
- capturePreFormulaState();
421
+ if (!(0, _helper.isEditorUndoRedoKeyEvent)(e.nativeEvent)) {
422
+ capturePreEditorHistoryState();
423
+ }
424
+ var isPotentialContentKey = !e.metaKey && !e.ctrlKey && !e.altKey && (e.key.length === 1 || e.key === "Backspace" || e.key === "Delete" || e.key === "Enter" || e.key === "Tab");
425
+ if (isPotentialContentKey) {
426
+ requestAnimationFrame(function () {
427
+ requestAnimationFrame(function () {
428
+ if ((0, _fortuneCore.getFormulaEditorOwner)(context) !== "cell") return;
429
+ appendEditorHistoryFromPrimaryEditor(function () {
430
+ return (0, _helper.getCursorPosition)(inputRef.current);
431
+ });
432
+ });
433
+ });
434
+ }
423
435
  var currentInputText = ((_b = (_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.innerText) === null || _b === void 0 ? void 0 : _b.trim()) || "";
424
436
  if ((e.key === "=" || currentInputText.startsWith("=")) && context.luckysheetCellUpdate.length === 2 && formulaAnchorCellRef.current == null) {
425
437
  setContext(function (draftCtx) {
@@ -436,9 +448,9 @@ var InputBox = function InputBox() {
436
448
  setContext(function (draftCtx) {
437
449
  draftCtx.formulaCache.rangeSelectionActive = null;
438
450
  });
439
- var _d = formulaAnchorCellRef.current,
440
- anchorRow_1 = _d[0],
441
- anchorCol_1 = _d[1];
451
+ var _m = formulaAnchorCellRef.current,
452
+ anchorRow_1 = _m[0],
453
+ anchorCol_1 = _m[1];
442
454
  skipNextAnchorSelectionSyncRef.current = true;
443
455
  setTimeout(function () {
444
456
  setContext(function (draftCtx) {
@@ -464,39 +476,93 @@ var InputBox = function InputBox() {
464
476
  }
465
477
  if ((e.metaKey || e.ctrlKey) && context.luckysheetCellUpdate.length > 0) {
466
478
  if (e.code === "KeyZ" || e.code === "KeyY") {
467
- var shouldUseFormulaHistory = currentInputText.startsWith("=") || formulaHistoryRef.current.active;
468
- if (shouldUseFormulaHistory) {
469
- var handledByFormulaHistory = handleFormulaHistoryUndoRedo(e.code === "KeyY" || e.code === "KeyZ" && e.shiftKey);
470
- if (handledByFormulaHistory) {
471
- e.preventDefault();
472
- }
473
- }
479
+ var isRedo_1 = e.code === "KeyY" || e.code === "KeyZ" && e.shiftKey;
480
+ e.preventDefault();
474
481
  e.stopPropagation();
482
+ var _attempt_ = function attempt_1(triesLeft) {
483
+ var handled = handleFormulaHistoryUndoRedo(isRedo_1);
484
+ if (handled) return;
485
+ if (triesLeft <= 0) return;
486
+ requestAnimationFrame(function () {
487
+ requestAnimationFrame(function () {
488
+ return _attempt_(triesLeft - 1);
489
+ });
490
+ });
491
+ };
492
+ _attempt_(2);
475
493
  return;
476
494
  }
477
495
  if (e.code === "KeyB") {
496
+ var beforeHtml_1 = (_d = (_c = inputRef.current) === null || _c === void 0 ? void 0 : _c.innerHTML) !== null && _d !== void 0 ? _d : "";
478
497
  (0, _fortuneCore.handleBold)(context, inputRef.current);
479
- appendFormulaHistoryFromPrimaryEditor(function () {
480
- return (0, _helper.getCursorPosition)(inputRef.current);
481
- });
498
+ var _pushFormattingSnapshot_ = function pushFormattingSnapshot_1(triesLeft) {
499
+ requestAnimationFrame(function () {
500
+ var _a, _b;
501
+ var afterHtml = (_b = (_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.innerHTML) !== null && _b !== void 0 ? _b : "";
502
+ if (afterHtml !== beforeHtml_1 || triesLeft <= 0) {
503
+ appendEditorHistoryFromPrimaryEditor(function () {
504
+ return (0, _helper.getCursorPosition)(inputRef.current);
505
+ });
506
+ return;
507
+ }
508
+ _pushFormattingSnapshot_(triesLeft - 1);
509
+ });
510
+ };
511
+ _pushFormattingSnapshot_(2);
482
512
  stopPropagation(e);
483
513
  } else if (e.code === "KeyI") {
514
+ var beforeHtml_2 = (_f = (_e = inputRef.current) === null || _e === void 0 ? void 0 : _e.innerHTML) !== null && _f !== void 0 ? _f : "";
484
515
  (0, _fortuneCore.handleItalic)(context, inputRef.current);
485
- appendFormulaHistoryFromPrimaryEditor(function () {
486
- return (0, _helper.getCursorPosition)(inputRef.current);
487
- });
516
+ var _pushFormattingSnapshot_2 = function pushFormattingSnapshot_2(triesLeft) {
517
+ requestAnimationFrame(function () {
518
+ var _a, _b;
519
+ var afterHtml = (_b = (_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.innerHTML) !== null && _b !== void 0 ? _b : "";
520
+ if (afterHtml !== beforeHtml_2 || triesLeft <= 0) {
521
+ appendEditorHistoryFromPrimaryEditor(function () {
522
+ return (0, _helper.getCursorPosition)(inputRef.current);
523
+ });
524
+ return;
525
+ }
526
+ _pushFormattingSnapshot_2(triesLeft - 1);
527
+ });
528
+ };
529
+ _pushFormattingSnapshot_2(2);
488
530
  stopPropagation(e);
489
531
  } else if (e.code === "KeyU") {
532
+ var beforeHtml_3 = (_h = (_g = inputRef.current) === null || _g === void 0 ? void 0 : _g.innerHTML) !== null && _h !== void 0 ? _h : "";
490
533
  (0, _fortuneCore.handleUnderline)(context, inputRef.current);
491
- appendFormulaHistoryFromPrimaryEditor(function () {
492
- return (0, _helper.getCursorPosition)(inputRef.current);
493
- });
534
+ var _pushFormattingSnapshot_3 = function pushFormattingSnapshot_3(triesLeft) {
535
+ requestAnimationFrame(function () {
536
+ var _a, _b;
537
+ var afterHtml = (_b = (_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.innerHTML) !== null && _b !== void 0 ? _b : "";
538
+ if (afterHtml !== beforeHtml_3 || triesLeft <= 0) {
539
+ appendEditorHistoryFromPrimaryEditor(function () {
540
+ return (0, _helper.getCursorPosition)(inputRef.current);
541
+ });
542
+ return;
543
+ }
544
+ _pushFormattingSnapshot_3(triesLeft - 1);
545
+ });
546
+ };
547
+ _pushFormattingSnapshot_3(2);
494
548
  stopPropagation(e);
495
549
  } else if (e.code === "KeyS") {
550
+ var beforeHtml_4 = (_k = (_j = inputRef.current) === null || _j === void 0 ? void 0 : _j.innerHTML) !== null && _k !== void 0 ? _k : "";
496
551
  (0, _fortuneCore.handleStrikeThrough)(context, inputRef.current);
497
- appendFormulaHistoryFromPrimaryEditor(function () {
498
- return (0, _helper.getCursorPosition)(inputRef.current);
499
- });
552
+ var _pushFormattingSnapshot_4 = function pushFormattingSnapshot_4(triesLeft) {
553
+ requestAnimationFrame(function () {
554
+ var _a, _b;
555
+ var afterHtml = (_b = (_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.innerHTML) !== null && _b !== void 0 ? _b : "";
556
+ if (afterHtml !== beforeHtml_4 || triesLeft <= 0) {
557
+ appendEditorHistoryFromPrimaryEditor(function () {
558
+ return (0, _helper.getCursorPosition)(inputRef.current);
559
+ });
560
+ return;
561
+ }
562
+ _pushFormattingSnapshot_4(triesLeft - 1);
563
+ });
564
+ };
565
+ _pushFormattingSnapshot_4(2);
500
566
  stopPropagation(e);
501
567
  }
502
568
  }
@@ -506,7 +572,7 @@ var InputBox = function InputBox() {
506
572
  }
507
573
  var allowListNavigation = true;
508
574
  var isArrowKey = e.key === "ArrowUp" || e.key === "ArrowDown" || e.key === "ArrowLeft" || e.key === "ArrowRight";
509
- var isInPlaceEditMode = ((_c = refs.globalCache) === null || _c === void 0 ? void 0 : _c.enteredEditByTyping) !== true;
575
+ var isInPlaceEditMode = ((_l = refs.globalCache) === null || _l === void 0 ? void 0 : _l.enteredEditByTyping) !== true;
510
576
  if (e.key === "Delete" || e.key === "Backspace") {
511
577
  var anchor = formulaAnchorCellRef.current;
512
578
  if (anchor != null) {
@@ -595,7 +661,7 @@ var InputBox = function InputBox() {
595
661
  }
596
662
  e.preventDefault();
597
663
  }
598
- }, [capturePreFormulaState, clearSearchItemActiveClass, context.luckysheetCellUpdate.length, handleFormulaHistoryUndoRedo, selectActiveFormula, setContext, firstSelection, refs.cellInput]);
664
+ }, [capturePreEditorHistoryState, clearSearchItemActiveClass, context.luckysheetCellUpdate.length, handleFormulaHistoryUndoRedo, selectActiveFormula, setContext, firstSelection, refs.cellInput]);
599
665
  var handleHideShowHint = function handleHideShowHint() {
600
666
  var _a, _b, _c, _d;
601
667
  var searchElFx = (_a = document.getElementsByClassName("fx-search")) === null || _a === void 0 ? void 0 : _a[0];
@@ -637,11 +703,11 @@ var InputBox = function InputBox() {
637
703
  }
638
704
  return;
639
705
  }
706
+ if ((0, _helper.isEditorUndoRedoKeyEvent)(e)) {
707
+ return;
708
+ }
640
709
  var kcode = e.keyCode;
641
710
  if (!kcode) return;
642
- appendFormulaHistoryFromPrimaryEditor(function () {
643
- return (0, _helper.getCursorPosition)(inputRef.current);
644
- });
645
711
  if (!(kcode >= 112 && kcode <= 123 || kcode <= 46 || kcode === 144 || kcode === 108 || e.ctrlKey || e.altKey || e.shiftKey && (kcode === 37 || kcode === 38 || kcode === 39 || kcode === 40)) || kcode === 8 || kcode === 32 || kcode === 46 || e.ctrlKey && kcode === 86) {
646
712
  setContext(function (draftCtx) {
647
713
  var _a, _b;
@@ -656,7 +722,13 @@ var InputBox = function InputBox() {
656
722
  }
657
723
  });
658
724
  }
659
- }, [refs.cellInput, refs.fxInput, setContext, appendFormulaHistoryFromPrimaryEditor]);
725
+ requestAnimationFrame(function () {
726
+ if ((0, _fortuneCore.getFormulaEditorOwner)(context) !== "cell") return;
727
+ appendEditorHistoryFromPrimaryEditor(function () {
728
+ return (0, _helper.getCursorPosition)(inputRef.current);
729
+ });
730
+ });
731
+ }, [refs.cellInput, refs.fxInput, setContext, appendEditorHistoryFromPrimaryEditor]);
660
732
  var onPaste = (0, _react.useCallback)(function (e) {
661
733
  var plainText = e.clipboardData.getData("text/plain");
662
734
  e.preventDefault();
@@ -1,6 +1,13 @@
1
1
  export declare function moveCursorToEnd(editableDiv: HTMLDivElement): void;
2
2
  export declare function getCursorPosition(editableDiv: HTMLDivElement): number;
3
+ export declare function getSelectionOffsets(editableDiv: HTMLDivElement): {
4
+ start: number;
5
+ end: number;
6
+ };
7
+ export declare function isEditorUndoRedoKeyEvent(e: KeyboardEvent): boolean;
8
+ export declare function shouldUseCustomEditorHistory(editorInnerTextTrimmed: string, historyActive: boolean): boolean;
3
9
  export declare function setCursorPosition(editableDiv: HTMLDivElement, targetOffset: number): void;
10
+ export declare function setSelectionOffsets(editableDiv: HTMLDivElement, startOffset: number, endOffset: number): void;
4
11
  export declare function buildFormulaSuggestionText(editableDiv: HTMLDivElement, formulaName: string): {
5
12
  text: string;
6
13
  caretOffset: number;