@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.
@@ -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,15 +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 } 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";
127
- export function handleGlobalEnter(ctx, cellInput, e, canvas) {
128
- var _a, _b, _c;
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
+ }
139
+ function isLegacyFormulaRangeMode(ctx) {
140
+ return !!ctx.formulaCache.rangestart || !!ctx.formulaCache.rangedrag_column_start || !!ctx.formulaCache.rangedrag_row_start || ctx.formulaCache.rangeSelectionActive === true || israngeseleciton(ctx);
141
+ }
142
+ export function handleGlobalEnter(ctx, cellInput, e, cache, canvas) {
143
+ var _a, _b, _c, _d;
129
144
  if ((e.altKey || e.metaKey) && ctx.luckysheetCellUpdate.length > 0) {
130
145
  var last = (_a = ctx.luckysheet_select_save) === null || _a === void 0 ? void 0 : _a[ctx.luckysheet_select_save.length - 1];
131
146
  if (last && !_.isNil(last.row_focus) && !_.isNil(last.column_focus)) {}
@@ -133,33 +148,61 @@ export function handleGlobalEnter(ctx, cellInput, e, canvas) {
133
148
  } else if (ctx.luckysheetCellUpdate.length > 0) {
134
149
  var lastCellUpdate = _.clone(ctx.luckysheetCellUpdate);
135
150
  updateCell(ctx, ctx.luckysheetCellUpdate[0], ctx.luckysheetCellUpdate[1], cellInput, undefined, canvas);
151
+ cache.enteredEditByTyping = false;
152
+ clearTypeOverPending(cache);
136
153
  ctx.luckysheet_select_save = [{
137
154
  row: [lastCellUpdate[0], lastCellUpdate[0]],
138
155
  column: [lastCellUpdate[1], lastCellUpdate[1]],
139
156
  row_focus: lastCellUpdate[0],
140
157
  column_focus: lastCellUpdate[1]
141
158
  }];
142
- moveHighlightCell(ctx, "down", hideCRCount(ctx, "ArrowDown"), "rangeOfSelect");
159
+ var rowStep = e.shiftKey ? -hideCRCount(ctx, "ArrowUp") : hideCRCount(ctx, "ArrowDown");
160
+ moveHighlightCell(ctx, "down", rowStep, "rangeOfSelect");
143
161
  e.preventDefault();
144
162
  } else {
145
163
  if (((_c = (_b = ctx.luckysheet_select_save) === null || _b === void 0 ? void 0 : _b.length) !== null && _c !== void 0 ? _c : 0) > 0) {
146
164
  var last = ctx.luckysheet_select_save[ctx.luckysheet_select_save.length - 1];
147
165
  var row_index = last.row_focus;
148
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
+ }
149
174
  ctx.luckysheetCellUpdate = [row_index, col_index];
175
+ cache.enteredEditByTyping = false;
176
+ clearTypeOverPending(cache);
150
177
  e.preventDefault();
151
178
  }
152
179
  }
153
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
+ }
154
194
  function moveToEdge(sheetData, key, curr, rowDelta, colDelta, startR, endR, startC, endC, maxRow, maxCol) {
155
- var _a, _b, _c, _d, _e, _f;
195
+ var _a, _b, _c;
156
196
  var selectedLimit = -1;
157
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;
158
198
  var maxRowCol = colDelta === 0 ? maxRow : maxCol;
159
199
  var r = colDelta === 0 ? selectedLimit : curr;
160
200
  var c = colDelta === 0 ? curr : selectedLimit;
161
201
  while (r >= 0 && c >= 0 && (colDelta === 0 ? r : c) < maxRowCol - 1) {
162
- 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))) {
163
206
  break;
164
207
  } else {
165
208
  r += 1 * rowDelta;
@@ -168,83 +211,120 @@ function moveToEdge(sheetData, key, curr, rowDelta, colDelta, startR, endR, star
168
211
  }
169
212
  return colDelta === 0 ? r : c;
170
213
  }
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("=");
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
+ }
171
247
  function handleControlPlusArrowKey(ctx, e, shiftPressed) {
172
- var _a, _b, _c, _d;
248
+ var isFormulaRefMode = isLegacyFormulaRangeMode(ctx);
249
+ if (isFormulaRefMode) {
250
+ ctx.formulaCache.rangeSelectionActive = true;
251
+ }
252
+ if (ctx.luckysheetCellUpdate.length > 0 && !isFormulaRefMode) {
253
+ return;
254
+ }
173
255
  var idx = getSheetIndex(ctx, ctx.currentSheetId);
174
256
  if (_.isNil(idx)) return;
175
257
  var file = ctx.luckysheetfile[idx];
258
+ if (!file || _.isNil(file.row) || _.isNil(file.column)) return;
176
259
  var maxRow = file.row;
177
260
  var maxCol = file.column;
178
- if (_.isNil(maxRow) || _.isNil(maxCol)) return;
179
- var last = ctx.luckysheet_select_save && ctx.luckysheet_select_save.length > 0 ? ctx.luckysheet_select_save[ctx.luckysheet_select_save.length - 1] : undefined;
261
+ var last;
262
+ if (ctx.luckysheet_select_save && ctx.luckysheet_select_save.length > 0) last = ctx.luckysheet_select_save[ctx.luckysheet_select_save.length - 1];
180
263
  if (!last) return;
181
264
  var currR = last.row_focus;
182
265
  var currC = last.column_focus;
183
266
  if (_.isNil(currR) || _.isNil(currC)) return;
184
- var startR = (_a = last.row) === null || _a === void 0 ? void 0 : _a[0];
185
- var endR = (_b = last.row) === null || _b === void 0 ? void 0 : _b[1];
186
- var startC = (_c = last.column) === null || _c === void 0 ? void 0 : _c[0];
187
- var endC = (_d = last.column) === null || _d === void 0 ? void 0 : _d[1];
188
- if (_.isNil(startR) || _.isNil(endR) || _.isNil(startC) || _.isNil(endC)) {
189
- return;
190
- }
267
+ var startR = last.row[0];
268
+ var endR = last.row[1];
269
+ var startC = last.column[0];
270
+ var endC = last.column[1];
191
271
  var horizontalOffset = currC - endC !== 0 ? currC - endC : currC - startC;
192
272
  var verticalOffset = currR - endR !== 0 ? currR - endR : currR - startR;
193
273
  var sheetData = file.data;
194
274
  if (!sheetData) return;
275
+ var selectedLimit;
195
276
  switch (e.key) {
196
277
  case "ArrowUp":
197
- {
198
- var selectedLimit = moveToEdge(sheetData, e.key, currC, -1, 0, startR, endR, startC, endC, maxRow, maxCol);
199
- if (shiftPressed) {
200
- moveHighlightRange(ctx, "down", verticalOffset, "rangeOfSelect");
201
- moveHighlightRange(ctx, "down", selectedLimit - currR, "rangeOfSelect");
202
- } else {
203
- moveHighlightCell(ctx, "down", selectedLimit - currR, "rangeOfSelect");
204
- }
278
+ selectedLimit = moveToEdge(sheetData, e.key, currC, -1, 0, startR, endR, startC, endC, maxRow, maxCol);
279
+ if (shiftPressed) {
280
+ moveHighlightRange(ctx, "down", verticalOffset, "rangeOfSelect");
281
+ moveHighlightRange(ctx, "down", selectedLimit - currR, "rangeOfSelect");
282
+ } else {
283
+ moveHighlightCell(ctx, "down", selectedLimit - currR, "rangeOfSelect");
205
284
  }
206
285
  break;
207
286
  case "ArrowDown":
208
- {
209
- var selectedLimit = moveToEdge(sheetData, e.key, currC, 1, 0, startR, endR, startC, endC, maxRow, maxCol);
210
- if (shiftPressed) {
211
- moveHighlightRange(ctx, "down", verticalOffset, "rangeOfSelect");
212
- moveHighlightRange(ctx, "down", selectedLimit - currR, "rangeOfSelect");
213
- } else {
214
- moveHighlightCell(ctx, "down", selectedLimit - currR, "rangeOfSelect");
215
- }
287
+ selectedLimit = moveToEdge(sheetData, e.key, currC, 1, 0, startR, endR, startC, endC, maxRow, maxCol);
288
+ if (shiftPressed) {
289
+ moveHighlightRange(ctx, "down", verticalOffset, "rangeOfSelect");
290
+ moveHighlightRange(ctx, "down", selectedLimit - currR, "rangeOfSelect");
291
+ } else {
292
+ moveHighlightCell(ctx, "down", selectedLimit - currR, "rangeOfSelect");
216
293
  }
217
294
  break;
218
295
  case "ArrowLeft":
219
- {
220
- var selectedLimit = moveToEdge(sheetData, e.key, currR, 0, -1, startR, endR, startC, endC, maxRow, maxCol);
221
- if (shiftPressed) {
222
- moveHighlightRange(ctx, "right", horizontalOffset, "rangeOfSelect");
223
- moveHighlightRange(ctx, "right", selectedLimit - currC, "rangeOfSelect");
224
- } else {
225
- moveHighlightCell(ctx, "right", selectedLimit - currC, "rangeOfSelect");
226
- }
296
+ selectedLimit = moveToEdge(sheetData, e.key, currR, 0, -1, startR, endR, startC, endC, maxRow, maxCol);
297
+ if (shiftPressed) {
298
+ moveHighlightRange(ctx, "right", horizontalOffset, "rangeOfSelect");
299
+ moveHighlightRange(ctx, "right", selectedLimit - currC, "rangeOfSelect");
300
+ } else {
301
+ moveHighlightCell(ctx, "right", selectedLimit - currC, "rangeOfSelect");
227
302
  }
228
303
  break;
229
304
  case "ArrowRight":
230
- {
231
- var selectedLimit = moveToEdge(sheetData, e.key, currR, 0, 1, startR, endR, startC, endC, maxRow, maxCol);
232
- if (shiftPressed) {
233
- moveHighlightRange(ctx, "right", horizontalOffset, "rangeOfSelect");
234
- moveHighlightRange(ctx, "right", selectedLimit - currC, "rangeOfSelect");
235
- } else {
236
- moveHighlightCell(ctx, "right", selectedLimit - currC, "rangeOfSelect");
237
- }
305
+ selectedLimit = moveToEdge(sheetData, e.key, currR, 0, 1, startR, endR, startC, endC, maxRow, maxCol);
306
+ if (shiftPressed) {
307
+ moveHighlightRange(ctx, "right", horizontalOffset, "rangeOfSelect");
308
+ moveHighlightRange(ctx, "right", selectedLimit - currC, "rangeOfSelect");
309
+ } else {
310
+ moveHighlightCell(ctx, "right", selectedLimit - currC, "rangeOfSelect");
238
311
  }
239
312
  break;
240
313
  default:
241
314
  break;
242
315
  }
243
316
  }
244
- export function handleWithCtrlOrMetaKey(ctx, cache, e, cellInput, fxInput, handleUndo, handleRedo) {
245
- var _a, _b, _c, _d;
317
+ export function handleWithCtrlOrMetaKey(ctx, cache, e, cellInput, fxInput, handleUndo, handleRedo, canvas) {
318
+ var _a, _b, _c, _d, _e, _f, _g;
246
319
  var flowdata = getFlowdata(ctx);
247
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
+ }
248
328
  if (e.shiftKey) {
249
329
  ctx.luckysheet_shiftpositon = _.cloneDeep((_a = ctx.luckysheet_select_save) === null || _a === void 0 ? void 0 : _a[ctx.luckysheet_select_save.length - 1]);
250
330
  ctx.luckysheet_shiftkeydown = true;
@@ -257,6 +337,8 @@ export function handleWithCtrlOrMetaKey(ctx, cache, e, cellInput, fxInput, handl
257
337
  var col_index = last.column_focus;
258
338
  updateCell(ctx, row_index, col_index, cellInput);
259
339
  ctx.luckysheetCellUpdate = [row_index, col_index];
340
+ cache.enteredEditByTyping = false;
341
+ clearTypeOverPending(cache);
260
342
  cache.ignoreWriteCell = true;
261
343
  var value = getNowDateTime(2);
262
344
  cellInput.innerText = value;
@@ -265,6 +347,14 @@ export function handleWithCtrlOrMetaKey(ctx, cache, e, cellInput, fxInput, handl
265
347
  handleRedo();
266
348
  e.stopPropagation();
267
349
  return;
350
+ } else if (e.code === "KeyV") {
351
+ if (((_d = (_c = ctx.luckysheet_select_save) === null || _c === void 0 ? void 0 : _c.length) !== null && _d !== void 0 ? _d : 0) > 1) {
352
+ return;
353
+ }
354
+ selectionCache.isPasteAction = true;
355
+ selectionCache.isPasteValuesOnly = true;
356
+ e.stopPropagation();
357
+ return;
268
358
  }
269
359
  } else if (["ArrowUp", "ArrowDown", "ArrowLeft", "ArrowRight"].includes(e.key)) {
270
360
  handleControlPlusArrowKey(ctx, e, false);
@@ -277,7 +367,14 @@ export function handleWithCtrlOrMetaKey(ctx, cache, e, cellInput, fxInput, handl
277
367
  } else if (e.code === "Backslash") {
278
368
  deleteSelectedCellFormat(ctx);
279
369
  } else if (e.code === "KeyC") {
280
- handleCopy(ctx);
370
+ if (ctx.luckysheetCellUpdate.length > 0) {
371
+ e.preventDefault();
372
+ var sel = window.getSelection();
373
+ var text = sel && !sel.isCollapsed ? sel.toString() : cellInput.innerText;
374
+ (_e = navigator.clipboard) === null || _e === void 0 ? void 0 : _e.writeText(text).catch(function () {});
375
+ } else {
376
+ handleCopy(ctx);
377
+ }
281
378
  e.stopPropagation();
282
379
  return;
283
380
  } else if (e.code === "KeyF") {
@@ -285,7 +382,7 @@ export function handleWithCtrlOrMetaKey(ctx, cache, e, cellInput, fxInput, handl
285
382
  } else if (e.code === "KeyH") {
286
383
  ctx.showReplace = true;
287
384
  } else if (e.code === "KeyV") {
288
- if (((_d = (_c = ctx.luckysheet_select_save) === null || _c === void 0 ? void 0 : _c.length) !== null && _d !== void 0 ? _d : 0) > 1) {
385
+ if (((_g = (_f = ctx.luckysheet_select_save) === null || _f === void 0 ? void 0 : _f.length) !== null && _g !== void 0 ? _g : 0) > 1) {
289
386
  return;
290
387
  }
291
388
  selectionCache.isPasteAction = true;
@@ -333,7 +430,17 @@ export function handleWithCtrlOrMetaKey(ctx, cache, e, cellInput, fxInput, handl
333
430
  }
334
431
  function handleShiftWithArrowKey(ctx, e) {
335
432
  var _a;
336
- if (ctx.luckysheetCellUpdate.length > 0) {
433
+ if (ctx.formulaCache.keyboardRangeSelectionLock === true) {
434
+ return;
435
+ }
436
+ if (ctx.formulaCache.rangeSelectionActive === false && !maybeRecoverDirtyRangeSelection(ctx)) {
437
+ return;
438
+ }
439
+ var isFormulaMode = isLegacyFormulaRangeMode(ctx);
440
+ if (isFormulaMode) {
441
+ ctx.formulaCache.rangeSelectionActive = true;
442
+ }
443
+ if (ctx.luckysheetCellUpdate.length > 0 && !isFormulaMode) {
337
444
  return;
338
445
  }
339
446
  ctx.luckysheet_shiftpositon = _.cloneDeep((_a = ctx.luckysheet_select_save) === null || _a === void 0 ? void 0 : _a[ctx.luckysheet_select_save.length - 1]);
@@ -357,17 +464,18 @@ function handleShiftWithArrowKey(ctx, e) {
357
464
  e.preventDefault();
358
465
  }
359
466
  export function handleArrowKey(ctx, e) {
360
- var _a, _b;
361
- if (ctx.luckysheetCellUpdate.length > 0 || ctx.luckysheet_cell_selected_move || ctx.luckysheet_cell_selected_extend) {
362
- var parser = new DOMParser();
363
- var doc = parser.parseFromString("<div>".concat((_a = document.getElementById("luckysheet-rich-text-editor")) === null || _a === void 0 ? void 0 : _a.innerHTML, "</div>"), "text/html");
364
- var spans = doc.querySelectorAll("span");
365
- var lastSpan = spans[spans.length - 1];
366
- var notFunctionInit = !((_b = document.getElementById("luckysheet-rich-text-editor")) === null || _b === void 0 ? void 0 : _b.innerText.includes("("));
367
- 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) && !_.includes(["="], lastSpan === null || lastSpan === void 0 ? void 0 : lastSpan.innerText) || /^[a-zA-Z]+$/.test(lastSpan === null || lastSpan === void 0 ? void 0 : lastSpan.innerText)) {
368
- return;
369
- }
467
+ if (ctx.formulaCache.keyboardRangeSelectionLock === true) {
468
+ return;
469
+ }
470
+ if (ctx.formulaCache.rangeSelectionActive === false && !maybeRecoverDirtyRangeSelection(ctx)) {
471
+ return;
370
472
  }
473
+ var isFormulaRefMode = isLegacyFormulaRangeMode(ctx);
474
+ if (isFormulaRefMode) ctx.formulaCache.rangeSelectionActive = true;
475
+ if (ctx.luckysheetCellUpdate.length > 0 && !isFormulaRefMode) {
476
+ return;
477
+ }
478
+ if (ctx.luckysheetCellUpdate.length > 0 || ctx.luckysheet_cell_selected_move || ctx.luckysheet_cell_selected_extend) {}
371
479
  var moveCount = hideCRCount(ctx, e.key);
372
480
  switch (e.key) {
373
481
  case "ArrowUp":
@@ -385,12 +493,13 @@ export function handleArrowKey(ctx, e) {
385
493
  default:
386
494
  break;
387
495
  }
496
+ e.preventDefault();
388
497
  }
389
498
  export function handleGlobalKeyDown(ctx, cellInput, fxInput, e, cache, handleUndo, handleRedo, canvas) {
390
- var _a, _b, _c, _d;
499
+ var _a, _b, _c, _d, _e, _f, _g;
391
500
  return __awaiter(this, void 0, void 0, function () {
392
- var handledFlvShortcut, kcode, kstr, allowEdit, isFxInput, ignoredKeys, restCod, last, row_index, col_index, isEditing, inputText, isFormulaEdit, enteredByTyping, last, row_index, col_index;
393
- return __generator(this, function (_e) {
501
+ 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;
502
+ return __generator(this, function (_h) {
394
503
  if (e.shiftKey && e.code === "Space") {
395
504
  e.stopImmediatePropagation();
396
505
  e.stopPropagation();
@@ -451,10 +560,12 @@ export function handleGlobalKeyDown(ctx, cellInput, fxInput, e, cache, handleUnd
451
560
  }
452
561
  if (kstr === "Enter") {
453
562
  if (!allowEdit) return [2];
454
- handleGlobalEnter(ctx, cellInput, e, canvas);
563
+ handleGlobalEnter(ctx, cellInput, e, cache, canvas);
455
564
  } else if (kstr === "Tab") {
456
565
  if (ctx.luckysheetCellUpdate.length > 0) {
457
566
  updateCell(ctx, ctx.luckysheetCellUpdate[0], ctx.luckysheetCellUpdate[1], cellInput, undefined, canvas);
567
+ cache.enteredEditByTyping = false;
568
+ clearTypeOverPending(cache);
458
569
  }
459
570
  if (e.shiftKey) {
460
571
  moveHighlightCell(ctx, "right", -hideCRCount(ctx, "ArrowLeft"), "rangeOfSelect");
@@ -471,20 +582,34 @@ export function handleGlobalKeyDown(ctx, cellInput, fxInput, e, cache, handleUnd
471
582
  if (!last) return [2];
472
583
  row_index = last.row_focus;
473
584
  col_index = last.column_focus;
585
+ if (!_.isNil(row_index) && !_.isNil(col_index)) {
586
+ flowdataF2 = getFlowdata(ctx);
587
+ cellF2 = (_d = flowdataF2 === null || flowdataF2 === void 0 ? void 0 : flowdataF2[row_index]) === null || _d === void 0 ? void 0 : _d[col_index];
588
+ if ((cellF2 === null || cellF2 === void 0 ? void 0 : cellF2.f) != null && String(cellF2.f).trim() !== "") {
589
+ suppressFormulaRangeSelectionForInitialEdit(ctx);
590
+ }
591
+ }
592
+ cache.enteredEditByTyping = false;
593
+ clearTypeOverPending(cache);
474
594
  ctx.luckysheetCellUpdate = [row_index, col_index];
475
595
  e.preventDefault();
476
596
  } else if (kstr === "F4" && ctx.luckysheetCellUpdate.length > 0) {
477
597
  e.preventDefault();
478
598
  } else if (kstr === "Escape" && ctx.luckysheetCellUpdate.length > 0) {
599
+ cache.enteredEditByTyping = false;
600
+ clearTypeOverPending(cache);
479
601
  cancelNormalSelected(ctx);
480
602
  moveHighlightCell(ctx, "down", 0, "rangeOfSelect");
481
603
  e.preventDefault();
482
604
  } else {
483
605
  if (e.ctrlKey || e.metaKey) {
484
- handleWithCtrlOrMetaKey(ctx, cache, e, cellInput, fxInput, handleUndo, handleRedo);
606
+ handleWithCtrlOrMetaKey(ctx, cache, e, cellInput, fxInput, handleUndo, handleRedo, canvas);
485
607
  return [2];
486
608
  }
487
609
  if (e.shiftKey && (kstr === "ArrowUp" || kstr === "ArrowDown" || kstr === "ArrowLeft" || kstr === "ArrowRight")) {
610
+ if (isDirectPlainTextCellEdit(ctx, cache, cellInput, fxInput)) {
611
+ commitDirectPlainCellEdit(ctx, cache, cellInput, canvas);
612
+ }
488
613
  handleShiftWithArrowKey(ctx, e);
489
614
  } else if (kstr === "Escape") {
490
615
  ctx.contextMenu = {};
@@ -493,18 +618,23 @@ export function handleGlobalKeyDown(ctx, cellInput, fxInput, e, cache, handleUnd
493
618
  if (ctx.activeImg != null) {
494
619
  removeActiveImage(ctx);
495
620
  } else {
621
+ if (ctx.formulaCache.rangeSelectionActive === true) {
622
+ markRangeSelectionDirty(ctx);
623
+ }
496
624
  deleteSelectedCellText(ctx);
497
625
  }
498
626
  jfrefreshgrid(ctx, null, undefined);
499
627
  e.preventDefault();
500
628
  } else if (kstr === "ArrowUp" || kstr === "ArrowDown" || kstr === "ArrowLeft" || kstr === "ArrowRight") {
501
629
  isEditing = ctx.luckysheetCellUpdate.length > 0;
502
- inputText = (_d = cellInput === null || cellInput === void 0 ? void 0 : cellInput.innerText) !== null && _d !== void 0 ? _d : "";
503
- isFormulaEdit = isEditing && inputText.trim().startsWith("=");
630
+ inlineText = (_e = cellInput === null || cellInput === void 0 ? void 0 : cellInput.innerText) !== null && _e !== void 0 ? _e : "";
631
+ fxText = (_f = fxInput === null || fxInput === void 0 ? void 0 : fxInput.innerText) !== null && _f !== void 0 ? _f : "";
632
+ isFormulaEdit = isEditing && (inlineText.trim().startsWith("=") || fxText.trim().startsWith("="));
504
633
  enteredByTyping = cache.enteredEditByTyping === true;
505
- if (isEditing && !isFormulaEdit && enteredByTyping) {
634
+ if (isEditing && !isFormulaEdit && enteredByTyping && !e.shiftKey) {
506
635
  updateCell(ctx, ctx.luckysheetCellUpdate[0], ctx.luckysheetCellUpdate[1], cellInput, undefined, canvas);
507
636
  cache.enteredEditByTyping = false;
637
+ clearTypeOverPending(cache);
508
638
  handleArrowKey(ctx, e);
509
639
  e.preventDefault();
510
640
  } else {
@@ -516,12 +646,33 @@ export function handleGlobalKeyDown(ctx, cellInput, fxInput, e, cache, handleUnd
516
646
  last = ctx.luckysheet_select_save[ctx.luckysheet_select_save.length - 1];
517
647
  row_index = last.row_focus;
518
648
  col_index = last.column_focus;
649
+ if (_.isNil(row_index) || _.isNil(col_index)) return [2];
650
+ flowdata = getFlowdata(ctx);
651
+ cellAt = (_g = flowdata === null || flowdata === void 0 ? void 0 : flowdata[row_index]) === null || _g === void 0 ? void 0 : _g[col_index];
652
+ existingFormula = (cellAt === null || cellAt === void 0 ? void 0 : cellAt.f) != null && String(cellAt.f).trim() !== "" ? String(cellAt.f).replace(/[\r\n]/g, "") : null;
653
+ if (existingFormula != null) {
654
+ suppressFormulaRangeSelectionForInitialEdit(ctx);
655
+ }
519
656
  ctx.luckysheetCellUpdate = [row_index, col_index];
520
657
  cache.overwriteCell = true;
521
- cache.overwriteCellFirstChar = e.key;
658
+ cache.pendingTypeOverCell = [row_index, col_index];
659
+ setFormulaEditorOwner(ctx, "cell");
522
660
  cache.enteredEditByTyping = true;
523
- handleFormulaInput(ctx, fxInput, cellInput, kcode);
524
- e.preventDefault();
661
+ cellInput.focus();
662
+ initial = getTypeOverInitialContent(e);
663
+ if (initial !== undefined) {
664
+ cellInput.textContent = initial;
665
+ if (fxInput) fxInput.textContent = initial;
666
+ handleFormulaInput(ctx, fxInput, cellInput, kcode);
667
+ e.preventDefault();
668
+ } else {
669
+ cellInput.textContent = "";
670
+ if (fxInput) fxInput.textContent = "";
671
+ handleFormulaInput(ctx, fxInput, cellInput, kcode);
672
+ }
673
+ queueMicrotask(function () {
674
+ moveToEnd(cellInput);
675
+ });
525
676
  }
526
677
  }
527
678
  }