@fileverse-dev/fortune-react 1.3.10 → 1.3.11-input-ref-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.
Files changed (29) hide show
  1. package/es/components/ContextMenu/index.js +1 -1
  2. package/es/components/FxEditor/index.js +200 -52
  3. package/es/components/SheetOverlay/InputBox.js +318 -150
  4. package/es/components/SheetOverlay/drag_and_drop/column-helpers.js +25 -1
  5. package/es/components/SheetOverlay/drag_and_drop/row-helpers.js +26 -2
  6. package/es/components/SheetOverlay/helper.d.ts +7 -0
  7. package/es/components/SheetOverlay/helper.js +95 -0
  8. package/es/components/SheetOverlay/index.css +1 -1
  9. package/es/components/Workbook/index.js +141 -11
  10. package/es/hooks/useFormulaEditorHistory.d.ts +24 -0
  11. package/es/hooks/useFormulaEditorHistory.js +119 -0
  12. package/es/hooks/useRerenderOnFormulaCaret.d.ts +2 -0
  13. package/es/hooks/useRerenderOnFormulaCaret.js +26 -0
  14. package/es/utils/convertCellsToCrypto.js +17 -0
  15. package/lib/components/ContextMenu/index.js +1 -1
  16. package/lib/components/FxEditor/index.js +198 -50
  17. package/lib/components/SheetOverlay/InputBox.js +316 -148
  18. package/lib/components/SheetOverlay/drag_and_drop/column-helpers.js +25 -1
  19. package/lib/components/SheetOverlay/drag_and_drop/row-helpers.js +26 -2
  20. package/lib/components/SheetOverlay/helper.d.ts +7 -0
  21. package/lib/components/SheetOverlay/helper.js +99 -0
  22. package/lib/components/SheetOverlay/index.css +1 -1
  23. package/lib/components/Workbook/index.js +142 -12
  24. package/lib/hooks/useFormulaEditorHistory.d.ts +24 -0
  25. package/lib/hooks/useFormulaEditorHistory.js +126 -0
  26. package/lib/hooks/useRerenderOnFormulaCaret.d.ts +2 -0
  27. package/lib/hooks/useRerenderOnFormulaCaret.js +32 -0
  28. package/lib/utils/convertCellsToCrypto.js +17 -0
  29. package/package.json +2 -2
@@ -219,7 +219,7 @@ var useColumnDragAndDrop = exports.useColumnDragAndDrop = function useColumnDrag
219
219
  var sheetIdx_1 = (0, _fortuneCore.getSheetIndex)(context, context.currentSheetId);
220
220
  if (sheetIdx_1 != null && sourceIndex_1 >= 0 && Number.isFinite(finalInsertionIndex_1) && finalInsertionIndex_1 >= 0) {
221
221
  setContext(function (draft) {
222
- var _a, _b, _c, _d, _e, _f, _g;
222
+ var _a, _b, _c, _d, _e, _f, _g, _h;
223
223
  var _sheet = draft.luckysheetfile[sheetIdx_1];
224
224
  if (!(_sheet === null || _sheet === void 0 ? void 0 : _sheet.data)) return;
225
225
  var rows = _sheet.data;
@@ -382,6 +382,30 @@ var useColumnDragAndDrop = exports.useColumnDragAndDrop = function useColumnDrag
382
382
  }
383
383
  });
384
384
  (_g = window === null || window === void 0 ? void 0 : window.updateDataBlockCalcFunctionAfterRowDrag) === null || _g === void 0 ? void 0 : _g.call(window, selectedSourceCol, selectedTargetCol, "column", context.currentSheetId, sourceIndex_1, targetIndex);
385
+ var cellChanges = [];
386
+ var affectedColStart = Math.min(sourceIndex_1, targetIndex);
387
+ var affectedColEnd = Math.max(sourceIndex_1, targetIndex) + selectedSourceCol.length - 1;
388
+ var numRows = rows.length;
389
+ for (var r = 0; r < numRows; r += 1) {
390
+ var row = rows[r];
391
+ for (var c = affectedColStart; c <= affectedColEnd; c += 1) {
392
+ var cell = row === null || row === void 0 ? void 0 : row[c];
393
+ cellChanges.push({
394
+ sheetId: draft.currentSheetId,
395
+ path: ["celldata"],
396
+ value: {
397
+ r: r,
398
+ c: c,
399
+ v: cell !== null && cell !== void 0 ? cell : null
400
+ },
401
+ key: "".concat(r, "_").concat(c),
402
+ type: "update"
403
+ });
404
+ }
405
+ }
406
+ if (cellChanges.length > 0 && ((_h = draft.hooks) === null || _h === void 0 ? void 0 : _h.updateCellYdoc)) {
407
+ draft.hooks.updateCellYdoc(cellChanges);
408
+ }
385
409
  var rowLen = (d === null || d === void 0 ? void 0 : d.length) || 0;
386
410
  _fortuneCore.api.setSelection(draft, [{
387
411
  row: [0, rowLen],
@@ -214,7 +214,7 @@ var useRowDragAndDrop = exports.useRowDragAndDrop = function useRowDragAndDrop(c
214
214
  var sheetIdx_1 = (0, _fortuneCore.getSheetIndex)(context, context.currentSheetId);
215
215
  if (sheetIdx_1 != null && sourceIndex_1 >= 0 && Number.isFinite(finalInsertionIndex_1) && finalInsertionIndex_1 >= 0) {
216
216
  setContext(function (draft) {
217
- var _a, _b, _c, _d, _e, _f;
217
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j;
218
218
  var _sheet = draft.luckysheetfile[sheetIdx_1];
219
219
  if (!(_sheet === null || _sheet === void 0 ? void 0 : _sheet.data)) return;
220
220
  var rows = _sheet.data;
@@ -371,7 +371,31 @@ var useRowDragAndDrop = exports.useRowDragAndDrop = function useRowDragAndDrop(c
371
371
  }
372
372
  });
373
373
  (_e = window === null || window === void 0 ? void 0 : window.updateDataBlockCalcFunctionAfterRowDrag) === null || _e === void 0 ? void 0 : _e.call(window, sourceIndex_1, targetIndex, "row", context.currentSheetId);
374
- var colLen = ((_f = d === null || d === void 0 ? void 0 : d[0]) === null || _f === void 0 ? void 0 : _f.length) || 0;
374
+ var cellChanges = [];
375
+ var affectedRowStart = Math.min(sourceIndex_1, targetIndex);
376
+ var affectedRowEnd = Math.max(sourceIndex_1, targetIndex) + selectedSourceRow.length - 1;
377
+ var numCols = (_g = (_f = d === null || d === void 0 ? void 0 : d[0]) === null || _f === void 0 ? void 0 : _f.length) !== null && _g !== void 0 ? _g : 0;
378
+ for (var r = affectedRowStart; r <= affectedRowEnd; r += 1) {
379
+ var row = rows[r];
380
+ for (var c = 0; c < numCols; c += 1) {
381
+ var cell = row === null || row === void 0 ? void 0 : row[c];
382
+ cellChanges.push({
383
+ sheetId: draft.currentSheetId,
384
+ path: ["celldata"],
385
+ value: {
386
+ r: r,
387
+ c: c,
388
+ v: cell !== null && cell !== void 0 ? cell : null
389
+ },
390
+ key: "".concat(r, "_").concat(c),
391
+ type: "update"
392
+ });
393
+ }
394
+ }
395
+ if (cellChanges.length > 0 && ((_h = draft.hooks) === null || _h === void 0 ? void 0 : _h.updateCellYdoc)) {
396
+ draft.hooks.updateCellYdoc(cellChanges);
397
+ }
398
+ var colLen = ((_j = d === null || d === void 0 ? void 0 : d[0]) === null || _j === void 0 ? void 0 : _j.length) || 0;
375
399
  _fortuneCore.api.setSelection(draft, [{
376
400
  row: [selectedTargetRow[0], selectedTargetRow[selectedTargetRow.length - 1]],
377
401
  column: [0, colLen]
@@ -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 setCursorPosition(editableDiv: HTMLDivElement, targetOffset: number): void;
4
+ export declare function buildFormulaSuggestionText(editableDiv: HTMLDivElement, formulaName: string): {
5
+ text: string;
6
+ caretOffset: number;
7
+ };
3
8
  export declare function isLetterNumberPattern(str: string): boolean;
9
+ export declare function shouldShowFormulaFunctionList(editor: HTMLDivElement | null): boolean;
10
+ export declare function getFunctionNameFromFormulaCaretSpans(editor: HTMLDivElement | null): string | null;
4
11
  export declare function removeLastSpan(htmlString: string): string;
5
12
  export declare function numberToColumn(colNumber: number): string;
6
13
  export declare function incrementColumn(cell: string): string;
@@ -3,16 +3,20 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
+ exports.buildFormulaSuggestionText = buildFormulaSuggestionText;
6
7
  exports.countCommasBeforeCursor = countCommasBeforeCursor;
7
8
  exports.decrementColumn = decrementColumn;
8
9
  exports.decrementRow = decrementRow;
9
10
  exports.getCursorPosition = getCursorPosition;
11
+ exports.getFunctionNameFromFormulaCaretSpans = getFunctionNameFromFormulaCaretSpans;
10
12
  exports.incrementColumn = incrementColumn;
11
13
  exports.incrementRow = incrementRow;
12
14
  exports.isLetterNumberPattern = isLetterNumberPattern;
13
15
  exports.moveCursorToEnd = moveCursorToEnd;
14
16
  exports.numberToColumn = numberToColumn;
15
17
  exports.removeLastSpan = removeLastSpan;
18
+ exports.setCursorPosition = setCursorPosition;
19
+ exports.shouldShowFormulaFunctionList = shouldShowFormulaFunctionList;
16
20
  function moveCursorToEnd(editableDiv) {
17
21
  editableDiv.focus();
18
22
  var range = document.createRange();
@@ -33,10 +37,105 @@ function getCursorPosition(editableDiv) {
33
37
  preRange.setEnd(range.endContainer, range.endOffset);
34
38
  return preRange.toString().length;
35
39
  }
40
+ function setCursorPosition(editableDiv, targetOffset) {
41
+ var _a, _b;
42
+ editableDiv.focus();
43
+ var selection = window.getSelection();
44
+ if (!selection) return;
45
+ var range = document.createRange();
46
+ var walker = document.createTreeWalker(editableDiv, NodeFilter.SHOW_TEXT);
47
+ var remaining = Math.max(0, targetOffset);
48
+ var node = walker.nextNode();
49
+ while (node) {
50
+ var textLength = (_b = (_a = node.textContent) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0;
51
+ if (remaining <= textLength) {
52
+ range.setStart(node, remaining);
53
+ range.collapse(true);
54
+ selection.removeAllRanges();
55
+ selection.addRange(range);
56
+ return;
57
+ }
58
+ remaining -= textLength;
59
+ node = walker.nextNode();
60
+ }
61
+ range.selectNodeContents(editableDiv);
62
+ range.collapse(false);
63
+ selection.removeAllRanges();
64
+ selection.addRange(range);
65
+ }
66
+ function buildFormulaSuggestionText(editableDiv, formulaName) {
67
+ var fullText = editableDiv.innerText || "";
68
+ var selection = window.getSelection();
69
+ var selectionInEditor = !!(selection === null || selection === void 0 ? void 0 : selection.rangeCount) && editableDiv.contains(selection.getRangeAt(0).startContainer);
70
+ var caretOffset = selectionInEditor ? getCursorPosition(editableDiv) : fullText.length;
71
+ var safeCaretOffset = Math.max(0, Math.min(caretOffset, fullText.length));
72
+ var beforeCaret = fullText.slice(0, safeCaretOffset);
73
+ var afterCaret = fullText.slice(safeCaretOffset);
74
+ var replaceStart = safeCaretOffset;
75
+ var tokenMatch = beforeCaret.match(/[A-Za-z_][A-Za-z0-9_]*$/);
76
+ if (tokenMatch) {
77
+ var token = tokenMatch[0];
78
+ var tokenStart = safeCaretOffset - token.length;
79
+ var charBeforeToken = tokenStart > 0 ? beforeCaret[tokenStart - 1] : "";
80
+ if (tokenStart === 0 || /[\s=(,+\-*/&^<>]$/.test(charBeforeToken)) {
81
+ replaceStart = tokenStart;
82
+ }
83
+ }
84
+ var shouldAddOpeningParen = !afterCaret.startsWith("(");
85
+ var insertedText = "".concat(formulaName).concat(shouldAddOpeningParen ? "(" : "");
86
+ var nextText = fullText.slice(0, replaceStart) + insertedText + afterCaret;
87
+ return {
88
+ text: nextText,
89
+ caretOffset: replaceStart + insertedText.length
90
+ };
91
+ }
36
92
  function isLetterNumberPattern(str) {
37
93
  var regex = /^[a-zA-Z]+\d+$/;
38
94
  return regex.test(str);
39
95
  }
96
+ function shouldShowFormulaFunctionList(editor) {
97
+ var _a, _b;
98
+ if (!editor) return false;
99
+ if (!((_a = editor.innerText) === null || _a === void 0 ? void 0 : _a.includes("="))) return false;
100
+ var parser = new DOMParser();
101
+ var doc = parser.parseFromString("<div>".concat(editor.innerHTML, "</div>"), "text/html");
102
+ var spans = doc.querySelectorAll("span");
103
+ var lastSpan = spans[spans.length - 1];
104
+ var lastText = (_b = lastSpan === null || lastSpan === void 0 ? void 0 : lastSpan.innerText) !== null && _b !== void 0 ? _b : "";
105
+ return /^=?[A-Za-z]*$/.test(lastText);
106
+ }
107
+ var FORMULA_FUNC_CLASS = "luckysheet-formula-text-func";
108
+ var FORMULA_LPAR_CLASS = "luckysheet-formula-text-lpar";
109
+ function getFunctionNameFromFormulaCaretSpans(editor) {
110
+ var _a, _b;
111
+ if (!editor) return null;
112
+ var sel = window.getSelection();
113
+ if (!(sel === null || sel === void 0 ? void 0 : sel.rangeCount)) return null;
114
+ var range = sel.getRangeAt(0);
115
+ if (!editor.contains(range.startContainer)) return null;
116
+ var n = range.startContainer;
117
+ while (n && n !== editor) {
118
+ if (n.nodeType === Node.ELEMENT_NODE) {
119
+ var elem = n;
120
+ if (elem.classList.contains(FORMULA_FUNC_CLASS)) {
121
+ var next = elem.nextElementSibling;
122
+ if (next === null || next === void 0 ? void 0 : next.classList.contains(FORMULA_LPAR_CLASS)) {
123
+ var name_1 = (_a = elem.textContent) === null || _a === void 0 ? void 0 : _a.trim();
124
+ return name_1 ? name_1.toUpperCase() : null;
125
+ }
126
+ }
127
+ if (elem.classList.contains(FORMULA_LPAR_CLASS)) {
128
+ var prev = elem.previousElementSibling;
129
+ if (prev === null || prev === void 0 ? void 0 : prev.classList.contains(FORMULA_FUNC_CLASS)) {
130
+ var name_2 = (_b = prev.textContent) === null || _b === void 0 ? void 0 : _b.trim();
131
+ return name_2 ? name_2.toUpperCase() : null;
132
+ }
133
+ }
134
+ }
135
+ n = n.parentNode;
136
+ }
137
+ return null;
138
+ }
40
139
  function removeLastSpan(htmlString) {
41
140
  var container = document.createElement("div");
42
141
  container.innerHTML = htmlString;
@@ -356,7 +356,7 @@
356
356
  }
357
357
 
358
358
  .luckysheet-formula-text-color {
359
- color: black;
359
+ color: darkred;
360
360
  }
361
361
 
362
362
  .luckysheet-formula-text-string {
@@ -25,7 +25,7 @@ var _FilterMenu = _interopRequireDefault(require("../ContextMenu/FilterMenu"));
25
25
  var _SheetList = _interopRequireDefault(require("../SheetList"));
26
26
  var _DunePreview = _interopRequireDefault(require("../DunePreview/DunePreview"));
27
27
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
28
- function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
28
+ function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t2 in e) "default" !== _t2 && {}.hasOwnProperty.call(e, _t2) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t2)) && (i.get || i.set) ? o(f, _t2, i) : f[_t2] = e[_t2]); return f; })(e, t); }
29
29
  var __assign = void 0 && (void 0).__assign || function () {
30
30
  __assign = Object.assign || function (t) {
31
31
  for (var s, i = 1, n = arguments.length; i < n; i++) {
@@ -151,6 +151,101 @@ var Workbook = /*#__PURE__*/_react.default.forwardRef(function (_a, ref) {
151
151
  onOp((0, _fortuneCore.patchToOp)(ctx, patches, options, undo));
152
152
  }
153
153
  }, [onOp]);
154
+ var emitYjsFromPatches = (0, _react.useCallback)(function (ctxBefore, ctxAfter, patches) {
155
+ var _a;
156
+ var _b = (_a = ctxBefore.hooks) !== null && _a !== void 0 ? _a : {},
157
+ updateCellYdoc = _b.updateCellYdoc,
158
+ updateAllCell = _b.updateAllCell;
159
+ if (!updateCellYdoc) return;
160
+ var mapFields = new Set(["celldata", "calcChain", "dataBlockCalcFunction", "liveQueryList", "dataVerification", "hyperlink", "conditionRules"]);
161
+ var changeMap = new Map();
162
+ var upsert = function upsert(change) {
163
+ var _a, _b, _c;
164
+ var k = "".concat(change.sheetId, ":").concat((_b = (_a = change.path) === null || _a === void 0 ? void 0 : _a[0]) !== null && _b !== void 0 ? _b : "", ":").concat((_c = change.key) !== null && _c !== void 0 ? _c : "");
165
+ changeMap.set(k, change);
166
+ };
167
+ var upsertCell = function upsertCell(sheetId, r, c) {
168
+ var _a, _b, _c;
169
+ var cell = (_c = (_b = (_a = (0, _fortuneCore.getFlowdata)(ctxAfter, sheetId)) === null || _a === void 0 ? void 0 : _a[r]) === null || _b === void 0 ? void 0 : _b[c]) !== null && _c !== void 0 ? _c : null;
170
+ var key = "".concat(r, "_").concat(c);
171
+ upsert({
172
+ sheetId: sheetId,
173
+ path: ["celldata"],
174
+ key: key,
175
+ value: {
176
+ r: r,
177
+ c: c,
178
+ v: cell
179
+ },
180
+ type: cell == null ? "delete" : "update"
181
+ });
182
+ };
183
+ patches.forEach(function (p) {
184
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w;
185
+ var path = p.path;
186
+ if ((path === null || path === void 0 ? void 0 : path[0]) !== "luckysheetfile") return;
187
+ var sheetIndex = path[1];
188
+ if (!_lodash.default.isNumber(sheetIndex)) return;
189
+ var sheetBefore = (_a = ctxBefore.luckysheetfile) === null || _a === void 0 ? void 0 : _a[sheetIndex];
190
+ var sheetAfter = (_b = ctxAfter.luckysheetfile) === null || _b === void 0 ? void 0 : _b[sheetIndex];
191
+ var sheetId = (sheetAfter === null || sheetAfter === void 0 ? void 0 : sheetAfter.id) || (sheetBefore === null || sheetBefore === void 0 ? void 0 : sheetBefore.id);
192
+ if (!sheetId) return;
193
+ var root = path[2];
194
+ if (root === "data") {
195
+ if (_lodash.default.isNumber(path[3]) && _lodash.default.isNumber(path[4])) {
196
+ upsertCell(sheetId, path[3], path[4]);
197
+ return;
198
+ }
199
+ if (_lodash.default.isNumber(path[3]) && path.length === 4) {
200
+ var r = path[3];
201
+ var beforeRow = (_d = (_c = sheetBefore === null || sheetBefore === void 0 ? void 0 : sheetBefore.data) === null || _c === void 0 ? void 0 : _c[r]) !== null && _d !== void 0 ? _d : [];
202
+ var afterRow = (_f = (_e = sheetAfter === null || sheetAfter === void 0 ? void 0 : sheetAfter.data) === null || _e === void 0 ? void 0 : _e[r]) !== null && _f !== void 0 ? _f : [];
203
+ var max = Math.max((_g = beforeRow.length) !== null && _g !== void 0 ? _g : 0, (_h = afterRow.length) !== null && _h !== void 0 ? _h : 0);
204
+ for (var c = 0; c < max; c += 1) {
205
+ if (!_lodash.default.isEqual((_j = beforeRow[c]) !== null && _j !== void 0 ? _j : null, (_k = afterRow[c]) !== null && _k !== void 0 ? _k : null)) {
206
+ upsertCell(sheetId, r, c);
207
+ }
208
+ }
209
+ return;
210
+ }
211
+ if (path.length === 3) {
212
+ var dataAfter = sheetAfter === null || sheetAfter === void 0 ? void 0 : sheetAfter.data;
213
+ var rows = (_l = dataAfter === null || dataAfter === void 0 ? void 0 : dataAfter.length) !== null && _l !== void 0 ? _l : 0;
214
+ var cols = rows > 0 ? (_o = (_m = dataAfter === null || dataAfter === void 0 ? void 0 : dataAfter[0]) === null || _m === void 0 ? void 0 : _m.length) !== null && _o !== void 0 ? _o : 0 : 0;
215
+ var size = rows * cols;
216
+ if (size > 50000 && updateAllCell) {
217
+ updateAllCell(sheetId);
218
+ return;
219
+ }
220
+ for (var r = 0; r < rows; r += 1) {
221
+ var beforeRow = (_q = (_p = sheetBefore === null || sheetBefore === void 0 ? void 0 : sheetBefore.data) === null || _p === void 0 ? void 0 : _p[r]) !== null && _q !== void 0 ? _q : [];
222
+ var afterRow = (_s = (_r = sheetAfter === null || sheetAfter === void 0 ? void 0 : sheetAfter.data) === null || _r === void 0 ? void 0 : _r[r]) !== null && _s !== void 0 ? _s : [];
223
+ var max = Math.max((_t = beforeRow.length) !== null && _t !== void 0 ? _t : 0, (_u = afterRow.length) !== null && _u !== void 0 ? _u : 0);
224
+ for (var c = 0; c < max; c += 1) {
225
+ if (!_lodash.default.isEqual((_v = beforeRow[c]) !== null && _v !== void 0 ? _v : null, (_w = afterRow[c]) !== null && _w !== void 0 ? _w : null)) {
226
+ upsertCell(sheetId, r, c);
227
+ }
228
+ }
229
+ }
230
+ }
231
+ return;
232
+ }
233
+ if (typeof root === "string" && mapFields.has(root)) {
234
+ var key = path[3];
235
+ if (typeof key === "string") {
236
+ upsert({
237
+ sheetId: sheetId,
238
+ path: [root],
239
+ key: key,
240
+ value: p.value,
241
+ type: p.op === "remove" || p.value == null ? "delete" : "update"
242
+ });
243
+ }
244
+ }
245
+ });
246
+ var changes = Array.from(changeMap.values());
247
+ if (changes.length > 0) updateCellYdoc(changes);
248
+ }, []);
154
249
  function reduceUndoList(ctx, ctxBefore) {
155
250
  var sheetsId = ctx.luckysheetfile.map(function (sheet) {
156
251
  return sheet.id;
@@ -308,6 +403,7 @@ var Workbook = /*#__PURE__*/_react.default.forwardRef(function (_a, ref) {
308
403
  delete inversedOptions.addSheet.value.data;
309
404
  }
310
405
  emitOp(newContext, history.inversePatches, inversedOptions, true);
406
+ emitYjsFromPatches(ctx_, newContext, history.inversePatches);
311
407
  var sheetIdxAfterUndo = (0, _fortuneCore.getSheetIndex)(newContext, newContext.currentSheetId);
312
408
  var nw = __assign(__assign({}, newContext), sheetIdxAfterUndo != null && ((_f = newContext.luckysheetfile[sheetIdxAfterUndo]) === null || _f === void 0 ? void 0 : _f.config) != null ? {
313
409
  config: newContext.luckysheetfile[sheetIdxAfterUndo].config
@@ -336,6 +432,7 @@ var Workbook = /*#__PURE__*/_react.default.forwardRef(function (_a, ref) {
336
432
  });
337
433
  globalCache.current.undoList.push(history);
338
434
  emitOp(newContext, history.patches, history.options);
435
+ emitYjsFromPatches(ctx_, newContext, history.patches);
339
436
  var sheetIdxAfterRedo = (0, _fortuneCore.getSheetIndex)(newContext, newContext.currentSheetId);
340
437
  var nw = __assign(__assign({}, newContext), sheetIdxAfterRedo != null && ((_a = newContext.luckysheetfile[sheetIdxAfterRedo]) === null || _a === void 0 ? void 0 : _a.config) != null ? {
341
438
  config: newContext.luckysheetfile[sheetIdxAfterRedo].config
@@ -362,18 +459,13 @@ var Workbook = /*#__PURE__*/_react.default.forwardRef(function (_a, ref) {
362
459
  (0, _react.useEffect)(function () {
363
460
  var _a, _b;
364
461
  setContext(function (ctx) {
365
- var _a, _b;
366
462
  var gridData = (0, _fortuneCore.getFlowdata)(ctx);
367
463
  var cellData = _fortuneCore.api.dataToCelldata(gridData);
368
- var denominatedUsed = false;
369
- for (var _i = 0, cellData_1 = cellData; _i < cellData_1.length; _i++) {
370
- var cell = cellData_1[_i];
464
+ var denominatedUsed = (cellData !== null && cellData !== void 0 ? cellData : []).some(function (cell) {
465
+ var _a, _b;
371
466
  var value = (_b = (_a = cell === null || cell === void 0 ? void 0 : cell.v) === null || _a === void 0 ? void 0 : _a.m) === null || _b === void 0 ? void 0 : _b.toString();
372
- if ((value === null || value === void 0 ? void 0 : value.includes("BTC")) || (value === null || value === void 0 ? void 0 : value.includes("ETH")) || (value === null || value === void 0 ? void 0 : value.includes("SOL"))) {
373
- denominatedUsed = true;
374
- break;
375
- }
376
- }
467
+ return (value === null || value === void 0 ? void 0 : value.includes("BTC")) || (value === null || value === void 0 ? void 0 : value.includes("ETH")) || (value === null || value === void 0 ? void 0 : value.includes("SOL"));
468
+ });
377
469
  var denoWarn = document.getElementById("denomination-warning");
378
470
  var scrollBar = document.getElementsByClassName("luckysheet-scrollbar-x")[0];
379
471
  if (denominatedUsed && denoWarn) {
@@ -447,6 +539,32 @@ var Workbook = /*#__PURE__*/_react.default.forwardRef(function (_a, ref) {
447
539
  context.hooks.afterOrderChanges();
448
540
  }
449
541
  }, [currentSheet === null || currentSheet === void 0 ? void 0 : currentSheet.order]);
542
+ var sheetColorSig = (0, _react.useMemo)(function () {
543
+ var _a;
544
+ return ((_a = context === null || context === void 0 ? void 0 : context.luckysheetfile) !== null && _a !== void 0 ? _a : []).map(function (s) {
545
+ var _a;
546
+ return "".concat(s.id, ":").concat((_a = s.color) !== null && _a !== void 0 ? _a : "");
547
+ }).join("|");
548
+ }, [context === null || context === void 0 ? void 0 : context.luckysheetfile]);
549
+ (0, _react.useEffect)(function () {
550
+ var _a;
551
+ if ((_a = context === null || context === void 0 ? void 0 : context.hooks) === null || _a === void 0 ? void 0 : _a.afterColorChanges) {
552
+ context.hooks.afterColorChanges();
553
+ }
554
+ }, [sheetColorSig]);
555
+ var sheetHideSig = (0, _react.useMemo)(function () {
556
+ var _a;
557
+ return ((_a = context === null || context === void 0 ? void 0 : context.luckysheetfile) !== null && _a !== void 0 ? _a : []).map(function (s) {
558
+ var _a;
559
+ return "".concat(s.id, ":").concat((_a = s.hide) !== null && _a !== void 0 ? _a : 0);
560
+ }).join("|");
561
+ }, [context === null || context === void 0 ? void 0 : context.luckysheetfile]);
562
+ (0, _react.useEffect)(function () {
563
+ var _a;
564
+ if ((_a = context === null || context === void 0 ? void 0 : context.hooks) === null || _a === void 0 ? void 0 : _a.afterHideChanges) {
565
+ context.hooks.afterHideChanges();
566
+ }
567
+ }, [sheetHideSig]);
450
568
  (0, _react.useEffect)(function () {
451
569
  var _a;
452
570
  if ((_a = context === null || context === void 0 ? void 0 : context.hooks) === null || _a === void 0 ? void 0 : _a.afterConfigChanges) {
@@ -513,6 +631,18 @@ var Workbook = /*#__PURE__*/_react.default.forwardRef(function (_a, ref) {
513
631
  context.hooks.conditionFormatChange();
514
632
  }
515
633
  }, [currentSheet === null || currentSheet === void 0 ? void 0 : currentSheet.luckysheet_conditionformat_save]);
634
+ (0, _react.useEffect)(function () {
635
+ var _a;
636
+ if ((_a = context === null || context === void 0 ? void 0 : context.hooks) === null || _a === void 0 ? void 0 : _a.filterSelectChange) {
637
+ context.hooks.filterSelectChange();
638
+ }
639
+ }, [currentSheet === null || currentSheet === void 0 ? void 0 : currentSheet.filter_select]);
640
+ (0, _react.useEffect)(function () {
641
+ var _a;
642
+ if ((_a = context === null || context === void 0 ? void 0 : context.hooks) === null || _a === void 0 ? void 0 : _a.filterChange) {
643
+ context.hooks.filterChange();
644
+ }
645
+ }, [currentSheet === null || currentSheet === void 0 ? void 0 : currentSheet.filter]);
516
646
  (0, _react.useEffect)(function () {
517
647
  var _a;
518
648
  if ((_a = context === null || context === void 0 ? void 0 : context.hooks) === null || _a === void 0 ? void 0 : _a.hyperlinkChange) {
@@ -757,7 +887,7 @@ var Workbook = /*#__PURE__*/_react.default.forwardRef(function (_a, ref) {
757
887
  });
758
888
  }
759
889
  var nativeEvent = e.nativeEvent;
760
- if ((e.ctrlKey || e.metaKey) && e.code === "KeyZ") {
890
+ if ((e.ctrlKey || e.metaKey) && e.code === "KeyZ" && context.luckysheetCellUpdate.length === 0) {
761
891
  if (e.shiftKey) {
762
892
  handleRedo();
763
893
  } else {
@@ -766,7 +896,7 @@ var Workbook = /*#__PURE__*/_react.default.forwardRef(function (_a, ref) {
766
896
  e.stopPropagation();
767
897
  return;
768
898
  }
769
- if ((e.ctrlKey || e.metaKey) && e.code === "KeyY") {
899
+ if ((e.ctrlKey || e.metaKey) && e.code === "KeyY" && context.luckysheetCellUpdate.length === 0) {
770
900
  handleRedo();
771
901
  e.stopPropagation();
772
902
  e.preventDefault();
@@ -0,0 +1,24 @@
1
+ import { type RefObject } from "react";
2
+ import { type Context } from "@fileverse-dev/fortune-core";
3
+ import type { SetContextOptions } from "../context";
4
+ export type FormulaHistoryEntry = {
5
+ text: string;
6
+ caret: number;
7
+ spanValues: string[];
8
+ };
9
+ export type FormulaEditorHistoryPrimary = "cell" | "fx";
10
+ 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): {
12
+ formulaHistoryRef: RefObject<{
13
+ active: boolean;
14
+ entries: FormulaHistoryEntry[];
15
+ index: number;
16
+ }>;
17
+ preTextRef: RefObject<string>;
18
+ preFormulaSpanValuesRef: RefObject<string[] | null>;
19
+ resetFormulaHistory: () => void;
20
+ handleFormulaHistoryUndoRedo: (isRedo: boolean) => boolean;
21
+ capturePreFormulaState: () => void;
22
+ appendFormulaHistoryFromPrimaryEditor: (getCaret: () => number) => void;
23
+ };
24
+ export {};
@@ -0,0 +1,126 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.useFormulaEditorHistory = useFormulaEditorHistory;
7
+ var _react = require("react");
8
+ var _lodash = _interopRequireDefault(require("lodash"));
9
+ var _fortuneCore = require("@fileverse-dev/fortune-core");
10
+ var _helper = require("../components/SheetOverlay/helper");
11
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
12
+ var MAX_FORMULA_HISTORY = 100;
13
+ function useFormulaEditorHistory(primaryRef, cellInputRef, fxInputRef, setContext, primary) {
14
+ var preTextRef = (0, _react.useRef)("");
15
+ var preFormulaSpanValuesRef = (0, _react.useRef)(null);
16
+ var formulaHistoryRef = (0, _react.useRef)({
17
+ active: false,
18
+ entries: [],
19
+ index: -1
20
+ });
21
+ var resetFormulaHistory = (0, _react.useCallback)(function () {
22
+ formulaHistoryRef.current = {
23
+ active: false,
24
+ entries: [],
25
+ index: -1
26
+ };
27
+ preFormulaSpanValuesRef.current = null;
28
+ }, []);
29
+ var pushFormulaHistoryEntry = (0, _react.useCallback)(function (entry) {
30
+ var history = formulaHistoryRef.current;
31
+ var current = history.entries[history.index];
32
+ if (current && current.spanValues.length > 0 && entry.spanValues.length > 0 && _lodash.default.isEqual(current.spanValues, entry.spanValues)) {
33
+ return;
34
+ }
35
+ if (current && current.spanValues.length === 0 && entry.spanValues.length === 0 && current.text === entry.text && current.caret === entry.caret) {
36
+ return;
37
+ }
38
+ var nextEntries = history.entries.slice(0, history.index + 1);
39
+ nextEntries.push(entry);
40
+ if (nextEntries.length > MAX_FORMULA_HISTORY) {
41
+ nextEntries.shift();
42
+ }
43
+ history.entries = nextEntries;
44
+ history.index = nextEntries.length - 1;
45
+ history.active = true;
46
+ }, []);
47
+ var applyFormulaHistoryEntry = (0, _react.useCallback)(function (entry) {
48
+ var primaryEl = primaryRef.current;
49
+ if (!primaryEl) return;
50
+ var safeText = (0, _fortuneCore.escapeScriptTag)(entry.text || "");
51
+ var html = safeText.startsWith("=") ? (0, _fortuneCore.functionHTMLGenerate)(safeText) : (0, _fortuneCore.escapeHTMLTag)(safeText);
52
+ var cell = cellInputRef.current;
53
+ var fx = fxInputRef.current;
54
+ primaryEl.innerHTML = html;
55
+ if (primary === "cell") {
56
+ if (fx) fx.innerHTML = html;
57
+ } else if (cell) {
58
+ cell.innerHTML = html;
59
+ }
60
+ (0, _helper.setCursorPosition)(primaryEl, Math.min(entry.caret, entry.text.length));
61
+ setContext(function (draftCtx) {
62
+ if (primary === "cell") {
63
+ if (!cellInputRef.current) return;
64
+ (0, _fortuneCore.handleFormulaInput)(draftCtx, fxInputRef.current, cellInputRef.current, 0);
65
+ } else {
66
+ if (!fxInputRef.current) return;
67
+ (0, _fortuneCore.handleFormulaInput)(draftCtx, cellInputRef.current, fxInputRef.current, 0);
68
+ }
69
+ });
70
+ }, [cellInputRef, fxInputRef, primary, primaryRef, setContext]);
71
+ var handleFormulaHistoryUndoRedo = (0, _react.useCallback)(function (isRedo) {
72
+ var history = formulaHistoryRef.current;
73
+ if (!history.active || history.entries.length === 0) return false;
74
+ var nextIndex = isRedo ? history.index + 1 : history.index - 1;
75
+ if (nextIndex < 0 || nextIndex >= history.entries.length) return true;
76
+ history.index = nextIndex;
77
+ applyFormulaHistoryEntry(history.entries[nextIndex]);
78
+ return true;
79
+ }, [applyFormulaHistoryEntry]);
80
+ var capturePreFormulaState = (0, _react.useCallback)(function () {
81
+ var el = primaryRef.current;
82
+ if (!el) return;
83
+ preTextRef.current = el.innerText;
84
+ preFormulaSpanValuesRef.current = Array.from(el.querySelectorAll("span.fortune-formula-functionrange-cell")).map(function (node) {
85
+ var _a;
86
+ return (_a = node.textContent) !== null && _a !== void 0 ? _a : "";
87
+ });
88
+ }, [primaryRef]);
89
+ var appendFormulaHistoryFromPrimaryEditor = (0, _react.useCallback)(function (getCaret) {
90
+ var _a, _b;
91
+ var el = primaryRef.current;
92
+ if (!el) return;
93
+ var currentText = el.innerText || "";
94
+ if (currentText.startsWith("=")) {
95
+ var caret = getCaret();
96
+ var spanValues = Array.from(el.querySelectorAll("span.fortune-formula-functionrange-cell")).map(function (node) {
97
+ var _a;
98
+ return (_a = node.textContent) !== null && _a !== void 0 ? _a : "";
99
+ });
100
+ if (!formulaHistoryRef.current.active) {
101
+ var seedText = preTextRef.current || "";
102
+ pushFormulaHistoryEntry({
103
+ text: seedText,
104
+ caret: Math.min(caret, seedText.length),
105
+ spanValues: (_b = (_a = preFormulaSpanValuesRef.current) !== null && _a !== void 0 ? _a : spanValues) !== null && _b !== void 0 ? _b : []
106
+ });
107
+ }
108
+ pushFormulaHistoryEntry({
109
+ text: currentText,
110
+ caret: caret,
111
+ spanValues: spanValues
112
+ });
113
+ } else if (formulaHistoryRef.current.active) {
114
+ resetFormulaHistory();
115
+ }
116
+ }, [primaryRef, pushFormulaHistoryEntry, resetFormulaHistory]);
117
+ return {
118
+ formulaHistoryRef: formulaHistoryRef,
119
+ preTextRef: preTextRef,
120
+ preFormulaSpanValuesRef: preFormulaSpanValuesRef,
121
+ resetFormulaHistory: resetFormulaHistory,
122
+ handleFormulaHistoryUndoRedo: handleFormulaHistoryUndoRedo,
123
+ capturePreFormulaState: capturePreFormulaState,
124
+ appendFormulaHistoryFromPrimaryEditor: appendFormulaHistoryFromPrimaryEditor
125
+ };
126
+ }
@@ -0,0 +1,2 @@
1
+ import { type RefObject } from "react";
2
+ export declare function useRerenderOnFormulaCaret(editorRef: RefObject<HTMLDivElement | null>, editSessionActive: boolean): void;
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.useRerenderOnFormulaCaret = useRerenderOnFormulaCaret;
7
+ var _react = require("react");
8
+ function useRerenderOnFormulaCaret(editorRef, editSessionActive) {
9
+ var _a = (0, _react.useState)(0),
10
+ bump = _a[1];
11
+ (0, _react.useEffect)(function () {
12
+ if (!editSessionActive) return;
13
+ var onSelectionChange = function onSelectionChange() {
14
+ var _a, _b;
15
+ var el = editorRef.current;
16
+ if (!el) return;
17
+ var text = (_b = (_a = el.innerText) === null || _a === void 0 ? void 0 : _a.trim()) !== null && _b !== void 0 ? _b : "";
18
+ if (!text.startsWith("=")) return;
19
+ var sel = window.getSelection();
20
+ if (!(sel === null || sel === void 0 ? void 0 : sel.rangeCount) || !el.contains(sel.getRangeAt(0).startContainer)) {
21
+ return;
22
+ }
23
+ bump(function (n) {
24
+ return n + 1;
25
+ });
26
+ };
27
+ document.addEventListener("selectionchange", onSelectionChange);
28
+ return function () {
29
+ return document.removeEventListener("selectionchange", onSelectionChange);
30
+ };
31
+ }, [editSessionActive, editorRef]);
32
+ }