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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (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 +409 -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 +407 -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
@@ -294,6 +294,10 @@ var ConditionRules = function ConditionRules(_a) {
294
294
  text: "textContains",
295
295
  value: "()",
296
296
  label: "Text Contains"
297
+ }, {
298
+ text: "empty",
299
+ value: "",
300
+ label: "Empty"
297
301
  }, {
298
302
  text: "occurrenceDate",
299
303
  value: conditionformat.yesterday,
@@ -334,7 +338,7 @@ var ConditionRules = function ConditionRules(_a) {
334
338
  marginBottom: "16px"
335
339
  }
336
340
  }, matchedConditionFormatKey.map(function (key) {
337
- var _a, _b;
341
+ var _a, _b, _c;
338
342
  return /*#__PURE__*/React.createElement("div", {
339
343
  onClick: function onClick() {
340
344
  var _a;
@@ -385,10 +389,10 @@ var ConditionRules = function ConditionRules(_a) {
385
389
  }, /*#__PURE__*/React.createElement("h3", {
386
390
  className: "fortune-condition-rules__heading condition-list-type",
387
391
  "data-testid": "condition-rules-heading-".concat(key)
388
- }, conditionformat[allConditionFormats[key].conditionName], " ", (_a = allConditionFormats[key].conditionValue) === null || _a === void 0 ? void 0 : _a[0]), /*#__PURE__*/React.createElement("p", {
392
+ }, conditionformat[allConditionFormats[key].conditionName], allConditionFormats[key].conditionName !== "empty" && " ".concat((_b = (_a = allConditionFormats[key].conditionValue) === null || _a === void 0 ? void 0 : _a[0]) !== null && _b !== void 0 ? _b : "")), /*#__PURE__*/React.createElement("p", {
389
393
  className: "fortune-condition-rules__para condition-list-range",
390
394
  "data-testid": "condition-rules-para-".concat(key)
391
- }, (_b = allConditionFormats[key].cellrange) === null || _b === void 0 ? void 0 : _b.map(function (range) {
395
+ }, (_c = allConditionFormats[key].cellrange) === null || _c === void 0 ? void 0 : _c.map(function (range) {
392
396
  var startCol = numberToColumn(range.column[0] + 1);
393
397
  var endCol = numberToColumn(range.column[1] + 1);
394
398
  var startRow = range.row[0] + 1;
@@ -467,6 +471,12 @@ var ConditionRules = function ConditionRules(_a) {
467
471
  }, "Format cells if"), /*#__PURE__*/React.createElement(Select, {
468
472
  value: type,
469
473
  onValueChange: function onValueChange(value) {
474
+ if (value === "empty" || type === "empty") {
475
+ setContext(function (ctx) {
476
+ ctx.conditionRules.rulesValue = "";
477
+ });
478
+ setEditConditionFormatValue(null);
479
+ }
470
480
  setType(value);
471
481
  }
472
482
  }, /*#__PURE__*/React.createElement(SelectTrigger, null, /*#__PURE__*/React.createElement(SelectValue, null, /*#__PURE__*/React.createElement("div", {
@@ -804,7 +814,7 @@ var ConditionRules = function ConditionRules(_a) {
804
814
  },
805
815
  tabIndex: 0
806
816
  }, button.cancel), editConditionFormatKey !== null ? (/*#__PURE__*/React.createElement(Button, {
807
- disabled: context.conditionRules.rulesValue === "",
817
+ disabled: context.conditionRules.rulesValue === "" && type !== "empty",
808
818
  variant: "default",
809
819
  style: {
810
820
  minWidth: "80px"
@@ -814,7 +824,7 @@ var ConditionRules = function ConditionRules(_a) {
814
824
  },
815
825
  tabIndex: 0
816
826
  }, "Update rule")) : (/*#__PURE__*/React.createElement(Button, {
817
- disabled: context.conditionRules.rulesValue === "",
827
+ disabled: context.conditionRules.rulesValue === "" && type !== "empty",
818
828
  variant: "default",
819
829
  style: {
820
830
  minWidth: "80px"
@@ -107,6 +107,9 @@ var ConditionalFormat = function ConditionalFormat(_a) {
107
107
  }, {
108
108
  text: "textContains",
109
109
  value: "()"
110
+ }, {
111
+ text: "empty",
112
+ value: ""
110
113
  }, {
111
114
  text: "occurrenceDate",
112
115
  value: conditionformat.yesterday
@@ -129,7 +129,7 @@ import "./index.css";
129
129
  import Menu from "./Menu";
130
130
  import "tippy.js/dist/tippy.css";
131
131
  import SVGIcon from "../SVGIcon";
132
- import { LucideIcon as LocalLucidIcon } from "../../components/SheetOverlay/LucideIcon";
132
+ import { LucideIcon as LocalLucidIcon } from "../SheetOverlay/LucideIcon";
133
133
  var ContextMenu = function ContextMenu() {
134
134
  var showDialog = useDialog().showDialog;
135
135
  var containerRef = useRef(null);
@@ -6,7 +6,7 @@ export var TEXT_COLORS = [{
6
6
  value: "228, 232, 237"
7
7
  }, {
8
8
  name: "White",
9
- value: "249, 249, 249"
9
+ value: "219, 233, 236"
10
10
  }, {
11
11
  name: "Pink",
12
12
  value: "244, 217, 227"
@@ -1,47 +1,75 @@
1
- import { getFlowdata, cancelNormalSelected, getCellValue, updateCell, getInlineStringNoStyle, isInlineStringCell, escapeScriptTag, moveHighlightCell, handleFormulaInput, rangeHightlightselected, valueShowEs, isShowHidenCR, escapeHTMLTag, isAllowEdit } from "@fileverse-dev/fortune-core";
2
- import React, { useContext, useState, useCallback, useEffect, useRef, useMemo } from "react";
1
+ import { getFlowdata, cancelNormalSelected, getCellValue, updateCell, getInlineStringNoStyle, isInlineStringCell, escapeScriptTag, moveHighlightCell, handleFormulaInput, getFormulaRangeIndexAtCaret, isCaretAtValidFormulaRangeInsertionPoint, markRangeSelectionDirty, rangeHightlightselected, getFormulaEditorOwner, setFormulaEditorOwner, createRangeHightlight, valueShowEs, isShowHidenCR, escapeHTMLTag, functionHTMLGenerate, isAllowEdit, suppressFormulaRangeSelectionForInitialEdit } from "@fileverse-dev/fortune-core";
2
+ import React, { useContext, useState, useCallback, useEffect, useLayoutEffect, useRef, useMemo } from "react";
3
3
  import "./index.css";
4
4
  import _ from "lodash";
5
+ import { Tooltip } from "@fileverse/ui";
5
6
  import WorkbookContext from "../../context";
6
7
  import ContentEditable from "../SheetOverlay/ContentEditable";
7
8
  import NameBox from "./NameBox";
8
- import FormulaSearch from "../../components/SheetOverlay/FormulaSearch";
9
- import FormulaHint from "../../components/SheetOverlay/FormulaHint";
9
+ import FormulaSearch from "../SheetOverlay/FormulaSearch";
10
+ import FormulaHint from "../SheetOverlay/FormulaHint";
10
11
  import usePrevious from "../../hooks/usePrevious";
11
- import { LucideIcon } from "../../components/SheetOverlay/LucideIcon";
12
- import { countCommasBeforeCursor, isLetterNumberPattern, moveCursorToEnd } from "../../components/SheetOverlay/helper";
12
+ import { useFormulaEditorHistory } from "../../hooks/useFormulaEditorHistory";
13
+ import { useRerenderOnFormulaCaret } from "../../hooks/useRerenderOnFormulaCaret";
14
+ import { LucideIcon } from "../SheetOverlay/LucideIcon";
15
+ import { countCommasBeforeCursor, getCursorPosition, getFunctionNameFromFormulaCaretSpans, isLetterNumberPattern, setCursorPosition, buildFormulaSuggestionText, shouldShowFormulaFunctionList } from "../SheetOverlay/helper";
16
+ import { isFormulaSegmentBoundaryKey } from "../SheetOverlay/formula-segment-boundary";
13
17
  var FxEditor = function FxEditor() {
14
- var _a;
18
+ var _a, _b, _c;
15
19
  var hideFormulaHintLocal = localStorage.getItem("formulaMore") === "true";
16
- var _b = useState(false),
17
- showSearchHint = _b[0],
18
- setShowSearchHint = _b[1];
19
- var _c = useState(!hideFormulaHintLocal),
20
- showFormulaHint = _c[0],
21
- setShowFormulaHint = _c[1];
22
- var _d = useState(0),
23
- commaCount = _d[0],
24
- setCommaCount = _d[1];
25
- var _e = useContext(WorkbookContext),
26
- context = _e.context,
27
- setContext = _e.setContext,
28
- refs = _e.refs;
20
+ var _d = useState(false),
21
+ showSearchHint = _d[0],
22
+ setShowSearchHint = _d[1];
23
+ var _e = useState(!hideFormulaHintLocal),
24
+ showFormulaHint = _e[0],
25
+ setShowFormulaHint = _e[1];
26
+ var _f = useState(0),
27
+ commaCount = _f[0],
28
+ setCommaCount = _f[1];
29
+ var _g = useContext(WorkbookContext),
30
+ context = _g.context,
31
+ setContext = _g.setContext,
32
+ refs = _g.refs;
29
33
  var lastKeyDownEventRef = useRef(null);
34
+ var _h = useFormulaEditorHistory(refs.fxInput, refs.cellInput, refs.fxInput, setContext, "fx"),
35
+ formulaHistoryRef = _h.formulaHistoryRef,
36
+ preTextRef = _h.preTextRef,
37
+ resetFormulaHistory = _h.resetFormulaHistory,
38
+ handleFormulaHistoryUndoRedo = _h.handleFormulaHistoryUndoRedo,
39
+ capturePreFormulaState = _h.capturePreFormulaState,
40
+ appendFormulaHistoryFromPrimaryEditor = _h.appendFormulaHistoryFromPrimaryEditor;
30
41
  var inputContainerRef = useRef(null);
31
- var _f = useState(false),
32
- isHidenRC = _f[0],
33
- setIsHidenRC = _f[1];
42
+ var _j = useState(false),
43
+ isHidenRC = _j[0],
44
+ setIsHidenRC = _j[1];
34
45
  var firstSelection = (_a = context.luckysheet_select_save) === null || _a === void 0 ? void 0 : _a[0];
35
46
  var prevFirstSelection = usePrevious(firstSelection);
47
+ var prevCellUpdate = usePrevious(context.luckysheetCellUpdate);
36
48
  var prevSheetId = usePrevious(context.currentSheetId);
37
49
  var recentText = useRef("");
50
+ var formulaAnchorCellRef = useRef(null);
38
51
  var handleShowFormulaHint = function handleShowFormulaHint() {
39
52
  localStorage.setItem("formulaMore", String(showFormulaHint));
40
53
  setShowFormulaHint(!showFormulaHint);
41
54
  };
55
+ useEffect(function () {
56
+ var handleKeyDown = function handleKeyDown(event) {
57
+ if (event.key === "F10") {
58
+ event.preventDefault();
59
+ handleShowFormulaHint();
60
+ }
61
+ };
62
+ window.addEventListener("keydown", handleKeyDown);
63
+ return function () {
64
+ return window.removeEventListener("keydown", handleKeyDown);
65
+ };
66
+ }, [showFormulaHint]);
42
67
  useEffect(function () {
43
68
  var _a;
44
69
  setIsHidenRC(isShowHidenCR(context));
70
+ if (context.luckysheetCellUpdate.length > 0) {
71
+ return;
72
+ }
45
73
  if (_.isEqual(prevFirstSelection, firstSelection) && context.currentSheetId === prevSheetId) {
46
74
  return;
47
75
  }
@@ -65,7 +93,74 @@ var FxEditor = function FxEditor() {
65
93
  } else {
66
94
  refs.fxInput.current.innerHTML = "";
67
95
  }
68
- }, [context.luckysheetfile, context.currentSheetId, context.luckysheet_select_save]);
96
+ }, [context.luckysheetfile, context.currentSheetId, context.luckysheet_select_save, context.luckysheetCellUpdate.length]);
97
+ useLayoutEffect(function () {
98
+ var _a;
99
+ var fxInput = refs.fxInput.current;
100
+ if (context.luckysheetCellUpdate.length === 0 || !fxInput) {
101
+ return;
102
+ }
103
+ if (refs.globalCache.doNotUpdateCell) {
104
+ delete refs.globalCache.doNotUpdateCell;
105
+ return;
106
+ }
107
+ if (_.isEqual(prevCellUpdate, context.luckysheetCellUpdate) && prevSheetId === context.currentSheetId) {
108
+ return;
109
+ }
110
+ var _b = context.luckysheetCellUpdate,
111
+ rowIndex = _b[0],
112
+ colIndex = _b[1];
113
+ if (_.isNil(rowIndex) || _.isNil(colIndex)) {
114
+ return;
115
+ }
116
+ var pending = refs.globalCache.pendingTypeOverCell;
117
+ if (pending && pending[0] === rowIndex && pending[1] === colIndex) {
118
+ refs.globalCache.overwriteCell = false;
119
+ refs.globalCache.ignoreWriteCell = false;
120
+ return;
121
+ }
122
+ var flowdata = getFlowdata(context);
123
+ if (!flowdata) {
124
+ return;
125
+ }
126
+ var cell = (_a = flowdata === null || flowdata === void 0 ? void 0 : flowdata[rowIndex]) === null || _a === void 0 ? void 0 : _a[colIndex];
127
+ var value = "";
128
+ var overwrite = refs.globalCache.overwriteCell;
129
+ if (cell && !overwrite) {
130
+ if (isInlineStringCell(cell)) {
131
+ value = getInlineStringNoStyle(rowIndex, colIndex, flowdata);
132
+ } else if (cell.f) {
133
+ value = getCellValue(rowIndex, colIndex, flowdata, "f");
134
+ } else {
135
+ value = valueShowEs(rowIndex, colIndex, flowdata);
136
+ }
137
+ }
138
+ refs.globalCache.overwriteCell = false;
139
+ if (!refs.globalCache.ignoreWriteCell && value) {
140
+ fxInput.innerHTML = escapeHTMLTag(escapeScriptTag(value));
141
+ } else if (!refs.globalCache.ignoreWriteCell && !overwrite) {
142
+ var valueD = getCellValue(rowIndex, colIndex, flowdata, "f");
143
+ fxInput.innerText = valueD;
144
+ }
145
+ refs.globalCache.ignoreWriteCell = false;
146
+ }, [context.luckysheetCellUpdate, context.luckysheetfile, context.currentSheetId, prevCellUpdate, prevSheetId, refs.fxInput, refs.globalCache]);
147
+ useEffect(function () {
148
+ if (_.isEmpty(context.luckysheetCellUpdate)) {
149
+ delete refs.globalCache.pendingTypeOverCell;
150
+ resetFormulaHistory();
151
+ }
152
+ }, [context.luckysheetCellUpdate, resetFormulaHistory, refs.globalCache]);
153
+ useEffect(function () {
154
+ var _a;
155
+ if (_.isEmpty(context.luckysheetCellUpdate) || !refs.fxInput.current) {
156
+ formulaAnchorCellRef.current = null;
157
+ return;
158
+ }
159
+ var inputText = ((_a = refs.fxInput.current.innerText) === null || _a === void 0 ? void 0 : _a.trim()) || "";
160
+ if (!inputText.startsWith("=")) {
161
+ formulaAnchorCellRef.current = null;
162
+ }
163
+ }, [context.luckysheetCellUpdate, refs.fxInput]);
69
164
  var onFocus = useCallback(function () {
70
165
  var _a, _b;
71
166
  if (context.allowEdit === false) {
@@ -73,31 +168,55 @@ var FxEditor = function FxEditor() {
73
168
  }
74
169
  if (((_b = (_a = context.luckysheet_select_save) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0) > 0 && !context.luckysheet_cell_selected_move && isAllowEdit(context, context.luckysheet_select_save)) {
75
170
  setContext(function (draftCtx) {
171
+ var _a;
172
+ setFormulaEditorOwner(draftCtx, "fx");
76
173
  var last = draftCtx.luckysheet_select_save[draftCtx.luckysheet_select_save.length - 1];
77
174
  var row_index = last.row_focus;
78
175
  var col_index = last.column_focus;
79
- draftCtx.luckysheetCellUpdate = [row_index, col_index];
80
- refs.globalCache.doNotFocus = true;
176
+ if (!_.isNil(row_index) && !_.isNil(col_index)) {
177
+ var flowdata = getFlowdata(draftCtx);
178
+ var cellAt = (_a = flowdata === null || flowdata === void 0 ? void 0 : flowdata[row_index]) === null || _a === void 0 ? void 0 : _a[col_index];
179
+ if ((cellAt === null || cellAt === void 0 ? void 0 : cellAt.f) != null && String(cellAt.f).trim() !== "") {
180
+ suppressFormulaRangeSelectionForInitialEdit(draftCtx);
181
+ }
182
+ }
183
+ var currentUpdate = draftCtx.luckysheetCellUpdate || [];
184
+ var alreadyEditingSameCell = currentUpdate.length === 2 && currentUpdate[0] === row_index && currentUpdate[1] === col_index;
185
+ if (!alreadyEditingSameCell) {
186
+ refs.globalCache.doNotUpdateCell = true;
187
+ draftCtx.luckysheetCellUpdate = [row_index, col_index];
188
+ refs.globalCache.doNotFocus = true;
189
+ }
81
190
  });
82
191
  }
192
+ setContext(function (draftCtx) {
193
+ setFormulaEditorOwner(draftCtx, "fx");
194
+ });
83
195
  }, [context.config, context.luckysheet_select_save, context.luckysheetfile, context.currentSheetId, refs.globalCache, setContext]);
84
196
  var getActiveFormula = useCallback(function () {
85
197
  return document.querySelector(".luckysheet-formula-search-item-active");
86
198
  }, []);
87
199
  var insertSelectedFormula = useCallback(function (formulaName) {
88
- 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>");
89
- refs.fxInput.current.innerHTML = ht;
200
+ var fxEditor = refs.fxInput.current;
201
+ if (!fxEditor) return;
90
202
  var cellEditor = document.getElementById("luckysheet-rich-text-editor");
203
+ var _a = buildFormulaSuggestionText(fxEditor, formulaName),
204
+ text = _a.text,
205
+ caretOffset = _a.caretOffset;
206
+ var safeText = escapeScriptTag(text);
207
+ var html = safeText.startsWith("=") ? functionHTMLGenerate(safeText) : escapeHTMLTag(safeText);
208
+ fxEditor.innerHTML = html;
91
209
  if (cellEditor) {
92
- cellEditor.innerHTML = ht;
210
+ cellEditor.innerHTML = html;
93
211
  }
94
- moveCursorToEnd(refs.fxInput.current);
212
+ setCursorPosition(fxEditor, caretOffset);
213
+ setShowSearchHint(shouldShowFormulaFunctionList(fxEditor));
95
214
  setContext(function (draftCtx) {
96
215
  draftCtx.functionCandidates = [];
97
216
  draftCtx.defaultCandidates = [];
98
- draftCtx.functionHint = formulaName;
217
+ draftCtx.functionHint = (formulaName || "").toUpperCase();
99
218
  });
100
- }, [setContext]);
219
+ }, [refs.fxInput, setContext]);
101
220
  var clearSearchItemActiveClass = useCallback(function () {
102
221
  var activeFormula = getActiveFormula();
103
222
  if (activeFormula) {
@@ -113,10 +232,11 @@ var FxEditor = function FxEditor() {
113
232
  return lastSpan === null || lastSpan === void 0 ? void 0 : lastSpan.innerText;
114
233
  };
115
234
  var selectActiveFormulaOnClick = useCallback(function (e) {
116
- var _a, _b, _c;
235
+ var _a, _b, _c, _d;
117
236
  if (e.target.className.includes("sign-fortune")) return;
118
- recentText.current = (_a = refs.fxInput) === null || _a === void 0 ? void 0 : _a.current.innerText;
119
- var formulaName = (_c = (_b = getActiveFormula()) === null || _b === void 0 ? void 0 : _b.querySelector(".luckysheet-formula-search-func")) === null || _c === void 0 ? void 0 : _c.textContent;
237
+ preTextRef.current = (_a = refs.fxInput) === null || _a === void 0 ? void 0 : _a.current.innerText;
238
+ recentText.current = (_b = refs.fxInput) === null || _b === void 0 ? void 0 : _b.current.innerText;
239
+ var formulaName = (_d = (_c = getActiveFormula()) === null || _c === void 0 ? void 0 : _c.querySelector(".luckysheet-formula-search-func")) === null || _d === void 0 ? void 0 : _d.textContent;
120
240
  var lastSpanText = getLastInputSpanText();
121
241
  if (formulaName && !isLetterNumberPattern(lastSpanText)) {
122
242
  insertSelectedFormula(formulaName);
@@ -125,18 +245,103 @@ var FxEditor = function FxEditor() {
125
245
  }
126
246
  }, [getActiveFormula, insertSelectedFormula]);
127
247
  var onKeyDown = useCallback(function (e) {
128
- var _a;
248
+ var _a, _b, _c, _d;
249
+ if (e.key === "Delete" || e.key === "Backspace") {
250
+ var anchor = formulaAnchorCellRef.current;
251
+ if (anchor != null) {
252
+ var anchorRow_1 = anchor[0],
253
+ anchorCol_1 = anchor[1];
254
+ setTimeout(function () {
255
+ setContext(function (draftCtx) {
256
+ draftCtx.luckysheetCellUpdate = [anchorRow_1, anchorCol_1];
257
+ draftCtx.luckysheet_select_save = [{
258
+ row: [anchorRow_1, anchorRow_1],
259
+ column: [anchorCol_1, anchorCol_1],
260
+ row_focus: anchorRow_1,
261
+ column_focus: anchorCol_1
262
+ }];
263
+ markRangeSelectionDirty(draftCtx);
264
+ var el = refs.fxInput.current;
265
+ if (el && el.innerText.trim().startsWith("=")) {
266
+ createRangeHightlight(draftCtx, el.innerHTML);
267
+ rangeHightlightselected(draftCtx, el);
268
+ }
269
+ });
270
+ }, 0);
271
+ }
272
+ }
129
273
  if (context.allowEdit === false) {
130
274
  return;
131
275
  }
276
+ setContext(function (draftCtx) {
277
+ setFormulaEditorOwner(draftCtx, "fx");
278
+ });
132
279
  var currentCommaCount = countCommasBeforeCursor((_a = refs.fxInput) === null || _a === void 0 ? void 0 : _a.current);
133
280
  setCommaCount(currentCommaCount);
134
281
  lastKeyDownEventRef.current = new KeyboardEvent(e.type, e.nativeEvent);
135
- var key = e.key;
282
+ capturePreFormulaState();
136
283
  recentText.current = refs.fxInput.current.innerText;
137
- if (key === "ArrowLeft" || key === "ArrowRight") {
284
+ var key = e.key;
285
+ var currentInputText = ((_c = (_b = refs.fxInput.current) === null || _b === void 0 ? void 0 : _b.innerText) === null || _c === void 0 ? void 0 : _c.trim()) || "";
286
+ if ((key === "=" || currentInputText.startsWith("=")) && context.luckysheetCellUpdate.length === 2 && formulaAnchorCellRef.current == null) {
287
+ setContext(function (draftCtx) {
288
+ draftCtx.formulaCache.rangeSelectionActive = null;
289
+ });
290
+ formulaAnchorCellRef.current = [context.luckysheetCellUpdate[0], context.luckysheetCellUpdate[1]];
291
+ }
292
+ if (key === "(" && currentInputText.startsWith("=")) {
293
+ setContext(function (draftCtx) {
294
+ draftCtx.formulaCache.rangeSelectionActive = null;
295
+ });
296
+ }
297
+ if (isFormulaSegmentBoundaryKey(key) && context.luckysheetCellUpdate.length > 0 && currentInputText.startsWith("=") && formulaAnchorCellRef.current) {
298
+ setContext(function (draftCtx) {
299
+ draftCtx.formulaCache.rangeSelectionActive = null;
300
+ });
301
+ var _e = formulaAnchorCellRef.current,
302
+ anchorRow_2 = _e[0],
303
+ anchorCol_2 = _e[1];
304
+ setTimeout(function () {
305
+ setContext(function (draftCtx) {
306
+ var _a, _b;
307
+ draftCtx.luckysheetCellUpdate = [anchorRow_2, anchorCol_2];
308
+ draftCtx.luckysheet_select_save = [{
309
+ row: [anchorRow_2, anchorRow_2],
310
+ column: [anchorCol_2, anchorCol_2],
311
+ row_focus: anchorRow_2,
312
+ column_focus: anchorCol_2
313
+ }];
314
+ draftCtx.formulaRangeSelect = undefined;
315
+ draftCtx.formulaCache.selectingRangeIndex = -1;
316
+ draftCtx.formulaCache.func_selectedrange = undefined;
317
+ draftCtx.formulaCache.rangechangeindex = undefined;
318
+ draftCtx.formulaCache.rangestart = false;
319
+ draftCtx.formulaCache.rangedrag_column_start = false;
320
+ draftCtx.formulaCache.rangedrag_row_start = false;
321
+ createRangeHightlight(draftCtx, ((_a = refs.fxInput.current) === null || _a === void 0 ? void 0 : _a.innerHTML) || ((_b = refs.cellInput.current) === null || _b === void 0 ? void 0 : _b.innerHTML) || "");
322
+ moveHighlightCell(draftCtx, "down", 0, "rangeOfSelect");
323
+ });
324
+ }, 0);
325
+ }
326
+ var isArrowKey = key === "ArrowUp" || key === "ArrowDown" || key === "ArrowLeft" || key === "ArrowRight";
327
+ var isDirectPlainTypeEdit = context.luckysheetCellUpdate.length > 0 && ((_d = refs.globalCache) === null || _d === void 0 ? void 0 : _d.enteredEditByTyping) === true && !currentInputText.startsWith("=");
328
+ var sheetArrowShortcut = isArrowKey && (e.metaKey || e.ctrlKey || e.shiftKey);
329
+ if ((key === "ArrowLeft" || key === "ArrowRight") && !(isDirectPlainTypeEdit && sheetArrowShortcut)) {
138
330
  e.stopPropagation();
139
331
  }
332
+ if ((e.metaKey || e.ctrlKey) && context.luckysheetCellUpdate.length > 0) {
333
+ if (e.code === "KeyZ" || e.code === "KeyY") {
334
+ var shouldUseFormulaHistory = currentInputText.startsWith("=") || formulaHistoryRef.current.active;
335
+ if (shouldUseFormulaHistory) {
336
+ var handledByFormulaHistory = handleFormulaHistoryUndoRedo(e.code === "KeyY" || e.code === "KeyZ" && e.shiftKey);
337
+ if (handledByFormulaHistory) {
338
+ e.preventDefault();
339
+ }
340
+ }
341
+ e.stopPropagation();
342
+ return;
343
+ }
344
+ }
140
345
  if ((e.key === "Enter" || e.key === "Tab") && context.luckysheetCellUpdate.length > 0) {
141
346
  if (e.altKey || e.metaKey) {
142
347
  document.execCommand("insertHTML", false, "\n ");
@@ -148,7 +353,7 @@ var FxEditor = function FxEditor() {
148
353
  }
149
354
  return;
150
355
  }
151
- if (e.key === "ArrowUp" && context.luckysheetCellUpdate.length > 0) {
356
+ if (!(e.metaKey || e.ctrlKey) && e.key === "ArrowUp" && context.luckysheetCellUpdate.length > 0) {
152
357
  if (document.getElementById("luckysheet-formula-search-c")) {
153
358
  var formulaSearchContainer = document.getElementById("luckysheet-formula-search-c");
154
359
  var activeItem = formulaSearchContainer === null || formulaSearchContainer === void 0 ? void 0 : formulaSearchContainer.querySelector(".luckysheet-formula-search-item-active");
@@ -167,7 +372,7 @@ var FxEditor = function FxEditor() {
167
372
  }
168
373
  }
169
374
  e.preventDefault();
170
- } else if (e.key === "ArrowDown" && context.luckysheetCellUpdate.length > 0) {
375
+ } else if (!(e.metaKey || e.ctrlKey) && e.key === "ArrowDown" && context.luckysheetCellUpdate.length > 0) {
171
376
  if (document.getElementById("luckysheet-formula-search-c")) {
172
377
  var formulaSearchContainer = document.getElementById("luckysheet-formula-search-c");
173
378
  var activeItem = formulaSearchContainer === null || formulaSearchContainer === void 0 ? void 0 : formulaSearchContainer.querySelector(".luckysheet-formula-search-item-active");
@@ -226,7 +431,7 @@ var FxEditor = function FxEditor() {
226
431
  }
227
432
  }
228
433
  });
229
- }, [context.allowEdit, context.luckysheetCellUpdate.length, refs.fxInput, setContext]);
434
+ }, [capturePreFormulaState, context.allowEdit, context.luckysheetCellUpdate, handleFormulaHistoryUndoRedo, refs.cellInput, refs.fxInput, setContext]);
230
435
  var handleHideShowHint = function handleHideShowHint() {
231
436
  var _a, _b, _c, _d;
232
437
  var el = (_a = document.getElementsByClassName("cell-hint")) === null || _a === void 0 ? void 0 : _a[0];
@@ -247,26 +452,54 @@ var FxEditor = function FxEditor() {
247
452
  }
248
453
  };
249
454
  var onChange = useCallback(function () {
250
- var _a, _b, _c;
455
+ var _a, _b, _c, _d;
251
456
  if (context.isFlvReadOnly) return;
252
- handleHideShowHint();
253
- if (((_b = (_a = refs.fxInput) === null || _a === void 0 ? void 0 : _a.current) === null || _b === void 0 ? void 0 : _b.innerText.includes("=")) && /^=?[A-Za-z]*$/.test(getLastInputSpanText())) {
254
- setShowSearchHint(true);
255
- } else {
256
- setShowSearchHint(false);
457
+ var fxEl = refs.fxInput.current;
458
+ if (fxEl && context.luckysheetCellUpdate.length === 2 && formulaAnchorCellRef.current == null) {
459
+ var t = ((_a = fxEl.innerText) === null || _a === void 0 ? void 0 : _a.trim()) || "";
460
+ if (t.startsWith("=")) {
461
+ formulaAnchorCellRef.current = [context.luckysheetCellUpdate[0], context.luckysheetCellUpdate[1]];
462
+ }
257
463
  }
258
- var currentCommaCount = countCommasBeforeCursor((_c = refs.fxInput) === null || _c === void 0 ? void 0 : _c.current);
464
+ handleHideShowHint();
465
+ setShowSearchHint(shouldShowFormulaFunctionList((_c = (_b = refs.fxInput) === null || _b === void 0 ? void 0 : _b.current) !== null && _c !== void 0 ? _c : null));
466
+ var currentCommaCount = countCommasBeforeCursor((_d = refs.fxInput) === null || _d === void 0 ? void 0 : _d.current);
259
467
  setCommaCount(currentCommaCount);
260
468
  var e = lastKeyDownEventRef.current;
261
- if (!e) return;
469
+ if (!e) {
470
+ var fx = refs.fxInput.current;
471
+ var cell = refs.cellInput.current;
472
+ var isFormula = function isFormula(el) {
473
+ var _a, _b;
474
+ return !!(((_a = el === null || el === void 0 ? void 0 : el.innerText) === null || _a === void 0 ? void 0 : _a.trim().startsWith("=")) || ((_b = el === null || el === void 0 ? void 0 : el.textContent) === null || _b === void 0 ? void 0 : _b.trim().startsWith("=")));
475
+ };
476
+ var sel = window.getSelection();
477
+ var editor_1 = null;
478
+ if (sel === null || sel === void 0 ? void 0 : sel.rangeCount) {
479
+ var node = sel.getRangeAt(0).startContainer;
480
+ if ((fx === null || fx === void 0 ? void 0 : fx.contains(node)) && isFormula(fx)) editor_1 = fx;else if ((cell === null || cell === void 0 ? void 0 : cell.contains(node)) && isFormula(cell)) editor_1 = cell;
481
+ }
482
+ if (!editor_1 && isFormula(fx)) editor_1 = fx;else if (!editor_1 && isFormula(cell)) editor_1 = cell;
483
+ if (editor_1) {
484
+ setContext(function (draftCtx) {
485
+ if (!isAllowEdit(draftCtx, draftCtx.luckysheet_select_save)) return;
486
+ rangeHightlightselected(draftCtx, editor_1);
487
+ });
488
+ }
489
+ return;
490
+ }
262
491
  var kcode = e.keyCode;
263
492
  if (!kcode) return;
493
+ appendFormulaHistoryFromPrimaryEditor(function () {
494
+ return getCursorPosition(refs.fxInput.current);
495
+ });
264
496
  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) {
265
497
  setContext(function (draftCtx) {
266
498
  handleFormulaInput(draftCtx, refs.cellInput.current, refs.fxInput.current, kcode, recentText.current);
267
499
  });
268
500
  }
269
- }, [refs.cellInput, refs.fxInput, setContext]);
501
+ }, [appendFormulaHistoryFromPrimaryEditor, context.isFlvReadOnly, context.luckysheetCellUpdate, refs.cellInput, refs.fxInput, setContext]);
502
+ useRerenderOnFormulaCaret(refs.fxInput, context.luckysheetCellUpdate.length > 0);
270
503
  var getFunctionNameFromInput = useCallback(function () {
271
504
  var _a, _b, _c, _d;
272
505
  var inputText = ((_b = (_a = refs.fxInput) === null || _a === void 0 ? void 0 : _a.current) === null || _b === void 0 ? void 0 : _b.innerText) || "";
@@ -283,8 +516,9 @@ var FxEditor = function FxEditor() {
283
516
  }
284
517
  return null;
285
518
  }, []);
286
- var functionName = context.functionHint || getFunctionNameFromInput();
519
+ var functionName = (_c = (_b = getFunctionNameFromFormulaCaretSpans(refs.fxInput.current)) !== null && _b !== void 0 ? _b : context.functionHint) !== null && _c !== void 0 ? _c : getFunctionNameFromInput();
287
520
  var fn = functionName ? context.formulaCache.functionlistMap[functionName] : null;
521
+ var showFxFormulaChrome = context.luckysheetCellUpdate.length > 0 && getFormulaEditorOwner(context) === "fx";
288
522
  var allowEdit = useMemo(function () {
289
523
  if (context.allowEdit === false) {
290
524
  return false;
@@ -298,9 +532,9 @@ var FxEditor = function FxEditor() {
298
532
  return true;
299
533
  }, [context.config, context.luckysheet_select_save, context.luckysheetfile, context.currentSheetId, isHidenRC]);
300
534
  var divRef = useRef(null);
301
- var _g = useState(false),
302
- isResizing = _g[0],
303
- setIsResizing = _g[1];
535
+ var _k = useState(false),
536
+ isResizing = _k[0],
537
+ setIsResizing = _k[1];
304
538
  var startResize = function startResize(e) {
305
539
  e.preventDefault();
306
540
  setIsResizing(true);
@@ -319,7 +553,7 @@ var FxEditor = function FxEditor() {
319
553
  document.addEventListener("mouseup", _onMouseUp);
320
554
  };
321
555
  return /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement("div", {
322
- className: "fortune-fx-editor",
556
+ className: showFxFormulaChrome ? "fortune-fx-editor fortune-fx-editor--formula-owner" : "fortune-fx-editor",
323
557
  ref: divRef
324
558
  }, /*#__PURE__*/React.createElement(NameBox, null), /*#__PURE__*/React.createElement("div", {
325
559
  className: "fortune-fx-icon",
@@ -346,8 +580,20 @@ var FxEditor = function FxEditor() {
346
580
  onMouseUp: function onMouseUp() {
347
581
  var _a;
348
582
  handleHideShowHint();
349
- var currentCommaCount = countCommasBeforeCursor((_a = refs.fxInput) === null || _a === void 0 ? void 0 : _a.current);
583
+ setContext(function (draftCtx) {
584
+ setFormulaEditorOwner(draftCtx, "fx");
585
+ });
586
+ var editor = (_a = refs.fxInput) === null || _a === void 0 ? void 0 : _a.current;
587
+ var currentCommaCount = countCommasBeforeCursor(editor);
350
588
  setCommaCount(currentCommaCount);
589
+ setContext(function (draftCtx) {
590
+ if (draftCtx.formulaCache.rangeSelectionActive !== true) return;
591
+ var clickedInsideManagedRange = getFormulaRangeIndexAtCaret(editor) !== null;
592
+ var atValidInsertionPoint = isCaretAtValidFormulaRangeInsertionPoint(editor);
593
+ if (clickedInsideManagedRange || !atValidInsertionPoint) {
594
+ markRangeSelectionDirty(draftCtx);
595
+ }
596
+ });
351
597
  },
352
598
  innerRef: function innerRef(e) {
353
599
  refs.fxInput.current = e;
@@ -360,7 +606,7 @@ var FxEditor = function FxEditor() {
360
606
  onChange: onChange,
361
607
  tabIndex: 0,
362
608
  allowEdit: allowEdit && !context.isFlvReadOnly
363
- }), showSearchHint && (/*#__PURE__*/React.createElement(FormulaSearch, {
609
+ }), (context.functionCandidates.length > 0 || context.functionHint || context.defaultCandidates.length > 0 || fn) && showFxFormulaChrome && (/*#__PURE__*/React.createElement(React.Fragment, null, showSearchHint && (/*#__PURE__*/React.createElement(FormulaSearch, {
364
610
  from: "fx",
365
611
  onMouseMove: function onMouseMove(e) {
366
612
  if (document.getElementById("luckysheet-formula-search-c")) {
@@ -376,12 +622,22 @@ var FxEditor = function FxEditor() {
376
622
  }
377
623
  })), /*#__PURE__*/React.createElement("div", {
378
624
  className: "fx-hint"
379
- }, showFormulaHint && fn && (/*#__PURE__*/React.createElement(FormulaHint, {
625
+ }, showFormulaHint && fn && !showSearchHint && (/*#__PURE__*/React.createElement(FormulaHint, {
380
626
  handleShowFormulaHint: handleShowFormulaHint,
381
627
  showFormulaHint: showFormulaHint,
382
628
  commaCount: commaCount,
383
629
  functionName: functionName
384
- })), !showFormulaHint && fn && (/*#__PURE__*/React.createElement("div", {
630
+ })), !showFormulaHint && fn && !showSearchHint && (/*#__PURE__*/React.createElement(Tooltip, {
631
+ text: "Turn on formula suggestions (F10)",
632
+ placement: "top",
633
+ defaultOpen: true,
634
+ style: {
635
+ position: "absolute",
636
+ top: "-50px",
637
+ left: "-130px",
638
+ width: "210px"
639
+ }
640
+ }, /*#__PURE__*/React.createElement("div", {
385
641
  className: "luckysheet-hin absolute show-more-btn",
386
642
  onClick: function onClick() {
387
643
  handleShowFormulaHint();
@@ -395,7 +651,7 @@ var FxEditor = function FxEditor() {
395
651
  margin: "auto",
396
652
  marginTop: "1px"
397
653
  }
398
- })))))), /*#__PURE__*/React.createElement("div", {
654
+ }))))))))), /*#__PURE__*/React.createElement("div", {
399
655
  className: "resize-handle",
400
656
  onMouseDown: startResize,
401
657
  style: {