@extend-ai/react-xlsx 0.8.1 → 0.8.2

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.
package/dist/index.js CHANGED
@@ -6261,6 +6261,84 @@ function resizeImageRect(rect, handle, deltaX, deltaY, minimumSize = 16) {
6261
6261
  return { height, left, top, width };
6262
6262
  }
6263
6263
 
6264
+ // src/safe-calculate.ts
6265
+ var SHEET_REF_REGEX = /'((?:[^']|'')+)'!|([A-Za-z_\u0080-\uFFFF][\w.\u0080-\uFFFF]*)!/g;
6266
+ function collectReferencedSheetNames(workbook) {
6267
+ const referenced = /* @__PURE__ */ new Set();
6268
+ for (let sheetIdx = 0; sheetIdx < workbook.sheetCount; sheetIdx += 1) {
6269
+ let sheet;
6270
+ try {
6271
+ sheet = workbook.getSheet(sheetIdx);
6272
+ } catch {
6273
+ continue;
6274
+ }
6275
+ const cells = sheet.formulaCells;
6276
+ if (!Array.isArray(cells)) {
6277
+ continue;
6278
+ }
6279
+ for (const cell of cells) {
6280
+ const formula = cell?.formula;
6281
+ if (!formula) {
6282
+ continue;
6283
+ }
6284
+ SHEET_REF_REGEX.lastIndex = 0;
6285
+ let match;
6286
+ while ((match = SHEET_REF_REGEX.exec(formula)) !== null) {
6287
+ const raw = match[1] ?? match[2];
6288
+ if (!raw) {
6289
+ continue;
6290
+ }
6291
+ referenced.add(raw.replace(/''/g, "'"));
6292
+ }
6293
+ }
6294
+ }
6295
+ return referenced;
6296
+ }
6297
+ function hasUnresolvedSheetReferences(workbook) {
6298
+ let names;
6299
+ try {
6300
+ names = workbook.sheetNames;
6301
+ } catch {
6302
+ return false;
6303
+ }
6304
+ const known = new Set(names);
6305
+ const referenced = collectReferencedSheetNames(workbook);
6306
+ for (const name of referenced) {
6307
+ if (!known.has(name)) {
6308
+ return true;
6309
+ }
6310
+ }
6311
+ return false;
6312
+ }
6313
+ function safeCalculate(workbook, options = {}) {
6314
+ if (hasUnresolvedSheetReferences(workbook)) {
6315
+ return { workbook, calculated: false, skipReason: "unresolved-sheet-refs" };
6316
+ }
6317
+ try {
6318
+ workbook.calculate();
6319
+ return { workbook, calculated: true, skipReason: null };
6320
+ } catch (err) {
6321
+ console.warn("[react-xlsx] workbook.calculate() trapped; falling back to cached formula values", err);
6322
+ if (options.reparse) {
6323
+ try {
6324
+ return { workbook: options.reparse(), calculated: false, skipReason: "calculate-trapped" };
6325
+ } catch (reparseErr) {
6326
+ console.warn("[react-xlsx] workbook reparse after calculate trap failed", reparseErr);
6327
+ }
6328
+ }
6329
+ return { workbook, calculated: false, skipReason: "calculate-trapped" };
6330
+ }
6331
+ }
6332
+ function tryRecalculate(workbook) {
6333
+ try {
6334
+ workbook.calculate();
6335
+ return { calculated: true, error: null };
6336
+ } catch (err) {
6337
+ console.warn("[react-xlsx] workbook.calculate() trapped during recalculation", err);
6338
+ return { calculated: false, error: err };
6339
+ }
6340
+ }
6341
+
6264
6342
  // src/wasm.ts
6265
6343
  var wasmModulePromise = null;
6266
6344
  function getSheetsWasmModule() {
@@ -7068,18 +7146,21 @@ async function resolveWorkbookBuffer({ file, src }, signal) {
7068
7146
  }
7069
7147
  async function parseWorkbookBuffer(buffer) {
7070
7148
  const wasmModule = await getSheetsWasmModule();
7071
- const workbook = wasmModule.Workbook.fromBytes(new Uint8Array(buffer));
7149
+ const initialWorkbook = wasmModule.Workbook.fromBytes(new Uint8Array(buffer));
7072
7150
  let totalFormulas = 0;
7073
- for (let index = 0; index < workbook.sheetCount; index += 1) {
7074
- totalFormulas += workbook.getSheet(index).formulaCount;
7151
+ for (let index = 0; index < initialWorkbook.sheetCount; index += 1) {
7152
+ totalFormulas += initialWorkbook.getSheet(index).formulaCount;
7075
7153
  }
7076
7154
  const shouldAutoCalculate = totalFormulas <= FORMULA_COUNT_THRESHOLD;
7077
- if (shouldAutoCalculate) {
7078
- workbook.calculate();
7155
+ if (!shouldAutoCalculate) {
7156
+ return { shouldAutoCalculate, workbook: initialWorkbook };
7079
7157
  }
7158
+ const result = safeCalculate(initialWorkbook, {
7159
+ reparse: () => wasmModule.Workbook.fromBytes(new Uint8Array(buffer))
7160
+ });
7080
7161
  return {
7081
- shouldAutoCalculate,
7082
- workbook
7162
+ shouldAutoCalculate: result.calculated,
7163
+ workbook: result.workbook
7083
7164
  };
7084
7165
  }
7085
7166
  function scheduleLowPriorityTask(task) {
@@ -8320,7 +8401,10 @@ function useXlsxViewerController(options) {
8320
8401
  if (!shouldAutoCalculate) {
8321
8402
  return;
8322
8403
  }
8323
- targetWorkbook.calculate();
8404
+ const result = tryRecalculate(targetWorkbook);
8405
+ if (!result.calculated) {
8406
+ setShouldAutoCalculate(false);
8407
+ }
8324
8408
  }, [shouldAutoCalculate]);
8325
8409
  const getActiveWorksheet = React.useCallback(() => {
8326
8410
  if (!workbook || !activeSheet) {
@@ -8957,8 +9041,12 @@ function useXlsxViewerController(options) {
8957
9041
  if (!workbook) {
8958
9042
  return;
8959
9043
  }
8960
- workbook.calculate();
8961
- refreshWorkbookState(workbook);
9044
+ const result = tryRecalculate(workbook);
9045
+ if (result.calculated) {
9046
+ refreshWorkbookState(workbook);
9047
+ return;
9048
+ }
9049
+ setShouldAutoCalculate(false);
8962
9050
  }, [refreshWorkbookState, workbook]);
8963
9051
  const resizeColumn = React.useCallback((col, widthPx) => {
8964
9052
  if (readOnly && !canResizeReadOnly || !workbook || !activeSheet) {
@@ -20459,6 +20547,7 @@ function GridRow({
20459
20547
  actualRow,
20460
20548
  editingCell,
20461
20549
  editingValue,
20550
+ frozenRowHeaderZIndex,
20462
20551
  getCellData,
20463
20552
  headerLabelLiveScale,
20464
20553
  leadingSpacerWidth,
@@ -20475,6 +20564,7 @@ function GridRow({
20475
20564
  readOnly,
20476
20565
  renderCellAdornment,
20477
20566
  rowHeight,
20567
+ rowHeaderZIndex,
20478
20568
  rowHeaderWidth,
20479
20569
  stickyLeftByCol,
20480
20570
  stickyTop,
@@ -20507,7 +20597,7 @@ function GridRow({
20507
20597
  textAlign: "center",
20508
20598
  userSelect: "none",
20509
20599
  width: rowHeaderWidth,
20510
- zIndex: stickyTop !== void 0 ? 45 : 35
20600
+ zIndex: stickyTop !== void 0 ? frozenRowHeaderZIndex : rowHeaderZIndex
20511
20601
  },
20512
20602
  children: /* @__PURE__ */ jsxs3("div", { style: { position: "relative" }, children: [
20513
20603
  /* @__PURE__ */ jsx3(
@@ -22316,7 +22406,7 @@ function XlsxGrid({
22316
22406
  const geometryCell = resolvePointerCellFromGeometry(clientX, clientY);
22317
22407
  const hitCell = resolvePointerCellFromHitTest(clientX, clientY);
22318
22408
  const actualRow = hitCell && rowIndexByActual.has(hitCell.row) ? hitCell.row : geometryCell?.row;
22319
- const actualCol = geometryCell?.col ?? hitCell?.col;
22409
+ const actualCol = hitCell?.col ?? geometryCell?.col;
22320
22410
  if (actualRow === void 0 || actualCol === void 0) {
22321
22411
  return null;
22322
22412
  }
@@ -24971,7 +25061,7 @@ function XlsxGrid({
24971
25061
  paneContext.textBaseline = "middle";
24972
25062
  paneContext.fillText(`${row.actualRow + 1}`, rowHeaderWidth / 2, row.localTop + row.height / 2);
24973
25063
  }
24974
- cornerContext.fillStyle = palette.rowHeaderSurface;
25064
+ cornerContext.fillStyle = palette.headerSurface;
24975
25065
  cornerContext.fillRect(0, 0, rowHeaderWidth, headerHeight);
24976
25066
  cornerContext.strokeStyle = palette.border;
24977
25067
  cornerContext.lineWidth = 1;
@@ -25254,7 +25344,18 @@ function XlsxGrid({
25254
25344
  const canvasSelectionTransition = shouldAnimateCanvasSelection ? "left 120ms cubic-bezier(0.22, 1, 0.36, 1), top 120ms cubic-bezier(0.22, 1, 0.36, 1), width 120ms cubic-bezier(0.22, 1, 0.36, 1), height 120ms cubic-bezier(0.22, 1, 0.36, 1), opacity 100ms linear" : "none";
25255
25345
  const rowColSpan = renderedCols.length + 1 + (leadingColumnSpacerWidth > 0 ? 1 : 0) + (trailingColumnSpacerWidth > 0 ? 1 : 0);
25256
25346
  const gutterSeparatorShadow = `inset -1px 0 0 ${palette.border}, inset 0 -1px 0 ${palette.border}`;
25257
- const canvasHeaderOverlayZIndex = 1e5;
25347
+ const maxDrawingOverlayZIndex = Math.max(
25348
+ 0,
25349
+ ...shapes.map((shape) => shape.zIndex + 20),
25350
+ ...formControls.map((control) => control.zIndex + 20),
25351
+ ...images.map((image) => image.zIndex + 22),
25352
+ ...charts.map((chart) => chart.zIndex + 22)
25353
+ );
25354
+ const rowHeaderOverlayZIndex = Math.max(35, maxDrawingOverlayZIndex + 1);
25355
+ const canvasHeaderOverlayZIndex = Math.max(50, rowHeaderOverlayZIndex + 1);
25356
+ const stickyHeaderOverlayZIndex = canvasHeaderOverlayZIndex + 1;
25357
+ const cornerHeaderOverlayZIndex = canvasHeaderOverlayZIndex + 2;
25358
+ const frozenRowHeaderOverlayZIndex = rowHeaderOverlayZIndex;
25258
25359
  const headerCellStyle = scaleCssProperties({
25259
25360
  backgroundColor: palette.headerSurface,
25260
25361
  borderBottom: "none",
@@ -25271,7 +25372,7 @@ function XlsxGrid({
25271
25372
  top: 0,
25272
25373
  userSelect: "none",
25273
25374
  whiteSpace: "nowrap",
25274
- zIndex: 50
25375
+ zIndex: canvasHeaderOverlayZIndex
25275
25376
  }, zoomFactor);
25276
25377
  const columnResizeHandleStyle = scaleCssProperties({
25277
25378
  backgroundColor: "transparent",
@@ -25360,7 +25461,7 @@ function XlsxGrid({
25360
25461
  display: topFrozenHeaderCanvasWidth > 0 && drawingViewport.height > 0 ? "block" : "none",
25361
25462
  left: displayRowHeaderWidth,
25362
25463
  top: 0,
25363
- zIndex: canvasHeaderOverlayZIndex + 1
25464
+ zIndex: stickyHeaderOverlayZIndex
25364
25465
  };
25365
25466
  const canvasTopScrollHeaderStyle = {
25366
25467
  ...canvasHeaderBaseStyle,
@@ -25374,7 +25475,7 @@ function XlsxGrid({
25374
25475
  display: leftFrozenHeaderCanvasHeight > 0 && drawingViewport.width > 0 ? "block" : "none",
25375
25476
  left: 0,
25376
25477
  top: displayHeaderHeight,
25377
- zIndex: canvasHeaderOverlayZIndex + 1
25478
+ zIndex: stickyHeaderOverlayZIndex
25378
25479
  };
25379
25480
  const canvasLeftScrollHeaderStyle = {
25380
25481
  ...canvasHeaderBaseStyle,
@@ -25390,7 +25491,7 @@ function XlsxGrid({
25390
25491
  position: "absolute",
25391
25492
  top: 0,
25392
25493
  transformOrigin: "0 0",
25393
- zIndex: canvasHeaderOverlayZIndex + 1
25494
+ zIndex: cornerHeaderOverlayZIndex
25394
25495
  };
25395
25496
  const editingOverlayRect = experimentalCanvas && editingCell ? resolveCellDisplayRect(editingCell) : null;
25396
25497
  const activeCellAdornment = experimentalCanvas && activeCell ? renderCellAdornment(activeCell) : null;
@@ -26724,7 +26825,7 @@ function XlsxGrid({
26724
26825
  )),
26725
26826
  trailingColumnSpacerWidth > 0 ? /* @__PURE__ */ jsx3("col", { style: { width: trailingColumnSpacerWidth } }) : null
26726
26827
  ] }),
26727
- /* @__PURE__ */ jsx3("thead", { style: { position: "sticky", top: 0, zIndex: 50 }, children: /* @__PURE__ */ jsxs3("tr", { children: [
26828
+ /* @__PURE__ */ jsx3("thead", { style: { position: "sticky", top: 0, zIndex: canvasHeaderOverlayZIndex }, children: /* @__PURE__ */ jsxs3("tr", { children: [
26728
26829
  /* @__PURE__ */ jsx3(
26729
26830
  "th",
26730
26831
  {
@@ -26733,7 +26834,7 @@ function XlsxGrid({
26733
26834
  backgroundColor: palette.headerSurface,
26734
26835
  left: 0,
26735
26836
  width: displayRowHeaderWidth,
26736
- zIndex: 60
26837
+ zIndex: cornerHeaderOverlayZIndex
26737
26838
  }
26738
26839
  }
26739
26840
  ),
@@ -26747,7 +26848,7 @@ function XlsxGrid({
26747
26848
  style: {
26748
26849
  ...headerCellStyle,
26749
26850
  left: stickyLeftByCol.get(column.actualCol),
26750
- zIndex: stickyLeftByCol.has(column.actualCol) ? 55 : headerCellStyle.zIndex
26851
+ zIndex: stickyLeftByCol.has(column.actualCol) ? stickyHeaderOverlayZIndex : headerCellStyle.zIndex
26751
26852
  },
26752
26853
  children: /* @__PURE__ */ jsxs3("div", { style: { position: "relative" }, children: [
26753
26854
  /* @__PURE__ */ jsx3(
@@ -26802,6 +26903,7 @@ function XlsxGrid({
26802
26903
  actualRow,
26803
26904
  editingCell,
26804
26905
  editingValue,
26906
+ frozenRowHeaderZIndex: frozenRowHeaderOverlayZIndex,
26805
26907
  getCellData,
26806
26908
  leadingSpacerWidth: leadingColumnSpacerWidth,
26807
26909
  onCellClick: handleCellClick,
@@ -26818,6 +26920,7 @@ function XlsxGrid({
26818
26920
  readOnly,
26819
26921
  renderCellAdornment,
26820
26922
  rowHeight: virtualRow.size,
26923
+ rowHeaderZIndex: rowHeaderOverlayZIndex,
26821
26924
  rowHeaderWidth: displayRowHeaderWidth,
26822
26925
  stickyLeftByCol,
26823
26926
  stickyTop: stickyTopByRow.get(actualRow),