@fileverse-dev/fortune-react 1.3.11-input-ref-1 → 1.3.11-input-ref-3

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.
@@ -56,31 +56,34 @@ var InputBox = function InputBox() {
56
56
  var _x = useState(0),
57
57
  commaCount = _x[0],
58
58
  setCommaCount = _x[1];
59
+ var _y = useState(false),
60
+ cellEditorIsFormula = _y[0],
61
+ setCellEditorIsFormula = _y[1];
59
62
  var hideFormulaHintLocal = localStorage.getItem("formulaMore") === "true";
60
- var _y = useState(!hideFormulaHintLocal),
61
- showFormulaHint = _y[0],
62
- setShowFormulaHint = _y[1];
63
- var _z = useState(false),
64
- showSearchHint = _z[0],
65
- setShowSearchHint = _z[1];
63
+ var _z = useState(!hideFormulaHintLocal),
64
+ showFormulaHint = _z[0],
65
+ setShowFormulaHint = _z[1];
66
+ var _0 = useState(false),
67
+ showSearchHint = _0[0],
68
+ setShowSearchHint = _0[1];
66
69
  var row_index = firstSelection === null || firstSelection === void 0 ? void 0 : firstSelection.row_focus;
67
70
  var col_index = firstSelection === null || firstSelection === void 0 ? void 0 : firstSelection.column_focus;
68
71
  var isComposingRef = useRef(false);
69
72
  var formulaAnchorCellRef = useRef(null);
70
73
  var skipNextAnchorSelectionSyncRef = useRef(false);
71
74
  var lastHandledMouseDragSignatureRef = useRef("");
72
- var _0 = useFormulaEditorHistory(inputRef, refs.cellInput, refs.fxInput, setContext, "cell"),
73
- formulaHistoryRef = _0.formulaHistoryRef,
74
- preTextRef = _0.preTextRef,
75
- resetFormulaHistory = _0.resetFormulaHistory,
76
- handleFormulaHistoryUndoRedo = _0.handleFormulaHistoryUndoRedo,
77
- capturePreFormulaState = _0.capturePreFormulaState,
78
- appendFormulaHistoryFromPrimaryEditor = _0.appendFormulaHistoryFromPrimaryEditor;
75
+ var _1 = useFormulaEditorHistory(inputRef, refs.cellInput, refs.fxInput, setContext, "cell"),
76
+ formulaHistoryRef = _1.formulaHistoryRef,
77
+ preTextRef = _1.preTextRef,
78
+ resetFormulaHistory = _1.resetFormulaHistory,
79
+ handleFormulaHistoryUndoRedo = _1.handleFormulaHistoryUndoRedo,
80
+ capturePreFormulaState = _1.capturePreFormulaState,
81
+ appendFormulaHistoryFromPrimaryEditor = _1.appendFormulaHistoryFromPrimaryEditor;
79
82
  var ZWSP = "\u200B";
80
83
  var inputBoxInnerRef = useRef(null);
81
- var _1 = useState([]),
82
- linkSelectionHighlightRects = _1[0],
83
- setLinkSelectionHighlightRects = _1[1];
84
+ var _2 = useState([]),
85
+ linkSelectionHighlightRects = _2[0],
86
+ setLinkSelectionHighlightRects = _2[1];
84
87
  var ensureNotEmpty = function ensureNotEmpty() {
85
88
  var el = inputRef.current;
86
89
  if (!el) return;
@@ -103,12 +106,11 @@ var InputBox = function InputBox() {
103
106
  return lastSpan === null || lastSpan === void 0 ? void 0 : lastSpan.innerText;
104
107
  };
105
108
  var inputBoxStyle = useMemo(function () {
106
- var _a;
107
109
  if (firstSelectionActiveCell && context.luckysheetCellUpdate.length > 0) {
108
110
  var flowdata = getFlowdata(context);
109
111
  if (!flowdata) return {};
110
112
  var style = getStyleByCell(context, flowdata, firstSelectionActiveCell.row_focus, firstSelectionActiveCell.column_focus);
111
- if (((_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.innerText.charAt(0)) === "=") {
113
+ if (cellEditorIsFormula) {
112
114
  style = __assign(__assign({}, style), {
113
115
  textAlign: "left"
114
116
  });
@@ -116,7 +118,7 @@ var InputBox = function InputBox() {
116
118
  return style;
117
119
  }
118
120
  return {};
119
- }, [context.luckysheetfile, context.currentSheetId, context.luckysheetCellUpdate, (_b = context === null || context === void 0 ? void 0 : context.luckysheetCellUpdate) === null || _b === void 0 ? void 0 : _b.length, firstSelectionActiveCell]);
121
+ }, [context.luckysheetfile, context.currentSheetId, context.luckysheetCellUpdate, (_b = context === null || context === void 0 ? void 0 : context.luckysheetCellUpdate) === null || _b === void 0 ? void 0 : _b.length, firstSelectionActiveCell, cellEditorIsFormula]);
120
122
  useLayoutEffect(function () {
121
123
  var _a;
122
124
  if (!context.allowEdit) {
@@ -136,10 +138,23 @@ var InputBox = function InputBox() {
136
138
  if (_.isEqual(prevCellUpdate, context.luckysheetCellUpdate) && prevSheetId === context.currentSheetId) {
137
139
  return;
138
140
  }
141
+ var _b = context.luckysheetCellUpdate,
142
+ ur = _b[0],
143
+ uc = _b[1];
144
+ var pending_1 = refs.globalCache.pendingTypeOverCell;
145
+ if (pending_1 && pending_1[0] === ur && pending_1[1] === uc) {
146
+ refs.globalCache.overwriteCell = false;
147
+ if (inputRef.current) {
148
+ setCellEditorIsFormula(inputRef.current.innerText.trim().startsWith("="));
149
+ }
150
+ delete refs.globalCache.doNotFocus;
151
+ return;
152
+ }
139
153
  var flowdata = getFlowdata(context);
140
154
  var cell = (_a = flowdata === null || flowdata === void 0 ? void 0 : flowdata[row_index]) === null || _a === void 0 ? void 0 : _a[col_index];
155
+ var overwrite = refs.globalCache.overwriteCell;
141
156
  var value_1 = "";
142
- if (cell && !refs.globalCache.overwriteCell) {
157
+ if (cell && !overwrite) {
143
158
  if (isInlineStringCell(cell)) {
144
159
  value_1 = getInlineStringHTML(row_index, col_index, flowdata);
145
160
  } else if (cell.f) {
@@ -155,14 +170,20 @@ var InputBox = function InputBox() {
155
170
  }
156
171
  }
157
172
  refs.globalCache.overwriteCell = false;
173
+ var wroteEditorFromStoredCell = false;
158
174
  if (!refs.globalCache.ignoreWriteCell && inputRef.current && value_1) {
159
175
  inputRef.current.innerHTML = escapeHTMLTag(escapeScriptTag(value_1));
160
- } else if (!refs.globalCache.ignoreWriteCell && inputRef.current && !value_1) {
176
+ wroteEditorFromStoredCell = true;
177
+ } else if (!refs.globalCache.ignoreWriteCell && inputRef.current && !value_1 && !overwrite) {
161
178
  var valueD = getCellValue(row_index, col_index, flowdata, "f");
162
179
  inputRef.current.innerText = valueD;
180
+ wroteEditorFromStoredCell = true;
163
181
  }
164
182
  refs.globalCache.ignoreWriteCell = false;
165
- if (!refs.globalCache.doNotFocus) {
183
+ if (inputRef.current) {
184
+ setCellEditorIsFormula(inputRef.current.innerText.trim().startsWith("="));
185
+ }
186
+ if (wroteEditorFromStoredCell && !refs.globalCache.doNotFocus) {
166
187
  setTimeout(function () {
167
188
  moveToEnd(inputRef.current);
168
189
  });
@@ -175,9 +196,15 @@ var InputBox = function InputBox() {
175
196
  if (inputRef.current) {
176
197
  inputRef.current.innerHTML = "";
177
198
  }
199
+ delete refs.globalCache.pendingTypeOverCell;
200
+ setCellEditorIsFormula(false);
178
201
  resetFormulaHistory();
179
202
  }
180
- }, [context.luckysheetCellUpdate, resetFormulaHistory]);
203
+ }, [context.luckysheetCellUpdate, resetFormulaHistory, refs.globalCache]);
204
+ useEffect(function () {
205
+ if (_.isEmpty(context.luckysheetCellUpdate)) return;
206
+ delete refs.globalCache.pendingTypeOverCell;
207
+ }, [context.luckysheetCellUpdate, refs.globalCache]);
181
208
  useEffect(function () {
182
209
  var _a;
183
210
  if (_.isEmpty(context.luckysheetCellUpdate) || !refs.cellInput.current) {
@@ -373,7 +400,7 @@ var InputBox = function InputBox() {
373
400
  });
374
401
  }, [formulaMouseDragSignature, context.formulaCache.func_selectedrange, refs.cellInput, setContext]);
375
402
  var onKeyDown = useCallback(function (e) {
376
- var _a, _b;
403
+ var _a, _b, _c;
377
404
  setContext(function (draftCtx) {
378
405
  setFormulaEditorOwner(draftCtx, "cell");
379
406
  });
@@ -395,9 +422,9 @@ var InputBox = function InputBox() {
395
422
  setContext(function (draftCtx) {
396
423
  draftCtx.formulaCache.rangeSelectionActive = null;
397
424
  });
398
- var _c = formulaAnchorCellRef.current,
399
- anchorRow_1 = _c[0],
400
- anchorCol_1 = _c[1];
425
+ var _d = formulaAnchorCellRef.current,
426
+ anchorRow_1 = _d[0],
427
+ anchorCol_1 = _d[1];
401
428
  skipNextAnchorSelectionSyncRef.current = true;
402
429
  setTimeout(function () {
403
430
  setContext(function (draftCtx) {
@@ -453,6 +480,7 @@ var InputBox = function InputBox() {
453
480
  }
454
481
  var allowListNavigation = true;
455
482
  var isArrowKey = e.key === "ArrowUp" || e.key === "ArrowDown" || e.key === "ArrowLeft" || e.key === "ArrowRight";
483
+ var isInPlaceEditMode = ((_c = refs.globalCache) === null || _c === void 0 ? void 0 : _c.enteredEditByTyping) !== true;
456
484
  if (e.key === "Delete" || e.key === "Backspace") {
457
485
  var anchor = formulaAnchorCellRef.current;
458
486
  if (anchor != null) {
@@ -468,6 +496,12 @@ var InputBox = function InputBox() {
468
496
  row_focus: anchorRow_2,
469
497
  column_focus: anchorCol_2
470
498
  }];
499
+ markRangeSelectionDirty(draftCtx);
500
+ var el = refs.cellInput.current;
501
+ if (el && el.innerText.trim().startsWith("=")) {
502
+ createRangeHightlight(draftCtx, el.innerHTML);
503
+ rangeHightlightselected(draftCtx, el);
504
+ }
471
505
  });
472
506
  }, 0);
473
507
  }
@@ -498,7 +532,7 @@ var InputBox = function InputBox() {
498
532
  e.preventDefault();
499
533
  } else if (e.key === "F4" && context.luckysheetCellUpdate.length > 0) {
500
534
  e.preventDefault();
501
- } else if (e.key === "ArrowUp" && context.luckysheetCellUpdate.length > 0 && allowListNavigation) {
535
+ } else if (!(e.metaKey || e.ctrlKey) && e.key === "ArrowUp" && context.luckysheetCellUpdate.length > 0 && allowListNavigation && !(e.shiftKey && isInPlaceEditMode)) {
502
536
  if (document.getElementById("luckysheet-formula-search-c")) {
503
537
  var formulaSearchContainer = document.getElementById("luckysheet-formula-search-c");
504
538
  var activeItem = formulaSearchContainer === null || formulaSearchContainer === void 0 ? void 0 : formulaSearchContainer.querySelector(".luckysheet-formula-search-item-active");
@@ -517,7 +551,7 @@ var InputBox = function InputBox() {
517
551
  }
518
552
  }
519
553
  e.preventDefault();
520
- } else if (e.key === "ArrowDown" && context.luckysheetCellUpdate.length > 0 && allowListNavigation) {
554
+ } else if (!(e.metaKey || e.ctrlKey) && e.key === "ArrowDown" && context.luckysheetCellUpdate.length > 0 && allowListNavigation && !(e.shiftKey && isInPlaceEditMode)) {
521
555
  if (document.getElementById("luckysheet-formula-search-c")) {
522
556
  var formulaSearchContainer = document.getElementById("luckysheet-formula-search-c");
523
557
  var activeItem = formulaSearchContainer === null || formulaSearchContainer === void 0 ? void 0 : formulaSearchContainer.querySelector(".luckysheet-formula-search-item-active");
@@ -535,7 +569,7 @@ var InputBox = function InputBox() {
535
569
  }
536
570
  e.preventDefault();
537
571
  }
538
- }, [capturePreFormulaState, clearSearchItemActiveClass, context.luckysheetCellUpdate.length, handleFormulaHistoryUndoRedo, selectActiveFormula, setContext, firstSelection]);
572
+ }, [capturePreFormulaState, clearSearchItemActiveClass, context.luckysheetCellUpdate.length, handleFormulaHistoryUndoRedo, selectActiveFormula, setContext, firstSelection, refs.cellInput]);
539
573
  var handleHideShowHint = function handleHideShowHint() {
540
574
  var _a, _b, _c, _d;
541
575
  var searchElFx = (_a = document.getElementsByClassName("fx-search")) === null || _a === void 0 ? void 0 : _a[0];
@@ -556,10 +590,12 @@ var InputBox = function InputBox() {
556
590
  }
557
591
  };
558
592
  var onChange = useCallback(function (__, isBlur) {
559
- var _a, _b, _c;
593
+ var _a, _b, _c, _d, _e, _f;
560
594
  if (context.isFlvReadOnly) return;
561
595
  handleHideShowHint();
562
- setShowSearchHint(shouldShowFormulaFunctionList((_a = inputRef === null || inputRef === void 0 ? void 0 : inputRef.current) !== null && _a !== void 0 ? _a : null));
596
+ var editorText = (_c = (_b = (_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.innerText) === null || _b === void 0 ? void 0 : _b.trim()) !== null && _c !== void 0 ? _c : "";
597
+ setCellEditorIsFormula(editorText.startsWith("="));
598
+ setShowSearchHint(shouldShowFormulaFunctionList((_d = inputRef === null || inputRef === void 0 ? void 0 : inputRef.current) !== null && _d !== void 0 ? _d : null));
563
599
  if (!isComposingRef.current) {
564
600
  var currentCommaCount = countCommasBeforeCursor(inputRef === null || inputRef === void 0 ? void 0 : inputRef.current);
565
601
  setCommaCount(currentCommaCount);
@@ -567,7 +603,7 @@ var InputBox = function InputBox() {
567
603
  var e = lastKeyDownEventRef.current;
568
604
  if (!e) {
569
605
  var cellEl_1 = refs.cellInput.current;
570
- 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("=")))) {
606
+ if (cellEl_1 && (((_e = cellEl_1.innerText) === null || _e === void 0 ? void 0 : _e.trim().startsWith("=")) || ((_f = cellEl_1.textContent) === null || _f === void 0 ? void 0 : _f.trim().startsWith("=")))) {
571
607
  setContext(function (draftCtx) {
572
608
  if (!isAllowEdit(draftCtx, draftCtx.luckysheet_select_save)) return;
573
609
  rangeHightlightselected(draftCtx, cellEl_1);
@@ -582,11 +618,16 @@ var InputBox = function InputBox() {
582
618
  });
583
619
  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) {
584
620
  setContext(function (draftCtx) {
621
+ var _a, _b;
585
622
  if ((draftCtx.formulaCache.rangestart || draftCtx.formulaCache.rangedrag_column_start || draftCtx.formulaCache.rangedrag_row_start || israngeseleciton(draftCtx)) && isBlur) return;
586
623
  if (!isAllowEdit(draftCtx, draftCtx.luckysheet_select_save)) {
587
624
  return;
588
625
  }
589
626
  handleFormulaInput(draftCtx, refs.fxInput.current, refs.cellInput.current, kcode, preTextRef.current);
627
+ var cellEl = refs.cellInput.current;
628
+ if (cellEl && (((_a = cellEl.innerText) === null || _a === void 0 ? void 0 : _a.trim().startsWith("=")) || ((_b = cellEl.textContent) === null || _b === void 0 ? void 0 : _b.trim().startsWith("=")))) {
629
+ rangeHightlightselected(draftCtx, cellEl);
630
+ }
590
631
  });
591
632
  }
592
633
  }, [refs.cellInput, refs.fxInput, setContext, appendFormulaHistoryFromPrimaryEditor]);
@@ -336,9 +336,9 @@
336
336
  overflow: hidden;
337
337
  white-space: pre-wrap;
338
338
  outline: none;
339
- -webkit-box-shadow: 0 2px 5px rgb(0 0 0 / 40%);
340
- -moz-box-shadow: 0 2px 5px rgba(0, 0, 0, 0.4);
341
- box-shadow: 0 2px 5px rgb(0 0 0 / 40%);
339
+ /* -webkit-box-shadow: 0 2px 5px rgb(0 0 0 / 40%);
340
+ -moz-box-shadow: 0 2px 5px rgba(0, 0, 0, 0.4); */
341
+ /* box-shadow: 0 2px 5px rgb(0 0 0 / 40%); */
342
342
  word-wrap: break-word;
343
343
  background-color: rgb(255, 255, 255);
344
344
  font-size: 13px;
@@ -801,44 +801,11 @@
801
801
  }
802
802
 
803
803
  .fortune-formula-functionrange-highlight .fortune-selection-copy-hc {
804
- border: 2px solid #5e5e5e;
805
- opacity: 0.03;
804
+ border-width: 2px;
805
+ border-style: dotted;
806
806
  z-index: initial;
807
807
  }
808
808
 
809
- .fortune-selection-highlight-lt {
810
- left: -3px;
811
- top: -3px;
812
- cursor: se-resize;
813
- }
814
-
815
- .fortune-selection-highlight-rt {
816
- right: -3px;
817
- top: -3px;
818
- cursor: ne-resize;
819
- }
820
-
821
- .fortune-selection-highlight-lb {
822
- left: -3px;
823
- bottom: -3px;
824
- cursor: ne-resize;
825
- }
826
-
827
- .fortune-selection-highlight-rb {
828
- right: -3px;
829
- bottom: -3px;
830
- cursor: se-resize;
831
- }
832
-
833
- .fortune-formula-functionrange-highlight .luckysheet-highlight {
834
- position: absolute;
835
- z-index: 19;
836
- border: 1px solid #fff;
837
- background: #0188fb;
838
- width: 6px;
839
- height: 6px;
840
- }
841
-
842
809
  .fortune-presence-username {
843
810
  position: absolute;
844
811
  padding-left: 6px;
@@ -10,7 +10,7 @@ var __assign = this && this.__assign || function () {
10
10
  };
11
11
  import React, { useContext, useCallback, useRef, useEffect, useLayoutEffect, useMemo } from "react";
12
12
  import "./index.css";
13
- import { locale, drawArrow, handleCellAreaDoubleClick, handleCellAreaMouseDown, handleContextMenu, handleOverlayMouseMove, handleOverlayMouseUp, selectAll, handleOverlayTouchEnd, handleOverlayTouchStart, createDropCellRange, getCellRowColumn, getCellHyperlink, showLinkCard, isAllowEdit, onCellsMoveStart, insertRowCol, getSheetIndex, fixRowStyleOverflowInFreeze, fixColumnStyleOverflowInFreeze, handleKeydownForZoom } from "@fileverse-dev/fortune-core";
13
+ import { locale, drawArrow, handleCellAreaDoubleClick, handleCellAreaMouseDown, handleContextMenu, handleOverlayMouseMove, handleOverlayMouseUp, selectAll, handleOverlayTouchEnd, handleOverlayTouchStart, createDropCellRange, getCellRowColumn, getCellHyperlink, showLinkCard, isAllowEdit, israngeseleciton, onCellsMoveStart, insertRowCol, getSheetIndex, fixRowStyleOverflowInFreeze, fixColumnStyleOverflowInFreeze, handleKeydownForZoom } from "@fileverse-dev/fortune-core";
14
14
  import _ from "lodash";
15
15
  import WorkbookContext from "../../context";
16
16
  import ColumnHeader from "./ColumnHeader";
@@ -28,6 +28,22 @@ import { useDialog } from "../../hooks/useDialog";
28
28
  import DropDownList from "../DataVerification/DropdownList";
29
29
  import IframeBoxs from "../IFrameBoxs/iFrameBoxs";
30
30
  import ErrorBoxes from "../ErrorState";
31
+ function formulaRangeHighlightHcStyle(hex) {
32
+ var h = hex.replace("#", "");
33
+ if (h.length !== 6) {
34
+ return {
35
+ backgroundColor: hex,
36
+ borderColor: hex
37
+ };
38
+ }
39
+ var r = parseInt(h.slice(0, 2), 16);
40
+ var g = parseInt(h.slice(2, 4), 16);
41
+ var b = parseInt(h.slice(4, 6), 16);
42
+ return {
43
+ backgroundColor: "rgba(".concat(r, ",").concat(g, ",").concat(b, ",0.08)"),
44
+ borderColor: hex
45
+ };
46
+ }
31
47
  var SheetOverlay = function SheetOverlay() {
32
48
  var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
33
49
  var _o = useContext(WorkbookContext),
@@ -49,6 +65,7 @@ var SheetOverlay = function SheetOverlay() {
49
65
  setContext(function (draftCtx) {
50
66
  var _a;
51
67
  handleCellAreaMouseDown(draftCtx, refs.globalCache, nativeEvent, refs.cellInput.current, refs.cellArea.current, refs.fxInput.current, refs.canvas.current.getContext("2d"));
68
+ var keepFormulaBarFocused = draftCtx.luckysheetCellUpdate.length > 0 && draftCtx.formulaCache.formulaEditorOwner === "fx" && (draftCtx.formulaCache.rangestart || draftCtx.formulaCache.rangedrag_column_start || draftCtx.formulaCache.rangedrag_row_start || israngeseleciton(draftCtx));
52
69
  if (!_.isEmpty((_a = draftCtx.luckysheet_select_save) === null || _a === void 0 ? void 0 : _a[0]) && refs.cellInput.current) {
53
70
  if (!isAllowEdit(draftCtx)) {
54
71
  setTimeout(function () {
@@ -60,7 +77,13 @@ var SheetOverlay = function SheetOverlay() {
60
77
  } else {
61
78
  setTimeout(function () {
62
79
  var _a;
63
- (_a = refs.cellInput.current) === null || _a === void 0 ? void 0 : _a.focus();
80
+ if (keepFormulaBarFocused && refs.fxInput.current) {
81
+ refs.fxInput.current.focus({
82
+ preventScroll: true
83
+ });
84
+ } else {
85
+ (_a = refs.cellInput.current) === null || _a === void 0 ? void 0 : _a.focus();
86
+ }
64
87
  });
65
88
  }
66
89
  }
@@ -290,18 +313,7 @@ var SheetOverlay = function SheetOverlay() {
290
313
  });
291
314
  }), /*#__PURE__*/React.createElement("div", {
292
315
  className: "fortune-selection-copy-hc",
293
- style: {
294
- backgroundColor: backgroundColor
295
- }
296
- }), ["lt", "rt", "lb", "rb"].map(function (d) {
297
- return /*#__PURE__*/React.createElement("div", {
298
- key: d,
299
- "data-type": d,
300
- className: "fortune-selection-highlight-".concat(d, " luckysheet-highlight"),
301
- style: {
302
- backgroundColor: backgroundColor
303
- }
304
- });
316
+ style: formulaRangeHighlightHcStyle(backgroundColor)
305
317
  }));
306
318
  }), /*#__PURE__*/React.createElement("div", {
307
319
  className: "luckysheet-row-count-show luckysheet-count-show",
@@ -3,7 +3,9 @@ export function useRerenderOnFormulaCaret(editorRef, editSessionActive) {
3
3
  var _a = useState(0),
4
4
  bump = _a[1];
5
5
  useEffect(function () {
6
- if (!editSessionActive) return;
6
+ if (!editSessionActive) {
7
+ return function () {};
8
+ }
7
9
  var onSelectionChange = function onSelectionChange() {
8
10
  var _a, _b;
9
11
  var el = editorRef.current;
@@ -20,7 +22,7 @@ export function useRerenderOnFormulaCaret(editorRef, editSessionActive) {
20
22
  };
21
23
  document.addEventListener("selectionchange", onSelectionChange);
22
24
  return function () {
23
- return document.removeEventListener("selectionchange", onSelectionChange);
25
+ document.removeEventListener("selectionchange", onSelectionChange);
24
26
  };
25
27
  }, [editSessionActive, editorRef]);
26
28
  }
@@ -55,6 +55,7 @@ var FxEditor = function FxEditor() {
55
55
  var prevCellUpdate = (0, _usePrevious.default)(context.luckysheetCellUpdate);
56
56
  var prevSheetId = (0, _usePrevious.default)(context.currentSheetId);
57
57
  var recentText = (0, _react.useRef)("");
58
+ var formulaAnchorCellRef = (0, _react.useRef)(null);
58
59
  var handleShowFormulaHint = function handleShowFormulaHint() {
59
60
  localStorage.setItem("formulaMore", String(showFormulaHint));
60
61
  setShowFormulaHint(!showFormulaHint);
@@ -120,13 +121,20 @@ var FxEditor = function FxEditor() {
120
121
  if (_lodash.default.isNil(rowIndex) || _lodash.default.isNil(colIndex)) {
121
122
  return;
122
123
  }
124
+ var pending = refs.globalCache.pendingTypeOverCell;
125
+ if (pending && pending[0] === rowIndex && pending[1] === colIndex) {
126
+ refs.globalCache.overwriteCell = false;
127
+ refs.globalCache.ignoreWriteCell = false;
128
+ return;
129
+ }
123
130
  var flowdata = (0, _fortuneCore.getFlowdata)(context);
124
131
  if (!flowdata) {
125
132
  return;
126
133
  }
127
134
  var cell = (_a = flowdata === null || flowdata === void 0 ? void 0 : flowdata[rowIndex]) === null || _a === void 0 ? void 0 : _a[colIndex];
128
135
  var value = "";
129
- if (cell && !refs.globalCache.overwriteCell) {
136
+ var overwrite = refs.globalCache.overwriteCell;
137
+ if (cell && !overwrite) {
130
138
  if ((0, _fortuneCore.isInlineStringCell)(cell)) {
131
139
  value = (0, _fortuneCore.getInlineStringNoStyle)(rowIndex, colIndex, flowdata);
132
140
  } else if (cell.f) {
@@ -138,7 +146,7 @@ var FxEditor = function FxEditor() {
138
146
  refs.globalCache.overwriteCell = false;
139
147
  if (!refs.globalCache.ignoreWriteCell && value) {
140
148
  fxInput.innerHTML = (0, _fortuneCore.escapeHTMLTag)((0, _fortuneCore.escapeScriptTag)(value));
141
- } else if (!refs.globalCache.ignoreWriteCell) {
149
+ } else if (!refs.globalCache.ignoreWriteCell && !overwrite) {
142
150
  var valueD = (0, _fortuneCore.getCellValue)(rowIndex, colIndex, flowdata, "f");
143
151
  fxInput.innerText = valueD;
144
152
  }
@@ -146,9 +154,21 @@ var FxEditor = function FxEditor() {
146
154
  }, [context.luckysheetCellUpdate, context.luckysheetfile, context.currentSheetId, prevCellUpdate, prevSheetId, refs.fxInput, refs.globalCache]);
147
155
  (0, _react.useEffect)(function () {
148
156
  if (_lodash.default.isEmpty(context.luckysheetCellUpdate)) {
157
+ delete refs.globalCache.pendingTypeOverCell;
149
158
  resetFormulaHistory();
150
159
  }
151
- }, [context.luckysheetCellUpdate, resetFormulaHistory]);
160
+ }, [context.luckysheetCellUpdate, resetFormulaHistory, refs.globalCache]);
161
+ (0, _react.useEffect)(function () {
162
+ var _a;
163
+ if (_lodash.default.isEmpty(context.luckysheetCellUpdate) || !refs.fxInput.current) {
164
+ formulaAnchorCellRef.current = null;
165
+ return;
166
+ }
167
+ var inputText = ((_a = refs.fxInput.current.innerText) === null || _a === void 0 ? void 0 : _a.trim()) || "";
168
+ if (!inputText.startsWith("=")) {
169
+ formulaAnchorCellRef.current = null;
170
+ }
171
+ }, [context.luckysheetCellUpdate, refs.fxInput]);
152
172
  var onFocus = (0, _react.useCallback)(function () {
153
173
  var _a, _b;
154
174
  if (context.allowEdit === false) {
@@ -156,12 +176,25 @@ var FxEditor = function FxEditor() {
156
176
  }
157
177
  if (((_b = (_a = context.luckysheet_select_save) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0) > 0 && !context.luckysheet_cell_selected_move && (0, _fortuneCore.isAllowEdit)(context, context.luckysheet_select_save)) {
158
178
  setContext(function (draftCtx) {
179
+ var _a;
159
180
  (0, _fortuneCore.setFormulaEditorOwner)(draftCtx, "fx");
160
181
  var last = draftCtx.luckysheet_select_save[draftCtx.luckysheet_select_save.length - 1];
161
182
  var row_index = last.row_focus;
162
183
  var col_index = last.column_focus;
163
- draftCtx.luckysheetCellUpdate = [row_index, col_index];
164
- refs.globalCache.doNotFocus = true;
184
+ if (!_lodash.default.isNil(row_index) && !_lodash.default.isNil(col_index)) {
185
+ var flowdata = (0, _fortuneCore.getFlowdata)(draftCtx);
186
+ var cellAt = (_a = flowdata === null || flowdata === void 0 ? void 0 : flowdata[row_index]) === null || _a === void 0 ? void 0 : _a[col_index];
187
+ if ((cellAt === null || cellAt === void 0 ? void 0 : cellAt.f) != null && String(cellAt.f).trim() !== "") {
188
+ (0, _fortuneCore.suppressFormulaRangeSelectionForInitialEdit)(draftCtx);
189
+ }
190
+ }
191
+ var currentUpdate = draftCtx.luckysheetCellUpdate || [];
192
+ var alreadyEditingSameCell = currentUpdate.length === 2 && currentUpdate[0] === row_index && currentUpdate[1] === col_index;
193
+ if (!alreadyEditingSameCell) {
194
+ refs.globalCache.doNotUpdateCell = true;
195
+ draftCtx.luckysheetCellUpdate = [row_index, col_index];
196
+ refs.globalCache.doNotFocus = true;
197
+ }
165
198
  });
166
199
  }
167
200
  setContext(function (draftCtx) {
@@ -220,7 +253,26 @@ var FxEditor = function FxEditor() {
220
253
  }
221
254
  }, [getActiveFormula, insertSelectedFormula]);
222
255
  var onKeyDown = (0, _react.useCallback)(function (e) {
223
- var _a, _b, _c;
256
+ var _a, _b, _c, _d;
257
+ if (e.key === "Delete" || e.key === "Backspace") {
258
+ var anchor = formulaAnchorCellRef.current;
259
+ if (anchor != null) {
260
+ var anchorRow_1 = anchor[0],
261
+ anchorCol_1 = anchor[1];
262
+ setTimeout(function () {
263
+ setContext(function (draftCtx) {
264
+ draftCtx.luckysheetCellUpdate = [anchorRow_1, anchorCol_1];
265
+ draftCtx.luckysheet_select_save = [{
266
+ row: [anchorRow_1, anchorRow_1],
267
+ column: [anchorCol_1, anchorCol_1],
268
+ row_focus: anchorRow_1,
269
+ column_focus: anchorCol_1
270
+ }];
271
+ (0, _fortuneCore.markRangeSelectionDirty)(draftCtx);
272
+ });
273
+ }, 0);
274
+ }
275
+ }
224
276
  if (context.allowEdit === false) {
225
277
  return;
226
278
  }
@@ -234,7 +286,50 @@ var FxEditor = function FxEditor() {
234
286
  recentText.current = refs.fxInput.current.innerText;
235
287
  var key = e.key;
236
288
  var currentInputText = ((_c = (_b = refs.fxInput.current) === null || _b === void 0 ? void 0 : _b.innerText) === null || _c === void 0 ? void 0 : _c.trim()) || "";
237
- if (key === "ArrowLeft" || key === "ArrowRight") {
289
+ if ((key === "=" || currentInputText.startsWith("=")) && context.luckysheetCellUpdate.length === 2 && formulaAnchorCellRef.current == null) {
290
+ setContext(function (draftCtx) {
291
+ draftCtx.formulaCache.rangeSelectionActive = null;
292
+ });
293
+ formulaAnchorCellRef.current = [context.luckysheetCellUpdate[0], context.luckysheetCellUpdate[1]];
294
+ }
295
+ if (key === "(" && currentInputText.startsWith("=")) {
296
+ setContext(function (draftCtx) {
297
+ draftCtx.formulaCache.rangeSelectionActive = null;
298
+ });
299
+ }
300
+ if (key === "," && context.luckysheetCellUpdate.length > 0 && currentInputText.startsWith("=") && formulaAnchorCellRef.current) {
301
+ setContext(function (draftCtx) {
302
+ draftCtx.formulaCache.rangeSelectionActive = null;
303
+ });
304
+ var _e = formulaAnchorCellRef.current,
305
+ anchorRow_2 = _e[0],
306
+ anchorCol_2 = _e[1];
307
+ setTimeout(function () {
308
+ setContext(function (draftCtx) {
309
+ var _a, _b;
310
+ draftCtx.luckysheetCellUpdate = [anchorRow_2, anchorCol_2];
311
+ draftCtx.luckysheet_select_save = [{
312
+ row: [anchorRow_2, anchorRow_2],
313
+ column: [anchorCol_2, anchorCol_2],
314
+ row_focus: anchorRow_2,
315
+ column_focus: anchorCol_2
316
+ }];
317
+ draftCtx.formulaRangeSelect = undefined;
318
+ draftCtx.formulaCache.selectingRangeIndex = -1;
319
+ draftCtx.formulaCache.func_selectedrange = undefined;
320
+ draftCtx.formulaCache.rangechangeindex = undefined;
321
+ draftCtx.formulaCache.rangestart = false;
322
+ draftCtx.formulaCache.rangedrag_column_start = false;
323
+ draftCtx.formulaCache.rangedrag_row_start = false;
324
+ (0, _fortuneCore.createRangeHightlight)(draftCtx, ((_a = refs.fxInput.current) === null || _a === void 0 ? void 0 : _a.innerHTML) || ((_b = refs.cellInput.current) === null || _b === void 0 ? void 0 : _b.innerHTML) || "");
325
+ (0, _fortuneCore.moveHighlightCell)(draftCtx, "down", 0, "rangeOfSelect");
326
+ });
327
+ }, 0);
328
+ }
329
+ var isArrowKey = key === "ArrowUp" || key === "ArrowDown" || key === "ArrowLeft" || key === "ArrowRight";
330
+ var isDirectPlainTypeEdit = context.luckysheetCellUpdate.length > 0 && ((_d = refs.globalCache) === null || _d === void 0 ? void 0 : _d.enteredEditByTyping) === true && !currentInputText.startsWith("=");
331
+ var sheetArrowShortcut = isArrowKey && (e.metaKey || e.ctrlKey || e.shiftKey);
332
+ if ((key === "ArrowLeft" || key === "ArrowRight") && !(isDirectPlainTypeEdit && sheetArrowShortcut)) {
238
333
  e.stopPropagation();
239
334
  }
240
335
  if ((e.metaKey || e.ctrlKey) && context.luckysheetCellUpdate.length > 0) {
@@ -261,7 +356,7 @@ var FxEditor = function FxEditor() {
261
356
  }
262
357
  return;
263
358
  }
264
- if (e.key === "ArrowUp" && context.luckysheetCellUpdate.length > 0) {
359
+ if (!(e.metaKey || e.ctrlKey) && e.key === "ArrowUp" && context.luckysheetCellUpdate.length > 0) {
265
360
  if (document.getElementById("luckysheet-formula-search-c")) {
266
361
  var formulaSearchContainer = document.getElementById("luckysheet-formula-search-c");
267
362
  var activeItem = formulaSearchContainer === null || formulaSearchContainer === void 0 ? void 0 : formulaSearchContainer.querySelector(".luckysheet-formula-search-item-active");
@@ -280,7 +375,7 @@ var FxEditor = function FxEditor() {
280
375
  }
281
376
  }
282
377
  e.preventDefault();
283
- } else if (e.key === "ArrowDown" && context.luckysheetCellUpdate.length > 0) {
378
+ } else if (!(e.metaKey || e.ctrlKey) && e.key === "ArrowDown" && context.luckysheetCellUpdate.length > 0) {
284
379
  if (document.getElementById("luckysheet-formula-search-c")) {
285
380
  var formulaSearchContainer = document.getElementById("luckysheet-formula-search-c");
286
381
  var activeItem = formulaSearchContainer === null || formulaSearchContainer === void 0 ? void 0 : formulaSearchContainer.querySelector(".luckysheet-formula-search-item-active");
@@ -339,7 +434,7 @@ var FxEditor = function FxEditor() {
339
434
  }
340
435
  }
341
436
  });
342
- }, [capturePreFormulaState, context.allowEdit, context.luckysheetCellUpdate.length, handleFormulaHistoryUndoRedo, refs.fxInput, setContext]);
437
+ }, [capturePreFormulaState, context.allowEdit, context.luckysheetCellUpdate, handleFormulaHistoryUndoRedo, refs.cellInput, refs.fxInput, setContext]);
343
438
  var handleHideShowHint = function handleHideShowHint() {
344
439
  var _a, _b, _c, _d;
345
440
  var el = (_a = document.getElementsByClassName("cell-hint")) === null || _a === void 0 ? void 0 : _a[0];
@@ -360,11 +455,18 @@ var FxEditor = function FxEditor() {
360
455
  }
361
456
  };
362
457
  var onChange = (0, _react.useCallback)(function () {
363
- var _a, _b, _c;
458
+ var _a, _b, _c, _d;
364
459
  if (context.isFlvReadOnly) return;
460
+ var fxEl = refs.fxInput.current;
461
+ if (fxEl && context.luckysheetCellUpdate.length === 2 && formulaAnchorCellRef.current == null) {
462
+ var t = ((_a = fxEl.innerText) === null || _a === void 0 ? void 0 : _a.trim()) || "";
463
+ if (t.startsWith("=")) {
464
+ formulaAnchorCellRef.current = [context.luckysheetCellUpdate[0], context.luckysheetCellUpdate[1]];
465
+ }
466
+ }
365
467
  handleHideShowHint();
366
- setShowSearchHint((0, _helper.shouldShowFormulaFunctionList)((_b = (_a = refs.fxInput) === null || _a === void 0 ? void 0 : _a.current) !== null && _b !== void 0 ? _b : null));
367
- var currentCommaCount = (0, _helper.countCommasBeforeCursor)((_c = refs.fxInput) === null || _c === void 0 ? void 0 : _c.current);
468
+ setShowSearchHint((0, _helper.shouldShowFormulaFunctionList)((_c = (_b = refs.fxInput) === null || _b === void 0 ? void 0 : _b.current) !== null && _c !== void 0 ? _c : null));
469
+ var currentCommaCount = (0, _helper.countCommasBeforeCursor)((_d = refs.fxInput) === null || _d === void 0 ? void 0 : _d.current);
368
470
  setCommaCount(currentCommaCount);
369
471
  var e = lastKeyDownEventRef.current;
370
472
  if (!e) {
@@ -399,7 +501,7 @@ var FxEditor = function FxEditor() {
399
501
  (0, _fortuneCore.handleFormulaInput)(draftCtx, refs.cellInput.current, refs.fxInput.current, kcode, recentText.current);
400
502
  });
401
503
  }
402
- }, [appendFormulaHistoryFromPrimaryEditor, context.isFlvReadOnly, refs.cellInput, refs.fxInput, setContext]);
504
+ }, [appendFormulaHistoryFromPrimaryEditor, context.isFlvReadOnly, context.luckysheetCellUpdate, refs.cellInput, refs.fxInput, setContext]);
403
505
  (0, _useRerenderOnFormulaCaret.useRerenderOnFormulaCaret)(refs.fxInput, context.luckysheetCellUpdate.length > 0);
404
506
  var getFunctionNameFromInput = (0, _react.useCallback)(function () {
405
507
  var _a, _b, _c, _d;
@@ -454,7 +556,7 @@ var FxEditor = function FxEditor() {
454
556
  document.addEventListener("mouseup", _onMouseUp);
455
557
  };
456
558
  return /*#__PURE__*/_react.default.createElement("div", null, /*#__PURE__*/_react.default.createElement("div", {
457
- className: "fortune-fx-editor",
559
+ className: showFxFormulaChrome ? "fortune-fx-editor fortune-fx-editor--formula-owner" : "fortune-fx-editor",
458
560
  ref: divRef
459
561
  }, /*#__PURE__*/_react.default.createElement(_NameBox.default, null), /*#__PURE__*/_react.default.createElement("div", {
460
562
  className: "fortune-fx-icon",