@fileverse-dev/fortune-react 1.3.10 → 1.3.11-input-ref

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.
@@ -1,4 +1,4 @@
1
- import { getFlowdata, cancelNormalSelected, getCellValue, updateCell, getInlineStringNoStyle, isInlineStringCell, escapeScriptTag, moveHighlightCell, handleFormulaInput, rangeHightlightselected, valueShowEs, isShowHidenCR, escapeHTMLTag, isAllowEdit } from "@fileverse-dev/fortune-core";
1
+ import { getFlowdata, cancelNormalSelected, getCellValue, updateCell, getInlineStringNoStyle, isInlineStringCell, escapeScriptTag, moveHighlightCell, handleFormulaInput, rangeHightlightselected, valueShowEs, isShowHidenCR, escapeHTMLTag, functionHTMLGenerate, isAllowEdit } from "@fileverse-dev/fortune-core";
2
2
  import React, { useContext, useState, useCallback, useEffect, useRef, useMemo } from "react";
3
3
  import "./index.css";
4
4
  import _ from "lodash";
@@ -9,7 +9,7 @@ import FormulaSearch from "../../components/SheetOverlay/FormulaSearch";
9
9
  import FormulaHint from "../../components/SheetOverlay/FormulaHint";
10
10
  import usePrevious from "../../hooks/usePrevious";
11
11
  import { LucideIcon } from "../../components/SheetOverlay/LucideIcon";
12
- import { countCommasBeforeCursor, isLetterNumberPattern, moveCursorToEnd } from "../../components/SheetOverlay/helper";
12
+ import { countCommasBeforeCursor, isLetterNumberPattern, setCursorPosition, buildFormulaSuggestionText } from "../../components/SheetOverlay/helper";
13
13
  var FxEditor = function FxEditor() {
14
14
  var _a;
15
15
  var hideFormulaHintLocal = localStorage.getItem("formulaMore") === "true";
@@ -85,13 +85,19 @@ var FxEditor = function FxEditor() {
85
85
  return document.querySelector(".luckysheet-formula-search-item-active");
86
86
  }, []);
87
87
  var insertSelectedFormula = useCallback(function (formulaName) {
88
- 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>");
89
- refs.fxInput.current.innerHTML = ht;
88
+ var fxEditor = refs.fxInput.current;
89
+ if (!fxEditor) return;
90
90
  var cellEditor = document.getElementById("luckysheet-rich-text-editor");
91
+ var _a = buildFormulaSuggestionText(fxEditor, formulaName),
92
+ text = _a.text,
93
+ caretOffset = _a.caretOffset;
94
+ var safeText = escapeScriptTag(text);
95
+ var html = safeText.startsWith("=") ? functionHTMLGenerate(safeText) : escapeHTMLTag(safeText);
96
+ fxEditor.innerHTML = html;
91
97
  if (cellEditor) {
92
- cellEditor.innerHTML = ht;
98
+ cellEditor.innerHTML = html;
93
99
  }
94
- moveCursorToEnd(refs.fxInput.current);
100
+ setCursorPosition(fxEditor, caretOffset);
95
101
  setContext(function (draftCtx) {
96
102
  draftCtx.functionCandidates = [];
97
103
  draftCtx.defaultCandidates = [];
@@ -8,7 +8,7 @@ var __assign = this && this.__assign || function () {
8
8
  };
9
9
  return __assign.apply(this, arguments);
10
10
  };
11
- import { cancelNormalSelected, getCellValue, getInlineStringHTML, getStyleByCell, isInlineStringCell, moveToEnd, getFlowdata, handleFormulaInput, moveHighlightCell, escapeScriptTag, valueShowEs, createRangeHightlight, isShowHidenCR, israngeseleciton, escapeHTMLTag, isAllowEdit, getrangeseleciton, indexToColumnChar, handleBold, handleItalic, handleUnderline, handleStrikeThrough, getRangeRectsByCharacterOffset } from "@fileverse-dev/fortune-core";
11
+ import { cancelNormalSelected, getCellValue, getInlineStringHTML, getStyleByCell, isInlineStringCell, moveToEnd, getFlowdata, handleFormulaInput, moveHighlightCell, escapeScriptTag, valueShowEs, createRangeHightlight, isShowHidenCR, israngeseleciton, escapeHTMLTag, isAllowEdit, indexToColumnChar, functionHTMLGenerate, handleBold, handleItalic, handleUnderline, handleStrikeThrough, getRangeRectsByCharacterOffset, rangeSetValue, getFormulaRangeIndexForKeyboardSync, createFormulaRangeSelect, seletedHighlistByindex, isFormulaReferenceInputMode, rangeHightlightselected } from "@fileverse-dev/fortune-core";
12
12
  import React, { useContext, useEffect, useMemo, useRef, useCallback, useLayoutEffect, useState } from "react";
13
13
  import _ from "lodash";
14
14
  import { Tooltip } from "@fileverse/ui";
@@ -17,60 +17,62 @@ import ContentEditable from "./ContentEditable";
17
17
  import FormulaSearch from "./FormulaSearch";
18
18
  import FormulaHint from "./FormulaHint";
19
19
  import usePrevious from "../../hooks/usePrevious";
20
- import { moveCursorToEnd, getCursorPosition, isLetterNumberPattern, removeLastSpan, incrementColumn, decrementColumn, incrementRow, decrementRow, countCommasBeforeCursor } from "./helper";
20
+ import { moveCursorToEnd, getCursorPosition, setCursorPosition, buildFormulaSuggestionText, isLetterNumberPattern, countCommasBeforeCursor } from "./helper";
21
21
  import { LucideIcon } from "./LucideIcon";
22
22
  var InputBox = function InputBox() {
23
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
24
- var _m = useContext(WorkbookContext),
25
- context = _m.context,
26
- setContext = _m.setContext,
27
- refs = _m.refs;
23
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
24
+ var _o = useContext(WorkbookContext),
25
+ context = _o.context,
26
+ setContext = _o.setContext,
27
+ refs = _o.refs;
28
28
  var inputRef = useRef(null);
29
29
  var lastKeyDownEventRef = useRef(null);
30
30
  var prevCellUpdate = usePrevious(context.luckysheetCellUpdate);
31
31
  var prevSheetId = usePrevious(context.currentSheetId);
32
- var _o = useState(false),
33
- isHidenRC = _o[0],
34
- setIsHidenRC = _o[1];
35
32
  var _p = useState(false),
36
- isInputBoxActive = _p[0],
37
- setIsInputBoxActive = _p[1];
38
- var _q = useState(""),
39
- activeCell = _q[0],
40
- setActiveCell = _q[1];
33
+ isHidenRC = _p[0],
34
+ setIsHidenRC = _p[1];
35
+ var _q = useState(false),
36
+ isInputBoxActive = _q[0],
37
+ setIsInputBoxActive = _q[1];
41
38
  var _r = useState(""),
42
- activeRefCell = _r[0],
43
- setActiveRefCell = _r[1];
44
- var _s = useState({
39
+ activeCell = _r[0],
40
+ setActiveCell = _r[1];
41
+ var _s = useState(""),
42
+ activeRefCell = _s[0],
43
+ setActiveRefCell = _s[1];
44
+ var _t = useState({
45
45
  left: 0,
46
46
  top: 0
47
47
  }),
48
- frozenPosition = _s[0],
49
- setFrozenPosition = _s[1];
48
+ frozenPosition = _t[0],
49
+ setFrozenPosition = _t[1];
50
50
  var firstSelection = (_a = context.luckysheet_select_save) === null || _a === void 0 ? void 0 : _a[0];
51
- var _t = useState({}),
52
- firstSelectionActiveCell = _t[0],
53
- setFirstSelectionActiveCell = _t[1];
54
- var _u = useState(0),
55
- commaCount = _u[0],
56
- setCommaCount = _u[1];
51
+ var _u = useState({}),
52
+ firstSelectionActiveCell = _u[0],
53
+ setFirstSelectionActiveCell = _u[1];
54
+ var _v = useState(0),
55
+ commaCount = _v[0],
56
+ setCommaCount = _v[1];
57
57
  var hideFormulaHintLocal = localStorage.getItem("formulaMore") === "true";
58
- var _v = useState(!hideFormulaHintLocal),
59
- showFormulaHint = _v[0],
60
- setShowFormulaHint = _v[1];
61
- var _w = useState(false),
62
- showSearchHint = _w[0],
63
- setShowSearchHint = _w[1];
58
+ var _w = useState(!hideFormulaHintLocal),
59
+ showFormulaHint = _w[0],
60
+ setShowFormulaHint = _w[1];
61
+ var _x = useState(false),
62
+ showSearchHint = _x[0],
63
+ setShowSearchHint = _x[1];
64
64
  var row_index = firstSelection === null || firstSelection === void 0 ? void 0 : firstSelection.row_focus;
65
65
  var col_index = firstSelection === null || firstSelection === void 0 ? void 0 : firstSelection.column_focus;
66
66
  var preText = useRef("");
67
- var placeRef = useRef("");
68
67
  var isComposingRef = useRef(false);
68
+ var formulaAnchorCellRef = useRef(null);
69
+ var skipNextAnchorSelectionSyncRef = useRef(false);
70
+ var lastHandledMouseDragSignatureRef = useRef("");
69
71
  var ZWSP = "\u200B";
70
72
  var inputBoxInnerRef = useRef(null);
71
- var _x = useState([]),
72
- linkSelectionHighlightRects = _x[0],
73
- setLinkSelectionHighlightRects = _x[1];
73
+ var _y = useState([]),
74
+ linkSelectionHighlightRects = _y[0],
75
+ setLinkSelectionHighlightRects = _y[1];
74
76
  var ensureNotEmpty = function ensureNotEmpty() {
75
77
  var el = inputRef.current;
76
78
  if (!el) return;
@@ -167,6 +169,34 @@ var InputBox = function InputBox() {
167
169
  }
168
170
  }
169
171
  }, [context.luckysheetCellUpdate]);
172
+ useEffect(function () {
173
+ var _a;
174
+ if (_.isEmpty(context.luckysheetCellUpdate) || !refs.cellInput.current) {
175
+ formulaAnchorCellRef.current = null;
176
+ lastHandledMouseDragSignatureRef.current = "";
177
+ return;
178
+ }
179
+ var inputText = ((_a = refs.cellInput.current.innerText) === null || _a === void 0 ? void 0 : _a.trim()) || "";
180
+ if (!inputText.startsWith("=")) {
181
+ formulaAnchorCellRef.current = null;
182
+ lastHandledMouseDragSignatureRef.current = "";
183
+ }
184
+ }, [context.luckysheetCellUpdate, refs.cellInput]);
185
+ useEffect(function () {
186
+ if (_.isEmpty(context.luckysheetCellUpdate) || _.isEmpty(prevCellUpdate) || _.isEqual(prevCellUpdate, context.luckysheetCellUpdate)) {
187
+ return;
188
+ }
189
+ setContext(function (ctx) {
190
+ ctx.formulaRangeHighlight = [];
191
+ ctx.formulaRangeSelect = undefined;
192
+ ctx.formulaCache.selectingRangeIndex = -1;
193
+ ctx.formulaCache.func_selectedrange = undefined;
194
+ ctx.formulaCache.rangestart = false;
195
+ ctx.formulaCache.rangedrag_column_start = false;
196
+ ctx.formulaCache.rangedrag_row_start = false;
197
+ ctx.formulaCache.rangechangeindex = undefined;
198
+ });
199
+ }, [context.luckysheetCellUpdate, prevCellUpdate, setContext]);
170
200
  useEffect(function () {
171
201
  setIsHidenRC(isShowHidenCR(context));
172
202
  }, [context.luckysheet_select_save]);
@@ -176,61 +206,40 @@ var InputBox = function InputBox() {
176
206
  setIsInputBoxActive(false);
177
207
  }
178
208
  }, [firstSelection, (_c = context.rangeDialog) === null || _c === void 0 ? void 0 : _c.show, context.luckysheetCellUpdate]);
209
+ useEffect(function () {
210
+ if (isInputBoxActive) return;
211
+ setContext(function (ctx) {
212
+ if (_.isEmpty(ctx.formulaRangeHighlight) && !ctx.formulaRangeSelect && ctx.formulaCache.selectingRangeIndex === -1 && !ctx.formulaCache.func_selectedrange) {
213
+ return;
214
+ }
215
+ ctx.formulaRangeHighlight = [];
216
+ ctx.formulaRangeSelect = undefined;
217
+ ctx.formulaCache.selectingRangeIndex = -1;
218
+ ctx.formulaCache.func_selectedrange = undefined;
219
+ ctx.formulaCache.rangestart = false;
220
+ ctx.formulaCache.rangedrag_column_start = false;
221
+ ctx.formulaCache.rangedrag_row_start = false;
222
+ ctx.formulaCache.rangechangeindex = undefined;
223
+ ctx.formulaCache.rangeSelectionActive = null;
224
+ });
225
+ }, [isInputBoxActive, setContext]);
179
226
  var getActiveFormula = useCallback(function () {
180
227
  return document.querySelector(".luckysheet-formula-search-item-active");
181
228
  }, []);
182
229
  var insertSelectedFormula = useCallback(function (formulaName) {
183
- var _a;
184
- if (/^=[a-zA-Z]+$/.test(inputRef.current.innerText)) {
185
- 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>");
186
- inputRef.current.innerHTML = ht;
187
- var fxEditor = document.getElementById("luckysheet-functionbox-cell");
188
- if (fxEditor) {
189
- fxEditor.innerHTML = ht;
190
- }
191
- moveCursorToEnd(inputRef.current);
192
- setContext(function (draftCtx) {
193
- draftCtx.functionCandidates = [];
194
- draftCtx.defaultCandidates = [];
195
- draftCtx.functionHint = formulaName;
196
- });
197
- return;
198
- }
199
- var textEditor = document.getElementById("luckysheet-rich-text-editor");
230
+ var textEditor = inputRef.current;
200
231
  if (!textEditor) return;
201
- textEditor.focus();
202
- var selection = window.getSelection();
203
- var range = (selection === null || selection === void 0 ? void 0 : selection.rangeCount) ? selection.getRangeAt(0) : null;
204
- if (!selection || !range || !textEditor.contains(range.startContainer)) {
205
- range = document.createRange();
206
- range.selectNodeContents(textEditor);
207
- range.collapse(false);
208
- selection = window.getSelection();
209
- selection === null || selection === void 0 ? void 0 : selection.removeAllRanges();
210
- selection === null || selection === void 0 ? void 0 : selection.addRange(range);
211
- }
212
- var searchTxt = ((_a = getrangeseleciton()) === null || _a === void 0 ? void 0 : _a.textContent) || "";
213
- var deleteCount = searchTxt === "=" ? 0 : searchTxt.length;
214
- if (deleteCount > 0 && range.startContainer.nodeType === Node.TEXT_NODE && textEditor.contains(range.startContainer)) {
215
- var startOffset = Math.max(range.startOffset - deleteCount, 0);
216
- var endOffset = range.startOffset;
217
- range.setStart(range.startContainer, startOffset);
218
- range.setEnd(range.startContainer, endOffset);
219
- range.deleteContents();
220
- }
221
- textEditor.querySelectorAll(".luckysheet-formula-text-func, .luckysheet-formula-text-lpar").forEach(function (el) {
222
- return el.remove();
223
- });
224
- var funcNode = new DOMParser().parseFromString("<span dir=\"auto\" class=\"luckysheet-formula-text-func\">".concat(formulaName, "</span>"), "text/html").body.firstChild;
225
- var parNode = new DOMParser().parseFromString("<span dir=\"auto\" class=\"luckysheet-formula-text-lpar\">(</span>", "text/html").body.firstChild;
226
- if (range && parNode && funcNode) {
227
- range.insertNode(funcNode);
228
- range.collapse(false);
229
- range.insertNode(parNode);
230
- range.collapse(false);
231
- selection === null || selection === void 0 ? void 0 : selection.removeAllRanges();
232
- selection === null || selection === void 0 ? void 0 : selection.addRange(range);
233
- }
232
+ var fxEditor = document.getElementById("luckysheet-functionbox-cell");
233
+ var _a = buildFormulaSuggestionText(textEditor, formulaName),
234
+ text = _a.text,
235
+ caretOffset = _a.caretOffset;
236
+ var safeText = escapeScriptTag(text);
237
+ var html = safeText.startsWith("=") ? functionHTMLGenerate(safeText) : escapeHTMLTag(safeText);
238
+ textEditor.innerHTML = html;
239
+ if (fxEditor) {
240
+ fxEditor.innerHTML = html;
241
+ }
242
+ setCursorPosition(textEditor, caretOffset);
234
243
  setContext(function (draftCtx) {
235
244
  draftCtx.functionCandidates = [];
236
245
  draftCtx.defaultCandidates = [];
@@ -270,10 +279,135 @@ var InputBox = function InputBox() {
270
279
  event.stopPropagation();
271
280
  event.preventDefault();
272
281
  };
282
+ useEffect(function () {
283
+ var _a;
284
+ var cellInputEl = refs.cellInput.current;
285
+ if (!((_a = context.luckysheet_select_save) === null || _a === void 0 ? void 0 : _a[0]) || !cellInputEl) return;
286
+ setContext(function (ctx) {
287
+ var _a;
288
+ var currentSelection = (_a = ctx.luckysheet_select_save) === null || _a === void 0 ? void 0 : _a[ctx.luckysheet_select_save.length - 1];
289
+ if (!currentSelection) return;
290
+ var isMouseFormulaRangeDrag = !!ctx.formulaCache.func_selectedrange && (ctx.formulaCache.rangestart || ctx.formulaCache.rangedrag_column_start || ctx.formulaCache.rangedrag_row_start);
291
+ if (isMouseFormulaRangeDrag) {
292
+ return;
293
+ }
294
+ israngeseleciton(ctx);
295
+ var keyboardSyncRangeIndex = getFormulaRangeIndexForKeyboardSync(cellInputEl);
296
+ if (skipNextAnchorSelectionSyncRef.current && formulaAnchorCellRef.current) {
297
+ var _b = formulaAnchorCellRef.current,
298
+ anchorRow = _b[0],
299
+ anchorCol = _b[1];
300
+ var isAnchorSelection = currentSelection.row_focus === anchorRow && currentSelection.column_focus === anchorCol;
301
+ if (isAnchorSelection) {
302
+ skipNextAnchorSelectionSyncRef.current = false;
303
+ return;
304
+ }
305
+ }
306
+ var isFormulaMode = isFormulaReferenceInputMode(ctx);
307
+ if (!isFormulaMode) return;
308
+ if (keyboardSyncRangeIndex !== null) {
309
+ ctx.formulaCache.rangechangeindex = keyboardSyncRangeIndex;
310
+ ctx.formulaCache.rangestart = true;
311
+ ctx.formulaCache.rangedrag_column_start = false;
312
+ ctx.formulaCache.rangedrag_row_start = false;
313
+ } else {
314
+ ctx.formulaCache.rangechangeindex = undefined;
315
+ ctx.formulaCache.rangestart = false;
316
+ }
317
+ ctx.formulaCache.rangeSelectionActive = true;
318
+ rangeSetValue === null || rangeSetValue === void 0 ? void 0 : rangeSetValue(ctx, cellInputEl, {
319
+ row: currentSelection.row,
320
+ column: currentSelection.column
321
+ }, refs.fxInput.current);
322
+ rangeHightlightselected(ctx, cellInputEl);
323
+ if (!_.isNil(ctx.formulaCache.rangechangeindex)) {
324
+ ctx.formulaCache.selectingRangeIndex = ctx.formulaCache.rangechangeindex;
325
+ createRangeHightlight(ctx, cellInputEl.innerHTML, ctx.formulaCache.rangechangeindex);
326
+ var rectFromSelection = seletedHighlistByindex(ctx, currentSelection.row[0], currentSelection.row[1], currentSelection.column[0], currentSelection.column[1]);
327
+ if (rectFromSelection) {
328
+ createFormulaRangeSelect(ctx, {
329
+ rangeIndex: ctx.formulaCache.rangechangeindex || 0,
330
+ left: rectFromSelection.left,
331
+ top: rectFromSelection.top,
332
+ width: rectFromSelection.width,
333
+ height: rectFromSelection.height
334
+ });
335
+ }
336
+ }
337
+ });
338
+ }, [context.luckysheet_select_save, (_d = context.rangeDialog) === null || _d === void 0 ? void 0 : _d.show]);
339
+ var formulaMouseDragSignature = function () {
340
+ var _a, _b, _c, _d;
341
+ var r = context.formulaCache.func_selectedrange;
342
+ if (!r) return "";
343
+ 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(":");
344
+ }();
345
+ useEffect(function () {
346
+ var _a;
347
+ if (!formulaMouseDragSignature) return;
348
+ if (lastHandledMouseDragSignatureRef.current === formulaMouseDragSignature) {
349
+ return;
350
+ }
351
+ if (!refs.cellInput.current) return;
352
+ var inputText = ((_a = refs.cellInput.current.innerText) === null || _a === void 0 ? void 0 : _a.trim()) || "";
353
+ if (!inputText.startsWith("=")) return;
354
+ var dragRange = context.formulaCache.func_selectedrange;
355
+ if (!dragRange) return;
356
+ lastHandledMouseDragSignatureRef.current = formulaMouseDragSignature;
357
+ setContext(function (ctx) {
358
+ ctx.luckysheet_select_save = [{
359
+ row: [dragRange.row[0], dragRange.row[1]],
360
+ column: [dragRange.column[0], dragRange.column[1]],
361
+ row_focus: _.isNil(dragRange.row_focus) ? dragRange.row[0] : dragRange.row_focus,
362
+ column_focus: _.isNil(dragRange.column_focus) ? dragRange.column[0] : dragRange.column_focus
363
+ }];
364
+ });
365
+ }, [formulaMouseDragSignature, context.formulaCache.func_selectedrange, refs.cellInput, setContext]);
273
366
  var onKeyDown = useCallback(function (e) {
274
367
  var _a, _b;
275
368
  lastKeyDownEventRef.current = new KeyboardEvent(e.type, e.nativeEvent);
276
369
  preText.current = inputRef.current.innerText;
370
+ var currentInputText = ((_b = (_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.innerText) === null || _b === void 0 ? void 0 : _b.trim()) || "";
371
+ if ((e.key === "=" || currentInputText.startsWith("=")) && context.luckysheetCellUpdate.length === 2 && formulaAnchorCellRef.current == null) {
372
+ setContext(function (draftCtx) {
373
+ draftCtx.formulaCache.rangeSelectionActive = null;
374
+ });
375
+ formulaAnchorCellRef.current = [context.luckysheetCellUpdate[0], context.luckysheetCellUpdate[1]];
376
+ }
377
+ if (e.key === "(" && currentInputText.startsWith("=")) {
378
+ setContext(function (draftCtx) {
379
+ draftCtx.formulaCache.rangeSelectionActive = null;
380
+ });
381
+ }
382
+ if (e.key === "," && context.luckysheetCellUpdate.length > 0 && currentInputText.startsWith("=") && formulaAnchorCellRef.current) {
383
+ setContext(function (draftCtx) {
384
+ draftCtx.formulaCache.rangeSelectionActive = null;
385
+ });
386
+ var _c = formulaAnchorCellRef.current,
387
+ anchorRow_1 = _c[0],
388
+ anchorCol_1 = _c[1];
389
+ skipNextAnchorSelectionSyncRef.current = true;
390
+ setTimeout(function () {
391
+ setContext(function (draftCtx) {
392
+ draftCtx.luckysheetCellUpdate = [anchorRow_1, anchorCol_1];
393
+ draftCtx.luckysheet_select_save = [{
394
+ row: [anchorRow_1, anchorRow_1],
395
+ column: [anchorCol_1, anchorCol_1],
396
+ row_focus: anchorRow_1,
397
+ column_focus: anchorCol_1
398
+ }];
399
+ draftCtx.formulaRangeHighlight = [];
400
+ draftCtx.formulaRangeSelect = undefined;
401
+ draftCtx.formulaCache.selectingRangeIndex = -1;
402
+ draftCtx.formulaCache.func_selectedrange = undefined;
403
+ draftCtx.formulaCache.rangechangeindex = undefined;
404
+ draftCtx.formulaCache.rangestart = false;
405
+ draftCtx.formulaCache.rangedrag_column_start = false;
406
+ draftCtx.formulaCache.rangedrag_row_start = false;
407
+ moveHighlightCell(draftCtx, "down", 0, "rangeOfSelect");
408
+ });
409
+ }, 0);
410
+ }
277
411
  if (e.metaKey && context.luckysheetCellUpdate.length > 0) {
278
412
  if (e.code === "KeyB") {
279
413
  handleBold(context, inputRef.current);
@@ -294,6 +428,7 @@ var InputBox = function InputBox() {
294
428
  setCommaCount(currentCommaCount);
295
429
  }
296
430
  var allowListNavigation = true;
431
+ var isArrowKey = e.key === "ArrowUp" || e.key === "ArrowDown" || e.key === "ArrowLeft" || e.key === "ArrowRight";
297
432
  if (e.key === "Delete" || e.key === "Backspace") {
298
433
  if (isComposingRef.current) requestAnimationFrame(ensureNotEmpty);
299
434
  if (getCursorPosition(inputRef === null || inputRef === void 0 ? void 0 : inputRef.current) === (inputRef === null || inputRef === void 0 ? void 0 : inputRef.current.innerText.length)) {
@@ -302,39 +437,8 @@ var InputBox = function InputBox() {
302
437
  }, 5);
303
438
  }
304
439
  }
305
- var refCell = placeRef.current;
306
- if (e.key === "ArrowUp") {
307
- refCell = decrementRow(placeRef.current);
308
- } else if (e.key === "ArrowDown") {
309
- refCell = incrementRow(placeRef.current);
310
- } else if (e.key === "ArrowLeft") {
311
- refCell = decrementColumn(placeRef.current);
312
- } else if (e.key === "ArrowRight") {
313
- refCell = incrementColumn(placeRef.current);
314
- }
315
- if ((e.key === "ArrowUp" || e.key === "ArrowDown" || e.key === "ArrowLeft" || e.key === "ArrowRight") && !(getCursorPosition(inputRef === null || inputRef === void 0 ? void 0 : inputRef.current) !== (inputRef === null || inputRef === void 0 ? void 0 : inputRef.current.innerText.length) && e.key === "ArrowRight")) {
316
- var parser = new DOMParser();
317
- 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");
318
- var spans = doc.querySelectorAll("span");
319
- var lastSpan = spans[spans.length - 1];
320
- var notFunctionInit = !((_b = document.getElementById("luckysheet-rich-text-editor")) === null || _b === void 0 ? void 0 : _b.innerText.includes("("));
321
- 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) && !_.includes(["="], lastSpan === null || lastSpan === void 0 ? void 0 : lastSpan.innerText);
322
- 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('"')) && !isLetterNumberPattern(lastSpan === null || lastSpan === void 0 ? void 0 : lastSpan.innerText) && !arrowRefNotAllowed && !/^[a-zA-Z]+$/.test(lastSpan === null || lastSpan === void 0 ? void 0 : lastSpan.innerText)) {
323
- allowListNavigation = false;
324
- inputRef.current.innerHTML = "".concat(inputRef.current.innerHTML, "<span class=\"fortune-formula-functionrange-cell\" rangeindex=\"0\" dir=\"auto\" style=\"color:#c1232b;\">").concat(refCell, "</span>");
325
- setTimeout(function () {
326
- moveCursorToEnd(inputRef.current);
327
- }, 1);
328
- }
329
- if (isLetterNumberPattern(lastSpan === null || lastSpan === void 0 ? void 0 : lastSpan.innerText)) {
330
- allowListNavigation = false;
331
- var htmlR = removeLastSpan(inputRef === null || inputRef === void 0 ? void 0 : inputRef.current.innerHTML);
332
- inputRef.current.innerHTML = "".concat(htmlR, "<span class=\"fortune-formula-functionrange-cell\" rangeindex=\"0\" dir=\"auto\" style=\"color:#c1232b;\">").concat(refCell, "</span>");
333
- moveCursorToEnd(inputRef.current);
334
- setTimeout(function () {
335
- moveCursorToEnd(inputRef.current);
336
- }, 1);
337
- }
440
+ if (isArrowKey && isFormulaReferenceInputMode(context)) {
441
+ allowListNavigation = false;
338
442
  }
339
443
  if (e.key === "Escape" && context.luckysheetCellUpdate.length > 0) {
340
444
  setContext(function (draftCtx) {
@@ -411,7 +515,7 @@ var InputBox = function InputBox() {
411
515
  }
412
516
  };
413
517
  var onChange = useCallback(function (__, isBlur) {
414
- var _a;
518
+ var _a, _b, _c;
415
519
  if (context.isFlvReadOnly) return;
416
520
  handleHideShowHint();
417
521
  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())) {
@@ -424,7 +528,16 @@ var InputBox = function InputBox() {
424
528
  setCommaCount(currentCommaCount);
425
529
  }
426
530
  var e = lastKeyDownEventRef.current;
427
- if (!e) return;
531
+ if (!e) {
532
+ var cellEl_1 = refs.cellInput.current;
533
+ 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("=")))) {
534
+ setContext(function (draftCtx) {
535
+ if (!isAllowEdit(draftCtx, draftCtx.luckysheet_select_save)) return;
536
+ rangeHightlightselected(draftCtx, cellEl_1);
537
+ });
538
+ }
539
+ return;
540
+ }
428
541
  var kcode = e.keyCode;
429
542
  if (!kcode) return;
430
543
  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) {
@@ -480,7 +593,7 @@ var InputBox = function InputBox() {
480
593
  zIndex: _.isEmpty(context.luckysheetCellUpdate) ? -1 : 19,
481
594
  display: "block"
482
595
  };
483
- }, [firstSelection, (_d = context.rangeDialog) === null || _d === void 0 ? void 0 : _d.show, context.luckysheetCellUpdate, refs.cellArea, isInputBoxActive, frozenPosition, context.scrollLeft, context.scrollTop]);
596
+ }, [firstSelection, (_e = context.rangeDialog) === null || _e === void 0 ? void 0 : _e.show, context.luckysheetCellUpdate, refs.cellArea, isInputBoxActive, frozenPosition, context.scrollLeft, context.scrollTop]);
484
597
  useEffect(function () {
485
598
  var _a;
486
599
  if (firstSelection && !((_a = context.rangeDialog) === null || _a === void 0 ? void 0 : _a.show) && !isInputBoxActive && !_.isEmpty(context.luckysheetCellUpdate)) {
@@ -496,7 +609,7 @@ var InputBox = function InputBox() {
496
609
  setIsInputBoxActive(true);
497
610
  }
498
611
  }
499
- }, [firstSelection, (_e = context.rangeDialog) === null || _e === void 0 ? void 0 : _e.show, context.luckysheetCellUpdate, isInputBoxActive, context.scrollLeft, context.scrollTop, refs.cellArea]);
612
+ }, [firstSelection, (_f = context.rangeDialog) === null || _f === void 0 ? void 0 : _f.show, context.luckysheetCellUpdate, isInputBoxActive, context.scrollLeft, context.scrollTop, refs.cellArea]);
500
613
  var getAddressIndicatorPosition = useCallback(function () {
501
614
  var _a;
502
615
  if (!firstSelection || ((_a = context.rangeDialog) === null || _a === void 0 ? void 0 : _a.show)) {
@@ -509,7 +622,7 @@ var InputBox = function InputBox() {
509
622
  left: "0",
510
623
  display: "block"
511
624
  };
512
- }, [firstSelection, (_f = context.rangeDialog) === null || _f === void 0 ? void 0 : _f.show]);
625
+ }, [firstSelection, (_g = context.rangeDialog) === null || _g === void 0 ? void 0 : _g.show]);
513
626
  var getCellAddress = useCallback(function () {
514
627
  if (!firstSelection) return "";
515
628
  var rowIndex = firstSelection.row_focus || 0;
@@ -559,10 +672,9 @@ var InputBox = function InputBox() {
559
672
  };
560
673
  });
561
674
  setLinkSelectionHighlightRects(relative);
562
- }, [(_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]);
675
+ }, [(_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]);
563
676
  var wraperGetCell = function wraperGetCell() {
564
677
  var cell = getCellAddress();
565
- placeRef.current = cell;
566
678
  if (activeRefCell !== cell) {
567
679
  setActiveRefCell(cell);
568
680
  }
@@ -586,6 +698,7 @@ var InputBox = function InputBox() {
586
698
  }, []);
587
699
  var functionName = context.functionHint || getFunctionNameFromInput();
588
700
  var fn = functionName ? context.formulaCache.functionlistMap[functionName] : null;
701
+ var inputBoxBaseSelection = isInputBoxActive && firstSelectionActiveCell ? firstSelectionActiveCell : firstSelection;
589
702
  return /*#__PURE__*/React.createElement("div", {
590
703
  className: "luckysheet-input-box",
591
704
  id: "luckysheet-input-box",
@@ -596,16 +709,16 @@ var InputBox = function InputBox() {
596
709
  onMouseUp: function onMouseUp(e) {
597
710
  return e.stopPropagation();
598
711
  }
599
- }, firstSelection && !((_l = context.rangeDialog) === null || _l === void 0 ? void 0 : _l.show) && (/*#__PURE__*/React.createElement("div", {
712
+ }, firstSelection && !((_m = context.rangeDialog) === null || _m === void 0 ? void 0 : _m.show) && (/*#__PURE__*/React.createElement("div", {
600
713
  className: "luckysheet-cell-address-indicator",
601
714
  style: getAddressIndicatorPosition()
602
715
  }, wraperGetCell())), /*#__PURE__*/React.createElement("div", {
603
716
  ref: inputBoxInnerRef,
604
717
  className: "luckysheet-input-box-inner",
605
- style: firstSelection ? __assign({
718
+ style: inputBoxBaseSelection ? __assign({
606
719
  position: "relative",
607
- minWidth: firstSelection.width,
608
- minHeight: firstSelection.height
720
+ minWidth: inputBoxBaseSelection.width,
721
+ minHeight: inputBoxBaseSelection.height
609
722
  }, inputBoxStyle) : {
610
723
  position: "relative"
611
724
  }
@@ -211,7 +211,7 @@ export var useColumnDragAndDrop = function useColumnDragAndDrop(containerRef, se
211
211
  var sheetIdx_1 = getSheetIndex(context, context.currentSheetId);
212
212
  if (sheetIdx_1 != null && sourceIndex_1 >= 0 && Number.isFinite(finalInsertionIndex_1) && finalInsertionIndex_1 >= 0) {
213
213
  setContext(function (draft) {
214
- var _a, _b, _c, _d, _e, _f, _g;
214
+ var _a, _b, _c, _d, _e, _f, _g, _h;
215
215
  var _sheet = draft.luckysheetfile[sheetIdx_1];
216
216
  if (!(_sheet === null || _sheet === void 0 ? void 0 : _sheet.data)) return;
217
217
  var rows = _sheet.data;
@@ -374,6 +374,30 @@ export var useColumnDragAndDrop = function useColumnDragAndDrop(containerRef, se
374
374
  }
375
375
  });
376
376
  (_g = window === null || window === void 0 ? void 0 : window.updateDataBlockCalcFunctionAfterRowDrag) === null || _g === void 0 ? void 0 : _g.call(window, selectedSourceCol, selectedTargetCol, "column", context.currentSheetId, sourceIndex_1, targetIndex);
377
+ var cellChanges = [];
378
+ var affectedColStart = Math.min(sourceIndex_1, targetIndex);
379
+ var affectedColEnd = Math.max(sourceIndex_1, targetIndex) + selectedSourceCol.length - 1;
380
+ var numRows = rows.length;
381
+ for (var r = 0; r < numRows; r += 1) {
382
+ var row = rows[r];
383
+ for (var c = affectedColStart; c <= affectedColEnd; c += 1) {
384
+ var cell = row === null || row === void 0 ? void 0 : row[c];
385
+ cellChanges.push({
386
+ sheetId: draft.currentSheetId,
387
+ path: ["celldata"],
388
+ value: {
389
+ r: r,
390
+ c: c,
391
+ v: cell !== null && cell !== void 0 ? cell : null
392
+ },
393
+ key: "".concat(r, "_").concat(c),
394
+ type: "update"
395
+ });
396
+ }
397
+ }
398
+ if (cellChanges.length > 0 && ((_h = draft.hooks) === null || _h === void 0 ? void 0 : _h.updateCellYdoc)) {
399
+ draft.hooks.updateCellYdoc(cellChanges);
400
+ }
377
401
  var rowLen = (d === null || d === void 0 ? void 0 : d.length) || 0;
378
402
  api.setSelection(draft, [{
379
403
  row: [0, rowLen],
@@ -207,7 +207,7 @@ export var useRowDragAndDrop = function useRowDragAndDrop(containerRef, selected
207
207
  var sheetIdx_1 = getSheetIndex(context, context.currentSheetId);
208
208
  if (sheetIdx_1 != null && sourceIndex_1 >= 0 && Number.isFinite(finalInsertionIndex_1) && finalInsertionIndex_1 >= 0) {
209
209
  setContext(function (draft) {
210
- var _a, _b, _c, _d, _e, _f;
210
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j;
211
211
  var _sheet = draft.luckysheetfile[sheetIdx_1];
212
212
  if (!(_sheet === null || _sheet === void 0 ? void 0 : _sheet.data)) return;
213
213
  var rows = _sheet.data;
@@ -364,7 +364,31 @@ export var useRowDragAndDrop = function useRowDragAndDrop(containerRef, selected
364
364
  }
365
365
  });
366
366
  (_e = window === null || window === void 0 ? void 0 : window.updateDataBlockCalcFunctionAfterRowDrag) === null || _e === void 0 ? void 0 : _e.call(window, sourceIndex_1, targetIndex, "row", context.currentSheetId);
367
- var colLen = ((_f = d === null || d === void 0 ? void 0 : d[0]) === null || _f === void 0 ? void 0 : _f.length) || 0;
367
+ var cellChanges = [];
368
+ var affectedRowStart = Math.min(sourceIndex_1, targetIndex);
369
+ var affectedRowEnd = Math.max(sourceIndex_1, targetIndex) + selectedSourceRow.length - 1;
370
+ var numCols = (_g = (_f = d === null || d === void 0 ? void 0 : d[0]) === null || _f === void 0 ? void 0 : _f.length) !== null && _g !== void 0 ? _g : 0;
371
+ for (var r = affectedRowStart; r <= affectedRowEnd; r += 1) {
372
+ var row = rows[r];
373
+ for (var c = 0; c < numCols; c += 1) {
374
+ var cell = row === null || row === void 0 ? void 0 : row[c];
375
+ cellChanges.push({
376
+ sheetId: draft.currentSheetId,
377
+ path: ["celldata"],
378
+ value: {
379
+ r: r,
380
+ c: c,
381
+ v: cell !== null && cell !== void 0 ? cell : null
382
+ },
383
+ key: "".concat(r, "_").concat(c),
384
+ type: "update"
385
+ });
386
+ }
387
+ }
388
+ if (cellChanges.length > 0 && ((_h = draft.hooks) === null || _h === void 0 ? void 0 : _h.updateCellYdoc)) {
389
+ draft.hooks.updateCellYdoc(cellChanges);
390
+ }
391
+ var colLen = ((_j = d === null || d === void 0 ? void 0 : d[0]) === null || _j === void 0 ? void 0 : _j.length) || 0;
368
392
  api.setSelection(draft, [{
369
393
  row: [selectedTargetRow[0], selectedTargetRow[selectedTargetRow.length - 1]],
370
394
  column: [0, colLen]
@@ -1,5 +1,10 @@
1
1
  export declare function moveCursorToEnd(editableDiv: HTMLDivElement): void;
2
2
  export declare function getCursorPosition(editableDiv: HTMLDivElement): number;
3
+ export declare function setCursorPosition(editableDiv: HTMLDivElement, targetOffset: number): void;
4
+ export declare function buildFormulaSuggestionText(editableDiv: HTMLDivElement, formulaName: string): {
5
+ text: string;
6
+ caretOffset: number;
7
+ };
3
8
  export declare function isLetterNumberPattern(str: string): boolean;
4
9
  export declare function removeLastSpan(htmlString: string): string;
5
10
  export declare function numberToColumn(colNumber: number): string;