@blueshed/joined 0.2.2 → 0.3.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.
Files changed (237) hide show
  1. package/.claude/skills/joined/SKILL.md +9 -1
  2. package/.claude/skills/joined/reference.md +316 -63
  3. package/.claude/skills/joined-door/SKILL.md +57 -0
  4. package/README.md +19 -50
  5. package/docs/design.md +158 -37
  6. package/docs/introduction.md +27 -27
  7. package/docs/light-and-shadow.svg +2 -1
  8. package/docs/links-and-trust.svg +1 -0
  9. package/docs/stories-to-code.svg +2 -1
  10. package/docs/wire-op.svg +1 -0
  11. package/examples/README.md +20 -0
  12. package/examples/account/account.json +1 -0
  13. package/examples/account/derive.ts +41 -0
  14. package/examples/account/generated/elements.ts +52 -0
  15. package/examples/account/generated/rows.ts +22 -0
  16. package/examples/account/generated/rules.ts +32 -0
  17. package/examples/account/routes.ts +5 -0
  18. package/examples/app.css +11 -1
  19. package/examples/cafe/books.html +2 -2
  20. package/examples/cafe/cafe.css +96 -85
  21. package/examples/cafe/generated/books.rows.ts +22 -0
  22. package/examples/cafe/generated/books.rules.ts +3 -2
  23. package/examples/cafe/generated/kitchen.rows.ts +27 -0
  24. package/examples/cafe/generated/kitchen.rules.ts +11 -1
  25. package/examples/cafe/generated/menu.rows.ts +22 -0
  26. package/examples/cafe/generated/menu.rules.ts +3 -2
  27. package/examples/cafe/generated/order.rows.ts +27 -0
  28. package/examples/cafe/generated/order.rules.ts +11 -1
  29. package/examples/cafe/generated/staff.elements.ts +7 -2
  30. package/examples/cafe/generated/staff.rows.ts +22 -0
  31. package/examples/cafe/generated/staff.rules.ts +9 -1
  32. package/examples/cafe/generated/supplies.queries.ts +4 -2
  33. package/examples/cafe/generated/supplies.rows.ts +22 -0
  34. package/examples/cafe/generated/supplies.rules.ts +17 -1
  35. package/examples/cafe/generated/till.queries.ts +2 -0
  36. package/examples/cafe/generated/till.rows.ts +27 -0
  37. package/examples/cafe/generated/till.rules.ts +11 -1
  38. package/examples/cafe/index.html +14 -15
  39. package/examples/cafe/kitchen.css +11 -19
  40. package/examples/cafe/kitchen.ts +7 -3
  41. package/examples/cafe/look.ts +30 -0
  42. package/examples/cafe/menu.html +10 -26
  43. package/examples/cafe/menu.ts +8 -80
  44. package/examples/cafe/migrations/002-guard-supplies.ts +15 -0
  45. package/examples/cafe/order.html +8 -7
  46. package/examples/cafe/order.ts +6 -2
  47. package/examples/cafe/routes.ts +0 -1
  48. package/examples/cafe/staff.json +5 -0
  49. package/examples/cafe/stories.jsonl +6 -2
  50. package/examples/cafe/supplies.html +35 -34
  51. package/examples/cafe/supplies.ts +59 -39
  52. package/examples/cafe/till.ts +19 -11
  53. package/examples/cafe/views.ts +7 -5
  54. package/examples/chart/arches.ts +150 -0
  55. package/examples/chart/chart.css +11 -0
  56. package/examples/chart/chart.json +87 -0
  57. package/examples/chart/generated/elements.ts +105 -0
  58. package/examples/chart/generated/queries.ts +36 -0
  59. package/examples/chart/generated/rows.ts +27 -0
  60. package/examples/chart/generated/rules.ts +111 -0
  61. package/examples/chart/grid.ts +118 -0
  62. package/examples/chart/index.html +78 -0
  63. package/examples/chart/migrations/001-chart.ts +6 -0
  64. package/examples/chart/migrations/002-create-conditions.ts +6 -0
  65. package/examples/chart/page.ts +20 -0
  66. package/examples/chart/routes.ts +7 -0
  67. package/examples/chart/stories.jsonl +14 -0
  68. package/examples/chart/teeth.ts +57 -0
  69. package/examples/desk/desk.css +6 -10
  70. package/examples/desk/generated/elements.ts +1 -1
  71. package/examples/desk/generated/rows.ts +22 -0
  72. package/examples/desk/generated/rules.ts +34 -1
  73. package/examples/desk/index.html +10 -3
  74. package/examples/desk/lanes.css +21 -19
  75. package/examples/desk/routes.ts +0 -2
  76. package/examples/desk/stories.jsonl +4 -3
  77. package/examples/desk/tally.css +1 -1
  78. package/examples/desk/thumbs.ts +2 -2
  79. package/examples/docs.ts +7 -4
  80. package/examples/hello/generated/elements.ts +2 -2
  81. package/examples/hello/generated/rules.ts +3 -2
  82. package/examples/hello/hello.css +2 -4
  83. package/examples/hello/index.html +10 -3
  84. package/examples/hello/page.ts +4 -4
  85. package/examples/index.html +20 -12
  86. package/examples/notes/generated/elements.ts +112 -0
  87. package/examples/notes/generated/queries.ts +42 -0
  88. package/examples/notes/generated/rows.ts +27 -0
  89. package/examples/notes/generated/rules.ts +133 -0
  90. package/examples/notes/index.html +32 -0
  91. package/examples/notes/look.ts +41 -0
  92. package/examples/notes/migrations/001-notes.ts +7 -0
  93. package/examples/notes/notes.json +78 -0
  94. package/examples/notes/page.ts +21 -0
  95. package/examples/notes/routes.ts +6 -0
  96. package/examples/notes/stories.jsonl +24 -0
  97. package/examples/page.ts +9 -2
  98. package/examples/people/generated/rows.ts +22 -0
  99. package/examples/people/generated/rules.ts +11 -1
  100. package/examples/people/links.ts +8 -9
  101. package/examples/people/people.css +1 -1
  102. package/examples/people/routes.ts +1 -3
  103. package/examples/people/stories.jsonl +1 -0
  104. package/examples/people/view.ts +5 -2
  105. package/examples/trip/generated/elements.ts +5 -3
  106. package/examples/trip/generated/queries.ts +12 -1
  107. package/examples/trip/generated/rows.ts +27 -0
  108. package/examples/trip/generated/rules.ts +56 -2
  109. package/examples/trip/index.html +58 -12
  110. package/examples/trip/line.css +186 -0
  111. package/examples/trip/line.ts +51 -40
  112. package/examples/trip/page.ts +65 -71
  113. package/examples/trip/routes.ts +9 -5
  114. package/examples/trip/stays.css +13 -13
  115. package/examples/trip/stays.ts +15 -10
  116. package/examples/trip/stories.jsonl +2 -0
  117. package/examples/trip/told.ts +3 -26
  118. package/examples/trip/trip.css +16 -0
  119. package/examples/trips/generated/elements.ts +1 -1
  120. package/examples/trips/generated/rows.ts +22 -0
  121. package/examples/trips/generated/rules.ts +9 -1
  122. package/examples/trips/index.html +10 -10
  123. package/examples/trips/map.ts +3 -14
  124. package/examples/trips/new-trip.ts +14 -16
  125. package/examples/trips/trips.css +24 -23
  126. package/examples/weeks/generated/elements.ts +1 -1
  127. package/examples/weeks/generated/queries.ts +11 -0
  128. package/examples/weeks/generated/rows.ts +22 -0
  129. package/examples/weeks/generated/rules.ts +11 -5
  130. package/examples/weeks/routes.ts +3 -2
  131. package/examples/weeks/stories.jsonl +1 -0
  132. package/examples/welcome.css +43 -33
  133. package/package.json +8 -2
  134. package/src/attachment.ts +17 -2
  135. package/src/awesome.ts +103 -0
  136. package/src/board.css +76 -0
  137. package/src/board.ts +173 -0
  138. package/src/cli.ts +28 -3
  139. package/src/compile.ts +821 -109
  140. package/src/definitions.ts +113 -27
  141. package/src/demo.css +2 -5
  142. package/src/draft.ts +48 -27
  143. package/src/drawer.ts +248 -0
  144. package/src/engine.ts +253 -170
  145. package/src/fields.ts +341 -0
  146. package/src/file-value.ts +12 -2
  147. package/src/files.ts +113 -33
  148. package/src/ground.ts +110 -0
  149. package/src/icon-names.ts +19 -2
  150. package/src/keep.ts +16 -3
  151. package/src/kit.ts +51 -0
  152. package/src/link.ts +3 -3
  153. package/src/map.css +140 -0
  154. package/src/map.ts +200 -0
  155. package/src/mcp.ts +129 -24
  156. package/src/members/demo.ts +13 -12
  157. package/src/members/generated/rows.ts +22 -0
  158. package/src/members/generated/rules.ts +3 -2
  159. package/src/members/group.ts +105 -55
  160. package/src/members/keys.ts +95 -79
  161. package/src/members/panel.css +40 -35
  162. package/src/members/panel.ts +58 -37
  163. package/src/migrate.ts +178 -125
  164. package/src/migrations/005-change-kept.ts +7 -0
  165. package/src/month.css +129 -0
  166. package/src/month.ts +179 -0
  167. package/src/ops.ts +40 -1
  168. package/src/page.css +11 -2
  169. package/src/page.ts +68 -17
  170. package/src/parts.css +614 -230
  171. package/src/parts.ts +259 -36
  172. package/src/plane.css +61 -0
  173. package/src/plane.ts +153 -0
  174. package/src/postgres-server.ts +60 -0
  175. package/src/reading.css +73 -0
  176. package/src/reading.ts +238 -0
  177. package/src/rows.css +289 -0
  178. package/src/rows.ts +864 -0
  179. package/src/search.css +80 -0
  180. package/src/search.ts +130 -0
  181. package/src/server.ts +355 -137
  182. package/src/settings.ts +9 -4
  183. package/src/sheet.ts +116 -0
  184. package/src/skill.ts +40 -0
  185. package/src/slots.css +90 -0
  186. package/src/slots.ts +213 -0
  187. package/src/specs.ts +38 -0
  188. package/src/starter.ts +19 -2
  189. package/src/store.ts +224 -161
  190. package/src/stories.ts +43 -11
  191. package/src/surface.ts +164 -0
  192. package/src/time.ts +2 -0
  193. package/src/timeline.css +111 -0
  194. package/src/timeline.ts +235 -0
  195. package/src/toast.ts +21 -48
  196. package/src/together.ts +15 -24
  197. package/src/tokens.css +89 -3
  198. package/src/tokens.json +287 -7
  199. package/src/tool.ts +210 -0
  200. package/src/upkeep.ts +47 -22
  201. package/src/views.css +43 -0
  202. package/src/views.ts +111 -0
  203. package/src/week.css +150 -0
  204. package/src/week.ts +254 -0
  205. package/src/write.ts +326 -78
  206. package/starter/CHANGELOG.md +8 -0
  207. package/starter/CLAUDE.md +9 -3
  208. package/starter/bunfig +5 -0
  209. package/starter/githooks/pre-commit +13 -0
  210. package/starter/prettierignore +5 -0
  211. package/starter/tests/hello.test.ts +2 -0
  212. package/starter/tests/records.test.ts +40 -0
  213. package/starter/todo.jsonl +0 -0
  214. package/docs/learning.md +0 -76
  215. package/examples/cafe/trust.ts +0 -20
  216. package/examples/desk/trust.ts +0 -24
  217. package/examples/people/trust.ts +0 -14
  218. package/examples/trip/answer-rows.ts +0 -70
  219. package/examples/trip/bar.ts +0 -89
  220. package/examples/trip/calendar-map.css +0 -711
  221. package/examples/trip/calendar-map.ts +0 -249
  222. package/examples/trip/drags.ts +0 -143
  223. package/examples/trip/drawer.ts +0 -250
  224. package/examples/trip/event-form.ts +0 -200
  225. package/examples/trip/holding.ts +0 -61
  226. package/examples/trip/map.ts +0 -234
  227. package/examples/trip/month.ts +0 -87
  228. package/examples/trip/other-weeks.ts +0 -67
  229. package/examples/trip/peek.ts +0 -71
  230. package/examples/trip/people-chips.ts +0 -74
  231. package/examples/trip/shell.ts +0 -39
  232. package/examples/trip/trust.ts +0 -51
  233. package/examples/trip/week.ts +0 -237
  234. package/examples/trip/writes.ts +0 -91
  235. package/examples/trip/zones.ts +0 -16
  236. package/examples/weeks/derive.ts +0 -23
  237. package/src/scrim.ts +0 -81
package/src/month.ts ADDED
@@ -0,0 +1,179 @@
1
+ // <j-month for="plans" when="when"> (joined/kit): a collection read by its days, a month of them. Each day its number
2
+ // and the items that are that day's, each its time and its words, the way into it; a day tapped is marked, and under
3
+ // the month its items are listed, with Add on that day where this person may add (the sheet offered that day, still
4
+ // asked). An item dragged to another day keeps its hour (where its `may` lets it move). On a phone a day's items are
5
+ // a mark, and the marked day's are listed under the month, each a finger tall. The month shown is kept in the address.
6
+ // Small (`small`, what a time in a row pops), each day's items are a mark alone, and the month shown is its own `day`.
7
+
8
+ import { icon, iconButton } from "./icons.ts";
9
+ import { keep, type Row } from "./keep.ts";
10
+ import { el, Reading } from "./reading.ts";
11
+ import type { Item } from "./rows.ts";
12
+ import { DAY, dated, dayOf, firstOf, gridOf, local, now, wall } from "./time.ts";
13
+ import STYLE from "./month.css" with { type: "text" };
14
+
15
+ const SHEET = new CSSStyleSheet();
16
+ SHEET.replaceSync(STYLE);
17
+ const LONG: Intl.DateTimeFormatOptions = { weekday: "long", day: "numeric", month: "long" };
18
+
19
+ export class JMonth extends Reading {
20
+ static override sheets = [SHEET];
21
+ static observedAttributes = ["day", "small"];
22
+ #name = el("h3");
23
+ #grid = el("div", { className: "days", role: "grid" });
24
+ #under = el("section", { className: "that-day" });
25
+ #heading = el("h4");
26
+ #adding = el("button", { type: "button", className: "act small adding", part: "add" });
27
+ #list = el("ul");
28
+ #days = new Map<string, { shows: string; el: HTMLElement }>();
29
+ #rows = new Map<string, { shows: string; el: HTMLElement }>();
30
+
31
+ attributeChangedCallback() {
32
+ this.again();
33
+ }
34
+ get small() {
35
+ return this.hasAttribute("small");
36
+ }
37
+ override get day() {
38
+ const own = this.getAttribute("day");
39
+ return this.small && own ? dayOf(own) : super.day;
40
+ }
41
+ /** Small, the month shown is its own, never the address's. */
42
+ override set day(d: number) {
43
+ if (this.small) this.setAttribute("day", local(d * DAY).slice(0, 10));
44
+ else super.day = d;
45
+ }
46
+ protected made() {
47
+ const names = el("div", { className: "names", ariaHidden: "true" });
48
+ // Monday first, as the grid is
49
+ for (let i = 0; i < 7; i++) names.append(el("span", {}, dated(4 + i, { weekday: "short" })));
50
+ const step = (by: number) => () => {
51
+ const d = new Date(this.day * DAY),
52
+ to = new Date(Date.UTC(d.getUTCFullYear(), d.getUTCMonth() + by, 1)),
53
+ last = new Date(Date.UTC(to.getUTCFullYear(), to.getUTCMonth() + 1, 0)).getUTCDate();
54
+ to.setUTCDate(Math.min(d.getUTCDate(), last));
55
+ this.day = to.getTime() / DAY;
56
+ };
57
+ const today = el("button", { type: "button", className: "act small today", part: "today" }, "Today");
58
+ today.addEventListener("click", () => (this.day = dayOf(now())));
59
+ this.#adding.addEventListener("click", () => {
60
+ const f = this.when;
61
+ if (f) this.add({ [f.name]: `${local(this.day * DAY).slice(0, 10)}T09:00:00` });
62
+ });
63
+ this.#under.append(el("header", {}, this.#heading, this.#adding), this.#list);
64
+ this.root.append(
65
+ el(
66
+ "header",
67
+ { className: "head", part: "head" },
68
+ this.#name,
69
+ today,
70
+ iconButton("back", "The month before", step(-1)),
71
+ iconButton("on", "The month after", step(1)),
72
+ ),
73
+ names,
74
+ this.#grid,
75
+ this.#under,
76
+ );
77
+ }
78
+ protected draw() {
79
+ const f = this.when;
80
+ if (!f) return;
81
+ const day = this.day,
82
+ first = firstOf(day),
83
+ today = dayOf(now()),
84
+ month = new Date(first * DAY).getUTCMonth();
85
+ this.#name.textContent = dated(first, { month: "long", year: "numeric" });
86
+ const by = new Map<number, Item[]>();
87
+ for (const it of this.items()) {
88
+ const w = it.data[f.name];
89
+ if (typeof w !== "string") continue;
90
+ const d = dayOf(w);
91
+ (by.get(d) ?? by.set(d, []).get(d)!).push(it);
92
+ }
93
+ for (const its of by.values()) its.sort((a, b) => String(a.data[f.name]).localeCompare(String(b.data[f.name])));
94
+ const moves = (it: Item) => this.may(it, f.name);
95
+ keep(
96
+ this.#grid,
97
+ this.#days,
98
+ Array.from({ length: 42 }, (_, i) => gridOf(day) + i).map((d): Row => {
99
+ const its = by.get(d) ?? [],
100
+ away = new Date(d * DAY).getUTCMonth() !== month;
101
+ return {
102
+ key: String(d),
103
+ shows: JSON.stringify([
104
+ away,
105
+ this.small,
106
+ d === day,
107
+ d === today,
108
+ its.map((it) => [it.key, this.named(it), it.data[f.name], this.opened(it), moves(it)]),
109
+ ]),
110
+ make: () => {
111
+ const cell = el("div", { className: "day", part: "day", role: "gridcell" });
112
+ cell.dataset.day = String(d);
113
+ cell.toggleAttribute("away", away);
114
+ const n = el("button", {
115
+ type: "button",
116
+ className: "n",
117
+ textContent: String(new Date(d * DAY).getUTCDate()),
118
+ ariaLabel: `${dated(d, LONG)}${its.length ? `, ${its.length}` : ""}`,
119
+ });
120
+ if (d === day) n.ariaCurrent = "date";
121
+ n.toggleAttribute("today", d === today);
122
+ n.addEventListener("click", () => (this.day = d));
123
+ cell.append(n);
124
+ if (its.length) cell.append(el("i", { className: "mark" }));
125
+ if (!this.small) cell.append(...its.map((it) => this.#item(it, f.name)));
126
+ return cell;
127
+ },
128
+ };
129
+ }),
130
+ );
131
+ // the marked day's items, under the month: each the way into it, and Add on that day
132
+ this.#under.hidden = this.small;
133
+ this.#heading.textContent = dated(day, LONG);
134
+ this.#adding.hidden = !this.adds;
135
+ if (!this.#adding.childElementCount) this.#adding.append(icon("add"), "Add");
136
+ this.#adding.ariaLabel = `Add on ${dated(day, LONG)}`;
137
+ keep(
138
+ this.#list,
139
+ this.#rows,
140
+ (by.get(day) ?? []).map((it): Row => ({
141
+ key: String(it.key),
142
+ shows: JSON.stringify([this.named(it), it.data[f.name], this.opened(it)]),
143
+ make: () => el("li", {}, this.#item(it, f.name)),
144
+ })),
145
+ );
146
+ this.#list.ariaLabel = dated(day, LONG);
147
+ }
148
+ /** An item on its day: its time (where it has one) and its words; dragged to another day, moved there. */
149
+ #item(it: Item, field: string) {
150
+ const w = String(it.data[field]),
151
+ at = w.slice(11, 16),
152
+ b = el(
153
+ "button",
154
+ { type: "button", className: "item", part: "item", title: this.named(it) },
155
+ ...(at && at !== "00:00" ? [el("span", { className: "at" }, at), " "] : []),
156
+ this.named(it),
157
+ );
158
+ const cellAt = (e: PointerEvent) =>
159
+ [...this.#grid.children].find((c) => {
160
+ const r = c.getBoundingClientRect();
161
+ return e.clientX >= r.left && e.clientX < r.right && e.clientY >= r.top && e.clientY < r.bottom;
162
+ }) as HTMLElement | undefined;
163
+ this.press(
164
+ b,
165
+ it,
166
+ this.may(it, field)
167
+ ? (dx, dy, done, e) => {
168
+ b.style.translate = done ? "" : `${dx}px ${dy}px`;
169
+ for (const c of this.#grid.children) c.toggleAttribute("over", !done && !!e && c === cellAt(e));
170
+ const to = done && e && cellAt(e);
171
+ if (!to) return;
172
+ const d = Number(to.dataset.day);
173
+ if (d !== dayOf(w)) void this.change(it, { [field]: local(d * DAY + (wall(w) - dayOf(w) * DAY)) });
174
+ }
175
+ : undefined,
176
+ );
177
+ return b;
178
+ }
179
+ }
package/src/ops.ts CHANGED
@@ -3,6 +3,13 @@
3
3
  // /events/3/people/2 is the link to 2 (and /events/3/people/- one added).
4
4
 
5
5
  export type Op = { op: "add" | "replace" | "remove"; path: string; value?: any };
6
+ /** Whether what's sent is an op: an add, a replace or a remove, at a path (its value is the write's to refuse). */
7
+ export const isOp = (o: any): o is Op =>
8
+ typeof o === "object" && o !== null && ["add", "replace", "remove"].includes(o.op) && typeof o.path === "string";
9
+ /** What's said of ops that aren't. */
10
+ export const OPS = "ops are each an add, a replace or a remove, at a path";
11
+ /** The most a message may be, in bytes: a page's, over its socket, or an agent's, at the MCP door. */
12
+ export const MESSAGE = 1 << 20;
6
13
 
7
14
  /** A field's type, read off what the stories give it: a string, or one read off its form (a date-time, a time
8
15
  * zone, an instant, a duration, a place); a boolean; a number, an integer or a decimal; or a list of ids. */
@@ -30,7 +37,7 @@ export const attribute = (type: Type | undefined, value: unknown): string | null
30
37
  ? value === true
31
38
  ? ""
32
39
  : null
33
- : value === undefined || value === null
40
+ : value === undefined || value === null || (Array.isArray(value) && !value.length)
34
41
  ? null
35
42
  : Array.isArray(value)
36
43
  ? value.join(" ")
@@ -59,6 +66,38 @@ export const NTH = /^\$([1-9]\d*)$/;
59
66
  /** Whether a part's document is named by an add before it in its write (`board:$1`): no page holds it yet. */
60
67
  export const madeBy = (doc: string) => NTH.test(docParts(doc).key ?? "");
61
68
 
69
+ /** A document's collections as a write's "$n" reads them: each one's links (a field, what it's to), and whether it's
70
+ * the document's own fields (hello's). */
71
+ export type Linked = Record<string, { links: Record<string, unknown>; own?: boolean }>;
72
+ /** An op's value with each "$n" where a link is made what `nth` says (the id the write's nth add was given): in an
73
+ * add's item, a link's value; a link replaced, or an id added to a list, its value. Anywhere else "$n" is words, and
74
+ * kept as it's given. */
75
+ export function linksNamed(o: Op, collections: Linked | undefined, nth: (n: number, v: string) => unknown): Op {
76
+ if (!("value" in o)) return o;
77
+ // (a document's own field, /greeting, is its own collection's one item's)
78
+ const own = Object.entries(collections ?? {}).find(([, c]) => c.own)?.[0];
79
+ const { coll, key, field, link } = parts(own && /^\/[a-z][a-z0-9-]*$/.test(o.path) ? `/${own}/1${o.path}` : o.path);
80
+ const links = collections?.[coll]?.links ?? {};
81
+ const read = (v: unknown): unknown => {
82
+ const n = typeof v === "string" ? NTH.exec(v)?.[1] : undefined;
83
+ return n ? nth(Number(n), v as string) : Array.isArray(v) ? v.map(read) : v;
84
+ };
85
+ const isObject = (v: unknown): v is Record<string, unknown> => !!v && typeof v === "object" && !Array.isArray(v);
86
+ // an object's fields are each named for both (terms.days)
87
+ const fields = (v: Record<string, unknown>, at = ""): Record<string, unknown> =>
88
+ Object.fromEntries(
89
+ Object.entries(v).map(([f, x]) => [
90
+ f,
91
+ `${at}${f}` in links ? read(x) : !at && isObject(x) ? fields(x, `${f}.`) : x,
92
+ ]),
93
+ );
94
+ // an add's item, each of its fields; else a field, or an id added to its list
95
+ if (field === undefined) return key === "-" && isObject(o.value) ? { ...o, value: fields(o.value) } : o;
96
+ const named = link !== undefined && /^[a-z]/.test(link) ? `${field}.${link}` : field;
97
+ if (named in links) return { ...o, value: read(o.value) };
98
+ return isObject(o.value) && link === undefined ? { ...o, value: fields(o.value, `${field}.`) } : o;
99
+ }
100
+
62
101
  /** 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. */
63
102
  export function parts(path: string) {
64
103
  const [, coll = "", key = "", field, link, ...more] = path.split("/");
package/src/page.css CHANGED
@@ -12,7 +12,8 @@
12
12
  font: var(--font-body);
13
13
  max-width: 72rem;
14
14
  margin: 0 auto;
15
- padding: 0 var(--gutter) var(--space-48);
15
+ /* room at its top, whatever it opens with: the brand, a band, or nothing of joined's */
16
+ padding: var(--space-20) var(--gutter) var(--space-48);
16
17
  color: var(--ink);
17
18
  background: var(--paper);
18
19
  }
@@ -25,7 +26,7 @@
25
26
  display: inline-flex;
26
27
  align-items: center;
27
28
  gap: var(--space-8);
28
- margin: var(--space-20) 0 var(--space-24);
29
+ margin: 0 0 var(--space-24);
29
30
  color: var(--ink);
30
31
  font: var(--font-name);
31
32
  text-decoration: none;
@@ -70,6 +71,14 @@
70
71
  opacity: 0.6;
71
72
  }
72
73
 
74
+ /* a drawer docked at the screen's side (the compiled rows say so): the page keeps clear of it, so nothing it's
75
+ about is under it. On a phone it rises from the foot, over the page */
76
+ @media (min-width: 40rem) {
77
+ body[data-docked] {
78
+ margin-inline-end: var(--size-panel);
79
+ }
80
+ }
81
+
73
82
  .bar {
74
83
  display: flex;
75
84
  flex-wrap: wrap;
package/src/page.ts CHANGED
@@ -93,8 +93,9 @@ const socket = {
93
93
  };
94
94
  /** Why a stranger's page is refused a document, the server's words. */
95
95
  const SIGN_IN = "sign in with your link first";
96
- /** How long a socket asked, on waking, has to answer before it's taken as gone. */
97
- const ANSWER = 5000;
96
+ /** How long a socket asked, on waking, has to answer before it's taken as gone: five seconds, unless the page's
97
+ * root says otherwise (`data-answer`, in milliseconds: a test's, so it needn't wait five seconds to see it). */
98
+ const answerIn = () => Number(document.documentElement.dataset.answer) || 5000;
98
99
  const isOpen = () => socket.ws?.readyState === WebSocket.OPEN;
99
100
 
100
101
  // holding a document
@@ -174,10 +175,11 @@ document.addEventListener("ops", (e) => {
174
175
  if (writes && (!writes.length || off)) return answer?.(off ? `${off.doc} isn't on this page` : "a write of nothing");
175
176
  if (!isOpen() && (writes ?? [{ ops }]).every((p) => adds(p.ops)) && (writes || docs.has(doc)))
176
177
  return (keep(writes ? { writes } : { doc, ops, why }), answer?.());
177
- // landed, its sender is told the version it made, from the write's own answer (a page's undo keeps it: `undo`)
178
+ // landed, its sender is told the version it made, from the write's own answer (a page's undo keeps it: `undo`); a
179
+ // sender a removal has taken off the page by then, the document
178
180
  const told: Answered = (why, version) => {
179
181
  if (why === undefined && version !== undefined)
180
- e.target!.dispatchEvent(
182
+ ((e.target as Node).isConnected ? e.target! : document).dispatchEvent(
181
183
  new CustomEvent("landed", { bubbles: true, composed: true, detail: { doc: doc ?? writes![0]!.doc, version } }),
182
184
  );
183
185
  answer?.(why);
@@ -220,15 +222,21 @@ function send(doc: string | undefined, m: object, answer: Answered = () => {}) {
220
222
  // a document subscribed to again (the page back from away) says the version it has, to be sent only what's since;
221
223
  // answered, it's held on this socket, and what was kept for it is sent
222
224
  const subscribe = (s: WebSocket, doc: string) => {
223
- const had = document.body.dataset[versionKey(doc)];
224
225
  socket.sent.set(++socket.id, {
225
226
  answer: (why) => why === undefined && s === socket.ws && (socket.ready.add(doc), flush()),
226
227
  });
227
- s.send(JSON.stringify({ subscribe: doc, id: socket.id, ...(had ? { since: Number(had) } : {}) }));
228
+ s.send(JSON.stringify({ subscribe: doc, id: socket.id, ...since(doc) }));
229
+ };
230
+ /** The version the page has of a document, as a subscribe says it: none, and it's sent whole. */
231
+ const since = (doc: string) => {
232
+ const had = document.body.dataset[versionKey(doc)];
233
+ return had ? { since: Number(had) } : {};
228
234
  };
229
235
 
230
236
  /** What was kept while the page was away, sent now it's back: oldest first, each once its documents are held again;
231
- * the signal gone again, the rest wait. One refused is let go, and said. */
237
+ * the signal gone again, the rest wait. One refused is let go, and said. Each is sent with its own id (`kept`), so one
238
+ * the server made already (its answer lost as the signal went, or sent by another tab from the same storage) is
239
+ * answered done, not made twice. */
232
240
  async function flush() {
233
241
  // (a document the write makes, board:$1, is held by no page until it's made)
234
242
  const ready = (k: Kept) => (k.writes?.map((p) => p.doc) ?? [k.doc!]).every((d) => socket.ready.has(d) || madeBy(d));
@@ -237,7 +245,9 @@ async function flush() {
237
245
  try {
238
246
  for (let k = waiting().find(ready); k && isOpen(); k = waiting().find(ready)) {
239
247
  const { id, doc, ops, why, writes } = k;
240
- const refused = await new Promise<Answer>((answer) => send(doc, writes ? { writes } : { ops, why }, answer));
248
+ const refused = await new Promise<Answer>((answer) =>
249
+ send(doc, { ...(writes ? { writes } : { ops, why }), kept: id }, answer),
250
+ );
241
251
  if (refused && !isOpen()) break;
242
252
  sent(id);
243
253
  if (refused) toast(`A change made without signal wasn't kept: ${refused}`, { hold: true });
@@ -370,15 +380,16 @@ async function gone(code: number) {
370
380
  }
371
381
 
372
382
  /** A socket the browser calls open, asked whether it is: a subscribe to a document it holds, with id 0, which the
373
- * server answers done. Unanswered in time, it's let go. */
383
+ * server answers done; with the version the page has, as a page back from away asks, so it's sent only what it
384
+ * missed while asleep (nothing, mostly), never the document whole. Unanswered in time, it's let go. */
374
385
  function stillThere(s: WebSocket) {
375
386
  const [doc] = docs.keys();
376
387
  if (socket.asked || !doc) return;
377
388
  socket.asked = setTimeout(() => {
378
389
  ((s.onclose = s.onmessage = null), s.close());
379
390
  gone(1006);
380
- }, ANSWER);
381
- s.send(JSON.stringify({ subscribe: doc, id: 0 }));
391
+ }, answerIn());
392
+ s.send(JSON.stringify({ subscribe: doc, id: 0, ...since(doc) }));
382
393
  }
383
394
 
384
395
  /** The socket again, now: when it's time, or as soon as the phone wakes or the network comes back; one that
@@ -429,9 +440,7 @@ function apply(held: Map<string, Held>, o: Op): Element | undefined {
429
440
  * or, with no order, last. */
430
441
  function placed(h: Held, el: Element) {
431
442
  if (!h.order) return void (el.parentNode !== h.el && h.el.append(el));
432
- // without one, an item ranks by its key
433
- const at = (e: Element) =>
434
- [Number(e.getAttribute(h.order!) ?? e.getAttribute("key")), Number(e.getAttribute("key"))] as const;
443
+ const at = (e: Element) => rankOf(h, e);
435
444
  const [rank, key] = at(el);
436
445
  const after = [...h.items.values()].find((e) => {
437
446
  if (e === el || e.parentNode !== h.el) return false;
@@ -443,20 +452,62 @@ function placed(h: Held, el: Element) {
443
452
  h.el.insertBefore(el, after ?? null);
444
453
  }
445
454
 
455
+ /** Where an item ranks in its collection's order: by the field (without one, by its key), then by its key. */
456
+ const rankOf = (h: Held, el: Element) =>
457
+ [Number(el.getAttribute(h.order!) ?? el.getAttribute("key")), Number(el.getAttribute("key"))] as const;
458
+
459
+ /** A collection's items all put in its order, each one's rank read once: the longest run of them in order already
460
+ * stays where it is, and only the rest are moved, each before the next that stays. */
461
+ function inOrder(h: Held) {
462
+ const want = [...h.items.values()]
463
+ .map((el) => ({ el, at: rankOf(h, el) }))
464
+ .sort((a, b) => a.at[0] - b.at[0] || a.at[1] - b.at[1])
465
+ .map(({ el }) => el);
466
+ const now = new Map([...h.el.children].map((el, i) => [el, i]));
467
+ // the longest run: for each length, the item one so long ends with (the earliest on the page), and for each item the
468
+ // one before it in its run
469
+ const ends: Element[] = [],
470
+ before = new Map<Element, Element | undefined>();
471
+ for (const el of want) {
472
+ const here = now.get(el);
473
+ if (here === undefined) continue;
474
+ let from = 0,
475
+ to = ends.length;
476
+ while (from < to) {
477
+ const mid = (from + to) >> 1;
478
+ if (now.get(ends[mid]!)! < here) from = mid + 1;
479
+ else to = mid;
480
+ }
481
+ before.set(el, ends[from - 1]);
482
+ ends[from] = el;
483
+ }
484
+ const stays = new Set<Element>();
485
+ for (let el = ends.at(-1); el; el = before.get(el)) stays.add(el);
486
+ let moved: Element[] = [];
487
+ for (const el of want)
488
+ if (!stays.has(el)) moved.push(el);
489
+ else {
490
+ for (const m of moved) h.el.insertBefore(m, el);
491
+ moved = [];
492
+ }
493
+ for (const m of moved) h.el.append(m);
494
+ }
495
+
446
496
  /** A collection, whole, as the document has it: each item kept by key and changed only where it
447
497
  * differs, those gone taken away, and those new added. */
448
498
  function whole(h: Held, items: Record<string, unknown>[]) {
449
499
  const gone = new Set(h.items.keys());
450
500
  for (const item of items) {
451
501
  const el = h.items.get(String(item.id));
452
- if (!el) placed(h, element(h, item));
502
+ if (el) for (const [field, type] of Object.entries(h.fields)) set(el, type, field, valueAt(item, field));
453
503
  else {
454
- for (const [field, type] of Object.entries(h.fields)) set(el, type, field, valueAt(item, field));
455
- if (h.order) placed(h, el);
504
+ const made = element(h, item);
505
+ if (!h.order) placed(h, made);
456
506
  }
457
507
  gone.delete(String(item.id));
458
508
  }
459
509
  for (const key of gone) (h.items.get(key)!.remove(), h.items.delete(key));
510
+ if (h.order) inOrder(h);
460
511
  }
461
512
 
462
513
  function element({ tag, fields, items, may }: Held, item: Record<string, unknown>) {