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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. package/es/components/ContextMenu/index.js +1 -1
  2. package/es/components/FxEditor/index.js +316 -60
  3. package/es/components/SheetOverlay/FormulaHint/index.js +46 -25
  4. package/es/components/SheetOverlay/FormulaSearch/index.d.ts +3 -1
  5. package/es/components/SheetOverlay/FormulaSearch/index.js +58 -29
  6. package/es/components/SheetOverlay/InputBox.js +406 -194
  7. package/es/components/SheetOverlay/drag_and_drop/column-helpers.js +34 -48
  8. package/es/components/SheetOverlay/drag_and_drop/row-helpers.js +31 -41
  9. package/es/components/SheetOverlay/formula-segment-boundary.d.ts +1 -0
  10. package/es/components/SheetOverlay/formula-segment-boundary.js +4 -0
  11. package/es/components/SheetOverlay/helper.d.ts +7 -0
  12. package/es/components/SheetOverlay/helper.js +95 -0
  13. package/es/components/SheetOverlay/index.css +6 -45
  14. package/es/components/SheetOverlay/index.js +26 -14
  15. package/es/components/Workbook/index.js +5 -8
  16. package/es/hooks/useFormulaEditorHistory.d.ts +24 -0
  17. package/es/hooks/useFormulaEditorHistory.js +119 -0
  18. package/es/hooks/useRerenderOnFormulaCaret.d.ts +2 -0
  19. package/es/hooks/useRerenderOnFormulaCaret.js +28 -0
  20. package/lib/components/ContextMenu/index.js +1 -1
  21. package/lib/components/FxEditor/index.js +314 -58
  22. package/lib/components/SheetOverlay/FormulaHint/index.js +45 -24
  23. package/lib/components/SheetOverlay/FormulaSearch/index.d.ts +3 -1
  24. package/lib/components/SheetOverlay/FormulaSearch/index.js +57 -28
  25. package/lib/components/SheetOverlay/InputBox.js +404 -192
  26. package/lib/components/SheetOverlay/drag_and_drop/column-helpers.js +33 -47
  27. package/lib/components/SheetOverlay/drag_and_drop/row-helpers.js +31 -41
  28. package/lib/components/SheetOverlay/formula-segment-boundary.d.ts +1 -0
  29. package/lib/components/SheetOverlay/formula-segment-boundary.js +10 -0
  30. package/lib/components/SheetOverlay/helper.d.ts +7 -0
  31. package/lib/components/SheetOverlay/helper.js +99 -0
  32. package/lib/components/SheetOverlay/index.css +6 -45
  33. package/lib/components/SheetOverlay/index.js +25 -13
  34. package/lib/components/Workbook/index.js +5 -8
  35. package/lib/hooks/useFormulaEditorHistory.d.ts +24 -0
  36. package/lib/hooks/useFormulaEditorHistory.js +126 -0
  37. package/lib/hooks/useRerenderOnFormulaCaret.d.ts +2 -0
  38. package/lib/hooks/useRerenderOnFormulaCaret.js +34 -0
  39. package/package.json +2 -2
@@ -9,48 +9,76 @@ var _fortuneCore = require("@fileverse-dev/fortune-core");
9
9
  var _react = _interopRequireWildcard(require("react"));
10
10
  require("./index.css");
11
11
  var _lodash = _interopRequireDefault(require("lodash"));
12
+ var _ui = require("@fileverse/ui");
12
13
  var _context = _interopRequireDefault(require("../../context"));
13
14
  var _ContentEditable = _interopRequireDefault(require("../SheetOverlay/ContentEditable"));
14
15
  var _NameBox = _interopRequireDefault(require("./NameBox"));
15
- var _FormulaSearch = _interopRequireDefault(require("../../components/SheetOverlay/FormulaSearch"));
16
- var _FormulaHint = _interopRequireDefault(require("../../components/SheetOverlay/FormulaHint"));
16
+ var _FormulaSearch = _interopRequireDefault(require("../SheetOverlay/FormulaSearch"));
17
+ var _FormulaHint = _interopRequireDefault(require("../SheetOverlay/FormulaHint"));
17
18
  var _usePrevious = _interopRequireDefault(require("../../hooks/usePrevious"));
18
- var _LucideIcon = require("../../components/SheetOverlay/LucideIcon");
19
- var _helper = require("../../components/SheetOverlay/helper");
19
+ var _useFormulaEditorHistory = require("../../hooks/useFormulaEditorHistory");
20
+ var _useRerenderOnFormulaCaret = require("../../hooks/useRerenderOnFormulaCaret");
21
+ var _LucideIcon = require("../SheetOverlay/LucideIcon");
22
+ var _helper = require("../SheetOverlay/helper");
23
+ var _formulaSegmentBoundary = require("../SheetOverlay/formula-segment-boundary");
20
24
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
21
25
  function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
22
26
  var FxEditor = function FxEditor() {
23
- var _a;
27
+ var _a, _b, _c;
24
28
  var hideFormulaHintLocal = localStorage.getItem("formulaMore") === "true";
25
- var _b = (0, _react.useState)(false),
26
- showSearchHint = _b[0],
27
- setShowSearchHint = _b[1];
28
- var _c = (0, _react.useState)(!hideFormulaHintLocal),
29
- showFormulaHint = _c[0],
30
- setShowFormulaHint = _c[1];
31
- var _d = (0, _react.useState)(0),
32
- commaCount = _d[0],
33
- setCommaCount = _d[1];
34
- var _e = (0, _react.useContext)(_context.default),
35
- context = _e.context,
36
- setContext = _e.setContext,
37
- refs = _e.refs;
29
+ var _d = (0, _react.useState)(false),
30
+ showSearchHint = _d[0],
31
+ setShowSearchHint = _d[1];
32
+ var _e = (0, _react.useState)(!hideFormulaHintLocal),
33
+ showFormulaHint = _e[0],
34
+ setShowFormulaHint = _e[1];
35
+ var _f = (0, _react.useState)(0),
36
+ commaCount = _f[0],
37
+ setCommaCount = _f[1];
38
+ var _g = (0, _react.useContext)(_context.default),
39
+ context = _g.context,
40
+ setContext = _g.setContext,
41
+ refs = _g.refs;
38
42
  var lastKeyDownEventRef = (0, _react.useRef)(null);
43
+ var _h = (0, _useFormulaEditorHistory.useFormulaEditorHistory)(refs.fxInput, refs.cellInput, refs.fxInput, setContext, "fx"),
44
+ formulaHistoryRef = _h.formulaHistoryRef,
45
+ preTextRef = _h.preTextRef,
46
+ resetFormulaHistory = _h.resetFormulaHistory,
47
+ handleFormulaHistoryUndoRedo = _h.handleFormulaHistoryUndoRedo,
48
+ capturePreFormulaState = _h.capturePreFormulaState,
49
+ appendFormulaHistoryFromPrimaryEditor = _h.appendFormulaHistoryFromPrimaryEditor;
39
50
  var inputContainerRef = (0, _react.useRef)(null);
40
- var _f = (0, _react.useState)(false),
41
- isHidenRC = _f[0],
42
- setIsHidenRC = _f[1];
51
+ var _j = (0, _react.useState)(false),
52
+ isHidenRC = _j[0],
53
+ setIsHidenRC = _j[1];
43
54
  var firstSelection = (_a = context.luckysheet_select_save) === null || _a === void 0 ? void 0 : _a[0];
44
55
  var prevFirstSelection = (0, _usePrevious.default)(firstSelection);
56
+ var prevCellUpdate = (0, _usePrevious.default)(context.luckysheetCellUpdate);
45
57
  var prevSheetId = (0, _usePrevious.default)(context.currentSheetId);
46
58
  var recentText = (0, _react.useRef)("");
59
+ var formulaAnchorCellRef = (0, _react.useRef)(null);
47
60
  var handleShowFormulaHint = function handleShowFormulaHint() {
48
61
  localStorage.setItem("formulaMore", String(showFormulaHint));
49
62
  setShowFormulaHint(!showFormulaHint);
50
63
  };
64
+ (0, _react.useEffect)(function () {
65
+ var handleKeyDown = function handleKeyDown(event) {
66
+ if (event.key === "F10") {
67
+ event.preventDefault();
68
+ handleShowFormulaHint();
69
+ }
70
+ };
71
+ window.addEventListener("keydown", handleKeyDown);
72
+ return function () {
73
+ return window.removeEventListener("keydown", handleKeyDown);
74
+ };
75
+ }, [showFormulaHint]);
51
76
  (0, _react.useEffect)(function () {
52
77
  var _a;
53
78
  setIsHidenRC((0, _fortuneCore.isShowHidenCR)(context));
79
+ if (context.luckysheetCellUpdate.length > 0) {
80
+ return;
81
+ }
54
82
  if (_lodash.default.isEqual(prevFirstSelection, firstSelection) && context.currentSheetId === prevSheetId) {
55
83
  return;
56
84
  }
@@ -74,7 +102,74 @@ var FxEditor = function FxEditor() {
74
102
  } else {
75
103
  refs.fxInput.current.innerHTML = "";
76
104
  }
77
- }, [context.luckysheetfile, context.currentSheetId, context.luckysheet_select_save]);
105
+ }, [context.luckysheetfile, context.currentSheetId, context.luckysheet_select_save, context.luckysheetCellUpdate.length]);
106
+ (0, _react.useLayoutEffect)(function () {
107
+ var _a;
108
+ var fxInput = refs.fxInput.current;
109
+ if (context.luckysheetCellUpdate.length === 0 || !fxInput) {
110
+ return;
111
+ }
112
+ if (refs.globalCache.doNotUpdateCell) {
113
+ delete refs.globalCache.doNotUpdateCell;
114
+ return;
115
+ }
116
+ if (_lodash.default.isEqual(prevCellUpdate, context.luckysheetCellUpdate) && prevSheetId === context.currentSheetId) {
117
+ return;
118
+ }
119
+ var _b = context.luckysheetCellUpdate,
120
+ rowIndex = _b[0],
121
+ colIndex = _b[1];
122
+ if (_lodash.default.isNil(rowIndex) || _lodash.default.isNil(colIndex)) {
123
+ return;
124
+ }
125
+ var pending = refs.globalCache.pendingTypeOverCell;
126
+ if (pending && pending[0] === rowIndex && pending[1] === colIndex) {
127
+ refs.globalCache.overwriteCell = false;
128
+ refs.globalCache.ignoreWriteCell = false;
129
+ return;
130
+ }
131
+ var flowdata = (0, _fortuneCore.getFlowdata)(context);
132
+ if (!flowdata) {
133
+ return;
134
+ }
135
+ var cell = (_a = flowdata === null || flowdata === void 0 ? void 0 : flowdata[rowIndex]) === null || _a === void 0 ? void 0 : _a[colIndex];
136
+ var value = "";
137
+ var overwrite = refs.globalCache.overwriteCell;
138
+ if (cell && !overwrite) {
139
+ if ((0, _fortuneCore.isInlineStringCell)(cell)) {
140
+ value = (0, _fortuneCore.getInlineStringNoStyle)(rowIndex, colIndex, flowdata);
141
+ } else if (cell.f) {
142
+ value = (0, _fortuneCore.getCellValue)(rowIndex, colIndex, flowdata, "f");
143
+ } else {
144
+ value = (0, _fortuneCore.valueShowEs)(rowIndex, colIndex, flowdata);
145
+ }
146
+ }
147
+ refs.globalCache.overwriteCell = false;
148
+ if (!refs.globalCache.ignoreWriteCell && value) {
149
+ fxInput.innerHTML = (0, _fortuneCore.escapeHTMLTag)((0, _fortuneCore.escapeScriptTag)(value));
150
+ } else if (!refs.globalCache.ignoreWriteCell && !overwrite) {
151
+ var valueD = (0, _fortuneCore.getCellValue)(rowIndex, colIndex, flowdata, "f");
152
+ fxInput.innerText = valueD;
153
+ }
154
+ refs.globalCache.ignoreWriteCell = false;
155
+ }, [context.luckysheetCellUpdate, context.luckysheetfile, context.currentSheetId, prevCellUpdate, prevSheetId, refs.fxInput, refs.globalCache]);
156
+ (0, _react.useEffect)(function () {
157
+ if (_lodash.default.isEmpty(context.luckysheetCellUpdate)) {
158
+ delete refs.globalCache.pendingTypeOverCell;
159
+ resetFormulaHistory();
160
+ }
161
+ }, [context.luckysheetCellUpdate, resetFormulaHistory, refs.globalCache]);
162
+ (0, _react.useEffect)(function () {
163
+ var _a;
164
+ if (_lodash.default.isEmpty(context.luckysheetCellUpdate) || !refs.fxInput.current) {
165
+ formulaAnchorCellRef.current = null;
166
+ return;
167
+ }
168
+ var inputText = ((_a = refs.fxInput.current.innerText) === null || _a === void 0 ? void 0 : _a.trim()) || "";
169
+ if (!inputText.startsWith("=")) {
170
+ formulaAnchorCellRef.current = null;
171
+ }
172
+ }, [context.luckysheetCellUpdate, refs.fxInput]);
78
173
  var onFocus = (0, _react.useCallback)(function () {
79
174
  var _a, _b;
80
175
  if (context.allowEdit === false) {
@@ -82,31 +177,55 @@ var FxEditor = function FxEditor() {
82
177
  }
83
178
  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 && (0, _fortuneCore.isAllowEdit)(context, context.luckysheet_select_save)) {
84
179
  setContext(function (draftCtx) {
180
+ var _a;
181
+ (0, _fortuneCore.setFormulaEditorOwner)(draftCtx, "fx");
85
182
  var last = draftCtx.luckysheet_select_save[draftCtx.luckysheet_select_save.length - 1];
86
183
  var row_index = last.row_focus;
87
184
  var col_index = last.column_focus;
88
- draftCtx.luckysheetCellUpdate = [row_index, col_index];
89
- refs.globalCache.doNotFocus = true;
185
+ if (!_lodash.default.isNil(row_index) && !_lodash.default.isNil(col_index)) {
186
+ var flowdata = (0, _fortuneCore.getFlowdata)(draftCtx);
187
+ var cellAt = (_a = flowdata === null || flowdata === void 0 ? void 0 : flowdata[row_index]) === null || _a === void 0 ? void 0 : _a[col_index];
188
+ if ((cellAt === null || cellAt === void 0 ? void 0 : cellAt.f) != null && String(cellAt.f).trim() !== "") {
189
+ (0, _fortuneCore.suppressFormulaRangeSelectionForInitialEdit)(draftCtx);
190
+ }
191
+ }
192
+ var currentUpdate = draftCtx.luckysheetCellUpdate || [];
193
+ var alreadyEditingSameCell = currentUpdate.length === 2 && currentUpdate[0] === row_index && currentUpdate[1] === col_index;
194
+ if (!alreadyEditingSameCell) {
195
+ refs.globalCache.doNotUpdateCell = true;
196
+ draftCtx.luckysheetCellUpdate = [row_index, col_index];
197
+ refs.globalCache.doNotFocus = true;
198
+ }
90
199
  });
91
200
  }
201
+ setContext(function (draftCtx) {
202
+ (0, _fortuneCore.setFormulaEditorOwner)(draftCtx, "fx");
203
+ });
92
204
  }, [context.config, context.luckysheet_select_save, context.luckysheetfile, context.currentSheetId, refs.globalCache, setContext]);
93
205
  var getActiveFormula = (0, _react.useCallback)(function () {
94
206
  return document.querySelector(".luckysheet-formula-search-item-active");
95
207
  }, []);
96
208
  var insertSelectedFormula = (0, _react.useCallback)(function (formulaName) {
97
- 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>");
98
- refs.fxInput.current.innerHTML = ht;
209
+ var fxEditor = refs.fxInput.current;
210
+ if (!fxEditor) return;
99
211
  var cellEditor = document.getElementById("luckysheet-rich-text-editor");
212
+ var _a = (0, _helper.buildFormulaSuggestionText)(fxEditor, formulaName),
213
+ text = _a.text,
214
+ caretOffset = _a.caretOffset;
215
+ var safeText = (0, _fortuneCore.escapeScriptTag)(text);
216
+ var html = safeText.startsWith("=") ? (0, _fortuneCore.functionHTMLGenerate)(safeText) : (0, _fortuneCore.escapeHTMLTag)(safeText);
217
+ fxEditor.innerHTML = html;
100
218
  if (cellEditor) {
101
- cellEditor.innerHTML = ht;
219
+ cellEditor.innerHTML = html;
102
220
  }
103
- (0, _helper.moveCursorToEnd)(refs.fxInput.current);
221
+ (0, _helper.setCursorPosition)(fxEditor, caretOffset);
222
+ setShowSearchHint((0, _helper.shouldShowFormulaFunctionList)(fxEditor));
104
223
  setContext(function (draftCtx) {
105
224
  draftCtx.functionCandidates = [];
106
225
  draftCtx.defaultCandidates = [];
107
- draftCtx.functionHint = formulaName;
226
+ draftCtx.functionHint = (formulaName || "").toUpperCase();
108
227
  });
109
- }, [setContext]);
228
+ }, [refs.fxInput, setContext]);
110
229
  var clearSearchItemActiveClass = (0, _react.useCallback)(function () {
111
230
  var activeFormula = getActiveFormula();
112
231
  if (activeFormula) {
@@ -122,10 +241,11 @@ var FxEditor = function FxEditor() {
122
241
  return lastSpan === null || lastSpan === void 0 ? void 0 : lastSpan.innerText;
123
242
  };
124
243
  var selectActiveFormulaOnClick = (0, _react.useCallback)(function (e) {
125
- var _a, _b, _c;
244
+ var _a, _b, _c, _d;
126
245
  if (e.target.className.includes("sign-fortune")) return;
127
- recentText.current = (_a = refs.fxInput) === null || _a === void 0 ? void 0 : _a.current.innerText;
128
- 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;
246
+ preTextRef.current = (_a = refs.fxInput) === null || _a === void 0 ? void 0 : _a.current.innerText;
247
+ recentText.current = (_b = refs.fxInput) === null || _b === void 0 ? void 0 : _b.current.innerText;
248
+ 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;
129
249
  var lastSpanText = getLastInputSpanText();
130
250
  if (formulaName && !(0, _helper.isLetterNumberPattern)(lastSpanText)) {
131
251
  insertSelectedFormula(formulaName);
@@ -134,18 +254,103 @@ var FxEditor = function FxEditor() {
134
254
  }
135
255
  }, [getActiveFormula, insertSelectedFormula]);
136
256
  var onKeyDown = (0, _react.useCallback)(function (e) {
137
- var _a;
257
+ var _a, _b, _c, _d;
258
+ if (e.key === "Delete" || e.key === "Backspace") {
259
+ var anchor = formulaAnchorCellRef.current;
260
+ if (anchor != null) {
261
+ var anchorRow_1 = anchor[0],
262
+ anchorCol_1 = anchor[1];
263
+ setTimeout(function () {
264
+ setContext(function (draftCtx) {
265
+ draftCtx.luckysheetCellUpdate = [anchorRow_1, anchorCol_1];
266
+ draftCtx.luckysheet_select_save = [{
267
+ row: [anchorRow_1, anchorRow_1],
268
+ column: [anchorCol_1, anchorCol_1],
269
+ row_focus: anchorRow_1,
270
+ column_focus: anchorCol_1
271
+ }];
272
+ (0, _fortuneCore.markRangeSelectionDirty)(draftCtx);
273
+ var el = refs.fxInput.current;
274
+ if (el && el.innerText.trim().startsWith("=")) {
275
+ (0, _fortuneCore.createRangeHightlight)(draftCtx, el.innerHTML);
276
+ (0, _fortuneCore.rangeHightlightselected)(draftCtx, el);
277
+ }
278
+ });
279
+ }, 0);
280
+ }
281
+ }
138
282
  if (context.allowEdit === false) {
139
283
  return;
140
284
  }
285
+ setContext(function (draftCtx) {
286
+ (0, _fortuneCore.setFormulaEditorOwner)(draftCtx, "fx");
287
+ });
141
288
  var currentCommaCount = (0, _helper.countCommasBeforeCursor)((_a = refs.fxInput) === null || _a === void 0 ? void 0 : _a.current);
142
289
  setCommaCount(currentCommaCount);
143
290
  lastKeyDownEventRef.current = new KeyboardEvent(e.type, e.nativeEvent);
144
- var key = e.key;
291
+ capturePreFormulaState();
145
292
  recentText.current = refs.fxInput.current.innerText;
146
- if (key === "ArrowLeft" || key === "ArrowRight") {
293
+ var key = e.key;
294
+ var currentInputText = ((_c = (_b = refs.fxInput.current) === null || _b === void 0 ? void 0 : _b.innerText) === null || _c === void 0 ? void 0 : _c.trim()) || "";
295
+ if ((key === "=" || currentInputText.startsWith("=")) && context.luckysheetCellUpdate.length === 2 && formulaAnchorCellRef.current == null) {
296
+ setContext(function (draftCtx) {
297
+ draftCtx.formulaCache.rangeSelectionActive = null;
298
+ });
299
+ formulaAnchorCellRef.current = [context.luckysheetCellUpdate[0], context.luckysheetCellUpdate[1]];
300
+ }
301
+ if (key === "(" && currentInputText.startsWith("=")) {
302
+ setContext(function (draftCtx) {
303
+ draftCtx.formulaCache.rangeSelectionActive = null;
304
+ });
305
+ }
306
+ if ((0, _formulaSegmentBoundary.isFormulaSegmentBoundaryKey)(key) && context.luckysheetCellUpdate.length > 0 && currentInputText.startsWith("=") && formulaAnchorCellRef.current) {
307
+ setContext(function (draftCtx) {
308
+ draftCtx.formulaCache.rangeSelectionActive = null;
309
+ });
310
+ var _e = formulaAnchorCellRef.current,
311
+ anchorRow_2 = _e[0],
312
+ anchorCol_2 = _e[1];
313
+ setTimeout(function () {
314
+ setContext(function (draftCtx) {
315
+ var _a, _b;
316
+ draftCtx.luckysheetCellUpdate = [anchorRow_2, anchorCol_2];
317
+ draftCtx.luckysheet_select_save = [{
318
+ row: [anchorRow_2, anchorRow_2],
319
+ column: [anchorCol_2, anchorCol_2],
320
+ row_focus: anchorRow_2,
321
+ column_focus: anchorCol_2
322
+ }];
323
+ draftCtx.formulaRangeSelect = undefined;
324
+ draftCtx.formulaCache.selectingRangeIndex = -1;
325
+ draftCtx.formulaCache.func_selectedrange = undefined;
326
+ draftCtx.formulaCache.rangechangeindex = undefined;
327
+ draftCtx.formulaCache.rangestart = false;
328
+ draftCtx.formulaCache.rangedrag_column_start = false;
329
+ draftCtx.formulaCache.rangedrag_row_start = false;
330
+ (0, _fortuneCore.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) || "");
331
+ (0, _fortuneCore.moveHighlightCell)(draftCtx, "down", 0, "rangeOfSelect");
332
+ });
333
+ }, 0);
334
+ }
335
+ var isArrowKey = key === "ArrowUp" || key === "ArrowDown" || key === "ArrowLeft" || key === "ArrowRight";
336
+ var isDirectPlainTypeEdit = context.luckysheetCellUpdate.length > 0 && ((_d = refs.globalCache) === null || _d === void 0 ? void 0 : _d.enteredEditByTyping) === true && !currentInputText.startsWith("=");
337
+ var sheetArrowShortcut = isArrowKey && (e.metaKey || e.ctrlKey || e.shiftKey);
338
+ if ((key === "ArrowLeft" || key === "ArrowRight") && !(isDirectPlainTypeEdit && sheetArrowShortcut)) {
147
339
  e.stopPropagation();
148
340
  }
341
+ if ((e.metaKey || e.ctrlKey) && context.luckysheetCellUpdate.length > 0) {
342
+ if (e.code === "KeyZ" || e.code === "KeyY") {
343
+ var shouldUseFormulaHistory = currentInputText.startsWith("=") || formulaHistoryRef.current.active;
344
+ if (shouldUseFormulaHistory) {
345
+ var handledByFormulaHistory = handleFormulaHistoryUndoRedo(e.code === "KeyY" || e.code === "KeyZ" && e.shiftKey);
346
+ if (handledByFormulaHistory) {
347
+ e.preventDefault();
348
+ }
349
+ }
350
+ e.stopPropagation();
351
+ return;
352
+ }
353
+ }
149
354
  if ((e.key === "Enter" || e.key === "Tab") && context.luckysheetCellUpdate.length > 0) {
150
355
  if (e.altKey || e.metaKey) {
151
356
  document.execCommand("insertHTML", false, "\n ");
@@ -157,7 +362,7 @@ var FxEditor = function FxEditor() {
157
362
  }
158
363
  return;
159
364
  }
160
- if (e.key === "ArrowUp" && context.luckysheetCellUpdate.length > 0) {
365
+ if (!(e.metaKey || e.ctrlKey) && e.key === "ArrowUp" && context.luckysheetCellUpdate.length > 0) {
161
366
  if (document.getElementById("luckysheet-formula-search-c")) {
162
367
  var formulaSearchContainer = document.getElementById("luckysheet-formula-search-c");
163
368
  var activeItem = formulaSearchContainer === null || formulaSearchContainer === void 0 ? void 0 : formulaSearchContainer.querySelector(".luckysheet-formula-search-item-active");
@@ -176,7 +381,7 @@ var FxEditor = function FxEditor() {
176
381
  }
177
382
  }
178
383
  e.preventDefault();
179
- } else if (e.key === "ArrowDown" && context.luckysheetCellUpdate.length > 0) {
384
+ } else if (!(e.metaKey || e.ctrlKey) && e.key === "ArrowDown" && context.luckysheetCellUpdate.length > 0) {
180
385
  if (document.getElementById("luckysheet-formula-search-c")) {
181
386
  var formulaSearchContainer = document.getElementById("luckysheet-formula-search-c");
182
387
  var activeItem = formulaSearchContainer === null || formulaSearchContainer === void 0 ? void 0 : formulaSearchContainer.querySelector(".luckysheet-formula-search-item-active");
@@ -235,7 +440,7 @@ var FxEditor = function FxEditor() {
235
440
  }
236
441
  }
237
442
  });
238
- }, [context.allowEdit, context.luckysheetCellUpdate.length, refs.fxInput, setContext]);
443
+ }, [capturePreFormulaState, context.allowEdit, context.luckysheetCellUpdate, handleFormulaHistoryUndoRedo, refs.cellInput, refs.fxInput, setContext]);
239
444
  var handleHideShowHint = function handleHideShowHint() {
240
445
  var _a, _b, _c, _d;
241
446
  var el = (_a = document.getElementsByClassName("cell-hint")) === null || _a === void 0 ? void 0 : _a[0];
@@ -256,26 +461,54 @@ var FxEditor = function FxEditor() {
256
461
  }
257
462
  };
258
463
  var onChange = (0, _react.useCallback)(function () {
259
- var _a, _b, _c;
464
+ var _a, _b, _c, _d;
260
465
  if (context.isFlvReadOnly) return;
261
- handleHideShowHint();
262
- 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())) {
263
- setShowSearchHint(true);
264
- } else {
265
- setShowSearchHint(false);
466
+ var fxEl = refs.fxInput.current;
467
+ if (fxEl && context.luckysheetCellUpdate.length === 2 && formulaAnchorCellRef.current == null) {
468
+ var t = ((_a = fxEl.innerText) === null || _a === void 0 ? void 0 : _a.trim()) || "";
469
+ if (t.startsWith("=")) {
470
+ formulaAnchorCellRef.current = [context.luckysheetCellUpdate[0], context.luckysheetCellUpdate[1]];
471
+ }
266
472
  }
267
- var currentCommaCount = (0, _helper.countCommasBeforeCursor)((_c = refs.fxInput) === null || _c === void 0 ? void 0 : _c.current);
473
+ handleHideShowHint();
474
+ setShowSearchHint((0, _helper.shouldShowFormulaFunctionList)((_c = (_b = refs.fxInput) === null || _b === void 0 ? void 0 : _b.current) !== null && _c !== void 0 ? _c : null));
475
+ var currentCommaCount = (0, _helper.countCommasBeforeCursor)((_d = refs.fxInput) === null || _d === void 0 ? void 0 : _d.current);
268
476
  setCommaCount(currentCommaCount);
269
477
  var e = lastKeyDownEventRef.current;
270
- if (!e) return;
478
+ if (!e) {
479
+ var fx = refs.fxInput.current;
480
+ var cell = refs.cellInput.current;
481
+ var isFormula = function isFormula(el) {
482
+ var _a, _b;
483
+ 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("=")));
484
+ };
485
+ var sel = window.getSelection();
486
+ var editor_1 = null;
487
+ if (sel === null || sel === void 0 ? void 0 : sel.rangeCount) {
488
+ var node = sel.getRangeAt(0).startContainer;
489
+ 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;
490
+ }
491
+ if (!editor_1 && isFormula(fx)) editor_1 = fx;else if (!editor_1 && isFormula(cell)) editor_1 = cell;
492
+ if (editor_1) {
493
+ setContext(function (draftCtx) {
494
+ if (!(0, _fortuneCore.isAllowEdit)(draftCtx, draftCtx.luckysheet_select_save)) return;
495
+ (0, _fortuneCore.rangeHightlightselected)(draftCtx, editor_1);
496
+ });
497
+ }
498
+ return;
499
+ }
271
500
  var kcode = e.keyCode;
272
501
  if (!kcode) return;
502
+ appendFormulaHistoryFromPrimaryEditor(function () {
503
+ return (0, _helper.getCursorPosition)(refs.fxInput.current);
504
+ });
273
505
  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) {
274
506
  setContext(function (draftCtx) {
275
507
  (0, _fortuneCore.handleFormulaInput)(draftCtx, refs.cellInput.current, refs.fxInput.current, kcode, recentText.current);
276
508
  });
277
509
  }
278
- }, [refs.cellInput, refs.fxInput, setContext]);
510
+ }, [appendFormulaHistoryFromPrimaryEditor, context.isFlvReadOnly, context.luckysheetCellUpdate, refs.cellInput, refs.fxInput, setContext]);
511
+ (0, _useRerenderOnFormulaCaret.useRerenderOnFormulaCaret)(refs.fxInput, context.luckysheetCellUpdate.length > 0);
279
512
  var getFunctionNameFromInput = (0, _react.useCallback)(function () {
280
513
  var _a, _b, _c, _d;
281
514
  var inputText = ((_b = (_a = refs.fxInput) === null || _a === void 0 ? void 0 : _a.current) === null || _b === void 0 ? void 0 : _b.innerText) || "";
@@ -292,8 +525,9 @@ var FxEditor = function FxEditor() {
292
525
  }
293
526
  return null;
294
527
  }, []);
295
- var functionName = context.functionHint || getFunctionNameFromInput();
528
+ var functionName = (_c = (_b = (0, _helper.getFunctionNameFromFormulaCaretSpans)(refs.fxInput.current)) !== null && _b !== void 0 ? _b : context.functionHint) !== null && _c !== void 0 ? _c : getFunctionNameFromInput();
296
529
  var fn = functionName ? context.formulaCache.functionlistMap[functionName] : null;
530
+ var showFxFormulaChrome = context.luckysheetCellUpdate.length > 0 && (0, _fortuneCore.getFormulaEditorOwner)(context) === "fx";
297
531
  var allowEdit = (0, _react.useMemo)(function () {
298
532
  if (context.allowEdit === false) {
299
533
  return false;
@@ -307,9 +541,9 @@ var FxEditor = function FxEditor() {
307
541
  return true;
308
542
  }, [context.config, context.luckysheet_select_save, context.luckysheetfile, context.currentSheetId, isHidenRC]);
309
543
  var divRef = (0, _react.useRef)(null);
310
- var _g = (0, _react.useState)(false),
311
- isResizing = _g[0],
312
- setIsResizing = _g[1];
544
+ var _k = (0, _react.useState)(false),
545
+ isResizing = _k[0],
546
+ setIsResizing = _k[1];
313
547
  var startResize = function startResize(e) {
314
548
  e.preventDefault();
315
549
  setIsResizing(true);
@@ -328,7 +562,7 @@ var FxEditor = function FxEditor() {
328
562
  document.addEventListener("mouseup", _onMouseUp);
329
563
  };
330
564
  return /*#__PURE__*/_react.default.createElement("div", null, /*#__PURE__*/_react.default.createElement("div", {
331
- className: "fortune-fx-editor",
565
+ className: showFxFormulaChrome ? "fortune-fx-editor fortune-fx-editor--formula-owner" : "fortune-fx-editor",
332
566
  ref: divRef
333
567
  }, /*#__PURE__*/_react.default.createElement(_NameBox.default, null), /*#__PURE__*/_react.default.createElement("div", {
334
568
  className: "fortune-fx-icon",
@@ -355,8 +589,20 @@ var FxEditor = function FxEditor() {
355
589
  onMouseUp: function onMouseUp() {
356
590
  var _a;
357
591
  handleHideShowHint();
358
- var currentCommaCount = (0, _helper.countCommasBeforeCursor)((_a = refs.fxInput) === null || _a === void 0 ? void 0 : _a.current);
592
+ setContext(function (draftCtx) {
593
+ (0, _fortuneCore.setFormulaEditorOwner)(draftCtx, "fx");
594
+ });
595
+ var editor = (_a = refs.fxInput) === null || _a === void 0 ? void 0 : _a.current;
596
+ var currentCommaCount = (0, _helper.countCommasBeforeCursor)(editor);
359
597
  setCommaCount(currentCommaCount);
598
+ setContext(function (draftCtx) {
599
+ if (draftCtx.formulaCache.rangeSelectionActive !== true) return;
600
+ var clickedInsideManagedRange = (0, _fortuneCore.getFormulaRangeIndexAtCaret)(editor) !== null;
601
+ var atValidInsertionPoint = (0, _fortuneCore.isCaretAtValidFormulaRangeInsertionPoint)(editor);
602
+ if (clickedInsideManagedRange || !atValidInsertionPoint) {
603
+ (0, _fortuneCore.markRangeSelectionDirty)(draftCtx);
604
+ }
605
+ });
360
606
  },
361
607
  innerRef: function innerRef(e) {
362
608
  refs.fxInput.current = e;
@@ -369,7 +615,7 @@ var FxEditor = function FxEditor() {
369
615
  onChange: onChange,
370
616
  tabIndex: 0,
371
617
  allowEdit: allowEdit && !context.isFlvReadOnly
372
- }), showSearchHint && (/*#__PURE__*/_react.default.createElement(_FormulaSearch.default, {
618
+ }), (context.functionCandidates.length > 0 || context.functionHint || context.defaultCandidates.length > 0 || fn) && showFxFormulaChrome && (/*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, showSearchHint && (/*#__PURE__*/_react.default.createElement(_FormulaSearch.default, {
373
619
  from: "fx",
374
620
  onMouseMove: function onMouseMove(e) {
375
621
  if (document.getElementById("luckysheet-formula-search-c")) {
@@ -385,12 +631,22 @@ var FxEditor = function FxEditor() {
385
631
  }
386
632
  })), /*#__PURE__*/_react.default.createElement("div", {
387
633
  className: "fx-hint"
388
- }, showFormulaHint && fn && (/*#__PURE__*/_react.default.createElement(_FormulaHint.default, {
634
+ }, showFormulaHint && fn && !showSearchHint && (/*#__PURE__*/_react.default.createElement(_FormulaHint.default, {
389
635
  handleShowFormulaHint: handleShowFormulaHint,
390
636
  showFormulaHint: showFormulaHint,
391
637
  commaCount: commaCount,
392
638
  functionName: functionName
393
- })), !showFormulaHint && fn && (/*#__PURE__*/_react.default.createElement("div", {
639
+ })), !showFormulaHint && fn && !showSearchHint && (/*#__PURE__*/_react.default.createElement(_ui.Tooltip, {
640
+ text: "Turn on formula suggestions (F10)",
641
+ placement: "top",
642
+ defaultOpen: true,
643
+ style: {
644
+ position: "absolute",
645
+ top: "-50px",
646
+ left: "-130px",
647
+ width: "210px"
648
+ }
649
+ }, /*#__PURE__*/_react.default.createElement("div", {
394
650
  className: "luckysheet-hin absolute show-more-btn",
395
651
  onClick: function onClick() {
396
652
  handleShowFormulaHint();
@@ -404,7 +660,7 @@ var FxEditor = function FxEditor() {
404
660
  margin: "auto",
405
661
  marginTop: "1px"
406
662
  }
407
- })))))), /*#__PURE__*/_react.default.createElement("div", {
663
+ }))))))))), /*#__PURE__*/_react.default.createElement("div", {
408
664
  className: "resize-handle",
409
665
  onMouseDown: startResize,
410
666
  style: {
@@ -58,29 +58,49 @@ var FormulaHint = function FormulaHint(props) {
58
58
  ETHERSCAN_API_KEY: "Etherscan API key"
59
59
  };
60
60
  var hintRef = (0, _react.useRef)(null);
61
- var _h = (0, _react.useState)(0),
61
+ var cellHeightPx = (firstSelection === null || firstSelection === void 0 ? void 0 : firstSelection.height_move) != null ? Number(firstSelection.height_move) : 24;
62
+ var belowCellTop = cellHeightPx + 4;
63
+ var _h = (0, _react.useState)(belowCellTop),
62
64
  top = _h[0],
63
65
  setTop = _h[1];
64
66
  var _j = (0, _react.useState)(false),
65
67
  showDelayedHint = _j[0],
66
68
  setShowDelayedHint = _j[1];
67
- var calcuatePopUpPlacement = function calcuatePopUpPlacement() {
68
- var _a, _b, _c, _d, _e, _f;
69
- 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;
70
- var hintHeight = 360;
71
- var inputBoxTop = parseInt(((_c = document.getElementById("luckysheet-input-box")) === null || _c === void 0 ? void 0 : _c.style.top) || "0", 10) - 85;
72
- var inputBottom = inputBoxTop + firstSelection.height_move;
73
- var availableBelow = window.innerHeight - inputBottom;
74
- var hintAbove = hintHeight > availableBelow;
75
- var selectionHeight = (firstSelection === null || firstSelection === void 0 ? void 0 : firstSelection.height_move) || 0;
76
- var divOffset = ((_d = hintRef.current) === null || _d === void 0 ? void 0 : _d.offsetHeight) || 0;
77
- var topV = hintAbove ? selectionHeight - (divOffset + 30) : selectionHeight + 4;
78
- var el = (_e = document.getElementsByClassName("fx-hint")) === null || _e === void 0 ? void 0 : _e[0];
79
- if (el && ((_f = el === null || el === void 0 ? void 0 : el.style) === null || _f === void 0 ? void 0 : _f.display) !== "none") {
69
+ var measureFormulaHintPlacement = (0, _react.useCallback)(function () {
70
+ var _a;
71
+ if ((firstSelection === null || firstSelection === void 0 ? void 0 : firstSelection.top) == null || firstSelection.height_move == null) {
72
+ setTop(belowCellTop);
73
+ return;
74
+ }
75
+ var cellH = Number(firstSelection.height_move) || cellHeightPx;
76
+ var innerEl = hintRef.current;
77
+ var popupHeight = Math.min((innerEl === null || innerEl === void 0 ? void 0 : innerEl.offsetHeight) || 360, 360);
78
+ var inputBox = document.getElementById("luckysheet-input-box");
79
+ var rect = inputBox === null || inputBox === void 0 ? void 0 : inputBox.getBoundingClientRect();
80
+ if (!rect) {
81
+ setTop(cellH + 4);
82
+ return;
83
+ }
84
+ var cellBottomViewport = rect.top + cellH;
85
+ var availableBelow = window.innerHeight - cellBottomViewport - 12;
86
+ var preferBelow = popupHeight <= availableBelow;
87
+ var topV = preferBelow ? cellH + 4 : -(popupHeight + 8);
88
+ var fxHint = (_a = document.getElementsByClassName("fx-hint")) === null || _a === void 0 ? void 0 : _a[0];
89
+ if (fxHint && fxHint.style.display !== "none") {
80
90
  topV = 25;
81
91
  }
82
92
  setTop(topV);
83
- };
93
+ }, [belowCellTop, cellHeightPx, firstSelection === null || firstSelection === void 0 ? void 0 : firstSelection.height_move, firstSelection === null || firstSelection === void 0 ? void 0 : firstSelection.top]);
94
+ (0, _react.useLayoutEffect)(function () {
95
+ if (!fn) {
96
+ return undefined;
97
+ }
98
+ measureFormulaHintPlacement();
99
+ var id = requestAnimationFrame(measureFormulaHintPlacement);
100
+ return function () {
101
+ cancelAnimationFrame(id);
102
+ };
103
+ }, [functionName, context.functionHint, measureFormulaHintPlacement, showFormulaHint, commaCount, showFunctionBody]);
84
104
  var hexToRgbString = function hexToRgbString(hex) {
85
105
  hex = hex.replace("#", "");
86
106
  var r = parseInt(hex.substring(0, 2), 16);
@@ -93,14 +113,13 @@ var FormulaHint = function FormulaHint(props) {
93
113
  return bg;
94
114
  };
95
115
  (0, _react.useEffect)(function () {
96
- calcuatePopUpPlacement();
97
- }, []);
98
- (0, _react.useEffect)(function () {
99
- if (!top) {
100
- setTimeout(function () {
101
- setShowDelayedHint(true);
102
- }, 40);
103
- }
116
+ setShowDelayedHint(false);
117
+ var t = setTimeout(function () {
118
+ return setShowDelayedHint(true);
119
+ }, 40);
120
+ return function () {
121
+ return clearTimeout(t);
122
+ };
104
123
  }, [top]);
105
124
  if (!fn) return null;
106
125
  var fnNameClass = fn.n ? String(fn.n).replace(/[^a-zA-Z0-9-]/g, "-").replace(/-+/g, "-") : "";
@@ -183,7 +202,7 @@ var FormulaHint = function FormulaHint(props) {
183
202
  localStorage.setItem("formula-expand", "".concat(!showFunctionBody));
184
203
  setShouldShowFunctionBody(!showFunctionBody);
185
204
  setTimeout(function () {
186
- calcuatePopUpPlacement();
205
+ measureFormulaHintPlacement();
187
206
  }, 50);
188
207
  }
189
208
  dragHasMoved.current = false;
@@ -286,6 +305,7 @@ var FormulaHint = function FormulaHint(props) {
286
305
  cursor: "auto"
287
306
  }
288
307
  }, fn.API_KEY && (/*#__PURE__*/_react.default.createElement("div", {
308
+ className: "luckysheet-formula-help-content-api",
289
309
  style: {
290
310
  borderLeft: "4px solid ".concat(isKeyAdded ? "#177E23" : "#fb923c"),
291
311
  backgroundColor: "white",
@@ -349,6 +369,7 @@ var FormulaHint = function FormulaHint(props) {
349
369
  className: "fortune-formula-hint__cta min-w-[80px]",
350
370
  "data-testid": "formula-hint-cta-ok"
351
371
  }, "Ok"))))))), /*#__PURE__*/_react.default.createElement("div", {
372
+ id: "luckysheet-formula-help-content-example",
352
373
  style: {
353
374
  backgroundColor: "white",
354
375
  padding: "6px",