@fileverse-dev/fortune-react 1.3.13-create-2 → 1.3.13-create-4

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,7 +12,7 @@ import usePrevious from "../../hooks/usePrevious";
12
12
  import { useFormulaEditorHistory } from "../../hooks/useFormulaEditorHistory";
13
13
  import { useRerenderOnFormulaCaret } from "../../hooks/useRerenderOnFormulaCaret";
14
14
  import { LucideIcon } from "../SheetOverlay/LucideIcon";
15
- import { countCommasBeforeCursor, getCursorPosition, getFunctionNameFromFormulaCaretSpans, isLetterNumberPattern, setCursorPosition, buildFormulaSuggestionText, shouldShowFormulaFunctionList } from "../SheetOverlay/helper";
15
+ import { countCommasBeforeCursor, getCursorPosition, getFunctionNameFromFormulaCaretSpans, isEditorUndoRedoKeyEvent, isLetterNumberPattern, setCursorPosition, buildFormulaSuggestionText, shouldShowFormulaFunctionList } from "../SheetOverlay/helper";
16
16
  import { isFormulaSegmentBoundaryKey } from "../SheetOverlay/formula-segment-boundary";
17
17
  var FxEditor = function FxEditor() {
18
18
  var _a, _b, _c;
@@ -32,12 +32,11 @@ var FxEditor = function FxEditor() {
32
32
  refs = _g.refs;
33
33
  var lastKeyDownEventRef = useRef(null);
34
34
  var _h = useFormulaEditorHistory(refs.fxInput, refs.cellInput, refs.fxInput, setContext, "fx"),
35
- formulaHistoryRef = _h.formulaHistoryRef,
36
35
  preTextRef = _h.preTextRef,
37
36
  resetFormulaHistory = _h.resetFormulaHistory,
38
37
  handleFormulaHistoryUndoRedo = _h.handleFormulaHistoryUndoRedo,
39
- capturePreFormulaState = _h.capturePreFormulaState,
40
- appendFormulaHistoryFromPrimaryEditor = _h.appendFormulaHistoryFromPrimaryEditor;
38
+ capturePreEditorHistoryState = _h.capturePreEditorHistoryState,
39
+ appendEditorHistoryFromPrimaryEditor = _h.appendEditorHistoryFromPrimaryEditor;
41
40
  var inputContainerRef = useRef(null);
42
41
  var _j = useState(false),
43
42
  isHidenRC = _j[0],
@@ -279,7 +278,20 @@ var FxEditor = function FxEditor() {
279
278
  var currentCommaCount = countCommasBeforeCursor((_a = refs.fxInput) === null || _a === void 0 ? void 0 : _a.current);
280
279
  setCommaCount(currentCommaCount);
281
280
  lastKeyDownEventRef.current = new KeyboardEvent(e.type, e.nativeEvent);
282
- capturePreFormulaState();
281
+ if (!isEditorUndoRedoKeyEvent(e.nativeEvent)) {
282
+ capturePreEditorHistoryState();
283
+ }
284
+ var isPotentialContentKey = !e.metaKey && !e.ctrlKey && !e.altKey && (e.key.length === 1 || e.key === "Backspace" || e.key === "Delete" || e.key === "Enter" || e.key === "Tab");
285
+ if (isPotentialContentKey) {
286
+ requestAnimationFrame(function () {
287
+ requestAnimationFrame(function () {
288
+ if (getFormulaEditorOwner(context) !== "fx") return;
289
+ appendEditorHistoryFromPrimaryEditor(function () {
290
+ return getCursorPosition(refs.fxInput.current);
291
+ });
292
+ });
293
+ });
294
+ }
283
295
  recentText.current = refs.fxInput.current.innerText;
284
296
  var key = e.key;
285
297
  var currentInputText = ((_c = (_b = refs.fxInput.current) === null || _b === void 0 ? void 0 : _b.innerText) === null || _c === void 0 ? void 0 : _c.trim()) || "";
@@ -331,14 +343,20 @@ var FxEditor = function FxEditor() {
331
343
  }
332
344
  if ((e.metaKey || e.ctrlKey) && context.luckysheetCellUpdate.length > 0) {
333
345
  if (e.code === "KeyZ" || e.code === "KeyY") {
334
- var shouldUseFormulaHistory = currentInputText.startsWith("=") || formulaHistoryRef.current.active;
335
- if (shouldUseFormulaHistory) {
336
- var handledByFormulaHistory = handleFormulaHistoryUndoRedo(e.code === "KeyY" || e.code === "KeyZ" && e.shiftKey);
337
- if (handledByFormulaHistory) {
338
- e.preventDefault();
339
- }
340
- }
346
+ var isRedo_1 = e.code === "KeyY" || e.code === "KeyZ" && e.shiftKey;
347
+ e.preventDefault();
341
348
  e.stopPropagation();
349
+ var _attempt_ = function attempt_1(triesLeft) {
350
+ var handled = handleFormulaHistoryUndoRedo(isRedo_1);
351
+ if (handled) return;
352
+ if (triesLeft <= 0) return;
353
+ requestAnimationFrame(function () {
354
+ requestAnimationFrame(function () {
355
+ return _attempt_(triesLeft - 1);
356
+ });
357
+ });
358
+ };
359
+ _attempt_(2);
342
360
  return;
343
361
  }
344
362
  }
@@ -431,7 +449,7 @@ var FxEditor = function FxEditor() {
431
449
  }
432
450
  }
433
451
  });
434
- }, [capturePreFormulaState, context.allowEdit, context.luckysheetCellUpdate, handleFormulaHistoryUndoRedo, refs.cellInput, refs.fxInput, setContext]);
452
+ }, [capturePreEditorHistoryState, context.allowEdit, context.luckysheetCellUpdate, handleFormulaHistoryUndoRedo, refs.cellInput, refs.fxInput, setContext]);
435
453
  var handleHideShowHint = function handleHideShowHint() {
436
454
  var _a, _b, _c, _d;
437
455
  var el = (_a = document.getElementsByClassName("cell-hint")) === null || _a === void 0 ? void 0 : _a[0];
@@ -488,17 +506,23 @@ var FxEditor = function FxEditor() {
488
506
  }
489
507
  return;
490
508
  }
509
+ if (isEditorUndoRedoKeyEvent(e)) {
510
+ return;
511
+ }
491
512
  var kcode = e.keyCode;
492
513
  if (!kcode) return;
493
- appendFormulaHistoryFromPrimaryEditor(function () {
494
- return getCursorPosition(refs.fxInput.current);
495
- });
496
514
  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 || e.ctrlKey && kcode === 86) {
497
515
  setContext(function (draftCtx) {
498
516
  handleFormulaInput(draftCtx, refs.cellInput.current, refs.fxInput.current, kcode, recentText.current);
499
517
  });
500
518
  }
501
- }, [appendFormulaHistoryFromPrimaryEditor, context.isFlvReadOnly, context.luckysheetCellUpdate, refs.cellInput, refs.fxInput, setContext]);
519
+ requestAnimationFrame(function () {
520
+ if (getFormulaEditorOwner(context) !== "fx") return;
521
+ appendEditorHistoryFromPrimaryEditor(function () {
522
+ return getCursorPosition(refs.fxInput.current);
523
+ });
524
+ });
525
+ }, [appendEditorHistoryFromPrimaryEditor, context.isFlvReadOnly, context.luckysheetCellUpdate, refs.cellInput, refs.fxInput, setContext]);
502
526
  useRerenderOnFormulaCaret(refs.fxInput, context.luckysheetCellUpdate.length > 0);
503
527
  var getFunctionNameFromInput = useCallback(function () {
504
528
  var _a, _b, _c, _d;
@@ -41,10 +41,11 @@ var ContentEditable = function ContentEditable(_a) {
41
41
  if (root.current != null) {
42
42
  html = root.current.innerHTML;
43
43
  }
44
- if (onChange && html !== lastHtml.current) {
45
- onChange(html || "", isBlur);
44
+ var htmlStr = html || "";
45
+ if (onChange && htmlStr !== lastHtml.current) {
46
+ onChange(htmlStr, isBlur);
46
47
  }
47
- lastHtml.current = html || "";
48
+ lastHtml.current = htmlStr;
48
49
  }, [root, onChange]);
49
50
  var innerRef = props.innerRef,
50
51
  _onBlur = props.onBlur;
@@ -19,7 +19,7 @@ import FormulaHint from "./FormulaHint";
19
19
  import usePrevious from "../../hooks/usePrevious";
20
20
  import { useFormulaEditorHistory } from "../../hooks/useFormulaEditorHistory";
21
21
  import { useRerenderOnFormulaCaret } from "../../hooks/useRerenderOnFormulaCaret";
22
- import { moveCursorToEnd, getCursorPosition, setCursorPosition, buildFormulaSuggestionText, getFunctionNameFromFormulaCaretSpans, isLetterNumberPattern, countCommasBeforeCursor, shouldShowFormulaFunctionList } from "./helper";
22
+ import { moveCursorToEnd, getCursorPosition, setCursorPosition, buildFormulaSuggestionText, getFunctionNameFromFormulaCaretSpans, isLetterNumberPattern, countCommasBeforeCursor, shouldShowFormulaFunctionList, isEditorUndoRedoKeyEvent } from "./helper";
23
23
  import { isFormulaSegmentBoundaryKey } from "./formula-segment-boundary";
24
24
  import { LucideIcon } from "./LucideIcon";
25
25
  var InputBox = function InputBox() {
@@ -78,12 +78,11 @@ var InputBox = function InputBox() {
78
78
  var skipNextAnchorSelectionSyncRef = useRef(false);
79
79
  var lastHandledMouseDragSignatureRef = useRef("");
80
80
  var _2 = useFormulaEditorHistory(inputRef, refs.cellInput, refs.fxInput, setContext, "cell"),
81
- formulaHistoryRef = _2.formulaHistoryRef,
82
81
  preTextRef = _2.preTextRef,
83
82
  resetFormulaHistory = _2.resetFormulaHistory,
84
83
  handleFormulaHistoryUndoRedo = _2.handleFormulaHistoryUndoRedo,
85
- capturePreFormulaState = _2.capturePreFormulaState,
86
- appendFormulaHistoryFromPrimaryEditor = _2.appendFormulaHistoryFromPrimaryEditor;
84
+ capturePreEditorHistoryState = _2.capturePreEditorHistoryState,
85
+ appendEditorHistoryFromPrimaryEditor = _2.appendEditorHistoryFromPrimaryEditor;
87
86
  var ZWSP = "\u200B";
88
87
  var inputBoxInnerRef = useRef(null);
89
88
  var _3 = useState([]),
@@ -405,12 +404,25 @@ var InputBox = function InputBox() {
405
404
  });
406
405
  }, [formulaMouseDragSignature, context.formulaCache.func_selectedrange, refs.cellInput, setContext]);
407
406
  var onKeyDown = useCallback(function (e) {
408
- var _a, _b, _c;
407
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
409
408
  setContext(function (draftCtx) {
410
409
  setFormulaEditorOwner(draftCtx, "cell");
411
410
  });
412
411
  lastKeyDownEventRef.current = new KeyboardEvent(e.type, e.nativeEvent);
413
- capturePreFormulaState();
412
+ if (!isEditorUndoRedoKeyEvent(e.nativeEvent)) {
413
+ capturePreEditorHistoryState();
414
+ }
415
+ var isPotentialContentKey = !e.metaKey && !e.ctrlKey && !e.altKey && (e.key.length === 1 || e.key === "Backspace" || e.key === "Delete" || e.key === "Enter" || e.key === "Tab");
416
+ if (isPotentialContentKey) {
417
+ requestAnimationFrame(function () {
418
+ requestAnimationFrame(function () {
419
+ if (getFormulaEditorOwner(context) !== "cell") return;
420
+ appendEditorHistoryFromPrimaryEditor(function () {
421
+ return getCursorPosition(inputRef.current);
422
+ });
423
+ });
424
+ });
425
+ }
414
426
  var currentInputText = ((_b = (_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.innerText) === null || _b === void 0 ? void 0 : _b.trim()) || "";
415
427
  if ((e.key === "=" || currentInputText.startsWith("=")) && context.luckysheetCellUpdate.length === 2 && formulaAnchorCellRef.current == null) {
416
428
  setContext(function (draftCtx) {
@@ -427,9 +439,9 @@ var InputBox = function InputBox() {
427
439
  setContext(function (draftCtx) {
428
440
  draftCtx.formulaCache.rangeSelectionActive = null;
429
441
  });
430
- var _d = formulaAnchorCellRef.current,
431
- anchorRow_1 = _d[0],
432
- anchorCol_1 = _d[1];
442
+ var _m = formulaAnchorCellRef.current,
443
+ anchorRow_1 = _m[0],
444
+ anchorCol_1 = _m[1];
433
445
  skipNextAnchorSelectionSyncRef.current = true;
434
446
  setTimeout(function () {
435
447
  setContext(function (draftCtx) {
@@ -455,39 +467,93 @@ var InputBox = function InputBox() {
455
467
  }
456
468
  if ((e.metaKey || e.ctrlKey) && context.luckysheetCellUpdate.length > 0) {
457
469
  if (e.code === "KeyZ" || e.code === "KeyY") {
458
- var shouldUseFormulaHistory = currentInputText.startsWith("=") || formulaHistoryRef.current.active;
459
- if (shouldUseFormulaHistory) {
460
- var handledByFormulaHistory = handleFormulaHistoryUndoRedo(e.code === "KeyY" || e.code === "KeyZ" && e.shiftKey);
461
- if (handledByFormulaHistory) {
462
- e.preventDefault();
463
- }
464
- }
470
+ var isRedo_1 = e.code === "KeyY" || e.code === "KeyZ" && e.shiftKey;
471
+ e.preventDefault();
465
472
  e.stopPropagation();
473
+ var _attempt_ = function attempt_1(triesLeft) {
474
+ var handled = handleFormulaHistoryUndoRedo(isRedo_1);
475
+ if (handled) return;
476
+ if (triesLeft <= 0) return;
477
+ requestAnimationFrame(function () {
478
+ requestAnimationFrame(function () {
479
+ return _attempt_(triesLeft - 1);
480
+ });
481
+ });
482
+ };
483
+ _attempt_(2);
466
484
  return;
467
485
  }
468
486
  if (e.code === "KeyB") {
487
+ var beforeHtml_1 = (_d = (_c = inputRef.current) === null || _c === void 0 ? void 0 : _c.innerHTML) !== null && _d !== void 0 ? _d : "";
469
488
  handleBold(context, inputRef.current);
470
- appendFormulaHistoryFromPrimaryEditor(function () {
471
- return getCursorPosition(inputRef.current);
472
- });
489
+ var _pushFormattingSnapshot_ = function pushFormattingSnapshot_1(triesLeft) {
490
+ requestAnimationFrame(function () {
491
+ var _a, _b;
492
+ var afterHtml = (_b = (_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.innerHTML) !== null && _b !== void 0 ? _b : "";
493
+ if (afterHtml !== beforeHtml_1 || triesLeft <= 0) {
494
+ appendEditorHistoryFromPrimaryEditor(function () {
495
+ return getCursorPosition(inputRef.current);
496
+ });
497
+ return;
498
+ }
499
+ _pushFormattingSnapshot_(triesLeft - 1);
500
+ });
501
+ };
502
+ _pushFormattingSnapshot_(2);
473
503
  stopPropagation(e);
474
504
  } else if (e.code === "KeyI") {
505
+ var beforeHtml_2 = (_f = (_e = inputRef.current) === null || _e === void 0 ? void 0 : _e.innerHTML) !== null && _f !== void 0 ? _f : "";
475
506
  handleItalic(context, inputRef.current);
476
- appendFormulaHistoryFromPrimaryEditor(function () {
477
- return getCursorPosition(inputRef.current);
478
- });
507
+ var _pushFormattingSnapshot_2 = function pushFormattingSnapshot_2(triesLeft) {
508
+ requestAnimationFrame(function () {
509
+ var _a, _b;
510
+ var afterHtml = (_b = (_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.innerHTML) !== null && _b !== void 0 ? _b : "";
511
+ if (afterHtml !== beforeHtml_2 || triesLeft <= 0) {
512
+ appendEditorHistoryFromPrimaryEditor(function () {
513
+ return getCursorPosition(inputRef.current);
514
+ });
515
+ return;
516
+ }
517
+ _pushFormattingSnapshot_2(triesLeft - 1);
518
+ });
519
+ };
520
+ _pushFormattingSnapshot_2(2);
479
521
  stopPropagation(e);
480
522
  } else if (e.code === "KeyU") {
523
+ var beforeHtml_3 = (_h = (_g = inputRef.current) === null || _g === void 0 ? void 0 : _g.innerHTML) !== null && _h !== void 0 ? _h : "";
481
524
  handleUnderline(context, inputRef.current);
482
- appendFormulaHistoryFromPrimaryEditor(function () {
483
- return getCursorPosition(inputRef.current);
484
- });
525
+ var _pushFormattingSnapshot_3 = function pushFormattingSnapshot_3(triesLeft) {
526
+ requestAnimationFrame(function () {
527
+ var _a, _b;
528
+ var afterHtml = (_b = (_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.innerHTML) !== null && _b !== void 0 ? _b : "";
529
+ if (afterHtml !== beforeHtml_3 || triesLeft <= 0) {
530
+ appendEditorHistoryFromPrimaryEditor(function () {
531
+ return getCursorPosition(inputRef.current);
532
+ });
533
+ return;
534
+ }
535
+ _pushFormattingSnapshot_3(triesLeft - 1);
536
+ });
537
+ };
538
+ _pushFormattingSnapshot_3(2);
485
539
  stopPropagation(e);
486
540
  } else if (e.code === "KeyS") {
541
+ var beforeHtml_4 = (_k = (_j = inputRef.current) === null || _j === void 0 ? void 0 : _j.innerHTML) !== null && _k !== void 0 ? _k : "";
487
542
  handleStrikeThrough(context, inputRef.current);
488
- appendFormulaHistoryFromPrimaryEditor(function () {
489
- return getCursorPosition(inputRef.current);
490
- });
543
+ var _pushFormattingSnapshot_4 = function pushFormattingSnapshot_4(triesLeft) {
544
+ requestAnimationFrame(function () {
545
+ var _a, _b;
546
+ var afterHtml = (_b = (_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.innerHTML) !== null && _b !== void 0 ? _b : "";
547
+ if (afterHtml !== beforeHtml_4 || triesLeft <= 0) {
548
+ appendEditorHistoryFromPrimaryEditor(function () {
549
+ return getCursorPosition(inputRef.current);
550
+ });
551
+ return;
552
+ }
553
+ _pushFormattingSnapshot_4(triesLeft - 1);
554
+ });
555
+ };
556
+ _pushFormattingSnapshot_4(2);
491
557
  stopPropagation(e);
492
558
  }
493
559
  }
@@ -497,7 +563,7 @@ var InputBox = function InputBox() {
497
563
  }
498
564
  var allowListNavigation = true;
499
565
  var isArrowKey = e.key === "ArrowUp" || e.key === "ArrowDown" || e.key === "ArrowLeft" || e.key === "ArrowRight";
500
- var isInPlaceEditMode = ((_c = refs.globalCache) === null || _c === void 0 ? void 0 : _c.enteredEditByTyping) !== true;
566
+ var isInPlaceEditMode = ((_l = refs.globalCache) === null || _l === void 0 ? void 0 : _l.enteredEditByTyping) !== true;
501
567
  if (e.key === "Delete" || e.key === "Backspace") {
502
568
  var anchor = formulaAnchorCellRef.current;
503
569
  if (anchor != null) {
@@ -586,7 +652,7 @@ var InputBox = function InputBox() {
586
652
  }
587
653
  e.preventDefault();
588
654
  }
589
- }, [capturePreFormulaState, clearSearchItemActiveClass, context.luckysheetCellUpdate.length, handleFormulaHistoryUndoRedo, selectActiveFormula, setContext, firstSelection, refs.cellInput]);
655
+ }, [capturePreEditorHistoryState, clearSearchItemActiveClass, context.luckysheetCellUpdate.length, handleFormulaHistoryUndoRedo, selectActiveFormula, setContext, firstSelection, refs.cellInput]);
590
656
  var handleHideShowHint = function handleHideShowHint() {
591
657
  var _a, _b, _c, _d;
592
658
  var searchElFx = (_a = document.getElementsByClassName("fx-search")) === null || _a === void 0 ? void 0 : _a[0];
@@ -628,11 +694,11 @@ var InputBox = function InputBox() {
628
694
  }
629
695
  return;
630
696
  }
697
+ if (isEditorUndoRedoKeyEvent(e)) {
698
+ return;
699
+ }
631
700
  var kcode = e.keyCode;
632
701
  if (!kcode) return;
633
- appendFormulaHistoryFromPrimaryEditor(function () {
634
- return getCursorPosition(inputRef.current);
635
- });
636
702
  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 || e.ctrlKey && kcode === 86) {
637
703
  setContext(function (draftCtx) {
638
704
  var _a, _b;
@@ -647,7 +713,13 @@ var InputBox = function InputBox() {
647
713
  }
648
714
  });
649
715
  }
650
- }, [refs.cellInput, refs.fxInput, setContext, appendFormulaHistoryFromPrimaryEditor]);
716
+ requestAnimationFrame(function () {
717
+ if (getFormulaEditorOwner(context) !== "cell") return;
718
+ appendEditorHistoryFromPrimaryEditor(function () {
719
+ return getCursorPosition(inputRef.current);
720
+ });
721
+ });
722
+ }, [refs.cellInput, refs.fxInput, setContext, appendEditorHistoryFromPrimaryEditor]);
651
723
  var onPaste = useCallback(function (e) {
652
724
  var plainText = e.clipboardData.getData("text/plain");
653
725
  e.preventDefault();
@@ -1,6 +1,13 @@
1
1
  export declare function moveCursorToEnd(editableDiv: HTMLDivElement): void;
2
2
  export declare function getCursorPosition(editableDiv: HTMLDivElement): number;
3
+ export declare function getSelectionOffsets(editableDiv: HTMLDivElement): {
4
+ start: number;
5
+ end: number;
6
+ };
7
+ export declare function isEditorUndoRedoKeyEvent(e: KeyboardEvent): boolean;
8
+ export declare function shouldUseCustomEditorHistory(editorInnerTextTrimmed: string, historyActive: boolean): boolean;
3
9
  export declare function setCursorPosition(editableDiv: HTMLDivElement, targetOffset: number): void;
10
+ export declare function setSelectionOffsets(editableDiv: HTMLDivElement, startOffset: number, endOffset: number): void;
4
11
  export declare function buildFormulaSuggestionText(editableDiv: HTMLDivElement, formulaName: string): {
5
12
  text: string;
6
13
  caretOffset: number;
@@ -18,6 +18,44 @@ export function getCursorPosition(editableDiv) {
18
18
  preRange.setEnd(range.endContainer, range.endOffset);
19
19
  return preRange.toString().length;
20
20
  }
21
+ export function getSelectionOffsets(editableDiv) {
22
+ var selection = window.getSelection();
23
+ if (!selection || selection.rangeCount === 0) {
24
+ var caret = getCursorPosition(editableDiv);
25
+ return {
26
+ start: caret,
27
+ end: caret
28
+ };
29
+ }
30
+ var range = selection.getRangeAt(0);
31
+ if (!editableDiv.contains(range.startContainer) || !editableDiv.contains(range.endContainer)) {
32
+ var caret = getCursorPosition(editableDiv);
33
+ return {
34
+ start: caret,
35
+ end: caret
36
+ };
37
+ }
38
+ var startRange = range.cloneRange();
39
+ startRange.selectNodeContents(editableDiv);
40
+ startRange.setEnd(range.startContainer, range.startOffset);
41
+ var start = startRange.toString().length;
42
+ var endRange = range.cloneRange();
43
+ endRange.selectNodeContents(editableDiv);
44
+ endRange.setEnd(range.endContainer, range.endOffset);
45
+ var end = endRange.toString().length;
46
+ return {
47
+ start: Math.max(0, Math.min(start, end)),
48
+ end: Math.max(start, end)
49
+ };
50
+ }
51
+ export function isEditorUndoRedoKeyEvent(e) {
52
+ if (!e.metaKey && !e.ctrlKey) return false;
53
+ if (e.code === "KeyZ" || e.code === "KeyY") return true;
54
+ return e.keyCode === 90 || e.keyCode === 89;
55
+ }
56
+ export function shouldUseCustomEditorHistory(editorInnerTextTrimmed, historyActive) {
57
+ return historyActive || editorInnerTextTrimmed.length > 0;
58
+ }
21
59
  export function setCursorPosition(editableDiv, targetOffset) {
22
60
  var _a, _b;
23
61
  editableDiv.focus();
@@ -44,6 +82,43 @@ export function setCursorPosition(editableDiv, targetOffset) {
44
82
  selection.removeAllRanges();
45
83
  selection.addRange(range);
46
84
  }
85
+ export function setSelectionOffsets(editableDiv, startOffset, endOffset) {
86
+ editableDiv.focus();
87
+ var selection = window.getSelection();
88
+ if (!selection) return;
89
+ var startTarget = Math.max(0, Math.min(startOffset, endOffset));
90
+ var endTarget = Math.max(startTarget, endOffset);
91
+ var walker = document.createTreeWalker(editableDiv, NodeFilter.SHOW_TEXT);
92
+ var resolve = function resolve(target) {
93
+ var _a, _b;
94
+ var remaining = target;
95
+ var node = walker.nextNode();
96
+ while (node) {
97
+ var len = (_b = (_a = node.textContent) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0;
98
+ if (remaining <= len) return {
99
+ node: node,
100
+ offset: remaining
101
+ };
102
+ remaining -= len;
103
+ node = walker.nextNode();
104
+ }
105
+ return null;
106
+ };
107
+ walker.currentNode = editableDiv;
108
+ var startPos = resolve(startTarget);
109
+ walker.currentNode = editableDiv;
110
+ var endPos = resolve(endTarget);
111
+ var range = document.createRange();
112
+ if (startPos && endPos) {
113
+ range.setStart(startPos.node, startPos.offset);
114
+ range.setEnd(endPos.node, endPos.offset);
115
+ } else {
116
+ range.selectNodeContents(editableDiv);
117
+ range.collapse(false);
118
+ }
119
+ selection.removeAllRanges();
120
+ selection.addRange(range);
121
+ }
47
122
  export function buildFormulaSuggestionText(editableDiv, formulaName) {
48
123
  var fullText = editableDiv.innerText || "";
49
124
  var selection = window.getSelection();
@@ -12,8 +12,9 @@ var ColorPicker = function ColorPicker(_a) {
12
12
  return /*#__PURE__*/React.createElement("div", {
13
13
  key: c,
14
14
  className: "fortune-toolbar-color-picker-item",
15
- onClick: function onClick() {
16
- return onPick(c);
15
+ onMouseDown: function onMouseDown(e) {
16
+ e.preventDefault();
17
+ onPick(c);
17
18
  },
18
19
  tabIndex: 0,
19
20
  style: {
@@ -6,11 +6,15 @@ export var CustomColor = function CustomColor(_a) {
6
6
  var onCustomPick = _a.onCustomPick,
7
7
  onColorPick = _a.onColorPick;
8
8
  return /*#__PURE__*/React.createElement("div", {
9
- id: "fortune-custom-color"
9
+ id: "fortune-custom-color",
10
+ onMouseDown: function onMouseDown(e) {
11
+ e.preventDefault();
12
+ }
10
13
  }, /*#__PURE__*/React.createElement("div", {
11
14
  className: "color-reset",
12
- onClick: function onClick() {
13
- return onCustomPick(undefined);
15
+ onMouseDown: function onMouseDown(e) {
16
+ e.preventDefault();
17
+ onCustomPick(undefined);
14
18
  },
15
19
  tabIndex: 0
16
20
  }, /*#__PURE__*/React.createElement(SVGIcon, {
@@ -123,7 +123,7 @@ var __spreadArray = this && this.__spreadArray || function (to, from, pack) {
123
123
  return to.concat(ar || Array.prototype.slice.call(from));
124
124
  };
125
125
  import React, { useContext, useCallback, useRef, useEffect, useState } from "react";
126
- import { toolbarItemClickHandler, handleTextBackground, handleTextColor, handleTextSize, normalizedCellAttr, getFlowdata, newComment, editComment, deleteComment, showHideComment, showHideAllComments, autoSelectionFormula, handleSum, locale, handleMerge, handleBorder, toolbarItemSelectedFunc, handleFreeze, insertImage, showImgChooser, updateFormat, handleSort, handleHorizontalAlign, handleVerticalAlign, handleScreenShot, createFilter, clearFilter, applyLocation, insertDuneChart, api, getSheetIndex, is_date } from "@fileverse-dev/fortune-core";
126
+ import { toolbarItemClickHandler, handleTextBackground, handleTextColor, handleTextSize, normalizedCellAttr, getFlowdata, newComment, editComment, deleteComment, showHideComment, showHideAllComments, autoSelectionFormula, handleSum, locale, handleMerge, handleBorder, toolbarItemSelectedFunc, handleFreeze, insertImage, showImgChooser, updateFormat, handleSort, handleHorizontalAlign, handleVerticalAlign, handleScreenShot, createFilter, clearFilter, applyLocation, insertDuneChart, getFormulaEditorOwner, api, getSheetIndex, is_date } from "@fileverse-dev/fortune-core";
127
127
  import _ from "lodash";
128
128
  import { IconButton, LucideIcon, Tooltip, Command, CommandInput, CommandList, CommandEmpty, CommandGroup, CommandItem } from "@fileverse/ui";
129
129
  import DataVerificationPortal from "./dataVerificationPortal";
@@ -463,6 +463,14 @@ var Toolbar = function Toolbar(_a) {
463
463
  settings = _f.settings,
464
464
  handleUndo = _f.handleUndo,
465
465
  handleRedo = _f.handleRedo;
466
+ var restoreEditorFocusAfterToolbarAction = useCallback(function () {
467
+ if (context.luckysheetCellUpdate.length === 0) return;
468
+ requestAnimationFrame(function () {
469
+ var owner = getFormulaEditorOwner(context);
470
+ var target = owner === "fx" ? refs.fxInput.current : refs.cellInput.current;
471
+ target === null || target === void 0 ? void 0 : target.focus();
472
+ });
473
+ }, [context, refs.cellInput, refs.fxInput]);
466
474
  var contextRef = useRef(context);
467
475
  var containerRef = useRef(null);
468
476
  var _g = useState(-1),
@@ -1712,7 +1720,8 @@ var Toolbar = function Toolbar(_a) {
1712
1720
  return /*#__PURE__*/React.createElement("div", {
1713
1721
  ref: containerRef,
1714
1722
  className: "fortune-toolbar",
1715
- "aria-label": toolbar.toolbar
1723
+ "aria-label": toolbar.toolbar,
1724
+ onMouseUpCapture: restoreEditorFocusAfterToolbarAction
1716
1725
  }, /*#__PURE__*/React.createElement(DataVerificationPortal, {
1717
1726
  visible: showDataValidation
1718
1727
  }), /*#__PURE__*/React.createElement(ConditionalFormatPortal, {
@@ -2,22 +2,22 @@ import { type RefObject } from "react";
2
2
  import { type Context } from "@fileverse-dev/fortune-core";
3
3
  import type { SetContextOptions } from "../context";
4
4
  export type FormulaHistoryEntry = {
5
- text: string;
5
+ html: string;
6
6
  caret: number;
7
- spanValues: string[];
8
7
  };
9
8
  export type FormulaEditorHistoryPrimary = "cell" | "fx";
10
9
  type SetContext = (recipe: (ctx: Context) => void, options?: SetContextOptions) => void;
11
- export declare function useFormulaEditorHistory(primaryRef: RefObject<HTMLDivElement | null>, cellInputRef: RefObject<HTMLDivElement | null>, fxInputRef: RefObject<HTMLDivElement | null>, setContext: SetContext, primary: FormulaEditorHistoryPrimary): {
10
+ export declare function useFormulaEditorHistory(primaryRef: RefObject<HTMLDivElement | null>, cellInputRef: RefObject<HTMLDivElement | null>, fxInputRef: RefObject<HTMLDivElement | null>, _setContext: SetContext, primary: FormulaEditorHistoryPrimary): {
12
11
  formulaHistoryRef: RefObject<{
13
12
  active: boolean;
14
13
  entries: FormulaHistoryEntry[];
15
14
  index: number;
16
15
  }>;
17
16
  preTextRef: RefObject<string>;
18
- preFormulaSpanValuesRef: RefObject<string[] | null>;
19
17
  resetFormulaHistory: () => void;
20
18
  handleFormulaHistoryUndoRedo: (isRedo: boolean) => boolean;
19
+ capturePreEditorHistoryState: () => void;
20
+ appendEditorHistoryFromPrimaryEditor: (getCaret: () => number) => void;
21
21
  capturePreFormulaState: () => void;
22
22
  appendFormulaHistoryFromPrimaryEditor: (getCaret: () => number) => void;
23
23
  };