@pritzlaff/celldeck 0.0.0-stage → 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/lib/store.js ADDED
@@ -0,0 +1,87 @@
1
+ // Deck store: CRUD over one deck (stage + views), kept as JSON under one key of a Storage
2
+ // (window.localStorage, or memoryStorage() for a deck opened from ?src= that must not overwrite local work).
3
+ // Every write is validated with parseDeck before it is stored, so storage never holds a deck the page can't load.
4
+ // Synchronous; a failed write (e.g. quota) throws and leaves the deck unchanged.
5
+ import { EMPTY_DECK, parseDeck } from './core.js';
6
+
7
+ /** A Storage-shaped Map, for decks that live only as long as the page. */
8
+ export function memoryStorage() {
9
+ const m = new Map();
10
+ return { getItem: (k) => m.get(k) ?? null, setItem: (k, v) => { m.set(k, String(v)); }, removeItem: (k) => { m.delete(k); } };
11
+ }
12
+
13
+ /**
14
+ * @param storage getItem/setItem, e.g. localStorage
15
+ * @param key where the deck lives in storage
16
+ * @param initial deck to store when the key is empty (default: EMPTY_DECK)
17
+ * Throws if the stored deck is malformed (it is left in place, not overwritten).
18
+ */
19
+ export function createDeckStore(storage, key, initial = EMPTY_DECK) {
20
+ const raw = storage.getItem(key);
21
+ let deck = raw === null ? parseDeck(structuredClone(initial)) : parseDeck(JSON.parse(raw), 'The deck saved in this browser');
22
+ if (raw === null) storage.setItem(key, JSON.stringify(deck));
23
+ const listeners = new Set();
24
+
25
+ const commit = (next) => {
26
+ const ok = parseDeck(next);
27
+ storage.setItem(key, JSON.stringify(ok));
28
+ deck = ok;
29
+ listeners.forEach((f) => f(deck));
30
+ return deck;
31
+ };
32
+ const indexOfView = (id) => {
33
+ const i = deck.views.findIndex((v) => v.id === id);
34
+ if (i < 0) throw new Error(`No view with id "${id}".`);
35
+ return i;
36
+ };
37
+ const withViews = (views) => commit({ ...deck, views });
38
+
39
+ return {
40
+ /** The whole deck: { scene, camera, views }. */
41
+ get: () => deck,
42
+ /** Replace everything (opening a file). */
43
+ replace: (next) => commit(next),
44
+ /** Autosave of the editor stage. */
45
+ saveStage: ({ scene, camera }) => commit({ ...deck, scene, camera }),
46
+
47
+ listViews: () => deck.views,
48
+ /** Find by id, else by name (case-insensitive); null if neither matches. */
49
+ findView: (idOrName) => {
50
+ const n = String(idOrName).trim().toLowerCase();
51
+ return deck.views.find((v) => v.id === idOrName) ?? deck.views.find((v) => v.name.trim().toLowerCase() === n) ?? null;
52
+ },
53
+ createView: (view) => {
54
+ if (deck.views.some((v) => v.id === view.id)) throw new Error(`A view with id "${view.id}" already exists.`);
55
+ return withViews([...deck.views, view]);
56
+ },
57
+ /** patch: any of { name, scene, camera }. */
58
+ updateView: (id, patch) => {
59
+ const i = indexOfView(id);
60
+ return withViews(deck.views.map((v, j) => (j === i ? { ...v, ...patch, id } : v)));
61
+ },
62
+ deleteView: (id) => {
63
+ indexOfView(id);
64
+ return withViews(deck.views.filter((v) => v.id !== id));
65
+ },
66
+ /** Move a view earlier (dir -1) or later (+1); a no-op at either end. */
67
+ moveView: (id, dir) => {
68
+ const i = indexOfView(id), j = i + dir;
69
+ if (j < 0 || j >= deck.views.length) return deck;
70
+ const views = [...deck.views];
71
+ [views[i], views[j]] = [views[j], views[i]];
72
+ return withViews(views);
73
+ },
74
+
75
+ /** Move a view to position `to` (0-based, clamped), shifting the others along. */
76
+ moveViewTo: (id, to) => {
77
+ const i = indexOfView(id), j = Math.max(0, Math.min(deck.views.length - 1, to));
78
+ if (i === j) return deck;
79
+ const views = [...deck.views];
80
+ views.splice(j, 0, ...views.splice(i, 1));
81
+ return withViews(views);
82
+ },
83
+
84
+ /** Called with the new deck after every write; returns an unsubscribe function. */
85
+ subscribe: (f) => { listeners.add(f); return () => listeners.delete(f); },
86
+ };
87
+ }
package/package.json CHANGED
@@ -1,6 +1,28 @@
1
1
  {
2
2
  "name": "@pritzlaff/celldeck",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.1",
4
+ "description": "Cell Deck: bubble diagrams with saved views. Static page (index.html) + lib/ (core and store); no build step. `celldeck <deck.json>` edits a deck on disk.",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "git+https://github.com/aaronp/celldeck.git"
8
+ },
9
+ "homepage": "https://aaronp.github.io/celldeck/",
10
+ "type": "module",
11
+ "bin": {
12
+ "celldeck": "bin/celldeck.mjs"
13
+ },
14
+ "files": [
15
+ "bin",
16
+ "index.html",
17
+ "lib"
18
+ ],
19
+ "scripts": {
20
+ "test": "node --test"
21
+ },
22
+ "devDependencies": {
23
+ "@sinclair/typebox": "0.32.35"
24
+ },
25
+ "publishConfig": {
26
+ "access": "public"
27
+ }
28
+ }