@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
@@ -14,7 +14,10 @@ var _ContentEditable = _interopRequireDefault(require("./ContentEditable"));
14
14
  var _FormulaSearch = _interopRequireDefault(require("./FormulaSearch"));
15
15
  var _FormulaHint = _interopRequireDefault(require("./FormulaHint"));
16
16
  var _usePrevious = _interopRequireDefault(require("../../hooks/usePrevious"));
17
+ var _useFormulaEditorHistory = require("../../hooks/useFormulaEditorHistory");
18
+ var _useRerenderOnFormulaCaret = require("../../hooks/useRerenderOnFormulaCaret");
17
19
  var _helper = require("./helper");
20
+ var _formulaSegmentBoundary = require("./formula-segment-boundary");
18
21
  var _LucideIcon = require("./LucideIcon");
19
22
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
20
23
  function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t2 in e) "default" !== _t2 && {}.hasOwnProperty.call(e, _t2) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t2)) && (i.get || i.set) ? o(f, _t2, i) : f[_t2] = e[_t2]); return f; })(e, t); }
@@ -29,57 +32,72 @@ var __assign = void 0 && (void 0).__assign || function () {
29
32
  return __assign.apply(this, arguments);
30
33
  };
31
34
  var InputBox = function InputBox() {
32
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
33
- var _m = (0, _react.useContext)(_context.default),
34
- context = _m.context,
35
- setContext = _m.setContext,
36
- refs = _m.refs;
35
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p;
36
+ var _q = (0, _react.useContext)(_context.default),
37
+ context = _q.context,
38
+ setContext = _q.setContext,
39
+ refs = _q.refs;
37
40
  var inputRef = (0, _react.useRef)(null);
38
41
  var lastKeyDownEventRef = (0, _react.useRef)(null);
39
42
  var prevCellUpdate = (0, _usePrevious.default)(context.luckysheetCellUpdate);
40
43
  var prevSheetId = (0, _usePrevious.default)(context.currentSheetId);
41
- var _o = (0, _react.useState)(false),
42
- isHidenRC = _o[0],
43
- setIsHidenRC = _o[1];
44
- var _p = (0, _react.useState)(false),
45
- isInputBoxActive = _p[0],
46
- setIsInputBoxActive = _p[1];
47
- var _q = (0, _react.useState)(""),
48
- activeCell = _q[0],
49
- setActiveCell = _q[1];
50
- var _r = (0, _react.useState)(""),
51
- activeRefCell = _r[0],
52
- setActiveRefCell = _r[1];
53
- var _s = (0, _react.useState)({
44
+ var _r = (0, _react.useState)(false),
45
+ isHidenRC = _r[0],
46
+ setIsHidenRC = _r[1];
47
+ var _s = (0, _react.useState)(false),
48
+ isInputBoxActive = _s[0],
49
+ setIsInputBoxActive = _s[1];
50
+ var _t = (0, _react.useState)(""),
51
+ activeCell = _t[0],
52
+ setActiveCell = _t[1];
53
+ var _u = (0, _react.useState)(""),
54
+ activeRefCell = _u[0],
55
+ setActiveRefCell = _u[1];
56
+ var _v = (0, _react.useState)(false),
57
+ showAddressIndicator = _v[0],
58
+ setShowAddressIndicator = _v[1];
59
+ var scrollAtEditSessionStartRef = (0, _react.useRef)(null);
60
+ var _w = (0, _react.useState)({
54
61
  left: 0,
55
62
  top: 0
56
63
  }),
57
- frozenPosition = _s[0],
58
- setFrozenPosition = _s[1];
64
+ frozenPosition = _w[0],
65
+ setFrozenPosition = _w[1];
59
66
  var firstSelection = (_a = context.luckysheet_select_save) === null || _a === void 0 ? void 0 : _a[0];
60
- var _t = (0, _react.useState)({}),
61
- firstSelectionActiveCell = _t[0],
62
- setFirstSelectionActiveCell = _t[1];
63
- var _u = (0, _react.useState)(0),
64
- commaCount = _u[0],
65
- setCommaCount = _u[1];
67
+ var _x = (0, _react.useState)({}),
68
+ firstSelectionActiveCell = _x[0],
69
+ setFirstSelectionActiveCell = _x[1];
70
+ var _y = (0, _react.useState)(0),
71
+ commaCount = _y[0],
72
+ setCommaCount = _y[1];
73
+ var _z = (0, _react.useState)(false),
74
+ cellEditorIsFormula = _z[0],
75
+ setCellEditorIsFormula = _z[1];
66
76
  var hideFormulaHintLocal = localStorage.getItem("formulaMore") === "true";
67
- var _v = (0, _react.useState)(!hideFormulaHintLocal),
68
- showFormulaHint = _v[0],
69
- setShowFormulaHint = _v[1];
70
- var _w = (0, _react.useState)(false),
71
- showSearchHint = _w[0],
72
- setShowSearchHint = _w[1];
77
+ var _0 = (0, _react.useState)(!hideFormulaHintLocal),
78
+ showFormulaHint = _0[0],
79
+ setShowFormulaHint = _0[1];
80
+ var _1 = (0, _react.useState)(false),
81
+ showSearchHint = _1[0],
82
+ setShowSearchHint = _1[1];
73
83
  var row_index = firstSelection === null || firstSelection === void 0 ? void 0 : firstSelection.row_focus;
74
84
  var col_index = firstSelection === null || firstSelection === void 0 ? void 0 : firstSelection.column_focus;
75
- var preText = (0, _react.useRef)("");
76
- var placeRef = (0, _react.useRef)("");
77
85
  var isComposingRef = (0, _react.useRef)(false);
86
+ var formulaAnchorCellRef = (0, _react.useRef)(null);
87
+ var skipNextAnchorSelectionSyncRef = (0, _react.useRef)(false);
88
+ var lastHandledMouseDragSignatureRef = (0, _react.useRef)("");
89
+ var _2 = (0, _useFormulaEditorHistory.useFormulaEditorHistory)(inputRef, refs.cellInput, refs.fxInput, setContext, "cell"),
90
+ formulaHistoryRef = _2.formulaHistoryRef,
91
+ preTextRef = _2.preTextRef,
92
+ resetFormulaHistory = _2.resetFormulaHistory,
93
+ handleFormulaHistoryUndoRedo = _2.handleFormulaHistoryUndoRedo,
94
+ capturePreFormulaState = _2.capturePreFormulaState,
95
+ appendFormulaHistoryFromPrimaryEditor = _2.appendFormulaHistoryFromPrimaryEditor;
78
96
  var ZWSP = "\u200B";
79
97
  var inputBoxInnerRef = (0, _react.useRef)(null);
80
- var _x = (0, _react.useState)([]),
81
- linkSelectionHighlightRects = _x[0],
82
- setLinkSelectionHighlightRects = _x[1];
98
+ var _3 = (0, _react.useState)([]),
99
+ linkSelectionHighlightRects = _3[0],
100
+ setLinkSelectionHighlightRects = _3[1];
83
101
  var ensureNotEmpty = function ensureNotEmpty() {
84
102
  var el = inputRef.current;
85
103
  if (!el) return;
@@ -102,12 +120,11 @@ var InputBox = function InputBox() {
102
120
  return lastSpan === null || lastSpan === void 0 ? void 0 : lastSpan.innerText;
103
121
  };
104
122
  var inputBoxStyle = (0, _react.useMemo)(function () {
105
- var _a;
106
123
  if (firstSelectionActiveCell && context.luckysheetCellUpdate.length > 0) {
107
124
  var flowdata = (0, _fortuneCore.getFlowdata)(context);
108
125
  if (!flowdata) return {};
109
126
  var style = (0, _fortuneCore.getStyleByCell)(context, flowdata, firstSelectionActiveCell.row_focus, firstSelectionActiveCell.column_focus);
110
- if (((_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.innerText.charAt(0)) === "=") {
127
+ if (cellEditorIsFormula) {
111
128
  style = __assign(__assign({}, style), {
112
129
  textAlign: "left"
113
130
  });
@@ -115,7 +132,7 @@ var InputBox = function InputBox() {
115
132
  return style;
116
133
  }
117
134
  return {};
118
- }, [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]);
135
+ }, [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]);
119
136
  (0, _react.useLayoutEffect)(function () {
120
137
  var _a;
121
138
  if (!context.allowEdit) {
@@ -135,10 +152,23 @@ var InputBox = function InputBox() {
135
152
  if (_lodash.default.isEqual(prevCellUpdate, context.luckysheetCellUpdate) && prevSheetId === context.currentSheetId) {
136
153
  return;
137
154
  }
155
+ var _b = context.luckysheetCellUpdate,
156
+ ur = _b[0],
157
+ uc = _b[1];
158
+ var pending_1 = refs.globalCache.pendingTypeOverCell;
159
+ if (pending_1 && pending_1[0] === ur && pending_1[1] === uc) {
160
+ refs.globalCache.overwriteCell = false;
161
+ if (inputRef.current) {
162
+ setCellEditorIsFormula(inputRef.current.innerText.trim().startsWith("="));
163
+ }
164
+ delete refs.globalCache.doNotFocus;
165
+ return;
166
+ }
138
167
  var flowdata = (0, _fortuneCore.getFlowdata)(context);
139
168
  var cell = (_a = flowdata === null || flowdata === void 0 ? void 0 : flowdata[row_index]) === null || _a === void 0 ? void 0 : _a[col_index];
169
+ var overwrite = refs.globalCache.overwriteCell;
140
170
  var value_1 = "";
141
- if (cell && !refs.globalCache.overwriteCell) {
171
+ if (cell && !overwrite) {
142
172
  if ((0, _fortuneCore.isInlineStringCell)(cell)) {
143
173
  value_1 = (0, _fortuneCore.getInlineStringHTML)(row_index, col_index, flowdata);
144
174
  } else if (cell.f) {
@@ -154,14 +184,20 @@ var InputBox = function InputBox() {
154
184
  }
155
185
  }
156
186
  refs.globalCache.overwriteCell = false;
187
+ var wroteEditorFromStoredCell = false;
157
188
  if (!refs.globalCache.ignoreWriteCell && inputRef.current && value_1) {
158
189
  inputRef.current.innerHTML = (0, _fortuneCore.escapeHTMLTag)((0, _fortuneCore.escapeScriptTag)(value_1));
159
- } else if (!refs.globalCache.ignoreWriteCell && inputRef.current && !value_1) {
190
+ wroteEditorFromStoredCell = true;
191
+ } else if (!refs.globalCache.ignoreWriteCell && inputRef.current && !value_1 && !overwrite) {
160
192
  var valueD = (0, _fortuneCore.getCellValue)(row_index, col_index, flowdata, "f");
161
193
  inputRef.current.innerText = valueD;
194
+ wroteEditorFromStoredCell = true;
162
195
  }
163
196
  refs.globalCache.ignoreWriteCell = false;
164
- if (!refs.globalCache.doNotFocus) {
197
+ if (inputRef.current) {
198
+ setCellEditorIsFormula(inputRef.current.innerText.trim().startsWith("="));
199
+ }
200
+ if (wroteEditorFromStoredCell && !refs.globalCache.doNotFocus) {
165
201
  setTimeout(function () {
166
202
  (0, _fortuneCore.moveToEnd)(inputRef.current);
167
203
  });
@@ -174,8 +210,43 @@ var InputBox = function InputBox() {
174
210
  if (inputRef.current) {
175
211
  inputRef.current.innerHTML = "";
176
212
  }
213
+ delete refs.globalCache.pendingTypeOverCell;
214
+ setCellEditorIsFormula(false);
215
+ resetFormulaHistory();
177
216
  }
178
- }, [context.luckysheetCellUpdate]);
217
+ }, [context.luckysheetCellUpdate, resetFormulaHistory, refs.globalCache]);
218
+ (0, _react.useEffect)(function () {
219
+ if (_lodash.default.isEmpty(context.luckysheetCellUpdate)) return;
220
+ delete refs.globalCache.pendingTypeOverCell;
221
+ }, [context.luckysheetCellUpdate, refs.globalCache]);
222
+ (0, _react.useEffect)(function () {
223
+ var _a;
224
+ if (_lodash.default.isEmpty(context.luckysheetCellUpdate) || !refs.cellInput.current) {
225
+ formulaAnchorCellRef.current = null;
226
+ lastHandledMouseDragSignatureRef.current = "";
227
+ return;
228
+ }
229
+ var inputText = ((_a = refs.cellInput.current.innerText) === null || _a === void 0 ? void 0 : _a.trim()) || "";
230
+ if (!inputText.startsWith("=")) {
231
+ formulaAnchorCellRef.current = null;
232
+ lastHandledMouseDragSignatureRef.current = "";
233
+ }
234
+ }, [context.luckysheetCellUpdate, refs.cellInput]);
235
+ (0, _react.useEffect)(function () {
236
+ if (_lodash.default.isEmpty(context.luckysheetCellUpdate) || _lodash.default.isEmpty(prevCellUpdate) || _lodash.default.isEqual(prevCellUpdate, context.luckysheetCellUpdate)) {
237
+ return;
238
+ }
239
+ setContext(function (ctx) {
240
+ ctx.formulaRangeHighlight = [];
241
+ ctx.formulaRangeSelect = undefined;
242
+ ctx.formulaCache.selectingRangeIndex = -1;
243
+ ctx.formulaCache.func_selectedrange = undefined;
244
+ ctx.formulaCache.rangestart = false;
245
+ ctx.formulaCache.rangedrag_column_start = false;
246
+ ctx.formulaCache.rangedrag_row_start = false;
247
+ ctx.formulaCache.rangechangeindex = undefined;
248
+ });
249
+ }, [context.luckysheetCellUpdate, prevCellUpdate, setContext]);
179
250
  (0, _react.useEffect)(function () {
180
251
  setIsHidenRC((0, _fortuneCore.isShowHidenCR)(context));
181
252
  }, [context.luckysheet_select_save]);
@@ -185,65 +256,45 @@ var InputBox = function InputBox() {
185
256
  setIsInputBoxActive(false);
186
257
  }
187
258
  }, [firstSelection, (_c = context.rangeDialog) === null || _c === void 0 ? void 0 : _c.show, context.luckysheetCellUpdate]);
259
+ (0, _react.useEffect)(function () {
260
+ if (isInputBoxActive) return;
261
+ setContext(function (ctx) {
262
+ if (_lodash.default.isEmpty(ctx.formulaRangeHighlight) && !ctx.formulaRangeSelect && ctx.formulaCache.selectingRangeIndex === -1 && !ctx.formulaCache.func_selectedrange) {
263
+ return;
264
+ }
265
+ ctx.formulaRangeHighlight = [];
266
+ ctx.formulaRangeSelect = undefined;
267
+ ctx.formulaCache.selectingRangeIndex = -1;
268
+ ctx.formulaCache.func_selectedrange = undefined;
269
+ ctx.formulaCache.rangestart = false;
270
+ ctx.formulaCache.rangedrag_column_start = false;
271
+ ctx.formulaCache.rangedrag_row_start = false;
272
+ ctx.formulaCache.rangechangeindex = undefined;
273
+ ctx.formulaCache.rangeSelectionActive = null;
274
+ });
275
+ }, [isInputBoxActive, setContext]);
188
276
  var getActiveFormula = (0, _react.useCallback)(function () {
189
277
  return document.querySelector(".luckysheet-formula-search-item-active");
190
278
  }, []);
191
279
  var insertSelectedFormula = (0, _react.useCallback)(function (formulaName) {
192
- var _a;
193
- if (/^=[a-zA-Z]+$/.test(inputRef.current.innerText)) {
194
- 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>");
195
- inputRef.current.innerHTML = ht;
196
- var fxEditor = document.getElementById("luckysheet-functionbox-cell");
197
- if (fxEditor) {
198
- fxEditor.innerHTML = ht;
199
- }
200
- (0, _helper.moveCursorToEnd)(inputRef.current);
201
- setContext(function (draftCtx) {
202
- draftCtx.functionCandidates = [];
203
- draftCtx.defaultCandidates = [];
204
- draftCtx.functionHint = formulaName;
205
- });
206
- return;
207
- }
208
- var textEditor = document.getElementById("luckysheet-rich-text-editor");
280
+ var textEditor = inputRef.current;
209
281
  if (!textEditor) return;
210
- textEditor.focus();
211
- var selection = window.getSelection();
212
- var range = (selection === null || selection === void 0 ? void 0 : selection.rangeCount) ? selection.getRangeAt(0) : null;
213
- if (!selection || !range || !textEditor.contains(range.startContainer)) {
214
- range = document.createRange();
215
- range.selectNodeContents(textEditor);
216
- range.collapse(false);
217
- selection = window.getSelection();
218
- selection === null || selection === void 0 ? void 0 : selection.removeAllRanges();
219
- selection === null || selection === void 0 ? void 0 : selection.addRange(range);
220
- }
221
- var searchTxt = ((_a = (0, _fortuneCore.getrangeseleciton)()) === null || _a === void 0 ? void 0 : _a.textContent) || "";
222
- var deleteCount = searchTxt === "=" ? 0 : searchTxt.length;
223
- if (deleteCount > 0 && range.startContainer.nodeType === Node.TEXT_NODE && textEditor.contains(range.startContainer)) {
224
- var startOffset = Math.max(range.startOffset - deleteCount, 0);
225
- var endOffset = range.startOffset;
226
- range.setStart(range.startContainer, startOffset);
227
- range.setEnd(range.startContainer, endOffset);
228
- range.deleteContents();
229
- }
230
- textEditor.querySelectorAll(".luckysheet-formula-text-func, .luckysheet-formula-text-lpar").forEach(function (el) {
231
- return el.remove();
232
- });
233
- var funcNode = new DOMParser().parseFromString("<span dir=\"auto\" class=\"luckysheet-formula-text-func\">".concat(formulaName, "</span>"), "text/html").body.firstChild;
234
- var parNode = new DOMParser().parseFromString("<span dir=\"auto\" class=\"luckysheet-formula-text-lpar\">(</span>", "text/html").body.firstChild;
235
- if (range && parNode && funcNode) {
236
- range.insertNode(funcNode);
237
- range.collapse(false);
238
- range.insertNode(parNode);
239
- range.collapse(false);
240
- selection === null || selection === void 0 ? void 0 : selection.removeAllRanges();
241
- selection === null || selection === void 0 ? void 0 : selection.addRange(range);
242
- }
282
+ var fxEditor = document.getElementById("luckysheet-functionbox-cell");
283
+ var _a = (0, _helper.buildFormulaSuggestionText)(textEditor, formulaName),
284
+ text = _a.text,
285
+ caretOffset = _a.caretOffset;
286
+ var safeText = (0, _fortuneCore.escapeScriptTag)(text);
287
+ var html = safeText.startsWith("=") ? (0, _fortuneCore.functionHTMLGenerate)(safeText) : (0, _fortuneCore.escapeHTMLTag)(safeText);
288
+ textEditor.innerHTML = html;
289
+ if (fxEditor) {
290
+ fxEditor.innerHTML = html;
291
+ }
292
+ (0, _helper.setCursorPosition)(textEditor, caretOffset);
293
+ setShowSearchHint((0, _helper.shouldShowFormulaFunctionList)(textEditor));
243
294
  setContext(function (draftCtx) {
244
295
  draftCtx.functionCandidates = [];
245
296
  draftCtx.defaultCandidates = [];
246
- draftCtx.functionHint = formulaName;
297
+ draftCtx.functionHint = (formulaName || "").toUpperCase();
247
298
  });
248
299
  }, [setContext]);
249
300
  var clearSearchItemActiveClass = (0, _react.useCallback)(function () {
@@ -266,7 +317,7 @@ var InputBox = function InputBox() {
266
317
  var _a, _b;
267
318
  if (isComposingRef.current || !inputRef.current) return;
268
319
  if (e.target.className.includes("sign-fortune")) return;
269
- preText.current = inputRef.current.innerText;
320
+ preTextRef.current = inputRef.current.innerText;
270
321
  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;
271
322
  var lastSpanText = getLastInputSpanText();
272
323
  if (formulaName && !(0, _helper.isLetterNumberPattern)(lastSpanText)) {
@@ -279,22 +330,173 @@ var InputBox = function InputBox() {
279
330
  event.stopPropagation();
280
331
  event.preventDefault();
281
332
  };
333
+ (0, _react.useEffect)(function () {
334
+ var _a;
335
+ var cellInputEl = refs.cellInput.current;
336
+ if (!((_a = context.luckysheet_select_save) === null || _a === void 0 ? void 0 : _a[0]) || !cellInputEl) return;
337
+ setContext(function (ctx) {
338
+ var _a;
339
+ var currentSelection = (_a = ctx.luckysheet_select_save) === null || _a === void 0 ? void 0 : _a[ctx.luckysheet_select_save.length - 1];
340
+ if (!currentSelection) return;
341
+ if ((0, _fortuneCore.getFormulaEditorOwner)(ctx) === "fx") {
342
+ return;
343
+ }
344
+ (0, _fortuneCore.israngeseleciton)(ctx);
345
+ var keyboardSyncRangeIndex = (0, _fortuneCore.getFormulaRangeIndexForKeyboardSync)(cellInputEl);
346
+ if (skipNextAnchorSelectionSyncRef.current && formulaAnchorCellRef.current) {
347
+ var _b = formulaAnchorCellRef.current,
348
+ anchorRow = _b[0],
349
+ anchorCol = _b[1];
350
+ var isAnchorSelection = currentSelection.row_focus === anchorRow && currentSelection.column_focus === anchorCol;
351
+ if (isAnchorSelection) {
352
+ skipNextAnchorSelectionSyncRef.current = false;
353
+ return;
354
+ }
355
+ }
356
+ var isFormulaMode = (0, _fortuneCore.isFormulaReferenceInputMode)(ctx);
357
+ if (!isFormulaMode) return;
358
+ if (keyboardSyncRangeIndex !== null) {
359
+ ctx.formulaCache.rangechangeindex = keyboardSyncRangeIndex;
360
+ ctx.formulaCache.rangestart = true;
361
+ ctx.formulaCache.rangedrag_column_start = false;
362
+ ctx.formulaCache.rangedrag_row_start = false;
363
+ } else {
364
+ ctx.formulaCache.rangechangeindex = undefined;
365
+ ctx.formulaCache.rangestart = false;
366
+ }
367
+ ctx.formulaCache.rangeSelectionActive = true;
368
+ _fortuneCore.rangeSetValue === null || _fortuneCore.rangeSetValue === void 0 ? void 0 : (0, _fortuneCore.rangeSetValue)(ctx, cellInputEl, {
369
+ row: currentSelection.row,
370
+ column: currentSelection.column
371
+ }, refs.fxInput.current);
372
+ (0, _fortuneCore.rangeHightlightselected)(ctx, cellInputEl);
373
+ if (!_lodash.default.isNil(ctx.formulaCache.rangechangeindex)) {
374
+ ctx.formulaCache.selectingRangeIndex = ctx.formulaCache.rangechangeindex;
375
+ (0, _fortuneCore.createRangeHightlight)(ctx, cellInputEl.innerHTML, ctx.formulaCache.rangechangeindex);
376
+ var rectFromSelection = (0, _fortuneCore.seletedHighlistByindex)(ctx, currentSelection.row[0], currentSelection.row[1], currentSelection.column[0], currentSelection.column[1]);
377
+ if (rectFromSelection) {
378
+ (0, _fortuneCore.createFormulaRangeSelect)(ctx, {
379
+ rangeIndex: ctx.formulaCache.rangechangeindex || 0,
380
+ left: rectFromSelection.left,
381
+ top: rectFromSelection.top,
382
+ width: rectFromSelection.width,
383
+ height: rectFromSelection.height
384
+ });
385
+ }
386
+ }
387
+ });
388
+ }, [context.luckysheet_select_save, (_d = context.rangeDialog) === null || _d === void 0 ? void 0 : _d.show]);
389
+ var formulaMouseDragSignature = function () {
390
+ var _a, _b, _c, _d;
391
+ var r = context.formulaCache.func_selectedrange;
392
+ if (!r) return "";
393
+ 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(":");
394
+ }();
395
+ (0, _react.useEffect)(function () {
396
+ var _a;
397
+ if (!formulaMouseDragSignature) return;
398
+ if (lastHandledMouseDragSignatureRef.current === formulaMouseDragSignature) {
399
+ return;
400
+ }
401
+ if (!refs.cellInput.current) return;
402
+ var inputText = ((_a = refs.cellInput.current.innerText) === null || _a === void 0 ? void 0 : _a.trim()) || "";
403
+ if (!inputText.startsWith("=")) return;
404
+ var dragRange = context.formulaCache.func_selectedrange;
405
+ if (!dragRange) return;
406
+ lastHandledMouseDragSignatureRef.current = formulaMouseDragSignature;
407
+ setContext(function (ctx) {
408
+ ctx.luckysheet_select_save = [{
409
+ row: [dragRange.row[0], dragRange.row[1]],
410
+ column: [dragRange.column[0], dragRange.column[1]],
411
+ row_focus: _lodash.default.isNil(dragRange.row_focus) ? dragRange.row[0] : dragRange.row_focus,
412
+ column_focus: _lodash.default.isNil(dragRange.column_focus) ? dragRange.column[0] : dragRange.column_focus
413
+ }];
414
+ });
415
+ }, [formulaMouseDragSignature, context.formulaCache.func_selectedrange, refs.cellInput, setContext]);
282
416
  var onKeyDown = (0, _react.useCallback)(function (e) {
283
- var _a, _b;
417
+ var _a, _b, _c;
418
+ setContext(function (draftCtx) {
419
+ (0, _fortuneCore.setFormulaEditorOwner)(draftCtx, "cell");
420
+ });
284
421
  lastKeyDownEventRef.current = new KeyboardEvent(e.type, e.nativeEvent);
285
- preText.current = inputRef.current.innerText;
286
- if (e.metaKey && context.luckysheetCellUpdate.length > 0) {
422
+ capturePreFormulaState();
423
+ var currentInputText = ((_b = (_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.innerText) === null || _b === void 0 ? void 0 : _b.trim()) || "";
424
+ if ((e.key === "=" || currentInputText.startsWith("=")) && context.luckysheetCellUpdate.length === 2 && formulaAnchorCellRef.current == null) {
425
+ setContext(function (draftCtx) {
426
+ draftCtx.formulaCache.rangeSelectionActive = null;
427
+ });
428
+ formulaAnchorCellRef.current = [context.luckysheetCellUpdate[0], context.luckysheetCellUpdate[1]];
429
+ }
430
+ if (e.key === "(" && currentInputText.startsWith("=")) {
431
+ setContext(function (draftCtx) {
432
+ draftCtx.formulaCache.rangeSelectionActive = null;
433
+ });
434
+ }
435
+ if ((0, _formulaSegmentBoundary.isFormulaSegmentBoundaryKey)(e.key) && context.luckysheetCellUpdate.length > 0 && currentInputText.startsWith("=") && formulaAnchorCellRef.current) {
436
+ setContext(function (draftCtx) {
437
+ draftCtx.formulaCache.rangeSelectionActive = null;
438
+ });
439
+ var _d = formulaAnchorCellRef.current,
440
+ anchorRow_1 = _d[0],
441
+ anchorCol_1 = _d[1];
442
+ skipNextAnchorSelectionSyncRef.current = true;
443
+ setTimeout(function () {
444
+ setContext(function (draftCtx) {
445
+ var _a, _b;
446
+ draftCtx.luckysheetCellUpdate = [anchorRow_1, anchorCol_1];
447
+ draftCtx.luckysheet_select_save = [{
448
+ row: [anchorRow_1, anchorRow_1],
449
+ column: [anchorCol_1, anchorCol_1],
450
+ row_focus: anchorRow_1,
451
+ column_focus: anchorCol_1
452
+ }];
453
+ draftCtx.formulaRangeSelect = undefined;
454
+ draftCtx.formulaCache.selectingRangeIndex = -1;
455
+ draftCtx.formulaCache.func_selectedrange = undefined;
456
+ draftCtx.formulaCache.rangechangeindex = undefined;
457
+ draftCtx.formulaCache.rangestart = false;
458
+ draftCtx.formulaCache.rangedrag_column_start = false;
459
+ draftCtx.formulaCache.rangedrag_row_start = false;
460
+ (0, _fortuneCore.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) || "");
461
+ (0, _fortuneCore.moveHighlightCell)(draftCtx, "down", 0, "rangeOfSelect");
462
+ });
463
+ }, 0);
464
+ }
465
+ if ((e.metaKey || e.ctrlKey) && context.luckysheetCellUpdate.length > 0) {
466
+ if (e.code === "KeyZ" || e.code === "KeyY") {
467
+ var shouldUseFormulaHistory = currentInputText.startsWith("=") || formulaHistoryRef.current.active;
468
+ if (shouldUseFormulaHistory) {
469
+ var handledByFormulaHistory = handleFormulaHistoryUndoRedo(e.code === "KeyY" || e.code === "KeyZ" && e.shiftKey);
470
+ if (handledByFormulaHistory) {
471
+ e.preventDefault();
472
+ }
473
+ }
474
+ e.stopPropagation();
475
+ return;
476
+ }
287
477
  if (e.code === "KeyB") {
288
478
  (0, _fortuneCore.handleBold)(context, inputRef.current);
479
+ appendFormulaHistoryFromPrimaryEditor(function () {
480
+ return (0, _helper.getCursorPosition)(inputRef.current);
481
+ });
289
482
  stopPropagation(e);
290
483
  } else if (e.code === "KeyI") {
291
484
  (0, _fortuneCore.handleItalic)(context, inputRef.current);
485
+ appendFormulaHistoryFromPrimaryEditor(function () {
486
+ return (0, _helper.getCursorPosition)(inputRef.current);
487
+ });
292
488
  stopPropagation(e);
293
489
  } else if (e.code === "KeyU") {
294
490
  (0, _fortuneCore.handleUnderline)(context, inputRef.current);
491
+ appendFormulaHistoryFromPrimaryEditor(function () {
492
+ return (0, _helper.getCursorPosition)(inputRef.current);
493
+ });
295
494
  stopPropagation(e);
296
495
  } else if (e.code === "KeyS") {
297
496
  (0, _fortuneCore.handleStrikeThrough)(context, inputRef.current);
497
+ appendFormulaHistoryFromPrimaryEditor(function () {
498
+ return (0, _helper.getCursorPosition)(inputRef.current);
499
+ });
298
500
  stopPropagation(e);
299
501
  }
300
502
  }
@@ -303,7 +505,32 @@ var InputBox = function InputBox() {
303
505
  setCommaCount(currentCommaCount);
304
506
  }
305
507
  var allowListNavigation = true;
508
+ var isArrowKey = e.key === "ArrowUp" || e.key === "ArrowDown" || e.key === "ArrowLeft" || e.key === "ArrowRight";
509
+ var isInPlaceEditMode = ((_c = refs.globalCache) === null || _c === void 0 ? void 0 : _c.enteredEditByTyping) !== true;
306
510
  if (e.key === "Delete" || e.key === "Backspace") {
511
+ var anchor = formulaAnchorCellRef.current;
512
+ if (anchor != null) {
513
+ var anchorRow_2 = anchor[0],
514
+ anchorCol_2 = anchor[1];
515
+ skipNextAnchorSelectionSyncRef.current = true;
516
+ setTimeout(function () {
517
+ setContext(function (draftCtx) {
518
+ draftCtx.luckysheetCellUpdate = [anchorRow_2, anchorCol_2];
519
+ draftCtx.luckysheet_select_save = [{
520
+ row: [anchorRow_2, anchorRow_2],
521
+ column: [anchorCol_2, anchorCol_2],
522
+ row_focus: anchorRow_2,
523
+ column_focus: anchorCol_2
524
+ }];
525
+ (0, _fortuneCore.markRangeSelectionDirty)(draftCtx);
526
+ var el = refs.cellInput.current;
527
+ if (el && el.innerText.trim().startsWith("=")) {
528
+ (0, _fortuneCore.createRangeHightlight)(draftCtx, el.innerHTML);
529
+ (0, _fortuneCore.rangeHightlightselected)(draftCtx, el);
530
+ }
531
+ });
532
+ }, 0);
533
+ }
307
534
  if (isComposingRef.current) requestAnimationFrame(ensureNotEmpty);
308
535
  if ((0, _helper.getCursorPosition)(inputRef === null || inputRef === void 0 ? void 0 : inputRef.current) === (inputRef === null || inputRef === void 0 ? void 0 : inputRef.current.innerText.length)) {
309
536
  setTimeout(function () {
@@ -311,39 +538,8 @@ var InputBox = function InputBox() {
311
538
  }, 5);
312
539
  }
313
540
  }
314
- var refCell = placeRef.current;
315
- if (e.key === "ArrowUp") {
316
- refCell = (0, _helper.decrementRow)(placeRef.current);
317
- } else if (e.key === "ArrowDown") {
318
- refCell = (0, _helper.incrementRow)(placeRef.current);
319
- } else if (e.key === "ArrowLeft") {
320
- refCell = (0, _helper.decrementColumn)(placeRef.current);
321
- } else if (e.key === "ArrowRight") {
322
- refCell = (0, _helper.incrementColumn)(placeRef.current);
323
- }
324
- if ((e.key === "ArrowUp" || e.key === "ArrowDown" || e.key === "ArrowLeft" || e.key === "ArrowRight") && !((0, _helper.getCursorPosition)(inputRef === null || inputRef === void 0 ? void 0 : inputRef.current) !== (inputRef === null || inputRef === void 0 ? void 0 : inputRef.current.innerText.length) && e.key === "ArrowRight")) {
325
- var parser = new DOMParser();
326
- 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");
327
- var spans = doc.querySelectorAll("span");
328
- var lastSpan = spans[spans.length - 1];
329
- var notFunctionInit = !((_b = document.getElementById("luckysheet-rich-text-editor")) === null || _b === void 0 ? void 0 : _b.innerText.includes("("));
330
- 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) && !_lodash.default.includes(["="], lastSpan === null || lastSpan === void 0 ? void 0 : lastSpan.innerText);
331
- 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('"')) && !(0, _helper.isLetterNumberPattern)(lastSpan === null || lastSpan === void 0 ? void 0 : lastSpan.innerText) && !arrowRefNotAllowed && !/^[a-zA-Z]+$/.test(lastSpan === null || lastSpan === void 0 ? void 0 : lastSpan.innerText)) {
332
- allowListNavigation = false;
333
- inputRef.current.innerHTML = "".concat(inputRef.current.innerHTML, "<span class=\"fortune-formula-functionrange-cell\" rangeindex=\"0\" dir=\"auto\" style=\"color:#c1232b;\">").concat(refCell, "</span>");
334
- setTimeout(function () {
335
- (0, _helper.moveCursorToEnd)(inputRef.current);
336
- }, 1);
337
- }
338
- if ((0, _helper.isLetterNumberPattern)(lastSpan === null || lastSpan === void 0 ? void 0 : lastSpan.innerText)) {
339
- allowListNavigation = false;
340
- var htmlR = (0, _helper.removeLastSpan)(inputRef === null || inputRef === void 0 ? void 0 : inputRef.current.innerHTML);
341
- inputRef.current.innerHTML = "".concat(htmlR, "<span class=\"fortune-formula-functionrange-cell\" rangeindex=\"0\" dir=\"auto\" style=\"color:#c1232b;\">").concat(refCell, "</span>");
342
- (0, _helper.moveCursorToEnd)(inputRef.current);
343
- setTimeout(function () {
344
- (0, _helper.moveCursorToEnd)(inputRef.current);
345
- }, 1);
346
- }
541
+ if (isArrowKey && (0, _fortuneCore.isFormulaReferenceInputMode)(context)) {
542
+ allowListNavigation = false;
347
543
  }
348
544
  if (e.key === "Escape" && context.luckysheetCellUpdate.length > 0) {
349
545
  setContext(function (draftCtx) {
@@ -362,7 +558,7 @@ var InputBox = function InputBox() {
362
558
  e.preventDefault();
363
559
  } else if (e.key === "F4" && context.luckysheetCellUpdate.length > 0) {
364
560
  e.preventDefault();
365
- } else if (e.key === "ArrowUp" && context.luckysheetCellUpdate.length > 0 && allowListNavigation) {
561
+ } else if (!(e.metaKey || e.ctrlKey) && e.key === "ArrowUp" && context.luckysheetCellUpdate.length > 0 && allowListNavigation && !(e.shiftKey && isInPlaceEditMode)) {
366
562
  if (document.getElementById("luckysheet-formula-search-c")) {
367
563
  var formulaSearchContainer = document.getElementById("luckysheet-formula-search-c");
368
564
  var activeItem = formulaSearchContainer === null || formulaSearchContainer === void 0 ? void 0 : formulaSearchContainer.querySelector(".luckysheet-formula-search-item-active");
@@ -381,7 +577,7 @@ var InputBox = function InputBox() {
381
577
  }
382
578
  }
383
579
  e.preventDefault();
384
- } else if (e.key === "ArrowDown" && context.luckysheetCellUpdate.length > 0 && allowListNavigation) {
580
+ } else if (!(e.metaKey || e.ctrlKey) && e.key === "ArrowDown" && context.luckysheetCellUpdate.length > 0 && allowListNavigation && !(e.shiftKey && isInPlaceEditMode)) {
385
581
  if (document.getElementById("luckysheet-formula-search-c")) {
386
582
  var formulaSearchContainer = document.getElementById("luckysheet-formula-search-c");
387
583
  var activeItem = formulaSearchContainer === null || formulaSearchContainer === void 0 ? void 0 : formulaSearchContainer.querySelector(".luckysheet-formula-search-item-active");
@@ -399,7 +595,7 @@ var InputBox = function InputBox() {
399
595
  }
400
596
  e.preventDefault();
401
597
  }
402
- }, [clearSearchItemActiveClass, context.luckysheetCellUpdate.length, selectActiveFormula, setContext, firstSelection]);
598
+ }, [capturePreFormulaState, clearSearchItemActiveClass, context.luckysheetCellUpdate.length, handleFormulaHistoryUndoRedo, selectActiveFormula, setContext, firstSelection, refs.cellInput]);
403
599
  var handleHideShowHint = function handleHideShowHint() {
404
600
  var _a, _b, _c, _d;
405
601
  var searchElFx = (_a = document.getElementsByClassName("fx-search")) === null || _a === void 0 ? void 0 : _a[0];
@@ -420,36 +616,62 @@ var InputBox = function InputBox() {
420
616
  }
421
617
  };
422
618
  var onChange = (0, _react.useCallback)(function (__, isBlur) {
423
- var _a;
619
+ var _a, _b, _c, _d, _e, _f;
424
620
  if (context.isFlvReadOnly) return;
425
621
  handleHideShowHint();
426
- 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())) {
427
- setShowSearchHint(true);
428
- } else {
429
- setShowSearchHint(false);
430
- }
622
+ 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 : "";
623
+ setCellEditorIsFormula(editorText.startsWith("="));
624
+ setShowSearchHint((0, _helper.shouldShowFormulaFunctionList)((_d = inputRef === null || inputRef === void 0 ? void 0 : inputRef.current) !== null && _d !== void 0 ? _d : null));
431
625
  if (!isComposingRef.current) {
432
626
  var currentCommaCount = (0, _helper.countCommasBeforeCursor)(inputRef === null || inputRef === void 0 ? void 0 : inputRef.current);
433
627
  setCommaCount(currentCommaCount);
434
628
  }
435
629
  var e = lastKeyDownEventRef.current;
436
- if (!e) return;
630
+ if (!e) {
631
+ var cellEl_1 = refs.cellInput.current;
632
+ 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("=")))) {
633
+ setContext(function (draftCtx) {
634
+ if (!(0, _fortuneCore.isAllowEdit)(draftCtx, draftCtx.luckysheet_select_save)) return;
635
+ (0, _fortuneCore.rangeHightlightselected)(draftCtx, cellEl_1);
636
+ });
637
+ }
638
+ return;
639
+ }
437
640
  var kcode = e.keyCode;
438
641
  if (!kcode) return;
642
+ appendFormulaHistoryFromPrimaryEditor(function () {
643
+ return (0, _helper.getCursorPosition)(inputRef.current);
644
+ });
439
645
  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) {
440
646
  setContext(function (draftCtx) {
647
+ var _a, _b;
441
648
  if ((draftCtx.formulaCache.rangestart || draftCtx.formulaCache.rangedrag_column_start || draftCtx.formulaCache.rangedrag_row_start || (0, _fortuneCore.israngeseleciton)(draftCtx)) && isBlur) return;
442
649
  if (!(0, _fortuneCore.isAllowEdit)(draftCtx, draftCtx.luckysheet_select_save)) {
443
650
  return;
444
651
  }
445
- (0, _fortuneCore.handleFormulaInput)(draftCtx, refs.fxInput.current, refs.cellInput.current, kcode, preText.current);
652
+ (0, _fortuneCore.handleFormulaInput)(draftCtx, refs.fxInput.current, refs.cellInput.current, kcode, preTextRef.current);
653
+ var cellEl = refs.cellInput.current;
654
+ 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("=")))) {
655
+ (0, _fortuneCore.rangeHightlightselected)(draftCtx, cellEl);
656
+ }
446
657
  });
447
658
  }
448
- }, [refs.cellInput, refs.fxInput, setContext]);
659
+ }, [refs.cellInput, refs.fxInput, setContext, appendFormulaHistoryFromPrimaryEditor]);
449
660
  var onPaste = (0, _react.useCallback)(function (e) {
661
+ var plainText = e.clipboardData.getData("text/plain");
662
+ e.preventDefault();
450
663
  if (_lodash.default.isEmpty(context.luckysheetCellUpdate)) {
451
- e.preventDefault();
664
+ return;
452
665
  }
666
+ document.execCommand("insertText", false, plainText);
667
+ }, [context.luckysheetCellUpdate]);
668
+ var onCopy = (0, _react.useCallback)(function (e) {
669
+ var _a;
670
+ if (_lodash.default.isEmpty(context.luckysheetCellUpdate)) return;
671
+ e.preventDefault();
672
+ var sel = window.getSelection();
673
+ var text = sel && !sel.isCollapsed ? sel.toString() : e.currentTarget.innerText;
674
+ (_a = navigator.clipboard) === null || _a === void 0 ? void 0 : _a.writeText(text).catch(function () {});
453
675
  }, [context.luckysheetCellUpdate]);
454
676
  var cfg = context.config || {};
455
677
  var rowReadOnly = cfg.rowReadOnly || {};
@@ -489,7 +711,7 @@ var InputBox = function InputBox() {
489
711
  zIndex: _lodash.default.isEmpty(context.luckysheetCellUpdate) ? -1 : 19,
490
712
  display: "block"
491
713
  };
492
- }, [firstSelection, (_d = context.rangeDialog) === null || _d === void 0 ? void 0 : _d.show, context.luckysheetCellUpdate, refs.cellArea, isInputBoxActive, frozenPosition, context.scrollLeft, context.scrollTop]);
714
+ }, [firstSelection, (_e = context.rangeDialog) === null || _e === void 0 ? void 0 : _e.show, context.luckysheetCellUpdate, refs.cellArea, isInputBoxActive, frozenPosition, context.scrollLeft, context.scrollTop]);
493
715
  (0, _react.useEffect)(function () {
494
716
  var _a;
495
717
  if (firstSelection && !((_a = context.rangeDialog) === null || _a === void 0 ? void 0 : _a.show) && !isInputBoxActive && !_lodash.default.isEmpty(context.luckysheetCellUpdate)) {
@@ -505,7 +727,7 @@ var InputBox = function InputBox() {
505
727
  setIsInputBoxActive(true);
506
728
  }
507
729
  }
508
- }, [firstSelection, (_e = context.rangeDialog) === null || _e === void 0 ? void 0 : _e.show, context.luckysheetCellUpdate, isInputBoxActive, context.scrollLeft, context.scrollTop, refs.cellArea]);
730
+ }, [firstSelection, (_f = context.rangeDialog) === null || _f === void 0 ? void 0 : _f.show, context.luckysheetCellUpdate, isInputBoxActive, context.scrollLeft, context.scrollTop, refs.cellArea]);
509
731
  var getAddressIndicatorPosition = (0, _react.useCallback)(function () {
510
732
  var _a;
511
733
  if (!firstSelection || ((_a = context.rangeDialog) === null || _a === void 0 ? void 0 : _a.show)) {
@@ -518,7 +740,7 @@ var InputBox = function InputBox() {
518
740
  left: "0",
519
741
  display: "block"
520
742
  };
521
- }, [firstSelection, (_f = context.rangeDialog) === null || _f === void 0 ? void 0 : _f.show]);
743
+ }, [firstSelection, (_g = context.rangeDialog) === null || _g === void 0 ? void 0 : _g.show]);
522
744
  var getCellAddress = (0, _react.useCallback)(function () {
523
745
  if (!firstSelection) return "";
524
746
  var rowIndex = firstSelection.row_focus || 0;
@@ -534,6 +756,29 @@ var InputBox = function InputBox() {
534
756
  setFirstSelectionActiveCell((_a = context.luckysheet_select_save) === null || _a === void 0 ? void 0 : _a[0]);
535
757
  }
536
758
  }, [isInputBoxActive]);
759
+ (0, _react.useLayoutEffect)(function () {
760
+ var _a;
761
+ var editing = context.luckysheetCellUpdate.length > 0;
762
+ if (!editing) {
763
+ scrollAtEditSessionStartRef.current = null;
764
+ setShowAddressIndicator(false);
765
+ return;
766
+ }
767
+ var prevLen = (_a = prevCellUpdate === null || prevCellUpdate === void 0 ? void 0 : prevCellUpdate.length) !== null && _a !== void 0 ? _a : 0;
768
+ var startedThisCommit = prevLen === 0;
769
+ if (startedThisCommit || scrollAtEditSessionStartRef.current == null) {
770
+ scrollAtEditSessionStartRef.current = {
771
+ left: context.scrollLeft,
772
+ top: context.scrollTop
773
+ };
774
+ setShowAddressIndicator(false);
775
+ return;
776
+ }
777
+ var b = scrollAtEditSessionStartRef.current;
778
+ if (context.scrollLeft !== b.left || context.scrollTop !== b.top) {
779
+ setShowAddressIndicator(true);
780
+ }
781
+ }, [context.luckysheetCellUpdate, context.scrollLeft, context.scrollTop, prevCellUpdate]);
537
782
  (0, _react.useEffect)(function () {
538
783
  var handleKeyDown = function handleKeyDown(event) {
539
784
  if (event.key === "F10") {
@@ -568,15 +813,15 @@ var InputBox = function InputBox() {
568
813
  };
569
814
  });
570
815
  setLinkSelectionHighlightRects(relative);
571
- }, [(_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]);
816
+ }, [(_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]);
572
817
  var wraperGetCell = function wraperGetCell() {
573
818
  var cell = getCellAddress();
574
- placeRef.current = cell;
575
819
  if (activeRefCell !== cell) {
576
820
  setActiveRefCell(cell);
577
821
  }
578
822
  return activeCell || cell;
579
823
  };
824
+ (0, _useRerenderOnFormulaCaret.useRerenderOnFormulaCaret)(inputRef, context.luckysheetCellUpdate.length > 0);
580
825
  var getFunctionNameFromInput = (0, _react.useCallback)(function () {
581
826
  var _a, _b;
582
827
  var inputText = ((_a = inputRef === null || inputRef === void 0 ? void 0 : inputRef.current) === null || _a === void 0 ? void 0 : _a.innerText) || "";
@@ -593,8 +838,10 @@ var InputBox = function InputBox() {
593
838
  }
594
839
  return null;
595
840
  }, []);
596
- var functionName = context.functionHint || getFunctionNameFromInput();
841
+ var functionName = (_o = (_m = (0, _helper.getFunctionNameFromFormulaCaretSpans)(inputRef.current)) !== null && _m !== void 0 ? _m : context.functionHint) !== null && _o !== void 0 ? _o : getFunctionNameFromInput();
597
842
  var fn = functionName ? context.formulaCache.functionlistMap[functionName] : null;
843
+ var showCellFormulaChrome = context.luckysheetCellUpdate.length > 0 && (0, _fortuneCore.getFormulaEditorOwner)(context) === "cell";
844
+ var inputBoxBaseSelection = isInputBoxActive && firstSelectionActiveCell ? firstSelectionActiveCell : firstSelection;
598
845
  return /*#__PURE__*/_react.default.createElement("div", {
599
846
  className: "luckysheet-input-box",
600
847
  id: "luckysheet-input-box",
@@ -605,16 +852,16 @@ var InputBox = function InputBox() {
605
852
  onMouseUp: function onMouseUp(e) {
606
853
  return e.stopPropagation();
607
854
  }
608
- }, firstSelection && !((_l = context.rangeDialog) === null || _l === void 0 ? void 0 : _l.show) && (/*#__PURE__*/_react.default.createElement("div", {
855
+ }, firstSelection && !((_p = context.rangeDialog) === null || _p === void 0 ? void 0 : _p.show) && showAddressIndicator && (/*#__PURE__*/_react.default.createElement("div", {
609
856
  className: "luckysheet-cell-address-indicator",
610
857
  style: getAddressIndicatorPosition()
611
858
  }, wraperGetCell())), /*#__PURE__*/_react.default.createElement("div", {
612
859
  ref: inputBoxInnerRef,
613
860
  className: "luckysheet-input-box-inner",
614
- style: firstSelection ? __assign({
861
+ style: inputBoxBaseSelection ? __assign({
615
862
  position: "relative",
616
- minWidth: firstSelection.width,
617
- minHeight: firstSelection.height
863
+ minWidth: inputBoxBaseSelection.width,
864
+ minHeight: inputBoxBaseSelection.height
618
865
  }, inputBoxStyle) : {
619
866
  position: "relative"
620
867
  }
@@ -635,10 +882,23 @@ var InputBox = function InputBox() {
635
882
  },
636
883
  onMouseUp: function onMouseUp() {
637
884
  handleHideShowHint();
885
+ setContext(function (draftCtx) {
886
+ (0, _fortuneCore.setFormulaEditorOwner)(draftCtx, "cell");
887
+ });
638
888
  if (!isComposingRef.current) {
639
889
  var currentCommaCount = (0, _helper.countCommasBeforeCursor)(inputRef === null || inputRef === void 0 ? void 0 : inputRef.current);
640
890
  setCommaCount(currentCommaCount);
641
891
  }
892
+ var editor = inputRef.current;
893
+ if (!editor) return;
894
+ setContext(function (draftCtx) {
895
+ if (draftCtx.formulaCache.rangeSelectionActive !== true) return;
896
+ var clickedInsideManagedRange = (0, _fortuneCore.getFormulaRangeIndexAtCaret)(editor) !== null;
897
+ var atValidInsertionPoint = (0, _fortuneCore.isCaretAtValidFormulaRangeInsertionPoint)(editor);
898
+ if (clickedInsideManagedRange || !atValidInsertionPoint) {
899
+ (0, _fortuneCore.markRangeSelectionDirty)(draftCtx);
900
+ }
901
+ });
642
902
  },
643
903
  innerRef: function innerRef(e) {
644
904
  inputRef.current = e;
@@ -657,6 +917,7 @@ var InputBox = function InputBox() {
657
917
  onChange: onChange,
658
918
  onKeyDown: onKeyDown,
659
919
  onPaste: onPaste,
920
+ onCopy: onCopy,
660
921
  allowEdit: edit ? !isHidenRC : edit
661
922
  }), linkSelectionHighlightRects.length > 0 && (/*#__PURE__*/_react.default.createElement("div", {
662
923
  className: "luckysheet-input-box-link-selection-highlight",
@@ -681,7 +942,7 @@ var InputBox = function InputBox() {
681
942
  backgroundColor: "rgba(0, 123, 255, 0.25)"
682
943
  }
683
944
  });
684
- })))), (context.functionCandidates.length > 0 || context.functionHint || context.defaultCandidates.length > 0 || fn) && (/*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, showSearchHint && (/*#__PURE__*/_react.default.createElement(_FormulaSearch.default, {
945
+ })))), (context.functionCandidates.length > 0 || context.functionHint || context.defaultCandidates.length > 0 || fn) && showCellFormulaChrome && (/*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, showSearchHint && (/*#__PURE__*/_react.default.createElement(_FormulaSearch.default, {
685
946
  onMouseMove: function onMouseMove(e) {
686
947
  if (document.getElementById("luckysheet-formula-search-c")) {
687
948
  var hoveredItem = e.target.closest(".luckysheet-formula-search-item");
@@ -696,12 +957,12 @@ var InputBox = function InputBox() {
696
957
  }
697
958
  })), /*#__PURE__*/_react.default.createElement("div", {
698
959
  className: "cell-hint"
699
- }, showFormulaHint && fn && (/*#__PURE__*/_react.default.createElement(_FormulaHint.default, {
960
+ }, showFormulaHint && fn && !showSearchHint && (/*#__PURE__*/_react.default.createElement(_FormulaHint.default, {
700
961
  handleShowFormulaHint: handleShowFormulaHint,
701
962
  showFormulaHint: showFormulaHint,
702
963
  commaCount: commaCount,
703
964
  functionName: functionName
704
- })), !showFormulaHint && fn && (/*#__PURE__*/_react.default.createElement(_ui.Tooltip, {
965
+ })), !showFormulaHint && fn && !showSearchHint && (/*#__PURE__*/_react.default.createElement(_ui.Tooltip, {
705
966
  text: "Turn on formula suggestions (F10)",
706
967
  placement: "top",
707
968
  defaultOpen: true,