@fileverse-dev/fortune-react 1.3.11-input-ref → 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.
@@ -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,59 +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, _m;
33
- var _o = (0, _react.useContext)(_context.default),
34
- context = _o.context,
35
- setContext = _o.setContext,
36
- refs = _o.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 _p = (0, _react.useState)(false),
42
- isHidenRC = _p[0],
43
- setIsHidenRC = _p[1];
44
- var _q = (0, _react.useState)(false),
45
- isInputBoxActive = _q[0],
46
- setIsInputBoxActive = _q[1];
47
- var _r = (0, _react.useState)(""),
48
- activeCell = _r[0],
49
- setActiveCell = _r[1];
50
- var _s = (0, _react.useState)(""),
51
- activeRefCell = _s[0],
52
- setActiveRefCell = _s[1];
53
- var _t = (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 = _t[0],
58
- setFrozenPosition = _t[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 _u = (0, _react.useState)({}),
61
- firstSelectionActiveCell = _u[0],
62
- setFirstSelectionActiveCell = _u[1];
63
- var _v = (0, _react.useState)(0),
64
- commaCount = _v[0],
65
- setCommaCount = _v[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 _w = (0, _react.useState)(!hideFormulaHintLocal),
68
- showFormulaHint = _w[0],
69
- setShowFormulaHint = _w[1];
70
- var _x = (0, _react.useState)(false),
71
- showSearchHint = _x[0],
72
- setShowSearchHint = _x[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
77
  var isComposingRef = (0, _react.useRef)(false);
77
78
  var formulaAnchorCellRef = (0, _react.useRef)(null);
78
79
  var skipNextAnchorSelectionSyncRef = (0, _react.useRef)(false);
79
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;
80
88
  var ZWSP = "\u200B";
81
89
  var inputBoxInnerRef = (0, _react.useRef)(null);
82
- var _y = (0, _react.useState)([]),
83
- linkSelectionHighlightRects = _y[0],
84
- setLinkSelectionHighlightRects = _y[1];
90
+ var _1 = (0, _react.useState)([]),
91
+ linkSelectionHighlightRects = _1[0],
92
+ setLinkSelectionHighlightRects = _1[1];
85
93
  var ensureNotEmpty = function ensureNotEmpty() {
86
94
  var el = inputRef.current;
87
95
  if (!el) return;
@@ -176,8 +184,9 @@ var InputBox = function InputBox() {
176
184
  if (inputRef.current) {
177
185
  inputRef.current.innerHTML = "";
178
186
  }
187
+ resetFormulaHistory();
179
188
  }
180
- }, [context.luckysheetCellUpdate]);
189
+ }, [context.luckysheetCellUpdate, resetFormulaHistory]);
181
190
  (0, _react.useEffect)(function () {
182
191
  var _a;
183
192
  if (_lodash.default.isEmpty(context.luckysheetCellUpdate) || !refs.cellInput.current) {
@@ -249,10 +258,11 @@ var InputBox = function InputBox() {
249
258
  fxEditor.innerHTML = html;
250
259
  }
251
260
  (0, _helper.setCursorPosition)(textEditor, caretOffset);
261
+ setShowSearchHint((0, _helper.shouldShowFormulaFunctionList)(textEditor));
252
262
  setContext(function (draftCtx) {
253
263
  draftCtx.functionCandidates = [];
254
264
  draftCtx.defaultCandidates = [];
255
- draftCtx.functionHint = formulaName;
265
+ draftCtx.functionHint = (formulaName || "").toUpperCase();
256
266
  });
257
267
  }, [setContext]);
258
268
  var clearSearchItemActiveClass = (0, _react.useCallback)(function () {
@@ -275,7 +285,7 @@ var InputBox = function InputBox() {
275
285
  var _a, _b;
276
286
  if (isComposingRef.current || !inputRef.current) return;
277
287
  if (e.target.className.includes("sign-fortune")) return;
278
- preText.current = inputRef.current.innerText;
288
+ preTextRef.current = inputRef.current.innerText;
279
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;
280
290
  var lastSpanText = getLastInputSpanText();
281
291
  if (formulaName && !(0, _helper.isLetterNumberPattern)(lastSpanText)) {
@@ -296,8 +306,7 @@ var InputBox = function InputBox() {
296
306
  var _a;
297
307
  var currentSelection = (_a = ctx.luckysheet_select_save) === null || _a === void 0 ? void 0 : _a[ctx.luckysheet_select_save.length - 1];
298
308
  if (!currentSelection) return;
299
- var isMouseFormulaRangeDrag = !!ctx.formulaCache.func_selectedrange && (ctx.formulaCache.rangestart || ctx.formulaCache.rangedrag_column_start || ctx.formulaCache.rangedrag_row_start);
300
- if (isMouseFormulaRangeDrag) {
309
+ if ((0, _fortuneCore.getFormulaEditorOwner)(ctx) === "fx") {
301
310
  return;
302
311
  }
303
312
  (0, _fortuneCore.israngeseleciton)(ctx);
@@ -374,8 +383,11 @@ var InputBox = function InputBox() {
374
383
  }, [formulaMouseDragSignature, context.formulaCache.func_selectedrange, refs.cellInput, setContext]);
375
384
  var onKeyDown = (0, _react.useCallback)(function (e) {
376
385
  var _a, _b;
386
+ setContext(function (draftCtx) {
387
+ (0, _fortuneCore.setFormulaEditorOwner)(draftCtx, "cell");
388
+ });
377
389
  lastKeyDownEventRef.current = new KeyboardEvent(e.type, e.nativeEvent);
378
- preText.current = inputRef.current.innerText;
390
+ capturePreFormulaState();
379
391
  var currentInputText = ((_b = (_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.innerText) === null || _b === void 0 ? void 0 : _b.trim()) || "";
380
392
  if ((e.key === "=" || currentInputText.startsWith("=")) && context.luckysheetCellUpdate.length === 2 && formulaAnchorCellRef.current == null) {
381
393
  setContext(function (draftCtx) {
@@ -398,6 +410,7 @@ var InputBox = function InputBox() {
398
410
  skipNextAnchorSelectionSyncRef.current = true;
399
411
  setTimeout(function () {
400
412
  setContext(function (draftCtx) {
413
+ var _a, _b;
401
414
  draftCtx.luckysheetCellUpdate = [anchorRow_1, anchorCol_1];
402
415
  draftCtx.luckysheet_select_save = [{
403
416
  row: [anchorRow_1, anchorRow_1],
@@ -405,7 +418,6 @@ var InputBox = function InputBox() {
405
418
  row_focus: anchorRow_1,
406
419
  column_focus: anchorCol_1
407
420
  }];
408
- draftCtx.formulaRangeHighlight = [];
409
421
  draftCtx.formulaRangeSelect = undefined;
410
422
  draftCtx.formulaCache.selectingRangeIndex = -1;
411
423
  draftCtx.formulaCache.func_selectedrange = undefined;
@@ -413,11 +425,23 @@ var InputBox = function InputBox() {
413
425
  draftCtx.formulaCache.rangestart = false;
414
426
  draftCtx.formulaCache.rangedrag_column_start = false;
415
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) || "");
416
429
  (0, _fortuneCore.moveHighlightCell)(draftCtx, "down", 0, "rangeOfSelect");
417
430
  });
418
431
  }, 0);
419
432
  }
420
- if (e.metaKey && context.luckysheetCellUpdate.length > 0) {
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
+ }
421
445
  if (e.code === "KeyB") {
422
446
  (0, _fortuneCore.handleBold)(context, inputRef.current);
423
447
  stopPropagation(e);
@@ -439,6 +463,23 @@ var InputBox = function InputBox() {
439
463
  var allowListNavigation = true;
440
464
  var isArrowKey = e.key === "ArrowUp" || e.key === "ArrowDown" || e.key === "ArrowLeft" || e.key === "ArrowRight";
441
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
+ }
442
483
  if (isComposingRef.current) requestAnimationFrame(ensureNotEmpty);
443
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)) {
444
485
  setTimeout(function () {
@@ -503,7 +544,7 @@ var InputBox = function InputBox() {
503
544
  }
504
545
  e.preventDefault();
505
546
  }
506
- }, [clearSearchItemActiveClass, context.luckysheetCellUpdate.length, selectActiveFormula, setContext, firstSelection]);
547
+ }, [capturePreFormulaState, clearSearchItemActiveClass, context.luckysheetCellUpdate.length, handleFormulaHistoryUndoRedo, selectActiveFormula, setContext, firstSelection]);
507
548
  var handleHideShowHint = function handleHideShowHint() {
508
549
  var _a, _b, _c, _d;
509
550
  var searchElFx = (_a = document.getElementsByClassName("fx-search")) === null || _a === void 0 ? void 0 : _a[0];
@@ -527,11 +568,7 @@ var InputBox = function InputBox() {
527
568
  var _a, _b, _c;
528
569
  if (context.isFlvReadOnly) return;
529
570
  handleHideShowHint();
530
- 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())) {
531
- setShowSearchHint(true);
532
- } else {
533
- setShowSearchHint(false);
534
- }
571
+ setShowSearchHint((0, _helper.shouldShowFormulaFunctionList)((_a = inputRef === null || inputRef === void 0 ? void 0 : inputRef.current) !== null && _a !== void 0 ? _a : null));
535
572
  if (!isComposingRef.current) {
536
573
  var currentCommaCount = (0, _helper.countCommasBeforeCursor)(inputRef === null || inputRef === void 0 ? void 0 : inputRef.current);
537
574
  setCommaCount(currentCommaCount);
@@ -549,16 +586,19 @@ var InputBox = function InputBox() {
549
586
  }
550
587
  var kcode = e.keyCode;
551
588
  if (!kcode) return;
589
+ appendFormulaHistoryFromPrimaryEditor(function () {
590
+ return (0, _helper.getCursorPosition)(inputRef.current);
591
+ });
552
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) {
553
593
  setContext(function (draftCtx) {
554
594
  if ((draftCtx.formulaCache.rangestart || draftCtx.formulaCache.rangedrag_column_start || draftCtx.formulaCache.rangedrag_row_start || (0, _fortuneCore.israngeseleciton)(draftCtx)) && isBlur) return;
555
595
  if (!(0, _fortuneCore.isAllowEdit)(draftCtx, draftCtx.luckysheet_select_save)) {
556
596
  return;
557
597
  }
558
- (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);
559
599
  });
560
600
  }
561
- }, [refs.cellInput, refs.fxInput, setContext]);
601
+ }, [refs.cellInput, refs.fxInput, setContext, appendFormulaHistoryFromPrimaryEditor]);
562
602
  var onPaste = (0, _react.useCallback)(function (e) {
563
603
  if (_lodash.default.isEmpty(context.luckysheetCellUpdate)) {
564
604
  e.preventDefault();
@@ -689,6 +729,7 @@ var InputBox = function InputBox() {
689
729
  }
690
730
  return activeCell || cell;
691
731
  };
732
+ (0, _useRerenderOnFormulaCaret.useRerenderOnFormulaCaret)(inputRef, context.luckysheetCellUpdate.length > 0);
692
733
  var getFunctionNameFromInput = (0, _react.useCallback)(function () {
693
734
  var _a, _b;
694
735
  var inputText = ((_a = inputRef === null || inputRef === void 0 ? void 0 : inputRef.current) === null || _a === void 0 ? void 0 : _a.innerText) || "";
@@ -705,8 +746,9 @@ var InputBox = function InputBox() {
705
746
  }
706
747
  return null;
707
748
  }, []);
708
- 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();
709
750
  var fn = functionName ? context.formulaCache.functionlistMap[functionName] : null;
751
+ var showCellFormulaChrome = context.luckysheetCellUpdate.length > 0 && (0, _fortuneCore.getFormulaEditorOwner)(context) === "cell";
710
752
  var inputBoxBaseSelection = isInputBoxActive && firstSelectionActiveCell ? firstSelectionActiveCell : firstSelection;
711
753
  return /*#__PURE__*/_react.default.createElement("div", {
712
754
  className: "luckysheet-input-box",
@@ -718,7 +760,7 @@ var InputBox = function InputBox() {
718
760
  onMouseUp: function onMouseUp(e) {
719
761
  return e.stopPropagation();
720
762
  }
721
- }, firstSelection && !((_m = context.rangeDialog) === null || _m === void 0 ? void 0 : _m.show) && (/*#__PURE__*/_react.default.createElement("div", {
763
+ }, firstSelection && !((_p = context.rangeDialog) === null || _p === void 0 ? void 0 : _p.show) && (/*#__PURE__*/_react.default.createElement("div", {
722
764
  className: "luckysheet-cell-address-indicator",
723
765
  style: getAddressIndicatorPosition()
724
766
  }, wraperGetCell())), /*#__PURE__*/_react.default.createElement("div", {
@@ -748,10 +790,23 @@ var InputBox = function InputBox() {
748
790
  },
749
791
  onMouseUp: function onMouseUp() {
750
792
  handleHideShowHint();
793
+ setContext(function (draftCtx) {
794
+ (0, _fortuneCore.setFormulaEditorOwner)(draftCtx, "cell");
795
+ });
751
796
  if (!isComposingRef.current) {
752
797
  var currentCommaCount = (0, _helper.countCommasBeforeCursor)(inputRef === null || inputRef === void 0 ? void 0 : inputRef.current);
753
798
  setCommaCount(currentCommaCount);
754
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
+ });
755
810
  },
756
811
  innerRef: function innerRef(e) {
757
812
  inputRef.current = e;
@@ -794,7 +849,7 @@ var InputBox = function InputBox() {
794
849
  backgroundColor: "rgba(0, 123, 255, 0.25)"
795
850
  }
796
851
  });
797
- })))), (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, {
798
853
  onMouseMove: function onMouseMove(e) {
799
854
  if (document.getElementById("luckysheet-formula-search-c")) {
800
855
  var hoveredItem = e.target.closest(".luckysheet-formula-search-item");
@@ -809,12 +864,12 @@ var InputBox = function InputBox() {
809
864
  }
810
865
  })), /*#__PURE__*/_react.default.createElement("div", {
811
866
  className: "cell-hint"
812
- }, showFormulaHint && fn && (/*#__PURE__*/_react.default.createElement(_FormulaHint.default, {
867
+ }, showFormulaHint && fn && !showSearchHint && (/*#__PURE__*/_react.default.createElement(_FormulaHint.default, {
813
868
  handleShowFormulaHint: handleShowFormulaHint,
814
869
  showFormulaHint: showFormulaHint,
815
870
  commaCount: commaCount,
816
871
  functionName: functionName
817
- })), !showFormulaHint && fn && (/*#__PURE__*/_react.default.createElement(_ui.Tooltip, {
872
+ })), !showFormulaHint && fn && !showSearchHint && (/*#__PURE__*/_react.default.createElement(_ui.Tooltip, {
818
873
  text: "Turn on formula suggestions (F10)",
819
874
  placement: "top",
820
875
  defaultOpen: true,
@@ -6,6 +6,8 @@ export declare function buildFormulaSuggestionText(editableDiv: HTMLDivElement,
6
6
  caretOffset: number;
7
7
  };
8
8
  export declare function isLetterNumberPattern(str: string): boolean;
9
+ export declare function shouldShowFormulaFunctionList(editor: HTMLDivElement | null): boolean;
10
+ export declare function getFunctionNameFromFormulaCaretSpans(editor: HTMLDivElement | null): string | null;
9
11
  export declare function removeLastSpan(htmlString: string): string;
10
12
  export declare function numberToColumn(colNumber: number): string;
11
13
  export declare function incrementColumn(cell: string): string;
@@ -8,6 +8,7 @@ exports.countCommasBeforeCursor = countCommasBeforeCursor;
8
8
  exports.decrementColumn = decrementColumn;
9
9
  exports.decrementRow = decrementRow;
10
10
  exports.getCursorPosition = getCursorPosition;
11
+ exports.getFunctionNameFromFormulaCaretSpans = getFunctionNameFromFormulaCaretSpans;
11
12
  exports.incrementColumn = incrementColumn;
12
13
  exports.incrementRow = incrementRow;
13
14
  exports.isLetterNumberPattern = isLetterNumberPattern;
@@ -15,6 +16,7 @@ exports.moveCursorToEnd = moveCursorToEnd;
15
16
  exports.numberToColumn = numberToColumn;
16
17
  exports.removeLastSpan = removeLastSpan;
17
18
  exports.setCursorPosition = setCursorPosition;
19
+ exports.shouldShowFormulaFunctionList = shouldShowFormulaFunctionList;
18
20
  function moveCursorToEnd(editableDiv) {
19
21
  editableDiv.focus();
20
22
  var range = document.createRange();
@@ -91,6 +93,49 @@ function isLetterNumberPattern(str) {
91
93
  var regex = /^[a-zA-Z]+\d+$/;
92
94
  return regex.test(str);
93
95
  }
96
+ function shouldShowFormulaFunctionList(editor) {
97
+ var _a, _b;
98
+ if (!editor) return false;
99
+ if (!((_a = editor.innerText) === null || _a === void 0 ? void 0 : _a.includes("="))) return false;
100
+ var parser = new DOMParser();
101
+ var doc = parser.parseFromString("<div>".concat(editor.innerHTML, "</div>"), "text/html");
102
+ var spans = doc.querySelectorAll("span");
103
+ var lastSpan = spans[spans.length - 1];
104
+ var lastText = (_b = lastSpan === null || lastSpan === void 0 ? void 0 : lastSpan.innerText) !== null && _b !== void 0 ? _b : "";
105
+ return /^=?[A-Za-z]*$/.test(lastText);
106
+ }
107
+ var FORMULA_FUNC_CLASS = "luckysheet-formula-text-func";
108
+ var FORMULA_LPAR_CLASS = "luckysheet-formula-text-lpar";
109
+ function getFunctionNameFromFormulaCaretSpans(editor) {
110
+ var _a, _b;
111
+ if (!editor) return null;
112
+ var sel = window.getSelection();
113
+ if (!(sel === null || sel === void 0 ? void 0 : sel.rangeCount)) return null;
114
+ var range = sel.getRangeAt(0);
115
+ if (!editor.contains(range.startContainer)) return null;
116
+ var n = range.startContainer;
117
+ while (n && n !== editor) {
118
+ if (n.nodeType === Node.ELEMENT_NODE) {
119
+ var elem = n;
120
+ if (elem.classList.contains(FORMULA_FUNC_CLASS)) {
121
+ var next = elem.nextElementSibling;
122
+ if (next === null || next === void 0 ? void 0 : next.classList.contains(FORMULA_LPAR_CLASS)) {
123
+ var name_1 = (_a = elem.textContent) === null || _a === void 0 ? void 0 : _a.trim();
124
+ return name_1 ? name_1.toUpperCase() : null;
125
+ }
126
+ }
127
+ if (elem.classList.contains(FORMULA_LPAR_CLASS)) {
128
+ var prev = elem.previousElementSibling;
129
+ if (prev === null || prev === void 0 ? void 0 : prev.classList.contains(FORMULA_FUNC_CLASS)) {
130
+ var name_2 = (_b = prev.textContent) === null || _b === void 0 ? void 0 : _b.trim();
131
+ return name_2 ? name_2.toUpperCase() : null;
132
+ }
133
+ }
134
+ }
135
+ n = n.parentNode;
136
+ }
137
+ return null;
138
+ }
94
139
  function removeLastSpan(htmlString) {
95
140
  var container = document.createElement("div");
96
141
  container.innerHTML = htmlString;
@@ -356,7 +356,7 @@
356
356
  }
357
357
 
358
358
  .luckysheet-formula-text-color {
359
- color: black;
359
+ color: darkred;
360
360
  }
361
361
 
362
362
  .luckysheet-formula-text-string {
@@ -887,7 +887,7 @@ var Workbook = /*#__PURE__*/_react.default.forwardRef(function (_a, ref) {
887
887
  });
888
888
  }
889
889
  var nativeEvent = e.nativeEvent;
890
- if ((e.ctrlKey || e.metaKey) && e.code === "KeyZ") {
890
+ if ((e.ctrlKey || e.metaKey) && e.code === "KeyZ" && context.luckysheetCellUpdate.length === 0) {
891
891
  if (e.shiftKey) {
892
892
  handleRedo();
893
893
  } else {
@@ -896,7 +896,7 @@ var Workbook = /*#__PURE__*/_react.default.forwardRef(function (_a, ref) {
896
896
  e.stopPropagation();
897
897
  return;
898
898
  }
899
- if ((e.ctrlKey || e.metaKey) && e.code === "KeyY") {
899
+ if ((e.ctrlKey || e.metaKey) && e.code === "KeyY" && context.luckysheetCellUpdate.length === 0) {
900
900
  handleRedo();
901
901
  e.stopPropagation();
902
902
  e.preventDefault();
@@ -0,0 +1,24 @@
1
+ import { type RefObject } from "react";
2
+ import { type Context } from "@fileverse-dev/fortune-core";
3
+ import type { SetContextOptions } from "../context";
4
+ export type FormulaHistoryEntry = {
5
+ text: string;
6
+ caret: number;
7
+ spanValues: string[];
8
+ };
9
+ export type FormulaEditorHistoryPrimary = "cell" | "fx";
10
+ type SetContext = (recipe: (ctx: Context) => void, options?: SetContextOptions) => void;
11
+ export declare function useFormulaEditorHistory(primaryRef: RefObject<HTMLDivElement | null>, cellInputRef: RefObject<HTMLDivElement | null>, fxInputRef: RefObject<HTMLDivElement | null>, setContext: SetContext, primary: FormulaEditorHistoryPrimary): {
12
+ formulaHistoryRef: RefObject<{
13
+ active: boolean;
14
+ entries: FormulaHistoryEntry[];
15
+ index: number;
16
+ }>;
17
+ preTextRef: RefObject<string>;
18
+ preFormulaSpanValuesRef: RefObject<string[] | null>;
19
+ resetFormulaHistory: () => void;
20
+ handleFormulaHistoryUndoRedo: (isRedo: boolean) => boolean;
21
+ capturePreFormulaState: () => void;
22
+ appendFormulaHistoryFromPrimaryEditor: (getCaret: () => number) => void;
23
+ };
24
+ export {};
@@ -0,0 +1,126 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.useFormulaEditorHistory = useFormulaEditorHistory;
7
+ var _react = require("react");
8
+ var _lodash = _interopRequireDefault(require("lodash"));
9
+ var _fortuneCore = require("@fileverse-dev/fortune-core");
10
+ var _helper = require("../components/SheetOverlay/helper");
11
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
12
+ var MAX_FORMULA_HISTORY = 100;
13
+ function useFormulaEditorHistory(primaryRef, cellInputRef, fxInputRef, setContext, primary) {
14
+ var preTextRef = (0, _react.useRef)("");
15
+ var preFormulaSpanValuesRef = (0, _react.useRef)(null);
16
+ var formulaHistoryRef = (0, _react.useRef)({
17
+ active: false,
18
+ entries: [],
19
+ index: -1
20
+ });
21
+ var resetFormulaHistory = (0, _react.useCallback)(function () {
22
+ formulaHistoryRef.current = {
23
+ active: false,
24
+ entries: [],
25
+ index: -1
26
+ };
27
+ preFormulaSpanValuesRef.current = null;
28
+ }, []);
29
+ var pushFormulaHistoryEntry = (0, _react.useCallback)(function (entry) {
30
+ var history = formulaHistoryRef.current;
31
+ var current = history.entries[history.index];
32
+ if (current && current.spanValues.length > 0 && entry.spanValues.length > 0 && _lodash.default.isEqual(current.spanValues, entry.spanValues)) {
33
+ return;
34
+ }
35
+ if (current && current.spanValues.length === 0 && entry.spanValues.length === 0 && current.text === entry.text && current.caret === entry.caret) {
36
+ return;
37
+ }
38
+ var nextEntries = history.entries.slice(0, history.index + 1);
39
+ nextEntries.push(entry);
40
+ if (nextEntries.length > MAX_FORMULA_HISTORY) {
41
+ nextEntries.shift();
42
+ }
43
+ history.entries = nextEntries;
44
+ history.index = nextEntries.length - 1;
45
+ history.active = true;
46
+ }, []);
47
+ var applyFormulaHistoryEntry = (0, _react.useCallback)(function (entry) {
48
+ var primaryEl = primaryRef.current;
49
+ if (!primaryEl) return;
50
+ var safeText = (0, _fortuneCore.escapeScriptTag)(entry.text || "");
51
+ var html = safeText.startsWith("=") ? (0, _fortuneCore.functionHTMLGenerate)(safeText) : (0, _fortuneCore.escapeHTMLTag)(safeText);
52
+ var cell = cellInputRef.current;
53
+ var fx = fxInputRef.current;
54
+ primaryEl.innerHTML = html;
55
+ if (primary === "cell") {
56
+ if (fx) fx.innerHTML = html;
57
+ } else if (cell) {
58
+ cell.innerHTML = html;
59
+ }
60
+ (0, _helper.setCursorPosition)(primaryEl, Math.min(entry.caret, entry.text.length));
61
+ setContext(function (draftCtx) {
62
+ if (primary === "cell") {
63
+ if (!cellInputRef.current) return;
64
+ (0, _fortuneCore.handleFormulaInput)(draftCtx, fxInputRef.current, cellInputRef.current, 0);
65
+ } else {
66
+ if (!fxInputRef.current) return;
67
+ (0, _fortuneCore.handleFormulaInput)(draftCtx, cellInputRef.current, fxInputRef.current, 0);
68
+ }
69
+ });
70
+ }, [cellInputRef, fxInputRef, primary, primaryRef, setContext]);
71
+ var handleFormulaHistoryUndoRedo = (0, _react.useCallback)(function (isRedo) {
72
+ var history = formulaHistoryRef.current;
73
+ if (!history.active || history.entries.length === 0) return false;
74
+ var nextIndex = isRedo ? history.index + 1 : history.index - 1;
75
+ if (nextIndex < 0 || nextIndex >= history.entries.length) return true;
76
+ history.index = nextIndex;
77
+ applyFormulaHistoryEntry(history.entries[nextIndex]);
78
+ return true;
79
+ }, [applyFormulaHistoryEntry]);
80
+ var capturePreFormulaState = (0, _react.useCallback)(function () {
81
+ var el = primaryRef.current;
82
+ if (!el) return;
83
+ preTextRef.current = el.innerText;
84
+ preFormulaSpanValuesRef.current = Array.from(el.querySelectorAll("span.fortune-formula-functionrange-cell")).map(function (node) {
85
+ var _a;
86
+ return (_a = node.textContent) !== null && _a !== void 0 ? _a : "";
87
+ });
88
+ }, [primaryRef]);
89
+ var appendFormulaHistoryFromPrimaryEditor = (0, _react.useCallback)(function (getCaret) {
90
+ var _a, _b;
91
+ var el = primaryRef.current;
92
+ if (!el) return;
93
+ var currentText = el.innerText || "";
94
+ if (currentText.startsWith("=")) {
95
+ var caret = getCaret();
96
+ var spanValues = Array.from(el.querySelectorAll("span.fortune-formula-functionrange-cell")).map(function (node) {
97
+ var _a;
98
+ return (_a = node.textContent) !== null && _a !== void 0 ? _a : "";
99
+ });
100
+ if (!formulaHistoryRef.current.active) {
101
+ var seedText = preTextRef.current || "";
102
+ pushFormulaHistoryEntry({
103
+ text: seedText,
104
+ caret: Math.min(caret, seedText.length),
105
+ spanValues: (_b = (_a = preFormulaSpanValuesRef.current) !== null && _a !== void 0 ? _a : spanValues) !== null && _b !== void 0 ? _b : []
106
+ });
107
+ }
108
+ pushFormulaHistoryEntry({
109
+ text: currentText,
110
+ caret: caret,
111
+ spanValues: spanValues
112
+ });
113
+ } else if (formulaHistoryRef.current.active) {
114
+ resetFormulaHistory();
115
+ }
116
+ }, [primaryRef, pushFormulaHistoryEntry, resetFormulaHistory]);
117
+ return {
118
+ formulaHistoryRef: formulaHistoryRef,
119
+ preTextRef: preTextRef,
120
+ preFormulaSpanValuesRef: preFormulaSpanValuesRef,
121
+ resetFormulaHistory: resetFormulaHistory,
122
+ handleFormulaHistoryUndoRedo: handleFormulaHistoryUndoRedo,
123
+ capturePreFormulaState: capturePreFormulaState,
124
+ appendFormulaHistoryFromPrimaryEditor: appendFormulaHistoryFromPrimaryEditor
125
+ };
126
+ }
@@ -0,0 +1,2 @@
1
+ import { type RefObject } from "react";
2
+ export declare function useRerenderOnFormulaCaret(editorRef: RefObject<HTMLDivElement | null>, editSessionActive: boolean): void;
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.useRerenderOnFormulaCaret = useRerenderOnFormulaCaret;
7
+ var _react = require("react");
8
+ function useRerenderOnFormulaCaret(editorRef, editSessionActive) {
9
+ var _a = (0, _react.useState)(0),
10
+ bump = _a[1];
11
+ (0, _react.useEffect)(function () {
12
+ if (!editSessionActive) return;
13
+ var onSelectionChange = function onSelectionChange() {
14
+ var _a, _b;
15
+ var el = editorRef.current;
16
+ if (!el) return;
17
+ var text = (_b = (_a = el.innerText) === null || _a === void 0 ? void 0 : _a.trim()) !== null && _b !== void 0 ? _b : "";
18
+ if (!text.startsWith("=")) return;
19
+ var sel = window.getSelection();
20
+ if (!(sel === null || sel === void 0 ? void 0 : sel.rangeCount) || !el.contains(sel.getRangeAt(0).startContainer)) {
21
+ return;
22
+ }
23
+ bump(function (n) {
24
+ return n + 1;
25
+ });
26
+ };
27
+ document.addEventListener("selectionchange", onSelectionChange);
28
+ return function () {
29
+ return document.removeEventListener("selectionchange", onSelectionChange);
30
+ };
31
+ }, [editSessionActive, editorRef]);
32
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fileverse-dev/fortune-react",
3
- "version": "1.3.11-input-ref",
3
+ "version": "1.3.11-input-ref-1",
4
4
  "main": "lib/index.js",
5
5
  "types": "lib/index.d.ts",
6
6
  "module": "es/index.js",
@@ -16,7 +16,7 @@
16
16
  "tsc": "tsc"
17
17
  },
18
18
  "dependencies": {
19
- "@fileverse-dev/fortune-core": "1.3.11-input-ref",
19
+ "@fileverse-dev/fortune-core": "1.3.11-input-ref-1",
20
20
  "@fileverse/ui": "5.0.0",
21
21
  "@tippyjs/react": "^4.2.6",
22
22
  "@types/regenerator-runtime": "^0.13.6",