@fileverse-dev/fortune-react 1.3.10 → 1.3.11-input-ref-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 (29) hide show
  1. package/es/components/ContextMenu/index.js +1 -1
  2. package/es/components/FxEditor/index.js +200 -52
  3. package/es/components/SheetOverlay/InputBox.js +318 -150
  4. package/es/components/SheetOverlay/drag_and_drop/column-helpers.js +25 -1
  5. package/es/components/SheetOverlay/drag_and_drop/row-helpers.js +26 -2
  6. package/es/components/SheetOverlay/helper.d.ts +7 -0
  7. package/es/components/SheetOverlay/helper.js +95 -0
  8. package/es/components/SheetOverlay/index.css +1 -1
  9. package/es/components/Workbook/index.js +141 -11
  10. package/es/hooks/useFormulaEditorHistory.d.ts +24 -0
  11. package/es/hooks/useFormulaEditorHistory.js +119 -0
  12. package/es/hooks/useRerenderOnFormulaCaret.d.ts +2 -0
  13. package/es/hooks/useRerenderOnFormulaCaret.js +26 -0
  14. package/es/utils/convertCellsToCrypto.js +17 -0
  15. package/lib/components/ContextMenu/index.js +1 -1
  16. package/lib/components/FxEditor/index.js +198 -50
  17. package/lib/components/SheetOverlay/InputBox.js +316 -148
  18. package/lib/components/SheetOverlay/drag_and_drop/column-helpers.js +25 -1
  19. package/lib/components/SheetOverlay/drag_and_drop/row-helpers.js +26 -2
  20. package/lib/components/SheetOverlay/helper.d.ts +7 -0
  21. package/lib/components/SheetOverlay/helper.js +99 -0
  22. package/lib/components/SheetOverlay/index.css +1 -1
  23. package/lib/components/Workbook/index.js +142 -12
  24. package/lib/hooks/useFormulaEditorHistory.d.ts +24 -0
  25. package/lib/hooks/useFormulaEditorHistory.js +126 -0
  26. package/lib/hooks/useRerenderOnFormulaCaret.d.ts +2 -0
  27. package/lib/hooks/useRerenderOnFormulaCaret.js +32 -0
  28. package/lib/utils/convertCellsToCrypto.js +17 -0
  29. package/package.json +2 -2
@@ -14,6 +14,8 @@ var _ContentEditable = _interopRequireDefault(require("./ContentEditable"));
14
14
  var _FormulaSearch = _interopRequireDefault(require("./FormulaSearch"));
15
15
  var _FormulaHint = _interopRequireDefault(require("./FormulaHint"));
16
16
  var _usePrevious = _interopRequireDefault(require("../../hooks/usePrevious"));
17
+ var _useFormulaEditorHistory = require("../../hooks/useFormulaEditorHistory");
18
+ var _useRerenderOnFormulaCaret = require("../../hooks/useRerenderOnFormulaCaret");
17
19
  var _helper = require("./helper");
18
20
  var _LucideIcon = require("./LucideIcon");
19
21
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
@@ -29,57 +31,65 @@ var __assign = void 0 && (void 0).__assign || function () {
29
31
  return __assign.apply(this, arguments);
30
32
  };
31
33
  var InputBox = function InputBox() {
32
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
33
- var _m = (0, _react.useContext)(_context.default),
34
- context = _m.context,
35
- setContext = _m.setContext,
36
- refs = _m.refs;
34
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p;
35
+ var _q = (0, _react.useContext)(_context.default),
36
+ context = _q.context,
37
+ setContext = _q.setContext,
38
+ refs = _q.refs;
37
39
  var inputRef = (0, _react.useRef)(null);
38
40
  var lastKeyDownEventRef = (0, _react.useRef)(null);
39
41
  var prevCellUpdate = (0, _usePrevious.default)(context.luckysheetCellUpdate);
40
42
  var prevSheetId = (0, _usePrevious.default)(context.currentSheetId);
41
- var _o = (0, _react.useState)(false),
42
- isHidenRC = _o[0],
43
- setIsHidenRC = _o[1];
44
- var _p = (0, _react.useState)(false),
45
- isInputBoxActive = _p[0],
46
- setIsInputBoxActive = _p[1];
47
- var _q = (0, _react.useState)(""),
48
- activeCell = _q[0],
49
- setActiveCell = _q[1];
50
- var _r = (0, _react.useState)(""),
51
- activeRefCell = _r[0],
52
- setActiveRefCell = _r[1];
53
- var _s = (0, _react.useState)({
43
+ var _r = (0, _react.useState)(false),
44
+ isHidenRC = _r[0],
45
+ setIsHidenRC = _r[1];
46
+ var _s = (0, _react.useState)(false),
47
+ isInputBoxActive = _s[0],
48
+ setIsInputBoxActive = _s[1];
49
+ var _t = (0, _react.useState)(""),
50
+ activeCell = _t[0],
51
+ setActiveCell = _t[1];
52
+ var _u = (0, _react.useState)(""),
53
+ activeRefCell = _u[0],
54
+ setActiveRefCell = _u[1];
55
+ var _v = (0, _react.useState)({
54
56
  left: 0,
55
57
  top: 0
56
58
  }),
57
- frozenPosition = _s[0],
58
- setFrozenPosition = _s[1];
59
+ frozenPosition = _v[0],
60
+ setFrozenPosition = _v[1];
59
61
  var firstSelection = (_a = context.luckysheet_select_save) === null || _a === void 0 ? void 0 : _a[0];
60
- var _t = (0, _react.useState)({}),
61
- firstSelectionActiveCell = _t[0],
62
- setFirstSelectionActiveCell = _t[1];
63
- var _u = (0, _react.useState)(0),
64
- commaCount = _u[0],
65
- setCommaCount = _u[1];
62
+ var _w = (0, _react.useState)({}),
63
+ firstSelectionActiveCell = _w[0],
64
+ setFirstSelectionActiveCell = _w[1];
65
+ var _x = (0, _react.useState)(0),
66
+ commaCount = _x[0],
67
+ setCommaCount = _x[1];
66
68
  var hideFormulaHintLocal = localStorage.getItem("formulaMore") === "true";
67
- var _v = (0, _react.useState)(!hideFormulaHintLocal),
68
- showFormulaHint = _v[0],
69
- setShowFormulaHint = _v[1];
70
- var _w = (0, _react.useState)(false),
71
- showSearchHint = _w[0],
72
- setShowSearchHint = _w[1];
69
+ var _y = (0, _react.useState)(!hideFormulaHintLocal),
70
+ showFormulaHint = _y[0],
71
+ setShowFormulaHint = _y[1];
72
+ var _z = (0, _react.useState)(false),
73
+ showSearchHint = _z[0],
74
+ setShowSearchHint = _z[1];
73
75
  var row_index = firstSelection === null || firstSelection === void 0 ? void 0 : firstSelection.row_focus;
74
76
  var col_index = firstSelection === null || firstSelection === void 0 ? void 0 : firstSelection.column_focus;
75
- var preText = (0, _react.useRef)("");
76
- var placeRef = (0, _react.useRef)("");
77
77
  var isComposingRef = (0, _react.useRef)(false);
78
+ var formulaAnchorCellRef = (0, _react.useRef)(null);
79
+ var skipNextAnchorSelectionSyncRef = (0, _react.useRef)(false);
80
+ var lastHandledMouseDragSignatureRef = (0, _react.useRef)("");
81
+ var _0 = (0, _useFormulaEditorHistory.useFormulaEditorHistory)(inputRef, refs.cellInput, refs.fxInput, setContext, "cell"),
82
+ formulaHistoryRef = _0.formulaHistoryRef,
83
+ preTextRef = _0.preTextRef,
84
+ resetFormulaHistory = _0.resetFormulaHistory,
85
+ handleFormulaHistoryUndoRedo = _0.handleFormulaHistoryUndoRedo,
86
+ capturePreFormulaState = _0.capturePreFormulaState,
87
+ appendFormulaHistoryFromPrimaryEditor = _0.appendFormulaHistoryFromPrimaryEditor;
78
88
  var ZWSP = "\u200B";
79
89
  var inputBoxInnerRef = (0, _react.useRef)(null);
80
- var _x = (0, _react.useState)([]),
81
- linkSelectionHighlightRects = _x[0],
82
- setLinkSelectionHighlightRects = _x[1];
90
+ var _1 = (0, _react.useState)([]),
91
+ linkSelectionHighlightRects = _1[0],
92
+ setLinkSelectionHighlightRects = _1[1];
83
93
  var ensureNotEmpty = function ensureNotEmpty() {
84
94
  var el = inputRef.current;
85
95
  if (!el) return;
@@ -174,8 +184,37 @@ var InputBox = function InputBox() {
174
184
  if (inputRef.current) {
175
185
  inputRef.current.innerHTML = "";
176
186
  }
187
+ resetFormulaHistory();
177
188
  }
178
- }, [context.luckysheetCellUpdate]);
189
+ }, [context.luckysheetCellUpdate, resetFormulaHistory]);
190
+ (0, _react.useEffect)(function () {
191
+ var _a;
192
+ if (_lodash.default.isEmpty(context.luckysheetCellUpdate) || !refs.cellInput.current) {
193
+ formulaAnchorCellRef.current = null;
194
+ lastHandledMouseDragSignatureRef.current = "";
195
+ return;
196
+ }
197
+ var inputText = ((_a = refs.cellInput.current.innerText) === null || _a === void 0 ? void 0 : _a.trim()) || "";
198
+ if (!inputText.startsWith("=")) {
199
+ formulaAnchorCellRef.current = null;
200
+ lastHandledMouseDragSignatureRef.current = "";
201
+ }
202
+ }, [context.luckysheetCellUpdate, refs.cellInput]);
203
+ (0, _react.useEffect)(function () {
204
+ if (_lodash.default.isEmpty(context.luckysheetCellUpdate) || _lodash.default.isEmpty(prevCellUpdate) || _lodash.default.isEqual(prevCellUpdate, context.luckysheetCellUpdate)) {
205
+ return;
206
+ }
207
+ setContext(function (ctx) {
208
+ ctx.formulaRangeHighlight = [];
209
+ ctx.formulaRangeSelect = undefined;
210
+ ctx.formulaCache.selectingRangeIndex = -1;
211
+ ctx.formulaCache.func_selectedrange = undefined;
212
+ ctx.formulaCache.rangestart = false;
213
+ ctx.formulaCache.rangedrag_column_start = false;
214
+ ctx.formulaCache.rangedrag_row_start = false;
215
+ ctx.formulaCache.rangechangeindex = undefined;
216
+ });
217
+ }, [context.luckysheetCellUpdate, prevCellUpdate, setContext]);
179
218
  (0, _react.useEffect)(function () {
180
219
  setIsHidenRC((0, _fortuneCore.isShowHidenCR)(context));
181
220
  }, [context.luckysheet_select_save]);
@@ -185,65 +224,45 @@ var InputBox = function InputBox() {
185
224
  setIsInputBoxActive(false);
186
225
  }
187
226
  }, [firstSelection, (_c = context.rangeDialog) === null || _c === void 0 ? void 0 : _c.show, context.luckysheetCellUpdate]);
227
+ (0, _react.useEffect)(function () {
228
+ if (isInputBoxActive) return;
229
+ setContext(function (ctx) {
230
+ if (_lodash.default.isEmpty(ctx.formulaRangeHighlight) && !ctx.formulaRangeSelect && ctx.formulaCache.selectingRangeIndex === -1 && !ctx.formulaCache.func_selectedrange) {
231
+ return;
232
+ }
233
+ ctx.formulaRangeHighlight = [];
234
+ ctx.formulaRangeSelect = undefined;
235
+ ctx.formulaCache.selectingRangeIndex = -1;
236
+ ctx.formulaCache.func_selectedrange = undefined;
237
+ ctx.formulaCache.rangestart = false;
238
+ ctx.formulaCache.rangedrag_column_start = false;
239
+ ctx.formulaCache.rangedrag_row_start = false;
240
+ ctx.formulaCache.rangechangeindex = undefined;
241
+ ctx.formulaCache.rangeSelectionActive = null;
242
+ });
243
+ }, [isInputBoxActive, setContext]);
188
244
  var getActiveFormula = (0, _react.useCallback)(function () {
189
245
  return document.querySelector(".luckysheet-formula-search-item-active");
190
246
  }, []);
191
247
  var insertSelectedFormula = (0, _react.useCallback)(function (formulaName) {
192
- var _a;
193
- if (/^=[a-zA-Z]+$/.test(inputRef.current.innerText)) {
194
- 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>");
195
- inputRef.current.innerHTML = ht;
196
- var fxEditor = document.getElementById("luckysheet-functionbox-cell");
197
- if (fxEditor) {
198
- fxEditor.innerHTML = ht;
199
- }
200
- (0, _helper.moveCursorToEnd)(inputRef.current);
201
- setContext(function (draftCtx) {
202
- draftCtx.functionCandidates = [];
203
- draftCtx.defaultCandidates = [];
204
- draftCtx.functionHint = formulaName;
205
- });
206
- return;
207
- }
208
- var textEditor = document.getElementById("luckysheet-rich-text-editor");
248
+ var textEditor = inputRef.current;
209
249
  if (!textEditor) return;
210
- textEditor.focus();
211
- var selection = window.getSelection();
212
- var range = (selection === null || selection === void 0 ? void 0 : selection.rangeCount) ? selection.getRangeAt(0) : null;
213
- if (!selection || !range || !textEditor.contains(range.startContainer)) {
214
- range = document.createRange();
215
- range.selectNodeContents(textEditor);
216
- range.collapse(false);
217
- selection = window.getSelection();
218
- selection === null || selection === void 0 ? void 0 : selection.removeAllRanges();
219
- selection === null || selection === void 0 ? void 0 : selection.addRange(range);
220
- }
221
- var searchTxt = ((_a = (0, _fortuneCore.getrangeseleciton)()) === null || _a === void 0 ? void 0 : _a.textContent) || "";
222
- var deleteCount = searchTxt === "=" ? 0 : searchTxt.length;
223
- if (deleteCount > 0 && range.startContainer.nodeType === Node.TEXT_NODE && textEditor.contains(range.startContainer)) {
224
- var startOffset = Math.max(range.startOffset - deleteCount, 0);
225
- var endOffset = range.startOffset;
226
- range.setStart(range.startContainer, startOffset);
227
- range.setEnd(range.startContainer, endOffset);
228
- range.deleteContents();
229
- }
230
- textEditor.querySelectorAll(".luckysheet-formula-text-func, .luckysheet-formula-text-lpar").forEach(function (el) {
231
- return el.remove();
232
- });
233
- var funcNode = new DOMParser().parseFromString("<span dir=\"auto\" class=\"luckysheet-formula-text-func\">".concat(formulaName, "</span>"), "text/html").body.firstChild;
234
- var parNode = new DOMParser().parseFromString("<span dir=\"auto\" class=\"luckysheet-formula-text-lpar\">(</span>", "text/html").body.firstChild;
235
- if (range && parNode && funcNode) {
236
- range.insertNode(funcNode);
237
- range.collapse(false);
238
- range.insertNode(parNode);
239
- range.collapse(false);
240
- selection === null || selection === void 0 ? void 0 : selection.removeAllRanges();
241
- selection === null || selection === void 0 ? void 0 : selection.addRange(range);
242
- }
250
+ var fxEditor = document.getElementById("luckysheet-functionbox-cell");
251
+ var _a = (0, _helper.buildFormulaSuggestionText)(textEditor, formulaName),
252
+ text = _a.text,
253
+ caretOffset = _a.caretOffset;
254
+ var safeText = (0, _fortuneCore.escapeScriptTag)(text);
255
+ var html = safeText.startsWith("=") ? (0, _fortuneCore.functionHTMLGenerate)(safeText) : (0, _fortuneCore.escapeHTMLTag)(safeText);
256
+ textEditor.innerHTML = html;
257
+ if (fxEditor) {
258
+ fxEditor.innerHTML = html;
259
+ }
260
+ (0, _helper.setCursorPosition)(textEditor, caretOffset);
261
+ setShowSearchHint((0, _helper.shouldShowFormulaFunctionList)(textEditor));
243
262
  setContext(function (draftCtx) {
244
263
  draftCtx.functionCandidates = [];
245
264
  draftCtx.defaultCandidates = [];
246
- draftCtx.functionHint = formulaName;
265
+ draftCtx.functionHint = (formulaName || "").toUpperCase();
247
266
  });
248
267
  }, [setContext]);
249
268
  var clearSearchItemActiveClass = (0, _react.useCallback)(function () {
@@ -266,7 +285,7 @@ var InputBox = function InputBox() {
266
285
  var _a, _b;
267
286
  if (isComposingRef.current || !inputRef.current) return;
268
287
  if (e.target.className.includes("sign-fortune")) return;
269
- preText.current = inputRef.current.innerText;
288
+ preTextRef.current = inputRef.current.innerText;
270
289
  var formulaName = (_b = (_a = getActiveFormula()) === null || _a === void 0 ? void 0 : _a.querySelector(".luckysheet-formula-search-func")) === null || _b === void 0 ? void 0 : _b.textContent;
271
290
  var lastSpanText = getLastInputSpanText();
272
291
  if (formulaName && !(0, _helper.isLetterNumberPattern)(lastSpanText)) {
@@ -279,11 +298,150 @@ var InputBox = function InputBox() {
279
298
  event.stopPropagation();
280
299
  event.preventDefault();
281
300
  };
301
+ (0, _react.useEffect)(function () {
302
+ var _a;
303
+ var cellInputEl = refs.cellInput.current;
304
+ if (!((_a = context.luckysheet_select_save) === null || _a === void 0 ? void 0 : _a[0]) || !cellInputEl) return;
305
+ setContext(function (ctx) {
306
+ var _a;
307
+ var currentSelection = (_a = ctx.luckysheet_select_save) === null || _a === void 0 ? void 0 : _a[ctx.luckysheet_select_save.length - 1];
308
+ if (!currentSelection) return;
309
+ if ((0, _fortuneCore.getFormulaEditorOwner)(ctx) === "fx") {
310
+ return;
311
+ }
312
+ (0, _fortuneCore.israngeseleciton)(ctx);
313
+ var keyboardSyncRangeIndex = (0, _fortuneCore.getFormulaRangeIndexForKeyboardSync)(cellInputEl);
314
+ if (skipNextAnchorSelectionSyncRef.current && formulaAnchorCellRef.current) {
315
+ var _b = formulaAnchorCellRef.current,
316
+ anchorRow = _b[0],
317
+ anchorCol = _b[1];
318
+ var isAnchorSelection = currentSelection.row_focus === anchorRow && currentSelection.column_focus === anchorCol;
319
+ if (isAnchorSelection) {
320
+ skipNextAnchorSelectionSyncRef.current = false;
321
+ return;
322
+ }
323
+ }
324
+ var isFormulaMode = (0, _fortuneCore.isFormulaReferenceInputMode)(ctx);
325
+ if (!isFormulaMode) return;
326
+ if (keyboardSyncRangeIndex !== null) {
327
+ ctx.formulaCache.rangechangeindex = keyboardSyncRangeIndex;
328
+ ctx.formulaCache.rangestart = true;
329
+ ctx.formulaCache.rangedrag_column_start = false;
330
+ ctx.formulaCache.rangedrag_row_start = false;
331
+ } else {
332
+ ctx.formulaCache.rangechangeindex = undefined;
333
+ ctx.formulaCache.rangestart = false;
334
+ }
335
+ ctx.formulaCache.rangeSelectionActive = true;
336
+ _fortuneCore.rangeSetValue === null || _fortuneCore.rangeSetValue === void 0 ? void 0 : (0, _fortuneCore.rangeSetValue)(ctx, cellInputEl, {
337
+ row: currentSelection.row,
338
+ column: currentSelection.column
339
+ }, refs.fxInput.current);
340
+ (0, _fortuneCore.rangeHightlightselected)(ctx, cellInputEl);
341
+ if (!_lodash.default.isNil(ctx.formulaCache.rangechangeindex)) {
342
+ ctx.formulaCache.selectingRangeIndex = ctx.formulaCache.rangechangeindex;
343
+ (0, _fortuneCore.createRangeHightlight)(ctx, cellInputEl.innerHTML, ctx.formulaCache.rangechangeindex);
344
+ var rectFromSelection = (0, _fortuneCore.seletedHighlistByindex)(ctx, currentSelection.row[0], currentSelection.row[1], currentSelection.column[0], currentSelection.column[1]);
345
+ if (rectFromSelection) {
346
+ (0, _fortuneCore.createFormulaRangeSelect)(ctx, {
347
+ rangeIndex: ctx.formulaCache.rangechangeindex || 0,
348
+ left: rectFromSelection.left,
349
+ top: rectFromSelection.top,
350
+ width: rectFromSelection.width,
351
+ height: rectFromSelection.height
352
+ });
353
+ }
354
+ }
355
+ });
356
+ }, [context.luckysheet_select_save, (_d = context.rangeDialog) === null || _d === void 0 ? void 0 : _d.show]);
357
+ var formulaMouseDragSignature = function () {
358
+ var _a, _b, _c, _d;
359
+ var r = context.formulaCache.func_selectedrange;
360
+ if (!r) return "";
361
+ return [(_a = r.row) === null || _a === void 0 ? void 0 : _a[0], (_b = r.row) === null || _b === void 0 ? void 0 : _b[1], (_c = r.column) === null || _c === void 0 ? void 0 : _c[0], (_d = r.column) === null || _d === void 0 ? void 0 : _d[1], r.left_move, r.top_move, r.width_move, r.height_move].join(":");
362
+ }();
363
+ (0, _react.useEffect)(function () {
364
+ var _a;
365
+ if (!formulaMouseDragSignature) return;
366
+ if (lastHandledMouseDragSignatureRef.current === formulaMouseDragSignature) {
367
+ return;
368
+ }
369
+ if (!refs.cellInput.current) return;
370
+ var inputText = ((_a = refs.cellInput.current.innerText) === null || _a === void 0 ? void 0 : _a.trim()) || "";
371
+ if (!inputText.startsWith("=")) return;
372
+ var dragRange = context.formulaCache.func_selectedrange;
373
+ if (!dragRange) return;
374
+ lastHandledMouseDragSignatureRef.current = formulaMouseDragSignature;
375
+ setContext(function (ctx) {
376
+ ctx.luckysheet_select_save = [{
377
+ row: [dragRange.row[0], dragRange.row[1]],
378
+ column: [dragRange.column[0], dragRange.column[1]],
379
+ row_focus: _lodash.default.isNil(dragRange.row_focus) ? dragRange.row[0] : dragRange.row_focus,
380
+ column_focus: _lodash.default.isNil(dragRange.column_focus) ? dragRange.column[0] : dragRange.column_focus
381
+ }];
382
+ });
383
+ }, [formulaMouseDragSignature, context.formulaCache.func_selectedrange, refs.cellInput, setContext]);
282
384
  var onKeyDown = (0, _react.useCallback)(function (e) {
283
385
  var _a, _b;
386
+ setContext(function (draftCtx) {
387
+ (0, _fortuneCore.setFormulaEditorOwner)(draftCtx, "cell");
388
+ });
284
389
  lastKeyDownEventRef.current = new KeyboardEvent(e.type, e.nativeEvent);
285
- preText.current = inputRef.current.innerText;
286
- if (e.metaKey && context.luckysheetCellUpdate.length > 0) {
390
+ capturePreFormulaState();
391
+ var currentInputText = ((_b = (_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.innerText) === null || _b === void 0 ? void 0 : _b.trim()) || "";
392
+ if ((e.key === "=" || currentInputText.startsWith("=")) && context.luckysheetCellUpdate.length === 2 && formulaAnchorCellRef.current == null) {
393
+ setContext(function (draftCtx) {
394
+ draftCtx.formulaCache.rangeSelectionActive = null;
395
+ });
396
+ formulaAnchorCellRef.current = [context.luckysheetCellUpdate[0], context.luckysheetCellUpdate[1]];
397
+ }
398
+ if (e.key === "(" && currentInputText.startsWith("=")) {
399
+ setContext(function (draftCtx) {
400
+ draftCtx.formulaCache.rangeSelectionActive = null;
401
+ });
402
+ }
403
+ if (e.key === "," && context.luckysheetCellUpdate.length > 0 && currentInputText.startsWith("=") && formulaAnchorCellRef.current) {
404
+ setContext(function (draftCtx) {
405
+ draftCtx.formulaCache.rangeSelectionActive = null;
406
+ });
407
+ var _c = formulaAnchorCellRef.current,
408
+ anchorRow_1 = _c[0],
409
+ anchorCol_1 = _c[1];
410
+ skipNextAnchorSelectionSyncRef.current = true;
411
+ setTimeout(function () {
412
+ setContext(function (draftCtx) {
413
+ var _a, _b;
414
+ draftCtx.luckysheetCellUpdate = [anchorRow_1, anchorCol_1];
415
+ draftCtx.luckysheet_select_save = [{
416
+ row: [anchorRow_1, anchorRow_1],
417
+ column: [anchorCol_1, anchorCol_1],
418
+ row_focus: anchorRow_1,
419
+ column_focus: anchorCol_1
420
+ }];
421
+ draftCtx.formulaRangeSelect = undefined;
422
+ draftCtx.formulaCache.selectingRangeIndex = -1;
423
+ draftCtx.formulaCache.func_selectedrange = undefined;
424
+ draftCtx.formulaCache.rangechangeindex = undefined;
425
+ draftCtx.formulaCache.rangestart = false;
426
+ draftCtx.formulaCache.rangedrag_column_start = false;
427
+ draftCtx.formulaCache.rangedrag_row_start = false;
428
+ (0, _fortuneCore.createRangeHightlight)(draftCtx, ((_a = refs.cellInput.current) === null || _a === void 0 ? void 0 : _a.innerHTML) || ((_b = refs.fxInput.current) === null || _b === void 0 ? void 0 : _b.innerHTML) || "");
429
+ (0, _fortuneCore.moveHighlightCell)(draftCtx, "down", 0, "rangeOfSelect");
430
+ });
431
+ }, 0);
432
+ }
433
+ if ((e.metaKey || e.ctrlKey) && context.luckysheetCellUpdate.length > 0) {
434
+ if (e.code === "KeyZ" || e.code === "KeyY") {
435
+ var shouldUseFormulaHistory = currentInputText.startsWith("=") || formulaHistoryRef.current.active;
436
+ if (shouldUseFormulaHistory) {
437
+ var handledByFormulaHistory = handleFormulaHistoryUndoRedo(e.code === "KeyY" || e.code === "KeyZ" && e.shiftKey);
438
+ if (handledByFormulaHistory) {
439
+ e.preventDefault();
440
+ }
441
+ }
442
+ e.stopPropagation();
443
+ return;
444
+ }
287
445
  if (e.code === "KeyB") {
288
446
  (0, _fortuneCore.handleBold)(context, inputRef.current);
289
447
  stopPropagation(e);
@@ -303,7 +461,25 @@ var InputBox = function InputBox() {
303
461
  setCommaCount(currentCommaCount);
304
462
  }
305
463
  var allowListNavigation = true;
464
+ var isArrowKey = e.key === "ArrowUp" || e.key === "ArrowDown" || e.key === "ArrowLeft" || e.key === "ArrowRight";
306
465
  if (e.key === "Delete" || e.key === "Backspace") {
466
+ var anchor = formulaAnchorCellRef.current;
467
+ if (anchor != null) {
468
+ var anchorRow_2 = anchor[0],
469
+ anchorCol_2 = anchor[1];
470
+ skipNextAnchorSelectionSyncRef.current = true;
471
+ setTimeout(function () {
472
+ setContext(function (draftCtx) {
473
+ draftCtx.luckysheetCellUpdate = [anchorRow_2, anchorCol_2];
474
+ draftCtx.luckysheet_select_save = [{
475
+ row: [anchorRow_2, anchorRow_2],
476
+ column: [anchorCol_2, anchorCol_2],
477
+ row_focus: anchorRow_2,
478
+ column_focus: anchorCol_2
479
+ }];
480
+ });
481
+ }, 0);
482
+ }
307
483
  if (isComposingRef.current) requestAnimationFrame(ensureNotEmpty);
308
484
  if ((0, _helper.getCursorPosition)(inputRef === null || inputRef === void 0 ? void 0 : inputRef.current) === (inputRef === null || inputRef === void 0 ? void 0 : inputRef.current.innerText.length)) {
309
485
  setTimeout(function () {
@@ -311,39 +487,8 @@ var InputBox = function InputBox() {
311
487
  }, 5);
312
488
  }
313
489
  }
314
- var refCell = placeRef.current;
315
- if (e.key === "ArrowUp") {
316
- refCell = (0, _helper.decrementRow)(placeRef.current);
317
- } else if (e.key === "ArrowDown") {
318
- refCell = (0, _helper.incrementRow)(placeRef.current);
319
- } else if (e.key === "ArrowLeft") {
320
- refCell = (0, _helper.decrementColumn)(placeRef.current);
321
- } else if (e.key === "ArrowRight") {
322
- refCell = (0, _helper.incrementColumn)(placeRef.current);
323
- }
324
- if ((e.key === "ArrowUp" || e.key === "ArrowDown" || e.key === "ArrowLeft" || e.key === "ArrowRight") && !((0, _helper.getCursorPosition)(inputRef === null || inputRef === void 0 ? void 0 : inputRef.current) !== (inputRef === null || inputRef === void 0 ? void 0 : inputRef.current.innerText.length) && e.key === "ArrowRight")) {
325
- var parser = new DOMParser();
326
- var doc = parser.parseFromString("<div>".concat((_a = inputRef === null || inputRef === void 0 ? void 0 : inputRef.current) === null || _a === void 0 ? void 0 : _a.innerHTML, "</div>"), "text/html");
327
- var spans = doc.querySelectorAll("span");
328
- var lastSpan = spans[spans.length - 1];
329
- var notFunctionInit = !((_b = document.getElementById("luckysheet-rich-text-editor")) === null || _b === void 0 ? void 0 : _b.innerText.includes("("));
330
- var arrowRefNotAllowed = (lastSpan === null || lastSpan === void 0 ? void 0 : lastSpan.innerText.includes(")")) || notFunctionInit && /^[a-zA-Z]+$/.test(lastSpan === null || lastSpan === void 0 ? void 0 : lastSpan.innerText) && !_lodash.default.includes(["="], lastSpan === null || lastSpan === void 0 ? void 0 : lastSpan.innerText);
331
- if (((lastSpan === null || lastSpan === void 0 ? void 0 : lastSpan.innerText) === "(" || (lastSpan === null || lastSpan === void 0 ? void 0 : lastSpan.innerText) === "," || (lastSpan === null || lastSpan === void 0 ? void 0 : lastSpan.innerText.includes(":")) || (lastSpan === null || lastSpan === void 0 ? void 0 : lastSpan.innerText) !== ")") && !(lastSpan === null || lastSpan === void 0 ? void 0 : lastSpan.innerText.includes('"')) && !(0, _helper.isLetterNumberPattern)(lastSpan === null || lastSpan === void 0 ? void 0 : lastSpan.innerText) && !arrowRefNotAllowed && !/^[a-zA-Z]+$/.test(lastSpan === null || lastSpan === void 0 ? void 0 : lastSpan.innerText)) {
332
- allowListNavigation = false;
333
- inputRef.current.innerHTML = "".concat(inputRef.current.innerHTML, "<span class=\"fortune-formula-functionrange-cell\" rangeindex=\"0\" dir=\"auto\" style=\"color:#c1232b;\">").concat(refCell, "</span>");
334
- setTimeout(function () {
335
- (0, _helper.moveCursorToEnd)(inputRef.current);
336
- }, 1);
337
- }
338
- if ((0, _helper.isLetterNumberPattern)(lastSpan === null || lastSpan === void 0 ? void 0 : lastSpan.innerText)) {
339
- allowListNavigation = false;
340
- var htmlR = (0, _helper.removeLastSpan)(inputRef === null || inputRef === void 0 ? void 0 : inputRef.current.innerHTML);
341
- inputRef.current.innerHTML = "".concat(htmlR, "<span class=\"fortune-formula-functionrange-cell\" rangeindex=\"0\" dir=\"auto\" style=\"color:#c1232b;\">").concat(refCell, "</span>");
342
- (0, _helper.moveCursorToEnd)(inputRef.current);
343
- setTimeout(function () {
344
- (0, _helper.moveCursorToEnd)(inputRef.current);
345
- }, 1);
346
- }
490
+ if (isArrowKey && (0, _fortuneCore.isFormulaReferenceInputMode)(context)) {
491
+ allowListNavigation = false;
347
492
  }
348
493
  if (e.key === "Escape" && context.luckysheetCellUpdate.length > 0) {
349
494
  setContext(function (draftCtx) {
@@ -399,7 +544,7 @@ var InputBox = function InputBox() {
399
544
  }
400
545
  e.preventDefault();
401
546
  }
402
- }, [clearSearchItemActiveClass, context.luckysheetCellUpdate.length, selectActiveFormula, setContext, firstSelection]);
547
+ }, [capturePreFormulaState, clearSearchItemActiveClass, context.luckysheetCellUpdate.length, handleFormulaHistoryUndoRedo, selectActiveFormula, setContext, firstSelection]);
403
548
  var handleHideShowHint = function handleHideShowHint() {
404
549
  var _a, _b, _c, _d;
405
550
  var searchElFx = (_a = document.getElementsByClassName("fx-search")) === null || _a === void 0 ? void 0 : _a[0];
@@ -420,32 +565,40 @@ var InputBox = function InputBox() {
420
565
  }
421
566
  };
422
567
  var onChange = (0, _react.useCallback)(function (__, isBlur) {
423
- var _a;
568
+ var _a, _b, _c;
424
569
  if (context.isFlvReadOnly) return;
425
570
  handleHideShowHint();
426
- if (((_a = inputRef === null || inputRef === void 0 ? void 0 : inputRef.current) === null || _a === void 0 ? void 0 : _a.innerText.includes("=")) && /^=?[A-Za-z]*$/.test(getLastInputSpanText())) {
427
- setShowSearchHint(true);
428
- } else {
429
- setShowSearchHint(false);
430
- }
571
+ setShowSearchHint((0, _helper.shouldShowFormulaFunctionList)((_a = inputRef === null || inputRef === void 0 ? void 0 : inputRef.current) !== null && _a !== void 0 ? _a : null));
431
572
  if (!isComposingRef.current) {
432
573
  var currentCommaCount = (0, _helper.countCommasBeforeCursor)(inputRef === null || inputRef === void 0 ? void 0 : inputRef.current);
433
574
  setCommaCount(currentCommaCount);
434
575
  }
435
576
  var e = lastKeyDownEventRef.current;
436
- if (!e) return;
577
+ if (!e) {
578
+ var cellEl_1 = refs.cellInput.current;
579
+ if (cellEl_1 && (((_b = cellEl_1.innerText) === null || _b === void 0 ? void 0 : _b.trim().startsWith("=")) || ((_c = cellEl_1.textContent) === null || _c === void 0 ? void 0 : _c.trim().startsWith("=")))) {
580
+ setContext(function (draftCtx) {
581
+ if (!(0, _fortuneCore.isAllowEdit)(draftCtx, draftCtx.luckysheet_select_save)) return;
582
+ (0, _fortuneCore.rangeHightlightselected)(draftCtx, cellEl_1);
583
+ });
584
+ }
585
+ return;
586
+ }
437
587
  var kcode = e.keyCode;
438
588
  if (!kcode) return;
589
+ appendFormulaHistoryFromPrimaryEditor(function () {
590
+ return (0, _helper.getCursorPosition)(inputRef.current);
591
+ });
439
592
  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) {
440
593
  setContext(function (draftCtx) {
441
594
  if ((draftCtx.formulaCache.rangestart || draftCtx.formulaCache.rangedrag_column_start || draftCtx.formulaCache.rangedrag_row_start || (0, _fortuneCore.israngeseleciton)(draftCtx)) && isBlur) return;
442
595
  if (!(0, _fortuneCore.isAllowEdit)(draftCtx, draftCtx.luckysheet_select_save)) {
443
596
  return;
444
597
  }
445
- (0, _fortuneCore.handleFormulaInput)(draftCtx, refs.fxInput.current, refs.cellInput.current, kcode, preText.current);
598
+ (0, _fortuneCore.handleFormulaInput)(draftCtx, refs.fxInput.current, refs.cellInput.current, kcode, preTextRef.current);
446
599
  });
447
600
  }
448
- }, [refs.cellInput, refs.fxInput, setContext]);
601
+ }, [refs.cellInput, refs.fxInput, setContext, appendFormulaHistoryFromPrimaryEditor]);
449
602
  var onPaste = (0, _react.useCallback)(function (e) {
450
603
  if (_lodash.default.isEmpty(context.luckysheetCellUpdate)) {
451
604
  e.preventDefault();
@@ -489,7 +642,7 @@ var InputBox = function InputBox() {
489
642
  zIndex: _lodash.default.isEmpty(context.luckysheetCellUpdate) ? -1 : 19,
490
643
  display: "block"
491
644
  };
492
- }, [firstSelection, (_d = context.rangeDialog) === null || _d === void 0 ? void 0 : _d.show, context.luckysheetCellUpdate, refs.cellArea, isInputBoxActive, frozenPosition, context.scrollLeft, context.scrollTop]);
645
+ }, [firstSelection, (_e = context.rangeDialog) === null || _e === void 0 ? void 0 : _e.show, context.luckysheetCellUpdate, refs.cellArea, isInputBoxActive, frozenPosition, context.scrollLeft, context.scrollTop]);
493
646
  (0, _react.useEffect)(function () {
494
647
  var _a;
495
648
  if (firstSelection && !((_a = context.rangeDialog) === null || _a === void 0 ? void 0 : _a.show) && !isInputBoxActive && !_lodash.default.isEmpty(context.luckysheetCellUpdate)) {
@@ -505,7 +658,7 @@ var InputBox = function InputBox() {
505
658
  setIsInputBoxActive(true);
506
659
  }
507
660
  }
508
- }, [firstSelection, (_e = context.rangeDialog) === null || _e === void 0 ? void 0 : _e.show, context.luckysheetCellUpdate, isInputBoxActive, context.scrollLeft, context.scrollTop, refs.cellArea]);
661
+ }, [firstSelection, (_f = context.rangeDialog) === null || _f === void 0 ? void 0 : _f.show, context.luckysheetCellUpdate, isInputBoxActive, context.scrollLeft, context.scrollTop, refs.cellArea]);
509
662
  var getAddressIndicatorPosition = (0, _react.useCallback)(function () {
510
663
  var _a;
511
664
  if (!firstSelection || ((_a = context.rangeDialog) === null || _a === void 0 ? void 0 : _a.show)) {
@@ -518,7 +671,7 @@ var InputBox = function InputBox() {
518
671
  left: "0",
519
672
  display: "block"
520
673
  };
521
- }, [firstSelection, (_f = context.rangeDialog) === null || _f === void 0 ? void 0 : _f.show]);
674
+ }, [firstSelection, (_g = context.rangeDialog) === null || _g === void 0 ? void 0 : _g.show]);
522
675
  var getCellAddress = (0, _react.useCallback)(function () {
523
676
  if (!firstSelection) return "";
524
677
  var rowIndex = firstSelection.row_focus || 0;
@@ -568,15 +721,15 @@ var InputBox = function InputBox() {
568
721
  };
569
722
  });
570
723
  setLinkSelectionHighlightRects(relative);
571
- }, [(_g = context.linkCard) === null || _g === void 0 ? void 0 : _g.applyToSelection, (_h = context.linkCard) === null || _h === void 0 ? void 0 : _h.selectionOffsets, (_j = context.linkCard) === null || _j === void 0 ? void 0 : _j.r, (_k = context.linkCard) === null || _k === void 0 ? void 0 : _k.c, context.luckysheetCellUpdate]);
724
+ }, [(_h = context.linkCard) === null || _h === void 0 ? void 0 : _h.applyToSelection, (_j = context.linkCard) === null || _j === void 0 ? void 0 : _j.selectionOffsets, (_k = context.linkCard) === null || _k === void 0 ? void 0 : _k.r, (_l = context.linkCard) === null || _l === void 0 ? void 0 : _l.c, context.luckysheetCellUpdate]);
572
725
  var wraperGetCell = function wraperGetCell() {
573
726
  var cell = getCellAddress();
574
- placeRef.current = cell;
575
727
  if (activeRefCell !== cell) {
576
728
  setActiveRefCell(cell);
577
729
  }
578
730
  return activeCell || cell;
579
731
  };
732
+ (0, _useRerenderOnFormulaCaret.useRerenderOnFormulaCaret)(inputRef, context.luckysheetCellUpdate.length > 0);
580
733
  var getFunctionNameFromInput = (0, _react.useCallback)(function () {
581
734
  var _a, _b;
582
735
  var inputText = ((_a = inputRef === null || inputRef === void 0 ? void 0 : inputRef.current) === null || _a === void 0 ? void 0 : _a.innerText) || "";
@@ -593,8 +746,10 @@ var InputBox = function InputBox() {
593
746
  }
594
747
  return null;
595
748
  }, []);
596
- var functionName = context.functionHint || getFunctionNameFromInput();
749
+ var functionName = (_o = (_m = (0, _helper.getFunctionNameFromFormulaCaretSpans)(inputRef.current)) !== null && _m !== void 0 ? _m : context.functionHint) !== null && _o !== void 0 ? _o : getFunctionNameFromInput();
597
750
  var fn = functionName ? context.formulaCache.functionlistMap[functionName] : null;
751
+ var showCellFormulaChrome = context.luckysheetCellUpdate.length > 0 && (0, _fortuneCore.getFormulaEditorOwner)(context) === "cell";
752
+ var inputBoxBaseSelection = isInputBoxActive && firstSelectionActiveCell ? firstSelectionActiveCell : firstSelection;
598
753
  return /*#__PURE__*/_react.default.createElement("div", {
599
754
  className: "luckysheet-input-box",
600
755
  id: "luckysheet-input-box",
@@ -605,16 +760,16 @@ var InputBox = function InputBox() {
605
760
  onMouseUp: function onMouseUp(e) {
606
761
  return e.stopPropagation();
607
762
  }
608
- }, firstSelection && !((_l = context.rangeDialog) === null || _l === void 0 ? void 0 : _l.show) && (/*#__PURE__*/_react.default.createElement("div", {
763
+ }, firstSelection && !((_p = context.rangeDialog) === null || _p === void 0 ? void 0 : _p.show) && (/*#__PURE__*/_react.default.createElement("div", {
609
764
  className: "luckysheet-cell-address-indicator",
610
765
  style: getAddressIndicatorPosition()
611
766
  }, wraperGetCell())), /*#__PURE__*/_react.default.createElement("div", {
612
767
  ref: inputBoxInnerRef,
613
768
  className: "luckysheet-input-box-inner",
614
- style: firstSelection ? __assign({
769
+ style: inputBoxBaseSelection ? __assign({
615
770
  position: "relative",
616
- minWidth: firstSelection.width,
617
- minHeight: firstSelection.height
771
+ minWidth: inputBoxBaseSelection.width,
772
+ minHeight: inputBoxBaseSelection.height
618
773
  }, inputBoxStyle) : {
619
774
  position: "relative"
620
775
  }
@@ -635,10 +790,23 @@ var InputBox = function InputBox() {
635
790
  },
636
791
  onMouseUp: function onMouseUp() {
637
792
  handleHideShowHint();
793
+ setContext(function (draftCtx) {
794
+ (0, _fortuneCore.setFormulaEditorOwner)(draftCtx, "cell");
795
+ });
638
796
  if (!isComposingRef.current) {
639
797
  var currentCommaCount = (0, _helper.countCommasBeforeCursor)(inputRef === null || inputRef === void 0 ? void 0 : inputRef.current);
640
798
  setCommaCount(currentCommaCount);
641
799
  }
800
+ var editor = inputRef.current;
801
+ if (!editor) return;
802
+ setContext(function (draftCtx) {
803
+ if (draftCtx.formulaCache.rangeSelectionActive !== true) return;
804
+ var clickedInsideManagedRange = (0, _fortuneCore.getFormulaRangeIndexAtCaret)(editor) !== null;
805
+ var atValidInsertionPoint = (0, _fortuneCore.isCaretAtValidFormulaRangeInsertionPoint)(editor);
806
+ if (clickedInsideManagedRange || !atValidInsertionPoint) {
807
+ (0, _fortuneCore.markRangeSelectionDirty)(draftCtx);
808
+ }
809
+ });
642
810
  },
643
811
  innerRef: function innerRef(e) {
644
812
  inputRef.current = e;
@@ -681,7 +849,7 @@ var InputBox = function InputBox() {
681
849
  backgroundColor: "rgba(0, 123, 255, 0.25)"
682
850
  }
683
851
  });
684
- })))), (context.functionCandidates.length > 0 || context.functionHint || context.defaultCandidates.length > 0 || fn) && (/*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, showSearchHint && (/*#__PURE__*/_react.default.createElement(_FormulaSearch.default, {
852
+ })))), (context.functionCandidates.length > 0 || context.functionHint || context.defaultCandidates.length > 0 || fn) && showCellFormulaChrome && (/*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, showSearchHint && (/*#__PURE__*/_react.default.createElement(_FormulaSearch.default, {
685
853
  onMouseMove: function onMouseMove(e) {
686
854
  if (document.getElementById("luckysheet-formula-search-c")) {
687
855
  var hoveredItem = e.target.closest(".luckysheet-formula-search-item");
@@ -696,12 +864,12 @@ var InputBox = function InputBox() {
696
864
  }
697
865
  })), /*#__PURE__*/_react.default.createElement("div", {
698
866
  className: "cell-hint"
699
- }, showFormulaHint && fn && (/*#__PURE__*/_react.default.createElement(_FormulaHint.default, {
867
+ }, showFormulaHint && fn && !showSearchHint && (/*#__PURE__*/_react.default.createElement(_FormulaHint.default, {
700
868
  handleShowFormulaHint: handleShowFormulaHint,
701
869
  showFormulaHint: showFormulaHint,
702
870
  commaCount: commaCount,
703
871
  functionName: functionName
704
- })), !showFormulaHint && fn && (/*#__PURE__*/_react.default.createElement(_ui.Tooltip, {
872
+ })), !showFormulaHint && fn && !showSearchHint && (/*#__PURE__*/_react.default.createElement(_ui.Tooltip, {
705
873
  text: "Turn on formula suggestions (F10)",
706
874
  placement: "top",
707
875
  defaultOpen: true,