How it is built
The two layers and the five ideas everything else rests on.
Two layers
| Layer | Import | What |
|---|---|---|
Core (src/lib/core) | @klementine/svelte-sheet/core | the sheet as records, the formulas, the files, edits from code, the editor |
Svelte (src/lib/svelte) | @klementine/svelte-sheet | the grid, and the bridge that makes the editor’s values reactive |
The core never imports Svelte. It is plain TypeScript that Node runs as it is, so the tests type into cells and press keys without a browser, and a server can open a sheet to read or change it.
The main import is the Svelte layer with the whole core in it, so an app takes everything from @klementine/svelte-sheet.
The Svelte layer is small on purpose. The grid draws what the editor holds and tells it what happened: a press, a key, text that arrived. What each of them means is decided in the core, which is why it can be tested there and done from a script.
Five ideas
1. Every cell is a record in one store
A cell is plain data: its id (cell:B3), what was typed into it (input), and how it is
shown (style). Only cells that hold something exist. A column that is not the usual width is
a record too (col:A). Every write lands in a diff, records added, updated (before and
after) and removed, and is announced with where it came from: this user, or remote.
2. Undo is a listener on that stream
The history folds this user’s diffs into steps. The editor marks the history before and after everything a person does, so one action is one step, however many cells it changed. Changes from elsewhere are never recorded: undo takes back only what you did.
3. A cell keeps what was typed; its value is worked out
=B2*1.1 stays =B2*1.1. What the cell shows is worked out from what is typed in the sheet
now: each cell once, on first asking, and again after a change. A formula that needs a cell
which needs it back shows an error; nothing loops. Numbers are kept one way (a decimal point,
no separators) and shown and typed the reader’s way.
4. The editor is one person’s work on a sheet
The Editor holds the sheet, the selection and the entry being typed, and has a method for
everything a person does on a grid: selecting, typing, the keys, formats, copy and paste, undo.
The grid calls those methods; a test or a script calls the same ones.
5. The grid draws, and reports
Grid draws only the rows and columns in view, and of those only the cells with something in them: the
lines are a background, so an empty sheet costs nothing. It turns the pointer and the keys
into calls on the editor, and keeps only what needs a screen: pixels, scrolling, the caret.
Reactivity
What changes on the editor is held in small signals: anchor, focus, editing, peers.
A signal has .get(), .peek(), .set() and .listen(). The Svelte layer hooks their reads
into Svelte, so editor.anchor.get() in a template is reactive, with no stores and no
wrappers. What the editor reads from the sheet (editor.shown(row, col), editor.empty)
follows one signal, the sheet’s version: a reader is told once per change, however many cells
it looked at.
The signals are the store’s own, from svelte-draw. Another renderer hooks into them the same
way, with setTracker.
Where things are
| File | What |
|---|---|
core/address.ts | where a cell is and what it is called |
core/formula.ts | the formula language: parser, functions, the calculation |
core/store.ts | the record store, the undo history and the signals, from svelte-draw |
core/model.ts | the records, their checks, and Sheet |
core/editor.ts | Editor: selection, entry, keys, clipboard, undo steps |
core/display.ts | number formats, and typing a number the reader’s way |
core/file.ts | the sheet file, CSV and delimited text |
core/ops.ts | edits and reads from outside the screen |
svelte/reactive.ts | the bridge from the signals to Svelte |
svelte/grid.svelte | Grid |
sheet.css | the grid’s styles |