@fileverse-dev/fortune-core 1.3.11-input-ref-1 → 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.
@@ -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>;
@@ -117,18 +117,30 @@ import _ from "lodash";
117
117
  import { hideCRCount, removeActiveImage } from "..";
118
118
  import { getFlowdata } from "../context";
119
119
  import { updateCell, cancelNormalSelected } from "../modules/cell";
120
- import { handleFormulaInput, israngeseleciton, maybeRecoverDirtyRangeSelection, markRangeSelectionDirty } from "../modules/formula";
120
+ import { handleFormulaInput, israngeseleciton, maybeRecoverDirtyRangeSelection, markRangeSelectionDirty, setFormulaEditorOwner, getFormulaEditorOwner, suppressFormulaRangeSelectionForInitialEdit } from "../modules/formula";
121
+ import { isInlineStringCell } from "../modules/inline-string";
121
122
  import { copy, deleteSelectedCellText, deleteSelectedCellFormat, textFormat, fillDate, fillTime, fillRightData, fillDownData, moveHighlightCell, moveHighlightRange, selectAll, selectionCache } from "../modules/selection";
122
123
  import { cancelPaintModel, handleBold, handleItalic, handleUnderline, handleLink } from "../modules/toolbar";
123
124
  import { hasPartMC } from "../modules/validation";
124
125
  import { getNowDateTime, getSheetIndex, isAllowEdit } from "../utils";
125
126
  import { handleCopy } from "./copy";
126
127
  import { jfrefreshgrid } from "../modules/refresh";
128
+ import { moveToEnd } from "../modules/cursor";
129
+ function clearTypeOverPending(cache) {
130
+ delete cache.pendingTypeOverCell;
131
+ }
132
+ function getTypeOverInitialContent(e) {
133
+ if (e.keyCode === 229) return undefined;
134
+ if (e.ctrlKey || e.metaKey || e.altKey) return undefined;
135
+ if (e.key === "Backspace" || e.key === "Delete") return "";
136
+ if (e.key.length === 1) return e.key;
137
+ return undefined;
138
+ }
127
139
  function isLegacyFormulaRangeMode(ctx) {
128
140
  return !!ctx.formulaCache.rangestart || !!ctx.formulaCache.rangedrag_column_start || !!ctx.formulaCache.rangedrag_row_start || ctx.formulaCache.rangeSelectionActive === true || israngeseleciton(ctx);
129
141
  }
130
- export function handleGlobalEnter(ctx, cellInput, e, canvas) {
131
- var _a, _b, _c;
142
+ export function handleGlobalEnter(ctx, cellInput, e, cache, canvas) {
143
+ var _a, _b, _c, _d;
132
144
  if ((e.altKey || e.metaKey) && ctx.luckysheetCellUpdate.length > 0) {
133
145
  var last = (_a = ctx.luckysheet_select_save) === null || _a === void 0 ? void 0 : _a[ctx.luckysheet_select_save.length - 1];
134
146
  if (last && !_.isNil(last.row_focus) && !_.isNil(last.column_focus)) {}
@@ -136,33 +148,61 @@ export function handleGlobalEnter(ctx, cellInput, e, canvas) {
136
148
  } else if (ctx.luckysheetCellUpdate.length > 0) {
137
149
  var lastCellUpdate = _.clone(ctx.luckysheetCellUpdate);
138
150
  updateCell(ctx, ctx.luckysheetCellUpdate[0], ctx.luckysheetCellUpdate[1], cellInput, undefined, canvas);
151
+ cache.enteredEditByTyping = false;
152
+ clearTypeOverPending(cache);
139
153
  ctx.luckysheet_select_save = [{
140
154
  row: [lastCellUpdate[0], lastCellUpdate[0]],
141
155
  column: [lastCellUpdate[1], lastCellUpdate[1]],
142
156
  row_focus: lastCellUpdate[0],
143
157
  column_focus: lastCellUpdate[1]
144
158
  }];
145
- moveHighlightCell(ctx, "down", hideCRCount(ctx, "ArrowDown"), "rangeOfSelect");
159
+ var rowStep = e.shiftKey ? -hideCRCount(ctx, "ArrowUp") : hideCRCount(ctx, "ArrowDown");
160
+ moveHighlightCell(ctx, "down", rowStep, "rangeOfSelect");
146
161
  e.preventDefault();
147
162
  } else {
148
163
  if (((_c = (_b = ctx.luckysheet_select_save) === null || _b === void 0 ? void 0 : _b.length) !== null && _c !== void 0 ? _c : 0) > 0) {
149
164
  var last = ctx.luckysheet_select_save[ctx.luckysheet_select_save.length - 1];
150
165
  var row_index = last.row_focus;
151
166
  var col_index = last.column_focus;
167
+ if (!_.isNil(row_index) && !_.isNil(col_index)) {
168
+ var flowdata = getFlowdata(ctx);
169
+ var cellAt = (_d = flowdata === null || flowdata === void 0 ? void 0 : flowdata[row_index]) === null || _d === void 0 ? void 0 : _d[col_index];
170
+ if ((cellAt === null || cellAt === void 0 ? void 0 : cellAt.f) != null && String(cellAt.f).trim() !== "") {
171
+ suppressFormulaRangeSelectionForInitialEdit(ctx);
172
+ }
173
+ }
152
174
  ctx.luckysheetCellUpdate = [row_index, col_index];
175
+ cache.enteredEditByTyping = false;
176
+ clearTypeOverPending(cache);
153
177
  e.preventDefault();
154
178
  }
155
179
  }
156
180
  }
181
+ function cellCountsForDataEdge(cell) {
182
+ var _a, _b;
183
+ if (cell == null) return false;
184
+ if (!_.isPlainObject(cell)) return !_.isNil(cell);
185
+ if (cell.f != null && String(cell.f) !== "") return true;
186
+ if (!_.isNil(cell.v)) return true;
187
+ if (isInlineStringCell(cell)) {
188
+ return ((_b = (_a = cell.ct) === null || _a === void 0 ? void 0 : _a.s) !== null && _b !== void 0 ? _b : []).some(function (seg) {
189
+ return (seg === null || seg === void 0 ? void 0 : seg.v) != null && String(seg.v).length > 0;
190
+ });
191
+ }
192
+ return false;
193
+ }
157
194
  function moveToEdge(sheetData, key, curr, rowDelta, colDelta, startR, endR, startC, endC, maxRow, maxCol) {
158
- var _a, _b, _c, _d, _e, _f;
195
+ var _a, _b, _c;
159
196
  var selectedLimit = -1;
160
197
  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;
161
198
  var maxRowCol = colDelta === 0 ? maxRow : maxCol;
162
199
  var r = colDelta === 0 ? selectedLimit : curr;
163
200
  var c = colDelta === 0 ? curr : selectedLimit;
164
201
  while (r >= 0 && c >= 0 && (colDelta === 0 ? r : c) < maxRowCol - 1) {
165
- if (!_.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) && (_.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) || _.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))) {
202
+ var here = (_a = sheetData === null || sheetData === void 0 ? void 0 : sheetData[r]) === null || _a === void 0 ? void 0 : _a[c];
203
+ var behind = (_b = sheetData === null || sheetData === void 0 ? void 0 : sheetData[r - rowDelta]) === null || _b === void 0 ? void 0 : _b[c - colDelta];
204
+ var ahead = (_c = sheetData === null || sheetData === void 0 ? void 0 : sheetData[r + rowDelta]) === null || _c === void 0 ? void 0 : _c[c + colDelta];
205
+ if (cellCountsForDataEdge(here) && (!cellCountsForDataEdge(behind) || !cellCountsForDataEdge(ahead))) {
166
206
  break;
167
207
  } else {
168
208
  r += 1 * rowDelta;
@@ -171,10 +211,29 @@ function moveToEdge(sheetData, key, curr, rowDelta, colDelta, startR, endR, star
171
211
  }
172
212
  return colDelta === 0 ? r : c;
173
213
  }
174
- function handleControlPlusArrowKey(ctx, e, shiftPressed) {
175
- if (ctx.formulaCache.rangeSelectionActive === false && !maybeRecoverDirtyRangeSelection(ctx)) {
176
- return;
214
+ function isPlainTextCellOrFxEdit(ctx, cellInput, fxInput) {
215
+ var _a, _b, _c;
216
+ if (ctx.luckysheetCellUpdate.length === 0) return false;
217
+ var cellT = ((_a = cellInput === null || cellInput === void 0 ? void 0 : cellInput.innerText) !== null && _a !== void 0 ? _a : "").trim();
218
+ var fxT = ((_b = fxInput === null || fxInput === void 0 ? void 0 : fxInput.innerText) !== null && _b !== void 0 ? _b : "").trim();
219
+ var owner = getFormulaEditorOwner(ctx);
220
+ if (owner === "fx" && fxInput) {
221
+ return !fxT.startsWith("=");
177
222
  }
223
+ if (owner === "cell") {
224
+ return !cellT.startsWith("=");
225
+ }
226
+ var aid = (_c = document.activeElement) === null || _c === void 0 ? void 0 : _c.id;
227
+ if (aid === "luckysheet-functionbox-cell" && fxInput) {
228
+ return !fxT.startsWith("=");
229
+ }
230
+ if (aid === "luckysheet-rich-text-editor") {
231
+ return !cellT.startsWith("=");
232
+ }
233
+ if (cellT.startsWith("=") || fxT.startsWith("=")) return false;
234
+ return true;
235
+ }
236
+ function handleControlPlusArrowKey(ctx, e, shiftPressed) {
178
237
  var isFormulaRefMode = isLegacyFormulaRangeMode(ctx);
179
238
  if (isFormulaRefMode) {
180
239
  ctx.formulaCache.rangeSelectionActive = true;
@@ -248,6 +307,9 @@ export function handleWithCtrlOrMetaKey(ctx, cache, e, cellInput, fxInput, handl
248
307
  var _a, _b, _c, _d;
249
308
  var flowdata = getFlowdata(ctx);
250
309
  if (!flowdata) return;
310
+ if ((e.ctrlKey || e.metaKey) && ["ArrowUp", "ArrowDown", "ArrowLeft", "ArrowRight"].includes(e.key) && isPlainTextCellOrFxEdit(ctx, cellInput, fxInput)) {
311
+ return;
312
+ }
251
313
  if (e.shiftKey) {
252
314
  ctx.luckysheet_shiftpositon = _.cloneDeep((_a = ctx.luckysheet_select_save) === null || _a === void 0 ? void 0 : _a[ctx.luckysheet_select_save.length - 1]);
253
315
  ctx.luckysheet_shiftkeydown = true;
@@ -260,6 +322,8 @@ export function handleWithCtrlOrMetaKey(ctx, cache, e, cellInput, fxInput, handl
260
322
  var col_index = last.column_focus;
261
323
  updateCell(ctx, row_index, col_index, cellInput);
262
324
  ctx.luckysheetCellUpdate = [row_index, col_index];
325
+ cache.enteredEditByTyping = false;
326
+ clearTypeOverPending(cache);
263
327
  cache.ignoreWriteCell = true;
264
328
  var value = getNowDateTime(2);
265
329
  cellInput.innerText = value;
@@ -396,10 +460,10 @@ export function handleArrowKey(ctx, e) {
396
460
  e.preventDefault();
397
461
  }
398
462
  export function handleGlobalKeyDown(ctx, cellInput, fxInput, e, cache, handleUndo, handleRedo, canvas) {
399
- var _a, _b, _c;
463
+ var _a, _b, _c, _d, _e, _f, _g;
400
464
  return __awaiter(this, void 0, void 0, function () {
401
- var handledFlvShortcut, kcode, kstr, allowEdit, isFxInput, ignoredKeys, restCod, last, row_index, col_index, last, row_index, col_index;
402
- return __generator(this, function (_d) {
465
+ 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;
466
+ return __generator(this, function (_h) {
403
467
  if (e.shiftKey && e.code === "Space") {
404
468
  e.stopImmediatePropagation();
405
469
  e.stopPropagation();
@@ -460,10 +524,12 @@ export function handleGlobalKeyDown(ctx, cellInput, fxInput, e, cache, handleUnd
460
524
  }
461
525
  if (kstr === "Enter") {
462
526
  if (!allowEdit) return [2];
463
- handleGlobalEnter(ctx, cellInput, e, canvas);
527
+ handleGlobalEnter(ctx, cellInput, e, cache, canvas);
464
528
  } else if (kstr === "Tab") {
465
529
  if (ctx.luckysheetCellUpdate.length > 0) {
466
530
  updateCell(ctx, ctx.luckysheetCellUpdate[0], ctx.luckysheetCellUpdate[1], cellInput, undefined, canvas);
531
+ cache.enteredEditByTyping = false;
532
+ clearTypeOverPending(cache);
467
533
  }
468
534
  if (e.shiftKey) {
469
535
  moveHighlightCell(ctx, "right", -hideCRCount(ctx, "ArrowLeft"), "rangeOfSelect");
@@ -480,11 +546,22 @@ export function handleGlobalKeyDown(ctx, cellInput, fxInput, e, cache, handleUnd
480
546
  if (!last) return [2];
481
547
  row_index = last.row_focus;
482
548
  col_index = last.column_focus;
549
+ if (!_.isNil(row_index) && !_.isNil(col_index)) {
550
+ flowdataF2 = getFlowdata(ctx);
551
+ cellF2 = (_d = flowdataF2 === null || flowdataF2 === void 0 ? void 0 : flowdataF2[row_index]) === null || _d === void 0 ? void 0 : _d[col_index];
552
+ if ((cellF2 === null || cellF2 === void 0 ? void 0 : cellF2.f) != null && String(cellF2.f).trim() !== "") {
553
+ suppressFormulaRangeSelectionForInitialEdit(ctx);
554
+ }
555
+ }
556
+ cache.enteredEditByTyping = false;
557
+ clearTypeOverPending(cache);
483
558
  ctx.luckysheetCellUpdate = [row_index, col_index];
484
559
  e.preventDefault();
485
560
  } else if (kstr === "F4" && ctx.luckysheetCellUpdate.length > 0) {
486
561
  e.preventDefault();
487
562
  } else if (kstr === "Escape" && ctx.luckysheetCellUpdate.length > 0) {
563
+ cache.enteredEditByTyping = false;
564
+ clearTypeOverPending(cache);
488
565
  cancelNormalSelected(ctx);
489
566
  moveHighlightCell(ctx, "down", 0, "rangeOfSelect");
490
567
  e.preventDefault();
@@ -510,16 +587,53 @@ export function handleGlobalKeyDown(ctx, cellInput, fxInput, e, cache, handleUnd
510
587
  jfrefreshgrid(ctx, null, undefined);
511
588
  e.preventDefault();
512
589
  } else if (kstr === "ArrowUp" || kstr === "ArrowDown" || kstr === "ArrowLeft" || kstr === "ArrowRight") {
513
- handleArrowKey(ctx, e);
590
+ isEditing = ctx.luckysheetCellUpdate.length > 0;
591
+ inlineText = (_e = cellInput === null || cellInput === void 0 ? void 0 : cellInput.innerText) !== null && _e !== void 0 ? _e : "";
592
+ fxText = (_f = fxInput === null || fxInput === void 0 ? void 0 : fxInput.innerText) !== null && _f !== void 0 ? _f : "";
593
+ isFormulaEdit = isEditing && (inlineText.trim().startsWith("=") || fxText.trim().startsWith("="));
594
+ enteredByTyping = cache.enteredEditByTyping === true;
595
+ if (isEditing && !isFormulaEdit && enteredByTyping && !e.shiftKey) {
596
+ updateCell(ctx, ctx.luckysheetCellUpdate[0], ctx.luckysheetCellUpdate[1], cellInput, undefined, canvas);
597
+ cache.enteredEditByTyping = false;
598
+ clearTypeOverPending(cache);
599
+ handleArrowKey(ctx, e);
600
+ e.preventDefault();
601
+ } else {
602
+ handleArrowKey(ctx, e);
603
+ }
514
604
  } 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) {
515
605
  if (!allowEdit) return [2];
516
606
  if (String.fromCharCode(kcode) != null && !_.isEmpty(ctx.luckysheet_select_save) && kstr !== "CapsLock" && kstr !== "Win" && kcode !== 18) {
517
607
  last = ctx.luckysheet_select_save[ctx.luckysheet_select_save.length - 1];
518
608
  row_index = last.row_focus;
519
609
  col_index = last.column_focus;
610
+ if (_.isNil(row_index) || _.isNil(col_index)) return [2];
611
+ flowdata = getFlowdata(ctx);
612
+ cellAt = (_g = flowdata === null || flowdata === void 0 ? void 0 : flowdata[row_index]) === null || _g === void 0 ? void 0 : _g[col_index];
613
+ existingFormula = (cellAt === null || cellAt === void 0 ? void 0 : cellAt.f) != null && String(cellAt.f).trim() !== "" ? String(cellAt.f).replace(/[\r\n]/g, "") : null;
614
+ if (existingFormula != null) {
615
+ suppressFormulaRangeSelectionForInitialEdit(ctx);
616
+ }
520
617
  ctx.luckysheetCellUpdate = [row_index, col_index];
521
618
  cache.overwriteCell = true;
522
- handleFormulaInput(ctx, fxInput, cellInput, kcode);
619
+ cache.pendingTypeOverCell = [row_index, col_index];
620
+ setFormulaEditorOwner(ctx, "cell");
621
+ cache.enteredEditByTyping = true;
622
+ cellInput.focus();
623
+ initial = getTypeOverInitialContent(e);
624
+ if (initial !== undefined) {
625
+ cellInput.textContent = initial;
626
+ if (fxInput) fxInput.textContent = initial;
627
+ handleFormulaInput(ctx, fxInput, cellInput, kcode);
628
+ e.preventDefault();
629
+ } else {
630
+ cellInput.textContent = "";
631
+ if (fxInput) fxInput.textContent = "";
632
+ handleFormulaInput(ctx, fxInput, cellInput, kcode);
633
+ }
634
+ queueMicrotask(function () {
635
+ moveToEnd(cellInput);
636
+ });
523
637
  }
524
638
  }
525
639
  }
@@ -175,6 +175,7 @@ export function handleCellAreaMouseDown(ctx, globalCache, e, cellInput, containe
175
175
  ctx.luckysheet_scroll_status = true;
176
176
  if (ctx.luckysheetCellUpdate.length > 0) {
177
177
  if (ctx.formulaCache.rangestart || ctx.formulaCache.rangedrag_column_start || ctx.formulaCache.rangedrag_row_start || israngeseleciton(ctx)) {
178
+ var formulaEditorForCmdComma = getFormulaEditorOwner(ctx) === "fx" && fxInput ? fxInput : cellInput;
178
179
  var rowseleted = [row_index, row_index_ed];
179
180
  var columnseleted = [col_index, col_index_ed];
180
181
  var left = col_pre;
@@ -241,13 +242,14 @@ export function handleCellAreaMouseDown(ctx, globalCache, e, cellInput, containe
241
242
  last.top_move = top_1;
242
243
  last.height_move = height;
243
244
  ctx.formulaCache.func_selectedrange = last;
244
- } else if (e.ctrlKey && ((_g = _.last(cellInput.querySelectorAll("span"))) === null || _g === void 0 ? void 0 : _g.innerText) !== ",") {
245
- var vText = cellInput.innerText;
245
+ } else if ((e.ctrlKey || e.metaKey) && ((_g = _.last(formulaEditorForCmdComma.querySelectorAll("span"))) === null || _g === void 0 ? void 0 : _g.innerText) !== ",") {
246
+ var didCmdCommaFormulaHtml = false;
247
+ var vText = formulaEditorForCmdComma.innerText;
246
248
  if (vText[vText.length - 1] === ")") {
247
249
  vText = vText.substring(0, vText.length - 1);
248
250
  }
249
251
  if (vText.length > 0) {
250
- var lastWord = vText.substring(vText.length - 1, 1);
252
+ var lastWord = vText.slice(-1);
251
253
  if (lastWord !== "," && lastWord !== "=" && lastWord !== "(") {
252
254
  vText += ",";
253
255
  }
@@ -263,15 +265,21 @@ export function handleCellAreaMouseDown(ctx, globalCache, e, cellInput, containe
263
265
  ctx.formulaCache.functionRangeIndex = textRange;
264
266
  }
265
267
  cellInput.innerHTML = vText;
268
+ if (fxInput) fxInput.innerHTML = vText;
266
269
  cancelFunctionrangeSelected(ctx);
267
270
  createRangeHightlight(ctx, vText);
271
+ didCmdCommaFormulaHtml = true;
268
272
  }
269
273
  ctx.formulaCache.rangestart = false;
270
274
  ctx.formulaCache.rangedrag_column_start = false;
271
275
  ctx.formulaCache.rangedrag_row_start = false;
272
- if (fxInput) fxInput.innerHTML = vText;
273
- rangeHightlightselected(ctx, cellInput);
276
+ rangeHightlightselected(ctx, formulaEditorForCmdComma);
274
277
  israngeseleciton(ctx);
278
+ if (didCmdCommaFormulaHtml) {
279
+ ctx.formulaCache.rangechangeindex = undefined;
280
+ var ch = formulaEditorForCmdComma.childNodes;
281
+ ctx.formulaCache.rangeSetValueTo = ch.length > 0 ? ch[ch.length - 1] : undefined;
282
+ }
275
283
  ctx.formulaCache.func_selectedrange = {
276
284
  left: left,
277
285
  width: width,
@@ -305,6 +313,7 @@ export function handleCellAreaMouseDown(ctx, globalCache, e, cellInput, containe
305
313
  ctx.formulaCache.rangestart = true;
306
314
  ctx.formulaCache.rangedrag_column_start = false;
307
315
  ctx.formulaCache.rangedrag_row_start = false;
316
+ ctx.formulaCache.rangeSelectionActive = true;
308
317
  rangeSetValue(ctx, cellInput, {
309
318
  row: rowseleted,
310
319
  column: columnseleted
@@ -483,6 +492,8 @@ export function handleCellAreaDoubleClick(ctx, globalCache, settings, e, contain
483
492
  row_index = row_focus;
484
493
  col_index = column_focus;
485
494
  }
495
+ globalCache.enteredEditByTyping = false;
496
+ delete globalCache.pendingTypeOverCell;
486
497
  luckysheetUpdateCell(ctx, row_index, col_index);
487
498
  }
488
499
  export function handleContextMenu(ctx, settings, e, workbookContainer, container, area) {
@@ -1342,6 +1353,7 @@ export function handleRowHeaderMouseDown(ctx, globalCache, e, container, cellInp
1342
1353
  ctx.formulaCache.rangedrag_row_start = true;
1343
1354
  ctx.formulaCache.rangestart = false;
1344
1355
  ctx.formulaCache.rangedrag_column_start = false;
1356
+ ctx.formulaCache.rangeSelectionActive = true;
1345
1357
  ctx.formulaCache.selectingRangeIndex = ctx.formulaCache.rangechangeindex;
1346
1358
  if (ctx.formulaCache.rangechangeindex > ctx.formulaRangeHighlight.length) {
1347
1359
  createRangeHightlight(ctx, cellInput.innerHTML, ctx.formulaCache.rangechangeindex);
@@ -1574,6 +1586,7 @@ export function handleColumnHeaderMouseDown(ctx, globalCache, e, container, cell
1574
1586
  ctx.formulaCache.rangedrag_column_start = true;
1575
1587
  ctx.formulaCache.rangestart = false;
1576
1588
  ctx.formulaCache.rangedrag_row_start = false;
1589
+ ctx.formulaCache.rangeSelectionActive = true;
1577
1590
  ctx.formulaCache.selectingRangeIndex = ctx.formulaCache.rangechangeindex;
1578
1591
  if (ctx.formulaCache.rangechangeindex > ctx.formulaRangeHighlight.length) {
1579
1592
  createRangeHightlight(ctx, cellInput.innerHTML, ctx.formulaCache.rangechangeindex);
@@ -16,7 +16,7 @@ import { checkCF, getComputeMap } from "./ConditionFormat";
16
16
  import { getFailureText, validateCellData } from "./dataVerification";
17
17
  import { genarate, update } from "./format";
18
18
  import { clearCellError, getRowHeight } from "../api";
19
- import { delFunctionGroup, execfunction, execFunctionGroup, functionHTMLGenerate, getcellrange, iscelldata } from "./formula";
19
+ import { delFunctionGroup, execfunction, execFunctionGroup, functionHTMLGenerate, getcellrange, iscelldata, suppressFormulaRangeSelectionForInitialEdit } from "./formula";
20
20
  import { attrToCssName, convertSpanToShareString, isInlineStringCell, isInlineStringCT } from "./inline-string";
21
21
  import { isRealNull, isRealNum, valueIsError } from "./validation";
22
22
  import { getCellTextInfo } from "./text";
@@ -69,6 +69,32 @@ function newlinesToBr(text) {
69
69
  if (!text) return "";
70
70
  return text.replace(/\r\n|\r|\n/g, "<br />");
71
71
  }
72
+ function closeUnclosedParenthesesInFormula(formula) {
73
+ if (!formula.startsWith("=") || formula.length <= 1) return formula;
74
+ var body = formula.slice(1);
75
+ var depth = 0;
76
+ var inString = false;
77
+ for (var i = 0; i < body.length; i += 1) {
78
+ var ch = body[i];
79
+ if (inString) {
80
+ if (ch === '"') {
81
+ if (body[i + 1] === '"') {
82
+ i += 1;
83
+ } else {
84
+ inString = false;
85
+ }
86
+ }
87
+ continue;
88
+ }
89
+ if (ch === '"') {
90
+ inString = true;
91
+ continue;
92
+ }
93
+ if (ch === "(") depth += 1;else if (ch === ")") depth = Math.max(0, depth - 1);
94
+ }
95
+ if (depth <= 0) return formula;
96
+ return "".concat(formula).concat(")".repeat(depth));
97
+ }
72
98
  export function getCellValue(r, c, data, attr) {
73
99
  var _a;
74
100
  if (!attr) {
@@ -618,6 +644,11 @@ export function updateCell(ctx, r, c, $input, value, canvas) {
618
644
  }
619
645
  }
620
646
  value = value || inputText;
647
+ if (_.isString(value) && value.startsWith("=") && value.length > 1) {
648
+ value = closeUnclosedParenthesesInFormula(value);
649
+ } else if (_.isPlainObject(value) && _.isString(value.f) && value.f.startsWith("=") && value.f.length > 1) {
650
+ value.f = closeUnclosedParenthesesInFormula(value.f);
651
+ }
621
652
  var shouldClearError = (oldValue_1 === null || oldValue_1 === void 0 ? void 0 : oldValue_1.f) ? oldValue_1.f !== value : (oldValue_1 === null || oldValue_1 === void 0 ? void 0 : oldValue_1.v) !== value;
622
653
  if (shouldClearError) {
623
654
  clearCellError(ctx, r, c);
@@ -1226,6 +1257,12 @@ export function getdatabyselection(ctx, range, sheetId) {
1226
1257
  return data;
1227
1258
  }
1228
1259
  export function luckysheetUpdateCell(ctx, row_index, col_index) {
1260
+ var _a;
1261
+ var flowdata = getFlowdata(ctx);
1262
+ var cell = (_a = flowdata === null || flowdata === void 0 ? void 0 : flowdata[row_index]) === null || _a === void 0 ? void 0 : _a[col_index];
1263
+ if ((cell === null || cell === void 0 ? void 0 : cell.f) != null && String(cell.f).trim() !== "") {
1264
+ suppressFormulaRangeSelectionForInitialEdit(ctx);
1265
+ }
1229
1266
  ctx.luckysheetCellUpdate = [row_index, col_index];
1230
1267
  }
1231
1268
  export function getDataBySelectionNoCopy(ctx, range) {
@@ -50,6 +50,9 @@ export declare function getLastFormulaRangeIndex($editor: HTMLDivElement): numbe
50
50
  export declare function getFormulaRangeIndexAtCaret($editor: HTMLDivElement): number | null;
51
51
  export declare function setFormulaEditorOwner(ctx: Context, owner: "cell" | "fx" | null): void;
52
52
  export declare function getFormulaEditorOwner(ctx: Context): "cell" | "fx" | null;
53
+ export declare function hasIncompleteTruncatedCellRangeSyntax(formulaText: string): boolean;
54
+ export declare function isBareCellOrRangeOnlyFormula(formulaText: string): boolean;
55
+ export declare function suppressFormulaRangeSelectionForInitialEdit(ctx: Context): void;
53
56
  export declare function isCaretAtValidFormulaRangeInsertionPoint(editor: HTMLElement | null): boolean;
54
57
  export declare function markRangeSelectionDirty(ctx: Context): void;
55
58
  export declare function getFormulaRangeIndexForKeyboardSync($editor: HTMLDivElement): number | null;
@@ -1647,11 +1647,44 @@ export function getFormulaEditorOwner(ctx) {
1647
1647
  }
1648
1648
  return null;
1649
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
+ }
1650
1662
  function getCurrentFormulaSlotTextBeforeCaret(editor, caretOffset) {
1651
1663
  var textBefore = editor.innerText.slice(0, caretOffset);
1652
1664
  var parts = textBefore.split(/[=,(+\-*/&<>]/);
1653
1665
  return _.trim(parts[parts.length - 1] || "");
1654
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
+ }
1655
1688
  export function isCaretAtValidFormulaRangeInsertionPoint(editor) {
1656
1689
  var currSelection = window.getSelection();
1657
1690
  if (!editor || !currSelection || currSelection.rangeCount === 0) {
@@ -1665,9 +1698,15 @@ export function isCaretAtValidFormulaRangeInsertionPoint(editor) {
1665
1698
  if (!inputText.startsWith("=")) {
1666
1699
  return false;
1667
1700
  }
1701
+ if (hasIncompleteTruncatedCellRangeSyntax(inputText)) {
1702
+ return false;
1703
+ }
1668
1704
  if (/^=\s*[A-Za-z_][A-Za-z0-9_]*$/.test(inputText)) {
1669
1705
  return false;
1670
1706
  }
1707
+ if (isBareCellOrRangeOnlyFormula(inputText)) {
1708
+ return false;
1709
+ }
1671
1710
  var caretRange = currSelection.getRangeAt(0).cloneRange();
1672
1711
  var preCaretRange = document.createRange();
1673
1712
  preCaretRange.selectNodeContents(editor);
@@ -1680,7 +1719,22 @@ export function isCaretAtValidFormulaRangeInsertionPoint(editor) {
1680
1719
  var textAfter = editor.innerText.slice(caretOffset);
1681
1720
  var remaining = textAfter.replace(/^\s+/, "");
1682
1721
  if (remaining.length === 0) {
1683
- return true;
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;
1684
1738
  }
1685
1739
  var first = remaining[0];
1686
1740
  return first === "," || first === ")" || first === "&" || first in operatorjson;
@@ -1791,9 +1845,7 @@ export function handleFormulaInput(ctx, $copyTo, $editor, kcode, preText, refres
1791
1845
  functionRange(ctx, $editor, value, value1);
1792
1846
  if (refreshRangeSelect) {
1793
1847
  cancelFunctionrangeSelected(ctx);
1794
- if (kcode !== 46) {
1795
- createRangeHightlight(ctx, value);
1796
- }
1848
+ createRangeHightlight(ctx, value);
1797
1849
  ctx.formulaCache.rangestart = false;
1798
1850
  ctx.formulaCache.rangedrag_column_start = false;
1799
1851
  ctx.formulaCache.rangedrag_row_start = false;
@@ -2140,12 +2192,15 @@ export function israngeseleciton(ctx, istooltip) {
2140
2192
  return false;
2141
2193
  }
2142
2194
  export function isFormulaReferenceInputMode(ctx) {
2143
- var editor = document.getElementById("luckysheet-rich-text-editor");
2195
+ var editor = getActiveFormulaEditorElement(ctx);
2144
2196
  var inputText = ((editor === null || editor === void 0 ? void 0 : editor.innerText) || "").trim();
2145
- var hasRangeToken = (editor === null || editor === void 0 ? void 0 : editor.querySelector("span.fortune-formula-functionrange-cell")) != null;
2146
- 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) {
2147
2199
  return true;
2148
2200
  }
2201
+ if (ctx.formulaCache.rangeSelectionActive === false) {
2202
+ return false;
2203
+ }
2149
2204
  if (!inputText.startsWith("=")) {
2150
2205
  return false;
2151
2206
  }
@@ -2158,13 +2213,16 @@ export function maybeRecoverDirtyRangeSelection(ctx) {
2158
2213
  if (ctx.formulaCache.rangeSelectionActive !== false) {
2159
2214
  return false;
2160
2215
  }
2161
- var editor = document.getElementById("luckysheet-rich-text-editor");
2216
+ var editor = getActiveFormulaEditorElement(ctx);
2162
2217
  if (!editor) {
2163
2218
  return false;
2164
2219
  }
2165
2220
  var inputText = (editor.innerText || "").trim();
2221
+ if (hasIncompleteTruncatedCellRangeSyntax(inputText)) {
2222
+ return false;
2223
+ }
2166
2224
  var atCaretRangeIndex = getFormulaRangeIndexAtCaret(editor);
2167
- if (inputText.startsWith("=") && atCaretRangeIndex === null && israngeseleciton(ctx)) {
2225
+ if (inputText.startsWith("=") && atCaretRangeIndex === null && isCaretAtValidFormulaRangeInsertionPoint(editor) && israngeseleciton(ctx)) {
2168
2226
  ctx.formulaCache.rangeSelectionActive = null;
2169
2227
  return true;
2170
2228
  }
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
  }
@@ -196,6 +196,7 @@ function handleCellAreaMouseDown(ctx, globalCache, e, cellInput, container, fxIn
196
196
  ctx.luckysheet_scroll_status = true;
197
197
  if (ctx.luckysheetCellUpdate.length > 0) {
198
198
  if (ctx.formulaCache.rangestart || ctx.formulaCache.rangedrag_column_start || ctx.formulaCache.rangedrag_row_start || (0, _modules.israngeseleciton)(ctx)) {
199
+ var formulaEditorForCmdComma = (0, _modules.getFormulaEditorOwner)(ctx) === "fx" && fxInput ? fxInput : cellInput;
199
200
  var rowseleted = [row_index, row_index_ed];
200
201
  var columnseleted = [col_index, col_index_ed];
201
202
  var left = col_pre;
@@ -262,13 +263,14 @@ function handleCellAreaMouseDown(ctx, globalCache, e, cellInput, container, fxIn
262
263
  last.top_move = top_1;
263
264
  last.height_move = height;
264
265
  ctx.formulaCache.func_selectedrange = last;
265
- } else if (e.ctrlKey && ((_g = _lodash.default.last(cellInput.querySelectorAll("span"))) === null || _g === void 0 ? void 0 : _g.innerText) !== ",") {
266
- var vText = cellInput.innerText;
266
+ } else if ((e.ctrlKey || e.metaKey) && ((_g = _lodash.default.last(formulaEditorForCmdComma.querySelectorAll("span"))) === null || _g === void 0 ? void 0 : _g.innerText) !== ",") {
267
+ var didCmdCommaFormulaHtml = false;
268
+ var vText = formulaEditorForCmdComma.innerText;
267
269
  if (vText[vText.length - 1] === ")") {
268
270
  vText = vText.substring(0, vText.length - 1);
269
271
  }
270
272
  if (vText.length > 0) {
271
- var lastWord = vText.substring(vText.length - 1, 1);
273
+ var lastWord = vText.slice(-1);
272
274
  if (lastWord !== "," && lastWord !== "=" && lastWord !== "(") {
273
275
  vText += ",";
274
276
  }
@@ -284,15 +286,21 @@ function handleCellAreaMouseDown(ctx, globalCache, e, cellInput, container, fxIn
284
286
  ctx.formulaCache.functionRangeIndex = textRange;
285
287
  }
286
288
  cellInput.innerHTML = vText;
289
+ if (fxInput) fxInput.innerHTML = vText;
287
290
  (0, _cell.cancelFunctionrangeSelected)(ctx);
288
291
  (0, _modules.createRangeHightlight)(ctx, vText);
292
+ didCmdCommaFormulaHtml = true;
289
293
  }
290
294
  ctx.formulaCache.rangestart = false;
291
295
  ctx.formulaCache.rangedrag_column_start = false;
292
296
  ctx.formulaCache.rangedrag_row_start = false;
293
- if (fxInput) fxInput.innerHTML = vText;
294
- (0, _modules.rangeHightlightselected)(ctx, cellInput);
297
+ (0, _modules.rangeHightlightselected)(ctx, formulaEditorForCmdComma);
295
298
  (0, _modules.israngeseleciton)(ctx);
299
+ if (didCmdCommaFormulaHtml) {
300
+ ctx.formulaCache.rangechangeindex = undefined;
301
+ var ch = formulaEditorForCmdComma.childNodes;
302
+ ctx.formulaCache.rangeSetValueTo = ch.length > 0 ? ch[ch.length - 1] : undefined;
303
+ }
296
304
  ctx.formulaCache.func_selectedrange = {
297
305
  left: left,
298
306
  width: width,
@@ -326,6 +334,7 @@ function handleCellAreaMouseDown(ctx, globalCache, e, cellInput, container, fxIn
326
334
  ctx.formulaCache.rangestart = true;
327
335
  ctx.formulaCache.rangedrag_column_start = false;
328
336
  ctx.formulaCache.rangedrag_row_start = false;
337
+ ctx.formulaCache.rangeSelectionActive = true;
329
338
  (0, _modules.rangeSetValue)(ctx, cellInput, {
330
339
  row: rowseleted,
331
340
  column: columnseleted
@@ -504,6 +513,8 @@ function handleCellAreaDoubleClick(ctx, globalCache, settings, e, container) {
504
513
  row_index = row_focus;
505
514
  col_index = column_focus;
506
515
  }
516
+ globalCache.enteredEditByTyping = false;
517
+ delete globalCache.pendingTypeOverCell;
507
518
  (0, _cell.luckysheetUpdateCell)(ctx, row_index, col_index);
508
519
  }
509
520
  function handleContextMenu(ctx, settings, e, workbookContainer, container, area) {
@@ -1363,6 +1374,7 @@ function handleRowHeaderMouseDown(ctx, globalCache, e, container, cellInput, fxI
1363
1374
  ctx.formulaCache.rangedrag_row_start = true;
1364
1375
  ctx.formulaCache.rangestart = false;
1365
1376
  ctx.formulaCache.rangedrag_column_start = false;
1377
+ ctx.formulaCache.rangeSelectionActive = true;
1366
1378
  ctx.formulaCache.selectingRangeIndex = ctx.formulaCache.rangechangeindex;
1367
1379
  if (ctx.formulaCache.rangechangeindex > ctx.formulaRangeHighlight.length) {
1368
1380
  (0, _modules.createRangeHightlight)(ctx, cellInput.innerHTML, ctx.formulaCache.rangechangeindex);
@@ -1595,6 +1607,7 @@ function handleColumnHeaderMouseDown(ctx, globalCache, e, container, cellInput,
1595
1607
  ctx.formulaCache.rangedrag_column_start = true;
1596
1608
  ctx.formulaCache.rangestart = false;
1597
1609
  ctx.formulaCache.rangedrag_row_start = false;
1610
+ ctx.formulaCache.rangeSelectionActive = true;
1598
1611
  ctx.formulaCache.selectingRangeIndex = ctx.formulaCache.rangechangeindex;
1599
1612
  if (ctx.formulaCache.rangechangeindex > ctx.formulaRangeHighlight.length) {
1600
1613
  (0, _modules.createRangeHightlight)(ctx, cellInput.innerHTML, ctx.formulaCache.rangechangeindex);
@@ -102,6 +102,32 @@ function newlinesToBr(text) {
102
102
  if (!text) return "";
103
103
  return text.replace(/\r\n|\r|\n/g, "<br />");
104
104
  }
105
+ function closeUnclosedParenthesesInFormula(formula) {
106
+ if (!formula.startsWith("=") || formula.length <= 1) return formula;
107
+ var body = formula.slice(1);
108
+ var depth = 0;
109
+ var inString = false;
110
+ for (var i = 0; i < body.length; i += 1) {
111
+ var ch = body[i];
112
+ if (inString) {
113
+ if (ch === '"') {
114
+ if (body[i + 1] === '"') {
115
+ i += 1;
116
+ } else {
117
+ inString = false;
118
+ }
119
+ }
120
+ continue;
121
+ }
122
+ if (ch === '"') {
123
+ inString = true;
124
+ continue;
125
+ }
126
+ if (ch === "(") depth += 1;else if (ch === ")") depth = Math.max(0, depth - 1);
127
+ }
128
+ if (depth <= 0) return formula;
129
+ return "".concat(formula).concat(")".repeat(depth));
130
+ }
105
131
  function getCellValue(r, c, data, attr) {
106
132
  var _a;
107
133
  if (!attr) {
@@ -651,6 +677,11 @@ function updateCell(ctx, r, c, $input, value, canvas) {
651
677
  }
652
678
  }
653
679
  value = value || inputText;
680
+ if (_lodash.default.isString(value) && value.startsWith("=") && value.length > 1) {
681
+ value = closeUnclosedParenthesesInFormula(value);
682
+ } else if (_lodash.default.isPlainObject(value) && _lodash.default.isString(value.f) && value.f.startsWith("=") && value.f.length > 1) {
683
+ value.f = closeUnclosedParenthesesInFormula(value.f);
684
+ }
654
685
  var shouldClearError = (oldValue_1 === null || oldValue_1 === void 0 ? void 0 : oldValue_1.f) ? oldValue_1.f !== value : (oldValue_1 === null || oldValue_1 === void 0 ? void 0 : oldValue_1.v) !== value;
655
686
  if (shouldClearError) {
656
687
  (0, _api.clearCellError)(ctx, r, c);
@@ -1259,6 +1290,12 @@ function getdatabyselection(ctx, range, sheetId) {
1259
1290
  return data;
1260
1291
  }
1261
1292
  function luckysheetUpdateCell(ctx, row_index, col_index) {
1293
+ var _a;
1294
+ var flowdata = (0, _context.getFlowdata)(ctx);
1295
+ var cell = (_a = flowdata === null || flowdata === void 0 ? void 0 : flowdata[row_index]) === null || _a === void 0 ? void 0 : _a[col_index];
1296
+ if ((cell === null || cell === void 0 ? void 0 : cell.f) != null && String(cell.f).trim() !== "") {
1297
+ (0, _formula.suppressFormulaRangeSelectionForInitialEdit)(ctx);
1298
+ }
1262
1299
  ctx.luckysheetCellUpdate = [row_index, col_index];
1263
1300
  }
1264
1301
  function getDataBySelectionNoCopy(ctx, range) {
@@ -50,6 +50,9 @@ export declare function getLastFormulaRangeIndex($editor: HTMLDivElement): numbe
50
50
  export declare function getFormulaRangeIndexAtCaret($editor: HTMLDivElement): number | null;
51
51
  export declare function setFormulaEditorOwner(ctx: Context, owner: "cell" | "fx" | null): void;
52
52
  export declare function getFormulaEditorOwner(ctx: Context): "cell" | "fx" | null;
53
+ export declare function hasIncompleteTruncatedCellRangeSyntax(formulaText: string): boolean;
54
+ export declare function isBareCellOrRangeOnlyFormula(formulaText: string): boolean;
55
+ export declare function suppressFormulaRangeSelectionForInitialEdit(ctx: Context): void;
53
56
  export declare function isCaretAtValidFormulaRangeInsertionPoint(editor: HTMLElement | null): boolean;
54
57
  export declare function markRangeSelectionDirty(ctx: Context): void;
55
58
  export declare function getFormulaRangeIndexForKeyboardSync($editor: HTMLDivElement): number | null;
@@ -21,7 +21,9 @@ exports.getcellrange = getcellrange;
21
21
  exports.getrangeseleciton = getrangeseleciton;
22
22
  exports.groupValuesRefresh = groupValuesRefresh;
23
23
  exports.handleFormulaInput = handleFormulaInput;
24
+ exports.hasIncompleteTruncatedCellRangeSyntax = hasIncompleteTruncatedCellRangeSyntax;
24
25
  exports.insertUpdateFunctionGroup = insertUpdateFunctionGroup;
26
+ exports.isBareCellOrRangeOnlyFormula = isBareCellOrRangeOnlyFormula;
25
27
  exports.isCaretAtValidFormulaRangeInsertionPoint = isCaretAtValidFormulaRangeInsertionPoint;
26
28
  exports.isFormulaReferenceInputMode = isFormulaReferenceInputMode;
27
29
  exports.iscelldata = iscelldata;
@@ -37,6 +39,7 @@ exports.rangeHightlightselected = rangeHightlightselected;
37
39
  exports.rangeSetValue = rangeSetValue;
38
40
  exports.setCaretPosition = setCaretPosition;
39
41
  exports.setFormulaEditorOwner = setFormulaEditorOwner;
42
+ exports.suppressFormulaRangeSelectionForInitialEdit = suppressFormulaRangeSelectionForInitialEdit;
40
43
  var _formulaParser = require("@fileverse-dev/formula-parser");
41
44
  var _lodash = _interopRequireDefault(require("lodash"));
42
45
  var _context = require("../context");
@@ -1686,11 +1689,44 @@ function getFormulaEditorOwner(ctx) {
1686
1689
  }
1687
1690
  return null;
1688
1691
  }
1692
+ function getActiveFormulaEditorElement(ctx) {
1693
+ var _a;
1694
+ var cellEditor = document.getElementById("luckysheet-rich-text-editor");
1695
+ var fxEditor = document.getElementById("luckysheet-functionbox-cell");
1696
+ var owner = getFormulaEditorOwner(ctx);
1697
+ if (owner === "fx") return fxEditor !== null && fxEditor !== void 0 ? fxEditor : cellEditor;
1698
+ if (owner === "cell") return cellEditor !== null && cellEditor !== void 0 ? cellEditor : fxEditor;
1699
+ var activeId = (_a = document.activeElement) === null || _a === void 0 ? void 0 : _a.id;
1700
+ if (activeId === "luckysheet-functionbox-cell") return fxEditor !== null && fxEditor !== void 0 ? fxEditor : cellEditor;
1701
+ if (activeId === "luckysheet-rich-text-editor") return cellEditor !== null && cellEditor !== void 0 ? cellEditor : fxEditor;
1702
+ return cellEditor !== null && cellEditor !== void 0 ? cellEditor : fxEditor;
1703
+ }
1689
1704
  function getCurrentFormulaSlotTextBeforeCaret(editor, caretOffset) {
1690
1705
  var textBefore = editor.innerText.slice(0, caretOffset);
1691
1706
  var parts = textBefore.split(/[=,(+\-*/&<>]/);
1692
1707
  return _lodash.default.trim(parts[parts.length - 1] || "");
1693
1708
  }
1709
+ function hasIncompleteTruncatedCellRangeSyntax(formulaText) {
1710
+ var t = formulaText.replace(/\s/g, "");
1711
+ if (!t.startsWith("=")) return false;
1712
+ if (/[A-Za-z]+\d+:[A-Za-z]+$/i.test(t)) return true;
1713
+ if (/[A-Za-z]+\d+:\s*$/i.test(t)) return true;
1714
+ return false;
1715
+ }
1716
+ function isBareCellOrRangeOnlyFormula(formulaText) {
1717
+ var t = formulaText.trim();
1718
+ if (!t.startsWith("=")) return false;
1719
+ var body = t.slice(1).trim();
1720
+ if (!body) return false;
1721
+ if (body.includes("(") || body.includes(")")) return false;
1722
+ return iscelldata(body);
1723
+ }
1724
+ function suppressFormulaRangeSelectionForInitialEdit(ctx) {
1725
+ ctx.formulaCache.rangeSelectionActive = false;
1726
+ ctx.formulaCache.rangestart = false;
1727
+ ctx.formulaCache.rangedrag_column_start = false;
1728
+ ctx.formulaCache.rangedrag_row_start = false;
1729
+ }
1694
1730
  function isCaretAtValidFormulaRangeInsertionPoint(editor) {
1695
1731
  var currSelection = window.getSelection();
1696
1732
  if (!editor || !currSelection || currSelection.rangeCount === 0) {
@@ -1704,9 +1740,15 @@ function isCaretAtValidFormulaRangeInsertionPoint(editor) {
1704
1740
  if (!inputText.startsWith("=")) {
1705
1741
  return false;
1706
1742
  }
1743
+ if (hasIncompleteTruncatedCellRangeSyntax(inputText)) {
1744
+ return false;
1745
+ }
1707
1746
  if (/^=\s*[A-Za-z_][A-Za-z0-9_]*$/.test(inputText)) {
1708
1747
  return false;
1709
1748
  }
1749
+ if (isBareCellOrRangeOnlyFormula(inputText)) {
1750
+ return false;
1751
+ }
1710
1752
  var caretRange = currSelection.getRangeAt(0).cloneRange();
1711
1753
  var preCaretRange = document.createRange();
1712
1754
  preCaretRange.selectNodeContents(editor);
@@ -1719,7 +1761,22 @@ function isCaretAtValidFormulaRangeInsertionPoint(editor) {
1719
1761
  var textAfter = editor.innerText.slice(caretOffset);
1720
1762
  var remaining = textAfter.replace(/^\s+/, "");
1721
1763
  if (remaining.length === 0) {
1722
- return true;
1764
+ var atCaret = getFormulaRangeIndexAtCaret(editor);
1765
+ if (atCaret !== null) {
1766
+ return true;
1767
+ }
1768
+ var textBefore = editor.innerText.slice(0, caretOffset).trimEnd();
1769
+ var lastCh = textBefore.slice(-1);
1770
+ if (!lastCh) {
1771
+ return false;
1772
+ }
1773
+ if (lastCh === ")") {
1774
+ return false;
1775
+ }
1776
+ if (/^[=,(+\-*/&%^<>]$/.test(lastCh)) {
1777
+ return true;
1778
+ }
1779
+ return false;
1723
1780
  }
1724
1781
  var first = remaining[0];
1725
1782
  return first === "," || first === ")" || first === "&" || first in operatorjson;
@@ -1830,9 +1887,7 @@ function handleFormulaInput(ctx, $copyTo, $editor, kcode, preText, refreshRangeS
1830
1887
  functionRange(ctx, $editor, value, value1);
1831
1888
  if (refreshRangeSelect) {
1832
1889
  (0, _2.cancelFunctionrangeSelected)(ctx);
1833
- if (kcode !== 46) {
1834
- createRangeHightlight(ctx, value);
1835
- }
1890
+ createRangeHightlight(ctx, value);
1836
1891
  ctx.formulaCache.rangestart = false;
1837
1892
  ctx.formulaCache.rangedrag_column_start = false;
1838
1893
  ctx.formulaCache.rangedrag_row_start = false;
@@ -2179,12 +2234,15 @@ function israngeseleciton(ctx, istooltip) {
2179
2234
  return false;
2180
2235
  }
2181
2236
  function isFormulaReferenceInputMode(ctx) {
2182
- var editor = document.getElementById("luckysheet-rich-text-editor");
2237
+ var editor = getActiveFormulaEditorElement(ctx);
2183
2238
  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) {
2239
+ var refFlowActive = !!ctx.formulaCache.rangestart || !!ctx.formulaCache.rangedrag_column_start || !!ctx.formulaCache.rangedrag_row_start || ctx.formulaCache.rangeSelectionActive === true;
2240
+ if (refFlowActive) {
2186
2241
  return true;
2187
2242
  }
2243
+ if (ctx.formulaCache.rangeSelectionActive === false) {
2244
+ return false;
2245
+ }
2188
2246
  if (!inputText.startsWith("=")) {
2189
2247
  return false;
2190
2248
  }
@@ -2197,13 +2255,16 @@ function maybeRecoverDirtyRangeSelection(ctx) {
2197
2255
  if (ctx.formulaCache.rangeSelectionActive !== false) {
2198
2256
  return false;
2199
2257
  }
2200
- var editor = document.getElementById("luckysheet-rich-text-editor");
2258
+ var editor = getActiveFormulaEditorElement(ctx);
2201
2259
  if (!editor) {
2202
2260
  return false;
2203
2261
  }
2204
2262
  var inputText = (editor.innerText || "").trim();
2263
+ if (hasIncompleteTruncatedCellRangeSyntax(inputText)) {
2264
+ return false;
2265
+ }
2205
2266
  var atCaretRangeIndex = getFormulaRangeIndexAtCaret(editor);
2206
- if (inputText.startsWith("=") && atCaretRangeIndex === null && israngeseleciton(ctx)) {
2267
+ if (inputText.startsWith("=") && atCaretRangeIndex === null && isCaretAtValidFormulaRangeInsertionPoint(editor) && israngeseleciton(ctx)) {
2207
2268
  ctx.formulaCache.rangeSelectionActive = null;
2208
2269
  return true;
2209
2270
  }
package/lib/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;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fileverse-dev/fortune-core",
3
- "version": "1.3.11-input-ref-1",
3
+ "version": "1.3.11-input-ref-2",
4
4
  "main": "lib/index.js",
5
5
  "module": "es/index.js",
6
6
  "typings": "lib/index.d.ts",