@particle-academy/fancy-sheets 0.1.0 → 0.1.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.
package/dist/index.cjs CHANGED
@@ -578,9 +578,16 @@ function getRecalculationOrder(graph) {
578
578
  }
579
579
 
580
580
  // src/hooks/use-spreadsheet-store.ts
581
+ function recalculateWorkbook(workbook) {
582
+ return {
583
+ ...workbook,
584
+ sheets: workbook.sheets.map(recalculateSheet)
585
+ };
586
+ }
581
587
  function createInitialState(data) {
588
+ const workbook = data ?? createEmptyWorkbook();
582
589
  return {
583
- workbook: data ?? createEmptyWorkbook(),
590
+ workbook: recalculateWorkbook(workbook),
584
591
  selection: { activeCell: "A1", ranges: [{ start: "A1", end: "A1" }] },
585
592
  editingCell: null,
586
593
  editValue: "",
@@ -1018,18 +1025,22 @@ function CellEditor() {
1018
1025
  confirmEdit,
1019
1026
  cancelEdit,
1020
1027
  getColumnWidth,
1021
- rowHeight,
1022
- activeSheet
1028
+ rowHeight
1023
1029
  } = useSpreadsheet();
1024
1030
  const inputRef = react.useRef(null);
1031
+ const mountedAt = react.useRef(0);
1025
1032
  react.useEffect(() => {
1026
1033
  if (editingCell && inputRef.current) {
1034
+ mountedAt.current = Date.now();
1027
1035
  inputRef.current.focus();
1028
- inputRef.current.select();
1029
1036
  }
1030
1037
  }, [editingCell]);
1038
+ const handleBlur = react.useCallback(() => {
1039
+ if (Date.now() - mountedAt.current < 100) return;
1040
+ confirmEdit();
1041
+ }, [confirmEdit]);
1031
1042
  if (!editingCell) return null;
1032
- const { row, col } = parseAddress(editingCell);
1043
+ const { col } = parseAddress(editingCell);
1033
1044
  const width = getColumnWidth(col);
1034
1045
  const handleKeyDown = (e) => {
1035
1046
  if (e.key === "Enter") {
@@ -1053,7 +1064,7 @@ function CellEditor() {
1053
1064
  value: editValue,
1054
1065
  onChange: (e) => updateEdit(e.target.value),
1055
1066
  onKeyDown: handleKeyDown,
1056
- onBlur: confirmEdit
1067
+ onBlur: handleBlur
1057
1068
  }
1058
1069
  );
1059
1070
  }
@@ -1069,7 +1080,7 @@ function SelectionOverlay() {
1069
1080
  for (let c = 0; c < s.col; c++) left += getColumnWidth(c);
1070
1081
  let width = 0;
1071
1082
  for (let c = s.col; c <= e.col; c++) width += getColumnWidth(c);
1072
- const top = rowHeight + s.row * rowHeight;
1083
+ const top = s.row * rowHeight;
1073
1084
  const height = (e.row - s.row + 1) * rowHeight;
1074
1085
  return { left, top, width, height, isPrimary: i === 0 };
1075
1086
  });
@@ -1225,7 +1236,7 @@ function SpreadsheetGrid({ className }) {
1225
1236
  const { row, col } = parseAddress(editingCell);
1226
1237
  let left = 48;
1227
1238
  for (let c = 0; c < col; c++) left += getColumnWidth(c);
1228
- const top = rowHeight + row * rowHeight;
1239
+ const top = row * rowHeight;
1229
1240
  return { left, top };
1230
1241
  })() : null;
1231
1242
  return /* @__PURE__ */ jsxRuntime.jsxs(
@@ -1233,7 +1244,7 @@ function SpreadsheetGrid({ className }) {
1233
1244
  {
1234
1245
  ref: containerRef,
1235
1246
  "data-fancy-sheets-grid": "",
1236
- className: reactFancy.cn("relative flex-1 overflow-auto bg-white focus:outline-none dark:bg-zinc-900", className),
1247
+ className: reactFancy.cn("relative min-h-0 flex-1 overflow-auto bg-white focus:outline-none dark:bg-zinc-900", className),
1237
1248
  tabIndex: 0,
1238
1249
  onKeyDown: handleKeyDown,
1239
1250
  children: [
@@ -1464,13 +1475,24 @@ function SpreadsheetRoot({
1464
1475
  readOnly = false
1465
1476
  }) {
1466
1477
  const { state, actions } = useSpreadsheetStore(data ?? defaultData);
1478
+ const onChangeRef = react.useRef(onChange);
1479
+ const isExternalSync = react.useRef(false);
1480
+ const prevDataRef = react.useRef(data);
1481
+ onChangeRef.current = onChange;
1467
1482
  react.useEffect(() => {
1468
- if (data && data !== state.workbook) {
1483
+ if (data && data !== prevDataRef.current && data !== state.workbook) {
1484
+ isExternalSync.current = true;
1469
1485
  actions.setWorkbook(data);
1486
+ prevDataRef.current = data;
1470
1487
  }
1471
1488
  }, [data]);
1472
1489
  react.useEffect(() => {
1473
- onChange?.(state.workbook);
1490
+ if (isExternalSync.current) {
1491
+ isExternalSync.current = false;
1492
+ return;
1493
+ }
1494
+ prevDataRef.current = state.workbook;
1495
+ onChangeRef.current?.(state.workbook);
1474
1496
  }, [state.workbook]);
1475
1497
  const activeSheet = react.useMemo(
1476
1498
  () => state.workbook.sheets.find((s) => s.id === state.workbook.activeSheetId),
@@ -1521,7 +1543,7 @@ function SpreadsheetRoot({
1521
1543
  "div",
1522
1544
  {
1523
1545
  "data-fancy-sheets": "",
1524
- className: reactFancy.cn("flex flex-col overflow-hidden", className),
1546
+ className: reactFancy.cn("flex h-full flex-col overflow-hidden", className),
1525
1547
  children
1526
1548
  }
1527
1549
  ) });