@fileverse-dev/fortune-core 1.3.12 → 1.3.13-create-1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (65) hide show
  1. package/es/events/keyboard.d.ts +2 -2
  2. package/es/events/keyboard.js +204 -30
  3. package/es/events/mouse.js +79 -44
  4. package/es/events/paste.js +177 -56
  5. package/es/locale/en.d.ts +3 -0
  6. package/es/locale/en.js +3 -0
  7. package/es/locale/es.d.ts +3 -0
  8. package/es/locale/es.js +3 -0
  9. package/es/locale/hi.d.ts +3 -0
  10. package/es/locale/hi.js +3 -0
  11. package/es/locale/index.d.ts +3 -0
  12. package/es/locale/zh.d.ts +3 -0
  13. package/es/locale/zh.js +3 -0
  14. package/es/locale/zh_tw.d.ts +3 -0
  15. package/es/locale/zh_tw.js +3 -0
  16. package/es/modules/ConditionFormat.js +26 -0
  17. package/es/modules/cell.d.ts +4 -1
  18. package/es/modules/cell.js +124 -15
  19. package/es/modules/clipboard.js +111 -2
  20. package/es/modules/format.js +12 -7
  21. package/es/modules/formula.d.ts +23 -0
  22. package/es/modules/formula.js +610 -51
  23. package/es/modules/hyperlink.js +18 -6
  24. package/es/modules/inline-string.js +21 -4
  25. package/es/modules/moveCells.js +52 -9
  26. package/es/modules/selection.d.ts +1 -0
  27. package/es/modules/selection.js +102 -16
  28. package/es/modules/validation.js +6 -3
  29. package/es/paste-helpers/calculate-range-cell-size.js +5 -4
  30. package/es/paste-table-helpers.d.ts +1 -1
  31. package/es/paste-table-helpers.js +170 -21
  32. package/es/types.d.ts +3 -0
  33. package/lib/events/keyboard.d.ts +2 -2
  34. package/lib/events/keyboard.js +203 -29
  35. package/lib/events/mouse.js +78 -43
  36. package/lib/events/paste.js +175 -54
  37. package/lib/locale/en.d.ts +3 -0
  38. package/lib/locale/en.js +3 -0
  39. package/lib/locale/es.d.ts +3 -0
  40. package/lib/locale/es.js +3 -0
  41. package/lib/locale/hi.d.ts +3 -0
  42. package/lib/locale/hi.js +3 -0
  43. package/lib/locale/index.d.ts +3 -0
  44. package/lib/locale/zh.d.ts +3 -0
  45. package/lib/locale/zh.js +3 -0
  46. package/lib/locale/zh_tw.d.ts +3 -0
  47. package/lib/locale/zh_tw.js +3 -0
  48. package/lib/modules/ConditionFormat.js +26 -0
  49. package/lib/modules/cell.d.ts +4 -1
  50. package/lib/modules/cell.js +123 -14
  51. package/lib/modules/clipboard.js +111 -2
  52. package/lib/modules/format.js +12 -7
  53. package/lib/modules/formula.d.ts +23 -0
  54. package/lib/modules/formula.js +623 -51
  55. package/lib/modules/hyperlink.js +18 -6
  56. package/lib/modules/inline-string.js +21 -4
  57. package/lib/modules/moveCells.js +52 -9
  58. package/lib/modules/selection.d.ts +1 -0
  59. package/lib/modules/selection.js +101 -15
  60. package/lib/modules/validation.js +6 -3
  61. package/lib/paste-helpers/calculate-range-cell-size.js +5 -4
  62. package/lib/paste-table-helpers.d.ts +1 -1
  63. package/lib/paste-table-helpers.js +170 -21
  64. package/lib/types.d.ts +3 -0
  65. package/package.json +1 -1
@@ -11,15 +11,26 @@ exports.execFunctionGroup = execFunctionGroup;
11
11
  exports.execfunction = execfunction;
12
12
  exports.functionCopy = functionCopy;
13
13
  exports.functionHTMLGenerate = functionHTMLGenerate;
14
+ exports.functionMoveReference = functionMoveReference;
14
15
  exports.functionStrChange = functionStrChange;
15
16
  exports.getAllFunctionGroup = getAllFunctionGroup;
17
+ exports.getFormulaEditorOwner = getFormulaEditorOwner;
18
+ exports.getFormulaRangeIndexAtCaret = getFormulaRangeIndexAtCaret;
19
+ exports.getFormulaRangeIndexForKeyboardSync = getFormulaRangeIndexForKeyboardSync;
20
+ exports.getLastFormulaRangeIndex = getLastFormulaRangeIndex;
16
21
  exports.getcellrange = getcellrange;
17
22
  exports.getrangeseleciton = getrangeseleciton;
18
23
  exports.groupValuesRefresh = groupValuesRefresh;
19
24
  exports.handleFormulaInput = handleFormulaInput;
25
+ exports.hasIncompleteTruncatedCellRangeSyntax = hasIncompleteTruncatedCellRangeSyntax;
20
26
  exports.insertUpdateFunctionGroup = insertUpdateFunctionGroup;
27
+ exports.isBareCellOrRangeOnlyFormula = isBareCellOrRangeOnlyFormula;
28
+ exports.isCaretAtValidFormulaRangeInsertionPoint = isCaretAtValidFormulaRangeInsertionPoint;
29
+ exports.isFormulaReferenceInputMode = isFormulaReferenceInputMode;
21
30
  exports.iscelldata = iscelldata;
22
31
  exports.israngeseleciton = israngeseleciton;
32
+ exports.markRangeSelectionDirty = markRangeSelectionDirty;
33
+ exports.maybeRecoverDirtyRangeSelection = maybeRecoverDirtyRangeSelection;
23
34
  exports.moveCursorToEnd = moveCursorToEnd;
24
35
  exports.onFormulaRangeDragEnd = onFormulaRangeDragEnd;
25
36
  exports.rangeDrag = rangeDrag;
@@ -28,6 +39,8 @@ exports.rangeDragRow = rangeDragRow;
28
39
  exports.rangeHightlightselected = rangeHightlightselected;
29
40
  exports.rangeSetValue = rangeSetValue;
30
41
  exports.setCaretPosition = setCaretPosition;
42
+ exports.setFormulaEditorOwner = setFormulaEditorOwner;
43
+ exports.suppressFormulaRangeSelectionForInitialEdit = suppressFormulaRangeSelectionForInitialEdit;
31
44
  var _formulaParser = require("@fileverse-dev/formula-parser");
32
45
  var _lodash = _interopRequireDefault(require("lodash"));
33
46
  var _context = require("../context");
@@ -38,7 +51,6 @@ var _locale = require("../locale");
38
51
  var _color = require("./color");
39
52
  var _location = require("./location");
40
53
  var _2 = require(".");
41
- var _index = require("../utils/index");
42
54
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
43
55
  function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
44
56
  var __assign = void 0 && (void 0).__assign || function () {
@@ -86,6 +98,8 @@ var FormulaCache = exports.FormulaCache = function () {
86
98
  var that = this;
87
99
  this.data_parm_index = 0;
88
100
  this.selectingRangeIndex = -1;
101
+ this.rangeSelectionActive = null;
102
+ this.formulaEditorOwner = null;
89
103
  this.functionlistMap = {};
90
104
  this.execFunctionGlobalData = {};
91
105
  this.cellTextToIndexList = {};
@@ -1316,8 +1330,7 @@ function setCaretPosition(ctx, textDom, children, pos, parentTextDom) {
1316
1330
  sel === null || sel === void 0 ? void 0 : sel.removeAllRanges();
1317
1331
  sel === null || sel === void 0 ? void 0 : sel.addRange(range);
1318
1332
  el.focus();
1319
- } catch (err) {
1320
- console.error(err);
1333
+ } catch (_c) {
1321
1334
  moveCursorToEnd(parentTextDom);
1322
1335
  }
1323
1336
  }
@@ -1402,7 +1415,7 @@ function getrangeseleciton() {
1402
1415
  return null;
1403
1416
  }
1404
1417
  function helpFunctionExe($editer, currSelection, ctx) {
1405
- var _a;
1418
+ var _a, _b, _c, _d, _e, _f;
1406
1419
  var functionlist = (0, _locale.locale)(ctx).functionlist;
1407
1420
  if (_lodash.default.isEmpty(ctx.formulaCache.functionlistMap)) {
1408
1421
  for (var i_1 = 0; i_1 < functionlist.length; i_1 += 1) {
@@ -1413,22 +1426,23 @@ function helpFunctionExe($editer, currSelection, ctx) {
1413
1426
  return null;
1414
1427
  }
1415
1428
  var $prev = currSelection;
1416
- var $span = $editer.querySelectorAll("span");
1417
- var currentIndex = _lodash.default.indexOf((_a = currSelection.parentNode) === null || _a === void 0 ? void 0 : _a.childNodes, currSelection);
1429
+ var $span = Array.from($editer.querySelectorAll("span"));
1430
+ var selectionSpan = currSelection.nodeType === Node.ELEMENT_NODE ? currSelection.closest("span") : (_a = currSelection.parentElement) === null || _a === void 0 ? void 0 : _a.closest("span");
1431
+ var currentIndex = selectionSpan ? $span.indexOf(selectionSpan) : -1;
1418
1432
  var i = currentIndex;
1419
- if ($prev == null) {
1433
+ if ($prev == null || currentIndex < 0 || !$span[currentIndex]) {
1420
1434
  return null;
1421
1435
  }
1422
1436
  var funcName = null;
1423
1437
  var paramindex = null;
1424
- if ($span[i].classList.contains("luckysheet-formula-text-func")) {
1438
+ if ((_c = (_b = $span[i]) === null || _b === void 0 ? void 0 : _b.classList) === null || _c === void 0 ? void 0 : _c.contains("luckysheet-formula-text-func")) {
1425
1439
  funcName = $span[i].textContent;
1426
1440
  } else {
1427
1441
  var $cur = null;
1428
1442
  var exceptIndex = [-1, -1];
1429
1443
  while (--i > 0) {
1430
1444
  $cur = $span[i];
1431
- if ($cur.classList.contains("luckysheet-formula-text-func") || _lodash.default.trim($cur.textContent || "").toUpperCase() in ctx.formulaCache.functionlistMap) {
1445
+ if (((_d = $cur === null || $cur === void 0 ? void 0 : $cur.classList) === null || _d === void 0 ? void 0 : _d.contains("luckysheet-formula-text-func")) || _lodash.default.trim($cur.textContent || "").toUpperCase() in ctx.formulaCache.functionlistMap) {
1432
1446
  funcName = $cur.textContent;
1433
1447
  paramindex = null;
1434
1448
  var endstate = true;
@@ -1440,13 +1454,13 @@ function helpFunctionExe($editer, currSelection, ctx) {
1440
1454
  continue;
1441
1455
  }
1442
1456
  $cur = $span[a];
1443
- if ($cur.classList.contains("luckysheet-formula-text-rpar")) {
1457
+ if ((_e = $cur === null || $cur === void 0 ? void 0 : $cur.classList) === null || _e === void 0 ? void 0 : _e.contains("luckysheet-formula-text-rpar")) {
1444
1458
  exceptIndex = [i, a];
1445
1459
  funcName = null;
1446
1460
  endstate = false;
1447
1461
  break;
1448
1462
  }
1449
- if ($cur.classList.contains("luckysheet-formula-text-comma")) {
1463
+ if ((_f = $cur === null || $cur === void 0 ? void 0 : $cur.classList) === null || _f === void 0 ? void 0 : _f.contains("luckysheet-formula-text-comma")) {
1450
1464
  paramindex += 1;
1451
1465
  }
1452
1466
  }
@@ -1479,6 +1493,7 @@ function rangeHightlightselected(ctx, $editor) {
1479
1493
  var funcName = helpFunctionExe($editor, currSelection, ctx);
1480
1494
  ctx.functionHint = funcName === null || funcName === void 0 ? void 0 : funcName.toUpperCase();
1481
1495
  ctx.functionCandidates = [];
1496
+ ctx.defaultCandidates = [];
1482
1497
  }
1483
1498
  }
1484
1499
  function functionHTML(txt) {
@@ -1629,6 +1644,226 @@ function getRangeIndexes($editor) {
1629
1644
  });
1630
1645
  return res;
1631
1646
  }
1647
+ function getLastFormulaRangeIndex($editor) {
1648
+ var spans = Array.from($editor.querySelectorAll("span")).filter(function (span) {
1649
+ var _a;
1650
+ return (_a = span.textContent) === null || _a === void 0 ? void 0 : _a.trim().length;
1651
+ });
1652
+ var lastSpan = spans[spans.length - 1];
1653
+ if (!lastSpan) return null;
1654
+ if (!lastSpan.classList.contains("fortune-formula-functionrange-cell")) {
1655
+ return null;
1656
+ }
1657
+ var indexStr = lastSpan.getAttribute("rangeindex");
1658
+ if (!indexStr) return null;
1659
+ var rangeIndex = parseInt(indexStr, 10);
1660
+ return Number.isNaN(rangeIndex) ? null : rangeIndex;
1661
+ }
1662
+ function getFormulaRangeIndexAtCaret($editor) {
1663
+ var sel = window.getSelection();
1664
+ if (!sel || sel.rangeCount === 0) return null;
1665
+ var anchorNode = sel.anchorNode;
1666
+ if (!anchorNode) return null;
1667
+ var el = anchorNode.nodeType === Node.ELEMENT_NODE ? anchorNode : anchorNode.parentElement;
1668
+ if (!el) return null;
1669
+ var cell = el.closest(".fortune-formula-functionrange-cell");
1670
+ if (!cell || !$editor.contains(cell)) return null;
1671
+ var ri = cell.getAttribute("rangeindex");
1672
+ if (!ri) return null;
1673
+ var n = parseInt(ri, 10);
1674
+ return Number.isNaN(n) ? null : n;
1675
+ }
1676
+ function setFormulaEditorOwner(ctx, owner) {
1677
+ ctx.formulaCache.formulaEditorOwner = owner;
1678
+ }
1679
+ function getFormulaEditorOwner(ctx) {
1680
+ var _a, _b;
1681
+ var cachedOwner = ctx.formulaCache.formulaEditorOwner;
1682
+ if (cachedOwner === "cell" || cachedOwner === "fx") {
1683
+ return cachedOwner;
1684
+ }
1685
+ if (((_a = document.activeElement) === null || _a === void 0 ? void 0 : _a.id) === "luckysheet-functionbox-cell") {
1686
+ return "fx";
1687
+ }
1688
+ if (((_b = document.activeElement) === null || _b === void 0 ? void 0 : _b.id) === "luckysheet-rich-text-editor") {
1689
+ return "cell";
1690
+ }
1691
+ return null;
1692
+ }
1693
+ function getActiveFormulaEditorElement(ctx) {
1694
+ var _a;
1695
+ var cellEditor = document.getElementById("luckysheet-rich-text-editor");
1696
+ var fxEditor = document.getElementById("luckysheet-functionbox-cell");
1697
+ var owner = getFormulaEditorOwner(ctx);
1698
+ if (owner === "fx") return fxEditor !== null && fxEditor !== void 0 ? fxEditor : cellEditor;
1699
+ if (owner === "cell") return cellEditor !== null && cellEditor !== void 0 ? cellEditor : fxEditor;
1700
+ var activeId = (_a = document.activeElement) === null || _a === void 0 ? void 0 : _a.id;
1701
+ if (activeId === "luckysheet-functionbox-cell") return fxEditor !== null && fxEditor !== void 0 ? fxEditor : cellEditor;
1702
+ if (activeId === "luckysheet-rich-text-editor") return cellEditor !== null && cellEditor !== void 0 ? cellEditor : fxEditor;
1703
+ return cellEditor !== null && cellEditor !== void 0 ? cellEditor : fxEditor;
1704
+ }
1705
+ function getCurrentFormulaSlotTextBeforeCaret(editor, caretOffset) {
1706
+ var textBefore = editor.innerText.slice(0, caretOffset);
1707
+ var parts = textBefore.split(/[=,(+\-*/&<>]/);
1708
+ return _lodash.default.trim(parts[parts.length - 1] || "");
1709
+ }
1710
+ function hasIncompleteTruncatedCellRangeSyntax(formulaText) {
1711
+ var t = formulaText.replace(/\s/g, "");
1712
+ if (!t.startsWith("=")) return false;
1713
+ if (/[A-Za-z]+\d+:[A-Za-z]+$/i.test(t)) return true;
1714
+ if (/[A-Za-z]+\d+:\s*$/i.test(t)) return true;
1715
+ return false;
1716
+ }
1717
+ function isIncompleteTruncatedRangeToken(token) {
1718
+ var t = token.replace(/\s/g, "");
1719
+ if (!t) return false;
1720
+ if (/[A-Za-z]+\d+:[A-Za-z]*$/i.test(t)) {
1721
+ return !/[A-Za-z]+\d+:[A-Za-z]+\d+$/i.test(t);
1722
+ }
1723
+ return false;
1724
+ }
1725
+ function isCaretInsideIncompleteTruncatedRangeSyntax(editor, caretOffset) {
1726
+ var textBefore = editor.innerText.slice(0, caretOffset);
1727
+ var textAfter = editor.innerText.slice(caretOffset);
1728
+ var tokenSplit = /[=,()+\-*/&<>%^]/;
1729
+ var leftToken = (textBefore.split(tokenSplit).pop() || "").trim();
1730
+ var rightToken = (textAfter.split(tokenSplit)[0] || "").trim();
1731
+ var tokenAtCaret = "".concat(leftToken).concat(rightToken);
1732
+ return isIncompleteTruncatedRangeToken(tokenAtCaret);
1733
+ }
1734
+ function isBareCellOrRangeOnlyFormula(formulaText) {
1735
+ var t = formulaText.trim();
1736
+ if (!t.startsWith("=")) return false;
1737
+ var body = t.slice(1).trim();
1738
+ if (!body) return false;
1739
+ if (body.includes("(") || body.includes(")")) return false;
1740
+ return iscelldata(body);
1741
+ }
1742
+ function suppressFormulaRangeSelectionForInitialEdit(ctx) {
1743
+ ctx.formulaCache.rangeSelectionActive = false;
1744
+ ctx.formulaCache.keyboardRangeSelectionLock = true;
1745
+ ctx.formulaCache.rangestart = false;
1746
+ ctx.formulaCache.rangedrag_column_start = false;
1747
+ ctx.formulaCache.rangedrag_row_start = false;
1748
+ }
1749
+ function isCaretAtValidFormulaRangeInsertionPoint(editor) {
1750
+ var currSelection = window.getSelection();
1751
+ if (!editor || !currSelection || currSelection.rangeCount === 0) {
1752
+ return false;
1753
+ }
1754
+ var anchorNode = currSelection.anchorNode;
1755
+ if (anchorNode && !editor.contains(anchorNode)) {
1756
+ return false;
1757
+ }
1758
+ var inputText = editor.innerText.trim();
1759
+ if (!inputText.startsWith("=")) {
1760
+ return false;
1761
+ }
1762
+ if (/^=\s*[A-Za-z_][A-Za-z0-9_]*$/.test(inputText)) {
1763
+ return false;
1764
+ }
1765
+ if (isBareCellOrRangeOnlyFormula(inputText)) {
1766
+ return false;
1767
+ }
1768
+ var caretRange = currSelection.getRangeAt(0).cloneRange();
1769
+ var preCaretRange = document.createRange();
1770
+ preCaretRange.selectNodeContents(editor);
1771
+ preCaretRange.setEnd(caretRange.endContainer, caretRange.endOffset);
1772
+ var caretOffset = preCaretRange.toString().length;
1773
+ var slotTextBeforeCaret = getCurrentFormulaSlotTextBeforeCaret(editor, caretOffset);
1774
+ if (isCaretInsideIncompleteTruncatedRangeSyntax(editor, caretOffset)) {
1775
+ return false;
1776
+ }
1777
+ if (slotTextBeforeCaret.length > 0 && !iscelldata(slotTextBeforeCaret)) {
1778
+ return false;
1779
+ }
1780
+ var textAfter = editor.innerText.slice(caretOffset);
1781
+ var remaining = textAfter.replace(/^\s+/, "");
1782
+ if (remaining.length === 0) {
1783
+ var atCaret = getFormulaRangeIndexAtCaret(editor);
1784
+ if (atCaret !== null) {
1785
+ return true;
1786
+ }
1787
+ var textBefore = editor.innerText.slice(0, caretOffset).trimEnd();
1788
+ var lastCh = textBefore.slice(-1);
1789
+ if (!lastCh) {
1790
+ return false;
1791
+ }
1792
+ if (lastCh === ")") {
1793
+ return false;
1794
+ }
1795
+ if (/^[=,(+\-*/&%^<>]$/.test(lastCh)) {
1796
+ return true;
1797
+ }
1798
+ return false;
1799
+ }
1800
+ var first = remaining[0];
1801
+ return first === "," || first === ")" || first === "&" || first in operatorjson;
1802
+ }
1803
+ function hasCommaOrAnotherRefAfterRangeCell(cell) {
1804
+ var _a, _b;
1805
+ var n = cell.nextSibling;
1806
+ while (n) {
1807
+ if (n.nodeType === Node.ELEMENT_NODE) {
1808
+ var e = n;
1809
+ if ((_a = e.classList) === null || _a === void 0 ? void 0 : _a.contains("luckysheet-formula-text-comma")) return true;
1810
+ if ((_b = e.classList) === null || _b === void 0 ? void 0 : _b.contains("fortune-formula-functionrange-cell")) return true;
1811
+ }
1812
+ n = n.nextSibling;
1813
+ }
1814
+ return false;
1815
+ }
1816
+ function markRangeSelectionDirty(ctx) {
1817
+ ctx.formulaCache.rangeSelectionActive = false;
1818
+ ctx.formulaRangeHighlight = [];
1819
+ ctx.formulaRangeSelect = undefined;
1820
+ ctx.formulaCache.selectingRangeIndex = -1;
1821
+ ctx.formulaCache.func_selectedrange = undefined;
1822
+ ctx.formulaCache.rangestart = false;
1823
+ ctx.formulaCache.rangedrag_column_start = false;
1824
+ ctx.formulaCache.rangedrag_row_start = false;
1825
+ ctx.formulaCache.rangechangeindex = undefined;
1826
+ }
1827
+ function getFormulaRangeIndexForKeyboardSync($editor) {
1828
+ var atCaret = getFormulaRangeIndexAtCaret($editor);
1829
+ if (atCaret !== null) return atCaret;
1830
+ var lastIdx = getLastFormulaRangeIndex($editor);
1831
+ if (lastIdx === null) return null;
1832
+ var cell = $editor.querySelector("span.fortune-formula-functionrange-cell[rangeindex=\"".concat(lastIdx, "\"]"));
1833
+ if (!cell) return null;
1834
+ var sel = window.getSelection();
1835
+ if (!(sel === null || sel === void 0 ? void 0 : sel.anchorNode)) return lastIdx;
1836
+ var caretRange = document.createRange();
1837
+ try {
1838
+ caretRange.setStart(sel.anchorNode, sel.anchorOffset);
1839
+ caretRange.collapse(true);
1840
+ } catch (_a) {
1841
+ return lastIdx;
1842
+ }
1843
+ var cellRange = document.createRange();
1844
+ try {
1845
+ cellRange.selectNodeContents(cell);
1846
+ } catch (_b) {
1847
+ return lastIdx;
1848
+ }
1849
+ if (caretRange.compareBoundaryPoints(Range.START_TO_START, cellRange) < 0) {
1850
+ return null;
1851
+ }
1852
+ var afterCell = document.createRange();
1853
+ try {
1854
+ afterCell.setStartAfter(cell);
1855
+ afterCell.collapse(true);
1856
+ } catch (_c) {
1857
+ return lastIdx;
1858
+ }
1859
+ if (caretRange.compareBoundaryPoints(Range.START_TO_START, afterCell) >= 0) {
1860
+ if (hasCommaOrAnotherRefAfterRangeCell(cell)) {
1861
+ return null;
1862
+ }
1863
+ return lastIdx;
1864
+ }
1865
+ return lastIdx;
1866
+ }
1632
1867
  function handleFormulaInput(ctx, $copyTo, $editor, kcode, preText, refreshRangeSelect) {
1633
1868
  var _a, _b, _c, _d, _e, _f;
1634
1869
  if (refreshRangeSelect === void 0) {
@@ -1636,6 +1871,16 @@ function handleFormulaInput(ctx, $copyTo, $editor, kcode, preText, refreshRangeS
1636
1871
  }
1637
1872
  if (!$editor) return;
1638
1873
  try {
1874
+ if (ctx.formulaCache.keyboardRangeSelectionLock === true) {
1875
+ ctx.formulaCache.keyboardRangeSelectionLock = false;
1876
+ }
1877
+ var isBackspaceOrDelete = kcode === 8 || kcode === 46;
1878
+ var isAlphaNumeric = kcode >= 48 && kcode <= 57 || kcode >= 65 && kcode <= 90 || kcode >= 97 && kcode <= 122;
1879
+ if (ctx.formulaCache.rangeSelectionActive === true) {
1880
+ if (isBackspaceOrDelete || isAlphaNumeric) {
1881
+ markRangeSelectionDirty(ctx);
1882
+ }
1883
+ }
1639
1884
  var value1 = void 0;
1640
1885
  var value1txt = preText !== null && preText !== void 0 ? preText : $editor.innerText;
1641
1886
  var value = $editor.innerText;
@@ -1664,9 +1909,7 @@ function handleFormulaInput(ctx, $copyTo, $editor, kcode, preText, refreshRangeS
1664
1909
  functionRange(ctx, $editor, value, value1);
1665
1910
  if (refreshRangeSelect) {
1666
1911
  (0, _2.cancelFunctionrangeSelected)(ctx);
1667
- if (kcode !== 46) {
1668
- createRangeHightlight(ctx, value);
1669
- }
1912
+ createRangeHightlight(ctx, value);
1670
1913
  ctx.formulaCache.rangestart = false;
1671
1914
  ctx.formulaCache.rangedrag_column_start = false;
1672
1915
  ctx.formulaCache.rangedrag_row_start = false;
@@ -1866,61 +2109,190 @@ function functionStrChange_range(txt, type, rc, orient, stindex, step) {
1866
2109
  }
1867
2110
  return "";
1868
2111
  }
2112
+ function setRangeSetValueToFromCaretPosition(ctx, editor, sel) {
2113
+ if (sel.rangeCount === 0 || !sel.anchorNode) return false;
2114
+ if (!editor.contains(sel.anchorNode)) return false;
2115
+ var range = sel.getRangeAt(0).cloneRange();
2116
+ range.collapse(true);
2117
+ var startContainer = range.startContainer,
2118
+ startOffset = range.startOffset;
2119
+ if (startContainer.nodeType === Node.TEXT_NODE) {
2120
+ if (startOffset === 0) {
2121
+ var textParent = startContainer.parentElement;
2122
+ if (textParent === editor) {
2123
+ var prev = startContainer.previousSibling;
2124
+ if (prev) {
2125
+ ctx.formulaCache.rangeSetValueTo = prev;
2126
+ return true;
2127
+ }
2128
+ return false;
2129
+ }
2130
+ var el = textParent;
2131
+ while (el && el !== editor) {
2132
+ if (el.previousSibling) {
2133
+ ctx.formulaCache.rangeSetValueTo = el.previousSibling;
2134
+ return true;
2135
+ }
2136
+ el = el.parentElement;
2137
+ }
2138
+ return false;
2139
+ }
2140
+ var p = startContainer.parentElement;
2141
+ if (p && editor.contains(p)) {
2142
+ ctx.formulaCache.rangeSetValueTo = p;
2143
+ return true;
2144
+ }
2145
+ return false;
2146
+ }
2147
+ if (startContainer.nodeType === Node.ELEMENT_NODE && startContainer === editor) {
2148
+ if (startOffset > 0) {
2149
+ var prev = startContainer.childNodes[startOffset - 1];
2150
+ if (prev) {
2151
+ ctx.formulaCache.rangeSetValueTo = prev;
2152
+ return true;
2153
+ }
2154
+ }
2155
+ return false;
2156
+ }
2157
+ if (startContainer.nodeType === Node.ELEMENT_NODE) {
2158
+ if (startOffset > 0) {
2159
+ var prev = startContainer.childNodes[startOffset - 1];
2160
+ if (prev) {
2161
+ ctx.formulaCache.rangeSetValueTo = prev;
2162
+ return true;
2163
+ }
2164
+ }
2165
+ var el = startContainer;
2166
+ while (el && el !== editor) {
2167
+ if (el.previousSibling) {
2168
+ ctx.formulaCache.rangeSetValueTo = el.previousSibling;
2169
+ return true;
2170
+ }
2171
+ el = el.parentElement;
2172
+ }
2173
+ }
2174
+ return false;
2175
+ }
1869
2176
  function israngeseleciton(ctx, istooltip) {
1870
- var _a, _b, _c;
2177
+ var _a, _b, _c, _d;
1871
2178
  if (istooltip == null) {
1872
2179
  istooltip = false;
1873
2180
  }
1874
2181
  var currSelection = window.getSelection();
1875
- if (currSelection == null) return false;
2182
+ if (currSelection == null) {
2183
+ return false;
2184
+ }
1876
2185
  var anchor = currSelection.anchorNode;
1877
- if (!(anchor === null || anchor === void 0 ? void 0 : anchor.textContent)) return false;
2186
+ if (!anchor) {
2187
+ return false;
2188
+ }
1878
2189
  var anchorOffset = currSelection.anchorOffset;
1879
2190
  var anchorElement = anchor;
1880
2191
  var parentElement = anchor.parentNode;
2192
+ var allowRangeInsertionAtCaret = function allowRangeInsertionAtCaret() {
2193
+ var _a, _b;
2194
+ if (ctx.formulaCache.rangestart || ctx.formulaCache.rangedrag_column_start || ctx.formulaCache.rangedrag_row_start || ctx.formulaCache.rangeSelectionActive === true) {
2195
+ return true;
2196
+ }
2197
+ var editor = ((_a = anchorElement.closest) === null || _a === void 0 ? void 0 : _a.call(anchorElement, "#luckysheet-rich-text-editor, #luckysheet-functionbox-cell")) || ((_b = parentElement.closest) === null || _b === void 0 ? void 0 : _b.call(parentElement, "#luckysheet-rich-text-editor, #luckysheet-functionbox-cell")) || document.getElementById("luckysheet-rich-text-editor");
2198
+ return isCaretAtValidFormulaRangeInsertionPoint(editor);
2199
+ };
1881
2200
  if (((_a = anchor === null || anchor === void 0 ? void 0 : anchor.parentNode) === null || _a === void 0 ? void 0 : _a.nodeName.toLowerCase()) === "span" && anchorOffset !== 0) {
1882
- var txt = _lodash.default.trim(anchor.textContent);
2201
+ var txt = _lodash.default.trim((_b = anchor.textContent) !== null && _b !== void 0 ? _b : "");
1883
2202
  var lasttxt = "";
1884
2203
  if (txt.length === 0 && anchor.parentNode.previousSibling) {
1885
2204
  var ahr = anchor.parentNode.previousSibling;
1886
2205
  txt = _lodash.default.trim(ahr.textContent || "");
1887
- lasttxt = txt.substring(txt.length - 1, 1);
1888
- ctx.formulaCache.rangeSetValueTo = anchor.parentNode;
2206
+ lasttxt = txt.slice(-1);
1889
2207
  } else {
1890
- lasttxt = txt.substring(anchorOffset - 1, 1);
1891
- ctx.formulaCache.rangeSetValueTo = anchor.parentNode;
2208
+ lasttxt = anchorOffset > 0 ? txt.charAt(anchorOffset - 1) : "";
1892
2209
  }
1893
2210
  if (istooltip && (lasttxt === "(" || lasttxt === ",") || !istooltip && (lasttxt === "(" || lasttxt === "," || lasttxt === "=" || lasttxt in operatorjson || lasttxt === "&")) {
1894
- return true;
2211
+ ctx.formulaCache.rangeSetValueTo = anchor.parentNode;
2212
+ return allowRangeInsertionAtCaret();
1895
2213
  }
1896
2214
  } else if (anchorElement.id === "luckysheet-rich-text-editor" || anchorElement.id === "luckysheet-functionbox-cell") {
1897
- var txt = _lodash.default.trim((_b = _lodash.default.last(anchorElement.querySelectorAll("span"))) === null || _b === void 0 ? void 0 : _b.innerText);
1898
- ctx.formulaCache.rangeSetValueTo = _lodash.default.last(anchorElement.querySelectorAll("span"));
1899
- if (txt.length === 0 && anchorElement.querySelectorAll("span").length > 1) {
1900
- var ahr = anchorElement.querySelectorAll("span");
1901
- txt = _lodash.default.trim(ahr[ahr.length - 2].innerText);
1902
- txt = _lodash.default.trim(ahr[ahr.length - 2].innerText);
1903
- ctx.formulaCache.rangeSetValueTo = ahr;
2215
+ var editorEl = anchorElement;
2216
+ if (currSelection.rangeCount > 0 && setRangeSetValueToFromCaretPosition(ctx, editorEl, currSelection) && allowRangeInsertionAtCaret()) {
2217
+ return true;
2218
+ }
2219
+ var spans = editorEl.querySelectorAll("span");
2220
+ var txt = _lodash.default.trim((_c = _lodash.default.last(spans)) === null || _c === void 0 ? void 0 : _c.innerText);
2221
+ var refSpan = _lodash.default.last(spans);
2222
+ if (txt.length === 0 && spans.length > 1) {
2223
+ txt = _lodash.default.trim(spans[spans.length - 2].innerText);
2224
+ refSpan = spans[spans.length - 2];
1904
2225
  }
1905
- var lasttxt = txt.substring(txt.length - 1, 1);
2226
+ var lasttxt = txt.slice(-1);
1906
2227
  if (istooltip && (lasttxt === "(" || lasttxt === ",") || !istooltip && (lasttxt === "(" || lasttxt === "," || lasttxt === "=" || lasttxt in operatorjson || lasttxt === "&")) {
1907
- return true;
2228
+ ctx.formulaCache.rangeSetValueTo = refSpan;
2229
+ return allowRangeInsertionAtCaret();
1908
2230
  }
1909
2231
  } else if (parentElement.id === "luckysheet-rich-text-editor" || parentElement.id === "luckysheet-functionbox-cell" || anchorOffset === 0) {
1910
2232
  if (anchorOffset === 0) {
1911
2233
  anchor = anchor.parentNode;
1912
2234
  }
1913
- if (!anchor) return false;
1914
- if (((_c = anchor.previousSibling) === null || _c === void 0 ? void 0 : _c.textContent) == null) return false;
2235
+ if (!anchor) {
2236
+ return false;
2237
+ }
2238
+ if (((_d = anchor.previousSibling) === null || _d === void 0 ? void 0 : _d.textContent) == null) {
2239
+ return false;
2240
+ }
1915
2241
  if (anchor.previousSibling) {
1916
2242
  var txt = _lodash.default.trim(anchor.previousSibling.textContent);
1917
- var lasttxt = txt.substring(txt.length - 1, 1);
1918
- ctx.formulaCache.rangeSetValueTo = anchor.previousSibling;
2243
+ var lasttxt = txt.slice(-1);
1919
2244
  if (istooltip && (lasttxt === "(" || lasttxt === ",") || !istooltip && (lasttxt === "(" || lasttxt === "," || lasttxt === "=" || lasttxt in operatorjson || lasttxt === "&")) {
1920
- return true;
2245
+ ctx.formulaCache.rangeSetValueTo = anchor.previousSibling;
2246
+ return allowRangeInsertionAtCaret();
1921
2247
  }
1922
2248
  }
1923
2249
  }
2250
+ if (!istooltip && (ctx.formulaCache.rangestart || ctx.formulaCache.rangedrag_column_start || ctx.formulaCache.rangedrag_row_start || ctx.formulaCache.rangeSelectionActive === true)) {
2251
+ var editor = document.getElementById("luckysheet-rich-text-editor") || document.getElementById("luckysheet-functionbox-cell");
2252
+ if (editor && currSelection.rangeCount > 0 && setRangeSetValueToFromCaretPosition(ctx, editor, currSelection) && allowRangeInsertionAtCaret()) {
2253
+ return true;
2254
+ }
2255
+ }
2256
+ return false;
2257
+ }
2258
+ function isFormulaReferenceInputMode(ctx) {
2259
+ var editor = getActiveFormulaEditorElement(ctx);
2260
+ var inputText = ((editor === null || editor === void 0 ? void 0 : editor.innerText) || "").trim();
2261
+ var refFlowActive = !!ctx.formulaCache.rangestart || !!ctx.formulaCache.rangedrag_column_start || !!ctx.formulaCache.rangedrag_row_start || ctx.formulaCache.rangeSelectionActive === true;
2262
+ if (refFlowActive) {
2263
+ return true;
2264
+ }
2265
+ if (!inputText.startsWith("=")) {
2266
+ return false;
2267
+ }
2268
+ if (/^=\s*[A-Za-z_][A-Za-z0-9_]*$/.test(inputText)) {
2269
+ return false;
2270
+ }
2271
+ if (editor && isCaretAtValidFormulaRangeInsertionPoint(editor)) {
2272
+ if (ctx.formulaCache.rangeSelectionActive === false) {
2273
+ ctx.formulaCache.rangeSelectionActive = null;
2274
+ }
2275
+ return true;
2276
+ }
2277
+ if (ctx.formulaCache.rangeSelectionActive === false) {
2278
+ return false;
2279
+ }
2280
+ return israngeseleciton(ctx);
2281
+ }
2282
+ function maybeRecoverDirtyRangeSelection(ctx) {
2283
+ if (ctx.formulaCache.rangeSelectionActive !== false) {
2284
+ return false;
2285
+ }
2286
+ var editor = getActiveFormulaEditorElement(ctx);
2287
+ if (!editor) {
2288
+ return false;
2289
+ }
2290
+ var inputText = (editor.innerText || "").trim();
2291
+ var atCaretRangeIndex = getFormulaRangeIndexAtCaret(editor);
2292
+ if (inputText.startsWith("=") && atCaretRangeIndex === null && isCaretAtValidFormulaRangeInsertionPoint(editor) && israngeseleciton(ctx)) {
2293
+ ctx.formulaCache.rangeSelectionActive = null;
2294
+ return true;
2295
+ }
1924
2296
  return false;
1925
2297
  }
1926
2298
  function functionStrChange(txt, type, rc, orient, stindex, step) {
@@ -2018,18 +2390,22 @@ function functionStrChange(txt, type, rc, orient, stindex, step) {
2018
2390
  return function_str;
2019
2391
  }
2020
2392
  function rangeSetValue(ctx, cellInput, selected, fxInput) {
2021
- var _a, _b, _c, _d, _e;
2393
+ var _a, _b, _c, _d, _e, _f;
2022
2394
  var parser = new DOMParser();
2023
2395
  var doc = parser.parseFromString("<div>".concat(cellInput.innerHTML, "</div>"), "text/html");
2024
2396
  var spans = doc.querySelectorAll("span");
2025
2397
  var lastSpan = spans[spans.length - 1];
2026
- if (lastSpan && (0, _index.isLetterNumberPattern)(lastSpan === null || lastSpan === void 0 ? void 0 : lastSpan.innerText)) {
2027
- var htmlR = (0, _index.removeLastSpan)(cellInput.innerHTML);
2398
+ var isManagedRangeSpan = (_b = (_a = lastSpan === null || lastSpan === void 0 ? void 0 : lastSpan.classList) === null || _a === void 0 ? void 0 : _a.contains("fortune-formula-functionrange-cell")) !== null && _b !== void 0 ? _b : false;
2399
+ if (lastSpan && (0, _utils.isLetterNumberPattern)(lastSpan === null || lastSpan === void 0 ? void 0 : lastSpan.innerText) && !isManagedRangeSpan) {
2400
+ var htmlR = (0, _utils.removeLastSpan)(cellInput.innerHTML);
2028
2401
  cellInput.innerHTML = "".concat(htmlR);
2402
+ cellInput.focus();
2403
+ var kids = cellInput.childNodes;
2404
+ ctx.formulaCache.rangeSetValueTo = kids.length > 0 ? kids[kids.length - 1] : undefined;
2029
2405
  }
2030
2406
  var $editor = cellInput;
2031
2407
  var $copyTo = fxInput;
2032
- if (((_a = document.activeElement) === null || _a === void 0 ? void 0 : _a.id) === "luckysheet-functionbox-cell") {
2408
+ if (getFormulaEditorOwner(ctx) === "fx") {
2033
2409
  $editor = fxInput;
2034
2410
  $copyTo = cellInput;
2035
2411
  }
@@ -2044,20 +2420,23 @@ function rangeSetValue(ctx, cellInput, selected, fxInput) {
2044
2420
  } else {
2045
2421
  range = (0, _cell.getRangetxt)(ctx, ctx.currentSheetId, selected, ctx.formulaCache.rangetosheet);
2046
2422
  }
2047
- if (!israngeseleciton(ctx) && (ctx.formulaCache.rangestart || ctx.formulaCache.rangedrag_column_start || ctx.formulaCache.rangedrag_row_start)) {
2048
- var span = $editor.querySelector("span[rangeindex='".concat(ctx.formulaCache.rangechangeindex, "']"));
2049
- if (span) {
2050
- span.innerHTML = range;
2051
- setCaretPosition(ctx, span, 0, range.length);
2052
- }
2423
+ var activeRangeFlow = ctx.formulaCache.rangestart || ctx.formulaCache.rangedrag_column_start || ctx.formulaCache.rangedrag_row_start || ctx.formulaCache.rangeSelectionActive === true;
2424
+ var spanToReplace = !_lodash.default.isNil(ctx.formulaCache.rangechangeindex) ? $editor.querySelector("span[rangeindex='".concat(ctx.formulaCache.rangechangeindex, "']")) : null;
2425
+ if (activeRangeFlow && spanToReplace) {
2426
+ spanToReplace.innerHTML = range;
2427
+ setCaretPosition(ctx, spanToReplace, 0, range.length);
2053
2428
  } else {
2054
2429
  var function_str = "<span class=\"fortune-formula-functionrange-cell\" rangeindex=\"".concat(functionHTMLIndex, "\" dir=\"auto\" style=\"color:").concat(_color.colors[functionHTMLIndex], ";\">").concat(range, "</span>");
2055
2430
  var newEle = parseElement(function_str);
2056
2431
  var refEle = ctx.formulaCache.rangeSetValueTo;
2432
+ if (refEle && !refEle.parentNode) {
2433
+ israngeseleciton(ctx);
2434
+ refEle = ctx.formulaCache.rangeSetValueTo;
2435
+ }
2057
2436
  if (refEle && refEle.parentNode) {
2058
- var leftPar = (_b = document.getElementsByClassName("luckysheet-formula-text-lpar")) === null || _b === void 0 ? void 0 : _b[0];
2059
- if ((_c = leftPar === null || leftPar === void 0 ? void 0 : leftPar.parentElement) === null || _c === void 0 ? void 0 : _c.classList.contains("luckysheet-formula-text-color")) {
2060
- (_e = (_d = document.getElementsByClassName("luckysheet-formula-text-lpar")) === null || _d === void 0 ? void 0 : _d[0].parentNode) === null || _e === void 0 ? void 0 : _e.appendChild(newEle);
2437
+ var leftPar = (_c = document.getElementsByClassName("luckysheet-formula-text-lpar")) === null || _c === void 0 ? void 0 : _c[0];
2438
+ if ((_d = leftPar === null || leftPar === void 0 ? void 0 : leftPar.parentElement) === null || _d === void 0 ? void 0 : _d.classList.contains("luckysheet-formula-text-color")) {
2439
+ (_f = (_e = document.getElementsByClassName("luckysheet-formula-text-lpar")) === null || _e === void 0 ? void 0 : _e[0].parentNode) === null || _f === void 0 ? void 0 : _f.appendChild(newEle);
2061
2440
  } else {
2062
2441
  refEle.parentNode.insertBefore(newEle, refEle.nextSibling);
2063
2442
  }
@@ -2097,6 +2476,7 @@ function setRangeSelect(container, left, top, height, width) {
2097
2476
  rangeElement.style.width = "".concat(width, "px");
2098
2477
  }
2099
2478
  function rangeDrag(ctx, e, cellInput, scrollLeft, scrollTop, container, fxInput) {
2479
+ ctx.formulaCache.rangeSelectionActive = true;
2100
2480
  var func_selectedrange = ctx.formulaCache.func_selectedrange;
2101
2481
  if (!func_selectedrange || func_selectedrange.left == null || func_selectedrange.height == null || func_selectedrange.top == null || func_selectedrange.width == null) return;
2102
2482
  var rect = container.getBoundingClientRect();
@@ -2152,6 +2532,20 @@ function rangeDrag(ctx, e, cellInput, scrollLeft, scrollTop, container, fxInput)
2152
2532
  func_selectedrange.width_move = width;
2153
2533
  func_selectedrange.top_move = top;
2154
2534
  func_selectedrange.height_move = height;
2535
+ ctx.luckysheet_select_save = [{
2536
+ row: [rowseleted[0], rowseleted[1]],
2537
+ column: [columnseleted[0], columnseleted[1]],
2538
+ row_focus: row_index,
2539
+ column_focus: col_index,
2540
+ left: left,
2541
+ top: top,
2542
+ width: width,
2543
+ height: height,
2544
+ left_move: left,
2545
+ top_move: top,
2546
+ width_move: width,
2547
+ height_move: height
2548
+ }];
2155
2549
  rangeSetValue(ctx, cellInput, {
2156
2550
  row: rowseleted,
2157
2551
  column: columnseleted
@@ -2160,6 +2554,7 @@ function rangeDrag(ctx, e, cellInput, scrollLeft, scrollTop, container, fxInput)
2160
2554
  e.preventDefault();
2161
2555
  }
2162
2556
  function rangeDragColumn(ctx, e, cellInput, scrollLeft, scrollTop, container, fxInput) {
2557
+ ctx.formulaCache.rangeSelectionActive = true;
2163
2558
  var func_selectedrange = ctx.formulaCache.func_selectedrange;
2164
2559
  if (!func_selectedrange || func_selectedrange.left == null || func_selectedrange.height == null || func_selectedrange.top == null || func_selectedrange.width == null) return;
2165
2560
  var mouse = (0, _location.mousePosition)(e.pageX, e.pageY, ctx);
@@ -2195,6 +2590,20 @@ function rangeDragColumn(ctx, e, cellInput, scrollLeft, scrollTop, container, fx
2195
2590
  func_selectedrange.column = columnseleted;
2196
2591
  func_selectedrange.left_move = left;
2197
2592
  func_selectedrange.width_move = width;
2593
+ ctx.luckysheet_select_save = [{
2594
+ row: [0, row_index],
2595
+ column: [columnseleted[0], columnseleted[1]],
2596
+ row_focus: 0,
2597
+ column_focus: col_index,
2598
+ left: left,
2599
+ top: row_pre,
2600
+ width: width,
2601
+ height: row - row_pre - 1,
2602
+ left_move: left,
2603
+ top_move: row_pre,
2604
+ width_move: width,
2605
+ height_move: row - row_pre - 1
2606
+ }];
2198
2607
  rangeSetValue(ctx, cellInput, {
2199
2608
  row: [null, null],
2200
2609
  column: columnseleted
@@ -2202,6 +2611,7 @@ function rangeDragColumn(ctx, e, cellInput, scrollLeft, scrollTop, container, fx
2202
2611
  setRangeSelect(container, left, row_pre, row - row_pre - 1, width);
2203
2612
  }
2204
2613
  function rangeDragRow(ctx, e, cellInput, scrollLeft, scrollTop, container, fxInput) {
2614
+ ctx.formulaCache.rangeSelectionActive = true;
2205
2615
  var func_selectedrange = ctx.formulaCache.func_selectedrange;
2206
2616
  if (!func_selectedrange || func_selectedrange.left == null || func_selectedrange.height == null || func_selectedrange.top == null || func_selectedrange.width == null) return;
2207
2617
  var mouse = (0, _location.mousePosition)(e.pageX, e.pageY, ctx);
@@ -2237,6 +2647,20 @@ function rangeDragRow(ctx, e, cellInput, scrollLeft, scrollTop, container, fxInp
2237
2647
  func_selectedrange.row = rowseleted;
2238
2648
  func_selectedrange.top_move = top;
2239
2649
  func_selectedrange.height_move = height;
2650
+ ctx.luckysheet_select_save = [{
2651
+ row: [rowseleted[0], rowseleted[1]],
2652
+ column: [0, col_index],
2653
+ row_focus: row_index,
2654
+ column_focus: 0,
2655
+ left: col_pre,
2656
+ top: top,
2657
+ width: col - col_pre - 1,
2658
+ height: height,
2659
+ left_move: col_pre,
2660
+ top_move: top,
2661
+ width_move: col - col_pre - 1,
2662
+ height_move: height
2663
+ }];
2240
2664
  rangeSetValue(ctx, cellInput, {
2241
2665
  row: rowseleted,
2242
2666
  column: [null, null]
@@ -2438,7 +2862,7 @@ function functionCopy(ctx, txt, mode, step) {
2438
2862
  str += s;
2439
2863
  }
2440
2864
  if (i === funcstack.length - 1) {
2441
- if (iscelldata(_lodash.default.trim(str)) && !_lodash.default.trim(str).includes("$")) {
2865
+ if (iscelldata(_lodash.default.trim(str))) {
2442
2866
  if (mode === "down") {
2443
2867
  function_str += downparam(_lodash.default.trim(str), step);
2444
2868
  } else if (mode === "up") {
@@ -2456,4 +2880,152 @@ function functionCopy(ctx, txt, mode, step) {
2456
2880
  }
2457
2881
  function_str = function_str.replace(/NaN/g, "");
2458
2882
  return function_str;
2883
+ }
2884
+ function normalizeSheetName(ref) {
2885
+ var unquoted = ref.startsWith("'") && ref.endsWith("'") ? ref.slice(1, -1).replace(/''/g, "'") : ref;
2886
+ return unquoted;
2887
+ }
2888
+ function parseRefToken(token) {
2889
+ var m = token.match(/^(\$?)([A-Za-z]+)(\$?)(\d+)$/);
2890
+ if (!m) return null;
2891
+ var colAbs = m[1] === "$";
2892
+ var col = (0, _utils.columnCharToIndex)(m[2]);
2893
+ var rowAbs = m[3] === "$";
2894
+ var row = parseInt(m[4], 10) - 1;
2895
+ if (Number.isNaN(row) || Number.isNaN(col)) return null;
2896
+ return {
2897
+ colAbs: colAbs,
2898
+ rowAbs: rowAbs,
2899
+ col: col,
2900
+ row: row
2901
+ };
2902
+ }
2903
+ function formatRefToken(parts) {
2904
+ return "".concat(parts.colAbs ? "$" : "").concat((0, _utils.indexToColumnChar)(parts.col)).concat(parts.rowAbs ? "$" : "").concat(parts.row + 1);
2905
+ }
2906
+ function moveSingleRefToken(token, sourceRect, targetRowStart, targetColStart) {
2907
+ var parsed = parseRefToken(token);
2908
+ if (!parsed) return token;
2909
+ var inSourceRect = parsed.row >= sourceRect.rowStart && parsed.row <= sourceRect.rowEnd && parsed.col >= sourceRect.colStart && parsed.col <= sourceRect.colEnd;
2910
+ if (!inSourceRect) return token;
2911
+ return formatRefToken(__assign(__assign({}, parsed), {
2912
+ row: targetRowStart + (parsed.row - sourceRect.rowStart),
2913
+ col: targetColStart + (parsed.col - sourceRect.colStart)
2914
+ }));
2915
+ }
2916
+ function moveRangeRefToken(token, sourceRect, targetRowStart, targetColStart) {
2917
+ var parts = token.split(":");
2918
+ if (parts.length === 1) {
2919
+ return moveSingleRefToken(token, sourceRect, targetRowStart, targetColStart);
2920
+ }
2921
+ if (parts.length !== 2) return token;
2922
+ var left = moveSingleRefToken(parts[0], sourceRect, targetRowStart, targetColStart);
2923
+ var right = moveSingleRefToken(parts[1], sourceRect, targetRowStart, targetColStart);
2924
+ return "".concat(left, ":").concat(right);
2925
+ }
2926
+ function moveFormulaReferenceToken(token, formulaSheetName, movedSheetName, sourceRect, targetRowStart, targetColStart) {
2927
+ var exclamation = token.lastIndexOf("!");
2928
+ var sheetPrefix = "";
2929
+ var rangeToken = token;
2930
+ var refSheetName = formulaSheetName;
2931
+ if (exclamation > -1) {
2932
+ sheetPrefix = token.slice(0, exclamation + 1);
2933
+ rangeToken = token.slice(exclamation + 1);
2934
+ refSheetName = normalizeSheetName(sheetPrefix.slice(0, -1));
2935
+ }
2936
+ if (refSheetName !== movedSheetName) return token;
2937
+ var moved = moveRangeRefToken(rangeToken, sourceRect, targetRowStart, targetColStart);
2938
+ return "".concat(sheetPrefix).concat(moved);
2939
+ }
2940
+ function functionMoveReference(txt, formulaSheetName, movedSheetName, sourceRect, targetRowStart, targetColStart) {
2941
+ if (!txt) {
2942
+ return "";
2943
+ }
2944
+ if (txt.substring(0, 1) === "=") {
2945
+ txt = txt.substring(1);
2946
+ }
2947
+ var funcstack = txt.split("");
2948
+ var i = 0;
2949
+ var str = "";
2950
+ var function_str = "";
2951
+ var matchConfig = {
2952
+ bracket: 0,
2953
+ comma: 0,
2954
+ squote: 0,
2955
+ dquote: 0
2956
+ };
2957
+ while (i < funcstack.length) {
2958
+ var s = funcstack[i];
2959
+ if (s === "(" && matchConfig.dquote === 0) {
2960
+ matchConfig.bracket += 1;
2961
+ function_str += str.length > 0 ? "".concat(str, "(") : "(";
2962
+ str = "";
2963
+ } else if (s === ")" && matchConfig.dquote === 0) {
2964
+ matchConfig.bracket -= 1;
2965
+ function_str += "".concat(functionMoveReference(str, formulaSheetName, movedSheetName, sourceRect, targetRowStart, targetColStart), ")");
2966
+ str = "";
2967
+ } else if (s === '"' && matchConfig.squote === 0) {
2968
+ if (matchConfig.dquote > 0) {
2969
+ function_str += "".concat(str, "\"");
2970
+ matchConfig.dquote -= 1;
2971
+ str = "";
2972
+ } else {
2973
+ matchConfig.dquote += 1;
2974
+ str += '"';
2975
+ }
2976
+ } else if (s === "," && matchConfig.dquote === 0) {
2977
+ function_str += "".concat(functionMoveReference(str, formulaSheetName, movedSheetName, sourceRect, targetRowStart, targetColStart), ",");
2978
+ str = "";
2979
+ } else if (s === "&" && matchConfig.dquote === 0) {
2980
+ if (str.length > 0) {
2981
+ function_str += "".concat(functionMoveReference(str, formulaSheetName, movedSheetName, sourceRect, targetRowStart, targetColStart), "&");
2982
+ str = "";
2983
+ } else {
2984
+ function_str += "&";
2985
+ }
2986
+ } else if (s in operatorjson && matchConfig.dquote === 0) {
2987
+ var s_next = "";
2988
+ if (i + 1 < funcstack.length) {
2989
+ s_next = funcstack[i + 1];
2990
+ }
2991
+ var p = i - 1;
2992
+ var s_pre = null;
2993
+ if (p >= 0) {
2994
+ do {
2995
+ s_pre = funcstack[p];
2996
+ p -= 1;
2997
+ } while (p >= 0 && s_pre === " ");
2998
+ }
2999
+ if (s + s_next in operatorjson) {
3000
+ if (str.length > 0) {
3001
+ function_str += functionMoveReference(str, formulaSheetName, movedSheetName, sourceRect, targetRowStart, targetColStart) + s + s_next;
3002
+ str = "";
3003
+ } else {
3004
+ function_str += s + s_next;
3005
+ }
3006
+ i += 1;
3007
+ } else if (!/[^0-9]/.test(s_next) && s === "-" && (s_pre === "(" || s_pre == null || s_pre === "," || s_pre === " " || s_pre in operatorjson)) {
3008
+ str += s;
3009
+ } else {
3010
+ if (str.length > 0) {
3011
+ function_str += functionMoveReference(str, formulaSheetName, movedSheetName, sourceRect, targetRowStart, targetColStart) + s;
3012
+ str = "";
3013
+ } else {
3014
+ function_str += s;
3015
+ }
3016
+ }
3017
+ } else {
3018
+ str += s;
3019
+ }
3020
+ if (i === funcstack.length - 1) {
3021
+ var t = _lodash.default.trim(str);
3022
+ if (iscelldata(t)) {
3023
+ function_str += moveFormulaReferenceToken(t, formulaSheetName, movedSheetName, sourceRect, targetRowStart, targetColStart);
3024
+ } else {
3025
+ function_str += t;
3026
+ }
3027
+ }
3028
+ i += 1;
3029
+ }
3030
+ return function_str;
2459
3031
  }