@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
@@ -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);
@@ -1,47 +1,74 @@
1
- import { getFlowdata, cancelNormalSelected, getCellValue, updateCell, getInlineStringNoStyle, isInlineStringCell, escapeScriptTag, moveHighlightCell, handleFormulaInput, rangeHightlightselected, valueShowEs, isShowHidenCR, escapeHTMLTag, functionHTMLGenerate, 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, setCursorPosition, buildFormulaSuggestionText } 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";
13
16
  var FxEditor = function FxEditor() {
14
- var _a;
17
+ var _a, _b, _c;
15
18
  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;
19
+ var _d = useState(false),
20
+ showSearchHint = _d[0],
21
+ setShowSearchHint = _d[1];
22
+ var _e = useState(!hideFormulaHintLocal),
23
+ showFormulaHint = _e[0],
24
+ setShowFormulaHint = _e[1];
25
+ var _f = useState(0),
26
+ commaCount = _f[0],
27
+ setCommaCount = _f[1];
28
+ var _g = useContext(WorkbookContext),
29
+ context = _g.context,
30
+ setContext = _g.setContext,
31
+ refs = _g.refs;
29
32
  var lastKeyDownEventRef = useRef(null);
33
+ var _h = useFormulaEditorHistory(refs.fxInput, refs.cellInput, refs.fxInput, setContext, "fx"),
34
+ formulaHistoryRef = _h.formulaHistoryRef,
35
+ preTextRef = _h.preTextRef,
36
+ resetFormulaHistory = _h.resetFormulaHistory,
37
+ handleFormulaHistoryUndoRedo = _h.handleFormulaHistoryUndoRedo,
38
+ capturePreFormulaState = _h.capturePreFormulaState,
39
+ appendFormulaHistoryFromPrimaryEditor = _h.appendFormulaHistoryFromPrimaryEditor;
30
40
  var inputContainerRef = useRef(null);
31
- var _f = useState(false),
32
- isHidenRC = _f[0],
33
- setIsHidenRC = _f[1];
41
+ var _j = useState(false),
42
+ isHidenRC = _j[0],
43
+ setIsHidenRC = _j[1];
34
44
  var firstSelection = (_a = context.luckysheet_select_save) === null || _a === void 0 ? void 0 : _a[0];
35
45
  var prevFirstSelection = usePrevious(firstSelection);
46
+ var prevCellUpdate = usePrevious(context.luckysheetCellUpdate);
36
47
  var prevSheetId = usePrevious(context.currentSheetId);
37
48
  var recentText = useRef("");
49
+ var formulaAnchorCellRef = useRef(null);
38
50
  var handleShowFormulaHint = function handleShowFormulaHint() {
39
51
  localStorage.setItem("formulaMore", String(showFormulaHint));
40
52
  setShowFormulaHint(!showFormulaHint);
41
53
  };
54
+ useEffect(function () {
55
+ var handleKeyDown = function handleKeyDown(event) {
56
+ if (event.key === "F10") {
57
+ event.preventDefault();
58
+ handleShowFormulaHint();
59
+ }
60
+ };
61
+ window.addEventListener("keydown", handleKeyDown);
62
+ return function () {
63
+ return window.removeEventListener("keydown", handleKeyDown);
64
+ };
65
+ }, [showFormulaHint]);
42
66
  useEffect(function () {
43
67
  var _a;
44
68
  setIsHidenRC(isShowHidenCR(context));
69
+ if (context.luckysheetCellUpdate.length > 0) {
70
+ return;
71
+ }
45
72
  if (_.isEqual(prevFirstSelection, firstSelection) && context.currentSheetId === prevSheetId) {
46
73
  return;
47
74
  }
@@ -65,7 +92,74 @@ var FxEditor = function FxEditor() {
65
92
  } else {
66
93
  refs.fxInput.current.innerHTML = "";
67
94
  }
68
- }, [context.luckysheetfile, context.currentSheetId, context.luckysheet_select_save]);
95
+ }, [context.luckysheetfile, context.currentSheetId, context.luckysheet_select_save, context.luckysheetCellUpdate.length]);
96
+ useLayoutEffect(function () {
97
+ var _a;
98
+ var fxInput = refs.fxInput.current;
99
+ if (context.luckysheetCellUpdate.length === 0 || !fxInput) {
100
+ return;
101
+ }
102
+ if (refs.globalCache.doNotUpdateCell) {
103
+ delete refs.globalCache.doNotUpdateCell;
104
+ return;
105
+ }
106
+ if (_.isEqual(prevCellUpdate, context.luckysheetCellUpdate) && prevSheetId === context.currentSheetId) {
107
+ return;
108
+ }
109
+ var _b = context.luckysheetCellUpdate,
110
+ rowIndex = _b[0],
111
+ colIndex = _b[1];
112
+ if (_.isNil(rowIndex) || _.isNil(colIndex)) {
113
+ return;
114
+ }
115
+ var pending = refs.globalCache.pendingTypeOverCell;
116
+ if (pending && pending[0] === rowIndex && pending[1] === colIndex) {
117
+ refs.globalCache.overwriteCell = false;
118
+ refs.globalCache.ignoreWriteCell = false;
119
+ return;
120
+ }
121
+ var flowdata = getFlowdata(context);
122
+ if (!flowdata) {
123
+ return;
124
+ }
125
+ var cell = (_a = flowdata === null || flowdata === void 0 ? void 0 : flowdata[rowIndex]) === null || _a === void 0 ? void 0 : _a[colIndex];
126
+ var value = "";
127
+ var overwrite = refs.globalCache.overwriteCell;
128
+ if (cell && !overwrite) {
129
+ if (isInlineStringCell(cell)) {
130
+ value = getInlineStringNoStyle(rowIndex, colIndex, flowdata);
131
+ } else if (cell.f) {
132
+ value = getCellValue(rowIndex, colIndex, flowdata, "f");
133
+ } else {
134
+ value = valueShowEs(rowIndex, colIndex, flowdata);
135
+ }
136
+ }
137
+ refs.globalCache.overwriteCell = false;
138
+ if (!refs.globalCache.ignoreWriteCell && value) {
139
+ fxInput.innerHTML = escapeHTMLTag(escapeScriptTag(value));
140
+ } else if (!refs.globalCache.ignoreWriteCell && !overwrite) {
141
+ var valueD = getCellValue(rowIndex, colIndex, flowdata, "f");
142
+ fxInput.innerText = valueD;
143
+ }
144
+ refs.globalCache.ignoreWriteCell = false;
145
+ }, [context.luckysheetCellUpdate, context.luckysheetfile, context.currentSheetId, prevCellUpdate, prevSheetId, refs.fxInput, refs.globalCache]);
146
+ useEffect(function () {
147
+ if (_.isEmpty(context.luckysheetCellUpdate)) {
148
+ delete refs.globalCache.pendingTypeOverCell;
149
+ resetFormulaHistory();
150
+ }
151
+ }, [context.luckysheetCellUpdate, resetFormulaHistory, refs.globalCache]);
152
+ useEffect(function () {
153
+ var _a;
154
+ if (_.isEmpty(context.luckysheetCellUpdate) || !refs.fxInput.current) {
155
+ formulaAnchorCellRef.current = null;
156
+ return;
157
+ }
158
+ var inputText = ((_a = refs.fxInput.current.innerText) === null || _a === void 0 ? void 0 : _a.trim()) || "";
159
+ if (!inputText.startsWith("=")) {
160
+ formulaAnchorCellRef.current = null;
161
+ }
162
+ }, [context.luckysheetCellUpdate, refs.fxInput]);
69
163
  var onFocus = useCallback(function () {
70
164
  var _a, _b;
71
165
  if (context.allowEdit === false) {
@@ -73,13 +167,30 @@ var FxEditor = function FxEditor() {
73
167
  }
74
168
  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
169
  setContext(function (draftCtx) {
170
+ var _a;
171
+ setFormulaEditorOwner(draftCtx, "fx");
76
172
  var last = draftCtx.luckysheet_select_save[draftCtx.luckysheet_select_save.length - 1];
77
173
  var row_index = last.row_focus;
78
174
  var col_index = last.column_focus;
79
- draftCtx.luckysheetCellUpdate = [row_index, col_index];
80
- refs.globalCache.doNotFocus = true;
175
+ if (!_.isNil(row_index) && !_.isNil(col_index)) {
176
+ var flowdata = getFlowdata(draftCtx);
177
+ var cellAt = (_a = flowdata === null || flowdata === void 0 ? void 0 : flowdata[row_index]) === null || _a === void 0 ? void 0 : _a[col_index];
178
+ if ((cellAt === null || cellAt === void 0 ? void 0 : cellAt.f) != null && String(cellAt.f).trim() !== "") {
179
+ suppressFormulaRangeSelectionForInitialEdit(draftCtx);
180
+ }
181
+ }
182
+ var currentUpdate = draftCtx.luckysheetCellUpdate || [];
183
+ var alreadyEditingSameCell = currentUpdate.length === 2 && currentUpdate[0] === row_index && currentUpdate[1] === col_index;
184
+ if (!alreadyEditingSameCell) {
185
+ refs.globalCache.doNotUpdateCell = true;
186
+ draftCtx.luckysheetCellUpdate = [row_index, col_index];
187
+ refs.globalCache.doNotFocus = true;
188
+ }
81
189
  });
82
190
  }
191
+ setContext(function (draftCtx) {
192
+ setFormulaEditorOwner(draftCtx, "fx");
193
+ });
83
194
  }, [context.config, context.luckysheet_select_save, context.luckysheetfile, context.currentSheetId, refs.globalCache, setContext]);
84
195
  var getActiveFormula = useCallback(function () {
85
196
  return document.querySelector(".luckysheet-formula-search-item-active");
@@ -98,12 +209,13 @@ var FxEditor = function FxEditor() {
98
209
  cellEditor.innerHTML = html;
99
210
  }
100
211
  setCursorPosition(fxEditor, caretOffset);
212
+ setShowSearchHint(shouldShowFormulaFunctionList(fxEditor));
101
213
  setContext(function (draftCtx) {
102
214
  draftCtx.functionCandidates = [];
103
215
  draftCtx.defaultCandidates = [];
104
- draftCtx.functionHint = formulaName;
216
+ draftCtx.functionHint = (formulaName || "").toUpperCase();
105
217
  });
106
- }, [setContext]);
218
+ }, [refs.fxInput, setContext]);
107
219
  var clearSearchItemActiveClass = useCallback(function () {
108
220
  var activeFormula = getActiveFormula();
109
221
  if (activeFormula) {
@@ -119,10 +231,11 @@ var FxEditor = function FxEditor() {
119
231
  return lastSpan === null || lastSpan === void 0 ? void 0 : lastSpan.innerText;
120
232
  };
121
233
  var selectActiveFormulaOnClick = useCallback(function (e) {
122
- var _a, _b, _c;
234
+ var _a, _b, _c, _d;
123
235
  if (e.target.className.includes("sign-fortune")) return;
124
- recentText.current = (_a = refs.fxInput) === null || _a === void 0 ? void 0 : _a.current.innerText;
125
- 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;
236
+ preTextRef.current = (_a = refs.fxInput) === null || _a === void 0 ? void 0 : _a.current.innerText;
237
+ recentText.current = (_b = refs.fxInput) === null || _b === void 0 ? void 0 : _b.current.innerText;
238
+ 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;
126
239
  var lastSpanText = getLastInputSpanText();
127
240
  if (formulaName && !isLetterNumberPattern(lastSpanText)) {
128
241
  insertSelectedFormula(formulaName);
@@ -131,18 +244,95 @@ var FxEditor = function FxEditor() {
131
244
  }
132
245
  }, [getActiveFormula, insertSelectedFormula]);
133
246
  var onKeyDown = useCallback(function (e) {
134
- var _a;
247
+ var _a, _b, _c;
248
+ if (e.key === "Delete" || e.key === "Backspace") {
249
+ var anchor = formulaAnchorCellRef.current;
250
+ if (anchor != null) {
251
+ var anchorRow_1 = anchor[0],
252
+ anchorCol_1 = anchor[1];
253
+ setTimeout(function () {
254
+ setContext(function (draftCtx) {
255
+ draftCtx.luckysheetCellUpdate = [anchorRow_1, anchorCol_1];
256
+ draftCtx.luckysheet_select_save = [{
257
+ row: [anchorRow_1, anchorRow_1],
258
+ column: [anchorCol_1, anchorCol_1],
259
+ row_focus: anchorRow_1,
260
+ column_focus: anchorCol_1
261
+ }];
262
+ markRangeSelectionDirty(draftCtx);
263
+ });
264
+ }, 0);
265
+ }
266
+ }
135
267
  if (context.allowEdit === false) {
136
268
  return;
137
269
  }
270
+ setContext(function (draftCtx) {
271
+ setFormulaEditorOwner(draftCtx, "fx");
272
+ });
138
273
  var currentCommaCount = countCommasBeforeCursor((_a = refs.fxInput) === null || _a === void 0 ? void 0 : _a.current);
139
274
  setCommaCount(currentCommaCount);
140
275
  lastKeyDownEventRef.current = new KeyboardEvent(e.type, e.nativeEvent);
141
- var key = e.key;
276
+ capturePreFormulaState();
142
277
  recentText.current = refs.fxInput.current.innerText;
278
+ var key = e.key;
279
+ var currentInputText = ((_c = (_b = refs.fxInput.current) === null || _b === void 0 ? void 0 : _b.innerText) === null || _c === void 0 ? void 0 : _c.trim()) || "";
280
+ if ((key === "=" || currentInputText.startsWith("=")) && context.luckysheetCellUpdate.length === 2 && formulaAnchorCellRef.current == null) {
281
+ setContext(function (draftCtx) {
282
+ draftCtx.formulaCache.rangeSelectionActive = null;
283
+ });
284
+ formulaAnchorCellRef.current = [context.luckysheetCellUpdate[0], context.luckysheetCellUpdate[1]];
285
+ }
286
+ if (key === "(" && currentInputText.startsWith("=")) {
287
+ setContext(function (draftCtx) {
288
+ draftCtx.formulaCache.rangeSelectionActive = null;
289
+ });
290
+ }
291
+ if (key === "," && context.luckysheetCellUpdate.length > 0 && currentInputText.startsWith("=") && formulaAnchorCellRef.current) {
292
+ setContext(function (draftCtx) {
293
+ draftCtx.formulaCache.rangeSelectionActive = null;
294
+ });
295
+ var _d = formulaAnchorCellRef.current,
296
+ anchorRow_2 = _d[0],
297
+ anchorCol_2 = _d[1];
298
+ setTimeout(function () {
299
+ setContext(function (draftCtx) {
300
+ var _a, _b;
301
+ draftCtx.luckysheetCellUpdate = [anchorRow_2, anchorCol_2];
302
+ draftCtx.luckysheet_select_save = [{
303
+ row: [anchorRow_2, anchorRow_2],
304
+ column: [anchorCol_2, anchorCol_2],
305
+ row_focus: anchorRow_2,
306
+ column_focus: anchorCol_2
307
+ }];
308
+ draftCtx.formulaRangeSelect = undefined;
309
+ draftCtx.formulaCache.selectingRangeIndex = -1;
310
+ draftCtx.formulaCache.func_selectedrange = undefined;
311
+ draftCtx.formulaCache.rangechangeindex = undefined;
312
+ draftCtx.formulaCache.rangestart = false;
313
+ draftCtx.formulaCache.rangedrag_column_start = false;
314
+ draftCtx.formulaCache.rangedrag_row_start = false;
315
+ 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) || "");
316
+ moveHighlightCell(draftCtx, "down", 0, "rangeOfSelect");
317
+ });
318
+ }, 0);
319
+ }
143
320
  if (key === "ArrowLeft" || key === "ArrowRight") {
144
321
  e.stopPropagation();
145
322
  }
323
+ if ((e.metaKey || e.ctrlKey) && context.luckysheetCellUpdate.length > 0) {
324
+ if (e.code === "KeyZ" || e.code === "KeyY") {
325
+ var shouldUseFormulaHistory = currentInputText.startsWith("=") || formulaHistoryRef.current.active;
326
+ if (shouldUseFormulaHistory) {
327
+ var handledByFormulaHistory = handleFormulaHistoryUndoRedo(e.code === "KeyY" || e.code === "KeyZ" && e.shiftKey);
328
+ if (handledByFormulaHistory) {
329
+ e.preventDefault();
330
+ }
331
+ }
332
+ e.stopPropagation();
333
+ return;
334
+ }
335
+ }
146
336
  if ((e.key === "Enter" || e.key === "Tab") && context.luckysheetCellUpdate.length > 0) {
147
337
  if (e.altKey || e.metaKey) {
148
338
  document.execCommand("insertHTML", false, "\n ");
@@ -154,7 +344,7 @@ var FxEditor = function FxEditor() {
154
344
  }
155
345
  return;
156
346
  }
157
- if (e.key === "ArrowUp" && context.luckysheetCellUpdate.length > 0) {
347
+ if (!(e.metaKey || e.ctrlKey) && e.key === "ArrowUp" && context.luckysheetCellUpdate.length > 0) {
158
348
  if (document.getElementById("luckysheet-formula-search-c")) {
159
349
  var formulaSearchContainer = document.getElementById("luckysheet-formula-search-c");
160
350
  var activeItem = formulaSearchContainer === null || formulaSearchContainer === void 0 ? void 0 : formulaSearchContainer.querySelector(".luckysheet-formula-search-item-active");
@@ -173,7 +363,7 @@ var FxEditor = function FxEditor() {
173
363
  }
174
364
  }
175
365
  e.preventDefault();
176
- } else if (e.key === "ArrowDown" && context.luckysheetCellUpdate.length > 0) {
366
+ } else if (!(e.metaKey || e.ctrlKey) && e.key === "ArrowDown" && context.luckysheetCellUpdate.length > 0) {
177
367
  if (document.getElementById("luckysheet-formula-search-c")) {
178
368
  var formulaSearchContainer = document.getElementById("luckysheet-formula-search-c");
179
369
  var activeItem = formulaSearchContainer === null || formulaSearchContainer === void 0 ? void 0 : formulaSearchContainer.querySelector(".luckysheet-formula-search-item-active");
@@ -232,7 +422,7 @@ var FxEditor = function FxEditor() {
232
422
  }
233
423
  }
234
424
  });
235
- }, [context.allowEdit, context.luckysheetCellUpdate.length, refs.fxInput, setContext]);
425
+ }, [capturePreFormulaState, context.allowEdit, context.luckysheetCellUpdate, handleFormulaHistoryUndoRedo, refs.cellInput, refs.fxInput, setContext]);
236
426
  var handleHideShowHint = function handleHideShowHint() {
237
427
  var _a, _b, _c, _d;
238
428
  var el = (_a = document.getElementsByClassName("cell-hint")) === null || _a === void 0 ? void 0 : _a[0];
@@ -253,26 +443,54 @@ var FxEditor = function FxEditor() {
253
443
  }
254
444
  };
255
445
  var onChange = useCallback(function () {
256
- var _a, _b, _c;
446
+ var _a, _b, _c, _d;
257
447
  if (context.isFlvReadOnly) return;
258
- handleHideShowHint();
259
- 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())) {
260
- setShowSearchHint(true);
261
- } else {
262
- setShowSearchHint(false);
448
+ var fxEl = refs.fxInput.current;
449
+ if (fxEl && context.luckysheetCellUpdate.length === 2 && formulaAnchorCellRef.current == null) {
450
+ var t = ((_a = fxEl.innerText) === null || _a === void 0 ? void 0 : _a.trim()) || "";
451
+ if (t.startsWith("=")) {
452
+ formulaAnchorCellRef.current = [context.luckysheetCellUpdate[0], context.luckysheetCellUpdate[1]];
453
+ }
263
454
  }
264
- var currentCommaCount = countCommasBeforeCursor((_c = refs.fxInput) === null || _c === void 0 ? void 0 : _c.current);
455
+ handleHideShowHint();
456
+ setShowSearchHint(shouldShowFormulaFunctionList((_c = (_b = refs.fxInput) === null || _b === void 0 ? void 0 : _b.current) !== null && _c !== void 0 ? _c : null));
457
+ var currentCommaCount = countCommasBeforeCursor((_d = refs.fxInput) === null || _d === void 0 ? void 0 : _d.current);
265
458
  setCommaCount(currentCommaCount);
266
459
  var e = lastKeyDownEventRef.current;
267
- if (!e) return;
460
+ if (!e) {
461
+ var fx = refs.fxInput.current;
462
+ var cell = refs.cellInput.current;
463
+ var isFormula = function isFormula(el) {
464
+ var _a, _b;
465
+ 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("=")));
466
+ };
467
+ var sel = window.getSelection();
468
+ var editor_1 = null;
469
+ if (sel === null || sel === void 0 ? void 0 : sel.rangeCount) {
470
+ var node = sel.getRangeAt(0).startContainer;
471
+ 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;
472
+ }
473
+ if (!editor_1 && isFormula(fx)) editor_1 = fx;else if (!editor_1 && isFormula(cell)) editor_1 = cell;
474
+ if (editor_1) {
475
+ setContext(function (draftCtx) {
476
+ if (!isAllowEdit(draftCtx, draftCtx.luckysheet_select_save)) return;
477
+ rangeHightlightselected(draftCtx, editor_1);
478
+ });
479
+ }
480
+ return;
481
+ }
268
482
  var kcode = e.keyCode;
269
483
  if (!kcode) return;
484
+ appendFormulaHistoryFromPrimaryEditor(function () {
485
+ return getCursorPosition(refs.fxInput.current);
486
+ });
270
487
  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) {
271
488
  setContext(function (draftCtx) {
272
489
  handleFormulaInput(draftCtx, refs.cellInput.current, refs.fxInput.current, kcode, recentText.current);
273
490
  });
274
491
  }
275
- }, [refs.cellInput, refs.fxInput, setContext]);
492
+ }, [appendFormulaHistoryFromPrimaryEditor, context.isFlvReadOnly, context.luckysheetCellUpdate, refs.cellInput, refs.fxInput, setContext]);
493
+ useRerenderOnFormulaCaret(refs.fxInput, context.luckysheetCellUpdate.length > 0);
276
494
  var getFunctionNameFromInput = useCallback(function () {
277
495
  var _a, _b, _c, _d;
278
496
  var inputText = ((_b = (_a = refs.fxInput) === null || _a === void 0 ? void 0 : _a.current) === null || _b === void 0 ? void 0 : _b.innerText) || "";
@@ -289,8 +507,9 @@ var FxEditor = function FxEditor() {
289
507
  }
290
508
  return null;
291
509
  }, []);
292
- var functionName = context.functionHint || getFunctionNameFromInput();
510
+ var functionName = (_c = (_b = getFunctionNameFromFormulaCaretSpans(refs.fxInput.current)) !== null && _b !== void 0 ? _b : context.functionHint) !== null && _c !== void 0 ? _c : getFunctionNameFromInput();
293
511
  var fn = functionName ? context.formulaCache.functionlistMap[functionName] : null;
512
+ var showFxFormulaChrome = context.luckysheetCellUpdate.length > 0 && getFormulaEditorOwner(context) === "fx";
294
513
  var allowEdit = useMemo(function () {
295
514
  if (context.allowEdit === false) {
296
515
  return false;
@@ -304,9 +523,9 @@ var FxEditor = function FxEditor() {
304
523
  return true;
305
524
  }, [context.config, context.luckysheet_select_save, context.luckysheetfile, context.currentSheetId, isHidenRC]);
306
525
  var divRef = useRef(null);
307
- var _g = useState(false),
308
- isResizing = _g[0],
309
- setIsResizing = _g[1];
526
+ var _k = useState(false),
527
+ isResizing = _k[0],
528
+ setIsResizing = _k[1];
310
529
  var startResize = function startResize(e) {
311
530
  e.preventDefault();
312
531
  setIsResizing(true);
@@ -325,7 +544,7 @@ var FxEditor = function FxEditor() {
325
544
  document.addEventListener("mouseup", _onMouseUp);
326
545
  };
327
546
  return /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement("div", {
328
- className: "fortune-fx-editor",
547
+ className: showFxFormulaChrome ? "fortune-fx-editor fortune-fx-editor--formula-owner" : "fortune-fx-editor",
329
548
  ref: divRef
330
549
  }, /*#__PURE__*/React.createElement(NameBox, null), /*#__PURE__*/React.createElement("div", {
331
550
  className: "fortune-fx-icon",
@@ -352,8 +571,20 @@ var FxEditor = function FxEditor() {
352
571
  onMouseUp: function onMouseUp() {
353
572
  var _a;
354
573
  handleHideShowHint();
355
- var currentCommaCount = countCommasBeforeCursor((_a = refs.fxInput) === null || _a === void 0 ? void 0 : _a.current);
574
+ setContext(function (draftCtx) {
575
+ setFormulaEditorOwner(draftCtx, "fx");
576
+ });
577
+ var editor = (_a = refs.fxInput) === null || _a === void 0 ? void 0 : _a.current;
578
+ var currentCommaCount = countCommasBeforeCursor(editor);
356
579
  setCommaCount(currentCommaCount);
580
+ setContext(function (draftCtx) {
581
+ if (draftCtx.formulaCache.rangeSelectionActive !== true) return;
582
+ var clickedInsideManagedRange = getFormulaRangeIndexAtCaret(editor) !== null;
583
+ var atValidInsertionPoint = isCaretAtValidFormulaRangeInsertionPoint(editor);
584
+ if (clickedInsideManagedRange || !atValidInsertionPoint) {
585
+ markRangeSelectionDirty(draftCtx);
586
+ }
587
+ });
357
588
  },
358
589
  innerRef: function innerRef(e) {
359
590
  refs.fxInput.current = e;
@@ -366,7 +597,7 @@ var FxEditor = function FxEditor() {
366
597
  onChange: onChange,
367
598
  tabIndex: 0,
368
599
  allowEdit: allowEdit && !context.isFlvReadOnly
369
- }), showSearchHint && (/*#__PURE__*/React.createElement(FormulaSearch, {
600
+ }), (context.functionCandidates.length > 0 || context.functionHint || context.defaultCandidates.length > 0 || fn) && showFxFormulaChrome && (/*#__PURE__*/React.createElement(React.Fragment, null, showSearchHint && (/*#__PURE__*/React.createElement(FormulaSearch, {
370
601
  from: "fx",
371
602
  onMouseMove: function onMouseMove(e) {
372
603
  if (document.getElementById("luckysheet-formula-search-c")) {
@@ -382,12 +613,22 @@ var FxEditor = function FxEditor() {
382
613
  }
383
614
  })), /*#__PURE__*/React.createElement("div", {
384
615
  className: "fx-hint"
385
- }, showFormulaHint && fn && (/*#__PURE__*/React.createElement(FormulaHint, {
616
+ }, showFormulaHint && fn && !showSearchHint && (/*#__PURE__*/React.createElement(FormulaHint, {
386
617
  handleShowFormulaHint: handleShowFormulaHint,
387
618
  showFormulaHint: showFormulaHint,
388
619
  commaCount: commaCount,
389
620
  functionName: functionName
390
- })), !showFormulaHint && fn && (/*#__PURE__*/React.createElement("div", {
621
+ })), !showFormulaHint && fn && !showSearchHint && (/*#__PURE__*/React.createElement(Tooltip, {
622
+ text: "Turn on formula suggestions (F10)",
623
+ placement: "top",
624
+ defaultOpen: true,
625
+ style: {
626
+ position: "absolute",
627
+ top: "-50px",
628
+ left: "-130px",
629
+ width: "210px"
630
+ }
631
+ }, /*#__PURE__*/React.createElement("div", {
391
632
  className: "luckysheet-hin absolute show-more-btn",
392
633
  onClick: function onClick() {
393
634
  handleShowFormulaHint();
@@ -401,7 +642,7 @@ var FxEditor = function FxEditor() {
401
642
  margin: "auto",
402
643
  marginTop: "1px"
403
644
  }
404
- })))))), /*#__PURE__*/React.createElement("div", {
645
+ }))))))))), /*#__PURE__*/React.createElement("div", {
405
646
  className: "resize-handle",
406
647
  onMouseDown: startResize,
407
648
  style: {
@@ -10,7 +10,7 @@ var __assign = this && this.__assign || function () {
10
10
  };
11
11
  import { locale } from "@fileverse-dev/fortune-core";
12
12
  import { Button, TextField, LucideIcon, Tooltip } from "@fileverse/ui";
13
- import React, { useContext, useEffect, useRef, useState } from "react";
13
+ import React, { useCallback, useContext, useEffect, useLayoutEffect, useRef, useState } from "react";
14
14
  import WorkbookContext from "../../../context";
15
15
  import "./index.css";
16
16
  import { DraggableDiv } from "./dragable-div";
@@ -49,29 +49,49 @@ var FormulaHint = function FormulaHint(props) {
49
49
  ETHERSCAN_API_KEY: "Etherscan API key"
50
50
  };
51
51
  var hintRef = useRef(null);
52
- var _h = useState(0),
52
+ var cellHeightPx = (firstSelection === null || firstSelection === void 0 ? void 0 : firstSelection.height_move) != null ? Number(firstSelection.height_move) : 24;
53
+ var belowCellTop = cellHeightPx + 4;
54
+ var _h = useState(belowCellTop),
53
55
  top = _h[0],
54
56
  setTop = _h[1];
55
57
  var _j = useState(false),
56
58
  showDelayedHint = _j[0],
57
59
  setShowDelayedHint = _j[1];
58
- var calcuatePopUpPlacement = function calcuatePopUpPlacement() {
59
- var _a, _b, _c, _d, _e, _f;
60
- 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;
61
- var hintHeight = 360;
62
- var inputBoxTop = parseInt(((_c = document.getElementById("luckysheet-input-box")) === null || _c === void 0 ? void 0 : _c.style.top) || "0", 10) - 85;
63
- var inputBottom = inputBoxTop + firstSelection.height_move;
64
- var availableBelow = window.innerHeight - inputBottom;
65
- var hintAbove = hintHeight > availableBelow;
66
- var selectionHeight = (firstSelection === null || firstSelection === void 0 ? void 0 : firstSelection.height_move) || 0;
67
- var divOffset = ((_d = hintRef.current) === null || _d === void 0 ? void 0 : _d.offsetHeight) || 0;
68
- var topV = hintAbove ? selectionHeight - (divOffset + 30) : selectionHeight + 4;
69
- var el = (_e = document.getElementsByClassName("fx-hint")) === null || _e === void 0 ? void 0 : _e[0];
70
- if (el && ((_f = el === null || el === void 0 ? void 0 : el.style) === null || _f === void 0 ? void 0 : _f.display) !== "none") {
60
+ var measureFormulaHintPlacement = useCallback(function () {
61
+ var _a;
62
+ if ((firstSelection === null || firstSelection === void 0 ? void 0 : firstSelection.top) == null || firstSelection.height_move == null) {
63
+ setTop(belowCellTop);
64
+ return;
65
+ }
66
+ var cellH = Number(firstSelection.height_move) || cellHeightPx;
67
+ var innerEl = hintRef.current;
68
+ var popupHeight = Math.min((innerEl === null || innerEl === void 0 ? void 0 : innerEl.offsetHeight) || 360, 360);
69
+ var inputBox = document.getElementById("luckysheet-input-box");
70
+ var rect = inputBox === null || inputBox === void 0 ? void 0 : inputBox.getBoundingClientRect();
71
+ if (!rect) {
72
+ setTop(cellH + 4);
73
+ return;
74
+ }
75
+ var cellBottomViewport = rect.top + cellH;
76
+ var availableBelow = window.innerHeight - cellBottomViewport - 12;
77
+ var preferBelow = popupHeight <= availableBelow;
78
+ var topV = preferBelow ? cellH + 4 : -(popupHeight + 8);
79
+ var fxHint = (_a = document.getElementsByClassName("fx-hint")) === null || _a === void 0 ? void 0 : _a[0];
80
+ if (fxHint && fxHint.style.display !== "none") {
71
81
  topV = 25;
72
82
  }
73
83
  setTop(topV);
74
- };
84
+ }, [belowCellTop, cellHeightPx, firstSelection === null || firstSelection === void 0 ? void 0 : firstSelection.height_move, firstSelection === null || firstSelection === void 0 ? void 0 : firstSelection.top]);
85
+ useLayoutEffect(function () {
86
+ if (!fn) {
87
+ return undefined;
88
+ }
89
+ measureFormulaHintPlacement();
90
+ var id = requestAnimationFrame(measureFormulaHintPlacement);
91
+ return function () {
92
+ cancelAnimationFrame(id);
93
+ };
94
+ }, [functionName, context.functionHint, measureFormulaHintPlacement, showFormulaHint, commaCount, showFunctionBody]);
75
95
  var hexToRgbString = function hexToRgbString(hex) {
76
96
  hex = hex.replace("#", "");
77
97
  var r = parseInt(hex.substring(0, 2), 16);
@@ -84,14 +104,13 @@ var FormulaHint = function FormulaHint(props) {
84
104
  return bg;
85
105
  };
86
106
  useEffect(function () {
87
- calcuatePopUpPlacement();
88
- }, []);
89
- useEffect(function () {
90
- if (!top) {
91
- setTimeout(function () {
92
- setShowDelayedHint(true);
93
- }, 40);
94
- }
107
+ setShowDelayedHint(false);
108
+ var t = setTimeout(function () {
109
+ return setShowDelayedHint(true);
110
+ }, 40);
111
+ return function () {
112
+ return clearTimeout(t);
113
+ };
95
114
  }, [top]);
96
115
  if (!fn) return null;
97
116
  var fnNameClass = fn.n ? String(fn.n).replace(/[^a-zA-Z0-9-]/g, "-").replace(/-+/g, "-") : "";
@@ -174,7 +193,7 @@ var FormulaHint = function FormulaHint(props) {
174
193
  localStorage.setItem("formula-expand", "".concat(!showFunctionBody));
175
194
  setShouldShowFunctionBody(!showFunctionBody);
176
195
  setTimeout(function () {
177
- calcuatePopUpPlacement();
196
+ measureFormulaHintPlacement();
178
197
  }, 50);
179
198
  }
180
199
  dragHasMoved.current = false;
@@ -277,6 +296,7 @@ var FormulaHint = function FormulaHint(props) {
277
296
  cursor: "auto"
278
297
  }
279
298
  }, fn.API_KEY && (/*#__PURE__*/React.createElement("div", {
299
+ className: "luckysheet-formula-help-content-api",
280
300
  style: {
281
301
  borderLeft: "4px solid ".concat(isKeyAdded ? "#177E23" : "#fb923c"),
282
302
  backgroundColor: "white",
@@ -340,6 +360,7 @@ var FormulaHint = function FormulaHint(props) {
340
360
  className: "fortune-formula-hint__cta min-w-[80px]",
341
361
  "data-testid": "formula-hint-cta-ok"
342
362
  }, "Ok"))))))), /*#__PURE__*/React.createElement("div", {
363
+ id: "luckysheet-formula-help-content-example",
343
364
  style: {
344
365
  backgroundColor: "white",
345
366
  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;