@fileverse-dev/fortune-react 1.3.12-mixed-a → 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 (39) hide show
  1. package/es/components/ContextMenu/index.js +1 -1
  2. package/es/components/FxEditor/index.js +316 -60
  3. package/es/components/SheetOverlay/FormulaHint/index.js +46 -25
  4. package/es/components/SheetOverlay/FormulaSearch/index.d.ts +3 -1
  5. package/es/components/SheetOverlay/FormulaSearch/index.js +58 -29
  6. package/es/components/SheetOverlay/InputBox.js +406 -194
  7. package/es/components/SheetOverlay/drag_and_drop/column-helpers.js +34 -48
  8. package/es/components/SheetOverlay/drag_and_drop/row-helpers.js +31 -41
  9. package/es/components/SheetOverlay/formula-segment-boundary.d.ts +1 -0
  10. package/es/components/SheetOverlay/formula-segment-boundary.js +4 -0
  11. package/es/components/SheetOverlay/helper.d.ts +7 -0
  12. package/es/components/SheetOverlay/helper.js +95 -0
  13. package/es/components/SheetOverlay/index.css +6 -45
  14. package/es/components/SheetOverlay/index.js +26 -14
  15. package/es/components/Workbook/index.js +5 -8
  16. package/es/hooks/useFormulaEditorHistory.d.ts +24 -0
  17. package/es/hooks/useFormulaEditorHistory.js +119 -0
  18. package/es/hooks/useRerenderOnFormulaCaret.d.ts +2 -0
  19. package/es/hooks/useRerenderOnFormulaCaret.js +28 -0
  20. package/lib/components/ContextMenu/index.js +1 -1
  21. package/lib/components/FxEditor/index.js +314 -58
  22. package/lib/components/SheetOverlay/FormulaHint/index.js +45 -24
  23. package/lib/components/SheetOverlay/FormulaSearch/index.d.ts +3 -1
  24. package/lib/components/SheetOverlay/FormulaSearch/index.js +57 -28
  25. package/lib/components/SheetOverlay/InputBox.js +404 -192
  26. package/lib/components/SheetOverlay/drag_and_drop/column-helpers.js +33 -47
  27. package/lib/components/SheetOverlay/drag_and_drop/row-helpers.js +31 -41
  28. package/lib/components/SheetOverlay/formula-segment-boundary.d.ts +1 -0
  29. package/lib/components/SheetOverlay/formula-segment-boundary.js +10 -0
  30. package/lib/components/SheetOverlay/helper.d.ts +7 -0
  31. package/lib/components/SheetOverlay/helper.js +99 -0
  32. package/lib/components/SheetOverlay/index.css +6 -45
  33. package/lib/components/SheetOverlay/index.js +25 -13
  34. package/lib/components/Workbook/index.js +5 -8
  35. package/lib/hooks/useFormulaEditorHistory.d.ts +24 -0
  36. package/lib/hooks/useFormulaEditorHistory.js +126 -0
  37. package/lib/hooks/useRerenderOnFormulaCaret.d.ts +2 -0
  38. package/lib/hooks/useRerenderOnFormulaCaret.js +34 -0
  39. 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, 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, _m, _o;
24
- var _p = useContext(WorkbookContext),
25
- context = _p.context,
26
- setContext = _p.setContext,
27
- refs = _p.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 _q = useState(false),
33
- isHidenRC = _q[0],
34
- setIsHidenRC = _q[1];
35
35
  var _r = useState(false),
36
- isInputBoxActive = _r[0],
37
- setIsInputBoxActive = _r[1];
38
- var _s = useState(""),
39
- activeCell = _s[0],
40
- setActiveCell = _s[1];
36
+ isHidenRC = _r[0],
37
+ setIsHidenRC = _r[1];
38
+ var _s = useState(false),
39
+ isInputBoxActive = _s[0],
40
+ setIsInputBoxActive = _s[1];
41
41
  var _t = useState(""),
42
- activeRefCell = _t[0],
43
- setActiveRefCell = _t[1];
44
- var _u = 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 = _u[0],
49
- setFrozenPosition = _u[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 _v = useState({}),
52
- firstSelectionActiveCell = _v[0],
53
- setFirstSelectionActiveCell = _v[1];
54
- var _w = useState(0),
55
- commaCount = _w[0],
56
- setCommaCount = _w[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 _x = useState(!hideFormulaHintLocal),
59
- showFormulaHint = _x[0],
60
- setShowFormulaHint = _x[1];
61
- var _y = useState(false),
62
- showSearchHint = _y[0],
63
- setShowSearchHint = _y[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 _z = useState([]),
72
- linkSelectionHighlightRects = _z[0],
73
- setLinkSelectionHighlightRects = _z[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, (_c = inputRef.current) === null || _c === void 0 ? void 0 : _c.innerText]);
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,12 +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
- var wasOverwrite = refs.globalCache.overwriteCell;
133
- var overwriteFirstChar = refs.globalCache.overwriteCellFirstChar;
134
- if (cell && !refs.globalCache.overwriteCell) {
162
+ if (cell && !overwrite) {
135
163
  if (isInlineStringCell(cell)) {
136
164
  value_1 = getInlineStringHTML(row_index, col_index, flowdata);
137
165
  } else if (cell.f) {
@@ -147,20 +175,20 @@ var InputBox = function InputBox() {
147
175
  }
148
176
  }
149
177
  refs.globalCache.overwriteCell = false;
150
- delete refs.globalCache.overwriteCellFirstChar;
151
- if (wasOverwrite && inputRef.current) {
152
- inputRef.current.innerText = overwriteFirstChar !== null && overwriteFirstChar !== void 0 ? overwriteFirstChar : "";
153
- if (overwriteFirstChar) {
154
- moveToEnd(inputRef.current);
155
- }
156
- } else if (!refs.globalCache.ignoreWriteCell && inputRef.current && value_1) {
178
+ var wroteEditorFromStoredCell = false;
179
+ if (!refs.globalCache.ignoreWriteCell && inputRef.current && value_1) {
157
180
  inputRef.current.innerHTML = escapeHTMLTag(escapeScriptTag(value_1));
158
- } else if (!refs.globalCache.ignoreWriteCell && inputRef.current && !value_1 && !wasOverwrite) {
181
+ wroteEditorFromStoredCell = true;
182
+ } else if (!refs.globalCache.ignoreWriteCell && inputRef.current && !value_1 && !overwrite) {
159
183
  var valueD = getCellValue(row_index, col_index, flowdata, "f");
160
184
  inputRef.current.innerText = valueD;
185
+ wroteEditorFromStoredCell = true;
161
186
  }
162
187
  refs.globalCache.ignoreWriteCell = false;
163
- if (!refs.globalCache.doNotFocus) {
188
+ if (inputRef.current) {
189
+ setCellEditorIsFormula(inputRef.current.innerText.trim().startsWith("="));
190
+ }
191
+ if (wroteEditorFromStoredCell && !refs.globalCache.doNotFocus) {
164
192
  setTimeout(function () {
165
193
  moveToEnd(inputRef.current);
166
194
  });
@@ -173,8 +201,43 @@ var InputBox = function InputBox() {
173
201
  if (inputRef.current) {
174
202
  inputRef.current.innerHTML = "";
175
203
  }
204
+ delete refs.globalCache.pendingTypeOverCell;
205
+ setCellEditorIsFormula(false);
206
+ resetFormulaHistory();
176
207
  }
177
- }, [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]);
178
241
  useEffect(function () {
179
242
  setIsHidenRC(isShowHidenCR(context));
180
243
  }, [context.luckysheet_select_save]);
@@ -183,66 +246,46 @@ var InputBox = function InputBox() {
183
246
  if (!firstSelection || ((_a = context.rangeDialog) === null || _a === void 0 ? void 0 : _a.show) || _.isEmpty(context.luckysheetCellUpdate)) {
184
247
  setIsInputBoxActive(false);
185
248
  }
186
- }, [firstSelection, (_d = context.rangeDialog) === null || _d === void 0 ? void 0 : _d.show, context.luckysheetCellUpdate]);
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]);
187
267
  var getActiveFormula = useCallback(function () {
188
268
  return document.querySelector(".luckysheet-formula-search-item-active");
189
269
  }, []);
190
270
  var insertSelectedFormula = useCallback(function (formulaName) {
191
- var _a;
192
- if (/^=[a-zA-Z]+$/.test(inputRef.current.innerText)) {
193
- 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>");
194
- inputRef.current.innerHTML = ht;
195
- var fxEditor = document.getElementById("luckysheet-functionbox-cell");
196
- if (fxEditor) {
197
- fxEditor.innerHTML = ht;
198
- }
199
- moveCursorToEnd(inputRef.current);
200
- setContext(function (draftCtx) {
201
- draftCtx.functionCandidates = [];
202
- draftCtx.defaultCandidates = [];
203
- draftCtx.functionHint = formulaName;
204
- });
205
- return;
206
- }
207
- var textEditor = document.getElementById("luckysheet-rich-text-editor");
271
+ var textEditor = inputRef.current;
208
272
  if (!textEditor) return;
209
- textEditor.focus();
210
- var selection = window.getSelection();
211
- var range = (selection === null || selection === void 0 ? void 0 : selection.rangeCount) ? selection.getRangeAt(0) : null;
212
- if (!selection || !range || !textEditor.contains(range.startContainer)) {
213
- range = document.createRange();
214
- range.selectNodeContents(textEditor);
215
- range.collapse(false);
216
- selection = window.getSelection();
217
- selection === null || selection === void 0 ? void 0 : selection.removeAllRanges();
218
- selection === null || selection === void 0 ? void 0 : selection.addRange(range);
219
- }
220
- var searchTxt = ((_a = getrangeseleciton()) === null || _a === void 0 ? void 0 : _a.textContent) || "";
221
- var deleteCount = searchTxt === "=" ? 0 : searchTxt.length;
222
- if (deleteCount > 0 && range.startContainer.nodeType === Node.TEXT_NODE && textEditor.contains(range.startContainer)) {
223
- var startOffset = Math.max(range.startOffset - deleteCount, 0);
224
- var endOffset = range.startOffset;
225
- range.setStart(range.startContainer, startOffset);
226
- range.setEnd(range.startContainer, endOffset);
227
- range.deleteContents();
228
- }
229
- textEditor.querySelectorAll(".luckysheet-formula-text-func, .luckysheet-formula-text-lpar").forEach(function (el) {
230
- return el.remove();
231
- });
232
- var funcNode = new DOMParser().parseFromString("<span dir=\"auto\" class=\"luckysheet-formula-text-func\">".concat(formulaName, "</span>"), "text/html").body.firstChild;
233
- var parNode = new DOMParser().parseFromString("<span dir=\"auto\" class=\"luckysheet-formula-text-lpar\">(</span>", "text/html").body.firstChild;
234
- if (range && parNode && funcNode) {
235
- range.insertNode(funcNode);
236
- range.collapse(false);
237
- range.insertNode(parNode);
238
- range.collapse(false);
239
- selection === null || selection === void 0 ? void 0 : selection.removeAllRanges();
240
- selection === null || selection === void 0 ? void 0 : selection.addRange(range);
241
- }
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));
242
285
  setContext(function (draftCtx) {
243
286
  draftCtx.functionCandidates = [];
244
287
  draftCtx.defaultCandidates = [];
245
- draftCtx.functionHint = formulaName;
288
+ draftCtx.functionHint = (formulaName || "").toUpperCase();
246
289
  });
247
290
  }, [setContext]);
248
291
  var clearSearchItemActiveClass = useCallback(function () {
@@ -265,7 +308,7 @@ var InputBox = function InputBox() {
265
308
  var _a, _b;
266
309
  if (isComposingRef.current || !inputRef.current) return;
267
310
  if (e.target.className.includes("sign-fortune")) return;
268
- preText.current = inputRef.current.innerText;
311
+ preTextRef.current = inputRef.current.innerText;
269
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;
270
313
  var lastSpanText = getLastInputSpanText();
271
314
  if (formulaName && !isLetterNumberPattern(lastSpanText)) {
@@ -279,58 +322,149 @@ var InputBox = function InputBox() {
279
322
  event.preventDefault();
280
323
  };
281
324
  useEffect(function () {
282
- var _a, _b;
283
- var selection = (_a = context.luckysheet_select_save) === null || _a === void 0 ? void 0 : _a[0];
284
- var editor = inputRef.current;
285
- if (!selection || !editor || !isInputBoxActive || ((_b = context.rangeDialog) === null || _b === void 0 ? void 0 : _b.show)) {
286
- return;
287
- }
288
- var rowSel = selection.row;
289
- var colSel = selection.column;
290
- if (!rowSel || !colSel || rowSel.length < 2 || colSel.length < 2) return;
291
- var rowStart = Math.min(rowSel[0], rowSel[1]);
292
- var rowEnd = Math.max(rowSel[0], rowSel[1]);
293
- var colStart = Math.min(colSel[0], colSel[1]);
294
- var colEnd = Math.max(colSel[0], colSel[1]);
295
- var startRef = "".concat(indexToColumnChar(colStart)).concat(rowStart + 1);
296
- var endRef = "".concat(indexToColumnChar(colEnd)).concat(rowEnd + 1);
297
- var refText = startRef === endRef ? startRef : "".concat(startRef, ":").concat(endRef);
298
- var editorText = editor.innerText || "";
299
- if (!editorText.startsWith("=")) return;
300
- var spans = editor.querySelectorAll("span");
301
- var lastSpan = spans[spans.length - 1];
302
- var lastSpanText = (lastSpan === null || lastSpan === void 0 ? void 0 : lastSpan.innerText) || "";
303
- var isA1RangePattern = /^[a-zA-Z]+\d+:[a-zA-Z]+\d+$/.test(lastSpanText);
304
- var notFunctionInit = !editorText.includes("(");
305
- var refNotAllowed = lastSpanText.includes(")") || notFunctionInit && /^[a-zA-Z]+$/.test(lastSpanText) && !_.includes(["="], lastSpanText);
306
- var shouldHandleRef = (lastSpan === null || lastSpan === void 0 ? void 0 : lastSpan.classList.contains("fortune-formula-functionrange-cell")) || isLetterNumberPattern(lastSpanText) || isA1RangePattern || (lastSpanText === "(" || lastSpanText === "," || lastSpanText.includes(":") || lastSpanText === "=") && !lastSpanText.includes('"') && !refNotAllowed && !/^[a-zA-Z]+$/.test(lastSpanText);
307
- if (!shouldHandleRef) return;
308
- var refSpanHtml = "<span class=\"fortune-formula-functionrange-cell\" rangeindex=\"0\" dir=\"auto\" style=\"color:#c1232b;\">".concat(refText, "</span>");
309
- if (lastSpan === null || lastSpan === void 0 ? void 0 : lastSpan.classList.contains("fortune-formula-functionrange-cell")) {
310
- if (lastSpan.textContent !== refText) lastSpan.textContent = refText;
311
- setTimeout(function () {
312
- return moveCursorToEnd(editor);
313
- }, 1);
314
- return;
315
- }
316
- if (isLetterNumberPattern(lastSpanText) || isA1RangePattern) {
317
- var htmlR = removeLastSpan(editor.innerHTML);
318
- editor.innerHTML = "".concat(htmlR).concat(refSpanHtml);
319
- setTimeout(function () {
320
- return moveCursorToEnd(editor);
321
- }, 1);
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) {
322
390
  return;
323
391
  }
324
- editor.innerHTML = "".concat(editor.innerHTML).concat(refSpanHtml);
325
- setTimeout(function () {
326
- return moveCursorToEnd(editor);
327
- }, 1);
328
- }, [context.luckysheet_select_save, (_e = context.rangeDialog) === null || _e === void 0 ? void 0 : _e.show, isInputBoxActive]);
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]);
329
407
  var onKeyDown = useCallback(function (e) {
330
- var _a, _b;
408
+ var _a, _b, _c;
409
+ setContext(function (draftCtx) {
410
+ setFormulaEditorOwner(draftCtx, "cell");
411
+ });
331
412
  lastKeyDownEventRef.current = new KeyboardEvent(e.type, e.nativeEvent);
332
- preText.current = inputRef.current.innerText;
333
- 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
+ }
334
468
  if (e.code === "KeyB") {
335
469
  handleBold(context, inputRef.current);
336
470
  stopPropagation(e);
@@ -350,7 +484,32 @@ var InputBox = function InputBox() {
350
484
  setCommaCount(currentCommaCount);
351
485
  }
352
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;
353
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
+ }
354
513
  if (isComposingRef.current) requestAnimationFrame(ensureNotEmpty);
355
514
  if (getCursorPosition(inputRef === null || inputRef === void 0 ? void 0 : inputRef.current) === (inputRef === null || inputRef === void 0 ? void 0 : inputRef.current.innerText.length)) {
356
515
  setTimeout(function () {
@@ -358,20 +517,8 @@ var InputBox = function InputBox() {
358
517
  }, 5);
359
518
  }
360
519
  }
361
- var isArrowKey = e.key === "ArrowUp" || e.key === "ArrowDown" || e.key === "ArrowLeft" || e.key === "ArrowRight";
362
- if (isArrowKey && !(getCursorPosition(inputRef === null || inputRef === void 0 ? void 0 : inputRef.current) !== (inputRef === null || inputRef === void 0 ? void 0 : inputRef.current.innerText.length) && e.key === "ArrowRight")) {
363
- var editorText = ((_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.innerText) || "";
364
- var lastSpanText = getLastInputSpanText() || "";
365
- var isA1RangePattern = /^[a-zA-Z]+\d+:[a-zA-Z]+\d+$/.test(lastSpanText);
366
- var notFunctionInit = !editorText.includes("(");
367
- var refNotAllowed = lastSpanText.includes(")") || notFunctionInit && /^[a-zA-Z]+$/.test(lastSpanText) && !_.includes(["="], lastSpanText);
368
- var shouldTreatAsRefNavigation = editorText.startsWith("=") && (Boolean((_b = inputRef.current) === null || _b === void 0 ? void 0 : _b.querySelector(".fortune-formula-functionrange-cell")) || isLetterNumberPattern(lastSpanText) || isA1RangePattern || (lastSpanText === "(" || lastSpanText === "," || lastSpanText.includes(":") || lastSpanText === "=") && !lastSpanText.includes('"') && !refNotAllowed && !/^[a-zA-Z]+$/.test(lastSpanText));
369
- if (shouldTreatAsRefNavigation) {
370
- allowListNavigation = false;
371
- setTimeout(function () {
372
- if (inputRef.current) moveCursorToEnd(inputRef.current);
373
- }, 1);
374
- }
520
+ if (isArrowKey && isFormulaReferenceInputMode(context)) {
521
+ allowListNavigation = false;
375
522
  }
376
523
  if (e.key === "Escape" && context.luckysheetCellUpdate.length > 0) {
377
524
  setContext(function (draftCtx) {
@@ -390,7 +537,7 @@ var InputBox = function InputBox() {
390
537
  e.preventDefault();
391
538
  } else if (e.key === "F4" && context.luckysheetCellUpdate.length > 0) {
392
539
  e.preventDefault();
393
- } 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)) {
394
541
  if (document.getElementById("luckysheet-formula-search-c")) {
395
542
  var formulaSearchContainer = document.getElementById("luckysheet-formula-search-c");
396
543
  var activeItem = formulaSearchContainer === null || formulaSearchContainer === void 0 ? void 0 : formulaSearchContainer.querySelector(".luckysheet-formula-search-item-active");
@@ -409,7 +556,7 @@ var InputBox = function InputBox() {
409
556
  }
410
557
  }
411
558
  e.preventDefault();
412
- } 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)) {
413
560
  if (document.getElementById("luckysheet-formula-search-c")) {
414
561
  var formulaSearchContainer = document.getElementById("luckysheet-formula-search-c");
415
562
  var activeItem = formulaSearchContainer === null || formulaSearchContainer === void 0 ? void 0 : formulaSearchContainer.querySelector(".luckysheet-formula-search-item-active");
@@ -427,7 +574,7 @@ var InputBox = function InputBox() {
427
574
  }
428
575
  e.preventDefault();
429
576
  }
430
- }, [clearSearchItemActiveClass, context.luckysheetCellUpdate.length, selectActiveFormula, setContext]);
577
+ }, [capturePreFormulaState, clearSearchItemActiveClass, context.luckysheetCellUpdate.length, handleFormulaHistoryUndoRedo, selectActiveFormula, setContext, firstSelection, refs.cellInput]);
431
578
  var handleHideShowHint = function handleHideShowHint() {
432
579
  var _a, _b, _c, _d;
433
580
  var searchElFx = (_a = document.getElementsByClassName("fx-search")) === null || _a === void 0 ? void 0 : _a[0];
@@ -448,36 +595,62 @@ var InputBox = function InputBox() {
448
595
  }
449
596
  };
450
597
  var onChange = useCallback(function (__, isBlur) {
451
- var _a;
598
+ var _a, _b, _c, _d, _e, _f;
452
599
  if (context.isFlvReadOnly) return;
453
600
  handleHideShowHint();
454
- 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())) {
455
- setShowSearchHint(true);
456
- } else {
457
- setShowSearchHint(false);
458
- }
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));
459
604
  if (!isComposingRef.current) {
460
605
  var currentCommaCount = countCommasBeforeCursor(inputRef === null || inputRef === void 0 ? void 0 : inputRef.current);
461
606
  setCommaCount(currentCommaCount);
462
607
  }
463
608
  var e = lastKeyDownEventRef.current;
464
- 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
+ }
465
619
  var kcode = e.keyCode;
466
620
  if (!kcode) return;
621
+ appendFormulaHistoryFromPrimaryEditor(function () {
622
+ return getCursorPosition(inputRef.current);
623
+ });
467
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) {
468
625
  setContext(function (draftCtx) {
626
+ var _a, _b;
469
627
  if ((draftCtx.formulaCache.rangestart || draftCtx.formulaCache.rangedrag_column_start || draftCtx.formulaCache.rangedrag_row_start || israngeseleciton(draftCtx)) && isBlur) return;
470
628
  if (!isAllowEdit(draftCtx, draftCtx.luckysheet_select_save)) {
471
629
  return;
472
630
  }
473
- 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
+ }
474
636
  });
475
637
  }
476
- }, [refs.cellInput, refs.fxInput, setContext]);
638
+ }, [refs.cellInput, refs.fxInput, setContext, appendFormulaHistoryFromPrimaryEditor]);
477
639
  var onPaste = useCallback(function (e) {
640
+ var plainText = e.clipboardData.getData("text/plain");
641
+ e.preventDefault();
478
642
  if (_.isEmpty(context.luckysheetCellUpdate)) {
479
- e.preventDefault();
643
+ return;
480
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 () {});
481
654
  }, [context.luckysheetCellUpdate]);
482
655
  var cfg = context.config || {};
483
656
  var rowReadOnly = cfg.rowReadOnly || {};
@@ -517,7 +690,7 @@ var InputBox = function InputBox() {
517
690
  zIndex: _.isEmpty(context.luckysheetCellUpdate) ? -1 : 19,
518
691
  display: "block"
519
692
  };
520
- }, [firstSelection, (_f = context.rangeDialog) === null || _f === void 0 ? void 0 : _f.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]);
521
694
  useEffect(function () {
522
695
  var _a;
523
696
  if (firstSelection && !((_a = context.rangeDialog) === null || _a === void 0 ? void 0 : _a.show) && !isInputBoxActive && !_.isEmpty(context.luckysheetCellUpdate)) {
@@ -533,7 +706,7 @@ var InputBox = function InputBox() {
533
706
  setIsInputBoxActive(true);
534
707
  }
535
708
  }
536
- }, [firstSelection, (_g = context.rangeDialog) === null || _g === void 0 ? void 0 : _g.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]);
537
710
  var getAddressIndicatorPosition = useCallback(function () {
538
711
  var _a;
539
712
  if (!firstSelection || ((_a = context.rangeDialog) === null || _a === void 0 ? void 0 : _a.show)) {
@@ -546,7 +719,7 @@ var InputBox = function InputBox() {
546
719
  left: "0",
547
720
  display: "block"
548
721
  };
549
- }, [firstSelection, (_h = context.rangeDialog) === null || _h === void 0 ? void 0 : _h.show]);
722
+ }, [firstSelection, (_g = context.rangeDialog) === null || _g === void 0 ? void 0 : _g.show]);
550
723
  var getCellAddress = useCallback(function () {
551
724
  if (!firstSelection) return "";
552
725
  var rowIndex = firstSelection.row_focus || 0;
@@ -562,6 +735,29 @@ var InputBox = function InputBox() {
562
735
  setFirstSelectionActiveCell((_a = context.luckysheet_select_save) === null || _a === void 0 ? void 0 : _a[0]);
563
736
  }
564
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]);
565
761
  useEffect(function () {
566
762
  var handleKeyDown = function handleKeyDown(event) {
567
763
  if (event.key === "F10") {
@@ -596,15 +792,15 @@ var InputBox = function InputBox() {
596
792
  };
597
793
  });
598
794
  setLinkSelectionHighlightRects(relative);
599
- }, [(_j = context.linkCard) === null || _j === void 0 ? void 0 : _j.applyToSelection, (_k = context.linkCard) === null || _k === void 0 ? void 0 : _k.selectionOffsets, (_l = context.linkCard) === null || _l === void 0 ? void 0 : _l.r, (_m = context.linkCard) === null || _m === void 0 ? void 0 : _m.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]);
600
796
  var wraperGetCell = function wraperGetCell() {
601
797
  var cell = getCellAddress();
602
- placeRef.current = cell;
603
798
  if (activeRefCell !== cell) {
604
799
  setActiveRefCell(cell);
605
800
  }
606
801
  return activeCell || cell;
607
802
  };
803
+ useRerenderOnFormulaCaret(inputRef, context.luckysheetCellUpdate.length > 0);
608
804
  var getFunctionNameFromInput = useCallback(function () {
609
805
  var _a, _b;
610
806
  var inputText = ((_a = inputRef === null || inputRef === void 0 ? void 0 : inputRef.current) === null || _a === void 0 ? void 0 : _a.innerText) || "";
@@ -621,8 +817,10 @@ var InputBox = function InputBox() {
621
817
  }
622
818
  return null;
623
819
  }, []);
624
- 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();
625
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;
626
824
  return /*#__PURE__*/React.createElement("div", {
627
825
  className: "luckysheet-input-box",
628
826
  id: "luckysheet-input-box",
@@ -633,16 +831,16 @@ var InputBox = function InputBox() {
633
831
  onMouseUp: function onMouseUp(e) {
634
832
  return e.stopPropagation();
635
833
  }
636
- }, firstSelection && !((_o = context.rangeDialog) === null || _o === void 0 ? void 0 : _o.show) && (/*#__PURE__*/React.createElement("div", {
834
+ }, firstSelection && !((_p = context.rangeDialog) === null || _p === void 0 ? void 0 : _p.show) && showAddressIndicator && (/*#__PURE__*/React.createElement("div", {
637
835
  className: "luckysheet-cell-address-indicator",
638
836
  style: getAddressIndicatorPosition()
639
837
  }, wraperGetCell())), /*#__PURE__*/React.createElement("div", {
640
838
  ref: inputBoxInnerRef,
641
839
  className: "luckysheet-input-box-inner",
642
- style: firstSelection ? __assign({
840
+ style: inputBoxBaseSelection ? __assign({
643
841
  position: "relative",
644
- minWidth: firstSelection.width,
645
- minHeight: firstSelection.height
842
+ minWidth: inputBoxBaseSelection.width,
843
+ minHeight: inputBoxBaseSelection.height
646
844
  }, inputBoxStyle) : {
647
845
  position: "relative"
648
846
  }
@@ -663,10 +861,23 @@ var InputBox = function InputBox() {
663
861
  },
664
862
  onMouseUp: function onMouseUp() {
665
863
  handleHideShowHint();
864
+ setContext(function (draftCtx) {
865
+ setFormulaEditorOwner(draftCtx, "cell");
866
+ });
666
867
  if (!isComposingRef.current) {
667
868
  var currentCommaCount = countCommasBeforeCursor(inputRef === null || inputRef === void 0 ? void 0 : inputRef.current);
668
869
  setCommaCount(currentCommaCount);
669
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
+ });
670
881
  },
671
882
  innerRef: function innerRef(e) {
672
883
  inputRef.current = e;
@@ -685,6 +896,7 @@ var InputBox = function InputBox() {
685
896
  onChange: onChange,
686
897
  onKeyDown: onKeyDown,
687
898
  onPaste: onPaste,
899
+ onCopy: onCopy,
688
900
  allowEdit: edit ? !isHidenRC : edit
689
901
  }), linkSelectionHighlightRects.length > 0 && (/*#__PURE__*/React.createElement("div", {
690
902
  className: "luckysheet-input-box-link-selection-highlight",
@@ -709,7 +921,7 @@ var InputBox = function InputBox() {
709
921
  backgroundColor: "rgba(0, 123, 255, 0.25)"
710
922
  }
711
923
  });
712
- })))), (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, {
713
925
  onMouseMove: function onMouseMove(e) {
714
926
  if (document.getElementById("luckysheet-formula-search-c")) {
715
927
  var hoveredItem = e.target.closest(".luckysheet-formula-search-item");
@@ -724,12 +936,12 @@ var InputBox = function InputBox() {
724
936
  }
725
937
  })), /*#__PURE__*/React.createElement("div", {
726
938
  className: "cell-hint"
727
- }, showFormulaHint && fn && (/*#__PURE__*/React.createElement(FormulaHint, {
939
+ }, showFormulaHint && fn && !showSearchHint && (/*#__PURE__*/React.createElement(FormulaHint, {
728
940
  handleShowFormulaHint: handleShowFormulaHint,
729
941
  showFormulaHint: showFormulaHint,
730
942
  commaCount: commaCount,
731
943
  functionName: functionName
732
- })), !showFormulaHint && fn && (/*#__PURE__*/React.createElement(Tooltip, {
944
+ })), !showFormulaHint && fn && !showSearchHint && (/*#__PURE__*/React.createElement(Tooltip, {
733
945
  text: "Turn on formula suggestions (F10)",
734
946
  placement: "top",
735
947
  defaultOpen: true,