@fileverse-dev/fortune-react 1.3.11-input-ref → 1.3.11-input-ref-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 (31) hide show
  1. package/es/components/ContextMenu/index.js +1 -1
  2. package/es/components/FxEditor/index.js +296 -55
  3. package/es/components/SheetOverlay/FormulaHint/index.js +46 -25
  4. package/es/components/SheetOverlay/FormulaSearch/index.d.ts +3 -1
  5. package/es/components/SheetOverlay/FormulaSearch/index.js +58 -29
  6. package/es/components/SheetOverlay/InputBox.js +164 -69
  7. package/es/components/SheetOverlay/helper.d.ts +2 -0
  8. package/es/components/SheetOverlay/helper.js +43 -0
  9. package/es/components/SheetOverlay/index.css +4 -4
  10. package/es/components/SheetOverlay/index.js +9 -2
  11. package/es/components/Workbook/index.js +2 -2
  12. package/es/hooks/useFormulaEditorHistory.d.ts +24 -0
  13. package/es/hooks/useFormulaEditorHistory.js +119 -0
  14. package/es/hooks/useRerenderOnFormulaCaret.d.ts +2 -0
  15. package/es/hooks/useRerenderOnFormulaCaret.js +28 -0
  16. package/lib/components/ContextMenu/index.js +1 -1
  17. package/lib/components/FxEditor/index.js +294 -53
  18. package/lib/components/SheetOverlay/FormulaHint/index.js +45 -24
  19. package/lib/components/SheetOverlay/FormulaSearch/index.d.ts +3 -1
  20. package/lib/components/SheetOverlay/FormulaSearch/index.js +57 -28
  21. package/lib/components/SheetOverlay/InputBox.js +162 -67
  22. package/lib/components/SheetOverlay/helper.d.ts +2 -0
  23. package/lib/components/SheetOverlay/helper.js +45 -0
  24. package/lib/components/SheetOverlay/index.css +4 -4
  25. package/lib/components/SheetOverlay/index.js +8 -1
  26. package/lib/components/Workbook/index.js +2 -2
  27. package/lib/hooks/useFormulaEditorHistory.d.ts +24 -0
  28. package/lib/hooks/useFormulaEditorHistory.js +126 -0
  29. package/lib/hooks/useRerenderOnFormulaCaret.d.ts +2 -0
  30. package/lib/hooks/useRerenderOnFormulaCaret.js +34 -0
  31. package/package.json +2 -2
@@ -8,17 +8,27 @@ var __assign = this && this.__assign || function () {
8
8
  };
9
9
  return __assign.apply(this, arguments);
10
10
  };
11
+ var __rest = this && this.__rest || function (s, e) {
12
+ var t = {};
13
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
14
+ if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
15
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
16
+ }
17
+ return t;
18
+ };
11
19
  import _ from "lodash";
12
- import React, { useContext, useEffect, useRef, useState } from "react";
20
+ import React, { useCallback, useContext, useLayoutEffect, useRef, useState } from "react";
13
21
  import { LucideIcon, Tooltip } from "@fileverse/ui";
14
22
  import { UNFilter } from "./constant";
15
23
  import WorkbookContext from "../../../context";
16
24
  import "./index.css";
17
- var FormulaSearch = function FormulaSearch(props) {
18
- var _a;
19
- var _b = useContext(WorkbookContext),
20
- context = _b.context,
21
- isAuthorized = _b.settings.isAuthorized;
25
+ var FormulaSearch = function FormulaSearch(_a) {
26
+ var _b, _c, _d;
27
+ var from = _a.from,
28
+ divProps = __rest(_a, ["from"]);
29
+ var _e = useContext(WorkbookContext),
30
+ context = _e.context,
31
+ isAuthorized = _e.settings.isAuthorized;
22
32
  var authedFunction = ["COINGECKO", "ETHERSCAN", "DEFILLAMA", "GNOSIS", "BASE", "EOA", "PNL", "SAFE", "BLOCKSCOUT", "LENS", "FARCASTER", "Ethereum", "SMARTCONTRACT", "DUNESIM"];
23
33
  var filteredDefaultCandidates = context.defaultCandidates.filter(function (item) {
24
34
  return !authedFunction.includes(item.n);
@@ -30,39 +40,58 @@ var FormulaSearch = function FormulaSearch(props) {
30
40
  var finalFunctionCandidates = isAuthorized ? context.functionCandidates : context.functionCandidates.filter(function (item) {
31
41
  return item.t !== 20;
32
42
  });
33
- var firstSelection = (_a = context.luckysheet_select_save) === null || _a === void 0 ? void 0 : _a[0];
43
+ var firstSelection = (_b = context.luckysheet_select_save) === null || _b === void 0 ? void 0 : _b[0];
44
+ var cellHeightPx = (firstSelection === null || firstSelection === void 0 ? void 0 : firstSelection.height_move) != null ? Number(firstSelection.height_move) : 24;
45
+ var belowCellTop = cellHeightPx + 4;
34
46
  var hintRef = useRef(null);
35
- var _c = useState(0),
36
- top = _c[0],
37
- setTop = _c[1];
38
- var calcuatePopUpPlacement = function calcuatePopUpPlacement() {
39
- var _a, _b, _c, _d, _e, _f;
40
- if (!((_a = firstSelection === null || firstSelection === void 0 ? void 0 : firstSelection.top) === null || _a === void 0 ? void 0 : _a.toString()) || !((_b = firstSelection.height_move) === null || _b === void 0 ? void 0 : _b.toString()) || !hintRef.current) return;
41
- var hintHeight = 360;
42
- var inputBoxTop = parseInt(((_c = document.getElementById("luckysheet-input-box")) === null || _c === void 0 ? void 0 : _c.style.top) || "0", 10) - 85;
43
- var inputBottom = inputBoxTop + firstSelection.height_move;
44
- var availableBelow = window.innerHeight - inputBottom;
45
- var hintAbove = hintHeight > availableBelow;
46
- var selectionHeight = (firstSelection === null || firstSelection === void 0 ? void 0 : firstSelection.height_move) || 0;
47
- var divOffset = ((_d = hintRef.current) === null || _d === void 0 ? void 0 : _d.offsetHeight) || 0;
48
- var topV = hintAbove ? selectionHeight - (divOffset + 80) : selectionHeight + 4;
49
- var el = (_e = document.getElementsByClassName("fx-hint")) === null || _e === void 0 ? void 0 : _e[0];
50
- if (el && ((_f = el === null || el === void 0 ? void 0 : el.style) === null || _f === void 0 ? void 0 : _f.display) !== "none") {
47
+ var _f = useState(belowCellTop),
48
+ top = _f[0],
49
+ setTop = _f[1];
50
+ var applyPlacement = useCallback(function () {
51
+ var _a;
52
+ if ((firstSelection === null || firstSelection === void 0 ? void 0 : firstSelection.top) == null || firstSelection.height_move == null) {
53
+ setTop(belowCellTop);
54
+ return;
55
+ }
56
+ var fromFx = from === "fx";
57
+ var cellH = Number(firstSelection.height_move) || cellHeightPx;
58
+ if (fromFx) {
59
+ setTop(25);
60
+ return;
61
+ }
62
+ var innerEl = hintRef.current;
63
+ var popupHeight = Math.min((innerEl === null || innerEl === void 0 ? void 0 : innerEl.offsetHeight) || 360, 360);
64
+ var inputBox = document.getElementById("luckysheet-input-box");
65
+ var rect = inputBox === null || inputBox === void 0 ? void 0 : inputBox.getBoundingClientRect();
66
+ if (!rect) {
67
+ setTop(cellH + 4);
68
+ return;
69
+ }
70
+ var cellBottomViewport = rect.top + cellH;
71
+ var availableBelow = window.innerHeight - cellBottomViewport - 12;
72
+ var preferBelow = popupHeight <= availableBelow;
73
+ var topV = preferBelow ? cellH + 4 : -(popupHeight + 8);
74
+ var fxHint = (_a = document.getElementsByClassName("fx-hint")) === null || _a === void 0 ? void 0 : _a[0];
75
+ if (fxHint && fxHint.style.display !== "none") {
51
76
  topV = 25;
52
77
  }
53
78
  setTop(topV);
54
- };
55
- useEffect(function () {
56
- calcuatePopUpPlacement();
57
- });
79
+ }, [belowCellTop, cellHeightPx, firstSelection === null || firstSelection === void 0 ? void 0 : firstSelection.height_move, firstSelection === null || firstSelection === void 0 ? void 0 : firstSelection.top, from]);
80
+ useLayoutEffect(function () {
81
+ applyPlacement();
82
+ var id = requestAnimationFrame(applyPlacement);
83
+ return function () {
84
+ cancelAnimationFrame(id);
85
+ };
86
+ }, [applyPlacement, (_c = context.defaultCandidates) === null || _c === void 0 ? void 0 : _c.length, (_d = context.functionCandidates) === null || _d === void 0 ? void 0 : _d.length, context.functionHint]);
58
87
  if (_.isEmpty(context.functionCandidates) && _.isEmpty(context.defaultCandidates)) return null;
59
88
  return /*#__PURE__*/React.createElement("div", {
60
- className: "flex color-border-default border flex-col luckysheet-formula-search-c-p custom-scroll ".concat((props === null || props === void 0 ? void 0 : props.from) === "fx" ? "fx-search" : "cell-search"),
89
+ className: "flex color-border-default border flex-col luckysheet-formula-search-c-p custom-scroll ".concat(from === "fx" ? "fx-search" : "cell-search"),
61
90
  id: "luckysheet-formula-search-c-p",
62
91
  style: {
63
92
  top: top
64
93
  }
65
- }, /*#__PURE__*/React.createElement("div", __assign({}, props, {
94
+ }, /*#__PURE__*/React.createElement("div", __assign({}, divProps, {
66
95
  ref: hintRef,
67
96
  id: "luckysheet-formula-search-c",
68
97
  className: "luckysheet-formula-search-c"
@@ -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, indexToColumnChar, functionHTMLGenerate, handleBold, handleItalic, handleUnderline, handleStrikeThrough, getRangeRectsByCharacterOffset, rangeSetValue, getFormulaRangeIndexForKeyboardSync, createFormulaRangeSelect, seletedHighlistByindex, isFormulaReferenceInputMode, rangeHightlightselected } 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,62 +17,73 @@ 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, setCursorPosition, buildFormulaSuggestionText, isLetterNumberPattern, 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, _m;
24
- var _o = useContext(WorkbookContext),
25
- context = _o.context,
26
- setContext = _o.setContext,
27
- refs = _o.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 _p = useState(false),
33
- isHidenRC = _p[0],
34
- setIsHidenRC = _p[1];
35
- var _q = useState(false),
36
- isInputBoxActive = _q[0],
37
- setIsInputBoxActive = _q[1];
38
- var _r = useState(""),
39
- activeCell = _r[0],
40
- setActiveCell = _r[1];
41
- var _s = useState(""),
42
- activeRefCell = _s[0],
43
- setActiveRefCell = _s[1];
44
- var _t = 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 = _t[0],
49
- setFrozenPosition = _t[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 _u = useState({}),
52
- firstSelectionActiveCell = _u[0],
53
- setFirstSelectionActiveCell = _u[1];
54
- var _v = useState(0),
55
- commaCount = _v[0],
56
- setCommaCount = _v[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];
59
+ var _y = useState(false),
60
+ cellEditorIsFormula = _y[0],
61
+ setCellEditorIsFormula = _y[1];
57
62
  var hideFormulaHintLocal = localStorage.getItem("formulaMore") === "true";
58
- var _w = useState(!hideFormulaHintLocal),
59
- showFormulaHint = _w[0],
60
- setShowFormulaHint = _w[1];
61
- var _x = useState(false),
62
- showSearchHint = _x[0],
63
- setShowSearchHint = _x[1];
63
+ var _z = useState(!hideFormulaHintLocal),
64
+ showFormulaHint = _z[0],
65
+ setShowFormulaHint = _z[1];
66
+ var _0 = useState(false),
67
+ showSearchHint = _0[0],
68
+ setShowSearchHint = _0[1];
64
69
  var row_index = firstSelection === null || firstSelection === void 0 ? void 0 : firstSelection.row_focus;
65
70
  var col_index = firstSelection === null || firstSelection === void 0 ? void 0 : firstSelection.column_focus;
66
- var preText = useRef("");
67
71
  var isComposingRef = useRef(false);
68
72
  var formulaAnchorCellRef = useRef(null);
69
73
  var skipNextAnchorSelectionSyncRef = useRef(false);
70
74
  var lastHandledMouseDragSignatureRef = useRef("");
75
+ var _1 = useFormulaEditorHistory(inputRef, refs.cellInput, refs.fxInput, setContext, "cell"),
76
+ formulaHistoryRef = _1.formulaHistoryRef,
77
+ preTextRef = _1.preTextRef,
78
+ resetFormulaHistory = _1.resetFormulaHistory,
79
+ handleFormulaHistoryUndoRedo = _1.handleFormulaHistoryUndoRedo,
80
+ capturePreFormulaState = _1.capturePreFormulaState,
81
+ appendFormulaHistoryFromPrimaryEditor = _1.appendFormulaHistoryFromPrimaryEditor;
71
82
  var ZWSP = "\u200B";
72
83
  var inputBoxInnerRef = useRef(null);
73
- var _y = useState([]),
74
- linkSelectionHighlightRects = _y[0],
75
- setLinkSelectionHighlightRects = _y[1];
84
+ var _2 = useState([]),
85
+ linkSelectionHighlightRects = _2[0],
86
+ setLinkSelectionHighlightRects = _2[1];
76
87
  var ensureNotEmpty = function ensureNotEmpty() {
77
88
  var el = inputRef.current;
78
89
  if (!el) return;
@@ -95,12 +106,11 @@ var InputBox = function InputBox() {
95
106
  return lastSpan === null || lastSpan === void 0 ? void 0 : lastSpan.innerText;
96
107
  };
97
108
  var inputBoxStyle = useMemo(function () {
98
- var _a;
99
109
  if (firstSelectionActiveCell && context.luckysheetCellUpdate.length > 0) {
100
110
  var flowdata = getFlowdata(context);
101
111
  if (!flowdata) return {};
102
112
  var style = getStyleByCell(context, flowdata, firstSelectionActiveCell.row_focus, firstSelectionActiveCell.column_focus);
103
- if (((_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.innerText.charAt(0)) === "=") {
113
+ if (cellEditorIsFormula) {
104
114
  style = __assign(__assign({}, style), {
105
115
  textAlign: "left"
106
116
  });
@@ -108,7 +118,7 @@ var InputBox = function InputBox() {
108
118
  return style;
109
119
  }
110
120
  return {};
111
- }, [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]);
121
+ }, [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]);
112
122
  useLayoutEffect(function () {
113
123
  var _a;
114
124
  if (!context.allowEdit) {
@@ -128,10 +138,23 @@ var InputBox = function InputBox() {
128
138
  if (_.isEqual(prevCellUpdate, context.luckysheetCellUpdate) && prevSheetId === context.currentSheetId) {
129
139
  return;
130
140
  }
141
+ var _b = context.luckysheetCellUpdate,
142
+ ur = _b[0],
143
+ uc = _b[1];
144
+ var pending_1 = refs.globalCache.pendingTypeOverCell;
145
+ if (pending_1 && pending_1[0] === ur && pending_1[1] === uc) {
146
+ refs.globalCache.overwriteCell = false;
147
+ if (inputRef.current) {
148
+ setCellEditorIsFormula(inputRef.current.innerText.trim().startsWith("="));
149
+ }
150
+ delete refs.globalCache.doNotFocus;
151
+ return;
152
+ }
131
153
  var flowdata = getFlowdata(context);
132
154
  var cell = (_a = flowdata === null || flowdata === void 0 ? void 0 : flowdata[row_index]) === null || _a === void 0 ? void 0 : _a[col_index];
155
+ var overwrite = refs.globalCache.overwriteCell;
133
156
  var value_1 = "";
134
- if (cell && !refs.globalCache.overwriteCell) {
157
+ if (cell && !overwrite) {
135
158
  if (isInlineStringCell(cell)) {
136
159
  value_1 = getInlineStringHTML(row_index, col_index, flowdata);
137
160
  } else if (cell.f) {
@@ -147,14 +170,20 @@ var InputBox = function InputBox() {
147
170
  }
148
171
  }
149
172
  refs.globalCache.overwriteCell = false;
173
+ var wroteEditorFromStoredCell = false;
150
174
  if (!refs.globalCache.ignoreWriteCell && inputRef.current && value_1) {
151
175
  inputRef.current.innerHTML = escapeHTMLTag(escapeScriptTag(value_1));
152
- } else if (!refs.globalCache.ignoreWriteCell && inputRef.current && !value_1) {
176
+ wroteEditorFromStoredCell = true;
177
+ } else if (!refs.globalCache.ignoreWriteCell && inputRef.current && !value_1 && !overwrite) {
153
178
  var valueD = getCellValue(row_index, col_index, flowdata, "f");
154
179
  inputRef.current.innerText = valueD;
180
+ wroteEditorFromStoredCell = true;
155
181
  }
156
182
  refs.globalCache.ignoreWriteCell = false;
157
- if (!refs.globalCache.doNotFocus) {
183
+ if (inputRef.current) {
184
+ setCellEditorIsFormula(inputRef.current.innerText.trim().startsWith("="));
185
+ }
186
+ if (wroteEditorFromStoredCell && !refs.globalCache.doNotFocus) {
158
187
  setTimeout(function () {
159
188
  moveToEnd(inputRef.current);
160
189
  });
@@ -167,8 +196,15 @@ var InputBox = function InputBox() {
167
196
  if (inputRef.current) {
168
197
  inputRef.current.innerHTML = "";
169
198
  }
199
+ delete refs.globalCache.pendingTypeOverCell;
200
+ setCellEditorIsFormula(false);
201
+ resetFormulaHistory();
170
202
  }
171
- }, [context.luckysheetCellUpdate]);
203
+ }, [context.luckysheetCellUpdate, resetFormulaHistory, refs.globalCache]);
204
+ useEffect(function () {
205
+ if (_.isEmpty(context.luckysheetCellUpdate)) return;
206
+ delete refs.globalCache.pendingTypeOverCell;
207
+ }, [context.luckysheetCellUpdate, refs.globalCache]);
172
208
  useEffect(function () {
173
209
  var _a;
174
210
  if (_.isEmpty(context.luckysheetCellUpdate) || !refs.cellInput.current) {
@@ -240,10 +276,11 @@ var InputBox = function InputBox() {
240
276
  fxEditor.innerHTML = html;
241
277
  }
242
278
  setCursorPosition(textEditor, caretOffset);
279
+ setShowSearchHint(shouldShowFormulaFunctionList(textEditor));
243
280
  setContext(function (draftCtx) {
244
281
  draftCtx.functionCandidates = [];
245
282
  draftCtx.defaultCandidates = [];
246
- draftCtx.functionHint = formulaName;
283
+ draftCtx.functionHint = (formulaName || "").toUpperCase();
247
284
  });
248
285
  }, [setContext]);
249
286
  var clearSearchItemActiveClass = useCallback(function () {
@@ -266,7 +303,7 @@ var InputBox = function InputBox() {
266
303
  var _a, _b;
267
304
  if (isComposingRef.current || !inputRef.current) return;
268
305
  if (e.target.className.includes("sign-fortune")) return;
269
- preText.current = inputRef.current.innerText;
306
+ preTextRef.current = inputRef.current.innerText;
270
307
  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
308
  var lastSpanText = getLastInputSpanText();
272
309
  if (formulaName && !isLetterNumberPattern(lastSpanText)) {
@@ -287,8 +324,7 @@ var InputBox = function InputBox() {
287
324
  var _a;
288
325
  var currentSelection = (_a = ctx.luckysheet_select_save) === null || _a === void 0 ? void 0 : _a[ctx.luckysheet_select_save.length - 1];
289
326
  if (!currentSelection) return;
290
- var isMouseFormulaRangeDrag = !!ctx.formulaCache.func_selectedrange && (ctx.formulaCache.rangestart || ctx.formulaCache.rangedrag_column_start || ctx.formulaCache.rangedrag_row_start);
291
- if (isMouseFormulaRangeDrag) {
327
+ if (getFormulaEditorOwner(ctx) === "fx") {
292
328
  return;
293
329
  }
294
330
  israngeseleciton(ctx);
@@ -365,8 +401,11 @@ var InputBox = function InputBox() {
365
401
  }, [formulaMouseDragSignature, context.formulaCache.func_selectedrange, refs.cellInput, setContext]);
366
402
  var onKeyDown = useCallback(function (e) {
367
403
  var _a, _b;
404
+ setContext(function (draftCtx) {
405
+ setFormulaEditorOwner(draftCtx, "cell");
406
+ });
368
407
  lastKeyDownEventRef.current = new KeyboardEvent(e.type, e.nativeEvent);
369
- preText.current = inputRef.current.innerText;
408
+ capturePreFormulaState();
370
409
  var currentInputText = ((_b = (_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.innerText) === null || _b === void 0 ? void 0 : _b.trim()) || "";
371
410
  if ((e.key === "=" || currentInputText.startsWith("=")) && context.luckysheetCellUpdate.length === 2 && formulaAnchorCellRef.current == null) {
372
411
  setContext(function (draftCtx) {
@@ -389,6 +428,7 @@ var InputBox = function InputBox() {
389
428
  skipNextAnchorSelectionSyncRef.current = true;
390
429
  setTimeout(function () {
391
430
  setContext(function (draftCtx) {
431
+ var _a, _b;
392
432
  draftCtx.luckysheetCellUpdate = [anchorRow_1, anchorCol_1];
393
433
  draftCtx.luckysheet_select_save = [{
394
434
  row: [anchorRow_1, anchorRow_1],
@@ -396,7 +436,6 @@ var InputBox = function InputBox() {
396
436
  row_focus: anchorRow_1,
397
437
  column_focus: anchorCol_1
398
438
  }];
399
- draftCtx.formulaRangeHighlight = [];
400
439
  draftCtx.formulaRangeSelect = undefined;
401
440
  draftCtx.formulaCache.selectingRangeIndex = -1;
402
441
  draftCtx.formulaCache.func_selectedrange = undefined;
@@ -404,11 +443,23 @@ var InputBox = function InputBox() {
404
443
  draftCtx.formulaCache.rangestart = false;
405
444
  draftCtx.formulaCache.rangedrag_column_start = false;
406
445
  draftCtx.formulaCache.rangedrag_row_start = false;
446
+ 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) || "");
407
447
  moveHighlightCell(draftCtx, "down", 0, "rangeOfSelect");
408
448
  });
409
449
  }, 0);
410
450
  }
411
- if (e.metaKey && context.luckysheetCellUpdate.length > 0) {
451
+ if ((e.metaKey || e.ctrlKey) && context.luckysheetCellUpdate.length > 0) {
452
+ if (e.code === "KeyZ" || e.code === "KeyY") {
453
+ var shouldUseFormulaHistory = currentInputText.startsWith("=") || formulaHistoryRef.current.active;
454
+ if (shouldUseFormulaHistory) {
455
+ var handledByFormulaHistory = handleFormulaHistoryUndoRedo(e.code === "KeyY" || e.code === "KeyZ" && e.shiftKey);
456
+ if (handledByFormulaHistory) {
457
+ e.preventDefault();
458
+ }
459
+ }
460
+ e.stopPropagation();
461
+ return;
462
+ }
412
463
  if (e.code === "KeyB") {
413
464
  handleBold(context, inputRef.current);
414
465
  stopPropagation(e);
@@ -430,6 +481,29 @@ var InputBox = function InputBox() {
430
481
  var allowListNavigation = true;
431
482
  var isArrowKey = e.key === "ArrowUp" || e.key === "ArrowDown" || e.key === "ArrowLeft" || e.key === "ArrowRight";
432
483
  if (e.key === "Delete" || e.key === "Backspace") {
484
+ var anchor = formulaAnchorCellRef.current;
485
+ if (anchor != null) {
486
+ var anchorRow_2 = anchor[0],
487
+ anchorCol_2 = anchor[1];
488
+ skipNextAnchorSelectionSyncRef.current = true;
489
+ setTimeout(function () {
490
+ setContext(function (draftCtx) {
491
+ draftCtx.luckysheetCellUpdate = [anchorRow_2, anchorCol_2];
492
+ draftCtx.luckysheet_select_save = [{
493
+ row: [anchorRow_2, anchorRow_2],
494
+ column: [anchorCol_2, anchorCol_2],
495
+ row_focus: anchorRow_2,
496
+ column_focus: anchorCol_2
497
+ }];
498
+ markRangeSelectionDirty(draftCtx);
499
+ var el = refs.cellInput.current;
500
+ if (el && el.innerText.trim().startsWith("=")) {
501
+ createRangeHightlight(draftCtx, el.innerHTML);
502
+ rangeHightlightselected(draftCtx, el);
503
+ }
504
+ });
505
+ }, 0);
506
+ }
433
507
  if (isComposingRef.current) requestAnimationFrame(ensureNotEmpty);
434
508
  if (getCursorPosition(inputRef === null || inputRef === void 0 ? void 0 : inputRef.current) === (inputRef === null || inputRef === void 0 ? void 0 : inputRef.current.innerText.length)) {
435
509
  setTimeout(function () {
@@ -457,7 +531,7 @@ var InputBox = function InputBox() {
457
531
  e.preventDefault();
458
532
  } else if (e.key === "F4" && context.luckysheetCellUpdate.length > 0) {
459
533
  e.preventDefault();
460
- } else if (e.key === "ArrowUp" && context.luckysheetCellUpdate.length > 0 && allowListNavigation) {
534
+ } else if (!(e.metaKey || e.ctrlKey) && e.key === "ArrowUp" && context.luckysheetCellUpdate.length > 0 && allowListNavigation) {
461
535
  if (document.getElementById("luckysheet-formula-search-c")) {
462
536
  var formulaSearchContainer = document.getElementById("luckysheet-formula-search-c");
463
537
  var activeItem = formulaSearchContainer === null || formulaSearchContainer === void 0 ? void 0 : formulaSearchContainer.querySelector(".luckysheet-formula-search-item-active");
@@ -476,7 +550,7 @@ var InputBox = function InputBox() {
476
550
  }
477
551
  }
478
552
  e.preventDefault();
479
- } else if (e.key === "ArrowDown" && context.luckysheetCellUpdate.length > 0 && allowListNavigation) {
553
+ } else if (!(e.metaKey || e.ctrlKey) && e.key === "ArrowDown" && context.luckysheetCellUpdate.length > 0 && allowListNavigation) {
480
554
  if (document.getElementById("luckysheet-formula-search-c")) {
481
555
  var formulaSearchContainer = document.getElementById("luckysheet-formula-search-c");
482
556
  var activeItem = formulaSearchContainer === null || formulaSearchContainer === void 0 ? void 0 : formulaSearchContainer.querySelector(".luckysheet-formula-search-item-active");
@@ -494,7 +568,7 @@ var InputBox = function InputBox() {
494
568
  }
495
569
  e.preventDefault();
496
570
  }
497
- }, [clearSearchItemActiveClass, context.luckysheetCellUpdate.length, selectActiveFormula, setContext, firstSelection]);
571
+ }, [capturePreFormulaState, clearSearchItemActiveClass, context.luckysheetCellUpdate.length, handleFormulaHistoryUndoRedo, selectActiveFormula, setContext, firstSelection, refs.cellInput]);
498
572
  var handleHideShowHint = function handleHideShowHint() {
499
573
  var _a, _b, _c, _d;
500
574
  var searchElFx = (_a = document.getElementsByClassName("fx-search")) === null || _a === void 0 ? void 0 : _a[0];
@@ -515,14 +589,12 @@ var InputBox = function InputBox() {
515
589
  }
516
590
  };
517
591
  var onChange = useCallback(function (__, isBlur) {
518
- var _a, _b, _c;
592
+ var _a, _b, _c, _d, _e, _f;
519
593
  if (context.isFlvReadOnly) return;
520
594
  handleHideShowHint();
521
- 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())) {
522
- setShowSearchHint(true);
523
- } else {
524
- setShowSearchHint(false);
525
- }
595
+ 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 : "";
596
+ setCellEditorIsFormula(editorText.startsWith("="));
597
+ setShowSearchHint(shouldShowFormulaFunctionList((_d = inputRef === null || inputRef === void 0 ? void 0 : inputRef.current) !== null && _d !== void 0 ? _d : null));
526
598
  if (!isComposingRef.current) {
527
599
  var currentCommaCount = countCommasBeforeCursor(inputRef === null || inputRef === void 0 ? void 0 : inputRef.current);
528
600
  setCommaCount(currentCommaCount);
@@ -530,7 +602,7 @@ var InputBox = function InputBox() {
530
602
  var e = lastKeyDownEventRef.current;
531
603
  if (!e) {
532
604
  var cellEl_1 = refs.cellInput.current;
533
- 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("=")))) {
605
+ 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("=")))) {
534
606
  setContext(function (draftCtx) {
535
607
  if (!isAllowEdit(draftCtx, draftCtx.luckysheet_select_save)) return;
536
608
  rangeHightlightselected(draftCtx, cellEl_1);
@@ -540,16 +612,24 @@ var InputBox = function InputBox() {
540
612
  }
541
613
  var kcode = e.keyCode;
542
614
  if (!kcode) return;
615
+ appendFormulaHistoryFromPrimaryEditor(function () {
616
+ return getCursorPosition(inputRef.current);
617
+ });
543
618
  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) {
544
619
  setContext(function (draftCtx) {
620
+ var _a, _b;
545
621
  if ((draftCtx.formulaCache.rangestart || draftCtx.formulaCache.rangedrag_column_start || draftCtx.formulaCache.rangedrag_row_start || israngeseleciton(draftCtx)) && isBlur) return;
546
622
  if (!isAllowEdit(draftCtx, draftCtx.luckysheet_select_save)) {
547
623
  return;
548
624
  }
549
- handleFormulaInput(draftCtx, refs.fxInput.current, refs.cellInput.current, kcode, preText.current);
625
+ handleFormulaInput(draftCtx, refs.fxInput.current, refs.cellInput.current, kcode, preTextRef.current);
626
+ var cellEl = refs.cellInput.current;
627
+ 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("=")))) {
628
+ rangeHightlightselected(draftCtx, cellEl);
629
+ }
550
630
  });
551
631
  }
552
- }, [refs.cellInput, refs.fxInput, setContext]);
632
+ }, [refs.cellInput, refs.fxInput, setContext, appendFormulaHistoryFromPrimaryEditor]);
553
633
  var onPaste = useCallback(function (e) {
554
634
  if (_.isEmpty(context.luckysheetCellUpdate)) {
555
635
  e.preventDefault();
@@ -680,6 +760,7 @@ var InputBox = function InputBox() {
680
760
  }
681
761
  return activeCell || cell;
682
762
  };
763
+ useRerenderOnFormulaCaret(inputRef, context.luckysheetCellUpdate.length > 0);
683
764
  var getFunctionNameFromInput = useCallback(function () {
684
765
  var _a, _b;
685
766
  var inputText = ((_a = inputRef === null || inputRef === void 0 ? void 0 : inputRef.current) === null || _a === void 0 ? void 0 : _a.innerText) || "";
@@ -696,8 +777,9 @@ var InputBox = function InputBox() {
696
777
  }
697
778
  return null;
698
779
  }, []);
699
- var functionName = context.functionHint || getFunctionNameFromInput();
780
+ var functionName = (_o = (_m = getFunctionNameFromFormulaCaretSpans(inputRef.current)) !== null && _m !== void 0 ? _m : context.functionHint) !== null && _o !== void 0 ? _o : getFunctionNameFromInput();
700
781
  var fn = functionName ? context.formulaCache.functionlistMap[functionName] : null;
782
+ var showCellFormulaChrome = context.luckysheetCellUpdate.length > 0 && getFormulaEditorOwner(context) === "cell";
701
783
  var inputBoxBaseSelection = isInputBoxActive && firstSelectionActiveCell ? firstSelectionActiveCell : firstSelection;
702
784
  return /*#__PURE__*/React.createElement("div", {
703
785
  className: "luckysheet-input-box",
@@ -709,7 +791,7 @@ var InputBox = function InputBox() {
709
791
  onMouseUp: function onMouseUp(e) {
710
792
  return e.stopPropagation();
711
793
  }
712
- }, firstSelection && !((_m = context.rangeDialog) === null || _m === void 0 ? void 0 : _m.show) && (/*#__PURE__*/React.createElement("div", {
794
+ }, firstSelection && !((_p = context.rangeDialog) === null || _p === void 0 ? void 0 : _p.show) && (/*#__PURE__*/React.createElement("div", {
713
795
  className: "luckysheet-cell-address-indicator",
714
796
  style: getAddressIndicatorPosition()
715
797
  }, wraperGetCell())), /*#__PURE__*/React.createElement("div", {
@@ -739,10 +821,23 @@ var InputBox = function InputBox() {
739
821
  },
740
822
  onMouseUp: function onMouseUp() {
741
823
  handleHideShowHint();
824
+ setContext(function (draftCtx) {
825
+ setFormulaEditorOwner(draftCtx, "cell");
826
+ });
742
827
  if (!isComposingRef.current) {
743
828
  var currentCommaCount = countCommasBeforeCursor(inputRef === null || inputRef === void 0 ? void 0 : inputRef.current);
744
829
  setCommaCount(currentCommaCount);
745
830
  }
831
+ var editor = inputRef.current;
832
+ if (!editor) return;
833
+ setContext(function (draftCtx) {
834
+ if (draftCtx.formulaCache.rangeSelectionActive !== true) return;
835
+ var clickedInsideManagedRange = getFormulaRangeIndexAtCaret(editor) !== null;
836
+ var atValidInsertionPoint = isCaretAtValidFormulaRangeInsertionPoint(editor);
837
+ if (clickedInsideManagedRange || !atValidInsertionPoint) {
838
+ markRangeSelectionDirty(draftCtx);
839
+ }
840
+ });
746
841
  },
747
842
  innerRef: function innerRef(e) {
748
843
  inputRef.current = e;
@@ -785,7 +880,7 @@ var InputBox = function InputBox() {
785
880
  backgroundColor: "rgba(0, 123, 255, 0.25)"
786
881
  }
787
882
  });
788
- })))), (context.functionCandidates.length > 0 || context.functionHint || context.defaultCandidates.length > 0 || fn) && (/*#__PURE__*/React.createElement(React.Fragment, null, showSearchHint && (/*#__PURE__*/React.createElement(FormulaSearch, {
883
+ })))), (context.functionCandidates.length > 0 || context.functionHint || context.defaultCandidates.length > 0 || fn) && showCellFormulaChrome && (/*#__PURE__*/React.createElement(React.Fragment, null, showSearchHint && (/*#__PURE__*/React.createElement(FormulaSearch, {
789
884
  onMouseMove: function onMouseMove(e) {
790
885
  if (document.getElementById("luckysheet-formula-search-c")) {
791
886
  var hoveredItem = e.target.closest(".luckysheet-formula-search-item");
@@ -800,12 +895,12 @@ var InputBox = function InputBox() {
800
895
  }
801
896
  })), /*#__PURE__*/React.createElement("div", {
802
897
  className: "cell-hint"
803
- }, showFormulaHint && fn && (/*#__PURE__*/React.createElement(FormulaHint, {
898
+ }, showFormulaHint && fn && !showSearchHint && (/*#__PURE__*/React.createElement(FormulaHint, {
804
899
  handleShowFormulaHint: handleShowFormulaHint,
805
900
  showFormulaHint: showFormulaHint,
806
901
  commaCount: commaCount,
807
902
  functionName: functionName
808
- })), !showFormulaHint && fn && (/*#__PURE__*/React.createElement(Tooltip, {
903
+ })), !showFormulaHint && fn && !showSearchHint && (/*#__PURE__*/React.createElement(Tooltip, {
809
904
  text: "Turn on formula suggestions (F10)",
810
905
  placement: "top",
811
906
  defaultOpen: true,
@@ -6,6 +6,8 @@ export declare function buildFormulaSuggestionText(editableDiv: HTMLDivElement,
6
6
  caretOffset: number;
7
7
  };
8
8
  export declare function isLetterNumberPattern(str: string): boolean;
9
+ export declare function shouldShowFormulaFunctionList(editor: HTMLDivElement | null): boolean;
10
+ export declare function getFunctionNameFromFormulaCaretSpans(editor: HTMLDivElement | null): string | null;
9
11
  export declare function removeLastSpan(htmlString: string): string;
10
12
  export declare function numberToColumn(colNumber: number): string;
11
13
  export declare function incrementColumn(cell: string): string;
@@ -74,6 +74,49 @@ export function isLetterNumberPattern(str) {
74
74
  var regex = /^[a-zA-Z]+\d+$/;
75
75
  return regex.test(str);
76
76
  }
77
+ export function shouldShowFormulaFunctionList(editor) {
78
+ var _a, _b;
79
+ if (!editor) return false;
80
+ if (!((_a = editor.innerText) === null || _a === void 0 ? void 0 : _a.includes("="))) return false;
81
+ var parser = new DOMParser();
82
+ var doc = parser.parseFromString("<div>".concat(editor.innerHTML, "</div>"), "text/html");
83
+ var spans = doc.querySelectorAll("span");
84
+ var lastSpan = spans[spans.length - 1];
85
+ var lastText = (_b = lastSpan === null || lastSpan === void 0 ? void 0 : lastSpan.innerText) !== null && _b !== void 0 ? _b : "";
86
+ return /^=?[A-Za-z]*$/.test(lastText);
87
+ }
88
+ var FORMULA_FUNC_CLASS = "luckysheet-formula-text-func";
89
+ var FORMULA_LPAR_CLASS = "luckysheet-formula-text-lpar";
90
+ export function getFunctionNameFromFormulaCaretSpans(editor) {
91
+ var _a, _b;
92
+ if (!editor) return null;
93
+ var sel = window.getSelection();
94
+ if (!(sel === null || sel === void 0 ? void 0 : sel.rangeCount)) return null;
95
+ var range = sel.getRangeAt(0);
96
+ if (!editor.contains(range.startContainer)) return null;
97
+ var n = range.startContainer;
98
+ while (n && n !== editor) {
99
+ if (n.nodeType === Node.ELEMENT_NODE) {
100
+ var elem = n;
101
+ if (elem.classList.contains(FORMULA_FUNC_CLASS)) {
102
+ var next = elem.nextElementSibling;
103
+ if (next === null || next === void 0 ? void 0 : next.classList.contains(FORMULA_LPAR_CLASS)) {
104
+ var name_1 = (_a = elem.textContent) === null || _a === void 0 ? void 0 : _a.trim();
105
+ return name_1 ? name_1.toUpperCase() : null;
106
+ }
107
+ }
108
+ if (elem.classList.contains(FORMULA_LPAR_CLASS)) {
109
+ var prev = elem.previousElementSibling;
110
+ if (prev === null || prev === void 0 ? void 0 : prev.classList.contains(FORMULA_FUNC_CLASS)) {
111
+ var name_2 = (_b = prev.textContent) === null || _b === void 0 ? void 0 : _b.trim();
112
+ return name_2 ? name_2.toUpperCase() : null;
113
+ }
114
+ }
115
+ }
116
+ n = n.parentNode;
117
+ }
118
+ return null;
119
+ }
77
120
  export function removeLastSpan(htmlString) {
78
121
  var container = document.createElement("div");
79
122
  container.innerHTML = htmlString;