@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
@@ -9,48 +9,75 @@ 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");
20
23
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
21
24
  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
25
  var FxEditor = function FxEditor() {
23
- var _a;
26
+ var _a, _b, _c;
24
27
  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;
28
+ var _d = (0, _react.useState)(false),
29
+ showSearchHint = _d[0],
30
+ setShowSearchHint = _d[1];
31
+ var _e = (0, _react.useState)(!hideFormulaHintLocal),
32
+ showFormulaHint = _e[0],
33
+ setShowFormulaHint = _e[1];
34
+ var _f = (0, _react.useState)(0),
35
+ commaCount = _f[0],
36
+ setCommaCount = _f[1];
37
+ var _g = (0, _react.useContext)(_context.default),
38
+ context = _g.context,
39
+ setContext = _g.setContext,
40
+ refs = _g.refs;
38
41
  var lastKeyDownEventRef = (0, _react.useRef)(null);
42
+ var _h = (0, _useFormulaEditorHistory.useFormulaEditorHistory)(refs.fxInput, refs.cellInput, refs.fxInput, setContext, "fx"),
43
+ formulaHistoryRef = _h.formulaHistoryRef,
44
+ preTextRef = _h.preTextRef,
45
+ resetFormulaHistory = _h.resetFormulaHistory,
46
+ handleFormulaHistoryUndoRedo = _h.handleFormulaHistoryUndoRedo,
47
+ capturePreFormulaState = _h.capturePreFormulaState,
48
+ appendFormulaHistoryFromPrimaryEditor = _h.appendFormulaHistoryFromPrimaryEditor;
39
49
  var inputContainerRef = (0, _react.useRef)(null);
40
- var _f = (0, _react.useState)(false),
41
- isHidenRC = _f[0],
42
- setIsHidenRC = _f[1];
50
+ var _j = (0, _react.useState)(false),
51
+ isHidenRC = _j[0],
52
+ setIsHidenRC = _j[1];
43
53
  var firstSelection = (_a = context.luckysheet_select_save) === null || _a === void 0 ? void 0 : _a[0];
44
54
  var prevFirstSelection = (0, _usePrevious.default)(firstSelection);
55
+ var prevCellUpdate = (0, _usePrevious.default)(context.luckysheetCellUpdate);
45
56
  var prevSheetId = (0, _usePrevious.default)(context.currentSheetId);
46
57
  var recentText = (0, _react.useRef)("");
58
+ var formulaAnchorCellRef = (0, _react.useRef)(null);
47
59
  var handleShowFormulaHint = function handleShowFormulaHint() {
48
60
  localStorage.setItem("formulaMore", String(showFormulaHint));
49
61
  setShowFormulaHint(!showFormulaHint);
50
62
  };
63
+ (0, _react.useEffect)(function () {
64
+ var handleKeyDown = function handleKeyDown(event) {
65
+ if (event.key === "F10") {
66
+ event.preventDefault();
67
+ handleShowFormulaHint();
68
+ }
69
+ };
70
+ window.addEventListener("keydown", handleKeyDown);
71
+ return function () {
72
+ return window.removeEventListener("keydown", handleKeyDown);
73
+ };
74
+ }, [showFormulaHint]);
51
75
  (0, _react.useEffect)(function () {
52
76
  var _a;
53
77
  setIsHidenRC((0, _fortuneCore.isShowHidenCR)(context));
78
+ if (context.luckysheetCellUpdate.length > 0) {
79
+ return;
80
+ }
54
81
  if (_lodash.default.isEqual(prevFirstSelection, firstSelection) && context.currentSheetId === prevSheetId) {
55
82
  return;
56
83
  }
@@ -74,7 +101,74 @@ var FxEditor = function FxEditor() {
74
101
  } else {
75
102
  refs.fxInput.current.innerHTML = "";
76
103
  }
77
- }, [context.luckysheetfile, context.currentSheetId, context.luckysheet_select_save]);
104
+ }, [context.luckysheetfile, context.currentSheetId, context.luckysheet_select_save, context.luckysheetCellUpdate.length]);
105
+ (0, _react.useLayoutEffect)(function () {
106
+ var _a;
107
+ var fxInput = refs.fxInput.current;
108
+ if (context.luckysheetCellUpdate.length === 0 || !fxInput) {
109
+ return;
110
+ }
111
+ if (refs.globalCache.doNotUpdateCell) {
112
+ delete refs.globalCache.doNotUpdateCell;
113
+ return;
114
+ }
115
+ if (_lodash.default.isEqual(prevCellUpdate, context.luckysheetCellUpdate) && prevSheetId === context.currentSheetId) {
116
+ return;
117
+ }
118
+ var _b = context.luckysheetCellUpdate,
119
+ rowIndex = _b[0],
120
+ colIndex = _b[1];
121
+ if (_lodash.default.isNil(rowIndex) || _lodash.default.isNil(colIndex)) {
122
+ return;
123
+ }
124
+ var pending = refs.globalCache.pendingTypeOverCell;
125
+ if (pending && pending[0] === rowIndex && pending[1] === colIndex) {
126
+ refs.globalCache.overwriteCell = false;
127
+ refs.globalCache.ignoreWriteCell = false;
128
+ return;
129
+ }
130
+ var flowdata = (0, _fortuneCore.getFlowdata)(context);
131
+ if (!flowdata) {
132
+ return;
133
+ }
134
+ var cell = (_a = flowdata === null || flowdata === void 0 ? void 0 : flowdata[rowIndex]) === null || _a === void 0 ? void 0 : _a[colIndex];
135
+ var value = "";
136
+ var overwrite = refs.globalCache.overwriteCell;
137
+ if (cell && !overwrite) {
138
+ if ((0, _fortuneCore.isInlineStringCell)(cell)) {
139
+ value = (0, _fortuneCore.getInlineStringNoStyle)(rowIndex, colIndex, flowdata);
140
+ } else if (cell.f) {
141
+ value = (0, _fortuneCore.getCellValue)(rowIndex, colIndex, flowdata, "f");
142
+ } else {
143
+ value = (0, _fortuneCore.valueShowEs)(rowIndex, colIndex, flowdata);
144
+ }
145
+ }
146
+ refs.globalCache.overwriteCell = false;
147
+ if (!refs.globalCache.ignoreWriteCell && value) {
148
+ fxInput.innerHTML = (0, _fortuneCore.escapeHTMLTag)((0, _fortuneCore.escapeScriptTag)(value));
149
+ } else if (!refs.globalCache.ignoreWriteCell && !overwrite) {
150
+ var valueD = (0, _fortuneCore.getCellValue)(rowIndex, colIndex, flowdata, "f");
151
+ fxInput.innerText = valueD;
152
+ }
153
+ refs.globalCache.ignoreWriteCell = false;
154
+ }, [context.luckysheetCellUpdate, context.luckysheetfile, context.currentSheetId, prevCellUpdate, prevSheetId, refs.fxInput, refs.globalCache]);
155
+ (0, _react.useEffect)(function () {
156
+ if (_lodash.default.isEmpty(context.luckysheetCellUpdate)) {
157
+ delete refs.globalCache.pendingTypeOverCell;
158
+ resetFormulaHistory();
159
+ }
160
+ }, [context.luckysheetCellUpdate, resetFormulaHistory, refs.globalCache]);
161
+ (0, _react.useEffect)(function () {
162
+ var _a;
163
+ if (_lodash.default.isEmpty(context.luckysheetCellUpdate) || !refs.fxInput.current) {
164
+ formulaAnchorCellRef.current = null;
165
+ return;
166
+ }
167
+ var inputText = ((_a = refs.fxInput.current.innerText) === null || _a === void 0 ? void 0 : _a.trim()) || "";
168
+ if (!inputText.startsWith("=")) {
169
+ formulaAnchorCellRef.current = null;
170
+ }
171
+ }, [context.luckysheetCellUpdate, refs.fxInput]);
78
172
  var onFocus = (0, _react.useCallback)(function () {
79
173
  var _a, _b;
80
174
  if (context.allowEdit === false) {
@@ -82,13 +176,30 @@ var FxEditor = function FxEditor() {
82
176
  }
83
177
  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
178
  setContext(function (draftCtx) {
179
+ var _a;
180
+ (0, _fortuneCore.setFormulaEditorOwner)(draftCtx, "fx");
85
181
  var last = draftCtx.luckysheet_select_save[draftCtx.luckysheet_select_save.length - 1];
86
182
  var row_index = last.row_focus;
87
183
  var col_index = last.column_focus;
88
- draftCtx.luckysheetCellUpdate = [row_index, col_index];
89
- refs.globalCache.doNotFocus = true;
184
+ if (!_lodash.default.isNil(row_index) && !_lodash.default.isNil(col_index)) {
185
+ var flowdata = (0, _fortuneCore.getFlowdata)(draftCtx);
186
+ var cellAt = (_a = flowdata === null || flowdata === void 0 ? void 0 : flowdata[row_index]) === null || _a === void 0 ? void 0 : _a[col_index];
187
+ if ((cellAt === null || cellAt === void 0 ? void 0 : cellAt.f) != null && String(cellAt.f).trim() !== "") {
188
+ (0, _fortuneCore.suppressFormulaRangeSelectionForInitialEdit)(draftCtx);
189
+ }
190
+ }
191
+ var currentUpdate = draftCtx.luckysheetCellUpdate || [];
192
+ var alreadyEditingSameCell = currentUpdate.length === 2 && currentUpdate[0] === row_index && currentUpdate[1] === col_index;
193
+ if (!alreadyEditingSameCell) {
194
+ refs.globalCache.doNotUpdateCell = true;
195
+ draftCtx.luckysheetCellUpdate = [row_index, col_index];
196
+ refs.globalCache.doNotFocus = true;
197
+ }
90
198
  });
91
199
  }
200
+ setContext(function (draftCtx) {
201
+ (0, _fortuneCore.setFormulaEditorOwner)(draftCtx, "fx");
202
+ });
92
203
  }, [context.config, context.luckysheet_select_save, context.luckysheetfile, context.currentSheetId, refs.globalCache, setContext]);
93
204
  var getActiveFormula = (0, _react.useCallback)(function () {
94
205
  return document.querySelector(".luckysheet-formula-search-item-active");
@@ -107,12 +218,13 @@ var FxEditor = function FxEditor() {
107
218
  cellEditor.innerHTML = html;
108
219
  }
109
220
  (0, _helper.setCursorPosition)(fxEditor, caretOffset);
221
+ setShowSearchHint((0, _helper.shouldShowFormulaFunctionList)(fxEditor));
110
222
  setContext(function (draftCtx) {
111
223
  draftCtx.functionCandidates = [];
112
224
  draftCtx.defaultCandidates = [];
113
- draftCtx.functionHint = formulaName;
225
+ draftCtx.functionHint = (formulaName || "").toUpperCase();
114
226
  });
115
- }, [setContext]);
227
+ }, [refs.fxInput, setContext]);
116
228
  var clearSearchItemActiveClass = (0, _react.useCallback)(function () {
117
229
  var activeFormula = getActiveFormula();
118
230
  if (activeFormula) {
@@ -128,10 +240,11 @@ var FxEditor = function FxEditor() {
128
240
  return lastSpan === null || lastSpan === void 0 ? void 0 : lastSpan.innerText;
129
241
  };
130
242
  var selectActiveFormulaOnClick = (0, _react.useCallback)(function (e) {
131
- var _a, _b, _c;
243
+ var _a, _b, _c, _d;
132
244
  if (e.target.className.includes("sign-fortune")) return;
133
- recentText.current = (_a = refs.fxInput) === null || _a === void 0 ? void 0 : _a.current.innerText;
134
- 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;
245
+ preTextRef.current = (_a = refs.fxInput) === null || _a === void 0 ? void 0 : _a.current.innerText;
246
+ recentText.current = (_b = refs.fxInput) === null || _b === void 0 ? void 0 : _b.current.innerText;
247
+ 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;
135
248
  var lastSpanText = getLastInputSpanText();
136
249
  if (formulaName && !(0, _helper.isLetterNumberPattern)(lastSpanText)) {
137
250
  insertSelectedFormula(formulaName);
@@ -140,18 +253,95 @@ var FxEditor = function FxEditor() {
140
253
  }
141
254
  }, [getActiveFormula, insertSelectedFormula]);
142
255
  var onKeyDown = (0, _react.useCallback)(function (e) {
143
- var _a;
256
+ var _a, _b, _c;
257
+ if (e.key === "Delete" || e.key === "Backspace") {
258
+ var anchor = formulaAnchorCellRef.current;
259
+ if (anchor != null) {
260
+ var anchorRow_1 = anchor[0],
261
+ anchorCol_1 = anchor[1];
262
+ setTimeout(function () {
263
+ setContext(function (draftCtx) {
264
+ draftCtx.luckysheetCellUpdate = [anchorRow_1, anchorCol_1];
265
+ draftCtx.luckysheet_select_save = [{
266
+ row: [anchorRow_1, anchorRow_1],
267
+ column: [anchorCol_1, anchorCol_1],
268
+ row_focus: anchorRow_1,
269
+ column_focus: anchorCol_1
270
+ }];
271
+ (0, _fortuneCore.markRangeSelectionDirty)(draftCtx);
272
+ });
273
+ }, 0);
274
+ }
275
+ }
144
276
  if (context.allowEdit === false) {
145
277
  return;
146
278
  }
279
+ setContext(function (draftCtx) {
280
+ (0, _fortuneCore.setFormulaEditorOwner)(draftCtx, "fx");
281
+ });
147
282
  var currentCommaCount = (0, _helper.countCommasBeforeCursor)((_a = refs.fxInput) === null || _a === void 0 ? void 0 : _a.current);
148
283
  setCommaCount(currentCommaCount);
149
284
  lastKeyDownEventRef.current = new KeyboardEvent(e.type, e.nativeEvent);
150
- var key = e.key;
285
+ capturePreFormulaState();
151
286
  recentText.current = refs.fxInput.current.innerText;
287
+ var key = e.key;
288
+ var currentInputText = ((_c = (_b = refs.fxInput.current) === null || _b === void 0 ? void 0 : _b.innerText) === null || _c === void 0 ? void 0 : _c.trim()) || "";
289
+ if ((key === "=" || currentInputText.startsWith("=")) && context.luckysheetCellUpdate.length === 2 && formulaAnchorCellRef.current == null) {
290
+ setContext(function (draftCtx) {
291
+ draftCtx.formulaCache.rangeSelectionActive = null;
292
+ });
293
+ formulaAnchorCellRef.current = [context.luckysheetCellUpdate[0], context.luckysheetCellUpdate[1]];
294
+ }
295
+ if (key === "(" && currentInputText.startsWith("=")) {
296
+ setContext(function (draftCtx) {
297
+ draftCtx.formulaCache.rangeSelectionActive = null;
298
+ });
299
+ }
300
+ if (key === "," && context.luckysheetCellUpdate.length > 0 && currentInputText.startsWith("=") && formulaAnchorCellRef.current) {
301
+ setContext(function (draftCtx) {
302
+ draftCtx.formulaCache.rangeSelectionActive = null;
303
+ });
304
+ var _d = formulaAnchorCellRef.current,
305
+ anchorRow_2 = _d[0],
306
+ anchorCol_2 = _d[1];
307
+ setTimeout(function () {
308
+ setContext(function (draftCtx) {
309
+ var _a, _b;
310
+ draftCtx.luckysheetCellUpdate = [anchorRow_2, anchorCol_2];
311
+ draftCtx.luckysheet_select_save = [{
312
+ row: [anchorRow_2, anchorRow_2],
313
+ column: [anchorCol_2, anchorCol_2],
314
+ row_focus: anchorRow_2,
315
+ column_focus: anchorCol_2
316
+ }];
317
+ draftCtx.formulaRangeSelect = undefined;
318
+ draftCtx.formulaCache.selectingRangeIndex = -1;
319
+ draftCtx.formulaCache.func_selectedrange = undefined;
320
+ draftCtx.formulaCache.rangechangeindex = undefined;
321
+ draftCtx.formulaCache.rangestart = false;
322
+ draftCtx.formulaCache.rangedrag_column_start = false;
323
+ draftCtx.formulaCache.rangedrag_row_start = false;
324
+ (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) || "");
325
+ (0, _fortuneCore.moveHighlightCell)(draftCtx, "down", 0, "rangeOfSelect");
326
+ });
327
+ }, 0);
328
+ }
152
329
  if (key === "ArrowLeft" || key === "ArrowRight") {
153
330
  e.stopPropagation();
154
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
+ }
155
345
  if ((e.key === "Enter" || e.key === "Tab") && context.luckysheetCellUpdate.length > 0) {
156
346
  if (e.altKey || e.metaKey) {
157
347
  document.execCommand("insertHTML", false, "\n ");
@@ -163,7 +353,7 @@ var FxEditor = function FxEditor() {
163
353
  }
164
354
  return;
165
355
  }
166
- if (e.key === "ArrowUp" && context.luckysheetCellUpdate.length > 0) {
356
+ if (!(e.metaKey || e.ctrlKey) && e.key === "ArrowUp" && context.luckysheetCellUpdate.length > 0) {
167
357
  if (document.getElementById("luckysheet-formula-search-c")) {
168
358
  var formulaSearchContainer = document.getElementById("luckysheet-formula-search-c");
169
359
  var activeItem = formulaSearchContainer === null || formulaSearchContainer === void 0 ? void 0 : formulaSearchContainer.querySelector(".luckysheet-formula-search-item-active");
@@ -182,7 +372,7 @@ var FxEditor = function FxEditor() {
182
372
  }
183
373
  }
184
374
  e.preventDefault();
185
- } else if (e.key === "ArrowDown" && context.luckysheetCellUpdate.length > 0) {
375
+ } else if (!(e.metaKey || e.ctrlKey) && e.key === "ArrowDown" && context.luckysheetCellUpdate.length > 0) {
186
376
  if (document.getElementById("luckysheet-formula-search-c")) {
187
377
  var formulaSearchContainer = document.getElementById("luckysheet-formula-search-c");
188
378
  var activeItem = formulaSearchContainer === null || formulaSearchContainer === void 0 ? void 0 : formulaSearchContainer.querySelector(".luckysheet-formula-search-item-active");
@@ -241,7 +431,7 @@ var FxEditor = function FxEditor() {
241
431
  }
242
432
  }
243
433
  });
244
- }, [context.allowEdit, context.luckysheetCellUpdate.length, refs.fxInput, setContext]);
434
+ }, [capturePreFormulaState, context.allowEdit, context.luckysheetCellUpdate, handleFormulaHistoryUndoRedo, refs.cellInput, refs.fxInput, setContext]);
245
435
  var handleHideShowHint = function handleHideShowHint() {
246
436
  var _a, _b, _c, _d;
247
437
  var el = (_a = document.getElementsByClassName("cell-hint")) === null || _a === void 0 ? void 0 : _a[0];
@@ -262,26 +452,54 @@ var FxEditor = function FxEditor() {
262
452
  }
263
453
  };
264
454
  var onChange = (0, _react.useCallback)(function () {
265
- var _a, _b, _c;
455
+ var _a, _b, _c, _d;
266
456
  if (context.isFlvReadOnly) return;
267
- handleHideShowHint();
268
- 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())) {
269
- setShowSearchHint(true);
270
- } else {
271
- 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
+ }
272
463
  }
273
- var currentCommaCount = (0, _helper.countCommasBeforeCursor)((_c = refs.fxInput) === null || _c === void 0 ? void 0 : _c.current);
464
+ handleHideShowHint();
465
+ setShowSearchHint((0, _helper.shouldShowFormulaFunctionList)((_c = (_b = refs.fxInput) === null || _b === void 0 ? void 0 : _b.current) !== null && _c !== void 0 ? _c : null));
466
+ var currentCommaCount = (0, _helper.countCommasBeforeCursor)((_d = refs.fxInput) === null || _d === void 0 ? void 0 : _d.current);
274
467
  setCommaCount(currentCommaCount);
275
468
  var e = lastKeyDownEventRef.current;
276
- 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 (!(0, _fortuneCore.isAllowEdit)(draftCtx, draftCtx.luckysheet_select_save)) return;
486
+ (0, _fortuneCore.rangeHightlightselected)(draftCtx, editor_1);
487
+ });
488
+ }
489
+ return;
490
+ }
277
491
  var kcode = e.keyCode;
278
492
  if (!kcode) return;
493
+ appendFormulaHistoryFromPrimaryEditor(function () {
494
+ return (0, _helper.getCursorPosition)(refs.fxInput.current);
495
+ });
279
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) {
280
497
  setContext(function (draftCtx) {
281
498
  (0, _fortuneCore.handleFormulaInput)(draftCtx, refs.cellInput.current, refs.fxInput.current, kcode, recentText.current);
282
499
  });
283
500
  }
284
- }, [refs.cellInput, refs.fxInput, setContext]);
501
+ }, [appendFormulaHistoryFromPrimaryEditor, context.isFlvReadOnly, context.luckysheetCellUpdate, refs.cellInput, refs.fxInput, setContext]);
502
+ (0, _useRerenderOnFormulaCaret.useRerenderOnFormulaCaret)(refs.fxInput, context.luckysheetCellUpdate.length > 0);
285
503
  var getFunctionNameFromInput = (0, _react.useCallback)(function () {
286
504
  var _a, _b, _c, _d;
287
505
  var inputText = ((_b = (_a = refs.fxInput) === null || _a === void 0 ? void 0 : _a.current) === null || _b === void 0 ? void 0 : _b.innerText) || "";
@@ -298,8 +516,9 @@ var FxEditor = function FxEditor() {
298
516
  }
299
517
  return null;
300
518
  }, []);
301
- var functionName = context.functionHint || getFunctionNameFromInput();
519
+ var functionName = (_c = (_b = (0, _helper.getFunctionNameFromFormulaCaretSpans)(refs.fxInput.current)) !== null && _b !== void 0 ? _b : context.functionHint) !== null && _c !== void 0 ? _c : getFunctionNameFromInput();
302
520
  var fn = functionName ? context.formulaCache.functionlistMap[functionName] : null;
521
+ var showFxFormulaChrome = context.luckysheetCellUpdate.length > 0 && (0, _fortuneCore.getFormulaEditorOwner)(context) === "fx";
303
522
  var allowEdit = (0, _react.useMemo)(function () {
304
523
  if (context.allowEdit === false) {
305
524
  return false;
@@ -313,9 +532,9 @@ var FxEditor = function FxEditor() {
313
532
  return true;
314
533
  }, [context.config, context.luckysheet_select_save, context.luckysheetfile, context.currentSheetId, isHidenRC]);
315
534
  var divRef = (0, _react.useRef)(null);
316
- var _g = (0, _react.useState)(false),
317
- isResizing = _g[0],
318
- setIsResizing = _g[1];
535
+ var _k = (0, _react.useState)(false),
536
+ isResizing = _k[0],
537
+ setIsResizing = _k[1];
319
538
  var startResize = function startResize(e) {
320
539
  e.preventDefault();
321
540
  setIsResizing(true);
@@ -334,7 +553,7 @@ var FxEditor = function FxEditor() {
334
553
  document.addEventListener("mouseup", _onMouseUp);
335
554
  };
336
555
  return /*#__PURE__*/_react.default.createElement("div", null, /*#__PURE__*/_react.default.createElement("div", {
337
- className: "fortune-fx-editor",
556
+ className: showFxFormulaChrome ? "fortune-fx-editor fortune-fx-editor--formula-owner" : "fortune-fx-editor",
338
557
  ref: divRef
339
558
  }, /*#__PURE__*/_react.default.createElement(_NameBox.default, null), /*#__PURE__*/_react.default.createElement("div", {
340
559
  className: "fortune-fx-icon",
@@ -361,8 +580,20 @@ var FxEditor = function FxEditor() {
361
580
  onMouseUp: function onMouseUp() {
362
581
  var _a;
363
582
  handleHideShowHint();
364
- var currentCommaCount = (0, _helper.countCommasBeforeCursor)((_a = refs.fxInput) === null || _a === void 0 ? void 0 : _a.current);
583
+ setContext(function (draftCtx) {
584
+ (0, _fortuneCore.setFormulaEditorOwner)(draftCtx, "fx");
585
+ });
586
+ var editor = (_a = refs.fxInput) === null || _a === void 0 ? void 0 : _a.current;
587
+ var currentCommaCount = (0, _helper.countCommasBeforeCursor)(editor);
365
588
  setCommaCount(currentCommaCount);
589
+ setContext(function (draftCtx) {
590
+ if (draftCtx.formulaCache.rangeSelectionActive !== true) return;
591
+ var clickedInsideManagedRange = (0, _fortuneCore.getFormulaRangeIndexAtCaret)(editor) !== null;
592
+ var atValidInsertionPoint = (0, _fortuneCore.isCaretAtValidFormulaRangeInsertionPoint)(editor);
593
+ if (clickedInsideManagedRange || !atValidInsertionPoint) {
594
+ (0, _fortuneCore.markRangeSelectionDirty)(draftCtx);
595
+ }
596
+ });
366
597
  },
367
598
  innerRef: function innerRef(e) {
368
599
  refs.fxInput.current = e;
@@ -375,7 +606,7 @@ var FxEditor = function FxEditor() {
375
606
  onChange: onChange,
376
607
  tabIndex: 0,
377
608
  allowEdit: allowEdit && !context.isFlvReadOnly
378
- }), showSearchHint && (/*#__PURE__*/_react.default.createElement(_FormulaSearch.default, {
609
+ }), (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, {
379
610
  from: "fx",
380
611
  onMouseMove: function onMouseMove(e) {
381
612
  if (document.getElementById("luckysheet-formula-search-c")) {
@@ -391,12 +622,22 @@ var FxEditor = function FxEditor() {
391
622
  }
392
623
  })), /*#__PURE__*/_react.default.createElement("div", {
393
624
  className: "fx-hint"
394
- }, showFormulaHint && fn && (/*#__PURE__*/_react.default.createElement(_FormulaHint.default, {
625
+ }, showFormulaHint && fn && !showSearchHint && (/*#__PURE__*/_react.default.createElement(_FormulaHint.default, {
395
626
  handleShowFormulaHint: handleShowFormulaHint,
396
627
  showFormulaHint: showFormulaHint,
397
628
  commaCount: commaCount,
398
629
  functionName: functionName
399
- })), !showFormulaHint && fn && (/*#__PURE__*/_react.default.createElement("div", {
630
+ })), !showFormulaHint && fn && !showSearchHint && (/*#__PURE__*/_react.default.createElement(_ui.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.default.createElement("div", {
400
641
  className: "luckysheet-hin absolute show-more-btn",
401
642
  onClick: function onClick() {
402
643
  handleShowFormulaHint();
@@ -410,7 +651,7 @@ var FxEditor = function FxEditor() {
410
651
  margin: "auto",
411
652
  marginTop: "1px"
412
653
  }
413
- })))))), /*#__PURE__*/_react.default.createElement("div", {
654
+ }))))))))), /*#__PURE__*/_react.default.createElement("div", {
414
655
  className: "resize-handle",
415
656
  onMouseDown: startResize,
416
657
  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",
@@ -1,4 +1,6 @@
1
1
  import React from "react";
2
2
  import "./index.css";
3
- declare const FormulaSearch: React.FC<React.HTMLAttributes<HTMLDivElement>>;
3
+ declare const FormulaSearch: React.FC<React.HTMLAttributes<HTMLDivElement> & {
4
+ from?: string;
5
+ }>;
4
6
  export default FormulaSearch;