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

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 +421 -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 +419 -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,22 +321,173 @@ 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);
470
+ appendFormulaHistoryFromPrimaryEditor(function () {
471
+ return getCursorPosition(inputRef.current);
472
+ });
280
473
  stopPropagation(e);
281
474
  } else if (e.code === "KeyI") {
282
475
  handleItalic(context, inputRef.current);
476
+ appendFormulaHistoryFromPrimaryEditor(function () {
477
+ return getCursorPosition(inputRef.current);
478
+ });
283
479
  stopPropagation(e);
284
480
  } else if (e.code === "KeyU") {
285
481
  handleUnderline(context, inputRef.current);
482
+ appendFormulaHistoryFromPrimaryEditor(function () {
483
+ return getCursorPosition(inputRef.current);
484
+ });
286
485
  stopPropagation(e);
287
486
  } else if (e.code === "KeyS") {
288
487
  handleStrikeThrough(context, inputRef.current);
488
+ appendFormulaHistoryFromPrimaryEditor(function () {
489
+ return getCursorPosition(inputRef.current);
490
+ });
289
491
  stopPropagation(e);
290
492
  }
291
493
  }
@@ -294,7 +496,32 @@ var InputBox = function InputBox() {
294
496
  setCommaCount(currentCommaCount);
295
497
  }
296
498
  var allowListNavigation = true;
499
+ var isArrowKey = e.key === "ArrowUp" || e.key === "ArrowDown" || e.key === "ArrowLeft" || e.key === "ArrowRight";
500
+ var isInPlaceEditMode = ((_c = refs.globalCache) === null || _c === void 0 ? void 0 : _c.enteredEditByTyping) !== true;
297
501
  if (e.key === "Delete" || e.key === "Backspace") {
502
+ var anchor = formulaAnchorCellRef.current;
503
+ if (anchor != null) {
504
+ var anchorRow_2 = anchor[0],
505
+ anchorCol_2 = anchor[1];
506
+ skipNextAnchorSelectionSyncRef.current = true;
507
+ setTimeout(function () {
508
+ setContext(function (draftCtx) {
509
+ draftCtx.luckysheetCellUpdate = [anchorRow_2, anchorCol_2];
510
+ draftCtx.luckysheet_select_save = [{
511
+ row: [anchorRow_2, anchorRow_2],
512
+ column: [anchorCol_2, anchorCol_2],
513
+ row_focus: anchorRow_2,
514
+ column_focus: anchorCol_2
515
+ }];
516
+ markRangeSelectionDirty(draftCtx);
517
+ var el = refs.cellInput.current;
518
+ if (el && el.innerText.trim().startsWith("=")) {
519
+ createRangeHightlight(draftCtx, el.innerHTML);
520
+ rangeHightlightselected(draftCtx, el);
521
+ }
522
+ });
523
+ }, 0);
524
+ }
298
525
  if (isComposingRef.current) requestAnimationFrame(ensureNotEmpty);
299
526
  if (getCursorPosition(inputRef === null || inputRef === void 0 ? void 0 : inputRef.current) === (inputRef === null || inputRef === void 0 ? void 0 : inputRef.current.innerText.length)) {
300
527
  setTimeout(function () {
@@ -302,39 +529,8 @@ var InputBox = function InputBox() {
302
529
  }, 5);
303
530
  }
304
531
  }
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
- }
532
+ if (isArrowKey && isFormulaReferenceInputMode(context)) {
533
+ allowListNavigation = false;
338
534
  }
339
535
  if (e.key === "Escape" && context.luckysheetCellUpdate.length > 0) {
340
536
  setContext(function (draftCtx) {
@@ -353,7 +549,7 @@ var InputBox = function InputBox() {
353
549
  e.preventDefault();
354
550
  } else if (e.key === "F4" && context.luckysheetCellUpdate.length > 0) {
355
551
  e.preventDefault();
356
- } else if (e.key === "ArrowUp" && context.luckysheetCellUpdate.length > 0 && allowListNavigation) {
552
+ } else if (!(e.metaKey || e.ctrlKey) && e.key === "ArrowUp" && context.luckysheetCellUpdate.length > 0 && allowListNavigation && !(e.shiftKey && isInPlaceEditMode)) {
357
553
  if (document.getElementById("luckysheet-formula-search-c")) {
358
554
  var formulaSearchContainer = document.getElementById("luckysheet-formula-search-c");
359
555
  var activeItem = formulaSearchContainer === null || formulaSearchContainer === void 0 ? void 0 : formulaSearchContainer.querySelector(".luckysheet-formula-search-item-active");
@@ -372,7 +568,7 @@ var InputBox = function InputBox() {
372
568
  }
373
569
  }
374
570
  e.preventDefault();
375
- } else if (e.key === "ArrowDown" && context.luckysheetCellUpdate.length > 0 && allowListNavigation) {
571
+ } else if (!(e.metaKey || e.ctrlKey) && e.key === "ArrowDown" && context.luckysheetCellUpdate.length > 0 && allowListNavigation && !(e.shiftKey && isInPlaceEditMode)) {
376
572
  if (document.getElementById("luckysheet-formula-search-c")) {
377
573
  var formulaSearchContainer = document.getElementById("luckysheet-formula-search-c");
378
574
  var activeItem = formulaSearchContainer === null || formulaSearchContainer === void 0 ? void 0 : formulaSearchContainer.querySelector(".luckysheet-formula-search-item-active");
@@ -390,7 +586,7 @@ var InputBox = function InputBox() {
390
586
  }
391
587
  e.preventDefault();
392
588
  }
393
- }, [clearSearchItemActiveClass, context.luckysheetCellUpdate.length, selectActiveFormula, setContext, firstSelection]);
589
+ }, [capturePreFormulaState, clearSearchItemActiveClass, context.luckysheetCellUpdate.length, handleFormulaHistoryUndoRedo, selectActiveFormula, setContext, firstSelection, refs.cellInput]);
394
590
  var handleHideShowHint = function handleHideShowHint() {
395
591
  var _a, _b, _c, _d;
396
592
  var searchElFx = (_a = document.getElementsByClassName("fx-search")) === null || _a === void 0 ? void 0 : _a[0];
@@ -411,36 +607,62 @@ var InputBox = function InputBox() {
411
607
  }
412
608
  };
413
609
  var onChange = useCallback(function (__, isBlur) {
414
- var _a;
610
+ var _a, _b, _c, _d, _e, _f;
415
611
  if (context.isFlvReadOnly) return;
416
612
  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
- }
613
+ 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 : "";
614
+ setCellEditorIsFormula(editorText.startsWith("="));
615
+ setShowSearchHint(shouldShowFormulaFunctionList((_d = inputRef === null || inputRef === void 0 ? void 0 : inputRef.current) !== null && _d !== void 0 ? _d : null));
422
616
  if (!isComposingRef.current) {
423
617
  var currentCommaCount = countCommasBeforeCursor(inputRef === null || inputRef === void 0 ? void 0 : inputRef.current);
424
618
  setCommaCount(currentCommaCount);
425
619
  }
426
620
  var e = lastKeyDownEventRef.current;
427
- if (!e) return;
621
+ if (!e) {
622
+ var cellEl_1 = refs.cellInput.current;
623
+ 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("=")))) {
624
+ setContext(function (draftCtx) {
625
+ if (!isAllowEdit(draftCtx, draftCtx.luckysheet_select_save)) return;
626
+ rangeHightlightselected(draftCtx, cellEl_1);
627
+ });
628
+ }
629
+ return;
630
+ }
428
631
  var kcode = e.keyCode;
429
632
  if (!kcode) return;
633
+ appendFormulaHistoryFromPrimaryEditor(function () {
634
+ return getCursorPosition(inputRef.current);
635
+ });
430
636
  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
637
  setContext(function (draftCtx) {
638
+ var _a, _b;
432
639
  if ((draftCtx.formulaCache.rangestart || draftCtx.formulaCache.rangedrag_column_start || draftCtx.formulaCache.rangedrag_row_start || israngeseleciton(draftCtx)) && isBlur) return;
433
640
  if (!isAllowEdit(draftCtx, draftCtx.luckysheet_select_save)) {
434
641
  return;
435
642
  }
436
- handleFormulaInput(draftCtx, refs.fxInput.current, refs.cellInput.current, kcode, preText.current);
643
+ handleFormulaInput(draftCtx, refs.fxInput.current, refs.cellInput.current, kcode, preTextRef.current);
644
+ var cellEl = refs.cellInput.current;
645
+ 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("=")))) {
646
+ rangeHightlightselected(draftCtx, cellEl);
647
+ }
437
648
  });
438
649
  }
439
- }, [refs.cellInput, refs.fxInput, setContext]);
650
+ }, [refs.cellInput, refs.fxInput, setContext, appendFormulaHistoryFromPrimaryEditor]);
440
651
  var onPaste = useCallback(function (e) {
652
+ var plainText = e.clipboardData.getData("text/plain");
653
+ e.preventDefault();
441
654
  if (_.isEmpty(context.luckysheetCellUpdate)) {
442
- e.preventDefault();
655
+ return;
443
656
  }
657
+ document.execCommand("insertText", false, plainText);
658
+ }, [context.luckysheetCellUpdate]);
659
+ var onCopy = useCallback(function (e) {
660
+ var _a;
661
+ if (_.isEmpty(context.luckysheetCellUpdate)) return;
662
+ e.preventDefault();
663
+ var sel = window.getSelection();
664
+ var text = sel && !sel.isCollapsed ? sel.toString() : e.currentTarget.innerText;
665
+ (_a = navigator.clipboard) === null || _a === void 0 ? void 0 : _a.writeText(text).catch(function () {});
444
666
  }, [context.luckysheetCellUpdate]);
445
667
  var cfg = context.config || {};
446
668
  var rowReadOnly = cfg.rowReadOnly || {};
@@ -480,7 +702,7 @@ var InputBox = function InputBox() {
480
702
  zIndex: _.isEmpty(context.luckysheetCellUpdate) ? -1 : 19,
481
703
  display: "block"
482
704
  };
483
- }, [firstSelection, (_d = context.rangeDialog) === null || _d === void 0 ? void 0 : _d.show, context.luckysheetCellUpdate, refs.cellArea, isInputBoxActive, frozenPosition, context.scrollLeft, context.scrollTop]);
705
+ }, [firstSelection, (_e = context.rangeDialog) === null || _e === void 0 ? void 0 : _e.show, context.luckysheetCellUpdate, refs.cellArea, isInputBoxActive, frozenPosition, context.scrollLeft, context.scrollTop]);
484
706
  useEffect(function () {
485
707
  var _a;
486
708
  if (firstSelection && !((_a = context.rangeDialog) === null || _a === void 0 ? void 0 : _a.show) && !isInputBoxActive && !_.isEmpty(context.luckysheetCellUpdate)) {
@@ -496,7 +718,7 @@ var InputBox = function InputBox() {
496
718
  setIsInputBoxActive(true);
497
719
  }
498
720
  }
499
- }, [firstSelection, (_e = context.rangeDialog) === null || _e === void 0 ? void 0 : _e.show, context.luckysheetCellUpdate, isInputBoxActive, context.scrollLeft, context.scrollTop, refs.cellArea]);
721
+ }, [firstSelection, (_f = context.rangeDialog) === null || _f === void 0 ? void 0 : _f.show, context.luckysheetCellUpdate, isInputBoxActive, context.scrollLeft, context.scrollTop, refs.cellArea]);
500
722
  var getAddressIndicatorPosition = useCallback(function () {
501
723
  var _a;
502
724
  if (!firstSelection || ((_a = context.rangeDialog) === null || _a === void 0 ? void 0 : _a.show)) {
@@ -509,7 +731,7 @@ var InputBox = function InputBox() {
509
731
  left: "0",
510
732
  display: "block"
511
733
  };
512
- }, [firstSelection, (_f = context.rangeDialog) === null || _f === void 0 ? void 0 : _f.show]);
734
+ }, [firstSelection, (_g = context.rangeDialog) === null || _g === void 0 ? void 0 : _g.show]);
513
735
  var getCellAddress = useCallback(function () {
514
736
  if (!firstSelection) return "";
515
737
  var rowIndex = firstSelection.row_focus || 0;
@@ -525,6 +747,29 @@ var InputBox = function InputBox() {
525
747
  setFirstSelectionActiveCell((_a = context.luckysheet_select_save) === null || _a === void 0 ? void 0 : _a[0]);
526
748
  }
527
749
  }, [isInputBoxActive]);
750
+ useLayoutEffect(function () {
751
+ var _a;
752
+ var editing = context.luckysheetCellUpdate.length > 0;
753
+ if (!editing) {
754
+ scrollAtEditSessionStartRef.current = null;
755
+ setShowAddressIndicator(false);
756
+ return;
757
+ }
758
+ var prevLen = (_a = prevCellUpdate === null || prevCellUpdate === void 0 ? void 0 : prevCellUpdate.length) !== null && _a !== void 0 ? _a : 0;
759
+ var startedThisCommit = prevLen === 0;
760
+ if (startedThisCommit || scrollAtEditSessionStartRef.current == null) {
761
+ scrollAtEditSessionStartRef.current = {
762
+ left: context.scrollLeft,
763
+ top: context.scrollTop
764
+ };
765
+ setShowAddressIndicator(false);
766
+ return;
767
+ }
768
+ var b = scrollAtEditSessionStartRef.current;
769
+ if (context.scrollLeft !== b.left || context.scrollTop !== b.top) {
770
+ setShowAddressIndicator(true);
771
+ }
772
+ }, [context.luckysheetCellUpdate, context.scrollLeft, context.scrollTop, prevCellUpdate]);
528
773
  useEffect(function () {
529
774
  var handleKeyDown = function handleKeyDown(event) {
530
775
  if (event.key === "F10") {
@@ -559,15 +804,15 @@ var InputBox = function InputBox() {
559
804
  };
560
805
  });
561
806
  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]);
807
+ }, [(_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
808
  var wraperGetCell = function wraperGetCell() {
564
809
  var cell = getCellAddress();
565
- placeRef.current = cell;
566
810
  if (activeRefCell !== cell) {
567
811
  setActiveRefCell(cell);
568
812
  }
569
813
  return activeCell || cell;
570
814
  };
815
+ useRerenderOnFormulaCaret(inputRef, context.luckysheetCellUpdate.length > 0);
571
816
  var getFunctionNameFromInput = useCallback(function () {
572
817
  var _a, _b;
573
818
  var inputText = ((_a = inputRef === null || inputRef === void 0 ? void 0 : inputRef.current) === null || _a === void 0 ? void 0 : _a.innerText) || "";
@@ -584,8 +829,10 @@ var InputBox = function InputBox() {
584
829
  }
585
830
  return null;
586
831
  }, []);
587
- var functionName = context.functionHint || getFunctionNameFromInput();
832
+ var functionName = (_o = (_m = getFunctionNameFromFormulaCaretSpans(inputRef.current)) !== null && _m !== void 0 ? _m : context.functionHint) !== null && _o !== void 0 ? _o : getFunctionNameFromInput();
588
833
  var fn = functionName ? context.formulaCache.functionlistMap[functionName] : null;
834
+ var showCellFormulaChrome = context.luckysheetCellUpdate.length > 0 && getFormulaEditorOwner(context) === "cell";
835
+ var inputBoxBaseSelection = isInputBoxActive && firstSelectionActiveCell ? firstSelectionActiveCell : firstSelection;
589
836
  return /*#__PURE__*/React.createElement("div", {
590
837
  className: "luckysheet-input-box",
591
838
  id: "luckysheet-input-box",
@@ -596,16 +843,16 @@ var InputBox = function InputBox() {
596
843
  onMouseUp: function onMouseUp(e) {
597
844
  return e.stopPropagation();
598
845
  }
599
- }, firstSelection && !((_l = context.rangeDialog) === null || _l === void 0 ? void 0 : _l.show) && (/*#__PURE__*/React.createElement("div", {
846
+ }, firstSelection && !((_p = context.rangeDialog) === null || _p === void 0 ? void 0 : _p.show) && showAddressIndicator && (/*#__PURE__*/React.createElement("div", {
600
847
  className: "luckysheet-cell-address-indicator",
601
848
  style: getAddressIndicatorPosition()
602
849
  }, wraperGetCell())), /*#__PURE__*/React.createElement("div", {
603
850
  ref: inputBoxInnerRef,
604
851
  className: "luckysheet-input-box-inner",
605
- style: firstSelection ? __assign({
852
+ style: inputBoxBaseSelection ? __assign({
606
853
  position: "relative",
607
- minWidth: firstSelection.width,
608
- minHeight: firstSelection.height
854
+ minWidth: inputBoxBaseSelection.width,
855
+ minHeight: inputBoxBaseSelection.height
609
856
  }, inputBoxStyle) : {
610
857
  position: "relative"
611
858
  }
@@ -626,10 +873,23 @@ var InputBox = function InputBox() {
626
873
  },
627
874
  onMouseUp: function onMouseUp() {
628
875
  handleHideShowHint();
876
+ setContext(function (draftCtx) {
877
+ setFormulaEditorOwner(draftCtx, "cell");
878
+ });
629
879
  if (!isComposingRef.current) {
630
880
  var currentCommaCount = countCommasBeforeCursor(inputRef === null || inputRef === void 0 ? void 0 : inputRef.current);
631
881
  setCommaCount(currentCommaCount);
632
882
  }
883
+ var editor = inputRef.current;
884
+ if (!editor) return;
885
+ setContext(function (draftCtx) {
886
+ if (draftCtx.formulaCache.rangeSelectionActive !== true) return;
887
+ var clickedInsideManagedRange = getFormulaRangeIndexAtCaret(editor) !== null;
888
+ var atValidInsertionPoint = isCaretAtValidFormulaRangeInsertionPoint(editor);
889
+ if (clickedInsideManagedRange || !atValidInsertionPoint) {
890
+ markRangeSelectionDirty(draftCtx);
891
+ }
892
+ });
633
893
  },
634
894
  innerRef: function innerRef(e) {
635
895
  inputRef.current = e;
@@ -648,6 +908,7 @@ var InputBox = function InputBox() {
648
908
  onChange: onChange,
649
909
  onKeyDown: onKeyDown,
650
910
  onPaste: onPaste,
911
+ onCopy: onCopy,
651
912
  allowEdit: edit ? !isHidenRC : edit
652
913
  }), linkSelectionHighlightRects.length > 0 && (/*#__PURE__*/React.createElement("div", {
653
914
  className: "luckysheet-input-box-link-selection-highlight",
@@ -672,7 +933,7 @@ var InputBox = function InputBox() {
672
933
  backgroundColor: "rgba(0, 123, 255, 0.25)"
673
934
  }
674
935
  });
675
- })))), (context.functionCandidates.length > 0 || context.functionHint || context.defaultCandidates.length > 0 || fn) && (/*#__PURE__*/React.createElement(React.Fragment, null, showSearchHint && (/*#__PURE__*/React.createElement(FormulaSearch, {
936
+ })))), (context.functionCandidates.length > 0 || context.functionHint || context.defaultCandidates.length > 0 || fn) && showCellFormulaChrome && (/*#__PURE__*/React.createElement(React.Fragment, null, showSearchHint && (/*#__PURE__*/React.createElement(FormulaSearch, {
676
937
  onMouseMove: function onMouseMove(e) {
677
938
  if (document.getElementById("luckysheet-formula-search-c")) {
678
939
  var hoveredItem = e.target.closest(".luckysheet-formula-search-item");
@@ -687,12 +948,12 @@ var InputBox = function InputBox() {
687
948
  }
688
949
  })), /*#__PURE__*/React.createElement("div", {
689
950
  className: "cell-hint"
690
- }, showFormulaHint && fn && (/*#__PURE__*/React.createElement(FormulaHint, {
951
+ }, showFormulaHint && fn && !showSearchHint && (/*#__PURE__*/React.createElement(FormulaHint, {
691
952
  handleShowFormulaHint: handleShowFormulaHint,
692
953
  showFormulaHint: showFormulaHint,
693
954
  commaCount: commaCount,
694
955
  functionName: functionName
695
- })), !showFormulaHint && fn && (/*#__PURE__*/React.createElement(Tooltip, {
956
+ })), !showFormulaHint && fn && !showSearchHint && (/*#__PURE__*/React.createElement(Tooltip, {
696
957
  text: "Turn on formula suggestions (F10)",
697
958
  placement: "top",
698
959
  defaultOpen: true,