@particle-academy/fancy-sheets 0.7.5 → 0.8.0
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/README.md +23 -0
- package/dist/index.cjs +49 -30
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +90 -1
- package/dist/index.d.ts +90 -1
- package/dist/index.js +45 -31
- package/dist/index.js.map +1 -1
- package/docs/recipes/csv-roundtrip.md +53 -0
- package/docs/recipes/custom-functions.md +56 -0
- package/docs/recipes/external-state-sync.md +71 -0
- package/docs/recipes/headless-recalc.md +67 -0
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
# @particle-academy/fancy-sheets
|
|
2
2
|
|
|
3
|
+
[](https://particle.academy)
|
|
4
|
+
|
|
3
5
|
A full-featured spreadsheet component with formulas, formatting, selection, multi-sheet workbooks, clipboard, CSV import/export, and undo/redo. Custom engine — no third-party dependency.
|
|
4
6
|
|
|
5
7
|
## Installation
|
|
@@ -47,6 +49,26 @@ The component fills its container — wrap it in a sized element (height is requ
|
|
|
47
49
|
| Full component API (props, sub-components, `useSpreadsheet` hook, data model, keyboard shortcuts) | [docs/Spreadsheet.md](./docs/Spreadsheet.md) |
|
|
48
50
|
| 80+ built-in formula functions + custom function registration | [docs/formulas.md](./docs/formulas.md) |
|
|
49
51
|
| CSV import / export utilities | [docs/csv.md](./docs/csv.md) |
|
|
52
|
+
| **Headless recalculation** (Node / SSR / exports / tests) | [docs/recipes/headless-recalc.md](./docs/recipes/headless-recalc.md) |
|
|
53
|
+
| **External state sync + autosave** (controlled `data`, agent bridge, undo/redo) | [docs/recipes/external-state-sync.md](./docs/recipes/external-state-sync.md) |
|
|
54
|
+
| **Custom formula functions** (`registerFunction` walkthrough) | [docs/recipes/custom-functions.md](./docs/recipes/custom-functions.md) |
|
|
55
|
+
| **CSV round-trip** | [docs/recipes/csv-roundtrip.md](./docs/recipes/csv-roundtrip.md) |
|
|
56
|
+
|
|
57
|
+
### Headless formula engine (0.8+)
|
|
58
|
+
|
|
59
|
+
The engine is pure functions — no React, no DOM — so backends, schedulers,
|
|
60
|
+
exporters, and test rigs can use it directly:
|
|
61
|
+
|
|
62
|
+
```ts
|
|
63
|
+
import {
|
|
64
|
+
recalculateWorkbook, recalculateSheet, // recompute computedValues
|
|
65
|
+
lexFormula, parseFormula, evaluateAST, // low-level lex → parse → eval
|
|
66
|
+
} from "@particle-academy/fancy-sheets";
|
|
67
|
+
|
|
68
|
+
const computed = recalculateWorkbook(workbook); // new workbook, formulas evaluated
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
See [docs/recipes/headless-recalc.md](./docs/recipes/headless-recalc.md).
|
|
50
72
|
|
|
51
73
|
## Commands
|
|
52
74
|
|
|
@@ -63,6 +85,7 @@ pnpm --filter @particle-academy/fancy-sheets clean # Remove dist/
|
|
|
63
85
|
- **`useSpreadsheet` hook** — full workbook state and actions accessible from any child
|
|
64
86
|
- **80+ built-in formulas** — Math, Text, Logic, Conditional aggregates, Lookup, Date/Time, Info
|
|
65
87
|
- **Multi-sheet workbooks** — cross-sheet references via `Sheet2!A1` syntax
|
|
88
|
+
- **Headless engine (0.8+)** — `recalculateWorkbook` / `lexFormula` / `parseFormula` / `evaluateAST` exported as pure, React-free functions for SSR, exports, and Node tests
|
|
66
89
|
- **Features** — editing, navigation, selection (single, range, multi-range), formatting (bold/italic/align), clipboard (copy/cut/paste with TSV), column resize, freeze rows/cols, undo/redo (50 steps), drag-to-select
|
|
67
90
|
- **Zero third-party dependencies** — custom lexer, parser, evaluator, and dependency graph
|
|
68
91
|
|
package/dist/index.cjs
CHANGED
|
@@ -1255,7 +1255,7 @@ function getRecalculationOrder(graph) {
|
|
|
1255
1255
|
return order;
|
|
1256
1256
|
}
|
|
1257
1257
|
|
|
1258
|
-
// src/
|
|
1258
|
+
// src/engine/recalc.ts
|
|
1259
1259
|
function recalculateWorkbook(workbook) {
|
|
1260
1260
|
const recalculated = [];
|
|
1261
1261
|
for (const sheet of workbook.sheets) {
|
|
@@ -1267,33 +1267,6 @@ function recalculateWorkbook(workbook) {
|
|
|
1267
1267
|
}
|
|
1268
1268
|
return { ...workbook, sheets: finalSheets };
|
|
1269
1269
|
}
|
|
1270
|
-
function createInitialState(data) {
|
|
1271
|
-
const workbook = data ?? createEmptyWorkbook();
|
|
1272
|
-
return {
|
|
1273
|
-
workbook: recalculateWorkbook(workbook),
|
|
1274
|
-
selection: { activeCell: "A1", ranges: [{ start: "A1", end: "A1" }] },
|
|
1275
|
-
editingCell: null,
|
|
1276
|
-
editValue: "",
|
|
1277
|
-
undoStack: [],
|
|
1278
|
-
redoStack: []
|
|
1279
|
-
};
|
|
1280
|
-
}
|
|
1281
|
-
function getActiveSheet(state) {
|
|
1282
|
-
return state.workbook.sheets.find((s) => s.id === state.workbook.activeSheetId);
|
|
1283
|
-
}
|
|
1284
|
-
function updateActiveSheet(state, updater) {
|
|
1285
|
-
return {
|
|
1286
|
-
...state.workbook,
|
|
1287
|
-
sheets: state.workbook.sheets.map(
|
|
1288
|
-
(s) => s.id === state.workbook.activeSheetId ? updater(s) : s
|
|
1289
|
-
)
|
|
1290
|
-
};
|
|
1291
|
-
}
|
|
1292
|
-
function pushUndo(state) {
|
|
1293
|
-
const stack = [...state.undoStack, state.workbook];
|
|
1294
|
-
if (stack.length > 50) stack.shift();
|
|
1295
|
-
return { undoStack: stack, redoStack: [] };
|
|
1296
|
-
}
|
|
1297
1270
|
function recalculateSheet(sheet, allSheets) {
|
|
1298
1271
|
const graph = buildDependencyGraph(sheet.cells);
|
|
1299
1272
|
if (graph.size === 0) return sheet;
|
|
@@ -1350,6 +1323,35 @@ function recalculateSheet(sheet, allSheets) {
|
|
|
1350
1323
|
}
|
|
1351
1324
|
return { ...sheet, cells };
|
|
1352
1325
|
}
|
|
1326
|
+
|
|
1327
|
+
// src/hooks/use-spreadsheet-store.ts
|
|
1328
|
+
function createInitialState(data) {
|
|
1329
|
+
const workbook = data ?? createEmptyWorkbook();
|
|
1330
|
+
return {
|
|
1331
|
+
workbook: recalculateWorkbook(workbook),
|
|
1332
|
+
selection: { activeCell: "A1", ranges: [{ start: "A1", end: "A1" }] },
|
|
1333
|
+
editingCell: null,
|
|
1334
|
+
editValue: "",
|
|
1335
|
+
undoStack: [],
|
|
1336
|
+
redoStack: []
|
|
1337
|
+
};
|
|
1338
|
+
}
|
|
1339
|
+
function getActiveSheet(state) {
|
|
1340
|
+
return state.workbook.sheets.find((s) => s.id === state.workbook.activeSheetId);
|
|
1341
|
+
}
|
|
1342
|
+
function updateActiveSheet(state, updater) {
|
|
1343
|
+
return {
|
|
1344
|
+
...state.workbook,
|
|
1345
|
+
sheets: state.workbook.sheets.map(
|
|
1346
|
+
(s) => s.id === state.workbook.activeSheetId ? updater(s) : s
|
|
1347
|
+
)
|
|
1348
|
+
};
|
|
1349
|
+
}
|
|
1350
|
+
function pushUndo(state) {
|
|
1351
|
+
const stack = [...state.undoStack, state.workbook];
|
|
1352
|
+
if (stack.length > 50) stack.shift();
|
|
1353
|
+
return { undoStack: stack, redoStack: [] };
|
|
1354
|
+
}
|
|
1353
1355
|
function getCellDisplayValue(cell) {
|
|
1354
1356
|
if (!cell) return "";
|
|
1355
1357
|
if (cell.computedValue !== void 0) return String(cell.computedValue);
|
|
@@ -1561,7 +1563,7 @@ function reducer(state, action) {
|
|
|
1561
1563
|
};
|
|
1562
1564
|
}
|
|
1563
1565
|
case "SET_WORKBOOK":
|
|
1564
|
-
return { ...state, workbook: action.workbook };
|
|
1566
|
+
return { ...state, workbook: recalculateWorkbook(action.workbook) };
|
|
1565
1567
|
default:
|
|
1566
1568
|
return state;
|
|
1567
1569
|
}
|
|
@@ -2260,7 +2262,13 @@ function SpreadsheetGrid({ className }) {
|
|
|
2260
2262
|
return /* @__PURE__ */ jsxRuntime.jsxs(
|
|
2261
2263
|
"div",
|
|
2262
2264
|
{
|
|
2263
|
-
className:
|
|
2265
|
+
className: reactFancy.cn(
|
|
2266
|
+
"flex",
|
|
2267
|
+
// Frozen rows need a solid background so scrolling content
|
|
2268
|
+
// doesn't bleed through any cell-level transparency
|
|
2269
|
+
// (highlight overlays, alpha format colors, etc.).
|
|
2270
|
+
isFrozenRow && "bg-white dark:bg-zinc-900"
|
|
2271
|
+
),
|
|
2264
2272
|
style: isFrozenRow ? {
|
|
2265
2273
|
position: "sticky",
|
|
2266
2274
|
top: rowHeight + rowIdx * rowHeight,
|
|
@@ -2274,6 +2282,12 @@ function SpreadsheetGrid({ className }) {
|
|
|
2274
2282
|
return /* @__PURE__ */ jsxRuntime.jsx(
|
|
2275
2283
|
"div",
|
|
2276
2284
|
{
|
|
2285
|
+
className: reactFancy.cn(
|
|
2286
|
+
// Same solid-background guarantee for frozen
|
|
2287
|
+
// columns — keeps the column opaque while content
|
|
2288
|
+
// scrolls horizontally underneath.
|
|
2289
|
+
isFrozenCol && "bg-white dark:bg-zinc-900"
|
|
2290
|
+
),
|
|
2277
2291
|
style: isFrozenCol ? {
|
|
2278
2292
|
position: "sticky",
|
|
2279
2293
|
left: 48 + Array.from({ length: colIdx }, (_3, c) => getColumnWidth(c)).reduce((a, b) => a + b, 0),
|
|
@@ -2860,9 +2874,14 @@ exports.columnToLetter = columnToLetter;
|
|
|
2860
2874
|
exports.createEmptySheet = createEmptySheet;
|
|
2861
2875
|
exports.createEmptyWorkbook = createEmptyWorkbook;
|
|
2862
2876
|
exports.csvToWorkbook = csvToWorkbook;
|
|
2877
|
+
exports.evaluateAST = evaluateAST;
|
|
2863
2878
|
exports.letterToColumn = letterToColumn;
|
|
2879
|
+
exports.lexFormula = lexFormula;
|
|
2864
2880
|
exports.parseAddress = parseAddress;
|
|
2865
2881
|
exports.parseCSV = parseCSV;
|
|
2882
|
+
exports.parseFormula = parseFormula;
|
|
2883
|
+
exports.recalculateSheet = recalculateSheet;
|
|
2884
|
+
exports.recalculateWorkbook = recalculateWorkbook;
|
|
2866
2885
|
exports.registerFunction = registerFunction;
|
|
2867
2886
|
exports.stringifyCSV = stringifyCSV;
|
|
2868
2887
|
exports.toAddress = toAddress;
|