@fileverse-dev/fortune-react 1.3.10 → 1.3.11-input-ref

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.
@@ -18,6 +18,58 @@ export function getCursorPosition(editableDiv) {
18
18
  preRange.setEnd(range.endContainer, range.endOffset);
19
19
  return preRange.toString().length;
20
20
  }
21
+ export function setCursorPosition(editableDiv, targetOffset) {
22
+ var _a, _b;
23
+ editableDiv.focus();
24
+ var selection = window.getSelection();
25
+ if (!selection) return;
26
+ var range = document.createRange();
27
+ var walker = document.createTreeWalker(editableDiv, NodeFilter.SHOW_TEXT);
28
+ var remaining = Math.max(0, targetOffset);
29
+ var node = walker.nextNode();
30
+ while (node) {
31
+ var textLength = (_b = (_a = node.textContent) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0;
32
+ if (remaining <= textLength) {
33
+ range.setStart(node, remaining);
34
+ range.collapse(true);
35
+ selection.removeAllRanges();
36
+ selection.addRange(range);
37
+ return;
38
+ }
39
+ remaining -= textLength;
40
+ node = walker.nextNode();
41
+ }
42
+ range.selectNodeContents(editableDiv);
43
+ range.collapse(false);
44
+ selection.removeAllRanges();
45
+ selection.addRange(range);
46
+ }
47
+ export function buildFormulaSuggestionText(editableDiv, formulaName) {
48
+ var fullText = editableDiv.innerText || "";
49
+ var selection = window.getSelection();
50
+ var selectionInEditor = !!(selection === null || selection === void 0 ? void 0 : selection.rangeCount) && editableDiv.contains(selection.getRangeAt(0).startContainer);
51
+ var caretOffset = selectionInEditor ? getCursorPosition(editableDiv) : fullText.length;
52
+ var safeCaretOffset = Math.max(0, Math.min(caretOffset, fullText.length));
53
+ var beforeCaret = fullText.slice(0, safeCaretOffset);
54
+ var afterCaret = fullText.slice(safeCaretOffset);
55
+ var replaceStart = safeCaretOffset;
56
+ var tokenMatch = beforeCaret.match(/[A-Za-z_][A-Za-z0-9_]*$/);
57
+ if (tokenMatch) {
58
+ var token = tokenMatch[0];
59
+ var tokenStart = safeCaretOffset - token.length;
60
+ var charBeforeToken = tokenStart > 0 ? beforeCaret[tokenStart - 1] : "";
61
+ if (tokenStart === 0 || /[\s=(,+\-*/&^<>]$/.test(charBeforeToken)) {
62
+ replaceStart = tokenStart;
63
+ }
64
+ }
65
+ var shouldAddOpeningParen = !afterCaret.startsWith("(");
66
+ var insertedText = "".concat(formulaName).concat(shouldAddOpeningParen ? "(" : "");
67
+ var nextText = fullText.slice(0, replaceStart) + insertedText + afterCaret;
68
+ return {
69
+ text: nextText,
70
+ caretOffset: replaceStart + insertedText.length
71
+ };
72
+ }
21
73
  export function isLetterNumberPattern(str) {
22
74
  var regex = /^[a-zA-Z]+\d+$/;
23
75
  return regex.test(str);
@@ -142,6 +142,101 @@ var Workbook = /*#__PURE__*/React.forwardRef(function (_a, ref) {
142
142
  onOp(patchToOp(ctx, patches, options, undo));
143
143
  }
144
144
  }, [onOp]);
145
+ var emitYjsFromPatches = useCallback(function (ctxBefore, ctxAfter, patches) {
146
+ var _a;
147
+ var _b = (_a = ctxBefore.hooks) !== null && _a !== void 0 ? _a : {},
148
+ updateCellYdoc = _b.updateCellYdoc,
149
+ updateAllCell = _b.updateAllCell;
150
+ if (!updateCellYdoc) return;
151
+ var mapFields = new Set(["celldata", "calcChain", "dataBlockCalcFunction", "liveQueryList", "dataVerification", "hyperlink", "conditionRules"]);
152
+ var changeMap = new Map();
153
+ var upsert = function upsert(change) {
154
+ var _a, _b, _c;
155
+ 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 : "");
156
+ changeMap.set(k, change);
157
+ };
158
+ var upsertCell = function upsertCell(sheetId, r, c) {
159
+ var _a, _b, _c;
160
+ var cell = (_c = (_b = (_a = 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;
161
+ var key = "".concat(r, "_").concat(c);
162
+ upsert({
163
+ sheetId: sheetId,
164
+ path: ["celldata"],
165
+ key: key,
166
+ value: {
167
+ r: r,
168
+ c: c,
169
+ v: cell
170
+ },
171
+ type: cell == null ? "delete" : "update"
172
+ });
173
+ };
174
+ patches.forEach(function (p) {
175
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w;
176
+ var path = p.path;
177
+ if ((path === null || path === void 0 ? void 0 : path[0]) !== "luckysheetfile") return;
178
+ var sheetIndex = path[1];
179
+ if (!_.isNumber(sheetIndex)) return;
180
+ var sheetBefore = (_a = ctxBefore.luckysheetfile) === null || _a === void 0 ? void 0 : _a[sheetIndex];
181
+ var sheetAfter = (_b = ctxAfter.luckysheetfile) === null || _b === void 0 ? void 0 : _b[sheetIndex];
182
+ var sheetId = (sheetAfter === null || sheetAfter === void 0 ? void 0 : sheetAfter.id) || (sheetBefore === null || sheetBefore === void 0 ? void 0 : sheetBefore.id);
183
+ if (!sheetId) return;
184
+ var root = path[2];
185
+ if (root === "data") {
186
+ if (_.isNumber(path[3]) && _.isNumber(path[4])) {
187
+ upsertCell(sheetId, path[3], path[4]);
188
+ return;
189
+ }
190
+ if (_.isNumber(path[3]) && path.length === 4) {
191
+ var r = path[3];
192
+ 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 : [];
193
+ 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 : [];
194
+ var max = Math.max((_g = beforeRow.length) !== null && _g !== void 0 ? _g : 0, (_h = afterRow.length) !== null && _h !== void 0 ? _h : 0);
195
+ for (var c = 0; c < max; c += 1) {
196
+ if (!_.isEqual((_j = beforeRow[c]) !== null && _j !== void 0 ? _j : null, (_k = afterRow[c]) !== null && _k !== void 0 ? _k : null)) {
197
+ upsertCell(sheetId, r, c);
198
+ }
199
+ }
200
+ return;
201
+ }
202
+ if (path.length === 3) {
203
+ var dataAfter = sheetAfter === null || sheetAfter === void 0 ? void 0 : sheetAfter.data;
204
+ var rows = (_l = dataAfter === null || dataAfter === void 0 ? void 0 : dataAfter.length) !== null && _l !== void 0 ? _l : 0;
205
+ 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;
206
+ var size = rows * cols;
207
+ if (size > 50000 && updateAllCell) {
208
+ updateAllCell(sheetId);
209
+ return;
210
+ }
211
+ for (var r = 0; r < rows; r += 1) {
212
+ 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 : [];
213
+ 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 : [];
214
+ var max = Math.max((_t = beforeRow.length) !== null && _t !== void 0 ? _t : 0, (_u = afterRow.length) !== null && _u !== void 0 ? _u : 0);
215
+ for (var c = 0; c < max; c += 1) {
216
+ if (!_.isEqual((_v = beforeRow[c]) !== null && _v !== void 0 ? _v : null, (_w = afterRow[c]) !== null && _w !== void 0 ? _w : null)) {
217
+ upsertCell(sheetId, r, c);
218
+ }
219
+ }
220
+ }
221
+ }
222
+ return;
223
+ }
224
+ if (typeof root === "string" && mapFields.has(root)) {
225
+ var key = path[3];
226
+ if (typeof key === "string") {
227
+ upsert({
228
+ sheetId: sheetId,
229
+ path: [root],
230
+ key: key,
231
+ value: p.value,
232
+ type: p.op === "remove" || p.value == null ? "delete" : "update"
233
+ });
234
+ }
235
+ }
236
+ });
237
+ var changes = Array.from(changeMap.values());
238
+ if (changes.length > 0) updateCellYdoc(changes);
239
+ }, []);
145
240
  function reduceUndoList(ctx, ctxBefore) {
146
241
  var sheetsId = ctx.luckysheetfile.map(function (sheet) {
147
242
  return sheet.id;
@@ -299,6 +394,7 @@ var Workbook = /*#__PURE__*/React.forwardRef(function (_a, ref) {
299
394
  delete inversedOptions.addSheet.value.data;
300
395
  }
301
396
  emitOp(newContext, history.inversePatches, inversedOptions, true);
397
+ emitYjsFromPatches(ctx_, newContext, history.inversePatches);
302
398
  var sheetIdxAfterUndo = getSheetIndex(newContext, newContext.currentSheetId);
303
399
  var nw = __assign(__assign({}, newContext), sheetIdxAfterUndo != null && ((_f = newContext.luckysheetfile[sheetIdxAfterUndo]) === null || _f === void 0 ? void 0 : _f.config) != null ? {
304
400
  config: newContext.luckysheetfile[sheetIdxAfterUndo].config
@@ -327,6 +423,7 @@ var Workbook = /*#__PURE__*/React.forwardRef(function (_a, ref) {
327
423
  });
328
424
  globalCache.current.undoList.push(history);
329
425
  emitOp(newContext, history.patches, history.options);
426
+ emitYjsFromPatches(ctx_, newContext, history.patches);
330
427
  var sheetIdxAfterRedo = getSheetIndex(newContext, newContext.currentSheetId);
331
428
  var nw = __assign(__assign({}, newContext), sheetIdxAfterRedo != null && ((_a = newContext.luckysheetfile[sheetIdxAfterRedo]) === null || _a === void 0 ? void 0 : _a.config) != null ? {
332
429
  config: newContext.luckysheetfile[sheetIdxAfterRedo].config
@@ -353,18 +450,13 @@ var Workbook = /*#__PURE__*/React.forwardRef(function (_a, ref) {
353
450
  useEffect(function () {
354
451
  var _a, _b;
355
452
  setContext(function (ctx) {
356
- var _a, _b;
357
453
  var gridData = getFlowdata(ctx);
358
454
  var cellData = api.dataToCelldata(gridData);
359
- var denominatedUsed = false;
360
- for (var _i = 0, cellData_1 = cellData; _i < cellData_1.length; _i++) {
361
- var cell = cellData_1[_i];
455
+ var denominatedUsed = (cellData !== null && cellData !== void 0 ? cellData : []).some(function (cell) {
456
+ var _a, _b;
362
457
  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();
363
- 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"))) {
364
- denominatedUsed = true;
365
- break;
366
- }
367
- }
458
+ 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"));
459
+ });
368
460
  var denoWarn = document.getElementById("denomination-warning");
369
461
  var scrollBar = document.getElementsByClassName("luckysheet-scrollbar-x")[0];
370
462
  if (denominatedUsed && denoWarn) {
@@ -438,6 +530,32 @@ var Workbook = /*#__PURE__*/React.forwardRef(function (_a, ref) {
438
530
  context.hooks.afterOrderChanges();
439
531
  }
440
532
  }, [currentSheet === null || currentSheet === void 0 ? void 0 : currentSheet.order]);
533
+ var sheetColorSig = useMemo(function () {
534
+ var _a;
535
+ return ((_a = context === null || context === void 0 ? void 0 : context.luckysheetfile) !== null && _a !== void 0 ? _a : []).map(function (s) {
536
+ var _a;
537
+ return "".concat(s.id, ":").concat((_a = s.color) !== null && _a !== void 0 ? _a : "");
538
+ }).join("|");
539
+ }, [context === null || context === void 0 ? void 0 : context.luckysheetfile]);
540
+ useEffect(function () {
541
+ var _a;
542
+ if ((_a = context === null || context === void 0 ? void 0 : context.hooks) === null || _a === void 0 ? void 0 : _a.afterColorChanges) {
543
+ context.hooks.afterColorChanges();
544
+ }
545
+ }, [sheetColorSig]);
546
+ var sheetHideSig = useMemo(function () {
547
+ var _a;
548
+ return ((_a = context === null || context === void 0 ? void 0 : context.luckysheetfile) !== null && _a !== void 0 ? _a : []).map(function (s) {
549
+ var _a;
550
+ return "".concat(s.id, ":").concat((_a = s.hide) !== null && _a !== void 0 ? _a : 0);
551
+ }).join("|");
552
+ }, [context === null || context === void 0 ? void 0 : context.luckysheetfile]);
553
+ useEffect(function () {
554
+ var _a;
555
+ if ((_a = context === null || context === void 0 ? void 0 : context.hooks) === null || _a === void 0 ? void 0 : _a.afterHideChanges) {
556
+ context.hooks.afterHideChanges();
557
+ }
558
+ }, [sheetHideSig]);
441
559
  useEffect(function () {
442
560
  var _a;
443
561
  if ((_a = context === null || context === void 0 ? void 0 : context.hooks) === null || _a === void 0 ? void 0 : _a.afterConfigChanges) {
@@ -504,6 +622,18 @@ var Workbook = /*#__PURE__*/React.forwardRef(function (_a, ref) {
504
622
  context.hooks.conditionFormatChange();
505
623
  }
506
624
  }, [currentSheet === null || currentSheet === void 0 ? void 0 : currentSheet.luckysheet_conditionformat_save]);
625
+ useEffect(function () {
626
+ var _a;
627
+ if ((_a = context === null || context === void 0 ? void 0 : context.hooks) === null || _a === void 0 ? void 0 : _a.filterSelectChange) {
628
+ context.hooks.filterSelectChange();
629
+ }
630
+ }, [currentSheet === null || currentSheet === void 0 ? void 0 : currentSheet.filter_select]);
631
+ useEffect(function () {
632
+ var _a;
633
+ if ((_a = context === null || context === void 0 ? void 0 : context.hooks) === null || _a === void 0 ? void 0 : _a.filterChange) {
634
+ context.hooks.filterChange();
635
+ }
636
+ }, [currentSheet === null || currentSheet === void 0 ? void 0 : currentSheet.filter]);
507
637
  useEffect(function () {
508
638
  var _a;
509
639
  if ((_a = context === null || context === void 0 ? void 0 : context.hooks) === null || _a === void 0 ? void 0 : _a.hyperlinkChange) {
@@ -274,9 +274,12 @@ export function convertCellsToCrypto(_a) {
274
274
  });
275
275
  });
276
276
  setContext(function (ctx) {
277
+ var _a;
277
278
  var d = getFlowdata(ctx);
278
279
  if (!d || !Array.isArray(d)) return;
280
+ var ydocChanges = [];
279
281
  cellUpdates.forEach(function (_a) {
282
+ var _b, _c;
280
283
  var row = _a.row,
281
284
  col = _a.col,
282
285
  baseValue = _a.baseValue,
@@ -296,7 +299,21 @@ export function convertCellsToCrypto(_a) {
296
299
  cellCp.baseCurrency = baseCurrency.toLowerCase();
297
300
  cellCp.baseCurrencyPrice = baseCurrencyPrice;
298
301
  d[row][col] = cellCp;
302
+ ydocChanges.push({
303
+ sheetId: ctx.currentSheetId,
304
+ path: ["celldata"],
305
+ value: {
306
+ r: row,
307
+ c: col,
308
+ v: (_c = (_b = d[row]) === null || _b === void 0 ? void 0 : _b[col]) !== null && _c !== void 0 ? _c : null
309
+ },
310
+ key: "".concat(row, "_").concat(col),
311
+ type: "update"
312
+ });
299
313
  });
314
+ if (ydocChanges.length > 0 && ((_a = ctx === null || ctx === void 0 ? void 0 : ctx.hooks) === null || _a === void 0 ? void 0 : _a.updateCellYdoc)) {
315
+ ctx.hooks.updateCellYdoc(ydocChanges);
316
+ }
300
317
  });
301
318
  setContext(function (ctx) {
302
319
  api.calculateSheetFromula(ctx, ctx.currentSheetId);
@@ -94,13 +94,19 @@ var FxEditor = function FxEditor() {
94
94
  return document.querySelector(".luckysheet-formula-search-item-active");
95
95
  }, []);
96
96
  var insertSelectedFormula = (0, _react.useCallback)(function (formulaName) {
97
- var ht = "<span dir=\"auto\" class=\"luckysheet-formula-text-color\">=</span><span dir=\"auto\" class=\"luckysheet-formula-text-func\">".concat(formulaName, "</span><span dir=\"auto\" class=\"luckysheet-formula-text-lpar\">(</span>");
98
- refs.fxInput.current.innerHTML = ht;
97
+ var fxEditor = refs.fxInput.current;
98
+ if (!fxEditor) return;
99
99
  var cellEditor = document.getElementById("luckysheet-rich-text-editor");
100
+ var _a = (0, _helper.buildFormulaSuggestionText)(fxEditor, formulaName),
101
+ text = _a.text,
102
+ caretOffset = _a.caretOffset;
103
+ var safeText = (0, _fortuneCore.escapeScriptTag)(text);
104
+ var html = safeText.startsWith("=") ? (0, _fortuneCore.functionHTMLGenerate)(safeText) : (0, _fortuneCore.escapeHTMLTag)(safeText);
105
+ fxEditor.innerHTML = html;
100
106
  if (cellEditor) {
101
- cellEditor.innerHTML = ht;
107
+ cellEditor.innerHTML = html;
102
108
  }
103
- (0, _helper.moveCursorToEnd)(refs.fxInput.current);
109
+ (0, _helper.setCursorPosition)(fxEditor, caretOffset);
104
110
  setContext(function (draftCtx) {
105
111
  draftCtx.functionCandidates = [];
106
112
  draftCtx.defaultCandidates = [];