@fileverse-dev/fortune-core 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.
Files changed (43) hide show
  1. package/es/api/range.js +20 -0
  2. package/es/api/sheet.js +30 -2
  3. package/es/events/keyboard.js +50 -15
  4. package/es/events/mouse.js +36 -20
  5. package/es/events/paste.js +77 -28
  6. package/es/modules/cell.js +59 -2
  7. package/es/modules/comment.js +129 -24
  8. package/es/modules/dataVerification.js +34 -1
  9. package/es/modules/dropCell.js +65 -1
  10. package/es/modules/formula.d.ts +7 -0
  11. package/es/modules/formula.js +356 -45
  12. package/es/modules/hyperlink.js +52 -5
  13. package/es/modules/merge.js +93 -1
  14. package/es/modules/moveCells.js +35 -9
  15. package/es/modules/rowcol.js +75 -2
  16. package/es/modules/searchReplace.js +58 -2
  17. package/es/modules/selection.js +152 -42
  18. package/es/modules/sort.js +74 -9
  19. package/es/modules/splitColumn.js +21 -0
  20. package/es/modules/toolbar.js +46 -3
  21. package/es/settings.d.ts +5 -0
  22. package/lib/api/range.js +20 -0
  23. package/lib/api/sheet.js +29 -1
  24. package/lib/events/keyboard.js +49 -14
  25. package/lib/events/mouse.js +35 -19
  26. package/lib/events/paste.js +77 -28
  27. package/lib/modules/cell.js +59 -2
  28. package/lib/modules/comment.js +129 -24
  29. package/lib/modules/dataVerification.js +34 -1
  30. package/lib/modules/dropCell.js +65 -1
  31. package/lib/modules/formula.d.ts +7 -0
  32. package/lib/modules/formula.js +362 -45
  33. package/lib/modules/hyperlink.js +52 -5
  34. package/lib/modules/merge.js +93 -1
  35. package/lib/modules/moveCells.js +35 -9
  36. package/lib/modules/rowcol.js +75 -2
  37. package/lib/modules/searchReplace.js +58 -2
  38. package/lib/modules/selection.js +152 -42
  39. package/lib/modules/sort.js +74 -9
  40. package/lib/modules/splitColumn.js +21 -0
  41. package/lib/modules/toolbar.js +46 -3
  42. package/lib/settings.d.ts +5 -0
  43. package/package.json +1 -1
@@ -13,13 +13,19 @@ exports.functionCopy = functionCopy;
13
13
  exports.functionHTMLGenerate = functionHTMLGenerate;
14
14
  exports.functionStrChange = functionStrChange;
15
15
  exports.getAllFunctionGroup = getAllFunctionGroup;
16
+ exports.getFormulaRangeIndexAtCaret = getFormulaRangeIndexAtCaret;
17
+ exports.getFormulaRangeIndexForKeyboardSync = getFormulaRangeIndexForKeyboardSync;
18
+ exports.getLastFormulaRangeIndex = getLastFormulaRangeIndex;
16
19
  exports.getcellrange = getcellrange;
17
20
  exports.getrangeseleciton = getrangeseleciton;
18
21
  exports.groupValuesRefresh = groupValuesRefresh;
19
22
  exports.handleFormulaInput = handleFormulaInput;
20
23
  exports.insertUpdateFunctionGroup = insertUpdateFunctionGroup;
24
+ exports.isFormulaReferenceInputMode = isFormulaReferenceInputMode;
21
25
  exports.iscelldata = iscelldata;
22
26
  exports.israngeseleciton = israngeseleciton;
27
+ exports.markRangeSelectionDirty = markRangeSelectionDirty;
28
+ exports.maybeRecoverDirtyRangeSelection = maybeRecoverDirtyRangeSelection;
23
29
  exports.moveCursorToEnd = moveCursorToEnd;
24
30
  exports.onFormulaRangeDragEnd = onFormulaRangeDragEnd;
25
31
  exports.rangeDrag = rangeDrag;
@@ -86,6 +92,7 @@ var FormulaCache = exports.FormulaCache = function () {
86
92
  var that = this;
87
93
  this.data_parm_index = 0;
88
94
  this.selectingRangeIndex = -1;
95
+ this.rangeSelectionActive = null;
89
96
  this.functionlistMap = {};
90
97
  this.execFunctionGlobalData = {};
91
98
  this.cellTextToIndexList = {};
@@ -815,6 +822,7 @@ function insertUpdateDynamicArray(ctx, dynamicArrayItem) {
815
822
  return dynamicArray;
816
823
  }
817
824
  function groupValuesRefresh(ctx) {
825
+ var _a, _b, _c;
818
826
  var luckysheetfile = ctx.luckysheetfile;
819
827
  if (ctx.groupValuesRefreshData.length > 0) {
820
828
  for (var i = 0; i < ctx.groupValuesRefreshData.length; i += 1) {
@@ -837,6 +845,19 @@ function groupValuesRefresh(ctx) {
837
845
  updateValue.v = item.v;
838
846
  updateValue.f = item.f;
839
847
  (0, _cell.setCellValue)(ctx, item.r, item.c, data, updateValue);
848
+ if ((_a = ctx === null || ctx === void 0 ? void 0 : ctx.hooks) === null || _a === void 0 ? void 0 : _a.updateCellYdoc) {
849
+ ctx.hooks.updateCellYdoc([{
850
+ sheetId: item.id,
851
+ path: ["celldata"],
852
+ value: {
853
+ r: item.r,
854
+ c: item.c,
855
+ v: (_c = (_b = data === null || data === void 0 ? void 0 : data[item.r]) === null || _b === void 0 ? void 0 : _b[item.c]) !== null && _c !== void 0 ? _c : null
856
+ },
857
+ key: "".concat(item.r, "_").concat(item.c),
858
+ type: "update"
859
+ }]);
860
+ }
840
861
  }
841
862
  ctx.groupValuesRefreshData = [];
842
863
  }
@@ -1302,8 +1323,7 @@ function setCaretPosition(ctx, textDom, children, pos, parentTextDom) {
1302
1323
  sel === null || sel === void 0 ? void 0 : sel.removeAllRanges();
1303
1324
  sel === null || sel === void 0 ? void 0 : sel.addRange(range);
1304
1325
  el.focus();
1305
- } catch (err) {
1306
- console.error(err);
1326
+ } catch (_c) {
1307
1327
  moveCursorToEnd(parentTextDom);
1308
1328
  }
1309
1329
  }
@@ -1388,7 +1408,7 @@ function getrangeseleciton() {
1388
1408
  return null;
1389
1409
  }
1390
1410
  function helpFunctionExe($editer, currSelection, ctx) {
1391
- var _a;
1411
+ var _a, _b, _c, _d, _e, _f;
1392
1412
  var functionlist = (0, _locale.locale)(ctx).functionlist;
1393
1413
  if (_lodash.default.isEmpty(ctx.formulaCache.functionlistMap)) {
1394
1414
  for (var i_1 = 0; i_1 < functionlist.length; i_1 += 1) {
@@ -1399,22 +1419,23 @@ function helpFunctionExe($editer, currSelection, ctx) {
1399
1419
  return null;
1400
1420
  }
1401
1421
  var $prev = currSelection;
1402
- var $span = $editer.querySelectorAll("span");
1403
- var currentIndex = _lodash.default.indexOf((_a = currSelection.parentNode) === null || _a === void 0 ? void 0 : _a.childNodes, currSelection);
1422
+ var $span = Array.from($editer.querySelectorAll("span"));
1423
+ var selectionSpan = currSelection.nodeType === Node.ELEMENT_NODE ? currSelection.closest("span") : (_a = currSelection.parentElement) === null || _a === void 0 ? void 0 : _a.closest("span");
1424
+ var currentIndex = selectionSpan ? $span.indexOf(selectionSpan) : -1;
1404
1425
  var i = currentIndex;
1405
- if ($prev == null) {
1426
+ if ($prev == null || currentIndex < 0 || !$span[currentIndex]) {
1406
1427
  return null;
1407
1428
  }
1408
1429
  var funcName = null;
1409
1430
  var paramindex = null;
1410
- if ($span[i].classList.contains("luckysheet-formula-text-func")) {
1431
+ 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")) {
1411
1432
  funcName = $span[i].textContent;
1412
1433
  } else {
1413
1434
  var $cur = null;
1414
1435
  var exceptIndex = [-1, -1];
1415
1436
  while (--i > 0) {
1416
1437
  $cur = $span[i];
1417
- if ($cur.classList.contains("luckysheet-formula-text-func") || _lodash.default.trim($cur.textContent || "").toUpperCase() in ctx.formulaCache.functionlistMap) {
1438
+ 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) {
1418
1439
  funcName = $cur.textContent;
1419
1440
  paramindex = null;
1420
1441
  var endstate = true;
@@ -1426,13 +1447,13 @@ function helpFunctionExe($editer, currSelection, ctx) {
1426
1447
  continue;
1427
1448
  }
1428
1449
  $cur = $span[a];
1429
- if ($cur.classList.contains("luckysheet-formula-text-rpar")) {
1450
+ if ((_e = $cur === null || $cur === void 0 ? void 0 : $cur.classList) === null || _e === void 0 ? void 0 : _e.contains("luckysheet-formula-text-rpar")) {
1430
1451
  exceptIndex = [i, a];
1431
1452
  funcName = null;
1432
1453
  endstate = false;
1433
1454
  break;
1434
1455
  }
1435
- if ($cur.classList.contains("luckysheet-formula-text-comma")) {
1456
+ if ((_f = $cur === null || $cur === void 0 ? void 0 : $cur.classList) === null || _f === void 0 ? void 0 : _f.contains("luckysheet-formula-text-comma")) {
1436
1457
  paramindex += 1;
1437
1458
  }
1438
1459
  }
@@ -1465,6 +1486,7 @@ function rangeHightlightselected(ctx, $editor) {
1465
1486
  var funcName = helpFunctionExe($editor, currSelection, ctx);
1466
1487
  ctx.functionHint = funcName === null || funcName === void 0 ? void 0 : funcName.toUpperCase();
1467
1488
  ctx.functionCandidates = [];
1489
+ ctx.defaultCandidates = [];
1468
1490
  }
1469
1491
  }
1470
1492
  function functionHTML(txt) {
@@ -1615,6 +1637,99 @@ function getRangeIndexes($editor) {
1615
1637
  });
1616
1638
  return res;
1617
1639
  }
1640
+ function getLastFormulaRangeIndex($editor) {
1641
+ var spans = Array.from($editor.querySelectorAll("span")).filter(function (span) {
1642
+ var _a;
1643
+ return (_a = span.textContent) === null || _a === void 0 ? void 0 : _a.trim().length;
1644
+ });
1645
+ var lastSpan = spans[spans.length - 1];
1646
+ if (!lastSpan) return null;
1647
+ if (!lastSpan.classList.contains("fortune-formula-functionrange-cell")) {
1648
+ return null;
1649
+ }
1650
+ var indexStr = lastSpan.getAttribute("rangeindex");
1651
+ if (!indexStr) return null;
1652
+ var rangeIndex = parseInt(indexStr, 10);
1653
+ return Number.isNaN(rangeIndex) ? null : rangeIndex;
1654
+ }
1655
+ function getFormulaRangeIndexAtCaret($editor) {
1656
+ var sel = window.getSelection();
1657
+ if (!sel || sel.rangeCount === 0) return null;
1658
+ var anchorNode = sel.anchorNode;
1659
+ if (!anchorNode) return null;
1660
+ var el = anchorNode.nodeType === Node.ELEMENT_NODE ? anchorNode : anchorNode.parentElement;
1661
+ if (!el) return null;
1662
+ var cell = el.closest(".fortune-formula-functionrange-cell");
1663
+ if (!cell || !$editor.contains(cell)) return null;
1664
+ var ri = cell.getAttribute("rangeindex");
1665
+ if (!ri) return null;
1666
+ var n = parseInt(ri, 10);
1667
+ return Number.isNaN(n) ? null : n;
1668
+ }
1669
+ function hasCommaOrAnotherRefAfterRangeCell(cell) {
1670
+ var _a, _b;
1671
+ var n = cell.nextSibling;
1672
+ while (n) {
1673
+ if (n.nodeType === Node.ELEMENT_NODE) {
1674
+ var e = n;
1675
+ if ((_a = e.classList) === null || _a === void 0 ? void 0 : _a.contains("luckysheet-formula-text-comma")) return true;
1676
+ if ((_b = e.classList) === null || _b === void 0 ? void 0 : _b.contains("fortune-formula-functionrange-cell")) return true;
1677
+ }
1678
+ n = n.nextSibling;
1679
+ }
1680
+ return false;
1681
+ }
1682
+ function markRangeSelectionDirty(ctx) {
1683
+ ctx.formulaCache.rangeSelectionActive = false;
1684
+ ctx.formulaRangeHighlight = [];
1685
+ ctx.formulaRangeSelect = undefined;
1686
+ ctx.formulaCache.selectingRangeIndex = -1;
1687
+ ctx.formulaCache.func_selectedrange = undefined;
1688
+ ctx.formulaCache.rangestart = false;
1689
+ ctx.formulaCache.rangedrag_column_start = false;
1690
+ ctx.formulaCache.rangedrag_row_start = false;
1691
+ ctx.formulaCache.rangechangeindex = undefined;
1692
+ }
1693
+ function getFormulaRangeIndexForKeyboardSync($editor) {
1694
+ var atCaret = getFormulaRangeIndexAtCaret($editor);
1695
+ if (atCaret !== null) return atCaret;
1696
+ var lastIdx = getLastFormulaRangeIndex($editor);
1697
+ if (lastIdx === null) return null;
1698
+ var cell = $editor.querySelector("span.fortune-formula-functionrange-cell[rangeindex=\"".concat(lastIdx, "\"]"));
1699
+ if (!cell) return null;
1700
+ var sel = window.getSelection();
1701
+ if (!(sel === null || sel === void 0 ? void 0 : sel.anchorNode)) return lastIdx;
1702
+ var caretRange = document.createRange();
1703
+ try {
1704
+ caretRange.setStart(sel.anchorNode, sel.anchorOffset);
1705
+ caretRange.collapse(true);
1706
+ } catch (_a) {
1707
+ return lastIdx;
1708
+ }
1709
+ var cellRange = document.createRange();
1710
+ try {
1711
+ cellRange.selectNodeContents(cell);
1712
+ } catch (_b) {
1713
+ return lastIdx;
1714
+ }
1715
+ if (caretRange.compareBoundaryPoints(Range.START_TO_START, cellRange) < 0) {
1716
+ return null;
1717
+ }
1718
+ var afterCell = document.createRange();
1719
+ try {
1720
+ afterCell.setStartAfter(cell);
1721
+ afterCell.collapse(true);
1722
+ } catch (_c) {
1723
+ return lastIdx;
1724
+ }
1725
+ if (caretRange.compareBoundaryPoints(Range.START_TO_START, afterCell) >= 0) {
1726
+ if (hasCommaOrAnotherRefAfterRangeCell(cell)) {
1727
+ return null;
1728
+ }
1729
+ return lastIdx;
1730
+ }
1731
+ return lastIdx;
1732
+ }
1618
1733
  function handleFormulaInput(ctx, $copyTo, $editor, kcode, preText, refreshRangeSelect) {
1619
1734
  var _a, _b, _c, _d, _e, _f;
1620
1735
  if (refreshRangeSelect === void 0) {
@@ -1622,6 +1737,13 @@ function handleFormulaInput(ctx, $copyTo, $editor, kcode, preText, refreshRangeS
1622
1737
  }
1623
1738
  if (!$editor) return;
1624
1739
  try {
1740
+ var isBackspaceOrDelete = kcode === 8 || kcode === 46;
1741
+ var isAlphaNumeric = kcode >= 48 && kcode <= 57 || kcode >= 65 && kcode <= 90 || kcode >= 97 && kcode <= 122;
1742
+ if (ctx.formulaCache.rangeSelectionActive === true) {
1743
+ if (isBackspaceOrDelete || isAlphaNumeric) {
1744
+ markRangeSelectionDirty(ctx);
1745
+ }
1746
+ }
1625
1747
  var value1 = void 0;
1626
1748
  var value1txt = preText !== null && preText !== void 0 ? preText : $editor.innerText;
1627
1749
  var value = $editor.innerText;
@@ -1852,61 +1974,204 @@ function functionStrChange_range(txt, type, rc, orient, stindex, step) {
1852
1974
  }
1853
1975
  return "";
1854
1976
  }
1977
+ function setRangeSetValueToFromCaretPosition(ctx, editor, sel) {
1978
+ if (sel.rangeCount === 0 || !sel.anchorNode) return false;
1979
+ if (!editor.contains(sel.anchorNode)) return false;
1980
+ var range = sel.getRangeAt(0).cloneRange();
1981
+ range.collapse(true);
1982
+ var startContainer = range.startContainer,
1983
+ startOffset = range.startOffset;
1984
+ if (startContainer.nodeType === Node.TEXT_NODE) {
1985
+ if (startOffset === 0) {
1986
+ var textParent = startContainer.parentElement;
1987
+ if (textParent === editor) {
1988
+ var prev = startContainer.previousSibling;
1989
+ if (prev) {
1990
+ ctx.formulaCache.rangeSetValueTo = prev;
1991
+ return true;
1992
+ }
1993
+ return false;
1994
+ }
1995
+ var el = textParent;
1996
+ while (el && el !== editor) {
1997
+ if (el.previousSibling) {
1998
+ ctx.formulaCache.rangeSetValueTo = el.previousSibling;
1999
+ return true;
2000
+ }
2001
+ el = el.parentElement;
2002
+ }
2003
+ return false;
2004
+ }
2005
+ var p = startContainer.parentElement;
2006
+ if (p && editor.contains(p)) {
2007
+ ctx.formulaCache.rangeSetValueTo = p;
2008
+ return true;
2009
+ }
2010
+ return false;
2011
+ }
2012
+ if (startContainer.nodeType === Node.ELEMENT_NODE && startContainer === editor) {
2013
+ if (startOffset > 0) {
2014
+ var prev = startContainer.childNodes[startOffset - 1];
2015
+ if (prev) {
2016
+ ctx.formulaCache.rangeSetValueTo = prev;
2017
+ return true;
2018
+ }
2019
+ }
2020
+ return false;
2021
+ }
2022
+ if (startContainer.nodeType === Node.ELEMENT_NODE) {
2023
+ if (startOffset > 0) {
2024
+ var prev = startContainer.childNodes[startOffset - 1];
2025
+ if (prev) {
2026
+ ctx.formulaCache.rangeSetValueTo = prev;
2027
+ return true;
2028
+ }
2029
+ }
2030
+ var el = startContainer;
2031
+ while (el && el !== editor) {
2032
+ if (el.previousSibling) {
2033
+ ctx.formulaCache.rangeSetValueTo = el.previousSibling;
2034
+ return true;
2035
+ }
2036
+ el = el.parentElement;
2037
+ }
2038
+ }
2039
+ return false;
2040
+ }
1855
2041
  function israngeseleciton(ctx, istooltip) {
1856
- var _a, _b, _c;
2042
+ var _a, _b, _c, _d;
1857
2043
  if (istooltip == null) {
1858
2044
  istooltip = false;
1859
2045
  }
1860
2046
  var currSelection = window.getSelection();
1861
- if (currSelection == null) return false;
2047
+ if (currSelection == null) {
2048
+ return false;
2049
+ }
1862
2050
  var anchor = currSelection.anchorNode;
1863
- if (!(anchor === null || anchor === void 0 ? void 0 : anchor.textContent)) return false;
2051
+ if (!anchor) {
2052
+ return false;
2053
+ }
1864
2054
  var anchorOffset = currSelection.anchorOffset;
1865
2055
  var anchorElement = anchor;
1866
2056
  var parentElement = anchor.parentNode;
2057
+ var allowRangeInsertionAtCaret = function allowRangeInsertionAtCaret() {
2058
+ var _a, _b;
2059
+ if (ctx.formulaCache.rangestart || ctx.formulaCache.rangedrag_column_start || ctx.formulaCache.rangedrag_row_start || ctx.formulaCache.rangeSelectionActive === true) {
2060
+ return true;
2061
+ }
2062
+ 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");
2063
+ if (!editor || currSelection.rangeCount === 0) {
2064
+ return true;
2065
+ }
2066
+ var inputText = editor.innerText.trim();
2067
+ if (!inputText.startsWith("=")) {
2068
+ return false;
2069
+ }
2070
+ if (/^=\s*[A-Za-z_][A-Za-z0-9_]*$/.test(inputText)) {
2071
+ return false;
2072
+ }
2073
+ var caretRange = currSelection.getRangeAt(0).cloneRange();
2074
+ var preCaretRange = document.createRange();
2075
+ preCaretRange.selectNodeContents(editor);
2076
+ preCaretRange.setEnd(caretRange.endContainer, caretRange.endOffset);
2077
+ var caretOffset = preCaretRange.toString().length;
2078
+ var textAfter = editor.innerText.slice(caretOffset);
2079
+ var remaining = textAfter.replace(/^\s+/, "");
2080
+ if (remaining.length === 0) {
2081
+ return true;
2082
+ }
2083
+ var first = remaining[0];
2084
+ if (first === "," || first === ")" || first === "&" || first in operatorjson) {
2085
+ return true;
2086
+ }
2087
+ return false;
2088
+ };
1867
2089
  if (((_a = anchor === null || anchor === void 0 ? void 0 : anchor.parentNode) === null || _a === void 0 ? void 0 : _a.nodeName.toLowerCase()) === "span" && anchorOffset !== 0) {
1868
- var txt = _lodash.default.trim(anchor.textContent);
2090
+ var txt = _lodash.default.trim((_b = anchor.textContent) !== null && _b !== void 0 ? _b : "");
1869
2091
  var lasttxt = "";
1870
2092
  if (txt.length === 0 && anchor.parentNode.previousSibling) {
1871
2093
  var ahr = anchor.parentNode.previousSibling;
1872
2094
  txt = _lodash.default.trim(ahr.textContent || "");
1873
- lasttxt = txt.substring(txt.length - 1, 1);
1874
- ctx.formulaCache.rangeSetValueTo = anchor.parentNode;
2095
+ lasttxt = txt.slice(-1);
1875
2096
  } else {
1876
- lasttxt = txt.substring(anchorOffset - 1, 1);
1877
- ctx.formulaCache.rangeSetValueTo = anchor.parentNode;
2097
+ lasttxt = anchorOffset > 0 ? txt.charAt(anchorOffset - 1) : "";
1878
2098
  }
1879
2099
  if (istooltip && (lasttxt === "(" || lasttxt === ",") || !istooltip && (lasttxt === "(" || lasttxt === "," || lasttxt === "=" || lasttxt in operatorjson || lasttxt === "&")) {
1880
- return true;
2100
+ ctx.formulaCache.rangeSetValueTo = anchor.parentNode;
2101
+ return allowRangeInsertionAtCaret();
1881
2102
  }
1882
2103
  } else if (anchorElement.id === "luckysheet-rich-text-editor" || anchorElement.id === "luckysheet-functionbox-cell") {
1883
- var txt = _lodash.default.trim((_b = _lodash.default.last(anchorElement.querySelectorAll("span"))) === null || _b === void 0 ? void 0 : _b.innerText);
1884
- ctx.formulaCache.rangeSetValueTo = _lodash.default.last(anchorElement.querySelectorAll("span"));
1885
- if (txt.length === 0 && anchorElement.querySelectorAll("span").length > 1) {
1886
- var ahr = anchorElement.querySelectorAll("span");
1887
- txt = _lodash.default.trim(ahr[ahr.length - 2].innerText);
1888
- txt = _lodash.default.trim(ahr[ahr.length - 2].innerText);
1889
- ctx.formulaCache.rangeSetValueTo = ahr;
2104
+ var editorEl = anchorElement;
2105
+ if (currSelection.rangeCount > 0 && setRangeSetValueToFromCaretPosition(ctx, editorEl, currSelection) && allowRangeInsertionAtCaret()) {
2106
+ return true;
1890
2107
  }
1891
- var lasttxt = txt.substring(txt.length - 1, 1);
2108
+ var spans = editorEl.querySelectorAll("span");
2109
+ var txt = _lodash.default.trim((_c = _lodash.default.last(spans)) === null || _c === void 0 ? void 0 : _c.innerText);
2110
+ var refSpan = _lodash.default.last(spans);
2111
+ if (txt.length === 0 && spans.length > 1) {
2112
+ txt = _lodash.default.trim(spans[spans.length - 2].innerText);
2113
+ refSpan = spans[spans.length - 2];
2114
+ }
2115
+ var lasttxt = txt.slice(-1);
1892
2116
  if (istooltip && (lasttxt === "(" || lasttxt === ",") || !istooltip && (lasttxt === "(" || lasttxt === "," || lasttxt === "=" || lasttxt in operatorjson || lasttxt === "&")) {
1893
- return true;
2117
+ ctx.formulaCache.rangeSetValueTo = refSpan;
2118
+ return allowRangeInsertionAtCaret();
1894
2119
  }
1895
2120
  } else if (parentElement.id === "luckysheet-rich-text-editor" || parentElement.id === "luckysheet-functionbox-cell" || anchorOffset === 0) {
1896
2121
  if (anchorOffset === 0) {
1897
2122
  anchor = anchor.parentNode;
1898
2123
  }
1899
- if (!anchor) return false;
1900
- if (((_c = anchor.previousSibling) === null || _c === void 0 ? void 0 : _c.textContent) == null) return false;
2124
+ if (!anchor) {
2125
+ return false;
2126
+ }
2127
+ if (((_d = anchor.previousSibling) === null || _d === void 0 ? void 0 : _d.textContent) == null) {
2128
+ return false;
2129
+ }
1901
2130
  if (anchor.previousSibling) {
1902
2131
  var txt = _lodash.default.trim(anchor.previousSibling.textContent);
1903
- var lasttxt = txt.substring(txt.length - 1, 1);
1904
- ctx.formulaCache.rangeSetValueTo = anchor.previousSibling;
2132
+ var lasttxt = txt.slice(-1);
1905
2133
  if (istooltip && (lasttxt === "(" || lasttxt === ",") || !istooltip && (lasttxt === "(" || lasttxt === "," || lasttxt === "=" || lasttxt in operatorjson || lasttxt === "&")) {
1906
- return true;
2134
+ ctx.formulaCache.rangeSetValueTo = anchor.previousSibling;
2135
+ return allowRangeInsertionAtCaret();
1907
2136
  }
1908
2137
  }
1909
2138
  }
2139
+ if (!istooltip && (ctx.formulaCache.rangestart || ctx.formulaCache.rangedrag_column_start || ctx.formulaCache.rangedrag_row_start || ctx.formulaCache.rangeSelectionActive === true)) {
2140
+ var editor = document.getElementById("luckysheet-rich-text-editor") || document.getElementById("luckysheet-functionbox-cell");
2141
+ if (editor && currSelection.rangeCount > 0 && setRangeSetValueToFromCaretPosition(ctx, editor, currSelection) && allowRangeInsertionAtCaret()) {
2142
+ return true;
2143
+ }
2144
+ }
2145
+ return false;
2146
+ }
2147
+ function isFormulaReferenceInputMode(ctx) {
2148
+ var editor = document.getElementById("luckysheet-rich-text-editor");
2149
+ var inputText = ((editor === null || editor === void 0 ? void 0 : editor.innerText) || "").trim();
2150
+ var hasRangeToken = (editor === null || editor === void 0 ? void 0 : editor.querySelector("span.fortune-formula-functionrange-cell")) != null;
2151
+ if (!!ctx.formulaCache.rangestart || !!ctx.formulaCache.rangedrag_column_start || !!ctx.formulaCache.rangedrag_row_start || hasRangeToken || ctx.formulaCache.rangeSelectionActive === true) {
2152
+ return true;
2153
+ }
2154
+ if (!inputText.startsWith("=")) {
2155
+ return false;
2156
+ }
2157
+ if (/^=\s*[A-Za-z_][A-Za-z0-9_]*$/.test(inputText)) {
2158
+ return false;
2159
+ }
2160
+ return israngeseleciton(ctx);
2161
+ }
2162
+ function maybeRecoverDirtyRangeSelection(ctx) {
2163
+ if (ctx.formulaCache.rangeSelectionActive !== false) {
2164
+ return false;
2165
+ }
2166
+ var editor = document.getElementById("luckysheet-rich-text-editor");
2167
+ if (!editor) {
2168
+ return false;
2169
+ }
2170
+ var inputText = (editor.innerText || "").trim();
2171
+ if (inputText.startsWith("=") && israngeseleciton(ctx)) {
2172
+ ctx.formulaCache.rangeSelectionActive = null;
2173
+ return true;
2174
+ }
1910
2175
  return false;
1911
2176
  }
1912
2177
  function functionStrChange(txt, type, rc, orient, stindex, step) {
@@ -2004,18 +2269,22 @@ function functionStrChange(txt, type, rc, orient, stindex, step) {
2004
2269
  return function_str;
2005
2270
  }
2006
2271
  function rangeSetValue(ctx, cellInput, selected, fxInput) {
2007
- var _a, _b, _c, _d, _e;
2272
+ var _a, _b, _c, _d, _e, _f, _g;
2008
2273
  var parser = new DOMParser();
2009
2274
  var doc = parser.parseFromString("<div>".concat(cellInput.innerHTML, "</div>"), "text/html");
2010
2275
  var spans = doc.querySelectorAll("span");
2011
2276
  var lastSpan = spans[spans.length - 1];
2012
- if (lastSpan && (0, _index.isLetterNumberPattern)(lastSpan === null || lastSpan === void 0 ? void 0 : lastSpan.innerText)) {
2277
+ 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;
2278
+ if (lastSpan && (0, _index.isLetterNumberPattern)(lastSpan === null || lastSpan === void 0 ? void 0 : lastSpan.innerText) && !isManagedRangeSpan) {
2013
2279
  var htmlR = (0, _index.removeLastSpan)(cellInput.innerHTML);
2014
2280
  cellInput.innerHTML = "".concat(htmlR);
2281
+ cellInput.focus();
2282
+ var kids = cellInput.childNodes;
2283
+ ctx.formulaCache.rangeSetValueTo = kids.length > 0 ? kids[kids.length - 1] : undefined;
2015
2284
  }
2016
2285
  var $editor = cellInput;
2017
2286
  var $copyTo = fxInput;
2018
- if (((_a = document.activeElement) === null || _a === void 0 ? void 0 : _a.id) === "luckysheet-functionbox-cell") {
2287
+ if (((_c = document.activeElement) === null || _c === void 0 ? void 0 : _c.id) === "luckysheet-functionbox-cell") {
2019
2288
  $editor = fxInput;
2020
2289
  $copyTo = cellInput;
2021
2290
  }
@@ -2030,20 +2299,23 @@ function rangeSetValue(ctx, cellInput, selected, fxInput) {
2030
2299
  } else {
2031
2300
  range = (0, _cell.getRangetxt)(ctx, ctx.currentSheetId, selected, ctx.formulaCache.rangetosheet);
2032
2301
  }
2033
- if (!israngeseleciton(ctx) && (ctx.formulaCache.rangestart || ctx.formulaCache.rangedrag_column_start || ctx.formulaCache.rangedrag_row_start)) {
2034
- var span = $editor.querySelector("span[rangeindex='".concat(ctx.formulaCache.rangechangeindex, "']"));
2035
- if (span) {
2036
- span.innerHTML = range;
2037
- setCaretPosition(ctx, span, 0, range.length);
2038
- }
2302
+ var activeRangeFlow = ctx.formulaCache.rangestart || ctx.formulaCache.rangedrag_column_start || ctx.formulaCache.rangedrag_row_start || ctx.formulaCache.rangeSelectionActive === true;
2303
+ var spanToReplace = !_lodash.default.isNil(ctx.formulaCache.rangechangeindex) ? $editor.querySelector("span[rangeindex='".concat(ctx.formulaCache.rangechangeindex, "']")) : null;
2304
+ if (activeRangeFlow && spanToReplace) {
2305
+ spanToReplace.innerHTML = range;
2306
+ setCaretPosition(ctx, spanToReplace, 0, range.length);
2039
2307
  } else {
2040
2308
  var function_str = "<span class=\"fortune-formula-functionrange-cell\" rangeindex=\"".concat(functionHTMLIndex, "\" dir=\"auto\" style=\"color:").concat(_color.colors[functionHTMLIndex], ";\">").concat(range, "</span>");
2041
2309
  var newEle = parseElement(function_str);
2042
2310
  var refEle = ctx.formulaCache.rangeSetValueTo;
2311
+ if (refEle && !refEle.parentNode) {
2312
+ israngeseleciton(ctx);
2313
+ refEle = ctx.formulaCache.rangeSetValueTo;
2314
+ }
2043
2315
  if (refEle && refEle.parentNode) {
2044
- var leftPar = (_b = document.getElementsByClassName("luckysheet-formula-text-lpar")) === null || _b === void 0 ? void 0 : _b[0];
2045
- if ((_c = leftPar === null || leftPar === void 0 ? void 0 : leftPar.parentElement) === null || _c === void 0 ? void 0 : _c.classList.contains("luckysheet-formula-text-color")) {
2046
- (_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);
2316
+ var leftPar = (_d = document.getElementsByClassName("luckysheet-formula-text-lpar")) === null || _d === void 0 ? void 0 : _d[0];
2317
+ if ((_e = leftPar === null || leftPar === void 0 ? void 0 : leftPar.parentElement) === null || _e === void 0 ? void 0 : _e.classList.contains("luckysheet-formula-text-color")) {
2318
+ (_g = (_f = document.getElementsByClassName("luckysheet-formula-text-lpar")) === null || _f === void 0 ? void 0 : _f[0].parentNode) === null || _g === void 0 ? void 0 : _g.appendChild(newEle);
2047
2319
  } else {
2048
2320
  refEle.parentNode.insertBefore(newEle, refEle.nextSibling);
2049
2321
  }
@@ -2083,6 +2355,7 @@ function setRangeSelect(container, left, top, height, width) {
2083
2355
  rangeElement.style.width = "".concat(width, "px");
2084
2356
  }
2085
2357
  function rangeDrag(ctx, e, cellInput, scrollLeft, scrollTop, container, fxInput) {
2358
+ ctx.formulaCache.rangeSelectionActive = true;
2086
2359
  var func_selectedrange = ctx.formulaCache.func_selectedrange;
2087
2360
  if (!func_selectedrange || func_selectedrange.left == null || func_selectedrange.height == null || func_selectedrange.top == null || func_selectedrange.width == null) return;
2088
2361
  var rect = container.getBoundingClientRect();
@@ -2138,6 +2411,20 @@ function rangeDrag(ctx, e, cellInput, scrollLeft, scrollTop, container, fxInput)
2138
2411
  func_selectedrange.width_move = width;
2139
2412
  func_selectedrange.top_move = top;
2140
2413
  func_selectedrange.height_move = height;
2414
+ ctx.luckysheet_select_save = [{
2415
+ row: [rowseleted[0], rowseleted[1]],
2416
+ column: [columnseleted[0], columnseleted[1]],
2417
+ row_focus: row_index,
2418
+ column_focus: col_index,
2419
+ left: left,
2420
+ top: top,
2421
+ width: width,
2422
+ height: height,
2423
+ left_move: left,
2424
+ top_move: top,
2425
+ width_move: width,
2426
+ height_move: height
2427
+ }];
2141
2428
  rangeSetValue(ctx, cellInput, {
2142
2429
  row: rowseleted,
2143
2430
  column: columnseleted
@@ -2146,6 +2433,7 @@ function rangeDrag(ctx, e, cellInput, scrollLeft, scrollTop, container, fxInput)
2146
2433
  e.preventDefault();
2147
2434
  }
2148
2435
  function rangeDragColumn(ctx, e, cellInput, scrollLeft, scrollTop, container, fxInput) {
2436
+ ctx.formulaCache.rangeSelectionActive = true;
2149
2437
  var func_selectedrange = ctx.formulaCache.func_selectedrange;
2150
2438
  if (!func_selectedrange || func_selectedrange.left == null || func_selectedrange.height == null || func_selectedrange.top == null || func_selectedrange.width == null) return;
2151
2439
  var mouse = (0, _location.mousePosition)(e.pageX, e.pageY, ctx);
@@ -2181,6 +2469,20 @@ function rangeDragColumn(ctx, e, cellInput, scrollLeft, scrollTop, container, fx
2181
2469
  func_selectedrange.column = columnseleted;
2182
2470
  func_selectedrange.left_move = left;
2183
2471
  func_selectedrange.width_move = width;
2472
+ ctx.luckysheet_select_save = [{
2473
+ row: [0, row_index],
2474
+ column: [columnseleted[0], columnseleted[1]],
2475
+ row_focus: 0,
2476
+ column_focus: col_index,
2477
+ left: left,
2478
+ top: row_pre,
2479
+ width: width,
2480
+ height: row - row_pre - 1,
2481
+ left_move: left,
2482
+ top_move: row_pre,
2483
+ width_move: width,
2484
+ height_move: row - row_pre - 1
2485
+ }];
2184
2486
  rangeSetValue(ctx, cellInput, {
2185
2487
  row: [null, null],
2186
2488
  column: columnseleted
@@ -2188,6 +2490,7 @@ function rangeDragColumn(ctx, e, cellInput, scrollLeft, scrollTop, container, fx
2188
2490
  setRangeSelect(container, left, row_pre, row - row_pre - 1, width);
2189
2491
  }
2190
2492
  function rangeDragRow(ctx, e, cellInput, scrollLeft, scrollTop, container, fxInput) {
2493
+ ctx.formulaCache.rangeSelectionActive = true;
2191
2494
  var func_selectedrange = ctx.formulaCache.func_selectedrange;
2192
2495
  if (!func_selectedrange || func_selectedrange.left == null || func_selectedrange.height == null || func_selectedrange.top == null || func_selectedrange.width == null) return;
2193
2496
  var mouse = (0, _location.mousePosition)(e.pageX, e.pageY, ctx);
@@ -2223,6 +2526,20 @@ function rangeDragRow(ctx, e, cellInput, scrollLeft, scrollTop, container, fxInp
2223
2526
  func_selectedrange.row = rowseleted;
2224
2527
  func_selectedrange.top_move = top;
2225
2528
  func_selectedrange.height_move = height;
2529
+ ctx.luckysheet_select_save = [{
2530
+ row: [rowseleted[0], rowseleted[1]],
2531
+ column: [0, col_index],
2532
+ row_focus: row_index,
2533
+ column_focus: 0,
2534
+ left: col_pre,
2535
+ top: top,
2536
+ width: col - col_pre - 1,
2537
+ height: height,
2538
+ left_move: col_pre,
2539
+ top_move: top,
2540
+ width_move: col - col_pre - 1,
2541
+ height_move: height
2542
+ }];
2226
2543
  rangeSetValue(ctx, cellInput, {
2227
2544
  row: rowseleted,
2228
2545
  column: [null, null]