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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -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
  }
@@ -1,6 +1,6 @@
1
1
  import _ from "lodash";
2
2
  import { getFlowdata } from "../context";
3
- import { cancelActiveImgItem, cancelPaintModel, functionHTMLGenerate, israngeseleciton, maybeRecoverDirtyRangeSelection, rangeHightlightselected, rangeSetValue, onCommentBoxMove, onCommentBoxMoveEnd, onCommentBoxResize, onCommentBoxResizeEnd, onImageMove, onImageMoveEnd, onImageResize, onImageResizeEnd, removeEditingComment, removeOverShowComment, rangeDrag, onFormulaRangeDragEnd, createFormulaRangeSelect, createRangeHightlight, onCellsMoveEnd, onCellsMove, cellFocus, editComment, onIframeMoveEnd, onIframeResizeEnd, overShowError } from "../modules";
3
+ import { cancelActiveImgItem, cancelPaintModel, functionHTMLGenerate, israngeseleciton, maybeRecoverDirtyRangeSelection, rangeHightlightselected, rangeSetValue, getFormulaEditorOwner, onCommentBoxMove, onCommentBoxMoveEnd, onCommentBoxResize, onCommentBoxResizeEnd, onImageMove, onImageMoveEnd, onImageResize, onImageResizeEnd, removeEditingComment, removeOverShowComment, rangeDrag, onFormulaRangeDragEnd, createFormulaRangeSelect, createRangeHightlight, onCellsMoveEnd, onCellsMove, cellFocus, editComment, onIframeMoveEnd, onIframeResizeEnd, overShowError } from "../modules";
4
4
  import { getFrozenHandleLeft, getFrozenHandleTop, scrollToFrozenRowCol } from "../modules/freeze";
5
5
  import { cancelFunctionrangeSelected, mergeBorder, mergeMoveMain, updateCell, luckysheetUpdateCell } from "../modules/cell";
6
6
  import { colLocation, colLocationByIndex, rowLocation, rowLocationByIndex } from "../modules/location";
@@ -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) {
@@ -950,7 +961,7 @@ export function handleOverlayMouseMove(ctx, globalCache, e, cellInput, scrollX,
950
961
  }
951
962
  }
952
963
  export function handleOverlayMouseUp(ctx, globalCache, settings, e, scrollbarX, scrollbarY, container, cellInput, fxInput) {
953
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o;
964
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
954
965
  var rect = container.getBoundingClientRect();
955
966
  onImageMoveEnd(ctx, globalCache);
956
967
  onImageResizeEnd(ctx, globalCache);
@@ -963,11 +974,17 @@ export function handleOverlayMouseUp(ctx, globalCache, settings, e, scrollbarX,
963
974
  onRangeSelectionModalMoveEnd(globalCache);
964
975
  onCellsMoveEnd(ctx, globalCache, e, scrollbarX, scrollbarY, container);
965
976
  if (ctx.formulaCache.rangestart || ctx.formulaCache.rangedrag_column_start || ctx.formulaCache.rangedrag_row_start) {
966
- if (((_a = document.activeElement) === null || _a === void 0 ? void 0 : _a.id) === "luckysheet-functionbox-cell") {
977
+ if (getFormulaEditorOwner(ctx) === "fx") {
967
978
  handleFormulaInput(ctx, cellInput, fxInput, 0, undefined, false);
968
979
  } else {
969
980
  handleFormulaInput(ctx, fxInput, cellInput, 0, undefined, false);
970
981
  }
982
+ ctx.luckysheet_select_status = false;
983
+ ctx.luckysheet_scroll_status = false;
984
+ ctx.luckysheet_rows_selected_status = false;
985
+ ctx.luckysheet_cols_selected_status = false;
986
+ e.preventDefault();
987
+ return;
971
988
  }
972
989
  if (ctx.luckysheet_select_status) {
973
990
  if (ctx.luckysheetPaintModelOn) {
@@ -1009,10 +1026,10 @@ export function handleOverlayMouseUp(ctx, globalCache, settings, e, scrollbarX,
1009
1026
  cfg_1.customHeight[ctx.luckysheet_rows_change_size_start[1]] = 1;
1010
1027
  var changeRowIndex_1 = ctx.luckysheet_rows_change_size_start[1];
1011
1028
  var changeRowSelected_1 = false;
1012
- if (((_c = (_b = ctx.luckysheet_select_save) === null || _b === void 0 ? void 0 : _b.length) !== null && _c !== void 0 ? _c : 0) > 0) {
1013
- (_e = (_d = ctx.luckysheet_select_save) === null || _d === void 0 ? void 0 : _d.filter(function (select) {
1029
+ if (((_b = (_a = ctx.luckysheet_select_save) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0) > 0) {
1030
+ (_d = (_c = ctx.luckysheet_select_save) === null || _c === void 0 ? void 0 : _c.filter(function (select) {
1014
1031
  return select.row_select;
1015
- })) === null || _e === void 0 ? void 0 : _e.some(function (select) {
1032
+ })) === null || _d === void 0 ? void 0 : _d.some(function (select) {
1016
1033
  if (changeRowIndex_1 >= select.row[0] && changeRowIndex_1 <= select.row[1]) {
1017
1034
  changeRowSelected_1 = true;
1018
1035
  }
@@ -1021,9 +1038,9 @@ export function handleOverlayMouseUp(ctx, globalCache, settings, e, scrollbarX,
1021
1038
  }
1022
1039
  if (changeRowSelected_1) {
1023
1040
  cfg_1.rowlen || (cfg_1.rowlen = {});
1024
- (_g = (_f = ctx.luckysheet_select_save) === null || _f === void 0 ? void 0 : _f.filter(function (select) {
1041
+ (_f = (_e = ctx.luckysheet_select_save) === null || _e === void 0 ? void 0 : _e.filter(function (select) {
1025
1042
  return select.row_select;
1026
- })) === null || _g === void 0 ? void 0 : _g.forEach(function (select) {
1043
+ })) === null || _f === void 0 ? void 0 : _f.forEach(function (select) {
1027
1044
  for (var r = select.row[0]; r <= select.row[1]; r += 1) {
1028
1045
  cfg_1.rowlen[r] = Math.ceil(size_1 / ctx.zoomRatio);
1029
1046
  }
@@ -1067,10 +1084,10 @@ export function handleOverlayMouseUp(ctx, globalCache, settings, e, scrollbarX,
1067
1084
  cfg_2.customWidth[ctx.luckysheet_cols_change_size_start[1]] = 1;
1068
1085
  var changeColumnIndex_1 = ctx.luckysheet_cols_change_size_start[1];
1069
1086
  var changeColumnSelected_1 = false;
1070
- if (((_j = (_h = ctx.luckysheet_select_save) === null || _h === void 0 ? void 0 : _h.length) !== null && _j !== void 0 ? _j : 0) > 0) {
1071
- (_l = (_k = ctx.luckysheet_select_save) === null || _k === void 0 ? void 0 : _k.filter(function (select) {
1087
+ if (((_h = (_g = ctx.luckysheet_select_save) === null || _g === void 0 ? void 0 : _g.length) !== null && _h !== void 0 ? _h : 0) > 0) {
1088
+ (_k = (_j = ctx.luckysheet_select_save) === null || _j === void 0 ? void 0 : _j.filter(function (select) {
1072
1089
  return select.column_select;
1073
- })) === null || _l === void 0 ? void 0 : _l.some(function (select) {
1090
+ })) === null || _k === void 0 ? void 0 : _k.some(function (select) {
1074
1091
  if (changeColumnIndex_1 >= select.column[0] && changeColumnIndex_1 <= select.column[1]) {
1075
1092
  changeColumnSelected_1 = true;
1076
1093
  }
@@ -1079,9 +1096,9 @@ export function handleOverlayMouseUp(ctx, globalCache, settings, e, scrollbarX,
1079
1096
  }
1080
1097
  if (changeColumnSelected_1) {
1081
1098
  cfg_2.columnlen || (cfg_2.columnlen = {});
1082
- (_o = (_m = ctx.luckysheet_select_save) === null || _m === void 0 ? void 0 : _m.filter(function (select) {
1099
+ (_m = (_l = ctx.luckysheet_select_save) === null || _l === void 0 ? void 0 : _l.filter(function (select) {
1083
1100
  return select.column_select;
1084
- })) === null || _o === void 0 ? void 0 : _o.forEach(function (select) {
1101
+ })) === null || _m === void 0 ? void 0 : _m.forEach(function (select) {
1085
1102
  for (var r = select.column[0]; r <= select.column[1]; r += 1) {
1086
1103
  cfg_2.columnlen[r] = Math.ceil(size_2 / ctx.zoomRatio);
1087
1104
  }
@@ -1098,10 +1115,10 @@ export function handleOverlayMouseUp(ctx, globalCache, settings, e, scrollbarX,
1098
1115
  ctx.luckysheet_cols_freeze_drag = false;
1099
1116
  var scrollLeft = ctx.scrollLeft;
1100
1117
  var x = e.pageX - rect.left - ctx.rowHeaderWidth + scrollLeft - window.scrollX;
1101
- var _p = colLocation(x, ctx.visibledatacolumn),
1102
- col_pre = _p[0],
1103
- col_curr = _p[1],
1104
- col_index_curr = _p[2];
1118
+ var _o = colLocation(x, ctx.visibledatacolumn),
1119
+ col_pre = _o[0],
1120
+ col_curr = _o[1],
1121
+ col_index_curr = _o[2];
1105
1122
  var col_index = x > (col_curr + col_pre) / 2 ? col_index_curr : col_index_curr - 1;
1106
1123
  var idx = getSheetIndex(ctx, ctx.currentSheetId);
1107
1124
  if (idx == null) return;
@@ -1149,10 +1166,10 @@ export function handleOverlayMouseUp(ctx, globalCache, settings, e, scrollbarX,
1149
1166
  ctx.luckysheet_rows_freeze_drag = false;
1150
1167
  var scrollTop = ctx.scrollTop;
1151
1168
  var y = e.pageY - rect.top - ctx.columnHeaderHeight + scrollTop - window.scrollY;
1152
- var _q = rowLocation(y, ctx.visibledatarow),
1153
- row_pre = _q[0],
1154
- row_curr = _q[1],
1155
- row_index_curr = _q[2];
1169
+ var _p = rowLocation(y, ctx.visibledatarow),
1170
+ row_pre = _p[0],
1171
+ row_curr = _p[1],
1172
+ row_index_curr = _p[2];
1156
1173
  var row_index = y > (row_curr + row_pre) / 2 ? row_index_curr : row_index_curr - 1;
1157
1174
  var idx = getSheetIndex(ctx, ctx.currentSheetId);
1158
1175
  if (idx == null) return;
@@ -1336,6 +1353,7 @@ export function handleRowHeaderMouseDown(ctx, globalCache, e, container, cellInp
1336
1353
  ctx.formulaCache.rangedrag_row_start = true;
1337
1354
  ctx.formulaCache.rangestart = false;
1338
1355
  ctx.formulaCache.rangedrag_column_start = false;
1356
+ ctx.formulaCache.rangeSelectionActive = true;
1339
1357
  ctx.formulaCache.selectingRangeIndex = ctx.formulaCache.rangechangeindex;
1340
1358
  if (ctx.formulaCache.rangechangeindex > ctx.formulaRangeHighlight.length) {
1341
1359
  createRangeHightlight(ctx, cellInput.innerHTML, ctx.formulaCache.rangechangeindex);
@@ -1568,6 +1586,7 @@ export function handleColumnHeaderMouseDown(ctx, globalCache, e, container, cell
1568
1586
  ctx.formulaCache.rangedrag_column_start = true;
1569
1587
  ctx.formulaCache.rangestart = false;
1570
1588
  ctx.formulaCache.rangedrag_row_start = false;
1589
+ ctx.formulaCache.rangeSelectionActive = true;
1571
1590
  ctx.formulaCache.selectingRangeIndex = ctx.formulaCache.rangechangeindex;
1572
1591
  if (ctx.formulaCache.rangechangeindex > ctx.formulaRangeHighlight.length) {
1573
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) {
@@ -542,6 +568,7 @@ export function cancelNormalSelected(ctx) {
542
568
  ctx.formulaCache.rangedrag_column_start = false;
543
569
  ctx.formulaCache.rangedrag_row_start = false;
544
570
  ctx.formulaCache.rangeSelectionActive = null;
571
+ ctx.formulaCache.formulaEditorOwner = null;
545
572
  }
546
573
  export function updateCell(ctx, r, c, $input, value, canvas) {
547
574
  var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v;
@@ -617,6 +644,11 @@ export function updateCell(ctx, r, c, $input, value, canvas) {
617
644
  }
618
645
  }
619
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
+ }
620
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;
621
653
  if (shouldClearError) {
622
654
  clearCellError(ctx, r, c);
@@ -1225,6 +1257,12 @@ export function getdatabyselection(ctx, range, sheetId) {
1225
1257
  return data;
1226
1258
  }
1227
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
+ }
1228
1266
  ctx.luckysheetCellUpdate = [row_index, col_index];
1229
1267
  }
1230
1268
  export function getDataBySelectionNoCopy(ctx, range) {
@@ -21,6 +21,7 @@ export declare class FormulaCache {
21
21
  rangedrag_column_start?: boolean;
22
22
  rangedrag_row_start?: boolean;
23
23
  rangeSelectionActive?: boolean | null;
24
+ formulaEditorOwner?: "cell" | "fx" | null;
24
25
  functionRangeIndex?: number[];
25
26
  functionlistMap: any;
26
27
  execFunctionExist?: any[];
@@ -47,6 +48,12 @@ export declare function rangeHightlightselected(ctx: Context, $editor: HTMLDivEl
47
48
  export declare function functionHTMLGenerate(txt: string): string;
48
49
  export declare function getLastFormulaRangeIndex($editor: HTMLDivElement): number | null;
49
50
  export declare function getFormulaRangeIndexAtCaret($editor: HTMLDivElement): number | null;
51
+ export declare function setFormulaEditorOwner(ctx: Context, owner: "cell" | "fx" | null): void;
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;
56
+ export declare function isCaretAtValidFormulaRangeInsertionPoint(editor: HTMLElement | null): boolean;
50
57
  export declare function markRangeSelectionDirty(ctx: Context): void;
51
58
  export declare function getFormulaRangeIndexForKeyboardSync($editor: HTMLDivElement): number | null;
52
59
  export declare function handleFormulaInput(ctx: Context, $copyTo: HTMLDivElement | null | undefined, $editor: HTMLDivElement, kcode: number, preText?: string, refreshRangeSelect?: boolean): void;