@blueshed/joined 0.2.0 → 0.2.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.
@@ -362,7 +362,9 @@ Working: `examples/cafe/index.html`, the till's page, holds the menu, the till's
362
362
  A write of several documents is one message, `{ id, writes: [{ doc, ops, why? }, …] }`, sent by `together(writes)`
363
363
  from `joined/page`: each part a document the page holds, or one the write makes (`board:$1`), each asked in the
364
364
  write's transaction whether its writer may hold it, and the rules a write to it is held to. It's answered as one
365
- write. A document each person sees only some of is told a write's parts on it as one change.
365
+ write. A document each person sees only some of is told a write's parts on it as one change. An undo (`undo(doc,
366
+ version)`) is one document's: a write of several lands `landed` with its first part's version, and undoing it puts
367
+ back that part alone.
366
368
 
367
369
  ## 8. Routes: pages and edges
368
370
 
package/README.md CHANGED
@@ -31,7 +31,7 @@ elements.
31
31
  hold their links and can give them again. Behind them all is a kind BDFL, who always says why.
32
32
  - **Small.** Bun, web components and one dependency (lucide, for icons), and PGlite, optional, for a
33
33
  Postgres store; the migrations are joined's own. The compiler, the server, the page's side and the store
34
- together are about 4,200 lines of code, and a change to one of 100,000 events costs at most twice one of 1,000
34
+ together are about 4,700 lines of code, and a change to one of 100,000 events costs at most twice one of 1,000
35
35
  (`bun run claims` counts the one and times the other).
36
36
 
37
37
  ## Try it
@@ -64,7 +64,8 @@ so its connector address is `…/mcp`, with no link, and Claude changes it as an
64
64
  ## Your own apps
65
65
 
66
66
  joined is a package: a project takes it as a dependency and keeps its apps in `apps/`, each a
67
- folder like those in `examples/`. Once it's published (as `@blueshed/joined`), an app begins with:
67
+ folder like those in `examples/`. It's on npm as `@blueshed/joined`; once create-joined is published too, an app
68
+ begins with:
68
69
 
69
70
  ```
70
71
  bun create joined my-app
@@ -76,8 +77,9 @@ test telling hello's stories, and the scripts: `dev`, `compile` (each app's `gen
76
77
  its stories), `start` (serves `apps/`, keeping what the pages change in `.data/`), `link`, `upgrade` and `test`.
77
78
  `bun run upgrade` moves it to the newest joined and refreshes the skill.
78
79
 
79
- Until then, try it from a tarball: `bun run pack` in joined makes `.pack/blueshed-joined-<version>.tgz`, and
80
- `JOINED_FROM=<that tarball> bun <joined>/create/index.ts my-app` makes the app on it. Never take joined's folder
80
+ Until then, `bun <joined>/create/index.ts my-app` makes it, on the newest joined from npm. To try a joined not
81
+ released yet, `bun run pack` in joined makes `.pack/blueshed-joined-<version>.tgz`, and `JOINED_FROM=<that tarball>`
82
+ makes the app on it. Never take joined's folder
81
83
  itself (`file:../joined`): Bun copies a folder dependency whole, `.data/` (the store, and the members' keys)
82
84
  included.
83
85
 
@@ -187,7 +187,7 @@ Everything an app needs lives in one folder under `examples/`:
187
187
  | `index.html` & `page.ts` | The page and its controls |
188
188
  | `routes.ts` | Exports `document` and `routes` (the page and any edges like `/trip/1/trip.ics`) |
189
189
 
190
- To read deeper, see [docs/plan.md](plan.md) for `joined` as it's aimed: its architecture, what's decided
190
+ To read deeper, `docs/plan.md` in joined's repository is `joined` as it's aimed: its architecture, what's decided
191
191
  and why, every story and seed, what's built, and the phases still to build. See [docs/design.md](design.md) for how it
192
192
  looks: the rules for its surfaces, taps and drags, colour, type, icons and words, whose values and parts
193
193
  are `joined`'s design system.
package/docs/learning.md CHANGED
@@ -1,7 +1,7 @@
1
1
  # Learning joined
2
2
 
3
3
  Why joined is hard to pick up, and what would make it easier. It isn't the size: the compiler, the
4
- server, the page's side and the store are about 4,200 lines of code, and every line is tested. It's that a
4
+ server, the page's side and the store are about 4,700 lines of code, and every line is tested. It's that a
5
5
  newcomer meets several unfamiliar things at once, with nothing familiar to hold on to.
6
6
 
7
7
  ## Why it's hard
@@ -0,0 +1,119 @@
1
+ <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 880 360" font-family="system-ui, -apple-system, sans-serif">
2
+ <defs>
3
+ <marker id="arrow-obs" viewBox="0 0 10 10" refX="8" refY="5" markerWidth="6" markerHeight="6" orient="auto-start-reverse">
4
+ <path d="M 0 1.5 L 8.5 5 L 0 8.5 z" fill="#4e79a7"/>
5
+ </marker>
6
+ <marker id="arrow-op" viewBox="0 0 10 10" refX="8" refY="5" markerWidth="6" markerHeight="6" orient="auto-start-reverse">
7
+ <path d="M 0 1.5 L 8.5 5 L 0 8.5 z" fill="#f28e2b"/>
8
+ </marker>
9
+ </defs>
10
+
11
+ <!-- Outer Host Frame: <calendar-map> -->
12
+ <rect width="880" height="360" rx="10" fill="#fafafa" stroke="#ccc"/>
13
+ <text x="20" y="25" font-family="ui-monospace, SFMono-Regular, Menlo, monospace" font-size="13" font-weight="600" fill="#222">&lt;calendar-map people="people"&gt;</text>
14
+ <text x="860" y="25" font-size="12" fill="#666" text-anchor="end">The light DOM is the document; the shadow DOM is the display.</text>
15
+
16
+ <!-- Left Panel: Light DOM (The Document) -->
17
+ <g transform="translate(18, 40)">
18
+ <rect width="324" height="300" rx="8" fill="#ffffff" stroke="#4e79a7" stroke-width="1.5"/>
19
+ <rect x="0.75" y="0.75" width="322.5" height="28" rx="7" fill="#eef3f8"/>
20
+ <text x="12" y="19" font-size="11.5" font-weight="600" fill="#2c5282">Light DOM — the document (hidden)</text>
21
+ <text x="312" y="19" font-size="10.5" fill="#4e79a7" text-anchor="end">no copy beside it</text>
22
+
23
+ <g font-family="ui-monospace, SFMono-Regular, Menlo, monospace" font-size="10.2" fill="#222">
24
+ <text x="14" y="49" fill="#2c5282" font-weight="600">&lt;j-events&gt;</text>
25
+
26
+ <!-- Event 1 -->
27
+ <rect x="20" y="58" width="290" height="44" rx="5" fill="#f8f9fa" stroke="#e5e5e5"/>
28
+ <text x="28" y="75" fill="#666">&lt;j-event <tspan fill="#222">key="1" title="Land at Haneda"</tspan></text>
29
+ <text x="28" y="92" fill="#666"> start="2026-11-03T14:30:00" …&gt;</text>
30
+
31
+ <!-- Event 2 (Highlighted / Picked) -->
32
+ <rect x="20" y="112" width="290" height="86" rx="5" fill="#fdf6ec" stroke="#f28e2b" stroke-width="1.5"/>
33
+ <text x="28" y="129" font-weight="600" fill="#222">&lt;j-event <tspan fill="#d97706">key="2"</tspan></text>
34
+ <text x="38" y="146">title=<tspan fill="#59a14f">"Dinner in Shinjuku"</tspan></text>
35
+ <text x="38" y="163">start=<tspan fill="#2c5282" font-weight="600">"2026-11-04T19:00"</tspan> people=<tspan fill="#59a14f">"1 2"</tspan></text>
36
+ <text x="38" y="180">coordinates=<tspan fill="#2c5282" font-weight="600">"geo:35.6938,139.7034"</tspan>&gt;</text>
37
+
38
+ <!-- Event 3 -->
39
+ <rect x="20" y="208" width="290" height="44" rx="5" fill="#f8f9fa" stroke="#e5e5e5"/>
40
+ <text x="28" y="225" fill="#666">&lt;j-event <tspan fill="#222">key="3" title="Shinkansen"</tspan></text>
41
+ <text x="28" y="242" fill="#666"> start="2026-11-06T09:00:00" …&gt;</text>
42
+
43
+ <text x="14" y="274" fill="#2c5282" font-weight="600">&lt;/j-events&gt;</text>
44
+ </g>
45
+ </g>
46
+
47
+ <!-- Middle Bridge: MutationObserver & el.change() -->
48
+ <g transform="translate(342, 130)">
49
+ <!-- Forward arrow: MutationObserver -->
50
+ <path d="M 10 22 L 126 22" fill="none" stroke="#4e79a7" stroke-width="1.8" marker-end="url(#arrow-obs)"/>
51
+ <text x="68" y="13" font-size="10.5" font-weight="600" fill="#2c5282" text-anchor="middle">MutationObserver</text>
52
+ <text x="68" y="37" font-size="10" fill="#666" text-anchor="middle">1 attribute → 1 move</text>
53
+
54
+ <!-- Return arrow: el.change(...) -->
55
+ <path d="M 126 78 L 10 78" fill="none" stroke="#f28e2b" stroke-width="1.8" stroke-dasharray="4 3" marker-end="url(#arrow-op)"/>
56
+ <text x="68" y="69" font-family="ui-monospace, monospace" font-size="9.8" font-weight="600" fill="#d97706" text-anchor="middle">el.change({ start })</text>
57
+ <text x="68" y="93" font-size="10" fill="#666" text-anchor="middle">Save sends 1 write</text>
58
+ </g>
59
+
60
+ <!-- Right Panel: Shadow DOM (The Display) -->
61
+ <g transform="translate(478, 40)">
62
+ <rect width="384" height="300" rx="8" fill="#ffffff" stroke="#222" stroke-width="1.5"/>
63
+ <rect x="0.75" y="0.75" width="382.5" height="28" rx="7" fill="#f3f4f6"/>
64
+ <text x="14" y="19" font-size="12" font-weight="600" fill="#222">#shadow-root — drawn twice from one element</text>
65
+
66
+ <!-- Sub-view 1: Calendar Days -->
67
+ <g transform="translate(14, 42)">
68
+ <rect width="174" height="190" rx="6" fill="#ffffff" stroke="#ddd"/>
69
+ <line x1="58" y1="0" x2="58" y2="190" stroke="#eee"/>
70
+ <line x1="116" y1="0" x2="116" y2="190" stroke="#eee"/>
71
+ <line x1="0" y1="24" x2="174" y2="24" stroke="#eee"/>
72
+ <text x="29" y="16" font-size="10.5" font-weight="600" fill="#444" text-anchor="middle">Tue 3</text>
73
+ <text x="87" y="16" font-size="10.5" font-weight="600" fill="#222" text-anchor="middle">Wed 4</text>
74
+ <text x="145" y="16" font-size="10.5" font-weight="600" fill="#444" text-anchor="middle">Thu 5</text>
75
+
76
+ <!-- Hour guide lines -->
77
+ <line x1="0" y1="68" x2="174" y2="68" stroke="#f3f3f3"/>
78
+ <line x1="0" y1="112" x2="174" y2="112" stroke="#f3f3f3"/>
79
+ <line x1="0" y1="156" x2="174" y2="156" stroke="#f3f3f3"/>
80
+
81
+ <!-- Event 1 block -->
82
+ <rect x="4" y="92" width="50" height="32" rx="3" fill="#e8eff7" stroke="#4e79a7" stroke-width="1"/>
83
+ <text x="9" y="105" font-size="8.5" fill="#4e79a7" font-weight="600">14:30</text>
84
+ <text x="9" y="117" font-size="8.5" fill="#222">Land</text>
85
+
86
+ <!-- Event 2 block (Dinner, picked!) -->
87
+ <rect x="62" y="128" width="50" height="44" rx="3" fill="#fdebd6" stroke="#222" stroke-width="1.5"/>
88
+ <line x1="63" y1="128" x2="63" y2="172" stroke="#e15759" stroke-width="3"/>
89
+ <text x="69" y="140" font-size="8.5" font-weight="600" fill="#d97706">19:00</text>
90
+ <text x="69" y="152" font-size="8.5" font-weight="600" fill="#222">Dinner</text>
91
+ <circle cx="73" cy="163" r="3" fill="#f28e2b"/>
92
+ <circle cx="82" cy="163" r="3" fill="#e15759"/>
93
+ </g>
94
+
95
+ <!-- Sub-view 2: Map Tiles + Pins -->
96
+ <g transform="translate(198, 42)">
97
+ <rect width="172" height="190" rx="6" fill="#e8eef2" stroke="#ddd"/>
98
+ <!-- Stylised coastline / map grid -->
99
+ <path d="M 0 140 Q 55 120 95 85 T 172 45 L 172 190 L 0 190 Z" fill="#dbe6ec"/>
100
+ <path d="M 20 0 L 20 190 M 85 0 L 85 190 M 150 0 L 150 190 M 0 60 L 172 60 M 0 125 L 172 125" stroke="#dde5ea" stroke-width="1"/>
101
+
102
+ <!-- Pin 1 -->
103
+ <circle cx="122" cy="138" r="6" fill="#4e79a7" stroke="#fff" stroke-width="2"/>
104
+
105
+ <!-- Pin 2 (Dinner, picked!) -->
106
+ <circle cx="96" cy="88" r="8" fill="#e15759" stroke="#222" stroke-width="2"/>
107
+ <circle cx="96" cy="88" r="6" fill="none" stroke="#fff" stroke-width="1.5"/>
108
+ <rect x="40" y="56" width="112" height="22" rx="4" fill="#222"/>
109
+ <text x="96" y="71" font-size="9.5" font-weight="600" fill="#fff" text-anchor="middle">Dinner in Shinjuku</text>
110
+ </g>
111
+
112
+ <!-- Connecting callout lines inside shadow root showing both come from key="2" -->
113
+ <g transform="translate(14, 244)">
114
+ <rect width="356" height="44" rx="6" fill="#f8f9fa" stroke="#ddd"/>
115
+ <text x="12" y="18" font-size="11" font-weight="600" fill="#222">Editing “Dinner in Shinjuku”</text>
116
+ <text x="12" y="34" font-size="10.2" fill="#555">One element (<tspan font-family="ui-monospace, monospace" fill="#222">key="2"</tspan>) → one calendar block, one map pin, one form.</text>
117
+ </g>
118
+ </g>
119
+ </svg>
@@ -0,0 +1,93 @@
1
+ <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 880 320" font-family="system-ui, -apple-system, sans-serif">
2
+ <defs>
3
+ <marker id="l-arrow" viewBox="0 0 10 10" refX="8" refY="5" markerWidth="6" markerHeight="6" orient="auto-start-reverse">
4
+ <path d="M 0 1.5 L 8.5 5 L 0 8.5 z" fill="#e15759"/>
5
+ </marker>
6
+ <marker id="l-arrow-blue" viewBox="0 0 10 10" refX="8" refY="5" markerWidth="6" markerHeight="6" orient="auto-start-reverse">
7
+ <path d="M 0 1.5 L 8.5 5 L 0 8.5 z" fill="#4e79a7"/>
8
+ </marker>
9
+ </defs>
10
+
11
+ <rect width="880" height="320" rx="10" fill="#fafafa" stroke="#e5e5e5"/>
12
+
13
+ <!-- Left Half: Cross-Document Links via HTML IDREFS -->
14
+ <g transform="translate(20, 20)">
15
+ <rect width="410" height="280" rx="8" fill="#ffffff" stroke="#ccc"/>
16
+ <rect x="0.75" y="0.75" width="408.5" height="28" rx="7" fill="#f3f4f6"/>
17
+ <text x="14" y="19" font-size="12" font-weight="600" fill="#222">Links between documents: HTML IDREFS &amp; CSS</text>
18
+
19
+ <!-- Document 1: trip:1 -->
20
+ <g transform="translate(14, 42)">
21
+ <rect width="382" height="84" rx="6" fill="#f8f9fa" stroke="#ddd"/>
22
+ <text x="12" y="18" font-size="11" font-weight="600" fill="#4e79a7">Document: trip:1 (&lt;j-events&gt;)</text>
23
+ <text x="12" y="39" font-family="ui-monospace, monospace" font-size="9.8" fill="#222">&lt;j-event key="1" title="Land at Haneda" <tspan fill="#e15759" font-weight="600">people="1"</tspan>&gt;</text>
24
+ <text x="12" y="57" font-family="ui-monospace, monospace" font-size="9.8" fill="#222">&lt;j-event key="2" title="Dinner in Shinjuku" <tspan fill="#e15759" font-weight="600">people="1 2"</tspan>&gt;</text>
25
+ <text x="12" y="74" font-size="10.2" fill="#666">Field name = target collection name ("people")</text>
26
+ </g>
27
+
28
+ <!-- CSS Selector Query Bridge -->
29
+ <path d="M 135 168 L 135 130" fill="none" stroke="#e15759" stroke-width="1.6" marker-end="url(#l-arrow)"/>
30
+ <text x="147" y="153" font-family="ui-monospace, monospace" font-size="10.5" font-weight="600" fill="#e15759">querySelectorAll('[people~="2"]')</text>
31
+
32
+ <!-- Document 2: people -->
33
+ <g transform="translate(14, 172)">
34
+ <rect width="382" height="94" rx="6" fill="#fdf6ec" stroke="#f28e2b"/>
35
+ <text x="12" y="18" font-size="11" font-weight="600" fill="#d97706">Document: people (&lt;j-people&gt; in its popover)</text>
36
+ <text x="12" y="38" font-family="ui-monospace, monospace" font-size="10.5" fill="#222">&lt;j-person <tspan fill="#e15759" font-weight="600">key="2"</tspan> name="Kenji"&gt;</text>
37
+ <rect x="22" y="47" width="142" height="22" rx="4" fill="#ffffff" stroke="#222"/>
38
+ <text x="93" y="62" font-size="10.2" font-weight="600" fill="#222" text-anchor="middle">Dinner in Shinjuku ↗</text>
39
+ <text x="174" y="62" font-size="10.2" fill="#555">click dispatches <tspan font-family="ui-monospace, monospace" fill="#222">"pick"</tspan></text>
40
+ <text x="12" y="84" font-size="10.2" fill="#666">Server refuses removing Kenji while 1 event in trip links to him.</text>
41
+ </g>
42
+ </g>
43
+
44
+ <!-- Right Half: The Trust Network (src/members/) -->
45
+ <g transform="translate(450, 20)">
46
+ <rect width="410" height="280" rx="8" fill="#ffffff" stroke="#ccc"/>
47
+ <rect x="0.75" y="0.75" width="408.5" height="28" rx="7" fill="#f3f4f6"/>
48
+ <text x="14" y="19" font-size="12" font-weight="600" fill="#222">Sign-in by link: the trust tree (&lt;j-members&gt;)</text>
49
+
50
+ <!-- Tree Nodes -->
51
+ <!-- Root: Peter -->
52
+ <g transform="translate(20, 46)">
53
+ <rect width="246" height="34" rx="6" fill="#222"/>
54
+ <text x="12" y="21" font-size="12" font-weight="600" fill="#fff">Peter</text>
55
+ <rect x="52" y="8" width="38" height="18" rx="4" fill="#4e79a7"/>
56
+ <text x="71" y="20" font-size="9.5" font-weight="600" fill="#fff" text-anchor="middle">BDFL</text>
57
+ <text x="234" y="21" font-family="ui-monospace, monospace" font-size="10" fill="#ccc" text-anchor="end">key="1" (ROOT_TOKEN)</text>
58
+ </g>
59
+
60
+ <!-- Branch to Nick -->
61
+ <path d="M 34 80 L 34 108 L 50 108" fill="none" stroke="#888" stroke-width="1.5"/>
62
+ <g transform="translate(50, 91)">
63
+ <rect width="232" height="34" rx="6" fill="#f8f9fa" stroke="#ccc"/>
64
+ <text x="12" y="21" font-size="12" font-weight="600" fill="#222">Nick</text>
65
+ <text x="220" y="21" font-family="ui-monospace, monospace" font-size="10" fill="#666" text-anchor="end">key="2" inviter="1"</text>
66
+ </g>
67
+
68
+ <!-- Branch to Nora -->
69
+ <path d="M 64 125 L 64 153 L 80 153" fill="none" stroke="#888" stroke-width="1.5"/>
70
+ <g transform="translate(80, 136)">
71
+ <rect width="232" height="34" rx="6" fill="#f8f9fa" stroke="#ccc"/>
72
+ <text x="12" y="21" font-size="12" font-weight="600" fill="#222">Nora</text>
73
+ <text x="220" y="21" font-family="ui-monospace, monospace" font-size="10" fill="#666" text-anchor="end">key="3" inviter="2"</text>
74
+ </g>
75
+
76
+ <!-- Branch to Dev -->
77
+ <path d="M 94 170 L 94 198 L 110 198" fill="none" stroke="#888" stroke-width="1.5"/>
78
+ <g transform="translate(110, 181)">
79
+ <rect width="232" height="34" rx="6" fill="#f8f9fa" stroke="#ccc"/>
80
+ <text x="12" y="21" font-size="12" font-weight="600" fill="#222">Dev</text>
81
+ <text x="220" y="21" font-family="ui-monospace, monospace" font-size="10" fill="#666" text-anchor="end">key="4" inviter="3"</text>
82
+ </g>
83
+
84
+ <!-- Reparenting curved arrow (if Nora is removed, Dev moves up to Nick) -->
85
+ <path d="M 342 198 C 390 198, 390 108, 288 108" fill="none" stroke="#4e79a7" stroke-width="1.5" stroke-dasharray="3 2" marker-end="url(#l-arrow-blue)"/>
86
+
87
+ <!-- Rules summary at bottom -->
88
+ <g transform="translate(18, 228)" font-size="10.5" fill="#555">
89
+ <text x="0" y="14">• <tspan font-weight="600" fill="#222">Friends help friends:</tspan> your inviter holds your link to send again or replace.</text>
90
+ <text x="0" y="32">• <tspan font-weight="600" fill="#222">Remove with a reason:</tspan> if Nick removes Nora, Dev moves up to Nick.</text>
91
+ </g>
92
+ </g>
93
+ </svg>
package/docs/logo.svg ADDED
@@ -0,0 +1,47 @@
1
+ <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 180 180">
2
+ <!-- Warm amber right half -->
3
+ <rect x="6" y="6" width="168" height="168" fill="#e07a24"/>
4
+
5
+ <!-- Slate-blue left half (head, ear, trunk, and left block) -->
6
+ <path d="
7
+ M 6 6
8
+ H 90
9
+ V 20
10
+ A 56 56 0 0 1 146 76
11
+ V 134
12
+ A 21 21 0 0 1 104 134
13
+ H 124
14
+ V 128
15
+ A 28 28 0 0 0 68 128
16
+ V 174
17
+ H 6
18
+ Z
19
+ " fill="#46719e"/>
20
+
21
+ <!-- Top vertical join tick -->
22
+ <line x1="90" y1="6" x2="90" y2="20" stroke="#080808" stroke-width="10"/>
23
+
24
+ <!-- Continuous ear -> forehead -> outer trunk -> trunk tip -->
25
+ <path d="
26
+ M 68 100
27
+ A 40 40 0 0 1 68 20
28
+ H 90
29
+ A 56 56 0 0 1 146 76
30
+ V 134
31
+ A 21 21 0 0 1 104 134
32
+ " fill="none" stroke="#080808" stroke-width="10" stroke-linecap="butt"/>
33
+
34
+ <!-- Inner throat arch -> down into trunk curl + bottom vertical join -->
35
+ <path d="
36
+ M 68 174
37
+ V 128
38
+ A 28 28 0 0 1 124 128
39
+ V 135
40
+ " fill="none" stroke="#080808" stroke-width="10" stroke-linecap="round"/>
41
+
42
+ <!-- Outer square frame -->
43
+ <rect x="6" y="6" width="168" height="168" fill="none" stroke="#080808" stroke-width="12"/>
44
+
45
+ <!-- White dot eye -->
46
+ <circle cx="110" cy="65" r="5" fill="#fff"/>
47
+ </svg>
@@ -0,0 +1,86 @@
1
+ <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 880 390" font-family="system-ui, -apple-system, sans-serif">
2
+ <defs>
3
+ <marker id="arrow" viewBox="0 0 10 10" refX="8" refY="5" markerWidth="6" markerHeight="6" orient="auto-start-reverse">
4
+ <path d="M 0 1.5 L 8.5 5 L 0 8.5 z" fill="#666"/>
5
+ </marker>
6
+ <marker id="arrow-blue" viewBox="0 0 10 10" refX="8" refY="5" markerWidth="6" markerHeight="6" orient="auto-start-reverse">
7
+ <path d="M 0 1.5 L 8.5 5 L 0 8.5 z" fill="#4e79a7"/>
8
+ </marker>
9
+ </defs>
10
+
11
+ <!-- Background -->
12
+ <rect width="880" height="390" rx="10" fill="#fafafa" stroke="#e5e5e5"/>
13
+
14
+ <!-- 1. Top Box: stories.jsonl -->
15
+ <g transform="translate(30, 22)">
16
+ <rect width="820" height="104" rx="8" fill="#ffffff" stroke="#ccc"/>
17
+ <rect width="820" height="28" rx="8" fill="#f3f4f6"/>
18
+ <path d="M 0 22 H 820" stroke="#e5e5e5" fill="none"/>
19
+ <circle cx="16" cy="14" r="4.5" fill="#e15759"/>
20
+ <circle cx="30" cy="14" r="4.5" fill="#edc948"/>
21
+ <circle cx="44" cy="14" r="4.5" fill="#59a14f"/>
22
+ <text x="62" y="18" font-size="12" font-weight="600" fill="#222">examples/trip/stories.jsonl</text>
23
+ <text x="804" y="18" font-size="11" fill="#666" text-anchor="end">the single source of truth — schema, migrations &amp; tests</text>
24
+
25
+ <g font-family="ui-monospace, SFMono-Regular, Menlo, monospace" font-size="9.2" fill="#222">
26
+ <text x="16" y="48">
27
+ <tspan fill="#666">{"story":</tspan><tspan fill="#59a14f">"Add dinner"</tspan><tspan fill="#666">,"as":"peter","does":{"doc":</tspan><tspan fill="#4e79a7" font-weight="600">"trip:1"</tspan><tspan fill="#666">,"ops":[{"op":</tspan><tspan fill="#f28e2b" font-weight="600">"add"</tspan><tspan fill="#666">,"path":</tspan><tspan fill="#4e79a7" font-weight="600">"/events/-"</tspan><tspan fill="#666">,"value":{"people":[1,2],…}}]}}</tspan>
28
+ </text>
29
+ <text x="16" y="70">
30
+ <tspan fill="#666">{"story":</tspan><tspan fill="#59a14f">"Move train"</tspan><tspan fill="#666">,"as":"phone","does":{"doc":</tspan><tspan fill="#4e79a7" font-weight="600">"trip:1"</tspan><tspan fill="#666">,"ops":[{"op":</tspan><tspan fill="#f28e2b" font-weight="600">"replace"</tspan><tspan fill="#666">,"path":</tspan><tspan fill="#4e79a7" font-weight="600">"/events/3/start"</tspan><tspan fill="#666">,"value":"2026-11-06T10:00"}]}}</tspan>
31
+ </text>
32
+ <text x="16" y="92">
33
+ <tspan fill="#666">{"story":</tspan><tspan fill="#59a14f">"Clear place"</tspan><tspan fill="#666">,"as":"peter","does":{"doc":</tspan><tspan fill="#4e79a7" font-weight="600">"trip:1"</tspan><tspan fill="#666">,"ops":[{"op":</tspan><tspan fill="#e15759" font-weight="600">"remove"</tspan><tspan fill="#666">,"path":</tspan><tspan fill="#4e79a7" font-weight="600">"/events/2/location"</tspan><tspan fill="#666">}]}}</tspan>
34
+ </text>
35
+ </g>
36
+ </g>
37
+
38
+ <!-- Arrow down to compiler -->
39
+ <line x1="440" y1="126" x2="440" y2="156" stroke="#666" stroke-width="1.5" marker-end="url(#arrow)"/>
40
+ <text x="450" y="146" font-size="11" fill="#666">reduce stories → model</text>
41
+
42
+ <!-- 2. Middle Box: src/compile.ts -->
43
+ <g transform="translate(190, 160)">
44
+ <rect width="500" height="58" rx="8" fill="#222" stroke="#222"/>
45
+ <text x="250" y="23" font-size="13" font-weight="600" fill="#fff" text-anchor="middle">src/compile.ts — modelOf(stories)</text>
46
+ <text x="250" y="43" font-family="ui-monospace, SFMono-Regular, Menlo, monospace" font-size="10.5" fill="#ddd" text-anchor="middle">
47
+ events → &lt;j-events&gt;&lt;j-event&gt; · tables · required · replace · clear · links
48
+ </text>
49
+ </g>
50
+
51
+ <!-- Split arrows left and right -->
52
+ <path d="M 320 218 L 320 236 L 220 236 L 220 254" fill="none" stroke="#4e79a7" stroke-width="1.5" marker-end="url(#arrow-blue)"/>
53
+ <path d="M 560 218 L 560 236 L 660 236 L 660 254" fill="none" stroke="#4e79a7" stroke-width="1.5" marker-end="url(#arrow-blue)"/>
54
+ <text x="260" y="231" font-size="11" fill="#4e79a7" text-anchor="end">for the server &amp; store</text>
55
+ <text x="620" y="231" font-size="11" fill="#4e79a7">for the page</text>
56
+
57
+ <!-- 3A. Left Box: generated/rules.ts & migrations/ -->
58
+ <g transform="translate(30, 258)">
59
+ <rect width="395" height="112" rx="8" fill="#ffffff" stroke="#4e79a7" stroke-width="1.5"/>
60
+ <rect x="0.75" y="0.75" width="393.5" height="26" rx="7" fill="#eef3f8"/>
61
+ <text x="14" y="18" font-size="12" font-weight="600" fill="#2c5282">generated/rules.ts &amp; migrations/</text>
62
+ <text x="382" y="18" font-size="11" fill="#4e79a7" text-anchor="end">guards &amp; SQLite tables</text>
63
+
64
+ <g font-family="ui-monospace, SFMono-Regular, Menlo, monospace" font-size="10.5" fill="#222">
65
+ <text x="14" y="47"><tspan fill="#666">export function</tspan> <tspan fill="#2c5282" font-weight="600">check</tspan>(o: Op, has: Has): string | null</text>
66
+ <text x="14" y="66" font-family="system-ui, sans-serif" font-size="10.5" fill="#555">• Refuses any collection, field, or op no story did</text>
67
+ <text x="14" y="84" font-family="system-ui, sans-serif" font-size="10.5" fill="#555">• Checks every id in <tspan font-family="ui-monospace, monospace" fill="#222">people: [1, 2]</tspan> exists in <tspan font-family="ui-monospace, monospace" fill="#222">people</tspan></text>
68
+ <text x="14" y="102" font-family="system-ui, sans-serif" font-size="10.5" fill="#555">• Writes SQLite migrations (<tspan font-family="ui-monospace, monospace" fill="#222">events</tspan>, <tspan font-family="ui-monospace, monospace" fill="#222">events_people</tspan>)</text>
69
+ </g>
70
+ </g>
71
+
72
+ <!-- 3B. Right Box: generated/elements.ts -->
73
+ <g transform="translate(455, 258)">
74
+ <rect width="395" height="112" rx="8" fill="#ffffff" stroke="#4e79a7" stroke-width="1.5"/>
75
+ <rect x="0.75" y="0.75" width="393.5" height="26" rx="7" fill="#eef3f8"/>
76
+ <text x="14" y="18" font-size="12" font-weight="600" fill="#2c5282">generated/elements.ts</text>
77
+ <text x="382" y="18" font-size="11" fill="#4e79a7" text-anchor="end">web components in the DOM</text>
78
+
79
+ <g font-family="ui-monospace, SFMono-Regular, Menlo, monospace" font-size="10.5" fill="#222">
80
+ <text x="14" y="47"><tspan fill="#2c5282" font-weight="600">&lt;j-events&gt;</tspan> → <tspan fill="#666">hold("trip:1?week=…"), items, add(...events)</tspan></text>
81
+ <text x="14" y="67"><tspan fill="#2c5282" font-weight="600">&lt;j-event&gt;</tspan> → <tspan fill="#666">key, data (cached &amp; frozen)</tspan></text>
82
+ <text x="14" y="86" font-family="system-ui, sans-serif" font-size="10.5" fill="#555">• Only the ops stories do: <tspan font-family="ui-monospace, monospace" fill="#222">change({…}), clear(…), drop()</tspan></text>
83
+ <text x="14" y="103" font-family="system-ui, sans-serif" font-size="10.5" fill="#555">• No story removes a title → <tspan font-family="ui-monospace, monospace" fill="#222">clear("title")</tspan> does not exist</text>
84
+ </g>
85
+ </g>
86
+ </svg>
@@ -0,0 +1,99 @@
1
+ <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 880 310" font-family="system-ui, -apple-system, sans-serif">
2
+ <defs>
3
+ <marker id="w-arrow" viewBox="0 0 10 10" refX="8" refY="5" markerWidth="6" markerHeight="6" orient="auto-start-reverse">
4
+ <path d="M 0 1.5 L 8.5 5 L 0 8.5 z" fill="#4e79a7"/>
5
+ </marker>
6
+ <marker id="w-arrow-green" viewBox="0 0 10 10" refX="8" refY="5" markerWidth="6" markerHeight="6" orient="auto-start-reverse">
7
+ <path d="M 0 1.5 L 8.5 5 L 0 8.5 z" fill="#59a14f"/>
8
+ </marker>
9
+ </defs>
10
+
11
+ <rect width="880" height="310" rx="10" fill="#fafafa" stroke="#e5e5e5"/>
12
+
13
+ <!-- Column 1: Peter's Laptop -->
14
+ <g transform="translate(16, 24)">
15
+ <rect width="214" height="262" rx="8" fill="#ffffff" stroke="#ccc"/>
16
+ <rect x="0.75" y="0.75" width="212.5" height="28" rx="7" fill="#f3f4f6"/>
17
+ <text x="12" y="19" font-size="11.5" font-weight="600" fill="#222">1. Peter’s laptop</text>
18
+ <text x="202" y="19" font-size="10.5" fill="#666" text-anchor="end">one /ws per tab</text>
19
+
20
+ <text x="12" y="50" font-size="11" fill="#555">Drags the shinkansen to 10:00</text>
21
+ <text x="12" y="66" font-size="11" fill="#555">and presses <tspan font-weight="600" fill="#222">Save</tspan>:</text>
22
+
23
+ <rect x="12" y="78" width="190" height="52" rx="5" fill="#eef3f8" stroke="#4e79a7"/>
24
+ <text x="20" y="94" font-family="ui-monospace, monospace" font-size="10" fill="#2c5282" font-weight="600">el.change({</text>
25
+ <text x="20" y="109" font-family="ui-monospace, monospace" font-size="9.5" fill="#222"> start: "2026-11-06T10:00"</text>
26
+ <text x="20" y="123" font-family="ui-monospace, monospace" font-size="10" fill="#2c5282" font-weight="600">})</text>
27
+
28
+ <line x1="12" y1="146" x2="202" y2="146" stroke="#eee"/>
29
+
30
+ <text x="12" y="168" font-size="11" font-weight="600" fill="#59a14f">4a. On broadcast:</text>
31
+ <text x="12" y="187" font-family="ui-monospace, monospace" font-size="10" fill="#222">items.get("3")</text>
32
+ <text x="12" y="203" font-family="ui-monospace, monospace" font-size="10" fill="#222"> .setAttribute("start", …)</text>
33
+ <text x="12" y="226" font-size="10.5" fill="#555">Only <tspan font-family="ui-monospace, monospace" fill="#222">key="3"</tspan> moves; Save resets.</text>
34
+ <text x="12" y="246" font-family="ui-monospace, monospace" font-size="10" fill="#666">body[data-trip_1_week_…="2"]</text>
35
+ </g>
36
+
37
+ <!-- Arrow 1 -> 2 (Send Op) -->
38
+ <path d="M 236 96 L 342 96" fill="none" stroke="#4e79a7" stroke-width="1.8" marker-end="url(#w-arrow)"/>
39
+ <text x="289" y="75" font-family="ui-monospace, monospace" font-size="9.5" fill="#2c5282" text-anchor="middle">replace</text>
40
+ <text x="289" y="88" font-family="ui-monospace, monospace" font-size="9" fill="#4e79a7" text-anchor="middle">/events/3/start</text>
41
+
42
+ <!-- Arrow 2 -> 1 (Broadcast back) -->
43
+ <path d="M 342 188 L 236 188" fill="none" stroke="#59a14f" stroke-width="1.8" marker-end="url(#w-arrow-green)"/>
44
+ <text x="289" y="179" font-family="ui-monospace, monospace" font-size="9" fill="#59a14f" text-anchor="middle">{ version: 2, ops }</text>
45
+
46
+ <!-- Column 2: The Server -->
47
+ <g transform="translate(348, 24)">
48
+ <rect width="218" height="262" rx="8" fill="#ffffff" stroke="#222" stroke-width="1.5"/>
49
+ <rect x="0.75" y="0.75" width="216.5" height="28" rx="7" fill="#222"/>
50
+ <text x="109" y="19" font-size="11.5" font-weight="600" fill="#fff" text-anchor="middle">2. src/server.ts &amp; store.ts</text>
51
+
52
+ <g transform="translate(12, 44)" font-size="11" fill="#222">
53
+ <rect width="194" height="36" rx="5" fill="#f8f9fa" stroke="#e5e5e5"/>
54
+ <text x="10" y="15" font-weight="600">1. rules.check(op, has)</text>
55
+ <text x="10" y="29" font-size="10" fill="#666">Allowed by stories? Links valid?</text>
56
+
57
+ <rect y="44" width="194" height="36" rx="5" fill="#f8f9fa" stroke="#e5e5e5"/>
58
+ <text x="10" y="59" font-weight="600">2. group.mayWrite(doc, ops, …)</text>
59
+ <text x="10" y="73" font-size="10" fill="#666">Signed in? Not read-only? Trust ok?</text>
60
+
61
+ <rect y="88" width="194" height="36" rx="5" fill="#f8f9fa" stroke="#e5e5e5"/>
62
+ <text x="10" y="103" font-weight="600">3. store.write(doc, ops, …)</text>
63
+ <text x="10" y="117" font-size="10" fill="#666">1 SQLite transaction + changes row</text>
64
+
65
+ <rect y="132" width="194" height="44" rx="5" fill="#eef7ec" stroke="#59a14f"/>
66
+ <text x="10" y="149" font-weight="600" fill="#2b6e22">4. told(change) — pub/sub</text>
67
+ <text x="10" y="166" font-family="ui-monospace, monospace" font-size="9.2" fill="#222">{ doc, version: 2, ops, by: 1 }</text>
68
+ </g>
69
+
70
+ <text x="109" y="246" font-size="10" fill="#a33" text-anchor="middle">Refused? Tells sender "refused" + why</text>
71
+ </g>
72
+
73
+ <!-- Arrow 2 -> 3 (Broadcast to phone) -->
74
+ <path d="M 572 188 L 644 188" fill="none" stroke="#59a14f" stroke-width="1.8" marker-end="url(#w-arrow-green)"/>
75
+ <text x="608" y="179" font-family="ui-monospace, monospace" font-size="9.5" fill="#59a14f" text-anchor="middle">~25 ms</text>
76
+
77
+ <!-- Column 3: Peter's Phone -->
78
+ <g transform="translate(650, 24)">
79
+ <rect width="214" height="262" rx="8" fill="#ffffff" stroke="#ccc"/>
80
+ <rect x="0.75" y="0.75" width="212.5" height="28" rx="7" fill="#f3f4f6"/>
81
+ <text x="12" y="19" font-size="11.5" font-weight="600" fill="#222">3. Peter’s phone</text>
82
+ <text x="202" y="19" font-size="10.5" fill="#666" text-anchor="end">the same week</text>
83
+
84
+ <text x="12" y="52" font-size="11" fill="#555">Hears the same op on its socket:</text>
85
+
86
+ <rect x="12" y="64" width="190" height="58" rx="5" fill="#f8f9fa" stroke="#ddd"/>
87
+ <text x="20" y="82" font-family="ui-monospace, monospace" font-size="9.8" fill="#666">&lt;j-event <tspan fill="#222">key="3"</tspan></text>
88
+ <text x="20" y="98" font-family="ui-monospace, monospace" font-size="9.8" fill="#59a14f" font-weight="600"> start="2026-11-06T10:00"</text>
89
+ <text x="20" y="113" font-family="ui-monospace, monospace" font-size="9.8" fill="#666"> title="Shinkansen to Kyoto"&gt;</text>
90
+
91
+ <line x1="12" y1="146" x2="202" y2="146" stroke="#eee"/>
92
+
93
+ <text x="12" y="168" font-size="11" font-weight="600" fill="#59a14f">4b. Surgical update:</text>
94
+ <text x="12" y="188" font-size="10.2" fill="#555">• <tspan font-family="ui-monospace, monospace" fill="#222">MutationObserver</tspan> sees <tspan font-family="ui-monospace, monospace" fill="#222">key="3"</tspan></text>
95
+ <text x="12" y="207" font-size="10.5" fill="#555">• Moves only the Shinkansen block</text>
96
+ <text x="12" y="226" font-size="10.5" fill="#555">• Every other node stays untouched</text>
97
+ <text x="12" y="246" font-family="ui-monospace, monospace" font-size="10" fill="#666">body[data-trip_1_week_…="2"]</text>
98
+ </g>
99
+ </svg>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blueshed/joined",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
4
4
  "description": "Compiled documents, talking to web components as elements in the DOM.",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -52,7 +52,8 @@
52
52
  "docs/introduction.md",
53
53
  "docs/stories.md",
54
54
  "docs/design.md",
55
- "docs/learning.md"
55
+ "docs/learning.md",
56
+ "docs/*.svg"
56
57
  ],
57
58
  "dependencies": {
58
59
  "lucide": "^1.62.0"
package/src/attachment.ts CHANGED
@@ -1,8 +1,9 @@
1
1
  // A file field on the page (Attachment, joined/attachment): what an item holds in it shown, a photo as its picture and
2
- // a PDF as a link, each opening the file; and, where its person may, a way to choose one, and to take it off. A file
3
- // chosen is sent to the server for the item's document (/files, src/files.ts), which keeps it and answers its value;
4
- // that's put in the field by the view's own op (`put`: the item's replace, or an add form's field), never written to
5
- // the element. Drawn again as the item is, only what changed moves: the picture stays while its file does.
2
+ // a PDF or a Markdown file as a link, each opening the file; and, where its person may, a way to choose one, and to
3
+ // take it off. A file chosen is sent to the server for the item's document (/files, src/files.ts), which keeps it and
4
+ // answers its value; that's put in the field by the view's own op (`put`: the item's replace, or an add form's field),
5
+ // never written to the element. Drawn again as the item is, only what changed moves: the picture stays while its file
6
+ // does.
6
7
 
7
8
  import { fileUrl, MOST, TOO_BIG } from "./file-value.ts";
8
9
  import { icon, iconButton } from "./icons.ts";
@@ -60,11 +61,12 @@ export function attachment(doc: () => string, put: (file: string) => Promise<Ans
60
61
  draw(value: string | undefined, may: { put?: boolean; take?: boolean } = {}) {
61
62
  const url = value && fileUrl(value);
62
63
  if (url && shown.getAttribute("href") !== url) {
63
- const pdf = url.endsWith(".pdf");
64
- Object.assign(shown, { href: url, className: pdf ? "act small" : "j-file-picture" });
65
- shown.ariaLabel = pdf ? "Open the PDF" : "Open the picture";
66
- if (!pdf) picture.src = url;
67
- shown.replaceChildren(...(pdf ? [icon("download"), "PDF"] : [picture]));
64
+ // a PDF or a Markdown file, a link to it, named; a photo or an SVG, its picture
65
+ const linked = url.endsWith(".pdf") ? "PDF" : url.endsWith(".md") ? "Markdown" : undefined;
66
+ Object.assign(shown, { href: url, className: linked ? "act small" : "j-file-picture" });
67
+ shown.ariaLabel = linked ? `Open the ${linked}` : "Open the picture";
68
+ if (!linked) picture.src = url;
69
+ shown.replaceChildren(...(linked ? [icon("download"), linked] : [picture]));
68
70
  }
69
71
  shown.hidden = !url;
70
72
  words.textContent = value ? "Another file" : "Add a file";
package/src/client.ts CHANGED
@@ -26,6 +26,13 @@ export function applied(doc: Record<string, any>, o: Op): Record<string, any> {
26
26
  [coll]: items.flatMap((i) => (String(i.id) === key ? (f(i) ?? []) : [i])),
27
27
  });
28
28
  if (field === undefined) return o.op === "remove" ? changed(() => null) : changed((i) => ({ ...i, ...o.value }));
29
+ // one of an object's fields (/orders/1/terms/days): made where there's none; the object gone once it's empty
30
+ if (link !== undefined && !/^(\d+|-)$/.test(link))
31
+ return changed((i) => {
32
+ const { [link]: _, ...rest } = i[field] ?? {};
33
+ const now = o.op === "remove" ? rest : { ...rest, [link]: o.value };
34
+ return Object.keys(now).length ? { ...i, [field]: now } : (({ [field]: _, ...others }) => others)(i);
35
+ });
29
36
  // an id added to a list, or taken from it: the rest as it was
30
37
  if (link !== undefined)
31
38
  return changed((i) => {
package/src/files.ts CHANGED
@@ -1,4 +1,4 @@
1
- // A file a document names: a photo, an SVG or a PDF, its field's value `file:<its SHA-256>.<ext>` (the compiler's file form),
1
+ // A file a document names: a photo, an SVG, a PDF or a Markdown file, its field's value `file:<its SHA-256>.<ext>` (the compiler's file form),
2
2
  // kept in a folder, files/ beside the store, or a bucket (JOINED_FILES: `s3://<bucket>/<prefix>`), and served at
3
3
  // /files/<its name>. Its name is its content's hash, so
4
4
  // it never changes and is cached for good; the same file sent twice is kept once. A photo is kept as a WebP at most
@@ -22,7 +22,9 @@ type Handler = (req: Request, who?: Who) => Response | Promise<Response>;
22
22
  /** Who a request or a socket is: a member, by the token it came with, and the agent's link if it came by one. */
23
23
  export type Who = { member: number; token: string; agent?: Agent };
24
24
  /** A page's socket, as the server keeps it: the documents it subscribes to, who it is, and the site it came from. */
25
- export type Data = { docs: Set<string>; who: Who | null; origin: string };
25
+ /** A page's socket's own: the documents it holds, who it is, where it's from, and the changes a stranger's page made
26
+ * (`<doc> <version>`), which only it may undo. */
27
+ export type Data = { docs: Set<string>; who: Who | null; origin: string; made?: Set<string> };
26
28
  type Socket = Bun.ServerWebSocket<Data>;
27
29
  /** A write the group lets go: its ops, with what they bring, and what follows them. */
28
30
  /** A write the group lets go: its ops, with what they bring; what must land with them, in the write's own
package/src/ops.ts CHANGED
@@ -54,6 +54,11 @@ export function docParts(doc: string) {
54
54
  return { base, name, key, params: Object.fromEntries(new URLSearchParams(query ?? "")) as Record<string, string> };
55
55
  }
56
56
 
57
+ /** "$n": a write of several's nth add (src/together.ts). */
58
+ export const NTH = /^\$([1-9]\d*)$/;
59
+ /** Whether a part's document is named by an add before it in its write (`board:$1`): no page holds it yet. */
60
+ export const madeBy = (doc: string) => NTH.test(docParts(doc).key ?? "");
61
+
57
62
  /** An op's path, in its parts; `link` is an id in a list of ids, and `more` anything past it, which no op may have. */
58
63
  export function parts(path: string) {
59
64
  const [, coll = "", key = "", field, link, ...more] = path.split("/");
package/src/page.ts CHANGED
@@ -34,7 +34,7 @@
34
34
  // person has typed is waiting (a write unanswered, a draft's changes), saying so in the toast meanwhile.
35
35
 
36
36
  import { unsaved } from "./draft.ts";
37
- import { attribute, parts, valueAt, versionKey, type Op, type Type } from "./ops.ts";
37
+ import { attribute, madeBy, parts, valueAt, versionKey, type Op, type Type } from "./ops.ts";
38
38
  import { adds, keep, sent, waiting, type Kept } from "./outbox.ts";
39
39
  import { toast } from "./toast.ts";
40
40
  // a display's list kept by key: its own module (joined/keep), here too for what imported it from here
@@ -148,6 +148,7 @@ export function letGo(el: HTMLElement) {
148
148
  docs.delete(doc);
149
149
  mays.delete(doc);
150
150
  socket.behind.delete(doc);
151
+ socket.ready.delete(doc);
151
152
  delete document.body.dataset[versionKey(doc)];
152
153
  if (isOpen()) socket.ws!.send(JSON.stringify({ unsubscribe: doc }));
153
154
  return;
@@ -167,6 +168,9 @@ document.addEventListener("ops", (e) => {
167
168
  const { doc, ops, why, writes, answer } = (
168
169
  e as CustomEvent<{ doc: string; ops: Op[]; why?: string; writes?: Part[]; answer?: Answered }>
169
170
  ).detail;
171
+ // a write of several: each part a document the page holds, or one the write makes (board:$1)
172
+ const off = writes?.find((p) => !docs.has(p.doc) && !madeBy(p.doc));
173
+ if (writes && (!writes.length || off)) return answer?.(off ? `${off.doc} isn't on this page` : "a write of nothing");
170
174
  if (!isOpen() && (writes ?? [{ ops }]).every((p) => adds(p.ops)) && (writes || docs.has(doc)))
171
175
  return (keep(writes ? { writes } : { doc, ops, why }), answer?.());
172
176
  // landed, its sender is told the version it made, from the write's own answer (a page's undo keeps it: `undo`)
@@ -195,7 +199,8 @@ export const ask = (doc: string, asked: object) =>
195
199
 
196
200
  /** Undo a change of a document the page holds, its own, by the version it made (a write's `landed` says it): the
197
201
  * server puts back what it changed, unless something it changed has changed since. Its answer: why it was refused,
198
- * or the version the undo made, which undone in turn is a redo. What it did comes as the document's change. */
202
+ * or the version the undo made, which undone in turn is a redo. What it did comes as the document's change. One
203
+ * document's: a write of several (together) is undone by each part's own version, its first's alone told as landed. */
199
204
  export const undo = (doc: string, version: number) =>
200
205
  new Promise<{ why?: string; version?: number }>((done) =>
201
206
  docs.has(doc)
@@ -224,7 +229,8 @@ const subscribe = (s: WebSocket, doc: string) => {
224
229
  /** What was kept while the page was away, sent now it's back: oldest first, each once its documents are held again;
225
230
  * the signal gone again, the rest wait. One refused is let go, and said. */
226
231
  async function flush() {
227
- const ready = (k: Kept) => (k.writes?.map((p) => p.doc) ?? [k.doc!]).every((d) => socket.ready.has(d));
232
+ // (a document the write makes, board:$1, is held by no page until it's made)
233
+ const ready = (k: Kept) => (k.writes?.map((p) => p.doc) ?? [k.doc!]).every((d) => socket.ready.has(d) || madeBy(d));
228
234
  if (socket.sending) return;
229
235
  socket.sending = true;
230
236
  try {
package/src/server.ts CHANGED
@@ -406,7 +406,7 @@ export async function start({
406
406
  if (docParts(doc).name === GROUP) throw new Refused("the group's changes aren't undone");
407
407
  const ops = store.undoing(doc, version);
408
408
  // held to the stories as any write is
409
- const why = ungranted(doc, ops, null);
409
+ const why = ungranted(doc, ops, null, true, true);
410
410
  if (why) throw new Refused(why);
411
411
  const landed = land([{ doc, ops }], by, null, { undoes: version, held: pagesHold() });
412
412
  tell(landed);
@@ -668,23 +668,31 @@ export async function start({
668
668
  // in turn (a redo)
669
669
  if (m.undo !== undefined) {
670
670
  const doc = m.doc!,
671
+ base = docParts(doc).base,
671
672
  by = who?.member ?? null;
672
- const made = store.change(docParts(doc).base, m.undo);
673
+ const made = store.change(base, m.undo);
674
+ // nobody's change (a stranger's, the server's own, a rule's, a reaction's) is undone only by the page that made it
673
675
  const no = !ws.data.docs.has(doc)
674
- ? `this page isn't subscribed to ${docParts(doc).base}`
675
- : made && made.by !== by
676
+ ? `this page isn't subscribed to ${base}`
677
+ : made && (made.by !== by || (by === null && !ws.data.made?.has(`${base} ${m.undo}`)))
676
678
  ? "a change is undone by whoever made it"
677
- : null;
679
+ : docParts(base).name === GROUP
680
+ ? "the group's changes aren't undone"
681
+ : null;
682
+ // held as the page's own write is: who they are now (read-only, a role gone, a guest's level), the app's
683
+ // rules, and its settings (frozen); then guarded as the server's undo is
684
+ let w: Written;
678
685
  try {
679
686
  if (no) throw new Refused(no);
680
- const { version } = undo(doc, m.undo, by);
681
- log?.(`undo ${docParts(doc).base} ${m.undo} by ${byOf(who)}: done, version ${version}`);
682
- return say(ws, { done: m.id, version });
687
+ w = await written([{ doc, ops: store.undoing(base, m.undo) }], who, m.undo);
683
688
  } catch (e) {
684
689
  if (!(e instanceof Refused)) throw e;
685
- log?.(`undo ${docParts(doc).base} ${m.undo} by ${byOf(who)}: refused, ${e.message}`);
690
+ log?.(`undo ${base} ${m.undo} by ${byOf(who)}: refused, ${e.message}`);
686
691
  return say(ws, { refused: m.id, why: e.message });
687
692
  }
693
+ if ("why" in w) return say(ws, { refused: m.id, why: w.why });
694
+ if (by === null) (ws.data.made ??= new Set()).add(`${base} ${w.version}`);
695
+ return say(ws, { done: m.id, version: w.version });
688
696
  }
689
697
  if (!m.ops && !m.writes) {
690
698
  return say(ws, await group.asked(ws, m as Message & { id: number }));
@@ -703,6 +711,9 @@ export async function start({
703
711
  writes.map((w) => ({ ...w, why: w.why?.trim() || undefined })),
704
712
  who,
705
713
  );
714
+ // a stranger's change, which only this page undoes
715
+ if (!("why" in w) && who === null)
716
+ (ws.data.made ??= new Set()).add(`${docParts(writes[0]!.doc).base} ${w.version}`);
706
717
  say(
707
718
  ws,
708
719
  "why" in w
@@ -717,10 +728,11 @@ export async function start({
717
728
  /** A write, a page's or an agent's at the MCP door: of one document or several (src/together.ts), each as it names
718
729
  * it (a window's write is its document's), by who, landed whole or not at all, told, and answered: done, with the
719
730
  * version its first part made and the id each item added was given, in order, so the writer needn't match them; or
720
- * refused, why. Each logs one line. */
721
- async function written(writes: Part[], who: Who | null): Promise<Written> {
731
+ * refused, why. Each logs one line. `undoes`: a page's undo, of the version it puts back (src/write.ts, ask). */
732
+ async function written(writes: Part[], who: Who | null, undoes?: number): Promise<Written> {
722
733
  const doc = writes.map((w) => docParts(w.doc).base).join(" and "),
723
- by = byOf(who);
734
+ by = byOf(who),
735
+ act = undoes === undefined ? "write" : `undo ${undoes}`;
724
736
  // In the write's transaction, in order, refused at the first that says no: the app's own rules, the grants, and
725
737
  // the group's rules (src/write.ts), and, as its rows are written, the tables' constraints; each part's.
726
738
  const afters: ((applied: Op[]) => Promise<void>)[] = [];
@@ -730,23 +742,30 @@ export async function start({
730
742
  const no = mayHold(name, who?.member ?? null, !(members && rootToken));
731
743
  if (no) throw new Refused(no);
732
744
  let after: (applied: Op[]) => Promise<void> = async () => {};
733
- const asking = ask(name, ops, why, who, (doc, ops) => {
734
- const write = group.write(doc, ops, who, why);
735
- if (!("refused" in write)) after = write.after;
736
- return write;
737
- });
745
+ const asking = ask(
746
+ name,
747
+ ops,
748
+ why,
749
+ who,
750
+ (doc, ops) => {
751
+ const write = group.write(doc, ops, who, why);
752
+ if (!("refused" in write)) after = write.after;
753
+ return write;
754
+ },
755
+ undoes !== undefined,
756
+ );
738
757
  afters.push(after);
739
758
  return asking;
740
759
  };
741
760
  // a write lands whole or not at all, with what follows from it, in the store's one transaction; then it's told
742
761
  let landed: ReturnType<typeof land>;
743
762
  try {
744
- landed = land(writes, who?.member ?? null, who?.agent?.label ?? null, { asked, held: pagesHold() });
763
+ landed = land(writes, who?.member ?? null, who?.agent?.label ?? null, { undoes, asked, held: pagesHold() });
745
764
  } catch (e) {
746
765
  // refused in words; anything else is the server's, logged, and the writer told it wasn't saved
747
766
  if (!(e instanceof Refused)) console.error(`a write to ${doc} failed:`, e);
748
767
  const why = e instanceof Refused ? e.message : "That wasn't saved: the server had a problem with it.";
749
- log?.(`write ${doc} by ${by}: refused, ${why}`);
768
+ log?.(`${act} ${doc} by ${by}: refused, ${why}`);
750
769
  return { why };
751
770
  }
752
771
  tell(landed);
@@ -755,7 +774,7 @@ export async function start({
755
774
  // what follows each part (the group's pages told, a new member's link made), and then the writer's answer:
756
775
  // done is all of it done
757
776
  for (const [i, after] of afters.entries()) await after(landed.parts[i]!.ops);
758
- log?.(`write ${doc} by ${by}: done, version ${change.version}`);
777
+ log?.(`${act} ${doc} by ${by}: done, version ${change.version}`);
759
778
  const ids = landed.applied
760
779
  .filter((o) => o.op === "add" && parts(o.path).key === "-" && parts(o.path).field === undefined)
761
780
  .map((o) => o.value.id as number);
package/src/together.ts CHANGED
@@ -11,7 +11,7 @@
11
11
 
12
12
  import type { Story } from "./compile.ts";
13
13
  import { Refused } from "./engine.ts";
14
- import { docParts, parts, type Op } from "./ops.ts";
14
+ import { docParts, madeBy, NTH, parts, type Op } from "./ops.ts";
15
15
 
16
16
  /** One document's part of a write: its ops, and why, as a write of one document is. */
17
17
  export type Part = { doc: string; ops: Op[]; why?: string };
@@ -22,14 +22,10 @@ export type Told = Omit<Story, "does" | "cannot"> & { does?: Part | Part[]; cann
22
22
  /** The documents as the compiler holds a story's state: each by its name (a kind's, every instance's rows). */
23
23
  type State = Record<string, Record<string, unknown>>;
24
24
 
25
- /** "$n": the write's nth add. */
26
- const NTH = /^\$([1-9]\d*)$/;
27
-
28
25
  /** A story's does or cannot, as its parts: one write, or several. */
29
26
  export const partsOf = (w: Part | Part[]) => (Array.isArray(w) ? w : [w]);
30
27
 
31
- /** Whether a part's document is named by an add before it in its write (`board:$1`): no page holds it yet. */
32
- export const madeBy = (doc: string) => NTH.test(docParts(doc).key ?? "");
28
+ export { madeBy };
33
29
 
34
30
  /** Each add of an item, in order: what a later "$n" counts. */
35
31
  const addsOf = (ops: Op[]) =>
package/src/write.ts CHANGED
@@ -140,8 +140,9 @@ export function writes({
140
140
  /** Why a write does what no story does (an op of a form no path has, or one no story grants), or what the
141
141
  * writer's roles may not; null if neither. `roles` null: the server's own write, or nobody's on an open group,
142
142
  * asked only whether a story does it; `anyone`, whether what's granted anyone is the writer's. Asked in the write's
143
- * transaction: the values are the tables' to refuse. */
144
- function ungranted(doc: string, ops: Op[], roles: ((o: Op) => string[]) | null, anyone = true) {
143
+ * transaction: the values are the tables' to refuse. `undoing`: an undo's inverse, where an item removed is put back
144
+ * as it was, at whatever step it had come to, by anyone a story lets add one at any step. */
145
+ function ungranted(doc: string, ops: Op[], roles: ((o: Op) => string[]) | null, anyone = true, undoing = false) {
145
146
  const { grants, collections, moves } = rulesOf(doc);
146
147
  for (const o of ops) {
147
148
  const { coll, key, field, link, more } = parts(o.path);
@@ -169,15 +170,23 @@ export function writes({
169
170
  // an add giving a field that moves: its first step, from nothing
170
171
  const first =
171
172
  formed && o.op === "add" && field === undefined && moves?.[coll] ? (o.value as any)?.[moves[coll]] : undefined;
172
- const to = formed
173
- ? (grants[
174
- moved
175
- ? `${grantOf(o)}:${itemAt(doc, o)?.[field!]}>${o.value}`
176
- : first !== undefined
177
- ? `${grantOf(o)}.${moves![coll]}:>${first}`
178
- : grantOf(o)
179
- ] ?? (filled && !moved ? [] : undefined))
180
- : undefined;
173
+ const back =
174
+ undoing && first !== undefined
175
+ ? Object.entries(grants)
176
+ .filter(([g]) => g === grantOf(o) || g.startsWith(`${grantOf(o)}.${moves![coll]}:>`))
177
+ .flatMap(([, to]) => to)
178
+ : [];
179
+ const to = back.length
180
+ ? back
181
+ : formed
182
+ ? (grants[
183
+ moved
184
+ ? `${grantOf(o)}:${itemAt(doc, o)?.[field!]}>${o.value}`
185
+ : first !== undefined
186
+ ? `${grantOf(o)}.${moves![coll]}:>${first}`
187
+ : grantOf(o)
188
+ ] ?? (filled && !moved ? [] : undefined))
189
+ : undefined;
181
190
  if (!to) return moved || first !== undefined ? "that isn't a move you can make" : `no ${o.op} at ${o.path}`;
182
191
  if (roles && !grantsTo(to, () => roles(o), anyone)) return `you may not ${o.op} at ${o.path}`;
183
192
  }
@@ -412,6 +421,7 @@ export function writes({
412
421
  why: string | undefined,
413
422
  who: { member: number } | null,
414
423
  group: (doc: string, ops: Op[]) => { ops: Op[]; within?: (applied: Op[]) => void } | { refused: string },
424
+ undoing = false,
415
425
  ) {
416
426
  const r = rulesOf(doc);
417
427
  if (r.derived) throw new Refused(`${doc} is derived from ${r.derived.join(" and ")}: write that`);
@@ -420,7 +430,8 @@ export function writes({
420
430
  // an anonymous document's writes are anyone's; any other's, each op's by who the writer is to its item
421
431
  const ruled =
422
432
  trusts[r.doc]?.({ doc: docParts(doc).base, ops, why }, { member: who?.member ?? 0, roles }, read) ??
423
- serversIn(doc, ops) ??
433
+ // (an undo puts back what the server filled, as it was)
434
+ (undoing ? null : serversIn(doc, ops)) ??
424
435
  // who the writer is to the item each op is to: a member's roles, and a guest's (a page holding the document by
425
436
  // the item's own code: definitions' guests)
426
437
  ungranted(
@@ -428,11 +439,13 @@ export function writes({
428
439
  ops,
429
440
  !who && r.anonymous ? null : (o) => rolesTo(doc, who?.member ?? null, parts(o.path).coll, itemAt(doc, o)),
430
441
  anyoneIs(doc, who?.member ?? null),
442
+ undoing,
431
443
  ) ??
432
444
  // of a kind, what its root item's settings say of a write the grants allow (a frozen board, one held for approval)
433
445
  settled(
434
446
  r,
435
- settingsNow(r, () => rootItem(r, doc, read)),
447
+ // (an undo puts an item back at the step it had come to: frozen alone refuses it)
448
+ ((now) => (undoing ? { ...now, first: {} } : now))(settingsNow(r, () => rootItem(r, doc, read))),
436
449
  ops,
437
450
  );
438
451
  if (ruled) throw new Refused(ruled);
@@ -472,7 +485,9 @@ export function writes({
472
485
  applied: Op[] = [],
473
486
  own: Change[] = [];
474
487
  const one = (d: string, o: Op[], b: number | null, v: string | null, w?: (applied: Op[]) => void, u?: number) => {
475
- const made = store.write(d, fill(d, withGone(d, o), b), b, v, w, { undoes: u });
488
+ // (an undo puts back what was, filled as it was then: a field cleared since stays clear)
489
+ const gone = withGone(d, o);
490
+ const made = store.write(d, u === undefined ? fill(d, gone, b) : gone, b, v, w, { undoes: u });
476
491
  // what the definitions hold of it (unique, noOverlap), once the write is done: refused whole if not
477
492
  const held = rulesOf(d).holds?.(read(d), touchedBy(made.applied));
478
493
  if (held) throw new Refused(held);