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

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;
4
- export declare function handleWithCtrlOrMetaKey(ctx: Context, cache: GlobalCache, e: KeyboardEvent, cellInput: HTMLDivElement, fxInput: HTMLDivElement | null | undefined, handleUndo: () => void, handleRedo: () => void): void;
3
+ export declare function handleGlobalEnter(ctx: Context, cellInput: HTMLDivElement, e: KeyboardEvent, cache: GlobalCache, canvas?: CanvasRenderingContext2D): void;
4
+ export declare function handleWithCtrlOrMetaKey(ctx: Context, cache: GlobalCache, e: KeyboardEvent, cellInput: HTMLDivElement, fxInput: HTMLDivElement | null | undefined, handleUndo: () => void, handleRedo: () => void, canvas?: CanvasRenderingContext2D): 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,40 @@ 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("=");
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("=");
177
232
  }
233
+ if (cellT.startsWith("=") || fxT.startsWith("=")) return false;
234
+ return true;
235
+ }
236
+ function isDirectPlainTextCellEdit(ctx, cache, cellInput, fxInput) {
237
+ return (cache === null || cache === void 0 ? void 0 : cache.enteredEditByTyping) === true && ctx.luckysheetCellUpdate.length > 0 && isPlainTextCellOrFxEdit(ctx, cellInput, fxInput);
238
+ }
239
+ function commitDirectPlainCellEdit(ctx, cache, cellInput, canvas) {
240
+ if (ctx.luckysheetCellUpdate.length === 0) return;
241
+ updateCell(ctx, ctx.luckysheetCellUpdate[0], ctx.luckysheetCellUpdate[1], cellInput, undefined, canvas);
242
+ if (cache) {
243
+ cache.enteredEditByTyping = false;
244
+ clearTypeOverPending(cache);
245
+ }
246
+ }
247
+ function handleControlPlusArrowKey(ctx, e, shiftPressed) {
178
248
  var isFormulaRefMode = isLegacyFormulaRangeMode(ctx);
179
249
  if (isFormulaRefMode) {
180
250
  ctx.formulaCache.rangeSelectionActive = true;
@@ -244,10 +314,17 @@ function handleControlPlusArrowKey(ctx, e, shiftPressed) {
244
314
  break;
245
315
  }
246
316
  }
247
- export function handleWithCtrlOrMetaKey(ctx, cache, e, cellInput, fxInput, handleUndo, handleRedo) {
317
+ export function handleWithCtrlOrMetaKey(ctx, cache, e, cellInput, fxInput, handleUndo, handleRedo, canvas) {
248
318
  var _a, _b, _c, _d;
249
319
  var flowdata = getFlowdata(ctx);
250
320
  if (!flowdata) return;
321
+ if ((e.ctrlKey || e.metaKey) && ["ArrowUp", "ArrowDown", "ArrowLeft", "ArrowRight"].includes(e.key) && isPlainTextCellOrFxEdit(ctx, cellInput, fxInput)) {
322
+ if (isDirectPlainTextCellEdit(ctx, cache, cellInput, fxInput)) {
323
+ commitDirectPlainCellEdit(ctx, cache, cellInput, canvas);
324
+ } else {
325
+ return;
326
+ }
327
+ }
251
328
  if (e.shiftKey) {
252
329
  ctx.luckysheet_shiftpositon = _.cloneDeep((_a = ctx.luckysheet_select_save) === null || _a === void 0 ? void 0 : _a[ctx.luckysheet_select_save.length - 1]);
253
330
  ctx.luckysheet_shiftkeydown = true;
@@ -260,6 +337,8 @@ export function handleWithCtrlOrMetaKey(ctx, cache, e, cellInput, fxInput, handl
260
337
  var col_index = last.column_focus;
261
338
  updateCell(ctx, row_index, col_index, cellInput);
262
339
  ctx.luckysheetCellUpdate = [row_index, col_index];
340
+ cache.enteredEditByTyping = false;
341
+ clearTypeOverPending(cache);
263
342
  cache.ignoreWriteCell = true;
264
343
  var value = getNowDateTime(2);
265
344
  cellInput.innerText = value;
@@ -396,10 +475,10 @@ export function handleArrowKey(ctx, e) {
396
475
  e.preventDefault();
397
476
  }
398
477
  export function handleGlobalKeyDown(ctx, cellInput, fxInput, e, cache, handleUndo, handleRedo, canvas) {
399
- var _a, _b, _c;
478
+ var _a, _b, _c, _d, _e, _f, _g;
400
479
  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) {
480
+ 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;
481
+ return __generator(this, function (_h) {
403
482
  if (e.shiftKey && e.code === "Space") {
404
483
  e.stopImmediatePropagation();
405
484
  e.stopPropagation();
@@ -460,10 +539,12 @@ export function handleGlobalKeyDown(ctx, cellInput, fxInput, e, cache, handleUnd
460
539
  }
461
540
  if (kstr === "Enter") {
462
541
  if (!allowEdit) return [2];
463
- handleGlobalEnter(ctx, cellInput, e, canvas);
542
+ handleGlobalEnter(ctx, cellInput, e, cache, canvas);
464
543
  } else if (kstr === "Tab") {
465
544
  if (ctx.luckysheetCellUpdate.length > 0) {
466
545
  updateCell(ctx, ctx.luckysheetCellUpdate[0], ctx.luckysheetCellUpdate[1], cellInput, undefined, canvas);
546
+ cache.enteredEditByTyping = false;
547
+ clearTypeOverPending(cache);
467
548
  }
468
549
  if (e.shiftKey) {
469
550
  moveHighlightCell(ctx, "right", -hideCRCount(ctx, "ArrowLeft"), "rangeOfSelect");
@@ -480,20 +561,34 @@ export function handleGlobalKeyDown(ctx, cellInput, fxInput, e, cache, handleUnd
480
561
  if (!last) return [2];
481
562
  row_index = last.row_focus;
482
563
  col_index = last.column_focus;
564
+ if (!_.isNil(row_index) && !_.isNil(col_index)) {
565
+ flowdataF2 = getFlowdata(ctx);
566
+ cellF2 = (_d = flowdataF2 === null || flowdataF2 === void 0 ? void 0 : flowdataF2[row_index]) === null || _d === void 0 ? void 0 : _d[col_index];
567
+ if ((cellF2 === null || cellF2 === void 0 ? void 0 : cellF2.f) != null && String(cellF2.f).trim() !== "") {
568
+ suppressFormulaRangeSelectionForInitialEdit(ctx);
569
+ }
570
+ }
571
+ cache.enteredEditByTyping = false;
572
+ clearTypeOverPending(cache);
483
573
  ctx.luckysheetCellUpdate = [row_index, col_index];
484
574
  e.preventDefault();
485
575
  } else if (kstr === "F4" && ctx.luckysheetCellUpdate.length > 0) {
486
576
  e.preventDefault();
487
577
  } else if (kstr === "Escape" && ctx.luckysheetCellUpdate.length > 0) {
578
+ cache.enteredEditByTyping = false;
579
+ clearTypeOverPending(cache);
488
580
  cancelNormalSelected(ctx);
489
581
  moveHighlightCell(ctx, "down", 0, "rangeOfSelect");
490
582
  e.preventDefault();
491
583
  } else {
492
584
  if (e.ctrlKey || e.metaKey) {
493
- handleWithCtrlOrMetaKey(ctx, cache, e, cellInput, fxInput, handleUndo, handleRedo);
585
+ handleWithCtrlOrMetaKey(ctx, cache, e, cellInput, fxInput, handleUndo, handleRedo, canvas);
494
586
  return [2];
495
587
  }
496
588
  if (e.shiftKey && (kstr === "ArrowUp" || kstr === "ArrowDown" || kstr === "ArrowLeft" || kstr === "ArrowRight")) {
589
+ if (isDirectPlainTextCellEdit(ctx, cache, cellInput, fxInput)) {
590
+ commitDirectPlainCellEdit(ctx, cache, cellInput, canvas);
591
+ }
497
592
  handleShiftWithArrowKey(ctx, e);
498
593
  } else if (kstr === "Escape") {
499
594
  ctx.contextMenu = {};
@@ -510,16 +605,53 @@ export function handleGlobalKeyDown(ctx, cellInput, fxInput, e, cache, handleUnd
510
605
  jfrefreshgrid(ctx, null, undefined);
511
606
  e.preventDefault();
512
607
  } else if (kstr === "ArrowUp" || kstr === "ArrowDown" || kstr === "ArrowLeft" || kstr === "ArrowRight") {
513
- handleArrowKey(ctx, e);
608
+ isEditing = ctx.luckysheetCellUpdate.length > 0;
609
+ inlineText = (_e = cellInput === null || cellInput === void 0 ? void 0 : cellInput.innerText) !== null && _e !== void 0 ? _e : "";
610
+ fxText = (_f = fxInput === null || fxInput === void 0 ? void 0 : fxInput.innerText) !== null && _f !== void 0 ? _f : "";
611
+ isFormulaEdit = isEditing && (inlineText.trim().startsWith("=") || fxText.trim().startsWith("="));
612
+ enteredByTyping = cache.enteredEditByTyping === true;
613
+ if (isEditing && !isFormulaEdit && enteredByTyping && !e.shiftKey) {
614
+ updateCell(ctx, ctx.luckysheetCellUpdate[0], ctx.luckysheetCellUpdate[1], cellInput, undefined, canvas);
615
+ cache.enteredEditByTyping = false;
616
+ clearTypeOverPending(cache);
617
+ handleArrowKey(ctx, e);
618
+ e.preventDefault();
619
+ } else {
620
+ handleArrowKey(ctx, e);
621
+ }
514
622
  } 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
623
  if (!allowEdit) return [2];
516
624
  if (String.fromCharCode(kcode) != null && !_.isEmpty(ctx.luckysheet_select_save) && kstr !== "CapsLock" && kstr !== "Win" && kcode !== 18) {
517
625
  last = ctx.luckysheet_select_save[ctx.luckysheet_select_save.length - 1];
518
626
  row_index = last.row_focus;
519
627
  col_index = last.column_focus;
628
+ if (_.isNil(row_index) || _.isNil(col_index)) return [2];
629
+ flowdata = getFlowdata(ctx);
630
+ cellAt = (_g = flowdata === null || flowdata === void 0 ? void 0 : flowdata[row_index]) === null || _g === void 0 ? void 0 : _g[col_index];
631
+ existingFormula = (cellAt === null || cellAt === void 0 ? void 0 : cellAt.f) != null && String(cellAt.f).trim() !== "" ? String(cellAt.f).replace(/[\r\n]/g, "") : null;
632
+ if (existingFormula != null) {
633
+ suppressFormulaRangeSelectionForInitialEdit(ctx);
634
+ }
520
635
  ctx.luckysheetCellUpdate = [row_index, col_index];
521
636
  cache.overwriteCell = true;
522
- handleFormulaInput(ctx, fxInput, cellInput, kcode);
637
+ cache.pendingTypeOverCell = [row_index, col_index];
638
+ setFormulaEditorOwner(ctx, "cell");
639
+ cache.enteredEditByTyping = true;
640
+ cellInput.focus();
641
+ initial = getTypeOverInitialContent(e);
642
+ if (initial !== undefined) {
643
+ cellInput.textContent = initial;
644
+ if (fxInput) fxInput.textContent = initial;
645
+ handleFormulaInput(ctx, fxInput, cellInput, kcode);
646
+ e.preventDefault();
647
+ } else {
648
+ cellInput.textContent = "";
649
+ if (fxInput) fxInput.textContent = "";
650
+ handleFormulaInput(ctx, fxInput, cellInput, kcode);
651
+ }
652
+ queueMicrotask(function () {
653
+ moveToEnd(cellInput);
654
+ });
523
655
  }
524
656
  }
525
657
  }
@@ -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;
4
- export declare function handleWithCtrlOrMetaKey(ctx: Context, cache: GlobalCache, e: KeyboardEvent, cellInput: HTMLDivElement, fxInput: HTMLDivElement | null | undefined, handleUndo: () => void, handleRedo: () => void): void;
3
+ export declare function handleGlobalEnter(ctx: Context, cellInput: HTMLDivElement, e: KeyboardEvent, cache: GlobalCache, canvas?: CanvasRenderingContext2D): void;
4
+ export declare function handleWithCtrlOrMetaKey(ctx: Context, cache: GlobalCache, e: KeyboardEvent, cellInput: HTMLDivElement, fxInput: HTMLDivElement | null | undefined, handleUndo: () => void, handleRedo: () => void, canvas?: CanvasRenderingContext2D): 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,40 @@ 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("=");
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("=");
187
242
  }
243
+ if (cellT.startsWith("=") || fxT.startsWith("=")) return false;
244
+ return true;
245
+ }
246
+ function isDirectPlainTextCellEdit(ctx, cache, cellInput, fxInput) {
247
+ return (cache === null || cache === void 0 ? void 0 : cache.enteredEditByTyping) === true && ctx.luckysheetCellUpdate.length > 0 && isPlainTextCellOrFxEdit(ctx, cellInput, fxInput);
248
+ }
249
+ function commitDirectPlainCellEdit(ctx, cache, cellInput, canvas) {
250
+ if (ctx.luckysheetCellUpdate.length === 0) return;
251
+ (0, _cell.updateCell)(ctx, ctx.luckysheetCellUpdate[0], ctx.luckysheetCellUpdate[1], cellInput, undefined, canvas);
252
+ if (cache) {
253
+ cache.enteredEditByTyping = false;
254
+ clearTypeOverPending(cache);
255
+ }
256
+ }
257
+ function handleControlPlusArrowKey(ctx, e, shiftPressed) {
188
258
  var isFormulaRefMode = isLegacyFormulaRangeMode(ctx);
189
259
  if (isFormulaRefMode) {
190
260
  ctx.formulaCache.rangeSelectionActive = true;
@@ -254,10 +324,17 @@ function handleControlPlusArrowKey(ctx, e, shiftPressed) {
254
324
  break;
255
325
  }
256
326
  }
257
- function handleWithCtrlOrMetaKey(ctx, cache, e, cellInput, fxInput, handleUndo, handleRedo) {
327
+ function handleWithCtrlOrMetaKey(ctx, cache, e, cellInput, fxInput, handleUndo, handleRedo, canvas) {
258
328
  var _a, _b, _c, _d;
259
329
  var flowdata = (0, _context.getFlowdata)(ctx);
260
330
  if (!flowdata) return;
331
+ if ((e.ctrlKey || e.metaKey) && ["ArrowUp", "ArrowDown", "ArrowLeft", "ArrowRight"].includes(e.key) && isPlainTextCellOrFxEdit(ctx, cellInput, fxInput)) {
332
+ if (isDirectPlainTextCellEdit(ctx, cache, cellInput, fxInput)) {
333
+ commitDirectPlainCellEdit(ctx, cache, cellInput, canvas);
334
+ } else {
335
+ return;
336
+ }
337
+ }
261
338
  if (e.shiftKey) {
262
339
  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
340
  ctx.luckysheet_shiftkeydown = true;
@@ -270,6 +347,8 @@ function handleWithCtrlOrMetaKey(ctx, cache, e, cellInput, fxInput, handleUndo,
270
347
  var col_index = last.column_focus;
271
348
  (0, _cell.updateCell)(ctx, row_index, col_index, cellInput);
272
349
  ctx.luckysheetCellUpdate = [row_index, col_index];
350
+ cache.enteredEditByTyping = false;
351
+ clearTypeOverPending(cache);
273
352
  cache.ignoreWriteCell = true;
274
353
  var value = (0, _utils.getNowDateTime)(2);
275
354
  cellInput.innerText = value;
@@ -406,10 +485,10 @@ function handleArrowKey(ctx, e) {
406
485
  e.preventDefault();
407
486
  }
408
487
  function handleGlobalKeyDown(ctx, cellInput, fxInput, e, cache, handleUndo, handleRedo, canvas) {
409
- var _a, _b, _c;
488
+ var _a, _b, _c, _d, _e, _f, _g;
410
489
  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) {
490
+ 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;
491
+ return __generator(this, function (_h) {
413
492
  if (e.shiftKey && e.code === "Space") {
414
493
  e.stopImmediatePropagation();
415
494
  e.stopPropagation();
@@ -470,10 +549,12 @@ function handleGlobalKeyDown(ctx, cellInput, fxInput, e, cache, handleUndo, hand
470
549
  }
471
550
  if (kstr === "Enter") {
472
551
  if (!allowEdit) return [2];
473
- handleGlobalEnter(ctx, cellInput, e, canvas);
552
+ handleGlobalEnter(ctx, cellInput, e, cache, canvas);
474
553
  } else if (kstr === "Tab") {
475
554
  if (ctx.luckysheetCellUpdate.length > 0) {
476
555
  (0, _cell.updateCell)(ctx, ctx.luckysheetCellUpdate[0], ctx.luckysheetCellUpdate[1], cellInput, undefined, canvas);
556
+ cache.enteredEditByTyping = false;
557
+ clearTypeOverPending(cache);
477
558
  }
478
559
  if (e.shiftKey) {
479
560
  (0, _selection.moveHighlightCell)(ctx, "right", -(0, _2.hideCRCount)(ctx, "ArrowLeft"), "rangeOfSelect");
@@ -490,20 +571,34 @@ function handleGlobalKeyDown(ctx, cellInput, fxInput, e, cache, handleUndo, hand
490
571
  if (!last) return [2];
491
572
  row_index = last.row_focus;
492
573
  col_index = last.column_focus;
574
+ if (!_lodash.default.isNil(row_index) && !_lodash.default.isNil(col_index)) {
575
+ flowdataF2 = (0, _context.getFlowdata)(ctx);
576
+ cellF2 = (_d = flowdataF2 === null || flowdataF2 === void 0 ? void 0 : flowdataF2[row_index]) === null || _d === void 0 ? void 0 : _d[col_index];
577
+ if ((cellF2 === null || cellF2 === void 0 ? void 0 : cellF2.f) != null && String(cellF2.f).trim() !== "") {
578
+ (0, _formula.suppressFormulaRangeSelectionForInitialEdit)(ctx);
579
+ }
580
+ }
581
+ cache.enteredEditByTyping = false;
582
+ clearTypeOverPending(cache);
493
583
  ctx.luckysheetCellUpdate = [row_index, col_index];
494
584
  e.preventDefault();
495
585
  } else if (kstr === "F4" && ctx.luckysheetCellUpdate.length > 0) {
496
586
  e.preventDefault();
497
587
  } else if (kstr === "Escape" && ctx.luckysheetCellUpdate.length > 0) {
588
+ cache.enteredEditByTyping = false;
589
+ clearTypeOverPending(cache);
498
590
  (0, _cell.cancelNormalSelected)(ctx);
499
591
  (0, _selection.moveHighlightCell)(ctx, "down", 0, "rangeOfSelect");
500
592
  e.preventDefault();
501
593
  } else {
502
594
  if (e.ctrlKey || e.metaKey) {
503
- handleWithCtrlOrMetaKey(ctx, cache, e, cellInput, fxInput, handleUndo, handleRedo);
595
+ handleWithCtrlOrMetaKey(ctx, cache, e, cellInput, fxInput, handleUndo, handleRedo, canvas);
504
596
  return [2];
505
597
  }
506
598
  if (e.shiftKey && (kstr === "ArrowUp" || kstr === "ArrowDown" || kstr === "ArrowLeft" || kstr === "ArrowRight")) {
599
+ if (isDirectPlainTextCellEdit(ctx, cache, cellInput, fxInput)) {
600
+ commitDirectPlainCellEdit(ctx, cache, cellInput, canvas);
601
+ }
507
602
  handleShiftWithArrowKey(ctx, e);
508
603
  } else if (kstr === "Escape") {
509
604
  ctx.contextMenu = {};
@@ -520,16 +615,53 @@ function handleGlobalKeyDown(ctx, cellInput, fxInput, e, cache, handleUndo, hand
520
615
  (0, _refresh.jfrefreshgrid)(ctx, null, undefined);
521
616
  e.preventDefault();
522
617
  } else if (kstr === "ArrowUp" || kstr === "ArrowDown" || kstr === "ArrowLeft" || kstr === "ArrowRight") {
523
- handleArrowKey(ctx, e);
618
+ isEditing = ctx.luckysheetCellUpdate.length > 0;
619
+ inlineText = (_e = cellInput === null || cellInput === void 0 ? void 0 : cellInput.innerText) !== null && _e !== void 0 ? _e : "";
620
+ fxText = (_f = fxInput === null || fxInput === void 0 ? void 0 : fxInput.innerText) !== null && _f !== void 0 ? _f : "";
621
+ isFormulaEdit = isEditing && (inlineText.trim().startsWith("=") || fxText.trim().startsWith("="));
622
+ enteredByTyping = cache.enteredEditByTyping === true;
623
+ if (isEditing && !isFormulaEdit && enteredByTyping && !e.shiftKey) {
624
+ (0, _cell.updateCell)(ctx, ctx.luckysheetCellUpdate[0], ctx.luckysheetCellUpdate[1], cellInput, undefined, canvas);
625
+ cache.enteredEditByTyping = false;
626
+ clearTypeOverPending(cache);
627
+ handleArrowKey(ctx, e);
628
+ e.preventDefault();
629
+ } else {
630
+ handleArrowKey(ctx, e);
631
+ }
524
632
  } 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
633
  if (!allowEdit) return [2];
526
634
  if (String.fromCharCode(kcode) != null && !_lodash.default.isEmpty(ctx.luckysheet_select_save) && kstr !== "CapsLock" && kstr !== "Win" && kcode !== 18) {
527
635
  last = ctx.luckysheet_select_save[ctx.luckysheet_select_save.length - 1];
528
636
  row_index = last.row_focus;
529
637
  col_index = last.column_focus;
638
+ if (_lodash.default.isNil(row_index) || _lodash.default.isNil(col_index)) return [2];
639
+ flowdata = (0, _context.getFlowdata)(ctx);
640
+ cellAt = (_g = flowdata === null || flowdata === void 0 ? void 0 : flowdata[row_index]) === null || _g === void 0 ? void 0 : _g[col_index];
641
+ existingFormula = (cellAt === null || cellAt === void 0 ? void 0 : cellAt.f) != null && String(cellAt.f).trim() !== "" ? String(cellAt.f).replace(/[\r\n]/g, "") : null;
642
+ if (existingFormula != null) {
643
+ (0, _formula.suppressFormulaRangeSelectionForInitialEdit)(ctx);
644
+ }
530
645
  ctx.luckysheetCellUpdate = [row_index, col_index];
531
646
  cache.overwriteCell = true;
532
- (0, _formula.handleFormulaInput)(ctx, fxInput, cellInput, kcode);
647
+ cache.pendingTypeOverCell = [row_index, col_index];
648
+ (0, _formula.setFormulaEditorOwner)(ctx, "cell");
649
+ cache.enteredEditByTyping = true;
650
+ cellInput.focus();
651
+ initial = getTypeOverInitialContent(e);
652
+ if (initial !== undefined) {
653
+ cellInput.textContent = initial;
654
+ if (fxInput) fxInput.textContent = initial;
655
+ (0, _formula.handleFormulaInput)(ctx, fxInput, cellInput, kcode);
656
+ e.preventDefault();
657
+ } else {
658
+ cellInput.textContent = "";
659
+ if (fxInput) fxInput.textContent = "";
660
+ (0, _formula.handleFormulaInput)(ctx, fxInput, cellInput, kcode);
661
+ }
662
+ queueMicrotask(function () {
663
+ (0, _cursor.moveToEnd)(cellInput);
664
+ });
533
665
  }
534
666
  }
535
667
  }
@@ -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-3",
4
4
  "main": "lib/index.js",
5
5
  "module": "es/index.js",
6
6
  "typings": "lib/index.d.ts",