@blueshed/joined 0.0.0-stage → 0.1.19

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.
Files changed (213) hide show
  1. package/.claude/skills/joined/SKILL.md +17 -0
  2. package/.claude/skills/joined/reference.md +768 -0
  3. package/LICENSE +19 -0
  4. package/README.md +115 -2
  5. package/docs/design.md +148 -0
  6. package/docs/introduction.md +193 -0
  7. package/docs/learning.md +76 -0
  8. package/docs/stories.md +82 -0
  9. package/examples/app.css +27 -0
  10. package/examples/cafe/books.html +38 -0
  11. package/examples/cafe/books.json +13 -0
  12. package/examples/cafe/books.ts +64 -0
  13. package/examples/cafe/cafe.css +512 -0
  14. package/examples/cafe/front.html +115 -0
  15. package/examples/cafe/front.ts +7 -0
  16. package/examples/cafe/generated/books.elements.ts +52 -0
  17. package/examples/cafe/generated/books.queries.ts +11 -0
  18. package/examples/cafe/generated/books.rules.ts +42 -0
  19. package/examples/cafe/generated/kitchen.elements.ts +87 -0
  20. package/examples/cafe/generated/kitchen.queries.ts +47 -0
  21. package/examples/cafe/generated/kitchen.rules.ts +98 -0
  22. package/examples/cafe/generated/menu.elements.ts +61 -0
  23. package/examples/cafe/generated/menu.queries.ts +28 -0
  24. package/examples/cafe/generated/menu.rules.ts +65 -0
  25. package/examples/cafe/generated/order.elements.ts +87 -0
  26. package/examples/cafe/generated/order.queries.ts +47 -0
  27. package/examples/cafe/generated/order.rules.ts +95 -0
  28. package/examples/cafe/generated/staff.elements.ts +55 -0
  29. package/examples/cafe/generated/staff.queries.ts +27 -0
  30. package/examples/cafe/generated/staff.rules.ts +59 -0
  31. package/examples/cafe/generated/supplies.elements.ts +62 -0
  32. package/examples/cafe/generated/supplies.queries.ts +43 -0
  33. package/examples/cafe/generated/supplies.rules.ts +84 -0
  34. package/examples/cafe/generated/till.elements.ts +93 -0
  35. package/examples/cafe/generated/till.queries.ts +57 -0
  36. package/examples/cafe/generated/till.rules.ts +113 -0
  37. package/examples/cafe/index.html +70 -0
  38. package/examples/cafe/kitchen.css +107 -0
  39. package/examples/cafe/kitchen.html +39 -0
  40. package/examples/cafe/kitchen.ts +125 -0
  41. package/examples/cafe/menu.html +55 -0
  42. package/examples/cafe/menu.json +41 -0
  43. package/examples/cafe/menu.ts +84 -0
  44. package/examples/cafe/migrations/001-cafe.ts +11 -0
  45. package/examples/cafe/order.html +37 -0
  46. package/examples/cafe/order.ts +45 -0
  47. package/examples/cafe/rail-drag.ts +30 -0
  48. package/examples/cafe/routes.ts +32 -0
  49. package/examples/cafe/staff.json +20 -0
  50. package/examples/cafe/stories.jsonl +43 -0
  51. package/examples/cafe/supplies.html +77 -0
  52. package/examples/cafe/supplies.json +33 -0
  53. package/examples/cafe/supplies.ts +170 -0
  54. package/examples/cafe/till.json +95 -0
  55. package/examples/cafe/till.ts +122 -0
  56. package/examples/cafe/trust.ts +20 -0
  57. package/examples/cafe/views.ts +171 -0
  58. package/examples/docs.ts +28 -0
  59. package/examples/film.jpg +0 -0
  60. package/examples/hello/generated/elements.ts +45 -0
  61. package/examples/hello/generated/queries.ts +24 -0
  62. package/examples/hello/generated/rules.ts +45 -0
  63. package/examples/hello/hello.css +27 -0
  64. package/examples/hello/hello.json +1 -0
  65. package/examples/hello/index.html +32 -0
  66. package/examples/hello/migrations/001-hello.ts +6 -0
  67. package/examples/hello/page.ts +49 -0
  68. package/examples/hello/routes.ts +6 -0
  69. package/examples/hello/stories.jsonl +3 -0
  70. package/examples/index.html +146 -0
  71. package/examples/page.ts +11 -0
  72. package/examples/people/adding.ts +20 -0
  73. package/examples/people/generated/elements.ts +68 -0
  74. package/examples/people/generated/queries.ts +28 -0
  75. package/examples/people/generated/rules.ts +85 -0
  76. package/examples/people/links.ts +124 -0
  77. package/examples/people/migrations/001-people.ts +6 -0
  78. package/examples/people/migrations/002-add-people-member-and-guard-people.ts +7 -0
  79. package/examples/people/migrations/003-add-people-rank-and-guard-people.ts +7 -0
  80. package/examples/people/order.ts +61 -0
  81. package/examples/people/people.css +65 -0
  82. package/examples/people/people.json +26 -0
  83. package/examples/people/routes.ts +12 -0
  84. package/examples/people/stories.jsonl +20 -0
  85. package/examples/people/trust.ts +14 -0
  86. package/examples/people/view.ts +115 -0
  87. package/examples/routes.ts +11 -0
  88. package/examples/trip/answer-rows.ts +70 -0
  89. package/examples/trip/answers.ts +11 -0
  90. package/examples/trip/bar.ts +89 -0
  91. package/examples/trip/calendar-map.css +709 -0
  92. package/examples/trip/calendar-map.ts +249 -0
  93. package/examples/trip/drags.ts +143 -0
  94. package/examples/trip/drawer.ts +250 -0
  95. package/examples/trip/event-form.ts +199 -0
  96. package/examples/trip/generated/elements.ts +144 -0
  97. package/examples/trip/generated/queries.ts +46 -0
  98. package/examples/trip/generated/rules.ts +239 -0
  99. package/examples/trip/holding.ts +61 -0
  100. package/examples/trip/ics.ts +117 -0
  101. package/examples/trip/index.html +82 -0
  102. package/examples/trip/line-stops.ts +92 -0
  103. package/examples/trip/line-words.ts +79 -0
  104. package/examples/trip/line.ts +121 -0
  105. package/examples/trip/map.ts +234 -0
  106. package/examples/trip/mercator.ts +28 -0
  107. package/examples/trip/migrations/001-trip.ts +7 -0
  108. package/examples/trip/migrations/002-add-events-zone-and-guard-events.ts +7 -0
  109. package/examples/trip/migrations/003-guard-events-and-create-events_asked-and-more.ts +9 -0
  110. package/examples/trip/migrations/004-create-stays.ts +6 -0
  111. package/examples/trip/migrations/005-guard-events-and-create-events_arrives-and-more.ts +8 -0
  112. package/examples/trip/month.ts +87 -0
  113. package/examples/trip/other-weeks.ts +67 -0
  114. package/examples/trip/page.ts +121 -0
  115. package/examples/trip/peek.ts +71 -0
  116. package/examples/trip/people-chips.ts +74 -0
  117. package/examples/trip/place-search.ts +51 -0
  118. package/examples/trip/places.ts +34 -0
  119. package/examples/trip/routes.ts +27 -0
  120. package/examples/trip/shell.ts +39 -0
  121. package/examples/trip/stays.css +51 -0
  122. package/examples/trip/stays.ts +68 -0
  123. package/examples/trip/stories.jsonl +72 -0
  124. package/examples/trip/time.ts +46 -0
  125. package/examples/trip/told.ts +48 -0
  126. package/examples/trip/trip.json +69 -0
  127. package/examples/trip/trust.ts +51 -0
  128. package/examples/trip/week.ts +237 -0
  129. package/examples/trip/words.ts +4 -0
  130. package/examples/trip/writes.ts +91 -0
  131. package/examples/trip/zones.ts +16 -0
  132. package/examples/trips/card.ts +149 -0
  133. package/examples/trips/generated/elements.ts +60 -0
  134. package/examples/trips/generated/queries.ts +28 -0
  135. package/examples/trips/generated/rules.ts +62 -0
  136. package/examples/trips/index.html +47 -0
  137. package/examples/trips/map.ts +52 -0
  138. package/examples/trips/migrations/001-trips.ts +8 -0
  139. package/examples/trips/new-trip.ts +46 -0
  140. package/examples/trips/page.ts +9 -0
  141. package/examples/trips/routes.ts +7 -0
  142. package/examples/trips/stories.jsonl +12 -0
  143. package/examples/trips/trips.css +183 -0
  144. package/examples/trips/trips.json +7 -0
  145. package/examples/weeks/derive.ts +23 -0
  146. package/examples/weeks/generated/elements.ts +52 -0
  147. package/examples/weeks/generated/rules.ts +33 -0
  148. package/examples/weeks/routes.ts +4 -0
  149. package/examples/weeks/weeks.json +1 -0
  150. package/examples/welcome.css +299 -0
  151. package/package.json +51 -4
  152. package/src/attachment.ts +70 -0
  153. package/src/cli.ts +52 -0
  154. package/src/client.ts +98 -0
  155. package/src/compile.ts +2034 -0
  156. package/src/css.d.ts +5 -0
  157. package/src/definitions.ts +268 -0
  158. package/src/demo.css +29 -0
  159. package/src/demo.ts +42 -0
  160. package/src/draft.ts +69 -0
  161. package/src/engine.ts +288 -0
  162. package/src/file-value.ts +13 -0
  163. package/src/files.ts +187 -0
  164. package/src/follow.ts +56 -0
  165. package/src/hues.ts +3 -0
  166. package/src/icon-names.ts +77 -0
  167. package/src/icon.png +0 -0
  168. package/src/icons.ts +32 -0
  169. package/src/keep.ts +33 -0
  170. package/src/link.ts +31 -0
  171. package/src/mcp.ts +252 -0
  172. package/src/members/demo.ts +53 -0
  173. package/src/members/generated/elements.ts +68 -0
  174. package/src/members/generated/queries.ts +29 -0
  175. package/src/members/generated/rules.ts +65 -0
  176. package/src/members/group.ts +294 -0
  177. package/src/members/keys.ts +221 -0
  178. package/src/members/members.json +9 -0
  179. package/src/members/migrations/001-members.ts +6 -0
  180. package/src/members/migrations/002-guard-members.ts +11 -0
  181. package/src/members/migrations/003-add-members-act-and-guard-members.ts +7 -0
  182. package/src/members/panel.css +208 -0
  183. package/src/members/panel.ts +398 -0
  184. package/src/members/routes.ts +4 -0
  185. package/src/members/stories.jsonl +27 -0
  186. package/src/members/trust.ts +116 -0
  187. package/src/migrate.ts +597 -0
  188. package/src/migrations/001-joined.ts +13 -0
  189. package/src/migrations/002-group-keys.ts +16 -0
  190. package/src/migrations/003-change-inverse.ts +10 -0
  191. package/src/migrations/004-change-followed.ts +6 -0
  192. package/src/ops.ts +61 -0
  193. package/src/page.css +79 -0
  194. package/src/page.ts +413 -0
  195. package/src/parts.css +471 -0
  196. package/src/parts.ts +152 -0
  197. package/src/server.ts +862 -0
  198. package/src/settings.ts +94 -0
  199. package/src/starter.ts +89 -0
  200. package/src/store.ts +815 -0
  201. package/src/stories.ts +387 -0
  202. package/src/toast.ts +52 -0
  203. package/src/together.ts +167 -0
  204. package/src/tokens.css +85 -0
  205. package/src/tokens.json +457 -0
  206. package/src/upkeep.ts +86 -0
  207. package/src/visitors.ts +43 -0
  208. package/src/write.ts +567 -0
  209. package/starter/CLAUDE.md +37 -0
  210. package/starter/gitignore +3 -0
  211. package/starter/tests/hello-page.test.ts +14 -0
  212. package/starter/tests/hello.test.ts +8 -0
  213. package/starter/tsconfig.json +17 -0
package/src/css.d.ts ADDED
@@ -0,0 +1,5 @@
1
+ // A stylesheet imported as text (`with { type: "text" }`), for a shadow DOM to adopt.
2
+ declare module "*.css" {
3
+ const text: string;
4
+ export default text;
5
+ }
@@ -0,0 +1,268 @@
1
+ // An app's definitions (its stories.jsonl's header lines): what its stories can't show, read by the compiler alone and
2
+ // written into its generated/rules.ts as plain code, so nothing reads them as it runs (docs/plan.md, "The
3
+ // definitions"). Each key is optional, a line of its own.
4
+ //
5
+ // links a field that links though it isn't named for its collection: { organisers: "members" }, a list;
6
+ // { account: { to: "accounts", one: true } }, one id. Its own document's collection, or another's.
7
+ // { post: { to: "posts", one: true, gone: "with" } }: removing the post removes what links to it, as ops
8
+ // roles who someone is to the document, beyond a member, each a who-path to them.
9
+ // owns who sees what: a who-path for the whole document, or one per collection (a collection not named
10
+ // is everyone's). A page is sent, and told of, only the items its person sees.
11
+ // kind { root: "shelves" }: a document for each item of another document's collection (shelf:1,
12
+ // shelf:2), its collections each instance's own; a role's @field is that root item's, or the item's a write
13
+ // is to where it has that field (a post's author).
14
+ // fills a field the server fills on every add, and only the server writes (a route's write, a reaction):
15
+ // { cards: { added: "now" }, boards: { count: 0 } }; "now" is the instant it's added, "me" its writer.
16
+ // unique fields no two items of a collection share: { people: ["name"] }, or several together
17
+ // ({ rooms: [["hotel", "name"]] }); an item without one is held to nothing.
18
+ // noOverlap a collection's items that may not hold one time: { stays: { range: ["from", "until"], same:
19
+ // ["room"] } }, among those whose same fields are equal (none: all of them). A range is half
20
+ // open, from its start until its end, a time or a number, or a duration on from a time; an
21
+ // item without a start or an end is open on that side.
22
+ // window a part of a collection a page may hold alone, by a parameter of its query and its topic
23
+ // (trip?week=2026-11-02): { events: { week: { from: "start", for: "P7D" } } }, the items whose start is
24
+ // in the days from it; with a length too ({ ..., length: "duration" }), those whose time overlaps them; { products: { category: "category" } }, those whose field is it.
25
+ // copies fields an item takes from what a link of it points at, once, when it's added, and never follows:
26
+ // { items: { from: "product", fields: ["description", "image"] } }; the server's to fill, as fills are.
27
+ // order a collection held in a field's order, a number, then by id: { people: "rank" }. An item without one ranks by its id;
28
+ // one moved is put where its new rank says, on every page (a drag to reorder is a replace of its rank).
29
+ // timed a field whose time, an instant, comes: { cards: "due" }. Then the server clears it, as its own
30
+ // write, told and reacted to as any is (a clock's next tick is a reaction setting it again).
31
+ // frozen { by: "frozen", refused: "…" }: in a kind, while the root item's field is true, its instance is kept as it
32
+ // is: every page's write refused in those words (src/settings.ts).
33
+ // waits { by: "moderated", posts: "waiting", refused: "…" }: in a kind, while the root item's field is true, an
34
+ // add to posts gives its field that moves (moves) as that first step, or it's refused in those words.
35
+ //
36
+ // A who-path is terms joined by "|", either holding:
37
+ // trader a role (or "member", anyone)
38
+ // @members the item's own field: the member it names, or one of those it lists (in a role, the
39
+ // kind's root item's)
40
+ // holdings@members the members named by any item of a collection of this document; posts[role=trader]@members
41
+ // those of the items whose field is that; members[!inviter] the items with no inviter,
42
+ // themselves members; fund/posts@members, a collection of another document
43
+
44
+ import type { Op } from "./ops.ts";
45
+
46
+ /** An app's definitions, as its stories' header lines say them, one a key. */
47
+ export type Definitions = {
48
+ links?: Record<string, string | { to: string; one?: true; gone?: "with" }>;
49
+ roles?: Record<string, string>;
50
+ owns?: string | Record<string, string>;
51
+ kind?: { root: string };
52
+ fills?: Record<string, Record<string, unknown>>;
53
+ timed?: Record<string, string>;
54
+ unique?: Record<string, (string | string[])[]>;
55
+ noOverlap?: Record<string, { range: [string, string]; same?: string[] }>;
56
+ window?: Record<string, Record<string, string | { from: string; for: string; length?: string }>>;
57
+ order?: Record<string, string>;
58
+ copies?: Record<string, { from: string; fields: string[] }>;
59
+ documents?: Record<string, Query>;
60
+ takes?: Record<string, Takes>;
61
+ gives?: Record<string, Takes>;
62
+ /** A field whose value moves only as the stories move it, each step by the roles whose stories make it (an
63
+ * order's status: placed, then being made, by the kitchen). */
64
+ moves?: Record<string, string>;
65
+ totals?: Record<string, Record<string, Total>>;
66
+ /** A field holding an item's own code: a page holding the document by it (a window, `order?code=…`) is that
67
+ * item's guest, who signs in to nothing, and is the role `guest` to it alone. */
68
+ guests?: Record<string, string>;
69
+ /** A kind's root's field holding the level its guests are (`{ boards: "visitors" }`, src/visitors.ts): a guest is
70
+ * the role `guest:` that level, granted what the stories have a guest on an item of it do; any other, No access. */
71
+ visitors?: Record<string, string>;
72
+ where?: Record<string, Record<string, Where | Where[] | string>>;
73
+ /** In a kind, a yes-or-no field of the root item: while it's true, every page's write to its instance is refused, in
74
+ * these words (a frozen board). src/settings.ts. */
75
+ frozen?: { by: string; refused: string };
76
+ /** In a kind, a yes-or-no field of the root item: while it's true, an add to each collection named gives its field
77
+ * that moves as this first step, or it's refused in these words (a board held for approval, a post waiting).
78
+ * src/settings.ts. */
79
+ waits?: { by: string; refused: string; [coll: string]: string };
80
+ };
81
+ /** Which rows of a collection a document holds (definitions' where): those whose fields are each the value given,
82
+ * or one of the values; with `last`, the newest so many by a field (`by`). Several: any of them. A collection's
83
+ * name in their place: the rows linking to those its document holds of that one (an order's lines). `holds`, a
84
+ * who-path: who may hold the document at all. */
85
+ export type Where = { [field: string]: unknown; last?: number; by?: string };
86
+ /** A read-only document over an app's entities, by member (each side's books): a collection each, its items the rows
87
+ * of a table one of whose fields names the member (`sides`, each said as a word), those with `when` true; each with
88
+ * some of its fields, and its total: the sum of a field of the rows naming it, each times another of theirs. */
89
+ export type Query = Record<
90
+ string,
91
+ {
92
+ from: string;
93
+ when?: string;
94
+ sides: Record<string, string>;
95
+ fields: string[];
96
+ total: { sum: string; times?: string };
97
+ }
98
+ >;
99
+ /** A rule over values: a collection's field taken from a field of what a link of it points at, while a field of
100
+ * what another of its links points at is true (an item's quantity from its product's stock, while its order is
101
+ * placed): `{ items: { quantity: "product.stock", while: "order.placed", refused: "…" } }`. Taken or given back as
102
+ * either changes; refused, in its words, where it would leave less than none (`{stock}`, what's there; `{quantity}`,
103
+ * what was asked). */
104
+ export type Takes = { while: string; refused: string } & Record<string, string>;
105
+ /** A field the database keeps as the sum of the rows linking to its item (definitions' totals): an order's total,
106
+ * each of its lines' qty times its price. */
107
+ export type Total = { of: string; by: string; sum: string; times?: string };
108
+
109
+ const TERM =
110
+ /^(?:([a-z][a-z0-9-]*)\/)?([a-z][a-z0-9-]*)(?:\[(!?)([a-z][a-z0-9-]*)(?:=([^\]]+))?\])?(?:@([a-z][a-z0-9-]*))?$/;
111
+
112
+ /** A who-path as a JavaScript expression of m (the member), roles, item, read and doc (this document), true when m
113
+ * is one of them: `item` the item a role's @field is of. In a kind (`kind`), `item` is the root's `item` the item a role's @field is of. In a kind (`kind`), `item` is the root's
114
+ * and `at` the item a write is to: an @field is the item's where it has that field (a post's author), else the root's
115
+ * (the board's owners). */
116
+ export function whoCode(path: string, roles: string[], where: string, kind = false): string {
117
+ const names = (v: string) => `(${v} === m || (Array.isArray(${v}) && ${v}.includes(m)))`;
118
+ return path
119
+ .split("|")
120
+ .map((t) => t.trim())
121
+ .map((t) => {
122
+ if (t === "member") return "true";
123
+ if (roles.includes(t)) return `roles.includes(${JSON.stringify(t)})`;
124
+ if (/^@[a-z][a-z0-9-]*$/.test(t)) {
125
+ const f = JSON.stringify(t.slice(1));
126
+ return kind
127
+ ? `((v: any) => v === m || (Array.isArray(v) && v.includes(m)))((at != null && ${f} in at ? at : item)?.[${f}])`
128
+ : names(`item?.[${f}]`);
129
+ }
130
+ const [, of, coll, not, field, value, to] = t.match(TERM) ?? [];
131
+ if (!coll || (not && value)) throw new Error(`${where}: ${t} isn't a who-path`);
132
+ const at = (f: string) => `i[${JSON.stringify(f)}]`;
133
+ const cond = !field
134
+ ? "true"
135
+ : not
136
+ ? `${at(field)} === undefined`
137
+ : `String(${at(field)}) === ${JSON.stringify(value)}`;
138
+ return `(read(${of ? JSON.stringify(of) : "doc"})[${JSON.stringify(coll)}] ?? []).some((i: any) => ${cond} && ${to ? names(at(to)) : "i.id === m"})`;
139
+ })
140
+ .join(" || ");
141
+ }
142
+
143
+ /** What a who-path is asked of, as SQL: the roles (each a who-path), which fields are lists (a table of pairs), the
144
+ * document's own collections, and of a kind, the column its rows are kept with their instance by and its root's
145
+ * table (`$key`, the instance). */
146
+ export type WhoOf = {
147
+ roles: Record<string, string>;
148
+ list: (coll: string, field: string) => boolean;
149
+ own: string[];
150
+ kind?: { column: string; root: string };
151
+ };
152
+ /** A who-path as SQL of a row (`at`, of collection `coll`): true when $member is one of them. The same path its code
153
+ * is written from (whoCode), so the query holds what's asked of an item. A role's @field is its kind's root item's. */
154
+ export function whoSql(path: string, coll: string, at: string, who: WhoOf, where: string, root = false): string {
155
+ const q = (n: string) => `"${n.replaceAll('"', '""')}"`;
156
+ const names = (c: string, a: string, f: string) =>
157
+ who.list(c, f)
158
+ ? `EXISTS (SELECT 1 FROM ${q(`${c}_${f}`)} p WHERE p.${q(c)} = ${a}.id AND p.${q(f)} = $member)`
159
+ : `${a}.${q(f)} = $member`;
160
+ return path
161
+ .split("|")
162
+ .map((t) => t.trim())
163
+ .map((t) => {
164
+ if (t === "member") return "TRUE";
165
+ if (who.roles[t]) return `(${whoSql(who.roles[t]!, coll, at, who, where, true)})`;
166
+ if (/^@[a-z][a-z0-9-]*$/.test(t)) {
167
+ const f = t.slice(1);
168
+ // in a role of a kind, the root item's field
169
+ if (root && who.kind)
170
+ return who.list(who.kind.root, f)
171
+ ? `EXISTS (SELECT 1 FROM ${q(`${who.kind.root}_${f}`)} p WHERE p.${q(who.kind.root)} = $key AND p.${q(f)} = $member)`
172
+ : `(SELECT r.${q(f)} FROM ${q(who.kind.root)} r WHERE r.id = $key) = $member`;
173
+ return names(coll, at, f);
174
+ }
175
+ const [, of, c, not, field, value, to] = t.match(TERM) ?? [];
176
+ if (!c || (not && value)) throw new Error(`${where}: ${t} isn't a who-path`);
177
+ const cond = [
178
+ ...(field
179
+ ? [not ? `i.${q(field)} IS NULL` : `CAST(i.${q(field)} AS TEXT) = '${value!.replaceAll("'", "''")}'`]
180
+ : []),
181
+ // the document's own collection, of a kind: its instance's rows
182
+ ...(!of && who.kind && who.own.includes(c) ? [`i.${q(who.kind.column)} = $key`] : []),
183
+ to ? names(c, "i", to) : "i.id = $member",
184
+ ];
185
+ return `EXISTS (SELECT 1 FROM ${q(c)} i WHERE ${cond.join(" AND ")})`;
186
+ })
187
+ .join(" OR ");
188
+ }
189
+
190
+ /** An op's grant: what it does to which collection, or field; to one of an object's fields (`orders.terms.days`); or
191
+ * to one id of a list (`events.people[]`). */
192
+ export const grantOf = (o: Op) => {
193
+ const [, coll, , field, link] = o.path.split("/");
194
+ if (field === undefined) return `${o.op} ${coll}`;
195
+ if (link === undefined) return `${o.op} ${coll}.${field}`;
196
+ return /^(\d+|-)$/.test(link) ? `${o.op} ${coll}.${field}[]` : `${o.op} ${coll}.${field}.${link}`;
197
+ };
198
+
199
+ /** generated/rules.ts's who: kind (a document per item of another's collection), rolesOf (who someone is, read
200
+ * off the documents), the grants (what each role may do, read off the stories), and sees (what they're sent).
201
+ * `root` is the document holding a kind's root collection. The grants are asked in the write's transaction. */
202
+ export function whoRules(
203
+ doc: string,
204
+ collections: string[],
205
+ defs: Definitions,
206
+ grants: Record<string, string[]>,
207
+ root?: string,
208
+ ) {
209
+ const roles = Object.keys(defs.roles ?? {});
210
+ const where = `${doc}'s definitions`;
211
+ const k = defs.kind;
212
+ let out = k
213
+ ? `\n/** ${doc} is one of many: a document for each of ${root}'s ${k.root} (${doc}:1), its items each kept with it. */\nexport const kind = ${JSON.stringify({ root: k.root, doc: root, column: doc })};\n`
214
+ : `\n/** ${doc} is one document. */\nexport const kind = null;\n`;
215
+ out += `\n/** Each grant, read off the stories: the roles whose stories do it (a member: anyone). An op no story
216
+ * does isn't one. */\nexport const grants: Record<string, string[]> = ${JSON.stringify(grants, null, 2)};\n`;
217
+ if (!roles.length) {
218
+ // no roles: anyone may do what the stories do
219
+ out += `\n/** Who someone is to ${doc}: a member, as it names no roles. */\nexport const rolesOf = (_m: number, _read: (doc: string) => any, _doc: string, _at?: any): string[] => [];\n`;
220
+ return out + sight(doc, collections, defs, roles, where);
221
+ }
222
+ // a role's @field is the item's own a write is to (`at`: the supplier of this supply); in a kind, where that item
223
+ // has no such field, the root item's (of another document, or the instance's own row: order:7's order): a post's
224
+ // author, the board's owners; an instance's key is before its window (trip:1?week=2026-11-02)
225
+ const item = k
226
+ ? `(read(${JSON.stringify(root)})[${JSON.stringify(k.root)}] ?? []).find((i: any) => i.id === Number(doc.split(/[:?]/)[1]))`
227
+ : "at";
228
+ out += `\n/** Who someone is to ${doc} (and to an item of it, \`at\`), beyond a member: each role whose who-path finds them. */\n`;
229
+ out += `export function rolesOf(m: number, read: (doc: string) => any, doc: string, at?: any): string[] {\n const item: any = ${item};\n return [${roles.map((r) => `\n ${whoCode(defs.roles![r]!, [], where, !!k)} ? ${JSON.stringify(r)} : ""`).join(",")},\n ].filter(Boolean);\n}\n`;
230
+ return out + sight(doc, collections, defs, roles, where);
231
+ }
232
+
233
+ /** generated/rules.ts's sees: what each person is sent, from owns; null where there is none. */
234
+ function sight(doc: string, collections: string[], defs: Definitions, roles: string[], where: string) {
235
+ const owns = defs.owns;
236
+ const by = typeof owns === "string" ? Object.fromEntries(collections.map((c) => [c, owns])) : (owns ?? {});
237
+ for (const c of Object.keys(by))
238
+ if (!collections.includes(c)) throw new Error(`${where}: owns names ${c}, which isn't a collection of ${doc}`);
239
+ return owns
240
+ ? `\n/** Each person is sent only what they see of ${doc} (its definitions' owns): its query, by its member. */\nexport const sees = true;\n`
241
+ : `\n/** What someone sees: all of ${doc}, as it has no owns. */\nexport const sees = null;\n`;
242
+ }
243
+
244
+ /** Each does's grants, given to its teller's named role (or to member, where they have none), on the seeds. */
245
+ export function grantsOf<S extends { story: string; as?: string; does?: { doc: string; ops: Op[] } }>(
246
+ stories: S[],
247
+ rolesOf: (m: number | null, doc: string, o?: Op, s?: S) => string[],
248
+ memberOf: (name: string) => number | undefined,
249
+ keyOf: (o: Op, doc: string, s?: S) => string = grantOf,
250
+ ) {
251
+ const grants: Record<string, string[]> = {};
252
+ for (const s of stories) {
253
+ if (!s.does) continue;
254
+ // an anonymous story's ops are anyone's, but a guest's (the item's own code holder)
255
+ const m = s.as === undefined ? null : (memberOf(s.as) ?? null);
256
+ for (const o of s.does.ops) {
257
+ // who its teller is to the item each op is to
258
+ const named = rolesOf(m, s.does.doc, o, s);
259
+ // two roles: the one its cast names it for (the buyer, as Peter), or it can't say which it grants
260
+ const role = named.length > 1 && s.as && named.includes(s.as) ? s.as : named[0];
261
+ if (named.length > 1 && role !== s.as)
262
+ throw new Error(`"${s.story}": ${s.as} is ${named.join(" and ")}, so it can't say which it grants`);
263
+ const g = (grants[keyOf(o, s.does.doc, s)] ??= []);
264
+ if (!g.includes(role ?? "member")) g.push(role ?? "member");
265
+ }
266
+ }
267
+ return Object.fromEntries(Object.entries(grants).sort(([a], [b]) => a.localeCompare(b)));
268
+ }
package/src/demo.css ADDED
@@ -0,0 +1,29 @@
1
+ /* <joined-demo>'s look, in its shadow (with joined's parts): a quiet line in a well, a link a person, who you are
2
+ filled in ink. */
3
+ :host {
4
+ display: block;
5
+ }
6
+ :host([hidden]) {
7
+ display: none;
8
+ }
9
+ nav {
10
+ display: flex;
11
+ flex-wrap: wrap;
12
+ align-items: baseline;
13
+ gap: var(--space-4) var(--space-12);
14
+ padding: var(--space-8) var(--space-12);
15
+ border-radius: 4px;
16
+ background: var(--well);
17
+ color: var(--ink);
18
+ font: var(--font-row);
19
+ }
20
+ a {
21
+ padding: 0 var(--space-8);
22
+ border-radius: 99px;
23
+ color: inherit;
24
+ }
25
+ a[aria-current] {
26
+ background: var(--ink);
27
+ color: var(--card);
28
+ text-decoration: none;
29
+ }
package/src/demo.ts ADDED
@@ -0,0 +1,42 @@
1
+ // <joined-demo>: on a demo (a server started with JOINED_DEMO=1, as an app's `bun run dev` starts it), a line to be
2
+ // anyone in the group, the BDFL first, each a link the server signs this browser in by (/as/<id>), who you are filled;
3
+ // so being Nora or Nick always has a way back. Not a demo (the live site, a project with no members): it stays hidden.
4
+ // An app's home page carries one, under its head; it needs neither the group's panel nor anything of the page's.
5
+
6
+ import { PARTS } from "./parts.ts";
7
+ import STYLE from "./demo.css" with { type: "text" };
8
+
9
+ const SHEET = new CSSStyleSheet();
10
+ SHEET.replaceSync(STYLE);
11
+
12
+ class JoinedDemo extends HTMLElement {
13
+ #root = this.attachShadow({ mode: "open" });
14
+ connectedCallback() {
15
+ if (this.#root.childNodes.length) return;
16
+ this.hidden = true;
17
+ this.#root.adoptedStyleSheets = [PARTS, SHEET];
18
+ fetch("/as")
19
+ .then((r) => (r.ok ? (r.json() as Promise<{ me: number | null; people: Person[] }>) : null))
20
+ .then((demo) => demo && this.#draw(demo.people, demo.me));
21
+ }
22
+ /** The line: a link for each person, who you are filled. */
23
+ #draw(people: Person[], me: number | null) {
24
+ const line = Object.assign(document.createElement("nav"), { ariaLabel: "Be someone else" });
25
+ line.append(
26
+ "A demo, on this machine. Be:",
27
+ ...people.map((p) => {
28
+ const a = Object.assign(document.createElement("a"), {
29
+ href: `/as/${p.id}`,
30
+ textContent: p.bdfl ? `${p.name} (the BDFL)` : p.name,
31
+ });
32
+ a.toggleAttribute("aria-current", p.id === me);
33
+ return a;
34
+ }),
35
+ );
36
+ this.#root.append(line);
37
+ this.hidden = false;
38
+ }
39
+ }
40
+ type Person = { id: number; name: string; bdfl: boolean };
41
+
42
+ customElements.define("joined-demo", JoinedDemo);
package/src/draft.ts ADDED
@@ -0,0 +1,69 @@
1
+ // A draft: a form holding changes to one document element until Save (its button `go`), put back by
2
+ // Never mind (`never`). Each named control is held against the value the document has, so a change
3
+ // made elsewhere fills the controls not being changed here, and keeps the ones that are. The form
4
+ // says whether it's kept in its note, and why, if a write was refused. A page that wants it imports
5
+ // this; what its changes are as element ops is the page's own.
6
+
7
+ type Value = string | boolean;
8
+ type Control = HTMLInputElement;
9
+
10
+ /** Every draft on the page. */
11
+ const drafts = new Set<Draft>();
12
+ /** Whether a draft holds changes in a form that's shown: what's typed and not kept, or what its form says isn't (a
13
+ * choice that's no control: the people chosen for an event), which a reload would lose (a page told another build
14
+ * waits for none, src/page.ts). */
15
+ export const unsaved = () =>
16
+ [...drafts].some((d) => d.form.checkVisibility() && (d.changed || Object.keys(d.edits()).length > 0));
17
+
18
+ export class Draft {
19
+ /** Whether its form last said it holds changes not kept. */
20
+ changed = false;
21
+ constructor(
22
+ readonly form: HTMLFormElement,
23
+ readonly note: HTMLElement,
24
+ ) {
25
+ drafts.add(this);
26
+ }
27
+ /** The form as the document has it: afresh, every control set; as it changes elsewhere, only those
28
+ * not being changed here. */
29
+ fill(saved: Record<string, Value>, afresh = false) {
30
+ for (const [name, value] of Object.entries(saved)) {
31
+ const control = this.#control(name),
32
+ was = control.dataset.saved;
33
+ control.dataset.saved = String(value);
34
+ if (afresh || read(control) === was) write(control, value);
35
+ }
36
+ }
37
+ /** What the form has changed, by name: a tick as a boolean, the rest as strings. */
38
+ edits() {
39
+ const changed: Record<string, Value> = {};
40
+ for (const control of this.form.querySelectorAll<Control>("[data-saved]"))
41
+ if (read(control) !== control.dataset.saved)
42
+ changed[control.name] = control.type === "checkbox" ? control.checked : control.value;
43
+ return changed;
44
+ }
45
+ /** What the form says: "kept", nothing to save or put back; "changed", not kept yet; "new", not
46
+ * kept against anything. A refusal is said until it's taken back. */
47
+ show(state: "kept" | "changed" | "new") {
48
+ this.changed = state === "changed";
49
+ for (const name of ["go", "never"])
50
+ (this.form.elements.namedItem(name) as HTMLButtonElement).disabled = state === "kept";
51
+ if (!this.note.classList.contains("refused"))
52
+ this.note.textContent = state === "changed" ? "Not kept yet: Save keeps it, Never mind puts it back." : "";
53
+ }
54
+ /** Why a write was refused, said in the note; nothing takes it back. */
55
+ refuse(why: string) {
56
+ this.note.classList.toggle("refused", !!why);
57
+ this.note.textContent = why;
58
+ }
59
+ /** The drawer (or modal) it's in: its × (`close`) and Escape close it; Never mind (`never`) puts back what it holds. */
60
+ inside(box: HTMLElement, close: () => void, never: () => void) {
61
+ (this.form.elements.namedItem("close") as HTMLButtonElement).addEventListener("click", close);
62
+ (this.form.elements.namedItem("never") as HTMLButtonElement).addEventListener("click", never);
63
+ box.addEventListener("keydown", (e) => (e as KeyboardEvent).key === "Escape" && !e.defaultPrevented && close());
64
+ }
65
+ #control = (name: string) => this.form.elements.namedItem(name) as Control;
66
+ }
67
+
68
+ const read = (c: Control) => (c.type === "checkbox" ? String(c.checked) : c.value);
69
+ const write = (c: Control, v: Value) => (c.type === "checkbox" ? (c.checked = v === true) : (c.value = String(v)));