@fileverse-dev/fortune-react 1.3.10 → 1.3.11-input-ref-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 (29) hide show
  1. package/es/components/ContextMenu/index.js +1 -1
  2. package/es/components/FxEditor/index.js +200 -52
  3. package/es/components/SheetOverlay/InputBox.js +318 -150
  4. package/es/components/SheetOverlay/drag_and_drop/column-helpers.js +25 -1
  5. package/es/components/SheetOverlay/drag_and_drop/row-helpers.js +26 -2
  6. package/es/components/SheetOverlay/helper.d.ts +7 -0
  7. package/es/components/SheetOverlay/helper.js +95 -0
  8. package/es/components/SheetOverlay/index.css +1 -1
  9. package/es/components/Workbook/index.js +141 -11
  10. package/es/hooks/useFormulaEditorHistory.d.ts +24 -0
  11. package/es/hooks/useFormulaEditorHistory.js +119 -0
  12. package/es/hooks/useRerenderOnFormulaCaret.d.ts +2 -0
  13. package/es/hooks/useRerenderOnFormulaCaret.js +26 -0
  14. package/es/utils/convertCellsToCrypto.js +17 -0
  15. package/lib/components/ContextMenu/index.js +1 -1
  16. package/lib/components/FxEditor/index.js +198 -50
  17. package/lib/components/SheetOverlay/InputBox.js +316 -148
  18. package/lib/components/SheetOverlay/drag_and_drop/column-helpers.js +25 -1
  19. package/lib/components/SheetOverlay/drag_and_drop/row-helpers.js +26 -2
  20. package/lib/components/SheetOverlay/helper.d.ts +7 -0
  21. package/lib/components/SheetOverlay/helper.js +99 -0
  22. package/lib/components/SheetOverlay/index.css +1 -1
  23. package/lib/components/Workbook/index.js +142 -12
  24. package/lib/hooks/useFormulaEditorHistory.d.ts +24 -0
  25. package/lib/hooks/useFormulaEditorHistory.js +126 -0
  26. package/lib/hooks/useRerenderOnFormulaCaret.d.ts +2 -0
  27. package/lib/hooks/useRerenderOnFormulaCaret.js +32 -0
  28. package/lib/utils/convertCellsToCrypto.js +17 -0
  29. 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,70 @@ import ContentEditable from "./ContentEditable";
17
17
  import FormulaSearch from "./FormulaSearch";
18
18
  import FormulaHint from "./FormulaHint";
19
19
  import usePrevious from "../../hooks/usePrevious";
20
- import { moveCursorToEnd, getCursorPosition, isLetterNumberPattern, removeLastSpan, incrementColumn, decrementColumn, incrementRow, decrementRow, countCommasBeforeCursor } from "./helper";
20
+ import { useFormulaEditorHistory } from "../../hooks/useFormulaEditorHistory";
21
+ import { useRerenderOnFormulaCaret } from "../../hooks/useRerenderOnFormulaCaret";
22
+ import { moveCursorToEnd, getCursorPosition, setCursorPosition, buildFormulaSuggestionText, getFunctionNameFromFormulaCaretSpans, isLetterNumberPattern, countCommasBeforeCursor, shouldShowFormulaFunctionList } from "./helper";
21
23
  import { LucideIcon } from "./LucideIcon";
22
24
  var InputBox = function InputBox() {
23
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
24
- var _m = useContext(WorkbookContext),
25
- context = _m.context,
26
- setContext = _m.setContext,
27
- refs = _m.refs;
25
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p;
26
+ var _q = useContext(WorkbookContext),
27
+ context = _q.context,
28
+ setContext = _q.setContext,
29
+ refs = _q.refs;
28
30
  var inputRef = useRef(null);
29
31
  var lastKeyDownEventRef = useRef(null);
30
32
  var prevCellUpdate = usePrevious(context.luckysheetCellUpdate);
31
33
  var prevSheetId = usePrevious(context.currentSheetId);
32
- var _o = useState(false),
33
- isHidenRC = _o[0],
34
- setIsHidenRC = _o[1];
35
- var _p = useState(false),
36
- isInputBoxActive = _p[0],
37
- setIsInputBoxActive = _p[1];
38
- var _q = useState(""),
39
- activeCell = _q[0],
40
- setActiveCell = _q[1];
41
- var _r = useState(""),
42
- activeRefCell = _r[0],
43
- setActiveRefCell = _r[1];
44
- var _s = useState({
34
+ var _r = useState(false),
35
+ isHidenRC = _r[0],
36
+ setIsHidenRC = _r[1];
37
+ var _s = useState(false),
38
+ isInputBoxActive = _s[0],
39
+ setIsInputBoxActive = _s[1];
40
+ var _t = useState(""),
41
+ activeCell = _t[0],
42
+ setActiveCell = _t[1];
43
+ var _u = useState(""),
44
+ activeRefCell = _u[0],
45
+ setActiveRefCell = _u[1];
46
+ var _v = useState({
45
47
  left: 0,
46
48
  top: 0
47
49
  }),
48
- frozenPosition = _s[0],
49
- setFrozenPosition = _s[1];
50
+ frozenPosition = _v[0],
51
+ setFrozenPosition = _v[1];
50
52
  var firstSelection = (_a = context.luckysheet_select_save) === null || _a === void 0 ? void 0 : _a[0];
51
- var _t = useState({}),
52
- firstSelectionActiveCell = _t[0],
53
- setFirstSelectionActiveCell = _t[1];
54
- var _u = useState(0),
55
- commaCount = _u[0],
56
- setCommaCount = _u[1];
53
+ var _w = useState({}),
54
+ firstSelectionActiveCell = _w[0],
55
+ setFirstSelectionActiveCell = _w[1];
56
+ var _x = useState(0),
57
+ commaCount = _x[0],
58
+ setCommaCount = _x[1];
57
59
  var hideFormulaHintLocal = localStorage.getItem("formulaMore") === "true";
58
- var _v = useState(!hideFormulaHintLocal),
59
- showFormulaHint = _v[0],
60
- setShowFormulaHint = _v[1];
61
- var _w = useState(false),
62
- showSearchHint = _w[0],
63
- setShowSearchHint = _w[1];
60
+ var _y = useState(!hideFormulaHintLocal),
61
+ showFormulaHint = _y[0],
62
+ setShowFormulaHint = _y[1];
63
+ var _z = useState(false),
64
+ showSearchHint = _z[0],
65
+ setShowSearchHint = _z[1];
64
66
  var row_index = firstSelection === null || firstSelection === void 0 ? void 0 : firstSelection.row_focus;
65
67
  var col_index = firstSelection === null || firstSelection === void 0 ? void 0 : firstSelection.column_focus;
66
- var preText = useRef("");
67
- var placeRef = useRef("");
68
68
  var isComposingRef = useRef(false);
69
+ var formulaAnchorCellRef = useRef(null);
70
+ var skipNextAnchorSelectionSyncRef = useRef(false);
71
+ var lastHandledMouseDragSignatureRef = useRef("");
72
+ var _0 = useFormulaEditorHistory(inputRef, refs.cellInput, refs.fxInput, setContext, "cell"),
73
+ formulaHistoryRef = _0.formulaHistoryRef,
74
+ preTextRef = _0.preTextRef,
75
+ resetFormulaHistory = _0.resetFormulaHistory,
76
+ handleFormulaHistoryUndoRedo = _0.handleFormulaHistoryUndoRedo,
77
+ capturePreFormulaState = _0.capturePreFormulaState,
78
+ appendFormulaHistoryFromPrimaryEditor = _0.appendFormulaHistoryFromPrimaryEditor;
69
79
  var ZWSP = "\u200B";
70
80
  var inputBoxInnerRef = useRef(null);
71
- var _x = useState([]),
72
- linkSelectionHighlightRects = _x[0],
73
- setLinkSelectionHighlightRects = _x[1];
81
+ var _1 = useState([]),
82
+ linkSelectionHighlightRects = _1[0],
83
+ setLinkSelectionHighlightRects = _1[1];
74
84
  var ensureNotEmpty = function ensureNotEmpty() {
75
85
  var el = inputRef.current;
76
86
  if (!el) return;
@@ -165,8 +175,37 @@ var InputBox = function InputBox() {
165
175
  if (inputRef.current) {
166
176
  inputRef.current.innerHTML = "";
167
177
  }
178
+ resetFormulaHistory();
168
179
  }
169
- }, [context.luckysheetCellUpdate]);
180
+ }, [context.luckysheetCellUpdate, resetFormulaHistory]);
181
+ useEffect(function () {
182
+ var _a;
183
+ if (_.isEmpty(context.luckysheetCellUpdate) || !refs.cellInput.current) {
184
+ formulaAnchorCellRef.current = null;
185
+ lastHandledMouseDragSignatureRef.current = "";
186
+ return;
187
+ }
188
+ var inputText = ((_a = refs.cellInput.current.innerText) === null || _a === void 0 ? void 0 : _a.trim()) || "";
189
+ if (!inputText.startsWith("=")) {
190
+ formulaAnchorCellRef.current = null;
191
+ lastHandledMouseDragSignatureRef.current = "";
192
+ }
193
+ }, [context.luckysheetCellUpdate, refs.cellInput]);
194
+ useEffect(function () {
195
+ if (_.isEmpty(context.luckysheetCellUpdate) || _.isEmpty(prevCellUpdate) || _.isEqual(prevCellUpdate, context.luckysheetCellUpdate)) {
196
+ return;
197
+ }
198
+ setContext(function (ctx) {
199
+ ctx.formulaRangeHighlight = [];
200
+ ctx.formulaRangeSelect = undefined;
201
+ ctx.formulaCache.selectingRangeIndex = -1;
202
+ ctx.formulaCache.func_selectedrange = undefined;
203
+ ctx.formulaCache.rangestart = false;
204
+ ctx.formulaCache.rangedrag_column_start = false;
205
+ ctx.formulaCache.rangedrag_row_start = false;
206
+ ctx.formulaCache.rangechangeindex = undefined;
207
+ });
208
+ }, [context.luckysheetCellUpdate, prevCellUpdate, setContext]);
170
209
  useEffect(function () {
171
210
  setIsHidenRC(isShowHidenCR(context));
172
211
  }, [context.luckysheet_select_save]);
@@ -176,65 +215,45 @@ var InputBox = function InputBox() {
176
215
  setIsInputBoxActive(false);
177
216
  }
178
217
  }, [firstSelection, (_c = context.rangeDialog) === null || _c === void 0 ? void 0 : _c.show, context.luckysheetCellUpdate]);
218
+ useEffect(function () {
219
+ if (isInputBoxActive) return;
220
+ setContext(function (ctx) {
221
+ if (_.isEmpty(ctx.formulaRangeHighlight) && !ctx.formulaRangeSelect && ctx.formulaCache.selectingRangeIndex === -1 && !ctx.formulaCache.func_selectedrange) {
222
+ return;
223
+ }
224
+ ctx.formulaRangeHighlight = [];
225
+ ctx.formulaRangeSelect = undefined;
226
+ ctx.formulaCache.selectingRangeIndex = -1;
227
+ ctx.formulaCache.func_selectedrange = undefined;
228
+ ctx.formulaCache.rangestart = false;
229
+ ctx.formulaCache.rangedrag_column_start = false;
230
+ ctx.formulaCache.rangedrag_row_start = false;
231
+ ctx.formulaCache.rangechangeindex = undefined;
232
+ ctx.formulaCache.rangeSelectionActive = null;
233
+ });
234
+ }, [isInputBoxActive, setContext]);
179
235
  var getActiveFormula = useCallback(function () {
180
236
  return document.querySelector(".luckysheet-formula-search-item-active");
181
237
  }, []);
182
238
  var insertSelectedFormula = useCallback(function (formulaName) {
183
- var _a;
184
- if (/^=[a-zA-Z]+$/.test(inputRef.current.innerText)) {
185
- var ht = "<span dir=\"auto\" class=\"luckysheet-formula-text-color\">=</span><span dir=\"auto\" class=\"luckysheet-formula-text-func\">".concat(formulaName, "</span><span dir=\"auto\" class=\"luckysheet-formula-text-lpar\">(</span>");
186
- inputRef.current.innerHTML = ht;
187
- var fxEditor = document.getElementById("luckysheet-functionbox-cell");
188
- if (fxEditor) {
189
- fxEditor.innerHTML = ht;
190
- }
191
- moveCursorToEnd(inputRef.current);
192
- setContext(function (draftCtx) {
193
- draftCtx.functionCandidates = [];
194
- draftCtx.defaultCandidates = [];
195
- draftCtx.functionHint = formulaName;
196
- });
197
- return;
198
- }
199
- var textEditor = document.getElementById("luckysheet-rich-text-editor");
239
+ var textEditor = inputRef.current;
200
240
  if (!textEditor) return;
201
- textEditor.focus();
202
- var selection = window.getSelection();
203
- var range = (selection === null || selection === void 0 ? void 0 : selection.rangeCount) ? selection.getRangeAt(0) : null;
204
- if (!selection || !range || !textEditor.contains(range.startContainer)) {
205
- range = document.createRange();
206
- range.selectNodeContents(textEditor);
207
- range.collapse(false);
208
- selection = window.getSelection();
209
- selection === null || selection === void 0 ? void 0 : selection.removeAllRanges();
210
- selection === null || selection === void 0 ? void 0 : selection.addRange(range);
211
- }
212
- var searchTxt = ((_a = getrangeseleciton()) === null || _a === void 0 ? void 0 : _a.textContent) || "";
213
- var deleteCount = searchTxt === "=" ? 0 : searchTxt.length;
214
- if (deleteCount > 0 && range.startContainer.nodeType === Node.TEXT_NODE && textEditor.contains(range.startContainer)) {
215
- var startOffset = Math.max(range.startOffset - deleteCount, 0);
216
- var endOffset = range.startOffset;
217
- range.setStart(range.startContainer, startOffset);
218
- range.setEnd(range.startContainer, endOffset);
219
- range.deleteContents();
220
- }
221
- textEditor.querySelectorAll(".luckysheet-formula-text-func, .luckysheet-formula-text-lpar").forEach(function (el) {
222
- return el.remove();
223
- });
224
- var funcNode = new DOMParser().parseFromString("<span dir=\"auto\" class=\"luckysheet-formula-text-func\">".concat(formulaName, "</span>"), "text/html").body.firstChild;
225
- var parNode = new DOMParser().parseFromString("<span dir=\"auto\" class=\"luckysheet-formula-text-lpar\">(</span>", "text/html").body.firstChild;
226
- if (range && parNode && funcNode) {
227
- range.insertNode(funcNode);
228
- range.collapse(false);
229
- range.insertNode(parNode);
230
- range.collapse(false);
231
- selection === null || selection === void 0 ? void 0 : selection.removeAllRanges();
232
- selection === null || selection === void 0 ? void 0 : selection.addRange(range);
233
- }
241
+ var fxEditor = document.getElementById("luckysheet-functionbox-cell");
242
+ var _a = buildFormulaSuggestionText(textEditor, formulaName),
243
+ text = _a.text,
244
+ caretOffset = _a.caretOffset;
245
+ var safeText = escapeScriptTag(text);
246
+ var html = safeText.startsWith("=") ? functionHTMLGenerate(safeText) : escapeHTMLTag(safeText);
247
+ textEditor.innerHTML = html;
248
+ if (fxEditor) {
249
+ fxEditor.innerHTML = html;
250
+ }
251
+ setCursorPosition(textEditor, caretOffset);
252
+ setShowSearchHint(shouldShowFormulaFunctionList(textEditor));
234
253
  setContext(function (draftCtx) {
235
254
  draftCtx.functionCandidates = [];
236
255
  draftCtx.defaultCandidates = [];
237
- draftCtx.functionHint = formulaName;
256
+ draftCtx.functionHint = (formulaName || "").toUpperCase();
238
257
  });
239
258
  }, [setContext]);
240
259
  var clearSearchItemActiveClass = useCallback(function () {
@@ -257,7 +276,7 @@ var InputBox = function InputBox() {
257
276
  var _a, _b;
258
277
  if (isComposingRef.current || !inputRef.current) return;
259
278
  if (e.target.className.includes("sign-fortune")) return;
260
- preText.current = inputRef.current.innerText;
279
+ preTextRef.current = inputRef.current.innerText;
261
280
  var formulaName = (_b = (_a = getActiveFormula()) === null || _a === void 0 ? void 0 : _a.querySelector(".luckysheet-formula-search-func")) === null || _b === void 0 ? void 0 : _b.textContent;
262
281
  var lastSpanText = getLastInputSpanText();
263
282
  if (formulaName && !isLetterNumberPattern(lastSpanText)) {
@@ -270,11 +289,150 @@ var InputBox = function InputBox() {
270
289
  event.stopPropagation();
271
290
  event.preventDefault();
272
291
  };
292
+ useEffect(function () {
293
+ var _a;
294
+ var cellInputEl = refs.cellInput.current;
295
+ if (!((_a = context.luckysheet_select_save) === null || _a === void 0 ? void 0 : _a[0]) || !cellInputEl) return;
296
+ setContext(function (ctx) {
297
+ var _a;
298
+ var currentSelection = (_a = ctx.luckysheet_select_save) === null || _a === void 0 ? void 0 : _a[ctx.luckysheet_select_save.length - 1];
299
+ if (!currentSelection) return;
300
+ if (getFormulaEditorOwner(ctx) === "fx") {
301
+ return;
302
+ }
303
+ israngeseleciton(ctx);
304
+ var keyboardSyncRangeIndex = getFormulaRangeIndexForKeyboardSync(cellInputEl);
305
+ if (skipNextAnchorSelectionSyncRef.current && formulaAnchorCellRef.current) {
306
+ var _b = formulaAnchorCellRef.current,
307
+ anchorRow = _b[0],
308
+ anchorCol = _b[1];
309
+ var isAnchorSelection = currentSelection.row_focus === anchorRow && currentSelection.column_focus === anchorCol;
310
+ if (isAnchorSelection) {
311
+ skipNextAnchorSelectionSyncRef.current = false;
312
+ return;
313
+ }
314
+ }
315
+ var isFormulaMode = isFormulaReferenceInputMode(ctx);
316
+ if (!isFormulaMode) return;
317
+ if (keyboardSyncRangeIndex !== null) {
318
+ ctx.formulaCache.rangechangeindex = keyboardSyncRangeIndex;
319
+ ctx.formulaCache.rangestart = true;
320
+ ctx.formulaCache.rangedrag_column_start = false;
321
+ ctx.formulaCache.rangedrag_row_start = false;
322
+ } else {
323
+ ctx.formulaCache.rangechangeindex = undefined;
324
+ ctx.formulaCache.rangestart = false;
325
+ }
326
+ ctx.formulaCache.rangeSelectionActive = true;
327
+ rangeSetValue === null || rangeSetValue === void 0 ? void 0 : rangeSetValue(ctx, cellInputEl, {
328
+ row: currentSelection.row,
329
+ column: currentSelection.column
330
+ }, refs.fxInput.current);
331
+ rangeHightlightselected(ctx, cellInputEl);
332
+ if (!_.isNil(ctx.formulaCache.rangechangeindex)) {
333
+ ctx.formulaCache.selectingRangeIndex = ctx.formulaCache.rangechangeindex;
334
+ createRangeHightlight(ctx, cellInputEl.innerHTML, ctx.formulaCache.rangechangeindex);
335
+ var rectFromSelection = seletedHighlistByindex(ctx, currentSelection.row[0], currentSelection.row[1], currentSelection.column[0], currentSelection.column[1]);
336
+ if (rectFromSelection) {
337
+ createFormulaRangeSelect(ctx, {
338
+ rangeIndex: ctx.formulaCache.rangechangeindex || 0,
339
+ left: rectFromSelection.left,
340
+ top: rectFromSelection.top,
341
+ width: rectFromSelection.width,
342
+ height: rectFromSelection.height
343
+ });
344
+ }
345
+ }
346
+ });
347
+ }, [context.luckysheet_select_save, (_d = context.rangeDialog) === null || _d === void 0 ? void 0 : _d.show]);
348
+ var formulaMouseDragSignature = function () {
349
+ var _a, _b, _c, _d;
350
+ var r = context.formulaCache.func_selectedrange;
351
+ if (!r) return "";
352
+ 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(":");
353
+ }();
354
+ useEffect(function () {
355
+ var _a;
356
+ if (!formulaMouseDragSignature) return;
357
+ if (lastHandledMouseDragSignatureRef.current === formulaMouseDragSignature) {
358
+ return;
359
+ }
360
+ if (!refs.cellInput.current) return;
361
+ var inputText = ((_a = refs.cellInput.current.innerText) === null || _a === void 0 ? void 0 : _a.trim()) || "";
362
+ if (!inputText.startsWith("=")) return;
363
+ var dragRange = context.formulaCache.func_selectedrange;
364
+ if (!dragRange) return;
365
+ lastHandledMouseDragSignatureRef.current = formulaMouseDragSignature;
366
+ setContext(function (ctx) {
367
+ ctx.luckysheet_select_save = [{
368
+ row: [dragRange.row[0], dragRange.row[1]],
369
+ column: [dragRange.column[0], dragRange.column[1]],
370
+ row_focus: _.isNil(dragRange.row_focus) ? dragRange.row[0] : dragRange.row_focus,
371
+ column_focus: _.isNil(dragRange.column_focus) ? dragRange.column[0] : dragRange.column_focus
372
+ }];
373
+ });
374
+ }, [formulaMouseDragSignature, context.formulaCache.func_selectedrange, refs.cellInput, setContext]);
273
375
  var onKeyDown = useCallback(function (e) {
274
376
  var _a, _b;
377
+ setContext(function (draftCtx) {
378
+ setFormulaEditorOwner(draftCtx, "cell");
379
+ });
275
380
  lastKeyDownEventRef.current = new KeyboardEvent(e.type, e.nativeEvent);
276
- preText.current = inputRef.current.innerText;
277
- if (e.metaKey && context.luckysheetCellUpdate.length > 0) {
381
+ capturePreFormulaState();
382
+ var currentInputText = ((_b = (_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.innerText) === null || _b === void 0 ? void 0 : _b.trim()) || "";
383
+ if ((e.key === "=" || currentInputText.startsWith("=")) && context.luckysheetCellUpdate.length === 2 && formulaAnchorCellRef.current == null) {
384
+ setContext(function (draftCtx) {
385
+ draftCtx.formulaCache.rangeSelectionActive = null;
386
+ });
387
+ formulaAnchorCellRef.current = [context.luckysheetCellUpdate[0], context.luckysheetCellUpdate[1]];
388
+ }
389
+ if (e.key === "(" && currentInputText.startsWith("=")) {
390
+ setContext(function (draftCtx) {
391
+ draftCtx.formulaCache.rangeSelectionActive = null;
392
+ });
393
+ }
394
+ if (e.key === "," && context.luckysheetCellUpdate.length > 0 && currentInputText.startsWith("=") && formulaAnchorCellRef.current) {
395
+ setContext(function (draftCtx) {
396
+ draftCtx.formulaCache.rangeSelectionActive = null;
397
+ });
398
+ var _c = formulaAnchorCellRef.current,
399
+ anchorRow_1 = _c[0],
400
+ anchorCol_1 = _c[1];
401
+ skipNextAnchorSelectionSyncRef.current = true;
402
+ setTimeout(function () {
403
+ setContext(function (draftCtx) {
404
+ var _a, _b;
405
+ draftCtx.luckysheetCellUpdate = [anchorRow_1, anchorCol_1];
406
+ draftCtx.luckysheet_select_save = [{
407
+ row: [anchorRow_1, anchorRow_1],
408
+ column: [anchorCol_1, anchorCol_1],
409
+ row_focus: anchorRow_1,
410
+ column_focus: anchorCol_1
411
+ }];
412
+ draftCtx.formulaRangeSelect = undefined;
413
+ draftCtx.formulaCache.selectingRangeIndex = -1;
414
+ draftCtx.formulaCache.func_selectedrange = undefined;
415
+ draftCtx.formulaCache.rangechangeindex = undefined;
416
+ draftCtx.formulaCache.rangestart = false;
417
+ draftCtx.formulaCache.rangedrag_column_start = false;
418
+ draftCtx.formulaCache.rangedrag_row_start = false;
419
+ 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) || "");
420
+ moveHighlightCell(draftCtx, "down", 0, "rangeOfSelect");
421
+ });
422
+ }, 0);
423
+ }
424
+ if ((e.metaKey || e.ctrlKey) && context.luckysheetCellUpdate.length > 0) {
425
+ if (e.code === "KeyZ" || e.code === "KeyY") {
426
+ var shouldUseFormulaHistory = currentInputText.startsWith("=") || formulaHistoryRef.current.active;
427
+ if (shouldUseFormulaHistory) {
428
+ var handledByFormulaHistory = handleFormulaHistoryUndoRedo(e.code === "KeyY" || e.code === "KeyZ" && e.shiftKey);
429
+ if (handledByFormulaHistory) {
430
+ e.preventDefault();
431
+ }
432
+ }
433
+ e.stopPropagation();
434
+ return;
435
+ }
278
436
  if (e.code === "KeyB") {
279
437
  handleBold(context, inputRef.current);
280
438
  stopPropagation(e);
@@ -294,7 +452,25 @@ var InputBox = function InputBox() {
294
452
  setCommaCount(currentCommaCount);
295
453
  }
296
454
  var allowListNavigation = true;
455
+ var isArrowKey = e.key === "ArrowUp" || e.key === "ArrowDown" || e.key === "ArrowLeft" || e.key === "ArrowRight";
297
456
  if (e.key === "Delete" || e.key === "Backspace") {
457
+ var anchor = formulaAnchorCellRef.current;
458
+ if (anchor != null) {
459
+ var anchorRow_2 = anchor[0],
460
+ anchorCol_2 = anchor[1];
461
+ skipNextAnchorSelectionSyncRef.current = true;
462
+ setTimeout(function () {
463
+ setContext(function (draftCtx) {
464
+ draftCtx.luckysheetCellUpdate = [anchorRow_2, anchorCol_2];
465
+ draftCtx.luckysheet_select_save = [{
466
+ row: [anchorRow_2, anchorRow_2],
467
+ column: [anchorCol_2, anchorCol_2],
468
+ row_focus: anchorRow_2,
469
+ column_focus: anchorCol_2
470
+ }];
471
+ });
472
+ }, 0);
473
+ }
298
474
  if (isComposingRef.current) requestAnimationFrame(ensureNotEmpty);
299
475
  if (getCursorPosition(inputRef === null || inputRef === void 0 ? void 0 : inputRef.current) === (inputRef === null || inputRef === void 0 ? void 0 : inputRef.current.innerText.length)) {
300
476
  setTimeout(function () {
@@ -302,39 +478,8 @@ var InputBox = function InputBox() {
302
478
  }, 5);
303
479
  }
304
480
  }
305
- var refCell = placeRef.current;
306
- if (e.key === "ArrowUp") {
307
- refCell = decrementRow(placeRef.current);
308
- } else if (e.key === "ArrowDown") {
309
- refCell = incrementRow(placeRef.current);
310
- } else if (e.key === "ArrowLeft") {
311
- refCell = decrementColumn(placeRef.current);
312
- } else if (e.key === "ArrowRight") {
313
- refCell = incrementColumn(placeRef.current);
314
- }
315
- if ((e.key === "ArrowUp" || e.key === "ArrowDown" || e.key === "ArrowLeft" || e.key === "ArrowRight") && !(getCursorPosition(inputRef === null || inputRef === void 0 ? void 0 : inputRef.current) !== (inputRef === null || inputRef === void 0 ? void 0 : inputRef.current.innerText.length) && e.key === "ArrowRight")) {
316
- var parser = new DOMParser();
317
- var doc = parser.parseFromString("<div>".concat((_a = inputRef === null || inputRef === void 0 ? void 0 : inputRef.current) === null || _a === void 0 ? void 0 : _a.innerHTML, "</div>"), "text/html");
318
- var spans = doc.querySelectorAll("span");
319
- var lastSpan = spans[spans.length - 1];
320
- var notFunctionInit = !((_b = document.getElementById("luckysheet-rich-text-editor")) === null || _b === void 0 ? void 0 : _b.innerText.includes("("));
321
- var arrowRefNotAllowed = (lastSpan === null || lastSpan === void 0 ? void 0 : lastSpan.innerText.includes(")")) || notFunctionInit && /^[a-zA-Z]+$/.test(lastSpan === null || lastSpan === void 0 ? void 0 : lastSpan.innerText) && !_.includes(["="], lastSpan === null || lastSpan === void 0 ? void 0 : lastSpan.innerText);
322
- if (((lastSpan === null || lastSpan === void 0 ? void 0 : lastSpan.innerText) === "(" || (lastSpan === null || lastSpan === void 0 ? void 0 : lastSpan.innerText) === "," || (lastSpan === null || lastSpan === void 0 ? void 0 : lastSpan.innerText.includes(":")) || (lastSpan === null || lastSpan === void 0 ? void 0 : lastSpan.innerText) !== ")") && !(lastSpan === null || lastSpan === void 0 ? void 0 : lastSpan.innerText.includes('"')) && !isLetterNumberPattern(lastSpan === null || lastSpan === void 0 ? void 0 : lastSpan.innerText) && !arrowRefNotAllowed && !/^[a-zA-Z]+$/.test(lastSpan === null || lastSpan === void 0 ? void 0 : lastSpan.innerText)) {
323
- allowListNavigation = false;
324
- inputRef.current.innerHTML = "".concat(inputRef.current.innerHTML, "<span class=\"fortune-formula-functionrange-cell\" rangeindex=\"0\" dir=\"auto\" style=\"color:#c1232b;\">").concat(refCell, "</span>");
325
- setTimeout(function () {
326
- moveCursorToEnd(inputRef.current);
327
- }, 1);
328
- }
329
- if (isLetterNumberPattern(lastSpan === null || lastSpan === void 0 ? void 0 : lastSpan.innerText)) {
330
- allowListNavigation = false;
331
- var htmlR = removeLastSpan(inputRef === null || inputRef === void 0 ? void 0 : inputRef.current.innerHTML);
332
- inputRef.current.innerHTML = "".concat(htmlR, "<span class=\"fortune-formula-functionrange-cell\" rangeindex=\"0\" dir=\"auto\" style=\"color:#c1232b;\">").concat(refCell, "</span>");
333
- moveCursorToEnd(inputRef.current);
334
- setTimeout(function () {
335
- moveCursorToEnd(inputRef.current);
336
- }, 1);
337
- }
481
+ if (isArrowKey && isFormulaReferenceInputMode(context)) {
482
+ allowListNavigation = false;
338
483
  }
339
484
  if (e.key === "Escape" && context.luckysheetCellUpdate.length > 0) {
340
485
  setContext(function (draftCtx) {
@@ -390,7 +535,7 @@ var InputBox = function InputBox() {
390
535
  }
391
536
  e.preventDefault();
392
537
  }
393
- }, [clearSearchItemActiveClass, context.luckysheetCellUpdate.length, selectActiveFormula, setContext, firstSelection]);
538
+ }, [capturePreFormulaState, clearSearchItemActiveClass, context.luckysheetCellUpdate.length, handleFormulaHistoryUndoRedo, selectActiveFormula, setContext, firstSelection]);
394
539
  var handleHideShowHint = function handleHideShowHint() {
395
540
  var _a, _b, _c, _d;
396
541
  var searchElFx = (_a = document.getElementsByClassName("fx-search")) === null || _a === void 0 ? void 0 : _a[0];
@@ -411,32 +556,40 @@ var InputBox = function InputBox() {
411
556
  }
412
557
  };
413
558
  var onChange = useCallback(function (__, isBlur) {
414
- var _a;
559
+ var _a, _b, _c;
415
560
  if (context.isFlvReadOnly) return;
416
561
  handleHideShowHint();
417
- if (((_a = inputRef === null || inputRef === void 0 ? void 0 : inputRef.current) === null || _a === void 0 ? void 0 : _a.innerText.includes("=")) && /^=?[A-Za-z]*$/.test(getLastInputSpanText())) {
418
- setShowSearchHint(true);
419
- } else {
420
- setShowSearchHint(false);
421
- }
562
+ setShowSearchHint(shouldShowFormulaFunctionList((_a = inputRef === null || inputRef === void 0 ? void 0 : inputRef.current) !== null && _a !== void 0 ? _a : null));
422
563
  if (!isComposingRef.current) {
423
564
  var currentCommaCount = countCommasBeforeCursor(inputRef === null || inputRef === void 0 ? void 0 : inputRef.current);
424
565
  setCommaCount(currentCommaCount);
425
566
  }
426
567
  var e = lastKeyDownEventRef.current;
427
- if (!e) return;
568
+ if (!e) {
569
+ var cellEl_1 = refs.cellInput.current;
570
+ if (cellEl_1 && (((_b = cellEl_1.innerText) === null || _b === void 0 ? void 0 : _b.trim().startsWith("=")) || ((_c = cellEl_1.textContent) === null || _c === void 0 ? void 0 : _c.trim().startsWith("=")))) {
571
+ setContext(function (draftCtx) {
572
+ if (!isAllowEdit(draftCtx, draftCtx.luckysheet_select_save)) return;
573
+ rangeHightlightselected(draftCtx, cellEl_1);
574
+ });
575
+ }
576
+ return;
577
+ }
428
578
  var kcode = e.keyCode;
429
579
  if (!kcode) return;
580
+ appendFormulaHistoryFromPrimaryEditor(function () {
581
+ return getCursorPosition(inputRef.current);
582
+ });
430
583
  if (!(kcode >= 112 && kcode <= 123 || kcode <= 46 || kcode === 144 || kcode === 108 || e.ctrlKey || e.altKey || e.shiftKey && (kcode === 37 || kcode === 38 || kcode === 39 || kcode === 40)) || kcode === 8 || kcode === 32 || kcode === 46 || e.ctrlKey && kcode === 86) {
431
584
  setContext(function (draftCtx) {
432
585
  if ((draftCtx.formulaCache.rangestart || draftCtx.formulaCache.rangedrag_column_start || draftCtx.formulaCache.rangedrag_row_start || israngeseleciton(draftCtx)) && isBlur) return;
433
586
  if (!isAllowEdit(draftCtx, draftCtx.luckysheet_select_save)) {
434
587
  return;
435
588
  }
436
- handleFormulaInput(draftCtx, refs.fxInput.current, refs.cellInput.current, kcode, preText.current);
589
+ handleFormulaInput(draftCtx, refs.fxInput.current, refs.cellInput.current, kcode, preTextRef.current);
437
590
  });
438
591
  }
439
- }, [refs.cellInput, refs.fxInput, setContext]);
592
+ }, [refs.cellInput, refs.fxInput, setContext, appendFormulaHistoryFromPrimaryEditor]);
440
593
  var onPaste = useCallback(function (e) {
441
594
  if (_.isEmpty(context.luckysheetCellUpdate)) {
442
595
  e.preventDefault();
@@ -480,7 +633,7 @@ var InputBox = function InputBox() {
480
633
  zIndex: _.isEmpty(context.luckysheetCellUpdate) ? -1 : 19,
481
634
  display: "block"
482
635
  };
483
- }, [firstSelection, (_d = context.rangeDialog) === null || _d === void 0 ? void 0 : _d.show, context.luckysheetCellUpdate, refs.cellArea, isInputBoxActive, frozenPosition, context.scrollLeft, context.scrollTop]);
636
+ }, [firstSelection, (_e = context.rangeDialog) === null || _e === void 0 ? void 0 : _e.show, context.luckysheetCellUpdate, refs.cellArea, isInputBoxActive, frozenPosition, context.scrollLeft, context.scrollTop]);
484
637
  useEffect(function () {
485
638
  var _a;
486
639
  if (firstSelection && !((_a = context.rangeDialog) === null || _a === void 0 ? void 0 : _a.show) && !isInputBoxActive && !_.isEmpty(context.luckysheetCellUpdate)) {
@@ -496,7 +649,7 @@ var InputBox = function InputBox() {
496
649
  setIsInputBoxActive(true);
497
650
  }
498
651
  }
499
- }, [firstSelection, (_e = context.rangeDialog) === null || _e === void 0 ? void 0 : _e.show, context.luckysheetCellUpdate, isInputBoxActive, context.scrollLeft, context.scrollTop, refs.cellArea]);
652
+ }, [firstSelection, (_f = context.rangeDialog) === null || _f === void 0 ? void 0 : _f.show, context.luckysheetCellUpdate, isInputBoxActive, context.scrollLeft, context.scrollTop, refs.cellArea]);
500
653
  var getAddressIndicatorPosition = useCallback(function () {
501
654
  var _a;
502
655
  if (!firstSelection || ((_a = context.rangeDialog) === null || _a === void 0 ? void 0 : _a.show)) {
@@ -509,7 +662,7 @@ var InputBox = function InputBox() {
509
662
  left: "0",
510
663
  display: "block"
511
664
  };
512
- }, [firstSelection, (_f = context.rangeDialog) === null || _f === void 0 ? void 0 : _f.show]);
665
+ }, [firstSelection, (_g = context.rangeDialog) === null || _g === void 0 ? void 0 : _g.show]);
513
666
  var getCellAddress = useCallback(function () {
514
667
  if (!firstSelection) return "";
515
668
  var rowIndex = firstSelection.row_focus || 0;
@@ -559,15 +712,15 @@ var InputBox = function InputBox() {
559
712
  };
560
713
  });
561
714
  setLinkSelectionHighlightRects(relative);
562
- }, [(_g = context.linkCard) === null || _g === void 0 ? void 0 : _g.applyToSelection, (_h = context.linkCard) === null || _h === void 0 ? void 0 : _h.selectionOffsets, (_j = context.linkCard) === null || _j === void 0 ? void 0 : _j.r, (_k = context.linkCard) === null || _k === void 0 ? void 0 : _k.c, context.luckysheetCellUpdate]);
715
+ }, [(_h = context.linkCard) === null || _h === void 0 ? void 0 : _h.applyToSelection, (_j = context.linkCard) === null || _j === void 0 ? void 0 : _j.selectionOffsets, (_k = context.linkCard) === null || _k === void 0 ? void 0 : _k.r, (_l = context.linkCard) === null || _l === void 0 ? void 0 : _l.c, context.luckysheetCellUpdate]);
563
716
  var wraperGetCell = function wraperGetCell() {
564
717
  var cell = getCellAddress();
565
- placeRef.current = cell;
566
718
  if (activeRefCell !== cell) {
567
719
  setActiveRefCell(cell);
568
720
  }
569
721
  return activeCell || cell;
570
722
  };
723
+ useRerenderOnFormulaCaret(inputRef, context.luckysheetCellUpdate.length > 0);
571
724
  var getFunctionNameFromInput = useCallback(function () {
572
725
  var _a, _b;
573
726
  var inputText = ((_a = inputRef === null || inputRef === void 0 ? void 0 : inputRef.current) === null || _a === void 0 ? void 0 : _a.innerText) || "";
@@ -584,8 +737,10 @@ var InputBox = function InputBox() {
584
737
  }
585
738
  return null;
586
739
  }, []);
587
- var functionName = context.functionHint || getFunctionNameFromInput();
740
+ var functionName = (_o = (_m = getFunctionNameFromFormulaCaretSpans(inputRef.current)) !== null && _m !== void 0 ? _m : context.functionHint) !== null && _o !== void 0 ? _o : getFunctionNameFromInput();
588
741
  var fn = functionName ? context.formulaCache.functionlistMap[functionName] : null;
742
+ var showCellFormulaChrome = context.luckysheetCellUpdate.length > 0 && getFormulaEditorOwner(context) === "cell";
743
+ var inputBoxBaseSelection = isInputBoxActive && firstSelectionActiveCell ? firstSelectionActiveCell : firstSelection;
589
744
  return /*#__PURE__*/React.createElement("div", {
590
745
  className: "luckysheet-input-box",
591
746
  id: "luckysheet-input-box",
@@ -596,16 +751,16 @@ var InputBox = function InputBox() {
596
751
  onMouseUp: function onMouseUp(e) {
597
752
  return e.stopPropagation();
598
753
  }
599
- }, firstSelection && !((_l = context.rangeDialog) === null || _l === void 0 ? void 0 : _l.show) && (/*#__PURE__*/React.createElement("div", {
754
+ }, firstSelection && !((_p = context.rangeDialog) === null || _p === void 0 ? void 0 : _p.show) && (/*#__PURE__*/React.createElement("div", {
600
755
  className: "luckysheet-cell-address-indicator",
601
756
  style: getAddressIndicatorPosition()
602
757
  }, wraperGetCell())), /*#__PURE__*/React.createElement("div", {
603
758
  ref: inputBoxInnerRef,
604
759
  className: "luckysheet-input-box-inner",
605
- style: firstSelection ? __assign({
760
+ style: inputBoxBaseSelection ? __assign({
606
761
  position: "relative",
607
- minWidth: firstSelection.width,
608
- minHeight: firstSelection.height
762
+ minWidth: inputBoxBaseSelection.width,
763
+ minHeight: inputBoxBaseSelection.height
609
764
  }, inputBoxStyle) : {
610
765
  position: "relative"
611
766
  }
@@ -626,10 +781,23 @@ var InputBox = function InputBox() {
626
781
  },
627
782
  onMouseUp: function onMouseUp() {
628
783
  handleHideShowHint();
784
+ setContext(function (draftCtx) {
785
+ setFormulaEditorOwner(draftCtx, "cell");
786
+ });
629
787
  if (!isComposingRef.current) {
630
788
  var currentCommaCount = countCommasBeforeCursor(inputRef === null || inputRef === void 0 ? void 0 : inputRef.current);
631
789
  setCommaCount(currentCommaCount);
632
790
  }
791
+ var editor = inputRef.current;
792
+ if (!editor) return;
793
+ setContext(function (draftCtx) {
794
+ if (draftCtx.formulaCache.rangeSelectionActive !== true) return;
795
+ var clickedInsideManagedRange = getFormulaRangeIndexAtCaret(editor) !== null;
796
+ var atValidInsertionPoint = isCaretAtValidFormulaRangeInsertionPoint(editor);
797
+ if (clickedInsideManagedRange || !atValidInsertionPoint) {
798
+ markRangeSelectionDirty(draftCtx);
799
+ }
800
+ });
633
801
  },
634
802
  innerRef: function innerRef(e) {
635
803
  inputRef.current = e;
@@ -672,7 +840,7 @@ var InputBox = function InputBox() {
672
840
  backgroundColor: "rgba(0, 123, 255, 0.25)"
673
841
  }
674
842
  });
675
- })))), (context.functionCandidates.length > 0 || context.functionHint || context.defaultCandidates.length > 0 || fn) && (/*#__PURE__*/React.createElement(React.Fragment, null, showSearchHint && (/*#__PURE__*/React.createElement(FormulaSearch, {
843
+ })))), (context.functionCandidates.length > 0 || context.functionHint || context.defaultCandidates.length > 0 || fn) && showCellFormulaChrome && (/*#__PURE__*/React.createElement(React.Fragment, null, showSearchHint && (/*#__PURE__*/React.createElement(FormulaSearch, {
676
844
  onMouseMove: function onMouseMove(e) {
677
845
  if (document.getElementById("luckysheet-formula-search-c")) {
678
846
  var hoveredItem = e.target.closest(".luckysheet-formula-search-item");
@@ -687,12 +855,12 @@ var InputBox = function InputBox() {
687
855
  }
688
856
  })), /*#__PURE__*/React.createElement("div", {
689
857
  className: "cell-hint"
690
- }, showFormulaHint && fn && (/*#__PURE__*/React.createElement(FormulaHint, {
858
+ }, showFormulaHint && fn && !showSearchHint && (/*#__PURE__*/React.createElement(FormulaHint, {
691
859
  handleShowFormulaHint: handleShowFormulaHint,
692
860
  showFormulaHint: showFormulaHint,
693
861
  commaCount: commaCount,
694
862
  functionName: functionName
695
- })), !showFormulaHint && fn && (/*#__PURE__*/React.createElement(Tooltip, {
863
+ })), !showFormulaHint && fn && !showSearchHint && (/*#__PURE__*/React.createElement(Tooltip, {
696
864
  text: "Turn on formula suggestions (F10)",
697
865
  placement: "top",
698
866
  defaultOpen: true,