@fileverse-dev/fortune-core 1.3.11-input-ref → 1.3.11-input-ref-2

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.
@@ -21,14 +21,13 @@ var __spreadArray = this && this.__spreadArray || function (to, from, pack) {
21
21
  import { Parser, ERROR_REF } from "@fileverse-dev/formula-parser";
22
22
  import _ from "lodash";
23
23
  import { getFlowdata } from "../context";
24
- import { columnCharToIndex, escapeScriptTag, getSheetIndex, indexToColumnChar, getSheetIdByName, escapeHTMLTag } from "../utils";
24
+ import { columnCharToIndex, escapeScriptTag, getSheetIndex, indexToColumnChar, getSheetIdByName, escapeHTMLTag, isLetterNumberPattern, removeLastSpan } from "../utils";
25
25
  import { getcellFormula, getRangetxt, mergeMoveMain, setCellValue } from "./cell";
26
26
  import { error, detectErrorFromValue } from "./validation";
27
27
  import { locale } from "../locale";
28
28
  import { colors } from "./color";
29
29
  import { colLocation, mousePosition, rowLocation } from "./location";
30
30
  import { cancelFunctionrangeSelected, clearCellError, seletedHighlistByindex, setCellError, spillSortResult } from ".";
31
- import { isLetterNumberPattern, removeLastSpan } from "../utils/index";
32
31
  var functionHTMLIndex = 0;
33
32
  var rangeIndexes = [];
34
33
  var operatorPriority = {
@@ -56,6 +55,7 @@ var FormulaCache = function () {
56
55
  this.data_parm_index = 0;
57
56
  this.selectingRangeIndex = -1;
58
57
  this.rangeSelectionActive = null;
58
+ this.formulaEditorOwner = null;
59
59
  this.functionlistMap = {};
60
60
  this.execFunctionGlobalData = {};
61
61
  this.cellTextToIndexList = {};
@@ -1630,6 +1630,115 @@ export function getFormulaRangeIndexAtCaret($editor) {
1630
1630
  var n = parseInt(ri, 10);
1631
1631
  return Number.isNaN(n) ? null : n;
1632
1632
  }
1633
+ export function setFormulaEditorOwner(ctx, owner) {
1634
+ ctx.formulaCache.formulaEditorOwner = owner;
1635
+ }
1636
+ export function getFormulaEditorOwner(ctx) {
1637
+ var _a, _b;
1638
+ var cachedOwner = ctx.formulaCache.formulaEditorOwner;
1639
+ if (cachedOwner === "cell" || cachedOwner === "fx") {
1640
+ return cachedOwner;
1641
+ }
1642
+ if (((_a = document.activeElement) === null || _a === void 0 ? void 0 : _a.id) === "luckysheet-functionbox-cell") {
1643
+ return "fx";
1644
+ }
1645
+ if (((_b = document.activeElement) === null || _b === void 0 ? void 0 : _b.id) === "luckysheet-rich-text-editor") {
1646
+ return "cell";
1647
+ }
1648
+ return null;
1649
+ }
1650
+ function getActiveFormulaEditorElement(ctx) {
1651
+ var _a;
1652
+ var cellEditor = document.getElementById("luckysheet-rich-text-editor");
1653
+ var fxEditor = document.getElementById("luckysheet-functionbox-cell");
1654
+ var owner = getFormulaEditorOwner(ctx);
1655
+ if (owner === "fx") return fxEditor !== null && fxEditor !== void 0 ? fxEditor : cellEditor;
1656
+ if (owner === "cell") return cellEditor !== null && cellEditor !== void 0 ? cellEditor : fxEditor;
1657
+ var activeId = (_a = document.activeElement) === null || _a === void 0 ? void 0 : _a.id;
1658
+ if (activeId === "luckysheet-functionbox-cell") return fxEditor !== null && fxEditor !== void 0 ? fxEditor : cellEditor;
1659
+ if (activeId === "luckysheet-rich-text-editor") return cellEditor !== null && cellEditor !== void 0 ? cellEditor : fxEditor;
1660
+ return cellEditor !== null && cellEditor !== void 0 ? cellEditor : fxEditor;
1661
+ }
1662
+ function getCurrentFormulaSlotTextBeforeCaret(editor, caretOffset) {
1663
+ var textBefore = editor.innerText.slice(0, caretOffset);
1664
+ var parts = textBefore.split(/[=,(+\-*/&<>]/);
1665
+ return _.trim(parts[parts.length - 1] || "");
1666
+ }
1667
+ export function hasIncompleteTruncatedCellRangeSyntax(formulaText) {
1668
+ var t = formulaText.replace(/\s/g, "");
1669
+ if (!t.startsWith("=")) return false;
1670
+ if (/[A-Za-z]+\d+:[A-Za-z]+$/i.test(t)) return true;
1671
+ if (/[A-Za-z]+\d+:\s*$/i.test(t)) return true;
1672
+ return false;
1673
+ }
1674
+ export function isBareCellOrRangeOnlyFormula(formulaText) {
1675
+ var t = formulaText.trim();
1676
+ if (!t.startsWith("=")) return false;
1677
+ var body = t.slice(1).trim();
1678
+ if (!body) return false;
1679
+ if (body.includes("(") || body.includes(")")) return false;
1680
+ return iscelldata(body);
1681
+ }
1682
+ export function suppressFormulaRangeSelectionForInitialEdit(ctx) {
1683
+ ctx.formulaCache.rangeSelectionActive = false;
1684
+ ctx.formulaCache.rangestart = false;
1685
+ ctx.formulaCache.rangedrag_column_start = false;
1686
+ ctx.formulaCache.rangedrag_row_start = false;
1687
+ }
1688
+ export function isCaretAtValidFormulaRangeInsertionPoint(editor) {
1689
+ var currSelection = window.getSelection();
1690
+ if (!editor || !currSelection || currSelection.rangeCount === 0) {
1691
+ return false;
1692
+ }
1693
+ var anchorNode = currSelection.anchorNode;
1694
+ if (anchorNode && !editor.contains(anchorNode)) {
1695
+ return false;
1696
+ }
1697
+ var inputText = editor.innerText.trim();
1698
+ if (!inputText.startsWith("=")) {
1699
+ return false;
1700
+ }
1701
+ if (hasIncompleteTruncatedCellRangeSyntax(inputText)) {
1702
+ return false;
1703
+ }
1704
+ if (/^=\s*[A-Za-z_][A-Za-z0-9_]*$/.test(inputText)) {
1705
+ return false;
1706
+ }
1707
+ if (isBareCellOrRangeOnlyFormula(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
+ var atCaret = getFormulaRangeIndexAtCaret(editor);
1723
+ if (atCaret !== null) {
1724
+ return true;
1725
+ }
1726
+ var textBefore = editor.innerText.slice(0, caretOffset).trimEnd();
1727
+ var lastCh = textBefore.slice(-1);
1728
+ if (!lastCh) {
1729
+ return false;
1730
+ }
1731
+ if (lastCh === ")") {
1732
+ return false;
1733
+ }
1734
+ if (/^[=,(+\-*/&%^<>]$/.test(lastCh)) {
1735
+ return true;
1736
+ }
1737
+ return false;
1738
+ }
1739
+ var first = remaining[0];
1740
+ return first === "," || first === ")" || first === "&" || first in operatorjson;
1741
+ }
1633
1742
  function hasCommaOrAnotherRefAfterRangeCell(cell) {
1634
1743
  var _a, _b;
1635
1744
  var n = cell.nextSibling;
@@ -1736,9 +1845,7 @@ export function handleFormulaInput(ctx, $copyTo, $editor, kcode, preText, refres
1736
1845
  functionRange(ctx, $editor, value, value1);
1737
1846
  if (refreshRangeSelect) {
1738
1847
  cancelFunctionrangeSelected(ctx);
1739
- if (kcode !== 46) {
1740
- createRangeHightlight(ctx, value);
1741
- }
1848
+ createRangeHightlight(ctx, value);
1742
1849
  ctx.formulaCache.rangestart = false;
1743
1850
  ctx.formulaCache.rangedrag_column_start = false;
1744
1851
  ctx.formulaCache.rangedrag_row_start = false;
@@ -2024,31 +2131,7 @@ export function israngeseleciton(ctx, istooltip) {
2024
2131
  return true;
2025
2132
  }
2026
2133
  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");
2027
- if (!editor || currSelection.rangeCount === 0) {
2028
- return true;
2029
- }
2030
- var inputText = editor.innerText.trim();
2031
- if (!inputText.startsWith("=")) {
2032
- return false;
2033
- }
2034
- if (/^=\s*[A-Za-z_][A-Za-z0-9_]*$/.test(inputText)) {
2035
- return false;
2036
- }
2037
- var caretRange = currSelection.getRangeAt(0).cloneRange();
2038
- var preCaretRange = document.createRange();
2039
- preCaretRange.selectNodeContents(editor);
2040
- preCaretRange.setEnd(caretRange.endContainer, caretRange.endOffset);
2041
- var caretOffset = preCaretRange.toString().length;
2042
- var textAfter = editor.innerText.slice(caretOffset);
2043
- var remaining = textAfter.replace(/^\s+/, "");
2044
- if (remaining.length === 0) {
2045
- return true;
2046
- }
2047
- var first = remaining[0];
2048
- if (first === "," || first === ")" || first === "&" || first in operatorjson) {
2049
- return true;
2050
- }
2051
- return false;
2134
+ return isCaretAtValidFormulaRangeInsertionPoint(editor);
2052
2135
  };
2053
2136
  if (((_a = anchor === null || anchor === void 0 ? void 0 : anchor.parentNode) === null || _a === void 0 ? void 0 : _a.nodeName.toLowerCase()) === "span" && anchorOffset !== 0) {
2054
2137
  var txt = _.trim((_b = anchor.textContent) !== null && _b !== void 0 ? _b : "");
@@ -2109,12 +2192,15 @@ export function israngeseleciton(ctx, istooltip) {
2109
2192
  return false;
2110
2193
  }
2111
2194
  export function isFormulaReferenceInputMode(ctx) {
2112
- var editor = document.getElementById("luckysheet-rich-text-editor");
2195
+ var editor = getActiveFormulaEditorElement(ctx);
2113
2196
  var inputText = ((editor === null || editor === void 0 ? void 0 : editor.innerText) || "").trim();
2114
- var hasRangeToken = (editor === null || editor === void 0 ? void 0 : editor.querySelector("span.fortune-formula-functionrange-cell")) != null;
2115
- if (!!ctx.formulaCache.rangestart || !!ctx.formulaCache.rangedrag_column_start || !!ctx.formulaCache.rangedrag_row_start || hasRangeToken || ctx.formulaCache.rangeSelectionActive === true) {
2197
+ var refFlowActive = !!ctx.formulaCache.rangestart || !!ctx.formulaCache.rangedrag_column_start || !!ctx.formulaCache.rangedrag_row_start || ctx.formulaCache.rangeSelectionActive === true;
2198
+ if (refFlowActive) {
2116
2199
  return true;
2117
2200
  }
2201
+ if (ctx.formulaCache.rangeSelectionActive === false) {
2202
+ return false;
2203
+ }
2118
2204
  if (!inputText.startsWith("=")) {
2119
2205
  return false;
2120
2206
  }
@@ -2127,12 +2213,16 @@ export function maybeRecoverDirtyRangeSelection(ctx) {
2127
2213
  if (ctx.formulaCache.rangeSelectionActive !== false) {
2128
2214
  return false;
2129
2215
  }
2130
- var editor = document.getElementById("luckysheet-rich-text-editor");
2216
+ var editor = getActiveFormulaEditorElement(ctx);
2131
2217
  if (!editor) {
2132
2218
  return false;
2133
2219
  }
2134
2220
  var inputText = (editor.innerText || "").trim();
2135
- if (inputText.startsWith("=") && israngeseleciton(ctx)) {
2221
+ if (hasIncompleteTruncatedCellRangeSyntax(inputText)) {
2222
+ return false;
2223
+ }
2224
+ var atCaretRangeIndex = getFormulaRangeIndexAtCaret(editor);
2225
+ if (inputText.startsWith("=") && atCaretRangeIndex === null && isCaretAtValidFormulaRangeInsertionPoint(editor) && israngeseleciton(ctx)) {
2136
2226
  ctx.formulaCache.rangeSelectionActive = null;
2137
2227
  return true;
2138
2228
  }
@@ -2233,7 +2323,7 @@ export function functionStrChange(txt, type, rc, orient, stindex, step) {
2233
2323
  return function_str;
2234
2324
  }
2235
2325
  export function rangeSetValue(ctx, cellInput, selected, fxInput) {
2236
- var _a, _b, _c, _d, _e, _f, _g;
2326
+ var _a, _b, _c, _d, _e, _f;
2237
2327
  var parser = new DOMParser();
2238
2328
  var doc = parser.parseFromString("<div>".concat(cellInput.innerHTML, "</div>"), "text/html");
2239
2329
  var spans = doc.querySelectorAll("span");
@@ -2248,7 +2338,7 @@ export function rangeSetValue(ctx, cellInput, selected, fxInput) {
2248
2338
  }
2249
2339
  var $editor = cellInput;
2250
2340
  var $copyTo = fxInput;
2251
- if (((_c = document.activeElement) === null || _c === void 0 ? void 0 : _c.id) === "luckysheet-functionbox-cell") {
2341
+ if (getFormulaEditorOwner(ctx) === "fx") {
2252
2342
  $editor = fxInput;
2253
2343
  $copyTo = cellInput;
2254
2344
  }
@@ -2277,9 +2367,9 @@ export function rangeSetValue(ctx, cellInput, selected, fxInput) {
2277
2367
  refEle = ctx.formulaCache.rangeSetValueTo;
2278
2368
  }
2279
2369
  if (refEle && refEle.parentNode) {
2280
- var leftPar = (_d = document.getElementsByClassName("luckysheet-formula-text-lpar")) === null || _d === void 0 ? void 0 : _d[0];
2281
- if ((_e = leftPar === null || leftPar === void 0 ? void 0 : leftPar.parentElement) === null || _e === void 0 ? void 0 : _e.classList.contains("luckysheet-formula-text-color")) {
2282
- (_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);
2370
+ var leftPar = (_c = document.getElementsByClassName("luckysheet-formula-text-lpar")) === null || _c === void 0 ? void 0 : _c[0];
2371
+ if ((_d = leftPar === null || leftPar === void 0 ? void 0 : leftPar.parentElement) === null || _d === void 0 ? void 0 : _d.classList.contains("luckysheet-formula-text-color")) {
2372
+ (_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);
2283
2373
  } else {
2284
2374
  refEle.parentNode.insertBefore(newEle, refEle.nextSibling);
2285
2375
  }
package/es/types.d.ts CHANGED
@@ -325,6 +325,8 @@ export type Freezen = {
325
325
  };
326
326
  };
327
327
  export type GlobalCache = {
328
+ enteredEditByTyping?: boolean;
329
+ pendingTypeOverCell?: [number, number];
328
330
  verticalScrollLock?: boolean;
329
331
  horizontalScrollLock?: boolean;
330
332
  overwriteCell?: boolean;
@@ -1,6 +1,6 @@
1
1
  import { Context } from "../context";
2
2
  import { GlobalCache } from "../types";
3
- export declare function handleGlobalEnter(ctx: Context, cellInput: HTMLDivElement, e: KeyboardEvent, canvas?: CanvasRenderingContext2D): void;
3
+ export declare function handleGlobalEnter(ctx: Context, cellInput: HTMLDivElement, e: KeyboardEvent, cache: GlobalCache, canvas?: CanvasRenderingContext2D): void;
4
4
  export declare function handleWithCtrlOrMetaKey(ctx: Context, cache: GlobalCache, e: KeyboardEvent, cellInput: HTMLDivElement, fxInput: HTMLDivElement | null | undefined, handleUndo: () => void, handleRedo: () => void): void;
5
5
  export declare function handleArrowKey(ctx: Context, e: KeyboardEvent): void;
6
6
  export declare function handleGlobalKeyDown(ctx: Context, cellInput: HTMLDivElement, fxInput: HTMLDivElement | null | undefined, e: KeyboardEvent, cache: GlobalCache, handleUndo: () => void, handleRedo: () => void, canvas?: CanvasRenderingContext2D): Promise<void>;
@@ -12,12 +12,14 @@ var _2 = require("..");
12
12
  var _context = require("../context");
13
13
  var _cell = require("../modules/cell");
14
14
  var _formula = require("../modules/formula");
15
+ var _inlineString = require("../modules/inline-string");
15
16
  var _selection = require("../modules/selection");
16
17
  var _toolbar = require("../modules/toolbar");
17
18
  var _validation = require("../modules/validation");
18
19
  var _utils = require("../utils");
19
20
  var _copy = require("./copy");
20
21
  var _refresh = require("../modules/refresh");
22
+ var _cursor = require("../modules/cursor");
21
23
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
22
24
  var __awaiter = void 0 && (void 0).__awaiter || function (thisArg, _arguments, P, generator) {
23
25
  function adopt(value) {
@@ -134,11 +136,21 @@ var __generator = void 0 && (void 0).__generator || function (thisArg, body) {
134
136
  };
135
137
  }
136
138
  };
139
+ function clearTypeOverPending(cache) {
140
+ delete cache.pendingTypeOverCell;
141
+ }
142
+ function getTypeOverInitialContent(e) {
143
+ if (e.keyCode === 229) return undefined;
144
+ if (e.ctrlKey || e.metaKey || e.altKey) return undefined;
145
+ if (e.key === "Backspace" || e.key === "Delete") return "";
146
+ if (e.key.length === 1) return e.key;
147
+ return undefined;
148
+ }
137
149
  function isLegacyFormulaRangeMode(ctx) {
138
150
  return !!ctx.formulaCache.rangestart || !!ctx.formulaCache.rangedrag_column_start || !!ctx.formulaCache.rangedrag_row_start || ctx.formulaCache.rangeSelectionActive === true || (0, _formula.israngeseleciton)(ctx);
139
151
  }
140
- function handleGlobalEnter(ctx, cellInput, e, canvas) {
141
- var _a, _b, _c;
152
+ function handleGlobalEnter(ctx, cellInput, e, cache, canvas) {
153
+ var _a, _b, _c, _d;
142
154
  if ((e.altKey || e.metaKey) && ctx.luckysheetCellUpdate.length > 0) {
143
155
  var last = (_a = ctx.luckysheet_select_save) === null || _a === void 0 ? void 0 : _a[ctx.luckysheet_select_save.length - 1];
144
156
  if (last && !_lodash.default.isNil(last.row_focus) && !_lodash.default.isNil(last.column_focus)) {}
@@ -146,33 +158,61 @@ function handleGlobalEnter(ctx, cellInput, e, canvas) {
146
158
  } else if (ctx.luckysheetCellUpdate.length > 0) {
147
159
  var lastCellUpdate = _lodash.default.clone(ctx.luckysheetCellUpdate);
148
160
  (0, _cell.updateCell)(ctx, ctx.luckysheetCellUpdate[0], ctx.luckysheetCellUpdate[1], cellInput, undefined, canvas);
161
+ cache.enteredEditByTyping = false;
162
+ clearTypeOverPending(cache);
149
163
  ctx.luckysheet_select_save = [{
150
164
  row: [lastCellUpdate[0], lastCellUpdate[0]],
151
165
  column: [lastCellUpdate[1], lastCellUpdate[1]],
152
166
  row_focus: lastCellUpdate[0],
153
167
  column_focus: lastCellUpdate[1]
154
168
  }];
155
- (0, _selection.moveHighlightCell)(ctx, "down", (0, _2.hideCRCount)(ctx, "ArrowDown"), "rangeOfSelect");
169
+ var rowStep = e.shiftKey ? -(0, _2.hideCRCount)(ctx, "ArrowUp") : (0, _2.hideCRCount)(ctx, "ArrowDown");
170
+ (0, _selection.moveHighlightCell)(ctx, "down", rowStep, "rangeOfSelect");
156
171
  e.preventDefault();
157
172
  } else {
158
173
  if (((_c = (_b = ctx.luckysheet_select_save) === null || _b === void 0 ? void 0 : _b.length) !== null && _c !== void 0 ? _c : 0) > 0) {
159
174
  var last = ctx.luckysheet_select_save[ctx.luckysheet_select_save.length - 1];
160
175
  var row_index = last.row_focus;
161
176
  var col_index = last.column_focus;
177
+ if (!_lodash.default.isNil(row_index) && !_lodash.default.isNil(col_index)) {
178
+ var flowdata = (0, _context.getFlowdata)(ctx);
179
+ var cellAt = (_d = flowdata === null || flowdata === void 0 ? void 0 : flowdata[row_index]) === null || _d === void 0 ? void 0 : _d[col_index];
180
+ if ((cellAt === null || cellAt === void 0 ? void 0 : cellAt.f) != null && String(cellAt.f).trim() !== "") {
181
+ (0, _formula.suppressFormulaRangeSelectionForInitialEdit)(ctx);
182
+ }
183
+ }
162
184
  ctx.luckysheetCellUpdate = [row_index, col_index];
185
+ cache.enteredEditByTyping = false;
186
+ clearTypeOverPending(cache);
163
187
  e.preventDefault();
164
188
  }
165
189
  }
166
190
  }
191
+ function cellCountsForDataEdge(cell) {
192
+ var _a, _b;
193
+ if (cell == null) return false;
194
+ if (!_lodash.default.isPlainObject(cell)) return !_lodash.default.isNil(cell);
195
+ if (cell.f != null && String(cell.f) !== "") return true;
196
+ if (!_lodash.default.isNil(cell.v)) return true;
197
+ if ((0, _inlineString.isInlineStringCell)(cell)) {
198
+ return ((_b = (_a = cell.ct) === null || _a === void 0 ? void 0 : _a.s) !== null && _b !== void 0 ? _b : []).some(function (seg) {
199
+ return (seg === null || seg === void 0 ? void 0 : seg.v) != null && String(seg.v).length > 0;
200
+ });
201
+ }
202
+ return false;
203
+ }
167
204
  function moveToEdge(sheetData, key, curr, rowDelta, colDelta, startR, endR, startC, endC, maxRow, maxCol) {
168
- var _a, _b, _c, _d, _e, _f;
205
+ var _a, _b, _c;
169
206
  var selectedLimit = -1;
170
207
  if (key === "ArrowUp") selectedLimit = startR - 1;else if (key === "ArrowDown") selectedLimit = endR + 1;else if (key === "ArrowLeft") selectedLimit = startC - 1;else if (key === "ArrowRight") selectedLimit = endC + 1;
171
208
  var maxRowCol = colDelta === 0 ? maxRow : maxCol;
172
209
  var r = colDelta === 0 ? selectedLimit : curr;
173
210
  var c = colDelta === 0 ? curr : selectedLimit;
174
211
  while (r >= 0 && c >= 0 && (colDelta === 0 ? r : c) < maxRowCol - 1) {
175
- if (!_lodash.default.isNil((_b = (_a = sheetData === null || sheetData === void 0 ? void 0 : sheetData[r]) === null || _a === void 0 ? void 0 : _a[c]) === null || _b === void 0 ? void 0 : _b.v) && (_lodash.default.isNil((_d = (_c = sheetData === null || sheetData === void 0 ? void 0 : sheetData[r - rowDelta]) === null || _c === void 0 ? void 0 : _c[c - colDelta]) === null || _d === void 0 ? void 0 : _d.v) || _lodash.default.isNil((_f = (_e = sheetData === null || sheetData === void 0 ? void 0 : sheetData[r + rowDelta]) === null || _e === void 0 ? void 0 : _e[c + colDelta]) === null || _f === void 0 ? void 0 : _f.v))) {
212
+ var here = (_a = sheetData === null || sheetData === void 0 ? void 0 : sheetData[r]) === null || _a === void 0 ? void 0 : _a[c];
213
+ var behind = (_b = sheetData === null || sheetData === void 0 ? void 0 : sheetData[r - rowDelta]) === null || _b === void 0 ? void 0 : _b[c - colDelta];
214
+ var ahead = (_c = sheetData === null || sheetData === void 0 ? void 0 : sheetData[r + rowDelta]) === null || _c === void 0 ? void 0 : _c[c + colDelta];
215
+ if (cellCountsForDataEdge(here) && (!cellCountsForDataEdge(behind) || !cellCountsForDataEdge(ahead))) {
176
216
  break;
177
217
  } else {
178
218
  r += 1 * rowDelta;
@@ -181,10 +221,29 @@ function moveToEdge(sheetData, key, curr, rowDelta, colDelta, startR, endR, star
181
221
  }
182
222
  return colDelta === 0 ? r : c;
183
223
  }
184
- function handleControlPlusArrowKey(ctx, e, shiftPressed) {
185
- if (ctx.formulaCache.rangeSelectionActive === false && !(0, _formula.maybeRecoverDirtyRangeSelection)(ctx)) {
186
- return;
224
+ function isPlainTextCellOrFxEdit(ctx, cellInput, fxInput) {
225
+ var _a, _b, _c;
226
+ if (ctx.luckysheetCellUpdate.length === 0) return false;
227
+ var cellT = ((_a = cellInput === null || cellInput === void 0 ? void 0 : cellInput.innerText) !== null && _a !== void 0 ? _a : "").trim();
228
+ var fxT = ((_b = fxInput === null || fxInput === void 0 ? void 0 : fxInput.innerText) !== null && _b !== void 0 ? _b : "").trim();
229
+ var owner = (0, _formula.getFormulaEditorOwner)(ctx);
230
+ if (owner === "fx" && fxInput) {
231
+ return !fxT.startsWith("=");
187
232
  }
233
+ if (owner === "cell") {
234
+ return !cellT.startsWith("=");
235
+ }
236
+ var aid = (_c = document.activeElement) === null || _c === void 0 ? void 0 : _c.id;
237
+ if (aid === "luckysheet-functionbox-cell" && fxInput) {
238
+ return !fxT.startsWith("=");
239
+ }
240
+ if (aid === "luckysheet-rich-text-editor") {
241
+ return !cellT.startsWith("=");
242
+ }
243
+ if (cellT.startsWith("=") || fxT.startsWith("=")) return false;
244
+ return true;
245
+ }
246
+ function handleControlPlusArrowKey(ctx, e, shiftPressed) {
188
247
  var isFormulaRefMode = isLegacyFormulaRangeMode(ctx);
189
248
  if (isFormulaRefMode) {
190
249
  ctx.formulaCache.rangeSelectionActive = true;
@@ -258,6 +317,9 @@ function handleWithCtrlOrMetaKey(ctx, cache, e, cellInput, fxInput, handleUndo,
258
317
  var _a, _b, _c, _d;
259
318
  var flowdata = (0, _context.getFlowdata)(ctx);
260
319
  if (!flowdata) return;
320
+ if ((e.ctrlKey || e.metaKey) && ["ArrowUp", "ArrowDown", "ArrowLeft", "ArrowRight"].includes(e.key) && isPlainTextCellOrFxEdit(ctx, cellInput, fxInput)) {
321
+ return;
322
+ }
261
323
  if (e.shiftKey) {
262
324
  ctx.luckysheet_shiftpositon = _lodash.default.cloneDeep((_a = ctx.luckysheet_select_save) === null || _a === void 0 ? void 0 : _a[ctx.luckysheet_select_save.length - 1]);
263
325
  ctx.luckysheet_shiftkeydown = true;
@@ -270,6 +332,8 @@ function handleWithCtrlOrMetaKey(ctx, cache, e, cellInput, fxInput, handleUndo,
270
332
  var col_index = last.column_focus;
271
333
  (0, _cell.updateCell)(ctx, row_index, col_index, cellInput);
272
334
  ctx.luckysheetCellUpdate = [row_index, col_index];
335
+ cache.enteredEditByTyping = false;
336
+ clearTypeOverPending(cache);
273
337
  cache.ignoreWriteCell = true;
274
338
  var value = (0, _utils.getNowDateTime)(2);
275
339
  cellInput.innerText = value;
@@ -406,10 +470,10 @@ function handleArrowKey(ctx, e) {
406
470
  e.preventDefault();
407
471
  }
408
472
  function handleGlobalKeyDown(ctx, cellInput, fxInput, e, cache, handleUndo, handleRedo, canvas) {
409
- var _a, _b, _c;
473
+ var _a, _b, _c, _d, _e, _f, _g;
410
474
  return __awaiter(this, void 0, void 0, function () {
411
- var handledFlvShortcut, kcode, kstr, allowEdit, isFxInput, ignoredKeys, restCod, last, row_index, col_index, last, row_index, col_index;
412
- return __generator(this, function (_d) {
475
+ var handledFlvShortcut, kcode, kstr, allowEdit, isFxInput, ignoredKeys, restCod, last, row_index, col_index, flowdataF2, cellF2, isEditing, inlineText, fxText, isFormulaEdit, enteredByTyping, last, row_index, col_index, flowdata, cellAt, existingFormula, initial;
476
+ return __generator(this, function (_h) {
413
477
  if (e.shiftKey && e.code === "Space") {
414
478
  e.stopImmediatePropagation();
415
479
  e.stopPropagation();
@@ -470,10 +534,12 @@ function handleGlobalKeyDown(ctx, cellInput, fxInput, e, cache, handleUndo, hand
470
534
  }
471
535
  if (kstr === "Enter") {
472
536
  if (!allowEdit) return [2];
473
- handleGlobalEnter(ctx, cellInput, e, canvas);
537
+ handleGlobalEnter(ctx, cellInput, e, cache, canvas);
474
538
  } else if (kstr === "Tab") {
475
539
  if (ctx.luckysheetCellUpdate.length > 0) {
476
540
  (0, _cell.updateCell)(ctx, ctx.luckysheetCellUpdate[0], ctx.luckysheetCellUpdate[1], cellInput, undefined, canvas);
541
+ cache.enteredEditByTyping = false;
542
+ clearTypeOverPending(cache);
477
543
  }
478
544
  if (e.shiftKey) {
479
545
  (0, _selection.moveHighlightCell)(ctx, "right", -(0, _2.hideCRCount)(ctx, "ArrowLeft"), "rangeOfSelect");
@@ -490,11 +556,22 @@ function handleGlobalKeyDown(ctx, cellInput, fxInput, e, cache, handleUndo, hand
490
556
  if (!last) return [2];
491
557
  row_index = last.row_focus;
492
558
  col_index = last.column_focus;
559
+ if (!_lodash.default.isNil(row_index) && !_lodash.default.isNil(col_index)) {
560
+ flowdataF2 = (0, _context.getFlowdata)(ctx);
561
+ cellF2 = (_d = flowdataF2 === null || flowdataF2 === void 0 ? void 0 : flowdataF2[row_index]) === null || _d === void 0 ? void 0 : _d[col_index];
562
+ if ((cellF2 === null || cellF2 === void 0 ? void 0 : cellF2.f) != null && String(cellF2.f).trim() !== "") {
563
+ (0, _formula.suppressFormulaRangeSelectionForInitialEdit)(ctx);
564
+ }
565
+ }
566
+ cache.enteredEditByTyping = false;
567
+ clearTypeOverPending(cache);
493
568
  ctx.luckysheetCellUpdate = [row_index, col_index];
494
569
  e.preventDefault();
495
570
  } else if (kstr === "F4" && ctx.luckysheetCellUpdate.length > 0) {
496
571
  e.preventDefault();
497
572
  } else if (kstr === "Escape" && ctx.luckysheetCellUpdate.length > 0) {
573
+ cache.enteredEditByTyping = false;
574
+ clearTypeOverPending(cache);
498
575
  (0, _cell.cancelNormalSelected)(ctx);
499
576
  (0, _selection.moveHighlightCell)(ctx, "down", 0, "rangeOfSelect");
500
577
  e.preventDefault();
@@ -520,16 +597,53 @@ function handleGlobalKeyDown(ctx, cellInput, fxInput, e, cache, handleUndo, hand
520
597
  (0, _refresh.jfrefreshgrid)(ctx, null, undefined);
521
598
  e.preventDefault();
522
599
  } else if (kstr === "ArrowUp" || kstr === "ArrowDown" || kstr === "ArrowLeft" || kstr === "ArrowRight") {
523
- handleArrowKey(ctx, e);
600
+ isEditing = ctx.luckysheetCellUpdate.length > 0;
601
+ inlineText = (_e = cellInput === null || cellInput === void 0 ? void 0 : cellInput.innerText) !== null && _e !== void 0 ? _e : "";
602
+ fxText = (_f = fxInput === null || fxInput === void 0 ? void 0 : fxInput.innerText) !== null && _f !== void 0 ? _f : "";
603
+ isFormulaEdit = isEditing && (inlineText.trim().startsWith("=") || fxText.trim().startsWith("="));
604
+ enteredByTyping = cache.enteredEditByTyping === true;
605
+ if (isEditing && !isFormulaEdit && enteredByTyping && !e.shiftKey) {
606
+ (0, _cell.updateCell)(ctx, ctx.luckysheetCellUpdate[0], ctx.luckysheetCellUpdate[1], cellInput, undefined, canvas);
607
+ cache.enteredEditByTyping = false;
608
+ clearTypeOverPending(cache);
609
+ handleArrowKey(ctx, e);
610
+ e.preventDefault();
611
+ } else {
612
+ handleArrowKey(ctx, e);
613
+ }
524
614
  } else if (!(kcode >= 112 && kcode <= 123 || kcode <= 46 || kcode === 144 || kcode === 108 || e.ctrlKey || e.altKey || e.shiftKey && (kcode === 37 || kcode === 38 || kcode === 39 || kcode === 40)) || kcode === 8 || kcode === 32 || kcode === 46 || kcode === 0 || e.ctrlKey && kcode === 86) {
525
615
  if (!allowEdit) return [2];
526
616
  if (String.fromCharCode(kcode) != null && !_lodash.default.isEmpty(ctx.luckysheet_select_save) && kstr !== "CapsLock" && kstr !== "Win" && kcode !== 18) {
527
617
  last = ctx.luckysheet_select_save[ctx.luckysheet_select_save.length - 1];
528
618
  row_index = last.row_focus;
529
619
  col_index = last.column_focus;
620
+ if (_lodash.default.isNil(row_index) || _lodash.default.isNil(col_index)) return [2];
621
+ flowdata = (0, _context.getFlowdata)(ctx);
622
+ cellAt = (_g = flowdata === null || flowdata === void 0 ? void 0 : flowdata[row_index]) === null || _g === void 0 ? void 0 : _g[col_index];
623
+ existingFormula = (cellAt === null || cellAt === void 0 ? void 0 : cellAt.f) != null && String(cellAt.f).trim() !== "" ? String(cellAt.f).replace(/[\r\n]/g, "") : null;
624
+ if (existingFormula != null) {
625
+ (0, _formula.suppressFormulaRangeSelectionForInitialEdit)(ctx);
626
+ }
530
627
  ctx.luckysheetCellUpdate = [row_index, col_index];
531
628
  cache.overwriteCell = true;
532
- (0, _formula.handleFormulaInput)(ctx, fxInput, cellInput, kcode);
629
+ cache.pendingTypeOverCell = [row_index, col_index];
630
+ (0, _formula.setFormulaEditorOwner)(ctx, "cell");
631
+ cache.enteredEditByTyping = true;
632
+ cellInput.focus();
633
+ initial = getTypeOverInitialContent(e);
634
+ if (initial !== undefined) {
635
+ cellInput.textContent = initial;
636
+ if (fxInput) fxInput.textContent = initial;
637
+ (0, _formula.handleFormulaInput)(ctx, fxInput, cellInput, kcode);
638
+ e.preventDefault();
639
+ } else {
640
+ cellInput.textContent = "";
641
+ if (fxInput) fxInput.textContent = "";
642
+ (0, _formula.handleFormulaInput)(ctx, fxInput, cellInput, kcode);
643
+ }
644
+ queueMicrotask(function () {
645
+ (0, _cursor.moveToEnd)(cellInput);
646
+ });
533
647
  }
534
648
  }
535
649
  }