@fileverse-dev/fortune-core 1.3.10 → 1.3.11-input-ref-1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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 +62 -40
  5. package/es/events/paste.js +77 -28
  6. package/es/modules/cell.js +60 -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 +11 -0
  11. package/es/modules/formula.js +390 -47
  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 +61 -39
  26. package/lib/events/paste.js +77 -28
  27. package/lib/modules/cell.js +60 -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 +11 -0
  32. package/lib/modules/formula.js +399 -47
  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,21 @@ exports.functionCopy = functionCopy;
13
13
  exports.functionHTMLGenerate = functionHTMLGenerate;
14
14
  exports.functionStrChange = functionStrChange;
15
15
  exports.getAllFunctionGroup = getAllFunctionGroup;
16
+ exports.getFormulaEditorOwner = getFormulaEditorOwner;
17
+ exports.getFormulaRangeIndexAtCaret = getFormulaRangeIndexAtCaret;
18
+ exports.getFormulaRangeIndexForKeyboardSync = getFormulaRangeIndexForKeyboardSync;
19
+ exports.getLastFormulaRangeIndex = getLastFormulaRangeIndex;
16
20
  exports.getcellrange = getcellrange;
17
21
  exports.getrangeseleciton = getrangeseleciton;
18
22
  exports.groupValuesRefresh = groupValuesRefresh;
19
23
  exports.handleFormulaInput = handleFormulaInput;
20
24
  exports.insertUpdateFunctionGroup = insertUpdateFunctionGroup;
25
+ exports.isCaretAtValidFormulaRangeInsertionPoint = isCaretAtValidFormulaRangeInsertionPoint;
26
+ exports.isFormulaReferenceInputMode = isFormulaReferenceInputMode;
21
27
  exports.iscelldata = iscelldata;
22
28
  exports.israngeseleciton = israngeseleciton;
29
+ exports.markRangeSelectionDirty = markRangeSelectionDirty;
30
+ exports.maybeRecoverDirtyRangeSelection = maybeRecoverDirtyRangeSelection;
23
31
  exports.moveCursorToEnd = moveCursorToEnd;
24
32
  exports.onFormulaRangeDragEnd = onFormulaRangeDragEnd;
25
33
  exports.rangeDrag = rangeDrag;
@@ -28,6 +36,7 @@ exports.rangeDragRow = rangeDragRow;
28
36
  exports.rangeHightlightselected = rangeHightlightselected;
29
37
  exports.rangeSetValue = rangeSetValue;
30
38
  exports.setCaretPosition = setCaretPosition;
39
+ exports.setFormulaEditorOwner = setFormulaEditorOwner;
31
40
  var _formulaParser = require("@fileverse-dev/formula-parser");
32
41
  var _lodash = _interopRequireDefault(require("lodash"));
33
42
  var _context = require("../context");
@@ -38,7 +47,6 @@ var _locale = require("../locale");
38
47
  var _color = require("./color");
39
48
  var _location = require("./location");
40
49
  var _2 = require(".");
41
- var _index = require("../utils/index");
42
50
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
43
51
  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
52
  var __assign = void 0 && (void 0).__assign || function () {
@@ -86,6 +94,8 @@ var FormulaCache = exports.FormulaCache = function () {
86
94
  var that = this;
87
95
  this.data_parm_index = 0;
88
96
  this.selectingRangeIndex = -1;
97
+ this.rangeSelectionActive = null;
98
+ this.formulaEditorOwner = null;
89
99
  this.functionlistMap = {};
90
100
  this.execFunctionGlobalData = {};
91
101
  this.cellTextToIndexList = {};
@@ -815,6 +825,7 @@ function insertUpdateDynamicArray(ctx, dynamicArrayItem) {
815
825
  return dynamicArray;
816
826
  }
817
827
  function groupValuesRefresh(ctx) {
828
+ var _a, _b, _c;
818
829
  var luckysheetfile = ctx.luckysheetfile;
819
830
  if (ctx.groupValuesRefreshData.length > 0) {
820
831
  for (var i = 0; i < ctx.groupValuesRefreshData.length; i += 1) {
@@ -837,6 +848,19 @@ function groupValuesRefresh(ctx) {
837
848
  updateValue.v = item.v;
838
849
  updateValue.f = item.f;
839
850
  (0, _cell.setCellValue)(ctx, item.r, item.c, data, updateValue);
851
+ if ((_a = ctx === null || ctx === void 0 ? void 0 : ctx.hooks) === null || _a === void 0 ? void 0 : _a.updateCellYdoc) {
852
+ ctx.hooks.updateCellYdoc([{
853
+ sheetId: item.id,
854
+ path: ["celldata"],
855
+ value: {
856
+ r: item.r,
857
+ c: item.c,
858
+ 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
859
+ },
860
+ key: "".concat(item.r, "_").concat(item.c),
861
+ type: "update"
862
+ }]);
863
+ }
840
864
  }
841
865
  ctx.groupValuesRefreshData = [];
842
866
  }
@@ -1302,8 +1326,7 @@ function setCaretPosition(ctx, textDom, children, pos, parentTextDom) {
1302
1326
  sel === null || sel === void 0 ? void 0 : sel.removeAllRanges();
1303
1327
  sel === null || sel === void 0 ? void 0 : sel.addRange(range);
1304
1328
  el.focus();
1305
- } catch (err) {
1306
- console.error(err);
1329
+ } catch (_c) {
1307
1330
  moveCursorToEnd(parentTextDom);
1308
1331
  }
1309
1332
  }
@@ -1388,7 +1411,7 @@ function getrangeseleciton() {
1388
1411
  return null;
1389
1412
  }
1390
1413
  function helpFunctionExe($editer, currSelection, ctx) {
1391
- var _a;
1414
+ var _a, _b, _c, _d, _e, _f;
1392
1415
  var functionlist = (0, _locale.locale)(ctx).functionlist;
1393
1416
  if (_lodash.default.isEmpty(ctx.formulaCache.functionlistMap)) {
1394
1417
  for (var i_1 = 0; i_1 < functionlist.length; i_1 += 1) {
@@ -1399,22 +1422,23 @@ function helpFunctionExe($editer, currSelection, ctx) {
1399
1422
  return null;
1400
1423
  }
1401
1424
  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);
1425
+ var $span = Array.from($editer.querySelectorAll("span"));
1426
+ var selectionSpan = currSelection.nodeType === Node.ELEMENT_NODE ? currSelection.closest("span") : (_a = currSelection.parentElement) === null || _a === void 0 ? void 0 : _a.closest("span");
1427
+ var currentIndex = selectionSpan ? $span.indexOf(selectionSpan) : -1;
1404
1428
  var i = currentIndex;
1405
- if ($prev == null) {
1429
+ if ($prev == null || currentIndex < 0 || !$span[currentIndex]) {
1406
1430
  return null;
1407
1431
  }
1408
1432
  var funcName = null;
1409
1433
  var paramindex = null;
1410
- if ($span[i].classList.contains("luckysheet-formula-text-func")) {
1434
+ 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
1435
  funcName = $span[i].textContent;
1412
1436
  } else {
1413
1437
  var $cur = null;
1414
1438
  var exceptIndex = [-1, -1];
1415
1439
  while (--i > 0) {
1416
1440
  $cur = $span[i];
1417
- if ($cur.classList.contains("luckysheet-formula-text-func") || _lodash.default.trim($cur.textContent || "").toUpperCase() in ctx.formulaCache.functionlistMap) {
1441
+ 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
1442
  funcName = $cur.textContent;
1419
1443
  paramindex = null;
1420
1444
  var endstate = true;
@@ -1426,13 +1450,13 @@ function helpFunctionExe($editer, currSelection, ctx) {
1426
1450
  continue;
1427
1451
  }
1428
1452
  $cur = $span[a];
1429
- if ($cur.classList.contains("luckysheet-formula-text-rpar")) {
1453
+ if ((_e = $cur === null || $cur === void 0 ? void 0 : $cur.classList) === null || _e === void 0 ? void 0 : _e.contains("luckysheet-formula-text-rpar")) {
1430
1454
  exceptIndex = [i, a];
1431
1455
  funcName = null;
1432
1456
  endstate = false;
1433
1457
  break;
1434
1458
  }
1435
- if ($cur.classList.contains("luckysheet-formula-text-comma")) {
1459
+ if ((_f = $cur === null || $cur === void 0 ? void 0 : $cur.classList) === null || _f === void 0 ? void 0 : _f.contains("luckysheet-formula-text-comma")) {
1436
1460
  paramindex += 1;
1437
1461
  }
1438
1462
  }
@@ -1465,6 +1489,7 @@ function rangeHightlightselected(ctx, $editor) {
1465
1489
  var funcName = helpFunctionExe($editor, currSelection, ctx);
1466
1490
  ctx.functionHint = funcName === null || funcName === void 0 ? void 0 : funcName.toUpperCase();
1467
1491
  ctx.functionCandidates = [];
1492
+ ctx.defaultCandidates = [];
1468
1493
  }
1469
1494
  }
1470
1495
  function functionHTML(txt) {
@@ -1615,6 +1640,154 @@ function getRangeIndexes($editor) {
1615
1640
  });
1616
1641
  return res;
1617
1642
  }
1643
+ function getLastFormulaRangeIndex($editor) {
1644
+ var spans = Array.from($editor.querySelectorAll("span")).filter(function (span) {
1645
+ var _a;
1646
+ return (_a = span.textContent) === null || _a === void 0 ? void 0 : _a.trim().length;
1647
+ });
1648
+ var lastSpan = spans[spans.length - 1];
1649
+ if (!lastSpan) return null;
1650
+ if (!lastSpan.classList.contains("fortune-formula-functionrange-cell")) {
1651
+ return null;
1652
+ }
1653
+ var indexStr = lastSpan.getAttribute("rangeindex");
1654
+ if (!indexStr) return null;
1655
+ var rangeIndex = parseInt(indexStr, 10);
1656
+ return Number.isNaN(rangeIndex) ? null : rangeIndex;
1657
+ }
1658
+ function getFormulaRangeIndexAtCaret($editor) {
1659
+ var sel = window.getSelection();
1660
+ if (!sel || sel.rangeCount === 0) return null;
1661
+ var anchorNode = sel.anchorNode;
1662
+ if (!anchorNode) return null;
1663
+ var el = anchorNode.nodeType === Node.ELEMENT_NODE ? anchorNode : anchorNode.parentElement;
1664
+ if (!el) return null;
1665
+ var cell = el.closest(".fortune-formula-functionrange-cell");
1666
+ if (!cell || !$editor.contains(cell)) return null;
1667
+ var ri = cell.getAttribute("rangeindex");
1668
+ if (!ri) return null;
1669
+ var n = parseInt(ri, 10);
1670
+ return Number.isNaN(n) ? null : n;
1671
+ }
1672
+ function setFormulaEditorOwner(ctx, owner) {
1673
+ ctx.formulaCache.formulaEditorOwner = owner;
1674
+ }
1675
+ function getFormulaEditorOwner(ctx) {
1676
+ var _a, _b;
1677
+ var cachedOwner = ctx.formulaCache.formulaEditorOwner;
1678
+ if (cachedOwner === "cell" || cachedOwner === "fx") {
1679
+ return cachedOwner;
1680
+ }
1681
+ if (((_a = document.activeElement) === null || _a === void 0 ? void 0 : _a.id) === "luckysheet-functionbox-cell") {
1682
+ return "fx";
1683
+ }
1684
+ if (((_b = document.activeElement) === null || _b === void 0 ? void 0 : _b.id) === "luckysheet-rich-text-editor") {
1685
+ return "cell";
1686
+ }
1687
+ return null;
1688
+ }
1689
+ function getCurrentFormulaSlotTextBeforeCaret(editor, caretOffset) {
1690
+ var textBefore = editor.innerText.slice(0, caretOffset);
1691
+ var parts = textBefore.split(/[=,(+\-*/&<>]/);
1692
+ return _lodash.default.trim(parts[parts.length - 1] || "");
1693
+ }
1694
+ function isCaretAtValidFormulaRangeInsertionPoint(editor) {
1695
+ var currSelection = window.getSelection();
1696
+ if (!editor || !currSelection || currSelection.rangeCount === 0) {
1697
+ return false;
1698
+ }
1699
+ var anchorNode = currSelection.anchorNode;
1700
+ if (anchorNode && !editor.contains(anchorNode)) {
1701
+ return false;
1702
+ }
1703
+ var inputText = editor.innerText.trim();
1704
+ if (!inputText.startsWith("=")) {
1705
+ return false;
1706
+ }
1707
+ if (/^=\s*[A-Za-z_][A-Za-z0-9_]*$/.test(inputText)) {
1708
+ return false;
1709
+ }
1710
+ var caretRange = currSelection.getRangeAt(0).cloneRange();
1711
+ var preCaretRange = document.createRange();
1712
+ preCaretRange.selectNodeContents(editor);
1713
+ preCaretRange.setEnd(caretRange.endContainer, caretRange.endOffset);
1714
+ var caretOffset = preCaretRange.toString().length;
1715
+ var slotTextBeforeCaret = getCurrentFormulaSlotTextBeforeCaret(editor, caretOffset);
1716
+ if (slotTextBeforeCaret.length > 0 && !iscelldata(slotTextBeforeCaret)) {
1717
+ return false;
1718
+ }
1719
+ var textAfter = editor.innerText.slice(caretOffset);
1720
+ var remaining = textAfter.replace(/^\s+/, "");
1721
+ if (remaining.length === 0) {
1722
+ return true;
1723
+ }
1724
+ var first = remaining[0];
1725
+ return first === "," || first === ")" || first === "&" || first in operatorjson;
1726
+ }
1727
+ function hasCommaOrAnotherRefAfterRangeCell(cell) {
1728
+ var _a, _b;
1729
+ var n = cell.nextSibling;
1730
+ while (n) {
1731
+ if (n.nodeType === Node.ELEMENT_NODE) {
1732
+ var e = n;
1733
+ if ((_a = e.classList) === null || _a === void 0 ? void 0 : _a.contains("luckysheet-formula-text-comma")) return true;
1734
+ if ((_b = e.classList) === null || _b === void 0 ? void 0 : _b.contains("fortune-formula-functionrange-cell")) return true;
1735
+ }
1736
+ n = n.nextSibling;
1737
+ }
1738
+ return false;
1739
+ }
1740
+ function markRangeSelectionDirty(ctx) {
1741
+ ctx.formulaCache.rangeSelectionActive = false;
1742
+ ctx.formulaRangeHighlight = [];
1743
+ ctx.formulaRangeSelect = undefined;
1744
+ ctx.formulaCache.selectingRangeIndex = -1;
1745
+ ctx.formulaCache.func_selectedrange = undefined;
1746
+ ctx.formulaCache.rangestart = false;
1747
+ ctx.formulaCache.rangedrag_column_start = false;
1748
+ ctx.formulaCache.rangedrag_row_start = false;
1749
+ ctx.formulaCache.rangechangeindex = undefined;
1750
+ }
1751
+ function getFormulaRangeIndexForKeyboardSync($editor) {
1752
+ var atCaret = getFormulaRangeIndexAtCaret($editor);
1753
+ if (atCaret !== null) return atCaret;
1754
+ var lastIdx = getLastFormulaRangeIndex($editor);
1755
+ if (lastIdx === null) return null;
1756
+ var cell = $editor.querySelector("span.fortune-formula-functionrange-cell[rangeindex=\"".concat(lastIdx, "\"]"));
1757
+ if (!cell) return null;
1758
+ var sel = window.getSelection();
1759
+ if (!(sel === null || sel === void 0 ? void 0 : sel.anchorNode)) return lastIdx;
1760
+ var caretRange = document.createRange();
1761
+ try {
1762
+ caretRange.setStart(sel.anchorNode, sel.anchorOffset);
1763
+ caretRange.collapse(true);
1764
+ } catch (_a) {
1765
+ return lastIdx;
1766
+ }
1767
+ var cellRange = document.createRange();
1768
+ try {
1769
+ cellRange.selectNodeContents(cell);
1770
+ } catch (_b) {
1771
+ return lastIdx;
1772
+ }
1773
+ if (caretRange.compareBoundaryPoints(Range.START_TO_START, cellRange) < 0) {
1774
+ return null;
1775
+ }
1776
+ var afterCell = document.createRange();
1777
+ try {
1778
+ afterCell.setStartAfter(cell);
1779
+ afterCell.collapse(true);
1780
+ } catch (_c) {
1781
+ return lastIdx;
1782
+ }
1783
+ if (caretRange.compareBoundaryPoints(Range.START_TO_START, afterCell) >= 0) {
1784
+ if (hasCommaOrAnotherRefAfterRangeCell(cell)) {
1785
+ return null;
1786
+ }
1787
+ return lastIdx;
1788
+ }
1789
+ return lastIdx;
1790
+ }
1618
1791
  function handleFormulaInput(ctx, $copyTo, $editor, kcode, preText, refreshRangeSelect) {
1619
1792
  var _a, _b, _c, _d, _e, _f;
1620
1793
  if (refreshRangeSelect === void 0) {
@@ -1622,6 +1795,13 @@ function handleFormulaInput(ctx, $copyTo, $editor, kcode, preText, refreshRangeS
1622
1795
  }
1623
1796
  if (!$editor) return;
1624
1797
  try {
1798
+ var isBackspaceOrDelete = kcode === 8 || kcode === 46;
1799
+ var isAlphaNumeric = kcode >= 48 && kcode <= 57 || kcode >= 65 && kcode <= 90 || kcode >= 97 && kcode <= 122;
1800
+ if (ctx.formulaCache.rangeSelectionActive === true) {
1801
+ if (isBackspaceOrDelete || isAlphaNumeric) {
1802
+ markRangeSelectionDirty(ctx);
1803
+ }
1804
+ }
1625
1805
  var value1 = void 0;
1626
1806
  var value1txt = preText !== null && preText !== void 0 ? preText : $editor.innerText;
1627
1807
  var value = $editor.innerText;
@@ -1852,61 +2032,181 @@ function functionStrChange_range(txt, type, rc, orient, stindex, step) {
1852
2032
  }
1853
2033
  return "";
1854
2034
  }
2035
+ function setRangeSetValueToFromCaretPosition(ctx, editor, sel) {
2036
+ if (sel.rangeCount === 0 || !sel.anchorNode) return false;
2037
+ if (!editor.contains(sel.anchorNode)) return false;
2038
+ var range = sel.getRangeAt(0).cloneRange();
2039
+ range.collapse(true);
2040
+ var startContainer = range.startContainer,
2041
+ startOffset = range.startOffset;
2042
+ if (startContainer.nodeType === Node.TEXT_NODE) {
2043
+ if (startOffset === 0) {
2044
+ var textParent = startContainer.parentElement;
2045
+ if (textParent === editor) {
2046
+ var prev = startContainer.previousSibling;
2047
+ if (prev) {
2048
+ ctx.formulaCache.rangeSetValueTo = prev;
2049
+ return true;
2050
+ }
2051
+ return false;
2052
+ }
2053
+ var el = textParent;
2054
+ while (el && el !== editor) {
2055
+ if (el.previousSibling) {
2056
+ ctx.formulaCache.rangeSetValueTo = el.previousSibling;
2057
+ return true;
2058
+ }
2059
+ el = el.parentElement;
2060
+ }
2061
+ return false;
2062
+ }
2063
+ var p = startContainer.parentElement;
2064
+ if (p && editor.contains(p)) {
2065
+ ctx.formulaCache.rangeSetValueTo = p;
2066
+ return true;
2067
+ }
2068
+ return false;
2069
+ }
2070
+ if (startContainer.nodeType === Node.ELEMENT_NODE && startContainer === editor) {
2071
+ if (startOffset > 0) {
2072
+ var prev = startContainer.childNodes[startOffset - 1];
2073
+ if (prev) {
2074
+ ctx.formulaCache.rangeSetValueTo = prev;
2075
+ return true;
2076
+ }
2077
+ }
2078
+ return false;
2079
+ }
2080
+ if (startContainer.nodeType === Node.ELEMENT_NODE) {
2081
+ if (startOffset > 0) {
2082
+ var prev = startContainer.childNodes[startOffset - 1];
2083
+ if (prev) {
2084
+ ctx.formulaCache.rangeSetValueTo = prev;
2085
+ return true;
2086
+ }
2087
+ }
2088
+ var el = startContainer;
2089
+ while (el && el !== editor) {
2090
+ if (el.previousSibling) {
2091
+ ctx.formulaCache.rangeSetValueTo = el.previousSibling;
2092
+ return true;
2093
+ }
2094
+ el = el.parentElement;
2095
+ }
2096
+ }
2097
+ return false;
2098
+ }
1855
2099
  function israngeseleciton(ctx, istooltip) {
1856
- var _a, _b, _c;
2100
+ var _a, _b, _c, _d;
1857
2101
  if (istooltip == null) {
1858
2102
  istooltip = false;
1859
2103
  }
1860
2104
  var currSelection = window.getSelection();
1861
- if (currSelection == null) return false;
2105
+ if (currSelection == null) {
2106
+ return false;
2107
+ }
1862
2108
  var anchor = currSelection.anchorNode;
1863
- if (!(anchor === null || anchor === void 0 ? void 0 : anchor.textContent)) return false;
2109
+ if (!anchor) {
2110
+ return false;
2111
+ }
1864
2112
  var anchorOffset = currSelection.anchorOffset;
1865
2113
  var anchorElement = anchor;
1866
2114
  var parentElement = anchor.parentNode;
2115
+ var allowRangeInsertionAtCaret = function allowRangeInsertionAtCaret() {
2116
+ var _a, _b;
2117
+ if (ctx.formulaCache.rangestart || ctx.formulaCache.rangedrag_column_start || ctx.formulaCache.rangedrag_row_start || ctx.formulaCache.rangeSelectionActive === true) {
2118
+ return true;
2119
+ }
2120
+ 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");
2121
+ return isCaretAtValidFormulaRangeInsertionPoint(editor);
2122
+ };
1867
2123
  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);
2124
+ var txt = _lodash.default.trim((_b = anchor.textContent) !== null && _b !== void 0 ? _b : "");
1869
2125
  var lasttxt = "";
1870
2126
  if (txt.length === 0 && anchor.parentNode.previousSibling) {
1871
2127
  var ahr = anchor.parentNode.previousSibling;
1872
2128
  txt = _lodash.default.trim(ahr.textContent || "");
1873
- lasttxt = txt.substring(txt.length - 1, 1);
1874
- ctx.formulaCache.rangeSetValueTo = anchor.parentNode;
2129
+ lasttxt = txt.slice(-1);
1875
2130
  } else {
1876
- lasttxt = txt.substring(anchorOffset - 1, 1);
1877
- ctx.formulaCache.rangeSetValueTo = anchor.parentNode;
2131
+ lasttxt = anchorOffset > 0 ? txt.charAt(anchorOffset - 1) : "";
1878
2132
  }
1879
2133
  if (istooltip && (lasttxt === "(" || lasttxt === ",") || !istooltip && (lasttxt === "(" || lasttxt === "," || lasttxt === "=" || lasttxt in operatorjson || lasttxt === "&")) {
1880
- return true;
2134
+ ctx.formulaCache.rangeSetValueTo = anchor.parentNode;
2135
+ return allowRangeInsertionAtCaret();
1881
2136
  }
1882
2137
  } 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;
2138
+ var editorEl = anchorElement;
2139
+ if (currSelection.rangeCount > 0 && setRangeSetValueToFromCaretPosition(ctx, editorEl, currSelection) && allowRangeInsertionAtCaret()) {
2140
+ return true;
2141
+ }
2142
+ var spans = editorEl.querySelectorAll("span");
2143
+ var txt = _lodash.default.trim((_c = _lodash.default.last(spans)) === null || _c === void 0 ? void 0 : _c.innerText);
2144
+ var refSpan = _lodash.default.last(spans);
2145
+ if (txt.length === 0 && spans.length > 1) {
2146
+ txt = _lodash.default.trim(spans[spans.length - 2].innerText);
2147
+ refSpan = spans[spans.length - 2];
1890
2148
  }
1891
- var lasttxt = txt.substring(txt.length - 1, 1);
2149
+ var lasttxt = txt.slice(-1);
1892
2150
  if (istooltip && (lasttxt === "(" || lasttxt === ",") || !istooltip && (lasttxt === "(" || lasttxt === "," || lasttxt === "=" || lasttxt in operatorjson || lasttxt === "&")) {
1893
- return true;
2151
+ ctx.formulaCache.rangeSetValueTo = refSpan;
2152
+ return allowRangeInsertionAtCaret();
1894
2153
  }
1895
2154
  } else if (parentElement.id === "luckysheet-rich-text-editor" || parentElement.id === "luckysheet-functionbox-cell" || anchorOffset === 0) {
1896
2155
  if (anchorOffset === 0) {
1897
2156
  anchor = anchor.parentNode;
1898
2157
  }
1899
- if (!anchor) return false;
1900
- if (((_c = anchor.previousSibling) === null || _c === void 0 ? void 0 : _c.textContent) == null) return false;
2158
+ if (!anchor) {
2159
+ return false;
2160
+ }
2161
+ if (((_d = anchor.previousSibling) === null || _d === void 0 ? void 0 : _d.textContent) == null) {
2162
+ return false;
2163
+ }
1901
2164
  if (anchor.previousSibling) {
1902
2165
  var txt = _lodash.default.trim(anchor.previousSibling.textContent);
1903
- var lasttxt = txt.substring(txt.length - 1, 1);
1904
- ctx.formulaCache.rangeSetValueTo = anchor.previousSibling;
2166
+ var lasttxt = txt.slice(-1);
1905
2167
  if (istooltip && (lasttxt === "(" || lasttxt === ",") || !istooltip && (lasttxt === "(" || lasttxt === "," || lasttxt === "=" || lasttxt in operatorjson || lasttxt === "&")) {
1906
- return true;
2168
+ ctx.formulaCache.rangeSetValueTo = anchor.previousSibling;
2169
+ return allowRangeInsertionAtCaret();
1907
2170
  }
1908
2171
  }
1909
2172
  }
2173
+ if (!istooltip && (ctx.formulaCache.rangestart || ctx.formulaCache.rangedrag_column_start || ctx.formulaCache.rangedrag_row_start || ctx.formulaCache.rangeSelectionActive === true)) {
2174
+ var editor = document.getElementById("luckysheet-rich-text-editor") || document.getElementById("luckysheet-functionbox-cell");
2175
+ if (editor && currSelection.rangeCount > 0 && setRangeSetValueToFromCaretPosition(ctx, editor, currSelection) && allowRangeInsertionAtCaret()) {
2176
+ return true;
2177
+ }
2178
+ }
2179
+ return false;
2180
+ }
2181
+ function isFormulaReferenceInputMode(ctx) {
2182
+ var editor = document.getElementById("luckysheet-rich-text-editor");
2183
+ var inputText = ((editor === null || editor === void 0 ? void 0 : editor.innerText) || "").trim();
2184
+ var hasRangeToken = (editor === null || editor === void 0 ? void 0 : editor.querySelector("span.fortune-formula-functionrange-cell")) != null;
2185
+ if (!!ctx.formulaCache.rangestart || !!ctx.formulaCache.rangedrag_column_start || !!ctx.formulaCache.rangedrag_row_start || hasRangeToken || ctx.formulaCache.rangeSelectionActive === true) {
2186
+ return true;
2187
+ }
2188
+ if (!inputText.startsWith("=")) {
2189
+ return false;
2190
+ }
2191
+ if (/^=\s*[A-Za-z_][A-Za-z0-9_]*$/.test(inputText)) {
2192
+ return false;
2193
+ }
2194
+ return israngeseleciton(ctx);
2195
+ }
2196
+ function maybeRecoverDirtyRangeSelection(ctx) {
2197
+ if (ctx.formulaCache.rangeSelectionActive !== false) {
2198
+ return false;
2199
+ }
2200
+ var editor = document.getElementById("luckysheet-rich-text-editor");
2201
+ if (!editor) {
2202
+ return false;
2203
+ }
2204
+ var inputText = (editor.innerText || "").trim();
2205
+ var atCaretRangeIndex = getFormulaRangeIndexAtCaret(editor);
2206
+ if (inputText.startsWith("=") && atCaretRangeIndex === null && israngeseleciton(ctx)) {
2207
+ ctx.formulaCache.rangeSelectionActive = null;
2208
+ return true;
2209
+ }
1910
2210
  return false;
1911
2211
  }
1912
2212
  function functionStrChange(txt, type, rc, orient, stindex, step) {
@@ -2004,18 +2304,22 @@ function functionStrChange(txt, type, rc, orient, stindex, step) {
2004
2304
  return function_str;
2005
2305
  }
2006
2306
  function rangeSetValue(ctx, cellInput, selected, fxInput) {
2007
- var _a, _b, _c, _d, _e;
2307
+ var _a, _b, _c, _d, _e, _f;
2008
2308
  var parser = new DOMParser();
2009
2309
  var doc = parser.parseFromString("<div>".concat(cellInput.innerHTML, "</div>"), "text/html");
2010
2310
  var spans = doc.querySelectorAll("span");
2011
2311
  var lastSpan = spans[spans.length - 1];
2012
- if (lastSpan && (0, _index.isLetterNumberPattern)(lastSpan === null || lastSpan === void 0 ? void 0 : lastSpan.innerText)) {
2013
- var htmlR = (0, _index.removeLastSpan)(cellInput.innerHTML);
2312
+ 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;
2313
+ if (lastSpan && (0, _utils.isLetterNumberPattern)(lastSpan === null || lastSpan === void 0 ? void 0 : lastSpan.innerText) && !isManagedRangeSpan) {
2314
+ var htmlR = (0, _utils.removeLastSpan)(cellInput.innerHTML);
2014
2315
  cellInput.innerHTML = "".concat(htmlR);
2316
+ cellInput.focus();
2317
+ var kids = cellInput.childNodes;
2318
+ ctx.formulaCache.rangeSetValueTo = kids.length > 0 ? kids[kids.length - 1] : undefined;
2015
2319
  }
2016
2320
  var $editor = cellInput;
2017
2321
  var $copyTo = fxInput;
2018
- if (((_a = document.activeElement) === null || _a === void 0 ? void 0 : _a.id) === "luckysheet-functionbox-cell") {
2322
+ if (getFormulaEditorOwner(ctx) === "fx") {
2019
2323
  $editor = fxInput;
2020
2324
  $copyTo = cellInput;
2021
2325
  }
@@ -2030,20 +2334,23 @@ function rangeSetValue(ctx, cellInput, selected, fxInput) {
2030
2334
  } else {
2031
2335
  range = (0, _cell.getRangetxt)(ctx, ctx.currentSheetId, selected, ctx.formulaCache.rangetosheet);
2032
2336
  }
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
- }
2337
+ var activeRangeFlow = ctx.formulaCache.rangestart || ctx.formulaCache.rangedrag_column_start || ctx.formulaCache.rangedrag_row_start || ctx.formulaCache.rangeSelectionActive === true;
2338
+ var spanToReplace = !_lodash.default.isNil(ctx.formulaCache.rangechangeindex) ? $editor.querySelector("span[rangeindex='".concat(ctx.formulaCache.rangechangeindex, "']")) : null;
2339
+ if (activeRangeFlow && spanToReplace) {
2340
+ spanToReplace.innerHTML = range;
2341
+ setCaretPosition(ctx, spanToReplace, 0, range.length);
2039
2342
  } else {
2040
2343
  var function_str = "<span class=\"fortune-formula-functionrange-cell\" rangeindex=\"".concat(functionHTMLIndex, "\" dir=\"auto\" style=\"color:").concat(_color.colors[functionHTMLIndex], ";\">").concat(range, "</span>");
2041
2344
  var newEle = parseElement(function_str);
2042
2345
  var refEle = ctx.formulaCache.rangeSetValueTo;
2346
+ if (refEle && !refEle.parentNode) {
2347
+ israngeseleciton(ctx);
2348
+ refEle = ctx.formulaCache.rangeSetValueTo;
2349
+ }
2043
2350
  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);
2351
+ var leftPar = (_c = document.getElementsByClassName("luckysheet-formula-text-lpar")) === null || _c === void 0 ? void 0 : _c[0];
2352
+ if ((_d = leftPar === null || leftPar === void 0 ? void 0 : leftPar.parentElement) === null || _d === void 0 ? void 0 : _d.classList.contains("luckysheet-formula-text-color")) {
2353
+ (_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);
2047
2354
  } else {
2048
2355
  refEle.parentNode.insertBefore(newEle, refEle.nextSibling);
2049
2356
  }
@@ -2083,6 +2390,7 @@ function setRangeSelect(container, left, top, height, width) {
2083
2390
  rangeElement.style.width = "".concat(width, "px");
2084
2391
  }
2085
2392
  function rangeDrag(ctx, e, cellInput, scrollLeft, scrollTop, container, fxInput) {
2393
+ ctx.formulaCache.rangeSelectionActive = true;
2086
2394
  var func_selectedrange = ctx.formulaCache.func_selectedrange;
2087
2395
  if (!func_selectedrange || func_selectedrange.left == null || func_selectedrange.height == null || func_selectedrange.top == null || func_selectedrange.width == null) return;
2088
2396
  var rect = container.getBoundingClientRect();
@@ -2138,6 +2446,20 @@ function rangeDrag(ctx, e, cellInput, scrollLeft, scrollTop, container, fxInput)
2138
2446
  func_selectedrange.width_move = width;
2139
2447
  func_selectedrange.top_move = top;
2140
2448
  func_selectedrange.height_move = height;
2449
+ ctx.luckysheet_select_save = [{
2450
+ row: [rowseleted[0], rowseleted[1]],
2451
+ column: [columnseleted[0], columnseleted[1]],
2452
+ row_focus: row_index,
2453
+ column_focus: col_index,
2454
+ left: left,
2455
+ top: top,
2456
+ width: width,
2457
+ height: height,
2458
+ left_move: left,
2459
+ top_move: top,
2460
+ width_move: width,
2461
+ height_move: height
2462
+ }];
2141
2463
  rangeSetValue(ctx, cellInput, {
2142
2464
  row: rowseleted,
2143
2465
  column: columnseleted
@@ -2146,6 +2468,7 @@ function rangeDrag(ctx, e, cellInput, scrollLeft, scrollTop, container, fxInput)
2146
2468
  e.preventDefault();
2147
2469
  }
2148
2470
  function rangeDragColumn(ctx, e, cellInput, scrollLeft, scrollTop, container, fxInput) {
2471
+ ctx.formulaCache.rangeSelectionActive = true;
2149
2472
  var func_selectedrange = ctx.formulaCache.func_selectedrange;
2150
2473
  if (!func_selectedrange || func_selectedrange.left == null || func_selectedrange.height == null || func_selectedrange.top == null || func_selectedrange.width == null) return;
2151
2474
  var mouse = (0, _location.mousePosition)(e.pageX, e.pageY, ctx);
@@ -2181,6 +2504,20 @@ function rangeDragColumn(ctx, e, cellInput, scrollLeft, scrollTop, container, fx
2181
2504
  func_selectedrange.column = columnseleted;
2182
2505
  func_selectedrange.left_move = left;
2183
2506
  func_selectedrange.width_move = width;
2507
+ ctx.luckysheet_select_save = [{
2508
+ row: [0, row_index],
2509
+ column: [columnseleted[0], columnseleted[1]],
2510
+ row_focus: 0,
2511
+ column_focus: col_index,
2512
+ left: left,
2513
+ top: row_pre,
2514
+ width: width,
2515
+ height: row - row_pre - 1,
2516
+ left_move: left,
2517
+ top_move: row_pre,
2518
+ width_move: width,
2519
+ height_move: row - row_pre - 1
2520
+ }];
2184
2521
  rangeSetValue(ctx, cellInput, {
2185
2522
  row: [null, null],
2186
2523
  column: columnseleted
@@ -2188,6 +2525,7 @@ function rangeDragColumn(ctx, e, cellInput, scrollLeft, scrollTop, container, fx
2188
2525
  setRangeSelect(container, left, row_pre, row - row_pre - 1, width);
2189
2526
  }
2190
2527
  function rangeDragRow(ctx, e, cellInput, scrollLeft, scrollTop, container, fxInput) {
2528
+ ctx.formulaCache.rangeSelectionActive = true;
2191
2529
  var func_selectedrange = ctx.formulaCache.func_selectedrange;
2192
2530
  if (!func_selectedrange || func_selectedrange.left == null || func_selectedrange.height == null || func_selectedrange.top == null || func_selectedrange.width == null) return;
2193
2531
  var mouse = (0, _location.mousePosition)(e.pageX, e.pageY, ctx);
@@ -2223,6 +2561,20 @@ function rangeDragRow(ctx, e, cellInput, scrollLeft, scrollTop, container, fxInp
2223
2561
  func_selectedrange.row = rowseleted;
2224
2562
  func_selectedrange.top_move = top;
2225
2563
  func_selectedrange.height_move = height;
2564
+ ctx.luckysheet_select_save = [{
2565
+ row: [rowseleted[0], rowseleted[1]],
2566
+ column: [0, col_index],
2567
+ row_focus: row_index,
2568
+ column_focus: 0,
2569
+ left: col_pre,
2570
+ top: top,
2571
+ width: col - col_pre - 1,
2572
+ height: height,
2573
+ left_move: col_pre,
2574
+ top_move: top,
2575
+ width_move: col - col_pre - 1,
2576
+ height_move: height
2577
+ }];
2226
2578
  rangeSetValue(ctx, cellInput, {
2227
2579
  row: rowseleted,
2228
2580
  column: [null, null]