@fileverse-dev/fortune-core 1.3.12-mixed-a → 1.3.13-create-1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -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,8 +136,21 @@ var __generator = void 0 && (void 0).__generator || function (thisArg, body) {
134
136
  };
135
137
  }
136
138
  };
137
- function handleGlobalEnter(ctx, cellInput, e, canvas) {
138
- var _a, _b, _c;
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
+ }
149
+ function isLegacyFormulaRangeMode(ctx) {
150
+ return !!ctx.formulaCache.rangestart || !!ctx.formulaCache.rangedrag_column_start || !!ctx.formulaCache.rangedrag_row_start || ctx.formulaCache.rangeSelectionActive === true || (0, _formula.israngeseleciton)(ctx);
151
+ }
152
+ function handleGlobalEnter(ctx, cellInput, e, cache, canvas) {
153
+ var _a, _b, _c, _d;
139
154
  if ((e.altKey || e.metaKey) && ctx.luckysheetCellUpdate.length > 0) {
140
155
  var last = (_a = ctx.luckysheet_select_save) === null || _a === void 0 ? void 0 : _a[ctx.luckysheet_select_save.length - 1];
141
156
  if (last && !_lodash.default.isNil(last.row_focus) && !_lodash.default.isNil(last.column_focus)) {}
@@ -143,33 +158,61 @@ function handleGlobalEnter(ctx, cellInput, e, canvas) {
143
158
  } else if (ctx.luckysheetCellUpdate.length > 0) {
144
159
  var lastCellUpdate = _lodash.default.clone(ctx.luckysheetCellUpdate);
145
160
  (0, _cell.updateCell)(ctx, ctx.luckysheetCellUpdate[0], ctx.luckysheetCellUpdate[1], cellInput, undefined, canvas);
161
+ cache.enteredEditByTyping = false;
162
+ clearTypeOverPending(cache);
146
163
  ctx.luckysheet_select_save = [{
147
164
  row: [lastCellUpdate[0], lastCellUpdate[0]],
148
165
  column: [lastCellUpdate[1], lastCellUpdate[1]],
149
166
  row_focus: lastCellUpdate[0],
150
167
  column_focus: lastCellUpdate[1]
151
168
  }];
152
- (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");
153
171
  e.preventDefault();
154
172
  } else {
155
173
  if (((_c = (_b = ctx.luckysheet_select_save) === null || _b === void 0 ? void 0 : _b.length) !== null && _c !== void 0 ? _c : 0) > 0) {
156
174
  var last = ctx.luckysheet_select_save[ctx.luckysheet_select_save.length - 1];
157
175
  var row_index = last.row_focus;
158
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
+ }
159
184
  ctx.luckysheetCellUpdate = [row_index, col_index];
185
+ cache.enteredEditByTyping = false;
186
+ clearTypeOverPending(cache);
160
187
  e.preventDefault();
161
188
  }
162
189
  }
163
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
+ }
164
204
  function moveToEdge(sheetData, key, curr, rowDelta, colDelta, startR, endR, startC, endC, maxRow, maxCol) {
165
- var _a, _b, _c, _d, _e, _f;
205
+ var _a, _b, _c;
166
206
  var selectedLimit = -1;
167
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;
168
208
  var maxRowCol = colDelta === 0 ? maxRow : maxCol;
169
209
  var r = colDelta === 0 ? selectedLimit : curr;
170
210
  var c = colDelta === 0 ? curr : selectedLimit;
171
211
  while (r >= 0 && c >= 0 && (colDelta === 0 ? r : c) < maxRowCol - 1) {
172
- 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))) {
173
216
  break;
174
217
  } else {
175
218
  r += 1 * rowDelta;
@@ -178,83 +221,120 @@ function moveToEdge(sheetData, key, curr, rowDelta, colDelta, startR, endR, star
178
221
  }
179
222
  return colDelta === 0 ? r : c;
180
223
  }
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("=");
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
+ }
181
257
  function handleControlPlusArrowKey(ctx, e, shiftPressed) {
182
- var _a, _b, _c, _d;
258
+ var isFormulaRefMode = isLegacyFormulaRangeMode(ctx);
259
+ if (isFormulaRefMode) {
260
+ ctx.formulaCache.rangeSelectionActive = true;
261
+ }
262
+ if (ctx.luckysheetCellUpdate.length > 0 && !isFormulaRefMode) {
263
+ return;
264
+ }
183
265
  var idx = (0, _utils.getSheetIndex)(ctx, ctx.currentSheetId);
184
266
  if (_lodash.default.isNil(idx)) return;
185
267
  var file = ctx.luckysheetfile[idx];
268
+ if (!file || _lodash.default.isNil(file.row) || _lodash.default.isNil(file.column)) return;
186
269
  var maxRow = file.row;
187
270
  var maxCol = file.column;
188
- if (_lodash.default.isNil(maxRow) || _lodash.default.isNil(maxCol)) return;
189
- var last = ctx.luckysheet_select_save && ctx.luckysheet_select_save.length > 0 ? ctx.luckysheet_select_save[ctx.luckysheet_select_save.length - 1] : undefined;
271
+ var last;
272
+ if (ctx.luckysheet_select_save && ctx.luckysheet_select_save.length > 0) last = ctx.luckysheet_select_save[ctx.luckysheet_select_save.length - 1];
190
273
  if (!last) return;
191
274
  var currR = last.row_focus;
192
275
  var currC = last.column_focus;
193
276
  if (_lodash.default.isNil(currR) || _lodash.default.isNil(currC)) return;
194
- var startR = (_a = last.row) === null || _a === void 0 ? void 0 : _a[0];
195
- var endR = (_b = last.row) === null || _b === void 0 ? void 0 : _b[1];
196
- var startC = (_c = last.column) === null || _c === void 0 ? void 0 : _c[0];
197
- var endC = (_d = last.column) === null || _d === void 0 ? void 0 : _d[1];
198
- if (_lodash.default.isNil(startR) || _lodash.default.isNil(endR) || _lodash.default.isNil(startC) || _lodash.default.isNil(endC)) {
199
- return;
200
- }
277
+ var startR = last.row[0];
278
+ var endR = last.row[1];
279
+ var startC = last.column[0];
280
+ var endC = last.column[1];
201
281
  var horizontalOffset = currC - endC !== 0 ? currC - endC : currC - startC;
202
282
  var verticalOffset = currR - endR !== 0 ? currR - endR : currR - startR;
203
283
  var sheetData = file.data;
204
284
  if (!sheetData) return;
285
+ var selectedLimit;
205
286
  switch (e.key) {
206
287
  case "ArrowUp":
207
- {
208
- var selectedLimit = moveToEdge(sheetData, e.key, currC, -1, 0, startR, endR, startC, endC, maxRow, maxCol);
209
- if (shiftPressed) {
210
- (0, _selection.moveHighlightRange)(ctx, "down", verticalOffset, "rangeOfSelect");
211
- (0, _selection.moveHighlightRange)(ctx, "down", selectedLimit - currR, "rangeOfSelect");
212
- } else {
213
- (0, _selection.moveHighlightCell)(ctx, "down", selectedLimit - currR, "rangeOfSelect");
214
- }
288
+ selectedLimit = moveToEdge(sheetData, e.key, currC, -1, 0, startR, endR, startC, endC, maxRow, maxCol);
289
+ if (shiftPressed) {
290
+ (0, _selection.moveHighlightRange)(ctx, "down", verticalOffset, "rangeOfSelect");
291
+ (0, _selection.moveHighlightRange)(ctx, "down", selectedLimit - currR, "rangeOfSelect");
292
+ } else {
293
+ (0, _selection.moveHighlightCell)(ctx, "down", selectedLimit - currR, "rangeOfSelect");
215
294
  }
216
295
  break;
217
296
  case "ArrowDown":
218
- {
219
- var selectedLimit = moveToEdge(sheetData, e.key, currC, 1, 0, startR, endR, startC, endC, maxRow, maxCol);
220
- if (shiftPressed) {
221
- (0, _selection.moveHighlightRange)(ctx, "down", verticalOffset, "rangeOfSelect");
222
- (0, _selection.moveHighlightRange)(ctx, "down", selectedLimit - currR, "rangeOfSelect");
223
- } else {
224
- (0, _selection.moveHighlightCell)(ctx, "down", selectedLimit - currR, "rangeOfSelect");
225
- }
297
+ selectedLimit = moveToEdge(sheetData, e.key, currC, 1, 0, startR, endR, startC, endC, maxRow, maxCol);
298
+ if (shiftPressed) {
299
+ (0, _selection.moveHighlightRange)(ctx, "down", verticalOffset, "rangeOfSelect");
300
+ (0, _selection.moveHighlightRange)(ctx, "down", selectedLimit - currR, "rangeOfSelect");
301
+ } else {
302
+ (0, _selection.moveHighlightCell)(ctx, "down", selectedLimit - currR, "rangeOfSelect");
226
303
  }
227
304
  break;
228
305
  case "ArrowLeft":
229
- {
230
- var selectedLimit = moveToEdge(sheetData, e.key, currR, 0, -1, startR, endR, startC, endC, maxRow, maxCol);
231
- if (shiftPressed) {
232
- (0, _selection.moveHighlightRange)(ctx, "right", horizontalOffset, "rangeOfSelect");
233
- (0, _selection.moveHighlightRange)(ctx, "right", selectedLimit - currC, "rangeOfSelect");
234
- } else {
235
- (0, _selection.moveHighlightCell)(ctx, "right", selectedLimit - currC, "rangeOfSelect");
236
- }
306
+ selectedLimit = moveToEdge(sheetData, e.key, currR, 0, -1, startR, endR, startC, endC, maxRow, maxCol);
307
+ if (shiftPressed) {
308
+ (0, _selection.moveHighlightRange)(ctx, "right", horizontalOffset, "rangeOfSelect");
309
+ (0, _selection.moveHighlightRange)(ctx, "right", selectedLimit - currC, "rangeOfSelect");
310
+ } else {
311
+ (0, _selection.moveHighlightCell)(ctx, "right", selectedLimit - currC, "rangeOfSelect");
237
312
  }
238
313
  break;
239
314
  case "ArrowRight":
240
- {
241
- var selectedLimit = moveToEdge(sheetData, e.key, currR, 0, 1, startR, endR, startC, endC, maxRow, maxCol);
242
- if (shiftPressed) {
243
- (0, _selection.moveHighlightRange)(ctx, "right", horizontalOffset, "rangeOfSelect");
244
- (0, _selection.moveHighlightRange)(ctx, "right", selectedLimit - currC, "rangeOfSelect");
245
- } else {
246
- (0, _selection.moveHighlightCell)(ctx, "right", selectedLimit - currC, "rangeOfSelect");
247
- }
315
+ selectedLimit = moveToEdge(sheetData, e.key, currR, 0, 1, startR, endR, startC, endC, maxRow, maxCol);
316
+ if (shiftPressed) {
317
+ (0, _selection.moveHighlightRange)(ctx, "right", horizontalOffset, "rangeOfSelect");
318
+ (0, _selection.moveHighlightRange)(ctx, "right", selectedLimit - currC, "rangeOfSelect");
319
+ } else {
320
+ (0, _selection.moveHighlightCell)(ctx, "right", selectedLimit - currC, "rangeOfSelect");
248
321
  }
249
322
  break;
250
323
  default:
251
324
  break;
252
325
  }
253
326
  }
254
- function handleWithCtrlOrMetaKey(ctx, cache, e, cellInput, fxInput, handleUndo, handleRedo) {
255
- var _a, _b, _c, _d;
327
+ function handleWithCtrlOrMetaKey(ctx, cache, e, cellInput, fxInput, handleUndo, handleRedo, canvas) {
328
+ var _a, _b, _c, _d, _e, _f, _g;
256
329
  var flowdata = (0, _context.getFlowdata)(ctx);
257
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
+ }
258
338
  if (e.shiftKey) {
259
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]);
260
340
  ctx.luckysheet_shiftkeydown = true;
@@ -267,6 +347,8 @@ function handleWithCtrlOrMetaKey(ctx, cache, e, cellInput, fxInput, handleUndo,
267
347
  var col_index = last.column_focus;
268
348
  (0, _cell.updateCell)(ctx, row_index, col_index, cellInput);
269
349
  ctx.luckysheetCellUpdate = [row_index, col_index];
350
+ cache.enteredEditByTyping = false;
351
+ clearTypeOverPending(cache);
270
352
  cache.ignoreWriteCell = true;
271
353
  var value = (0, _utils.getNowDateTime)(2);
272
354
  cellInput.innerText = value;
@@ -275,6 +357,14 @@ function handleWithCtrlOrMetaKey(ctx, cache, e, cellInput, fxInput, handleUndo,
275
357
  handleRedo();
276
358
  e.stopPropagation();
277
359
  return;
360
+ } else if (e.code === "KeyV") {
361
+ if (((_d = (_c = ctx.luckysheet_select_save) === null || _c === void 0 ? void 0 : _c.length) !== null && _d !== void 0 ? _d : 0) > 1) {
362
+ return;
363
+ }
364
+ _selection.selectionCache.isPasteAction = true;
365
+ _selection.selectionCache.isPasteValuesOnly = true;
366
+ e.stopPropagation();
367
+ return;
278
368
  }
279
369
  } else if (["ArrowUp", "ArrowDown", "ArrowLeft", "ArrowRight"].includes(e.key)) {
280
370
  handleControlPlusArrowKey(ctx, e, false);
@@ -287,7 +377,14 @@ function handleWithCtrlOrMetaKey(ctx, cache, e, cellInput, fxInput, handleUndo,
287
377
  } else if (e.code === "Backslash") {
288
378
  (0, _selection.deleteSelectedCellFormat)(ctx);
289
379
  } else if (e.code === "KeyC") {
290
- (0, _copy.handleCopy)(ctx);
380
+ if (ctx.luckysheetCellUpdate.length > 0) {
381
+ e.preventDefault();
382
+ var sel = window.getSelection();
383
+ var text = sel && !sel.isCollapsed ? sel.toString() : cellInput.innerText;
384
+ (_e = navigator.clipboard) === null || _e === void 0 ? void 0 : _e.writeText(text).catch(function () {});
385
+ } else {
386
+ (0, _copy.handleCopy)(ctx);
387
+ }
291
388
  e.stopPropagation();
292
389
  return;
293
390
  } else if (e.code === "KeyF") {
@@ -295,7 +392,7 @@ function handleWithCtrlOrMetaKey(ctx, cache, e, cellInput, fxInput, handleUndo,
295
392
  } else if (e.code === "KeyH") {
296
393
  ctx.showReplace = true;
297
394
  } else if (e.code === "KeyV") {
298
- if (((_d = (_c = ctx.luckysheet_select_save) === null || _c === void 0 ? void 0 : _c.length) !== null && _d !== void 0 ? _d : 0) > 1) {
395
+ if (((_g = (_f = ctx.luckysheet_select_save) === null || _f === void 0 ? void 0 : _f.length) !== null && _g !== void 0 ? _g : 0) > 1) {
299
396
  return;
300
397
  }
301
398
  _selection.selectionCache.isPasteAction = true;
@@ -343,7 +440,17 @@ function handleWithCtrlOrMetaKey(ctx, cache, e, cellInput, fxInput, handleUndo,
343
440
  }
344
441
  function handleShiftWithArrowKey(ctx, e) {
345
442
  var _a;
346
- if (ctx.luckysheetCellUpdate.length > 0) {
443
+ if (ctx.formulaCache.keyboardRangeSelectionLock === true) {
444
+ return;
445
+ }
446
+ if (ctx.formulaCache.rangeSelectionActive === false && !(0, _formula.maybeRecoverDirtyRangeSelection)(ctx)) {
447
+ return;
448
+ }
449
+ var isFormulaMode = isLegacyFormulaRangeMode(ctx);
450
+ if (isFormulaMode) {
451
+ ctx.formulaCache.rangeSelectionActive = true;
452
+ }
453
+ if (ctx.luckysheetCellUpdate.length > 0 && !isFormulaMode) {
347
454
  return;
348
455
  }
349
456
  ctx.luckysheet_shiftpositon = _lodash.default.cloneDeep((_a = ctx.luckysheet_select_save) === null || _a === void 0 ? void 0 : _a[ctx.luckysheet_select_save.length - 1]);
@@ -367,17 +474,18 @@ function handleShiftWithArrowKey(ctx, e) {
367
474
  e.preventDefault();
368
475
  }
369
476
  function handleArrowKey(ctx, e) {
370
- var _a, _b;
371
- if (ctx.luckysheetCellUpdate.length > 0 || ctx.luckysheet_cell_selected_move || ctx.luckysheet_cell_selected_extend) {
372
- var parser = new DOMParser();
373
- var doc = parser.parseFromString("<div>".concat((_a = document.getElementById("luckysheet-rich-text-editor")) === null || _a === void 0 ? void 0 : _a.innerHTML, "</div>"), "text/html");
374
- var spans = doc.querySelectorAll("span");
375
- var lastSpan = spans[spans.length - 1];
376
- var notFunctionInit = !((_b = document.getElementById("luckysheet-rich-text-editor")) === null || _b === void 0 ? void 0 : _b.innerText.includes("("));
377
- if ((lastSpan === null || lastSpan === void 0 ? void 0 : lastSpan.innerText.includes(")")) || (lastSpan === null || lastSpan === void 0 ? void 0 : lastSpan.innerText.includes('"')) || notFunctionInit && /^[a-zA-Z]+$/.test(lastSpan === null || lastSpan === void 0 ? void 0 : lastSpan.innerText) && !_lodash.default.includes(["="], lastSpan === null || lastSpan === void 0 ? void 0 : lastSpan.innerText) || /^[a-zA-Z]+$/.test(lastSpan === null || lastSpan === void 0 ? void 0 : lastSpan.innerText)) {
378
- return;
379
- }
477
+ if (ctx.formulaCache.keyboardRangeSelectionLock === true) {
478
+ return;
479
+ }
480
+ if (ctx.formulaCache.rangeSelectionActive === false && !(0, _formula.maybeRecoverDirtyRangeSelection)(ctx)) {
481
+ return;
380
482
  }
483
+ var isFormulaRefMode = isLegacyFormulaRangeMode(ctx);
484
+ if (isFormulaRefMode) ctx.formulaCache.rangeSelectionActive = true;
485
+ if (ctx.luckysheetCellUpdate.length > 0 && !isFormulaRefMode) {
486
+ return;
487
+ }
488
+ if (ctx.luckysheetCellUpdate.length > 0 || ctx.luckysheet_cell_selected_move || ctx.luckysheet_cell_selected_extend) {}
381
489
  var moveCount = (0, _2.hideCRCount)(ctx, e.key);
382
490
  switch (e.key) {
383
491
  case "ArrowUp":
@@ -395,12 +503,13 @@ function handleArrowKey(ctx, e) {
395
503
  default:
396
504
  break;
397
505
  }
506
+ e.preventDefault();
398
507
  }
399
508
  function handleGlobalKeyDown(ctx, cellInput, fxInput, e, cache, handleUndo, handleRedo, canvas) {
400
- var _a, _b, _c, _d;
509
+ var _a, _b, _c, _d, _e, _f, _g;
401
510
  return __awaiter(this, void 0, void 0, function () {
402
- var handledFlvShortcut, kcode, kstr, allowEdit, isFxInput, ignoredKeys, restCod, last, row_index, col_index, isEditing, inputText, isFormulaEdit, enteredByTyping, last, row_index, col_index;
403
- return __generator(this, function (_e) {
511
+ 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;
512
+ return __generator(this, function (_h) {
404
513
  if (e.shiftKey && e.code === "Space") {
405
514
  e.stopImmediatePropagation();
406
515
  e.stopPropagation();
@@ -461,10 +570,12 @@ function handleGlobalKeyDown(ctx, cellInput, fxInput, e, cache, handleUndo, hand
461
570
  }
462
571
  if (kstr === "Enter") {
463
572
  if (!allowEdit) return [2];
464
- handleGlobalEnter(ctx, cellInput, e, canvas);
573
+ handleGlobalEnter(ctx, cellInput, e, cache, canvas);
465
574
  } else if (kstr === "Tab") {
466
575
  if (ctx.luckysheetCellUpdate.length > 0) {
467
576
  (0, _cell.updateCell)(ctx, ctx.luckysheetCellUpdate[0], ctx.luckysheetCellUpdate[1], cellInput, undefined, canvas);
577
+ cache.enteredEditByTyping = false;
578
+ clearTypeOverPending(cache);
468
579
  }
469
580
  if (e.shiftKey) {
470
581
  (0, _selection.moveHighlightCell)(ctx, "right", -(0, _2.hideCRCount)(ctx, "ArrowLeft"), "rangeOfSelect");
@@ -481,20 +592,34 @@ function handleGlobalKeyDown(ctx, cellInput, fxInput, e, cache, handleUndo, hand
481
592
  if (!last) return [2];
482
593
  row_index = last.row_focus;
483
594
  col_index = last.column_focus;
595
+ if (!_lodash.default.isNil(row_index) && !_lodash.default.isNil(col_index)) {
596
+ flowdataF2 = (0, _context.getFlowdata)(ctx);
597
+ cellF2 = (_d = flowdataF2 === null || flowdataF2 === void 0 ? void 0 : flowdataF2[row_index]) === null || _d === void 0 ? void 0 : _d[col_index];
598
+ if ((cellF2 === null || cellF2 === void 0 ? void 0 : cellF2.f) != null && String(cellF2.f).trim() !== "") {
599
+ (0, _formula.suppressFormulaRangeSelectionForInitialEdit)(ctx);
600
+ }
601
+ }
602
+ cache.enteredEditByTyping = false;
603
+ clearTypeOverPending(cache);
484
604
  ctx.luckysheetCellUpdate = [row_index, col_index];
485
605
  e.preventDefault();
486
606
  } else if (kstr === "F4" && ctx.luckysheetCellUpdate.length > 0) {
487
607
  e.preventDefault();
488
608
  } else if (kstr === "Escape" && ctx.luckysheetCellUpdate.length > 0) {
609
+ cache.enteredEditByTyping = false;
610
+ clearTypeOverPending(cache);
489
611
  (0, _cell.cancelNormalSelected)(ctx);
490
612
  (0, _selection.moveHighlightCell)(ctx, "down", 0, "rangeOfSelect");
491
613
  e.preventDefault();
492
614
  } else {
493
615
  if (e.ctrlKey || e.metaKey) {
494
- handleWithCtrlOrMetaKey(ctx, cache, e, cellInput, fxInput, handleUndo, handleRedo);
616
+ handleWithCtrlOrMetaKey(ctx, cache, e, cellInput, fxInput, handleUndo, handleRedo, canvas);
495
617
  return [2];
496
618
  }
497
619
  if (e.shiftKey && (kstr === "ArrowUp" || kstr === "ArrowDown" || kstr === "ArrowLeft" || kstr === "ArrowRight")) {
620
+ if (isDirectPlainTextCellEdit(ctx, cache, cellInput, fxInput)) {
621
+ commitDirectPlainCellEdit(ctx, cache, cellInput, canvas);
622
+ }
498
623
  handleShiftWithArrowKey(ctx, e);
499
624
  } else if (kstr === "Escape") {
500
625
  ctx.contextMenu = {};
@@ -503,18 +628,23 @@ function handleGlobalKeyDown(ctx, cellInput, fxInput, e, cache, handleUndo, hand
503
628
  if (ctx.activeImg != null) {
504
629
  (0, _2.removeActiveImage)(ctx);
505
630
  } else {
631
+ if (ctx.formulaCache.rangeSelectionActive === true) {
632
+ (0, _formula.markRangeSelectionDirty)(ctx);
633
+ }
506
634
  (0, _selection.deleteSelectedCellText)(ctx);
507
635
  }
508
636
  (0, _refresh.jfrefreshgrid)(ctx, null, undefined);
509
637
  e.preventDefault();
510
638
  } else if (kstr === "ArrowUp" || kstr === "ArrowDown" || kstr === "ArrowLeft" || kstr === "ArrowRight") {
511
639
  isEditing = ctx.luckysheetCellUpdate.length > 0;
512
- inputText = (_d = cellInput === null || cellInput === void 0 ? void 0 : cellInput.innerText) !== null && _d !== void 0 ? _d : "";
513
- isFormulaEdit = isEditing && inputText.trim().startsWith("=");
640
+ inlineText = (_e = cellInput === null || cellInput === void 0 ? void 0 : cellInput.innerText) !== null && _e !== void 0 ? _e : "";
641
+ fxText = (_f = fxInput === null || fxInput === void 0 ? void 0 : fxInput.innerText) !== null && _f !== void 0 ? _f : "";
642
+ isFormulaEdit = isEditing && (inlineText.trim().startsWith("=") || fxText.trim().startsWith("="));
514
643
  enteredByTyping = cache.enteredEditByTyping === true;
515
- if (isEditing && !isFormulaEdit && enteredByTyping) {
644
+ if (isEditing && !isFormulaEdit && enteredByTyping && !e.shiftKey) {
516
645
  (0, _cell.updateCell)(ctx, ctx.luckysheetCellUpdate[0], ctx.luckysheetCellUpdate[1], cellInput, undefined, canvas);
517
646
  cache.enteredEditByTyping = false;
647
+ clearTypeOverPending(cache);
518
648
  handleArrowKey(ctx, e);
519
649
  e.preventDefault();
520
650
  } else {
@@ -526,12 +656,33 @@ function handleGlobalKeyDown(ctx, cellInput, fxInput, e, cache, handleUndo, hand
526
656
  last = ctx.luckysheet_select_save[ctx.luckysheet_select_save.length - 1];
527
657
  row_index = last.row_focus;
528
658
  col_index = last.column_focus;
659
+ if (_lodash.default.isNil(row_index) || _lodash.default.isNil(col_index)) return [2];
660
+ flowdata = (0, _context.getFlowdata)(ctx);
661
+ cellAt = (_g = flowdata === null || flowdata === void 0 ? void 0 : flowdata[row_index]) === null || _g === void 0 ? void 0 : _g[col_index];
662
+ existingFormula = (cellAt === null || cellAt === void 0 ? void 0 : cellAt.f) != null && String(cellAt.f).trim() !== "" ? String(cellAt.f).replace(/[\r\n]/g, "") : null;
663
+ if (existingFormula != null) {
664
+ (0, _formula.suppressFormulaRangeSelectionForInitialEdit)(ctx);
665
+ }
529
666
  ctx.luckysheetCellUpdate = [row_index, col_index];
530
667
  cache.overwriteCell = true;
531
- cache.overwriteCellFirstChar = e.key;
668
+ cache.pendingTypeOverCell = [row_index, col_index];
669
+ (0, _formula.setFormulaEditorOwner)(ctx, "cell");
532
670
  cache.enteredEditByTyping = true;
533
- (0, _formula.handleFormulaInput)(ctx, fxInput, cellInput, kcode);
534
- e.preventDefault();
671
+ cellInput.focus();
672
+ initial = getTypeOverInitialContent(e);
673
+ if (initial !== undefined) {
674
+ cellInput.textContent = initial;
675
+ if (fxInput) fxInput.textContent = initial;
676
+ (0, _formula.handleFormulaInput)(ctx, fxInput, cellInput, kcode);
677
+ e.preventDefault();
678
+ } else {
679
+ cellInput.textContent = "";
680
+ if (fxInput) fxInput.textContent = "";
681
+ (0, _formula.handleFormulaInput)(ctx, fxInput, cellInput, kcode);
682
+ }
683
+ queueMicrotask(function () {
684
+ (0, _cursor.moveToEnd)(cellInput);
685
+ });
535
686
  }
536
687
  }
537
688
  }