@fileverse-dev/fortune-react 1.3.12 → 1.3.13-create-1

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.
Files changed (51) hide show
  1. package/es/components/ConditionFormat/ConditionRules.js +15 -5
  2. package/es/components/ConditionFormat/index.js +3 -0
  3. package/es/components/ContextMenu/index.js +1 -1
  4. package/es/components/DataVerification/ColorPicker.js +1 -1
  5. package/es/components/FxEditor/index.js +316 -60
  6. package/es/components/SheetOverlay/FormulaHint/index.js +46 -25
  7. package/es/components/SheetOverlay/FormulaSearch/index.d.ts +3 -1
  8. package/es/components/SheetOverlay/FormulaSearch/index.js +58 -29
  9. package/es/components/SheetOverlay/InputBox.js +409 -160
  10. package/es/components/SheetOverlay/drag_and_drop/column-helpers.js +34 -48
  11. package/es/components/SheetOverlay/drag_and_drop/row-helpers.js +31 -41
  12. package/es/components/SheetOverlay/formula-segment-boundary.d.ts +1 -0
  13. package/es/components/SheetOverlay/formula-segment-boundary.js +4 -0
  14. package/es/components/SheetOverlay/helper.d.ts +7 -0
  15. package/es/components/SheetOverlay/helper.js +95 -0
  16. package/es/components/SheetOverlay/index.css +6 -39
  17. package/es/components/SheetOverlay/index.js +29 -17
  18. package/es/components/Toolbar/index.js +17 -12
  19. package/es/components/Workbook/api.d.ts +3 -0
  20. package/es/components/Workbook/index.d.ts +3 -0
  21. package/es/components/Workbook/index.js +9 -3
  22. package/es/hooks/useFormulaEditorHistory.d.ts +24 -0
  23. package/es/hooks/useFormulaEditorHistory.js +119 -0
  24. package/es/hooks/useRerenderOnFormulaCaret.d.ts +2 -0
  25. package/es/hooks/useRerenderOnFormulaCaret.js +28 -0
  26. package/lib/components/ConditionFormat/ConditionRules.js +15 -5
  27. package/lib/components/ConditionFormat/index.js +3 -0
  28. package/lib/components/ContextMenu/index.js +1 -1
  29. package/lib/components/DataVerification/ColorPicker.js +1 -1
  30. package/lib/components/FxEditor/index.js +314 -58
  31. package/lib/components/SheetOverlay/FormulaHint/index.js +45 -24
  32. package/lib/components/SheetOverlay/FormulaSearch/index.d.ts +3 -1
  33. package/lib/components/SheetOverlay/FormulaSearch/index.js +57 -28
  34. package/lib/components/SheetOverlay/InputBox.js +407 -158
  35. package/lib/components/SheetOverlay/drag_and_drop/column-helpers.js +33 -47
  36. package/lib/components/SheetOverlay/drag_and_drop/row-helpers.js +31 -41
  37. package/lib/components/SheetOverlay/formula-segment-boundary.d.ts +1 -0
  38. package/lib/components/SheetOverlay/formula-segment-boundary.js +10 -0
  39. package/lib/components/SheetOverlay/helper.d.ts +7 -0
  40. package/lib/components/SheetOverlay/helper.js +99 -0
  41. package/lib/components/SheetOverlay/index.css +6 -39
  42. package/lib/components/SheetOverlay/index.js +28 -16
  43. package/lib/components/Toolbar/index.js +16 -11
  44. package/lib/components/Workbook/api.d.ts +3 -0
  45. package/lib/components/Workbook/index.d.ts +3 -0
  46. package/lib/components/Workbook/index.js +8 -2
  47. package/lib/hooks/useFormulaEditorHistory.d.ts +24 -0
  48. package/lib/hooks/useFormulaEditorHistory.js +126 -0
  49. package/lib/hooks/useRerenderOnFormulaCaret.d.ts +2 -0
  50. package/lib/hooks/useRerenderOnFormulaCaret.js +34 -0
  51. package/package.json +2 -2
@@ -8,7 +8,7 @@ var __assign = this && this.__assign || function () {
8
8
  };
9
9
  return __assign.apply(this, arguments);
10
10
  };
11
- import { cancelNormalSelected, getCellValue, getInlineStringHTML, getStyleByCell, isInlineStringCell, moveToEnd, getFlowdata, handleFormulaInput, moveHighlightCell, escapeScriptTag, valueShowEs, createRangeHightlight, isShowHidenCR, israngeseleciton, escapeHTMLTag, isAllowEdit, getrangeseleciton, indexToColumnChar, handleBold, handleItalic, handleUnderline, handleStrikeThrough, getRangeRectsByCharacterOffset } from "@fileverse-dev/fortune-core";
11
+ import { cancelNormalSelected, getCellValue, getInlineStringHTML, getStyleByCell, isInlineStringCell, moveToEnd, getFlowdata, handleFormulaInput, moveHighlightCell, escapeScriptTag, valueShowEs, createRangeHightlight, isShowHidenCR, israngeseleciton, escapeHTMLTag, isAllowEdit, indexToColumnChar, functionHTMLGenerate, handleBold, handleItalic, handleUnderline, handleStrikeThrough, getRangeRectsByCharacterOffset, rangeSetValue, getFormulaRangeIndexForKeyboardSync, getFormulaRangeIndexAtCaret, getFormulaEditorOwner, createFormulaRangeSelect, seletedHighlistByindex, isFormulaReferenceInputMode, isCaretAtValidFormulaRangeInsertionPoint, markRangeSelectionDirty, rangeHightlightselected, setFormulaEditorOwner } from "@fileverse-dev/fortune-core";
12
12
  import React, { useContext, useEffect, useMemo, useRef, useCallback, useLayoutEffect, useState } from "react";
13
13
  import _ from "lodash";
14
14
  import { Tooltip } from "@fileverse/ui";
@@ -17,60 +17,78 @@ import ContentEditable from "./ContentEditable";
17
17
  import FormulaSearch from "./FormulaSearch";
18
18
  import FormulaHint from "./FormulaHint";
19
19
  import usePrevious from "../../hooks/usePrevious";
20
- import { moveCursorToEnd, getCursorPosition, isLetterNumberPattern, removeLastSpan, incrementColumn, decrementColumn, incrementRow, decrementRow, countCommasBeforeCursor } from "./helper";
20
+ import { useFormulaEditorHistory } from "../../hooks/useFormulaEditorHistory";
21
+ import { useRerenderOnFormulaCaret } from "../../hooks/useRerenderOnFormulaCaret";
22
+ import { moveCursorToEnd, getCursorPosition, setCursorPosition, buildFormulaSuggestionText, getFunctionNameFromFormulaCaretSpans, isLetterNumberPattern, countCommasBeforeCursor, shouldShowFormulaFunctionList } from "./helper";
23
+ import { isFormulaSegmentBoundaryKey } from "./formula-segment-boundary";
21
24
  import { LucideIcon } from "./LucideIcon";
22
25
  var InputBox = function InputBox() {
23
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
24
- var _m = useContext(WorkbookContext),
25
- context = _m.context,
26
- setContext = _m.setContext,
27
- refs = _m.refs;
26
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p;
27
+ var _q = useContext(WorkbookContext),
28
+ context = _q.context,
29
+ setContext = _q.setContext,
30
+ refs = _q.refs;
28
31
  var inputRef = useRef(null);
29
32
  var lastKeyDownEventRef = useRef(null);
30
33
  var prevCellUpdate = usePrevious(context.luckysheetCellUpdate);
31
34
  var prevSheetId = usePrevious(context.currentSheetId);
32
- var _o = useState(false),
33
- isHidenRC = _o[0],
34
- setIsHidenRC = _o[1];
35
- var _p = useState(false),
36
- isInputBoxActive = _p[0],
37
- setIsInputBoxActive = _p[1];
38
- var _q = useState(""),
39
- activeCell = _q[0],
40
- setActiveCell = _q[1];
41
- var _r = useState(""),
42
- activeRefCell = _r[0],
43
- setActiveRefCell = _r[1];
44
- var _s = useState({
35
+ var _r = useState(false),
36
+ isHidenRC = _r[0],
37
+ setIsHidenRC = _r[1];
38
+ var _s = useState(false),
39
+ isInputBoxActive = _s[0],
40
+ setIsInputBoxActive = _s[1];
41
+ var _t = useState(""),
42
+ activeCell = _t[0],
43
+ setActiveCell = _t[1];
44
+ var _u = useState(""),
45
+ activeRefCell = _u[0],
46
+ setActiveRefCell = _u[1];
47
+ var _v = useState(false),
48
+ showAddressIndicator = _v[0],
49
+ setShowAddressIndicator = _v[1];
50
+ var scrollAtEditSessionStartRef = useRef(null);
51
+ var _w = useState({
45
52
  left: 0,
46
53
  top: 0
47
54
  }),
48
- frozenPosition = _s[0],
49
- setFrozenPosition = _s[1];
55
+ frozenPosition = _w[0],
56
+ setFrozenPosition = _w[1];
50
57
  var firstSelection = (_a = context.luckysheet_select_save) === null || _a === void 0 ? void 0 : _a[0];
51
- var _t = useState({}),
52
- firstSelectionActiveCell = _t[0],
53
- setFirstSelectionActiveCell = _t[1];
54
- var _u = useState(0),
55
- commaCount = _u[0],
56
- setCommaCount = _u[1];
58
+ var _x = useState({}),
59
+ firstSelectionActiveCell = _x[0],
60
+ setFirstSelectionActiveCell = _x[1];
61
+ var _y = useState(0),
62
+ commaCount = _y[0],
63
+ setCommaCount = _y[1];
64
+ var _z = useState(false),
65
+ cellEditorIsFormula = _z[0],
66
+ setCellEditorIsFormula = _z[1];
57
67
  var hideFormulaHintLocal = localStorage.getItem("formulaMore") === "true";
58
- var _v = useState(!hideFormulaHintLocal),
59
- showFormulaHint = _v[0],
60
- setShowFormulaHint = _v[1];
61
- var _w = useState(false),
62
- showSearchHint = _w[0],
63
- setShowSearchHint = _w[1];
68
+ var _0 = useState(!hideFormulaHintLocal),
69
+ showFormulaHint = _0[0],
70
+ setShowFormulaHint = _0[1];
71
+ var _1 = useState(false),
72
+ showSearchHint = _1[0],
73
+ setShowSearchHint = _1[1];
64
74
  var row_index = firstSelection === null || firstSelection === void 0 ? void 0 : firstSelection.row_focus;
65
75
  var col_index = firstSelection === null || firstSelection === void 0 ? void 0 : firstSelection.column_focus;
66
- var preText = useRef("");
67
- var placeRef = useRef("");
68
76
  var isComposingRef = useRef(false);
77
+ var formulaAnchorCellRef = useRef(null);
78
+ var skipNextAnchorSelectionSyncRef = useRef(false);
79
+ var lastHandledMouseDragSignatureRef = useRef("");
80
+ var _2 = useFormulaEditorHistory(inputRef, refs.cellInput, refs.fxInput, setContext, "cell"),
81
+ formulaHistoryRef = _2.formulaHistoryRef,
82
+ preTextRef = _2.preTextRef,
83
+ resetFormulaHistory = _2.resetFormulaHistory,
84
+ handleFormulaHistoryUndoRedo = _2.handleFormulaHistoryUndoRedo,
85
+ capturePreFormulaState = _2.capturePreFormulaState,
86
+ appendFormulaHistoryFromPrimaryEditor = _2.appendFormulaHistoryFromPrimaryEditor;
69
87
  var ZWSP = "\u200B";
70
88
  var inputBoxInnerRef = useRef(null);
71
- var _x = useState([]),
72
- linkSelectionHighlightRects = _x[0],
73
- setLinkSelectionHighlightRects = _x[1];
89
+ var _3 = useState([]),
90
+ linkSelectionHighlightRects = _3[0],
91
+ setLinkSelectionHighlightRects = _3[1];
74
92
  var ensureNotEmpty = function ensureNotEmpty() {
75
93
  var el = inputRef.current;
76
94
  if (!el) return;
@@ -93,12 +111,11 @@ var InputBox = function InputBox() {
93
111
  return lastSpan === null || lastSpan === void 0 ? void 0 : lastSpan.innerText;
94
112
  };
95
113
  var inputBoxStyle = useMemo(function () {
96
- var _a;
97
114
  if (firstSelectionActiveCell && context.luckysheetCellUpdate.length > 0) {
98
115
  var flowdata = getFlowdata(context);
99
116
  if (!flowdata) return {};
100
117
  var style = getStyleByCell(context, flowdata, firstSelectionActiveCell.row_focus, firstSelectionActiveCell.column_focus);
101
- if (((_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.innerText.charAt(0)) === "=") {
118
+ if (cellEditorIsFormula) {
102
119
  style = __assign(__assign({}, style), {
103
120
  textAlign: "left"
104
121
  });
@@ -106,7 +123,7 @@ var InputBox = function InputBox() {
106
123
  return style;
107
124
  }
108
125
  return {};
109
- }, [context.luckysheetfile, context.currentSheetId, context.luckysheetCellUpdate, (_b = context === null || context === void 0 ? void 0 : context.luckysheetCellUpdate) === null || _b === void 0 ? void 0 : _b.length, firstSelectionActiveCell]);
126
+ }, [context.luckysheetfile, context.currentSheetId, context.luckysheetCellUpdate, (_b = context === null || context === void 0 ? void 0 : context.luckysheetCellUpdate) === null || _b === void 0 ? void 0 : _b.length, firstSelectionActiveCell, cellEditorIsFormula]);
110
127
  useLayoutEffect(function () {
111
128
  var _a;
112
129
  if (!context.allowEdit) {
@@ -126,10 +143,23 @@ var InputBox = function InputBox() {
126
143
  if (_.isEqual(prevCellUpdate, context.luckysheetCellUpdate) && prevSheetId === context.currentSheetId) {
127
144
  return;
128
145
  }
146
+ var _b = context.luckysheetCellUpdate,
147
+ ur = _b[0],
148
+ uc = _b[1];
149
+ var pending_1 = refs.globalCache.pendingTypeOverCell;
150
+ if (pending_1 && pending_1[0] === ur && pending_1[1] === uc) {
151
+ refs.globalCache.overwriteCell = false;
152
+ if (inputRef.current) {
153
+ setCellEditorIsFormula(inputRef.current.innerText.trim().startsWith("="));
154
+ }
155
+ delete refs.globalCache.doNotFocus;
156
+ return;
157
+ }
129
158
  var flowdata = getFlowdata(context);
130
159
  var cell = (_a = flowdata === null || flowdata === void 0 ? void 0 : flowdata[row_index]) === null || _a === void 0 ? void 0 : _a[col_index];
160
+ var overwrite = refs.globalCache.overwriteCell;
131
161
  var value_1 = "";
132
- if (cell && !refs.globalCache.overwriteCell) {
162
+ if (cell && !overwrite) {
133
163
  if (isInlineStringCell(cell)) {
134
164
  value_1 = getInlineStringHTML(row_index, col_index, flowdata);
135
165
  } else if (cell.f) {
@@ -145,14 +175,20 @@ var InputBox = function InputBox() {
145
175
  }
146
176
  }
147
177
  refs.globalCache.overwriteCell = false;
178
+ var wroteEditorFromStoredCell = false;
148
179
  if (!refs.globalCache.ignoreWriteCell && inputRef.current && value_1) {
149
180
  inputRef.current.innerHTML = escapeHTMLTag(escapeScriptTag(value_1));
150
- } else if (!refs.globalCache.ignoreWriteCell && inputRef.current && !value_1) {
181
+ wroteEditorFromStoredCell = true;
182
+ } else if (!refs.globalCache.ignoreWriteCell && inputRef.current && !value_1 && !overwrite) {
151
183
  var valueD = getCellValue(row_index, col_index, flowdata, "f");
152
184
  inputRef.current.innerText = valueD;
185
+ wroteEditorFromStoredCell = true;
153
186
  }
154
187
  refs.globalCache.ignoreWriteCell = false;
155
- if (!refs.globalCache.doNotFocus) {
188
+ if (inputRef.current) {
189
+ setCellEditorIsFormula(inputRef.current.innerText.trim().startsWith("="));
190
+ }
191
+ if (wroteEditorFromStoredCell && !refs.globalCache.doNotFocus) {
156
192
  setTimeout(function () {
157
193
  moveToEnd(inputRef.current);
158
194
  });
@@ -165,8 +201,43 @@ var InputBox = function InputBox() {
165
201
  if (inputRef.current) {
166
202
  inputRef.current.innerHTML = "";
167
203
  }
204
+ delete refs.globalCache.pendingTypeOverCell;
205
+ setCellEditorIsFormula(false);
206
+ resetFormulaHistory();
168
207
  }
169
- }, [context.luckysheetCellUpdate]);
208
+ }, [context.luckysheetCellUpdate, resetFormulaHistory, refs.globalCache]);
209
+ useEffect(function () {
210
+ if (_.isEmpty(context.luckysheetCellUpdate)) return;
211
+ delete refs.globalCache.pendingTypeOverCell;
212
+ }, [context.luckysheetCellUpdate, refs.globalCache]);
213
+ useEffect(function () {
214
+ var _a;
215
+ if (_.isEmpty(context.luckysheetCellUpdate) || !refs.cellInput.current) {
216
+ formulaAnchorCellRef.current = null;
217
+ lastHandledMouseDragSignatureRef.current = "";
218
+ return;
219
+ }
220
+ var inputText = ((_a = refs.cellInput.current.innerText) === null || _a === void 0 ? void 0 : _a.trim()) || "";
221
+ if (!inputText.startsWith("=")) {
222
+ formulaAnchorCellRef.current = null;
223
+ lastHandledMouseDragSignatureRef.current = "";
224
+ }
225
+ }, [context.luckysheetCellUpdate, refs.cellInput]);
226
+ useEffect(function () {
227
+ if (_.isEmpty(context.luckysheetCellUpdate) || _.isEmpty(prevCellUpdate) || _.isEqual(prevCellUpdate, context.luckysheetCellUpdate)) {
228
+ return;
229
+ }
230
+ setContext(function (ctx) {
231
+ ctx.formulaRangeHighlight = [];
232
+ ctx.formulaRangeSelect = undefined;
233
+ ctx.formulaCache.selectingRangeIndex = -1;
234
+ ctx.formulaCache.func_selectedrange = undefined;
235
+ ctx.formulaCache.rangestart = false;
236
+ ctx.formulaCache.rangedrag_column_start = false;
237
+ ctx.formulaCache.rangedrag_row_start = false;
238
+ ctx.formulaCache.rangechangeindex = undefined;
239
+ });
240
+ }, [context.luckysheetCellUpdate, prevCellUpdate, setContext]);
170
241
  useEffect(function () {
171
242
  setIsHidenRC(isShowHidenCR(context));
172
243
  }, [context.luckysheet_select_save]);
@@ -176,65 +247,45 @@ var InputBox = function InputBox() {
176
247
  setIsInputBoxActive(false);
177
248
  }
178
249
  }, [firstSelection, (_c = context.rangeDialog) === null || _c === void 0 ? void 0 : _c.show, context.luckysheetCellUpdate]);
250
+ useEffect(function () {
251
+ if (isInputBoxActive) return;
252
+ setContext(function (ctx) {
253
+ if (_.isEmpty(ctx.formulaRangeHighlight) && !ctx.formulaRangeSelect && ctx.formulaCache.selectingRangeIndex === -1 && !ctx.formulaCache.func_selectedrange) {
254
+ return;
255
+ }
256
+ ctx.formulaRangeHighlight = [];
257
+ ctx.formulaRangeSelect = undefined;
258
+ ctx.formulaCache.selectingRangeIndex = -1;
259
+ ctx.formulaCache.func_selectedrange = undefined;
260
+ ctx.formulaCache.rangestart = false;
261
+ ctx.formulaCache.rangedrag_column_start = false;
262
+ ctx.formulaCache.rangedrag_row_start = false;
263
+ ctx.formulaCache.rangechangeindex = undefined;
264
+ ctx.formulaCache.rangeSelectionActive = null;
265
+ });
266
+ }, [isInputBoxActive, setContext]);
179
267
  var getActiveFormula = useCallback(function () {
180
268
  return document.querySelector(".luckysheet-formula-search-item-active");
181
269
  }, []);
182
270
  var insertSelectedFormula = useCallback(function (formulaName) {
183
- var _a;
184
- if (/^=[a-zA-Z]+$/.test(inputRef.current.innerText)) {
185
- var ht = "<span dir=\"auto\" class=\"luckysheet-formula-text-color\">=</span><span dir=\"auto\" class=\"luckysheet-formula-text-func\">".concat(formulaName, "</span><span dir=\"auto\" class=\"luckysheet-formula-text-lpar\">(</span>");
186
- inputRef.current.innerHTML = ht;
187
- var fxEditor = document.getElementById("luckysheet-functionbox-cell");
188
- if (fxEditor) {
189
- fxEditor.innerHTML = ht;
190
- }
191
- moveCursorToEnd(inputRef.current);
192
- setContext(function (draftCtx) {
193
- draftCtx.functionCandidates = [];
194
- draftCtx.defaultCandidates = [];
195
- draftCtx.functionHint = formulaName;
196
- });
197
- return;
198
- }
199
- var textEditor = document.getElementById("luckysheet-rich-text-editor");
271
+ var textEditor = inputRef.current;
200
272
  if (!textEditor) return;
201
- textEditor.focus();
202
- var selection = window.getSelection();
203
- var range = (selection === null || selection === void 0 ? void 0 : selection.rangeCount) ? selection.getRangeAt(0) : null;
204
- if (!selection || !range || !textEditor.contains(range.startContainer)) {
205
- range = document.createRange();
206
- range.selectNodeContents(textEditor);
207
- range.collapse(false);
208
- selection = window.getSelection();
209
- selection === null || selection === void 0 ? void 0 : selection.removeAllRanges();
210
- selection === null || selection === void 0 ? void 0 : selection.addRange(range);
211
- }
212
- var searchTxt = ((_a = getrangeseleciton()) === null || _a === void 0 ? void 0 : _a.textContent) || "";
213
- var deleteCount = searchTxt === "=" ? 0 : searchTxt.length;
214
- if (deleteCount > 0 && range.startContainer.nodeType === Node.TEXT_NODE && textEditor.contains(range.startContainer)) {
215
- var startOffset = Math.max(range.startOffset - deleteCount, 0);
216
- var endOffset = range.startOffset;
217
- range.setStart(range.startContainer, startOffset);
218
- range.setEnd(range.startContainer, endOffset);
219
- range.deleteContents();
220
- }
221
- textEditor.querySelectorAll(".luckysheet-formula-text-func, .luckysheet-formula-text-lpar").forEach(function (el) {
222
- return el.remove();
223
- });
224
- var funcNode = new DOMParser().parseFromString("<span dir=\"auto\" class=\"luckysheet-formula-text-func\">".concat(formulaName, "</span>"), "text/html").body.firstChild;
225
- var parNode = new DOMParser().parseFromString("<span dir=\"auto\" class=\"luckysheet-formula-text-lpar\">(</span>", "text/html").body.firstChild;
226
- if (range && parNode && funcNode) {
227
- range.insertNode(funcNode);
228
- range.collapse(false);
229
- range.insertNode(parNode);
230
- range.collapse(false);
231
- selection === null || selection === void 0 ? void 0 : selection.removeAllRanges();
232
- selection === null || selection === void 0 ? void 0 : selection.addRange(range);
233
- }
273
+ var fxEditor = document.getElementById("luckysheet-functionbox-cell");
274
+ var _a = buildFormulaSuggestionText(textEditor, formulaName),
275
+ text = _a.text,
276
+ caretOffset = _a.caretOffset;
277
+ var safeText = escapeScriptTag(text);
278
+ var html = safeText.startsWith("=") ? functionHTMLGenerate(safeText) : escapeHTMLTag(safeText);
279
+ textEditor.innerHTML = html;
280
+ if (fxEditor) {
281
+ fxEditor.innerHTML = html;
282
+ }
283
+ setCursorPosition(textEditor, caretOffset);
284
+ setShowSearchHint(shouldShowFormulaFunctionList(textEditor));
234
285
  setContext(function (draftCtx) {
235
286
  draftCtx.functionCandidates = [];
236
287
  draftCtx.defaultCandidates = [];
237
- draftCtx.functionHint = formulaName;
288
+ draftCtx.functionHint = (formulaName || "").toUpperCase();
238
289
  });
239
290
  }, [setContext]);
240
291
  var clearSearchItemActiveClass = useCallback(function () {
@@ -257,7 +308,7 @@ var InputBox = function InputBox() {
257
308
  var _a, _b;
258
309
  if (isComposingRef.current || !inputRef.current) return;
259
310
  if (e.target.className.includes("sign-fortune")) return;
260
- preText.current = inputRef.current.innerText;
311
+ preTextRef.current = inputRef.current.innerText;
261
312
  var formulaName = (_b = (_a = getActiveFormula()) === null || _a === void 0 ? void 0 : _a.querySelector(".luckysheet-formula-search-func")) === null || _b === void 0 ? void 0 : _b.textContent;
262
313
  var lastSpanText = getLastInputSpanText();
263
314
  if (formulaName && !isLetterNumberPattern(lastSpanText)) {
@@ -270,11 +321,150 @@ var InputBox = function InputBox() {
270
321
  event.stopPropagation();
271
322
  event.preventDefault();
272
323
  };
324
+ useEffect(function () {
325
+ var _a;
326
+ var cellInputEl = refs.cellInput.current;
327
+ if (!((_a = context.luckysheet_select_save) === null || _a === void 0 ? void 0 : _a[0]) || !cellInputEl) return;
328
+ setContext(function (ctx) {
329
+ var _a;
330
+ var currentSelection = (_a = ctx.luckysheet_select_save) === null || _a === void 0 ? void 0 : _a[ctx.luckysheet_select_save.length - 1];
331
+ if (!currentSelection) return;
332
+ if (getFormulaEditorOwner(ctx) === "fx") {
333
+ return;
334
+ }
335
+ israngeseleciton(ctx);
336
+ var keyboardSyncRangeIndex = getFormulaRangeIndexForKeyboardSync(cellInputEl);
337
+ if (skipNextAnchorSelectionSyncRef.current && formulaAnchorCellRef.current) {
338
+ var _b = formulaAnchorCellRef.current,
339
+ anchorRow = _b[0],
340
+ anchorCol = _b[1];
341
+ var isAnchorSelection = currentSelection.row_focus === anchorRow && currentSelection.column_focus === anchorCol;
342
+ if (isAnchorSelection) {
343
+ skipNextAnchorSelectionSyncRef.current = false;
344
+ return;
345
+ }
346
+ }
347
+ var isFormulaMode = isFormulaReferenceInputMode(ctx);
348
+ if (!isFormulaMode) return;
349
+ if (keyboardSyncRangeIndex !== null) {
350
+ ctx.formulaCache.rangechangeindex = keyboardSyncRangeIndex;
351
+ ctx.formulaCache.rangestart = true;
352
+ ctx.formulaCache.rangedrag_column_start = false;
353
+ ctx.formulaCache.rangedrag_row_start = false;
354
+ } else {
355
+ ctx.formulaCache.rangechangeindex = undefined;
356
+ ctx.formulaCache.rangestart = false;
357
+ }
358
+ ctx.formulaCache.rangeSelectionActive = true;
359
+ rangeSetValue === null || rangeSetValue === void 0 ? void 0 : rangeSetValue(ctx, cellInputEl, {
360
+ row: currentSelection.row,
361
+ column: currentSelection.column
362
+ }, refs.fxInput.current);
363
+ rangeHightlightselected(ctx, cellInputEl);
364
+ if (!_.isNil(ctx.formulaCache.rangechangeindex)) {
365
+ ctx.formulaCache.selectingRangeIndex = ctx.formulaCache.rangechangeindex;
366
+ createRangeHightlight(ctx, cellInputEl.innerHTML, ctx.formulaCache.rangechangeindex);
367
+ var rectFromSelection = seletedHighlistByindex(ctx, currentSelection.row[0], currentSelection.row[1], currentSelection.column[0], currentSelection.column[1]);
368
+ if (rectFromSelection) {
369
+ createFormulaRangeSelect(ctx, {
370
+ rangeIndex: ctx.formulaCache.rangechangeindex || 0,
371
+ left: rectFromSelection.left,
372
+ top: rectFromSelection.top,
373
+ width: rectFromSelection.width,
374
+ height: rectFromSelection.height
375
+ });
376
+ }
377
+ }
378
+ });
379
+ }, [context.luckysheet_select_save, (_d = context.rangeDialog) === null || _d === void 0 ? void 0 : _d.show]);
380
+ var formulaMouseDragSignature = function () {
381
+ var _a, _b, _c, _d;
382
+ var r = context.formulaCache.func_selectedrange;
383
+ if (!r) return "";
384
+ return [(_a = r.row) === null || _a === void 0 ? void 0 : _a[0], (_b = r.row) === null || _b === void 0 ? void 0 : _b[1], (_c = r.column) === null || _c === void 0 ? void 0 : _c[0], (_d = r.column) === null || _d === void 0 ? void 0 : _d[1], r.left_move, r.top_move, r.width_move, r.height_move].join(":");
385
+ }();
386
+ useEffect(function () {
387
+ var _a;
388
+ if (!formulaMouseDragSignature) return;
389
+ if (lastHandledMouseDragSignatureRef.current === formulaMouseDragSignature) {
390
+ return;
391
+ }
392
+ if (!refs.cellInput.current) return;
393
+ var inputText = ((_a = refs.cellInput.current.innerText) === null || _a === void 0 ? void 0 : _a.trim()) || "";
394
+ if (!inputText.startsWith("=")) return;
395
+ var dragRange = context.formulaCache.func_selectedrange;
396
+ if (!dragRange) return;
397
+ lastHandledMouseDragSignatureRef.current = formulaMouseDragSignature;
398
+ setContext(function (ctx) {
399
+ ctx.luckysheet_select_save = [{
400
+ row: [dragRange.row[0], dragRange.row[1]],
401
+ column: [dragRange.column[0], dragRange.column[1]],
402
+ row_focus: _.isNil(dragRange.row_focus) ? dragRange.row[0] : dragRange.row_focus,
403
+ column_focus: _.isNil(dragRange.column_focus) ? dragRange.column[0] : dragRange.column_focus
404
+ }];
405
+ });
406
+ }, [formulaMouseDragSignature, context.formulaCache.func_selectedrange, refs.cellInput, setContext]);
273
407
  var onKeyDown = useCallback(function (e) {
274
- var _a, _b;
408
+ var _a, _b, _c;
409
+ setContext(function (draftCtx) {
410
+ setFormulaEditorOwner(draftCtx, "cell");
411
+ });
275
412
  lastKeyDownEventRef.current = new KeyboardEvent(e.type, e.nativeEvent);
276
- preText.current = inputRef.current.innerText;
277
- if (e.metaKey && context.luckysheetCellUpdate.length > 0) {
413
+ capturePreFormulaState();
414
+ var currentInputText = ((_b = (_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.innerText) === null || _b === void 0 ? void 0 : _b.trim()) || "";
415
+ if ((e.key === "=" || currentInputText.startsWith("=")) && context.luckysheetCellUpdate.length === 2 && formulaAnchorCellRef.current == null) {
416
+ setContext(function (draftCtx) {
417
+ draftCtx.formulaCache.rangeSelectionActive = null;
418
+ });
419
+ formulaAnchorCellRef.current = [context.luckysheetCellUpdate[0], context.luckysheetCellUpdate[1]];
420
+ }
421
+ if (e.key === "(" && currentInputText.startsWith("=")) {
422
+ setContext(function (draftCtx) {
423
+ draftCtx.formulaCache.rangeSelectionActive = null;
424
+ });
425
+ }
426
+ if (isFormulaSegmentBoundaryKey(e.key) && context.luckysheetCellUpdate.length > 0 && currentInputText.startsWith("=") && formulaAnchorCellRef.current) {
427
+ setContext(function (draftCtx) {
428
+ draftCtx.formulaCache.rangeSelectionActive = null;
429
+ });
430
+ var _d = formulaAnchorCellRef.current,
431
+ anchorRow_1 = _d[0],
432
+ anchorCol_1 = _d[1];
433
+ skipNextAnchorSelectionSyncRef.current = true;
434
+ setTimeout(function () {
435
+ setContext(function (draftCtx) {
436
+ var _a, _b;
437
+ draftCtx.luckysheetCellUpdate = [anchorRow_1, anchorCol_1];
438
+ draftCtx.luckysheet_select_save = [{
439
+ row: [anchorRow_1, anchorRow_1],
440
+ column: [anchorCol_1, anchorCol_1],
441
+ row_focus: anchorRow_1,
442
+ column_focus: anchorCol_1
443
+ }];
444
+ draftCtx.formulaRangeSelect = undefined;
445
+ draftCtx.formulaCache.selectingRangeIndex = -1;
446
+ draftCtx.formulaCache.func_selectedrange = undefined;
447
+ draftCtx.formulaCache.rangechangeindex = undefined;
448
+ draftCtx.formulaCache.rangestart = false;
449
+ draftCtx.formulaCache.rangedrag_column_start = false;
450
+ draftCtx.formulaCache.rangedrag_row_start = false;
451
+ createRangeHightlight(draftCtx, ((_a = refs.cellInput.current) === null || _a === void 0 ? void 0 : _a.innerHTML) || ((_b = refs.fxInput.current) === null || _b === void 0 ? void 0 : _b.innerHTML) || "");
452
+ moveHighlightCell(draftCtx, "down", 0, "rangeOfSelect");
453
+ });
454
+ }, 0);
455
+ }
456
+ if ((e.metaKey || e.ctrlKey) && context.luckysheetCellUpdate.length > 0) {
457
+ if (e.code === "KeyZ" || e.code === "KeyY") {
458
+ var shouldUseFormulaHistory = currentInputText.startsWith("=") || formulaHistoryRef.current.active;
459
+ if (shouldUseFormulaHistory) {
460
+ var handledByFormulaHistory = handleFormulaHistoryUndoRedo(e.code === "KeyY" || e.code === "KeyZ" && e.shiftKey);
461
+ if (handledByFormulaHistory) {
462
+ e.preventDefault();
463
+ }
464
+ }
465
+ e.stopPropagation();
466
+ return;
467
+ }
278
468
  if (e.code === "KeyB") {
279
469
  handleBold(context, inputRef.current);
280
470
  stopPropagation(e);
@@ -294,7 +484,32 @@ var InputBox = function InputBox() {
294
484
  setCommaCount(currentCommaCount);
295
485
  }
296
486
  var allowListNavigation = true;
487
+ var isArrowKey = e.key === "ArrowUp" || e.key === "ArrowDown" || e.key === "ArrowLeft" || e.key === "ArrowRight";
488
+ var isInPlaceEditMode = ((_c = refs.globalCache) === null || _c === void 0 ? void 0 : _c.enteredEditByTyping) !== true;
297
489
  if (e.key === "Delete" || e.key === "Backspace") {
490
+ var anchor = formulaAnchorCellRef.current;
491
+ if (anchor != null) {
492
+ var anchorRow_2 = anchor[0],
493
+ anchorCol_2 = anchor[1];
494
+ skipNextAnchorSelectionSyncRef.current = true;
495
+ setTimeout(function () {
496
+ setContext(function (draftCtx) {
497
+ draftCtx.luckysheetCellUpdate = [anchorRow_2, anchorCol_2];
498
+ draftCtx.luckysheet_select_save = [{
499
+ row: [anchorRow_2, anchorRow_2],
500
+ column: [anchorCol_2, anchorCol_2],
501
+ row_focus: anchorRow_2,
502
+ column_focus: anchorCol_2
503
+ }];
504
+ markRangeSelectionDirty(draftCtx);
505
+ var el = refs.cellInput.current;
506
+ if (el && el.innerText.trim().startsWith("=")) {
507
+ createRangeHightlight(draftCtx, el.innerHTML);
508
+ rangeHightlightselected(draftCtx, el);
509
+ }
510
+ });
511
+ }, 0);
512
+ }
298
513
  if (isComposingRef.current) requestAnimationFrame(ensureNotEmpty);
299
514
  if (getCursorPosition(inputRef === null || inputRef === void 0 ? void 0 : inputRef.current) === (inputRef === null || inputRef === void 0 ? void 0 : inputRef.current.innerText.length)) {
300
515
  setTimeout(function () {
@@ -302,39 +517,8 @@ var InputBox = function InputBox() {
302
517
  }, 5);
303
518
  }
304
519
  }
305
- var refCell = placeRef.current;
306
- if (e.key === "ArrowUp") {
307
- refCell = decrementRow(placeRef.current);
308
- } else if (e.key === "ArrowDown") {
309
- refCell = incrementRow(placeRef.current);
310
- } else if (e.key === "ArrowLeft") {
311
- refCell = decrementColumn(placeRef.current);
312
- } else if (e.key === "ArrowRight") {
313
- refCell = incrementColumn(placeRef.current);
314
- }
315
- if ((e.key === "ArrowUp" || e.key === "ArrowDown" || e.key === "ArrowLeft" || e.key === "ArrowRight") && !(getCursorPosition(inputRef === null || inputRef === void 0 ? void 0 : inputRef.current) !== (inputRef === null || inputRef === void 0 ? void 0 : inputRef.current.innerText.length) && e.key === "ArrowRight")) {
316
- var parser = new DOMParser();
317
- var doc = parser.parseFromString("<div>".concat((_a = inputRef === null || inputRef === void 0 ? void 0 : inputRef.current) === null || _a === void 0 ? void 0 : _a.innerHTML, "</div>"), "text/html");
318
- var spans = doc.querySelectorAll("span");
319
- var lastSpan = spans[spans.length - 1];
320
- var notFunctionInit = !((_b = document.getElementById("luckysheet-rich-text-editor")) === null || _b === void 0 ? void 0 : _b.innerText.includes("("));
321
- var arrowRefNotAllowed = (lastSpan === null || lastSpan === void 0 ? void 0 : lastSpan.innerText.includes(")")) || notFunctionInit && /^[a-zA-Z]+$/.test(lastSpan === null || lastSpan === void 0 ? void 0 : lastSpan.innerText) && !_.includes(["="], lastSpan === null || lastSpan === void 0 ? void 0 : lastSpan.innerText);
322
- if (((lastSpan === null || lastSpan === void 0 ? void 0 : lastSpan.innerText) === "(" || (lastSpan === null || lastSpan === void 0 ? void 0 : lastSpan.innerText) === "," || (lastSpan === null || lastSpan === void 0 ? void 0 : lastSpan.innerText.includes(":")) || (lastSpan === null || lastSpan === void 0 ? void 0 : lastSpan.innerText) !== ")") && !(lastSpan === null || lastSpan === void 0 ? void 0 : lastSpan.innerText.includes('"')) && !isLetterNumberPattern(lastSpan === null || lastSpan === void 0 ? void 0 : lastSpan.innerText) && !arrowRefNotAllowed && !/^[a-zA-Z]+$/.test(lastSpan === null || lastSpan === void 0 ? void 0 : lastSpan.innerText)) {
323
- allowListNavigation = false;
324
- inputRef.current.innerHTML = "".concat(inputRef.current.innerHTML, "<span class=\"fortune-formula-functionrange-cell\" rangeindex=\"0\" dir=\"auto\" style=\"color:#c1232b;\">").concat(refCell, "</span>");
325
- setTimeout(function () {
326
- moveCursorToEnd(inputRef.current);
327
- }, 1);
328
- }
329
- if (isLetterNumberPattern(lastSpan === null || lastSpan === void 0 ? void 0 : lastSpan.innerText)) {
330
- allowListNavigation = false;
331
- var htmlR = removeLastSpan(inputRef === null || inputRef === void 0 ? void 0 : inputRef.current.innerHTML);
332
- inputRef.current.innerHTML = "".concat(htmlR, "<span class=\"fortune-formula-functionrange-cell\" rangeindex=\"0\" dir=\"auto\" style=\"color:#c1232b;\">").concat(refCell, "</span>");
333
- moveCursorToEnd(inputRef.current);
334
- setTimeout(function () {
335
- moveCursorToEnd(inputRef.current);
336
- }, 1);
337
- }
520
+ if (isArrowKey && isFormulaReferenceInputMode(context)) {
521
+ allowListNavigation = false;
338
522
  }
339
523
  if (e.key === "Escape" && context.luckysheetCellUpdate.length > 0) {
340
524
  setContext(function (draftCtx) {
@@ -353,7 +537,7 @@ var InputBox = function InputBox() {
353
537
  e.preventDefault();
354
538
  } else if (e.key === "F4" && context.luckysheetCellUpdate.length > 0) {
355
539
  e.preventDefault();
356
- } else if (e.key === "ArrowUp" && context.luckysheetCellUpdate.length > 0 && allowListNavigation) {
540
+ } else if (!(e.metaKey || e.ctrlKey) && e.key === "ArrowUp" && context.luckysheetCellUpdate.length > 0 && allowListNavigation && !(e.shiftKey && isInPlaceEditMode)) {
357
541
  if (document.getElementById("luckysheet-formula-search-c")) {
358
542
  var formulaSearchContainer = document.getElementById("luckysheet-formula-search-c");
359
543
  var activeItem = formulaSearchContainer === null || formulaSearchContainer === void 0 ? void 0 : formulaSearchContainer.querySelector(".luckysheet-formula-search-item-active");
@@ -372,7 +556,7 @@ var InputBox = function InputBox() {
372
556
  }
373
557
  }
374
558
  e.preventDefault();
375
- } else if (e.key === "ArrowDown" && context.luckysheetCellUpdate.length > 0 && allowListNavigation) {
559
+ } else if (!(e.metaKey || e.ctrlKey) && e.key === "ArrowDown" && context.luckysheetCellUpdate.length > 0 && allowListNavigation && !(e.shiftKey && isInPlaceEditMode)) {
376
560
  if (document.getElementById("luckysheet-formula-search-c")) {
377
561
  var formulaSearchContainer = document.getElementById("luckysheet-formula-search-c");
378
562
  var activeItem = formulaSearchContainer === null || formulaSearchContainer === void 0 ? void 0 : formulaSearchContainer.querySelector(".luckysheet-formula-search-item-active");
@@ -390,7 +574,7 @@ var InputBox = function InputBox() {
390
574
  }
391
575
  e.preventDefault();
392
576
  }
393
- }, [clearSearchItemActiveClass, context.luckysheetCellUpdate.length, selectActiveFormula, setContext, firstSelection]);
577
+ }, [capturePreFormulaState, clearSearchItemActiveClass, context.luckysheetCellUpdate.length, handleFormulaHistoryUndoRedo, selectActiveFormula, setContext, firstSelection, refs.cellInput]);
394
578
  var handleHideShowHint = function handleHideShowHint() {
395
579
  var _a, _b, _c, _d;
396
580
  var searchElFx = (_a = document.getElementsByClassName("fx-search")) === null || _a === void 0 ? void 0 : _a[0];
@@ -411,36 +595,62 @@ var InputBox = function InputBox() {
411
595
  }
412
596
  };
413
597
  var onChange = useCallback(function (__, isBlur) {
414
- var _a;
598
+ var _a, _b, _c, _d, _e, _f;
415
599
  if (context.isFlvReadOnly) return;
416
600
  handleHideShowHint();
417
- if (((_a = inputRef === null || inputRef === void 0 ? void 0 : inputRef.current) === null || _a === void 0 ? void 0 : _a.innerText.includes("=")) && /^=?[A-Za-z]*$/.test(getLastInputSpanText())) {
418
- setShowSearchHint(true);
419
- } else {
420
- setShowSearchHint(false);
421
- }
601
+ var editorText = (_c = (_b = (_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.innerText) === null || _b === void 0 ? void 0 : _b.trim()) !== null && _c !== void 0 ? _c : "";
602
+ setCellEditorIsFormula(editorText.startsWith("="));
603
+ setShowSearchHint(shouldShowFormulaFunctionList((_d = inputRef === null || inputRef === void 0 ? void 0 : inputRef.current) !== null && _d !== void 0 ? _d : null));
422
604
  if (!isComposingRef.current) {
423
605
  var currentCommaCount = countCommasBeforeCursor(inputRef === null || inputRef === void 0 ? void 0 : inputRef.current);
424
606
  setCommaCount(currentCommaCount);
425
607
  }
426
608
  var e = lastKeyDownEventRef.current;
427
- if (!e) return;
609
+ if (!e) {
610
+ var cellEl_1 = refs.cellInput.current;
611
+ if (cellEl_1 && (((_e = cellEl_1.innerText) === null || _e === void 0 ? void 0 : _e.trim().startsWith("=")) || ((_f = cellEl_1.textContent) === null || _f === void 0 ? void 0 : _f.trim().startsWith("=")))) {
612
+ setContext(function (draftCtx) {
613
+ if (!isAllowEdit(draftCtx, draftCtx.luckysheet_select_save)) return;
614
+ rangeHightlightselected(draftCtx, cellEl_1);
615
+ });
616
+ }
617
+ return;
618
+ }
428
619
  var kcode = e.keyCode;
429
620
  if (!kcode) return;
621
+ appendFormulaHistoryFromPrimaryEditor(function () {
622
+ return getCursorPosition(inputRef.current);
623
+ });
430
624
  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) {
431
625
  setContext(function (draftCtx) {
626
+ var _a, _b;
432
627
  if ((draftCtx.formulaCache.rangestart || draftCtx.formulaCache.rangedrag_column_start || draftCtx.formulaCache.rangedrag_row_start || israngeseleciton(draftCtx)) && isBlur) return;
433
628
  if (!isAllowEdit(draftCtx, draftCtx.luckysheet_select_save)) {
434
629
  return;
435
630
  }
436
- handleFormulaInput(draftCtx, refs.fxInput.current, refs.cellInput.current, kcode, preText.current);
631
+ handleFormulaInput(draftCtx, refs.fxInput.current, refs.cellInput.current, kcode, preTextRef.current);
632
+ var cellEl = refs.cellInput.current;
633
+ if (cellEl && (((_a = cellEl.innerText) === null || _a === void 0 ? void 0 : _a.trim().startsWith("=")) || ((_b = cellEl.textContent) === null || _b === void 0 ? void 0 : _b.trim().startsWith("=")))) {
634
+ rangeHightlightselected(draftCtx, cellEl);
635
+ }
437
636
  });
438
637
  }
439
- }, [refs.cellInput, refs.fxInput, setContext]);
638
+ }, [refs.cellInput, refs.fxInput, setContext, appendFormulaHistoryFromPrimaryEditor]);
440
639
  var onPaste = useCallback(function (e) {
640
+ var plainText = e.clipboardData.getData("text/plain");
641
+ e.preventDefault();
441
642
  if (_.isEmpty(context.luckysheetCellUpdate)) {
442
- e.preventDefault();
643
+ return;
443
644
  }
645
+ document.execCommand("insertText", false, plainText);
646
+ }, [context.luckysheetCellUpdate]);
647
+ var onCopy = useCallback(function (e) {
648
+ var _a;
649
+ if (_.isEmpty(context.luckysheetCellUpdate)) return;
650
+ e.preventDefault();
651
+ var sel = window.getSelection();
652
+ var text = sel && !sel.isCollapsed ? sel.toString() : e.currentTarget.innerText;
653
+ (_a = navigator.clipboard) === null || _a === void 0 ? void 0 : _a.writeText(text).catch(function () {});
444
654
  }, [context.luckysheetCellUpdate]);
445
655
  var cfg = context.config || {};
446
656
  var rowReadOnly = cfg.rowReadOnly || {};
@@ -480,7 +690,7 @@ var InputBox = function InputBox() {
480
690
  zIndex: _.isEmpty(context.luckysheetCellUpdate) ? -1 : 19,
481
691
  display: "block"
482
692
  };
483
- }, [firstSelection, (_d = context.rangeDialog) === null || _d === void 0 ? void 0 : _d.show, context.luckysheetCellUpdate, refs.cellArea, isInputBoxActive, frozenPosition, context.scrollLeft, context.scrollTop]);
693
+ }, [firstSelection, (_e = context.rangeDialog) === null || _e === void 0 ? void 0 : _e.show, context.luckysheetCellUpdate, refs.cellArea, isInputBoxActive, frozenPosition, context.scrollLeft, context.scrollTop]);
484
694
  useEffect(function () {
485
695
  var _a;
486
696
  if (firstSelection && !((_a = context.rangeDialog) === null || _a === void 0 ? void 0 : _a.show) && !isInputBoxActive && !_.isEmpty(context.luckysheetCellUpdate)) {
@@ -496,7 +706,7 @@ var InputBox = function InputBox() {
496
706
  setIsInputBoxActive(true);
497
707
  }
498
708
  }
499
- }, [firstSelection, (_e = context.rangeDialog) === null || _e === void 0 ? void 0 : _e.show, context.luckysheetCellUpdate, isInputBoxActive, context.scrollLeft, context.scrollTop, refs.cellArea]);
709
+ }, [firstSelection, (_f = context.rangeDialog) === null || _f === void 0 ? void 0 : _f.show, context.luckysheetCellUpdate, isInputBoxActive, context.scrollLeft, context.scrollTop, refs.cellArea]);
500
710
  var getAddressIndicatorPosition = useCallback(function () {
501
711
  var _a;
502
712
  if (!firstSelection || ((_a = context.rangeDialog) === null || _a === void 0 ? void 0 : _a.show)) {
@@ -509,7 +719,7 @@ var InputBox = function InputBox() {
509
719
  left: "0",
510
720
  display: "block"
511
721
  };
512
- }, [firstSelection, (_f = context.rangeDialog) === null || _f === void 0 ? void 0 : _f.show]);
722
+ }, [firstSelection, (_g = context.rangeDialog) === null || _g === void 0 ? void 0 : _g.show]);
513
723
  var getCellAddress = useCallback(function () {
514
724
  if (!firstSelection) return "";
515
725
  var rowIndex = firstSelection.row_focus || 0;
@@ -525,6 +735,29 @@ var InputBox = function InputBox() {
525
735
  setFirstSelectionActiveCell((_a = context.luckysheet_select_save) === null || _a === void 0 ? void 0 : _a[0]);
526
736
  }
527
737
  }, [isInputBoxActive]);
738
+ useLayoutEffect(function () {
739
+ var _a;
740
+ var editing = context.luckysheetCellUpdate.length > 0;
741
+ if (!editing) {
742
+ scrollAtEditSessionStartRef.current = null;
743
+ setShowAddressIndicator(false);
744
+ return;
745
+ }
746
+ var prevLen = (_a = prevCellUpdate === null || prevCellUpdate === void 0 ? void 0 : prevCellUpdate.length) !== null && _a !== void 0 ? _a : 0;
747
+ var startedThisCommit = prevLen === 0;
748
+ if (startedThisCommit || scrollAtEditSessionStartRef.current == null) {
749
+ scrollAtEditSessionStartRef.current = {
750
+ left: context.scrollLeft,
751
+ top: context.scrollTop
752
+ };
753
+ setShowAddressIndicator(false);
754
+ return;
755
+ }
756
+ var b = scrollAtEditSessionStartRef.current;
757
+ if (context.scrollLeft !== b.left || context.scrollTop !== b.top) {
758
+ setShowAddressIndicator(true);
759
+ }
760
+ }, [context.luckysheetCellUpdate, context.scrollLeft, context.scrollTop, prevCellUpdate]);
528
761
  useEffect(function () {
529
762
  var handleKeyDown = function handleKeyDown(event) {
530
763
  if (event.key === "F10") {
@@ -559,15 +792,15 @@ var InputBox = function InputBox() {
559
792
  };
560
793
  });
561
794
  setLinkSelectionHighlightRects(relative);
562
- }, [(_g = context.linkCard) === null || _g === void 0 ? void 0 : _g.applyToSelection, (_h = context.linkCard) === null || _h === void 0 ? void 0 : _h.selectionOffsets, (_j = context.linkCard) === null || _j === void 0 ? void 0 : _j.r, (_k = context.linkCard) === null || _k === void 0 ? void 0 : _k.c, context.luckysheetCellUpdate]);
795
+ }, [(_h = context.linkCard) === null || _h === void 0 ? void 0 : _h.applyToSelection, (_j = context.linkCard) === null || _j === void 0 ? void 0 : _j.selectionOffsets, (_k = context.linkCard) === null || _k === void 0 ? void 0 : _k.r, (_l = context.linkCard) === null || _l === void 0 ? void 0 : _l.c, context.luckysheetCellUpdate]);
563
796
  var wraperGetCell = function wraperGetCell() {
564
797
  var cell = getCellAddress();
565
- placeRef.current = cell;
566
798
  if (activeRefCell !== cell) {
567
799
  setActiveRefCell(cell);
568
800
  }
569
801
  return activeCell || cell;
570
802
  };
803
+ useRerenderOnFormulaCaret(inputRef, context.luckysheetCellUpdate.length > 0);
571
804
  var getFunctionNameFromInput = useCallback(function () {
572
805
  var _a, _b;
573
806
  var inputText = ((_a = inputRef === null || inputRef === void 0 ? void 0 : inputRef.current) === null || _a === void 0 ? void 0 : _a.innerText) || "";
@@ -584,8 +817,10 @@ var InputBox = function InputBox() {
584
817
  }
585
818
  return null;
586
819
  }, []);
587
- var functionName = context.functionHint || getFunctionNameFromInput();
820
+ var functionName = (_o = (_m = getFunctionNameFromFormulaCaretSpans(inputRef.current)) !== null && _m !== void 0 ? _m : context.functionHint) !== null && _o !== void 0 ? _o : getFunctionNameFromInput();
588
821
  var fn = functionName ? context.formulaCache.functionlistMap[functionName] : null;
822
+ var showCellFormulaChrome = context.luckysheetCellUpdate.length > 0 && getFormulaEditorOwner(context) === "cell";
823
+ var inputBoxBaseSelection = isInputBoxActive && firstSelectionActiveCell ? firstSelectionActiveCell : firstSelection;
589
824
  return /*#__PURE__*/React.createElement("div", {
590
825
  className: "luckysheet-input-box",
591
826
  id: "luckysheet-input-box",
@@ -596,16 +831,16 @@ var InputBox = function InputBox() {
596
831
  onMouseUp: function onMouseUp(e) {
597
832
  return e.stopPropagation();
598
833
  }
599
- }, firstSelection && !((_l = context.rangeDialog) === null || _l === void 0 ? void 0 : _l.show) && (/*#__PURE__*/React.createElement("div", {
834
+ }, firstSelection && !((_p = context.rangeDialog) === null || _p === void 0 ? void 0 : _p.show) && showAddressIndicator && (/*#__PURE__*/React.createElement("div", {
600
835
  className: "luckysheet-cell-address-indicator",
601
836
  style: getAddressIndicatorPosition()
602
837
  }, wraperGetCell())), /*#__PURE__*/React.createElement("div", {
603
838
  ref: inputBoxInnerRef,
604
839
  className: "luckysheet-input-box-inner",
605
- style: firstSelection ? __assign({
840
+ style: inputBoxBaseSelection ? __assign({
606
841
  position: "relative",
607
- minWidth: firstSelection.width,
608
- minHeight: firstSelection.height
842
+ minWidth: inputBoxBaseSelection.width,
843
+ minHeight: inputBoxBaseSelection.height
609
844
  }, inputBoxStyle) : {
610
845
  position: "relative"
611
846
  }
@@ -626,10 +861,23 @@ var InputBox = function InputBox() {
626
861
  },
627
862
  onMouseUp: function onMouseUp() {
628
863
  handleHideShowHint();
864
+ setContext(function (draftCtx) {
865
+ setFormulaEditorOwner(draftCtx, "cell");
866
+ });
629
867
  if (!isComposingRef.current) {
630
868
  var currentCommaCount = countCommasBeforeCursor(inputRef === null || inputRef === void 0 ? void 0 : inputRef.current);
631
869
  setCommaCount(currentCommaCount);
632
870
  }
871
+ var editor = inputRef.current;
872
+ if (!editor) return;
873
+ setContext(function (draftCtx) {
874
+ if (draftCtx.formulaCache.rangeSelectionActive !== true) return;
875
+ var clickedInsideManagedRange = getFormulaRangeIndexAtCaret(editor) !== null;
876
+ var atValidInsertionPoint = isCaretAtValidFormulaRangeInsertionPoint(editor);
877
+ if (clickedInsideManagedRange || !atValidInsertionPoint) {
878
+ markRangeSelectionDirty(draftCtx);
879
+ }
880
+ });
633
881
  },
634
882
  innerRef: function innerRef(e) {
635
883
  inputRef.current = e;
@@ -648,6 +896,7 @@ var InputBox = function InputBox() {
648
896
  onChange: onChange,
649
897
  onKeyDown: onKeyDown,
650
898
  onPaste: onPaste,
899
+ onCopy: onCopy,
651
900
  allowEdit: edit ? !isHidenRC : edit
652
901
  }), linkSelectionHighlightRects.length > 0 && (/*#__PURE__*/React.createElement("div", {
653
902
  className: "luckysheet-input-box-link-selection-highlight",
@@ -672,7 +921,7 @@ var InputBox = function InputBox() {
672
921
  backgroundColor: "rgba(0, 123, 255, 0.25)"
673
922
  }
674
923
  });
675
- })))), (context.functionCandidates.length > 0 || context.functionHint || context.defaultCandidates.length > 0 || fn) && (/*#__PURE__*/React.createElement(React.Fragment, null, showSearchHint && (/*#__PURE__*/React.createElement(FormulaSearch, {
924
+ })))), (context.functionCandidates.length > 0 || context.functionHint || context.defaultCandidates.length > 0 || fn) && showCellFormulaChrome && (/*#__PURE__*/React.createElement(React.Fragment, null, showSearchHint && (/*#__PURE__*/React.createElement(FormulaSearch, {
676
925
  onMouseMove: function onMouseMove(e) {
677
926
  if (document.getElementById("luckysheet-formula-search-c")) {
678
927
  var hoveredItem = e.target.closest(".luckysheet-formula-search-item");
@@ -687,12 +936,12 @@ var InputBox = function InputBox() {
687
936
  }
688
937
  })), /*#__PURE__*/React.createElement("div", {
689
938
  className: "cell-hint"
690
- }, showFormulaHint && fn && (/*#__PURE__*/React.createElement(FormulaHint, {
939
+ }, showFormulaHint && fn && !showSearchHint && (/*#__PURE__*/React.createElement(FormulaHint, {
691
940
  handleShowFormulaHint: handleShowFormulaHint,
692
941
  showFormulaHint: showFormulaHint,
693
942
  commaCount: commaCount,
694
943
  functionName: functionName
695
- })), !showFormulaHint && fn && (/*#__PURE__*/React.createElement(Tooltip, {
944
+ })), !showFormulaHint && fn && !showSearchHint && (/*#__PURE__*/React.createElement(Tooltip, {
696
945
  text: "Turn on formula suggestions (F10)",
697
946
  placement: "top",
698
947
  defaultOpen: true,