@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/parts.ts CHANGED
@@ -1,28 +1,35 @@
1
1
  // joined's parts (src/parts.css), one sheet, a layer (joined) beneath anything of an app's own, so any rule of an
2
2
  // app's wins, whatever its specificity: the page adopts it as this is first imported, and a display adopts it in its
3
3
  // shadow by adopt(), before its own. A display or the group imports this; a page holding the group needn't, unless
4
- // it wants a part's behaviour (the page's line said, a modal that asks first, a button showing it's open), and a page
5
- // without one (anonymous: hello) imports it for the look. The page's surfaces, and each adopted shadow's, dim the
6
- // page by its one scrim (./scrim.ts).
4
+ // it wants a part's behaviour (the page's line said, a modal that asks first), and a page without one (anonymous:
5
+ // hello) imports it for the look. It brings Web Awesome's components (./awesome.ts): the drawer on a phone, the modal,
6
+ // the peek, the tabs, the fields and the ticks are theirs.
7
7
 
8
8
  import STYLE from "./parts.css" with { type: "text" };
9
+ import { geo, world } from "./geo.ts";
10
+ import { tiles } from "./ground.ts";
9
11
  import { hueOf } from "./hues.ts";
12
+ import { icon, iconButton } from "./icons.ts";
13
+ import "./awesome.ts";
10
14
  import { keep } from "./keep.ts";
11
15
  import type { Answer } from "./page.ts";
12
- import { watch } from "./scrim.ts";
16
+
17
+ /** A phone, as joined's stylesheets have it. */
18
+ export const phone = matchMedia("(max-width: 40rem)");
19
+
20
+ /** Whether a drawer is docked at the screen's side: the page keeps clear of it (src/page.css, wider than a phone),
21
+ * said only as it changes. */
22
+ export function docked(is: boolean) {
23
+ if (document.body.hasAttribute("data-docked") !== is) document.body.toggleAttribute("data-docked", is);
24
+ }
13
25
 
14
26
  export const PARTS = new CSSStyleSheet();
15
27
  PARTS.replaceSync(STYLE);
16
- /** Where the scrim watches, a modal's and a sheet's own ::backdrop let go: the scrim dims for them. */
17
- const WATCHED = new CSSStyleSheet();
18
- WATCHED.replaceSync(`@layer joined { :is(dialog, .sheet)::backdrop { background: transparent; } }`);
19
- document.adoptedStyleSheets = [...document.adoptedStyleSheets, PARTS, WATCHED];
28
+ document.adoptedStyleSheets = [...document.adoptedStyleSheets, PARTS];
20
29
 
21
- /** A display's shadow given joined's parts, then its own sheets (which win, joined's being a layer beneath), its
22
- * modals, sheets and drawers dimming the page by its one scrim as the page's do. */
30
+ /** A display's shadow given joined's parts, then its own sheets (which win, joined's being a layer beneath). */
23
31
  export function adopt(root: ShadowRoot, ...sheets: CSSStyleSheet[]) {
24
- root.adoptedStyleSheets = [PARTS, WATCHED, ...sheets];
25
- watch(root);
32
+ root.adoptedStyleSheets = [PARTS, ...sheets];
26
33
  }
27
34
 
28
35
  /** A person's face (Identity): their initial in their hue, their name its title; or, where there's no room (an
@@ -44,35 +51,215 @@ export function chip(key: number, name: string, words = name) {
44
51
  return c;
45
52
  }
46
53
 
54
+ /** A row (Row, design.md "A row"): the button that opens it, to put in what holds the row (an item's element),
55
+ * after any tick. All of it is the one target: `open` is what a tap does. `say(words, more)` sets its words and, under
56
+ * them, what more is said, only where they've changed; `mark(is)` says its drawer is open, or isn't; `el.disabled`
57
+ * where this person may not open it, and it's its words alone. */
58
+ export function row(open: () => void) {
59
+ const el = Object.assign(document.createElement("button"), { type: "button", className: "j-open" }),
60
+ words = Object.assign(document.createElement("span"), { className: "j-words" }),
61
+ text = document.createElement("b"),
62
+ under = document.createElement("span");
63
+ words.append(text, under);
64
+ el.append(words, icon("on"));
65
+ el.addEventListener("click", open);
66
+ return {
67
+ el,
68
+ say(said: string, more = "") {
69
+ if (text.textContent !== said) text.textContent = said;
70
+ if (under.textContent !== more) under.textContent = more;
71
+ },
72
+ mark(is: boolean) {
73
+ el.ariaCurrent = is ? "true" : null;
74
+ },
75
+ };
76
+ }
77
+
78
+ /** A tick on a row (Row): a button that does something else than the row does, kept at once. `set(on, what)` says
79
+ * whether it's ticked, and what of ("Buy rice: done"). */
80
+ export function tick(act: () => void) {
81
+ const el = Object.assign(document.createElement("wa-checkbox"), { className: "j-tick" }) as HTMLElement & {
82
+ checked: boolean;
83
+ disabled: boolean;
84
+ };
85
+ const words = document.createElement("span");
86
+ el.append(words);
87
+ // what's ticked is the document's word: put back as it's pressed, and changed as the write lands
88
+ el.addEventListener("change", () => ((el.checked = !el.checked), act()));
89
+ return {
90
+ el,
91
+ set(on: boolean, what: string) {
92
+ el.checked = on;
93
+ if (words.textContent !== what) ((words.textContent = what), (el.title = what));
94
+ },
95
+ };
96
+ }
97
+
98
+ /** A place as a small map (design.md, "A peek"): OpenStreetMap's tiles round it, its pin at the middle. Drawn once
99
+ * it's on the page and has a width; nothing, where it's no place. */
100
+ export function place(at: string, zoom = 15) {
101
+ const el = Object.assign(document.createElement("div"), { className: "j-place", role: "img" }),
102
+ here = geo(at);
103
+ el.ariaLabel = "A map of it";
104
+ if (!here) return el;
105
+ const { x, y } = world(here.lat, here.lng, zoom);
106
+ new ResizeObserver(() => {
107
+ const [w, h] = [el.clientWidth, el.clientHeight];
108
+ if (!w || el.dataset.drawn === `${w}x${h}`) return;
109
+ el.dataset.drawn = `${w}x${h}`;
110
+ // the point at the middle
111
+ el.replaceChildren(...tiles(zoom, x - w / 2, y - h / 2, w, h), document.createElement("i"));
112
+ }).observe(el);
113
+ return el;
114
+ }
115
+
116
+ /** A mention (design.md, "A peek"): a thing named where it isn't its own place, a small tool that peeks at it.
117
+ * `say(words)` sets what it says, only where it's changed. */
118
+ export function mention(act: (by: HTMLElement) => void) {
119
+ // its words a pill inside it: the button is what's tapped, a finger tall under a finger, the pill only its look
120
+ const el = Object.assign(document.createElement("button"), { type: "button", className: "j-mention" }),
121
+ words = el.appendChild(document.createElement("span"));
122
+ el.addEventListener("click", () => act(el));
123
+ return {
124
+ el,
125
+ say(said: string) {
126
+ if (words.textContent !== said) words.textContent = said;
127
+ },
128
+ };
129
+ }
130
+
131
+ /** A peek (Peek, design.md "A peek"): a little more of a thing, by what was tapped, read and never typed in, with
132
+ * the way to open it whole (`open`, its foot's one button, said by `deed`). Web Awesome's wa-popover, one for a page,
133
+ * put in it once: `show(by, name)` places it by `by` (under it, or wherever there's room) with the thing's name at its
134
+ * head, its `body` the caller's to fill first; `hide()` takes it away, as its ×, Escape and a tap elsewhere do. While
135
+ * it's open, what it's by says so (aria-expanded). */
136
+ export function peek(open: () => void, deed = "Open") {
137
+ const el = Object.assign(document.createElement("wa-popover"), {
138
+ className: "j-peek",
139
+ placement: "bottom-start",
140
+ }) as HTMLElement & {
141
+ open: boolean;
142
+ anchor: Element | null;
143
+ hasUpdated: boolean;
144
+ updateComplete: Promise<unknown>;
145
+ },
146
+ head = document.createElement("header"),
147
+ name = document.createElement("b"),
148
+ body = Object.assign(document.createElement("div"), { className: "j-peek-body" }),
149
+ foot = document.createElement("footer"),
150
+ whole = Object.assign(document.createElement("button"), { type: "button", className: "act", textContent: deed });
151
+ let by: HTMLElement | null = null;
152
+ const hide = () => (el.open = false);
153
+ head.append(name, iconButton("close", "Close", hide));
154
+ whole.prepend(icon("on"));
155
+ whole.addEventListener("click", () => (hide(), open()));
156
+ foot.append(whole);
157
+ el.append(head, body, foot);
158
+ el.addEventListener("wa-after-hide", (e) => {
159
+ if (e.target !== el || el.open) return;
160
+ by?.removeAttribute("aria-expanded");
161
+ by = null;
162
+ });
163
+ return {
164
+ el,
165
+ body,
166
+ hide,
167
+ /** Its button's words, for what this pop opens whole; none, and it has no button (there's no whole to open). */
168
+ deed(words: string | null) {
169
+ foot.hidden = words === null;
170
+ if (words !== null) whole.lastChild!.textContent = words;
171
+ },
172
+ show(at: HTMLElement, said: string) {
173
+ by?.removeAttribute("aria-expanded");
174
+ name.textContent = said;
175
+ el.ariaLabel = said;
176
+ // anchored once it's first drawn: Web Awesome's popover clears its anchor as it first draws (it has no `for`)
177
+ const go = () => ((el.anchor = at), (el.open = true));
178
+ if (el.hasUpdated) go();
179
+ else void el.updateComplete.then(go);
180
+ (by = at).setAttribute("aria-expanded", "true");
181
+ },
182
+ };
183
+ }
184
+
47
185
  /** A line for a request's refusal (Said): emptied as it's sent, its why if it's refused. It returns the
48
186
  * refusal, if there was one; done is nothing to say, what it did showing as its change. */
49
187
  export async function said(line: Element, request: Promise<Answer>) {
188
+ const callout = line.localName === "wa-callout";
50
189
  line.textContent = "";
190
+ if (callout) (line as HTMLElement).hidden = true;
51
191
  const why = await request;
52
192
  if (why) line.textContent = `Refused: ${why}`;
193
+ if (callout) (((line as HTMLElement).hidden = !why), line.setAttribute("variant", "danger"));
53
194
  return why;
54
195
  }
55
196
 
56
- /** A modal that asks first (Modal): its question in its h2, then Never mind (keep) or the deed (sure). What it
57
- * returns asks it: the question, and what the deed does. */
58
- export function askFirst(dialog: HTMLDialogElement) {
197
+ /** A surface of Web Awesome's (a wa-drawer, a wa-dialog), given its props: on a phone, a drawer rises from the foot
198
+ * (design.md, "Three surfaces"), and from the right wider; light-dismissed, as a tap on the dimmed page closes it. */
199
+ export function surface<K extends "wa-drawer" | "wa-dialog">(tag: K, props: object = {}) {
200
+ const el = Object.assign(document.createElement(tag), props) as HTMLElement & { open: boolean; placement?: string };
201
+ el.setAttribute("light-dismiss", "");
202
+ return tag === "wa-drawer" ? placed(el) : el;
203
+ }
204
+
205
+ /** A drawer placed as the screen is: from the foot on a phone, from the right wider. */
206
+ export function placed<T extends HTMLElement>(drawer: T) {
207
+ const place = () => drawer.setAttribute("placement", phone.matches ? "bottom" : "end");
208
+ phone.addEventListener("change", place);
209
+ place();
210
+ return drawer;
211
+ }
212
+
213
+ /** What follows a removal, said under its title (Peter, 2 Dec: a modal states, it doesn't ask: the buttons ask). */
214
+ export const GONE = "It goes for everyone at once, and can't be put back.";
215
+
216
+ /** A modal that asks first (Modal), a wa-dialog: its title saying what will happen ("Soba will be removed."), a line
217
+ * saying what follows, then Never mind (keep) or the deed (sure), a removal's with its icon, made in it where it has
218
+ * none. What it returns asks it: what will happen, what the deed does, and what follows (`follows`, unless said). */
219
+ export function askFirst(dialog: HTMLElement & { open: boolean }, words = "Remove", follows = GONE) {
59
220
  let deed = () => {};
60
- dialog.querySelector("[name=keep]")!.addEventListener("click", () => dialog.close());
61
- dialog.querySelector("[name=sure]")!.addEventListener("click", () => (dialog.close(), deed()));
62
- return (question: string, then: () => void) => {
63
- dialog.querySelector("h2")!.textContent = question;
221
+ dialog.classList.add("j-ask");
222
+ if (!dialog.querySelector("[name=sure]")) {
223
+ const foot = Object.assign(document.createElement("footer"), { slot: "footer" });
224
+ foot.append(
225
+ Object.assign(document.createElement("button"), {
226
+ type: "button",
227
+ className: "act",
228
+ name: "keep",
229
+ textContent: "Never mind",
230
+ }),
231
+ Object.assign(document.createElement("button"), {
232
+ type: "button",
233
+ className: "act danger",
234
+ name: "sure",
235
+ textContent: words,
236
+ }),
237
+ );
238
+ // a deed that removes says its word, its icon beside it (design.md, Icons)
239
+ if (/^Remove\b/.test(words)) foot.lastElementChild!.prepend(icon("remove"));
240
+ dialog.append(Object.assign(document.createElement("h2"), { slot: "label" }), foot);
241
+ }
242
+ const line = dialog.querySelector(":scope > p") ?? dialog.appendChild(document.createElement("p"));
243
+ dialog.querySelector("[name=keep]")!.addEventListener("click", () => (dialog.open = false));
244
+ dialog.querySelector("[name=sure]")!.addEventListener("click", () => ((dialog.open = false), deed()));
245
+ return (title: string, then: () => void, said = follows) => {
246
+ dialog.querySelector("h2")!.textContent = title;
247
+ line.textContent = said;
64
248
  deed = then;
65
- dialog.showModal();
249
+ dialog.open = true;
66
250
  };
67
251
  }
68
252
 
69
- /** Each button in root that opens a popover says so while it's open (aria-expanded), and is drawn
70
- * pressed: a sheet that covers the page is never open with nothing to say what opened it. */
71
- export function showsOpen(root: ParentNode) {
72
- for (const b of root.querySelectorAll<HTMLButtonElement>("[popovertarget]:not([popovertargetaction=hide])"))
73
- b.popoverTargetElement?.addEventListener("toggle", (e) =>
74
- b.setAttribute("aria-expanded", String((e as ToggleEvent).newState === "open")),
75
- );
253
+ /** The page's own modal that asks first, for what removes from a row in the page (a person off the list, a stay
254
+ * cancelled): one for each deed's words, put in the page as it's first wanted. Removing is always asked first. */
255
+ const pageAsks = new Map<string, ReturnType<typeof askFirst>>();
256
+ export function askOnPage(title: string, then: () => void, words = "Remove", follows = GONE) {
257
+ if (!pageAsks.has(words)) {
258
+ const dialog = surface("wa-dialog", { className: "ask" });
259
+ document.body.append(dialog);
260
+ pageAsks.set(words, askFirst(dialog, words));
261
+ }
262
+ pageAsks.get(words)!(title, then, follows);
76
263
  }
77
264
 
78
265
  /** What a display has open, in the URL's hash (`#event=2`), so Back closes it and a link opens it again (design.md,
@@ -120,9 +307,14 @@ export function inHash<T extends { key: number }>(
120
307
  return { mark, again: () => void (wanted !== null && open()) };
121
308
  }
122
309
 
310
+ /** Each typeahead's list has an id of its own, and its matches theirs by it: what its field names them by. */
311
+ let lists = 0;
312
+
123
313
  /** A typeahead (Choosing: more than six): what's typed in `field` matched against what may be chosen, the matches
124
- * offered beneath it in `list` (the first eight), a list of choices; one tapped, or the first on Enter, is chosen
125
- * and the field emptied, and Escape lets them go, leaving what's typed. An underscore is read as a space
314
+ * offered beneath it in `list` (the first eight), a list of choices. It's a combobox (WAI-ARIA): the focus stays in
315
+ * the field, which says whether its list is shown and names its active match, the first until the arrows move it,
316
+ * from the last round to the first. One tapped, or the active one on Enter, is chosen and the field emptied; Escape
317
+ * lets them go, leaving what's typed, and the arrows bring them back. An underscore is read as a space
126
318
  * (America/New_York, typed "new york"). It returns its offer, to call again when what may be chosen changes. */
127
319
  export function typeahead(
128
320
  field: HTMLInputElement,
@@ -131,6 +323,22 @@ export function typeahead(
131
323
  choose: (key: number) => void,
132
324
  ) {
133
325
  const kept = new Map<string, { shows: string; el: HTMLElement }>();
326
+ let active: string | undefined; // the active match, by its id
327
+ list.id ||= `j-choices-${++lists}`;
328
+ list.role = "listbox";
329
+ field.role = "combobox";
330
+ field.ariaAutoComplete = "list";
331
+ field.setAttribute("aria-controls", list.id);
332
+ /** The list shown or not, and its active match, as the field says them: the one active still, or the first. */
333
+ const say = (shown: boolean) => {
334
+ const matches = [...list.children];
335
+ list.hidden = !shown;
336
+ field.ariaExpanded = String(shown);
337
+ active = shown ? (matches.find((m) => m.id === active) ?? matches[0])!.id : undefined;
338
+ for (const m of matches) m.ariaSelected = String(m.id === active);
339
+ if (active) field.setAttribute("aria-activedescendant", active);
340
+ else field.removeAttribute("aria-activedescendant");
341
+ };
134
342
  const offer = () => {
135
343
  const said = (s: string) => s.toLowerCase().replaceAll("_", " "),
136
344
  q = said(field.value.trim()),
@@ -139,7 +347,6 @@ export function typeahead(
139
347
  .filter(([, words]) => said(words).includes(q))
140
348
  .slice(0, 8)
141
349
  : [];
142
- list.hidden = !found.length;
143
350
  keep(
144
351
  list,
145
352
  kept,
@@ -147,19 +354,35 @@ export function typeahead(
147
354
  key: String(key),
148
355
  shows: words,
149
356
  make: () => {
150
- const b = Object.assign(document.createElement("button"), { type: "button", textContent: words });
151
- b.setAttribute("role", "option");
357
+ const b = Object.assign(document.createElement("button"), {
358
+ type: "button",
359
+ role: "option",
360
+ id: `${list.id}-${key}`,
361
+ tabIndex: -1, // the arrows reach it, from the field
362
+ textContent: words,
363
+ });
152
364
  b.addEventListener("click", () => ((field.value = ""), offer(), choose(key), field.focus()));
153
365
  return b;
154
366
  },
155
367
  })),
156
368
  );
369
+ say(found.length > 0);
157
370
  };
158
- field.addEventListener("input", offer);
371
+ say(false);
372
+ field.addEventListener("input", () => ((active = undefined), offer()));
159
373
  field.addEventListener("keydown", (e) => {
160
- const first = list.hidden ? null : list.querySelector("button");
161
- if (e.key === "Enter") (e.preventDefault(), first?.click()); // never the form's Save
162
- if (e.key === "Escape" && !list.hidden) (e.preventDefault(), (list.hidden = true));
374
+ if (e.isComposing) return; // the keys are the input method's
375
+ const matches = [...list.children] as HTMLElement[],
376
+ at = matches.findIndex((m) => m.id === active);
377
+ if (e.key === "Enter") (e.preventDefault(), matches[at]?.click()); // never the form's Save
378
+ if (e.key === "Escape" && !list.hidden) (e.preventDefault(), say(false));
379
+ if ((e.key === "ArrowDown" || e.key === "ArrowUp") && matches.length) {
380
+ e.preventDefault();
381
+ if (list.hidden) return offer();
382
+ active = matches.at((at + (e.key === "ArrowDown" ? 1 : -1)) % matches.length)!.id;
383
+ say(true);
384
+ matches.find((m) => m.id === active)!.scrollIntoView({ block: "nearest" });
385
+ }
163
386
  });
164
387
  return offer;
165
388
  }
package/src/plane.css ADDED
@@ -0,0 +1,61 @@
1
+ /* <j-plane>'s look (./plane.ts), after what readings share: the picture the page drew, as wide as there is room; its
2
+ marks on the accent, their words in card, drawn only when asked for (docs/maps.md: words on demand, never by
3
+ default): a mark pointed at, held by the keys, picked up or open; a mark picked up lifted; the tools a bar above. */
4
+ .plane {
5
+ border-radius: var(--radius);
6
+ overflow: hidden;
7
+ background: var(--well);
8
+ }
9
+ .frame {
10
+ display: block;
11
+ width: 100%;
12
+ height: auto;
13
+ touch-action: none;
14
+ }
15
+ .mark {
16
+ cursor: grab;
17
+ }
18
+ .mark circle {
19
+ fill: var(--accent);
20
+ stroke: var(--card);
21
+ }
22
+ .mark text {
23
+ fill: var(--card);
24
+ font-family: var(--sans);
25
+ font-weight: 600;
26
+ text-anchor: middle;
27
+ pointer-events: none;
28
+ visibility: hidden;
29
+ }
30
+ .mark:hover text,
31
+ .mark:focus text,
32
+ .mark[aria-current="true"] text,
33
+ .mark[dragged] text {
34
+ visibility: visible;
35
+ }
36
+ .mark[aria-current="true"] circle,
37
+ .mark[dragged] circle {
38
+ stroke: var(--ink);
39
+ }
40
+ .mark[dragged] {
41
+ cursor: grabbing;
42
+ }
43
+ .tools {
44
+ display: flex;
45
+ flex-wrap: wrap;
46
+ gap: var(--space-8);
47
+ }
48
+ .route {
49
+ fill: none;
50
+ stroke-linejoin: round;
51
+ pointer-events: none;
52
+ }
53
+ /* its parts one above another, a space between each, as joined's page spaces what it holds; none where one is empty */
54
+ :host {
55
+ display: grid;
56
+ gap: var(--space-12);
57
+ align-content: start;
58
+ }
59
+ .said:empty {
60
+ display: none;
61
+ }
package/src/plane.ts ADDED
@@ -0,0 +1,153 @@
1
+ // <j-plane for="marks" x="x" y="y" label="name"> (joined/kit): a collection on a picture drawn once, each item a mark
2
+ // where two of its fields put it, its name drawn only when it's asked for (pointed at, held, open: docs/maps.md, words
3
+ // on demand). The picture is the page's (<svg slot="ground">, its viewBox the plane's units: a
4
+ // course on the water, a floor's plan); the marks are drawn on it, kept by key, each moved only as it changes. A mark
5
+ // pressed and dragged is laid where it's let go, one write of both fields, where its `may` lets it replace them; a
6
+ // mark that may not move doesn't. A mark tapped opens its sheet. Its tools (<j-tool>) float on it: Lay a mark chosen,
7
+ // the picture tapped opens a new mark's sheet on that place, the rest asked. route="courses:order" draws a line for each
8
+ // item of another collection through the marks its field names, in order ("1 2 3", by their labels), back round to the
9
+ // first, from="510 690" where it starts: a fleet's course from the line. A route is drawn, never gestured.
10
+
11
+ import { el, Reading } from "./reading.ts";
12
+ import { hueOf } from "./hues.ts";
13
+ import type { Item } from "./rows.ts";
14
+ import { specOf } from "./specs.ts";
15
+ import { applied, Tools } from "./tool.ts";
16
+ import STYLE from "./plane.css" with { type: "text" };
17
+
18
+ const SHEET = new CSSStyleSheet();
19
+ SHEET.replaceSync(STYLE);
20
+ const NS = "http://www.w3.org/2000/svg";
21
+ const svg = <K extends keyof SVGElementTagNameMap>(tag: K, attrs: Record<string, string | number> = {}) => {
22
+ const e = document.createElementNS(NS, tag);
23
+ for (const [k, v] of Object.entries(attrs)) e.setAttribute(k, String(v));
24
+ return e;
25
+ };
26
+
27
+ export class JPlane extends Reading {
28
+ static override sheets = [SHEET];
29
+ #tools = new Tools(this);
30
+ #frame = svg("svg", { class: "frame" });
31
+ #ground = svg("g", { class: "ground" });
32
+ #marks = svg("g", { class: "marks" });
33
+ #routes = svg("g", { class: "routes" });
34
+ /** The items drawn as routes, and the field naming their marks in order. */
35
+ #route?: { items: HTMLCollectionOf<Item>; field: string };
36
+ #kept = new Map<number, { shows: string; g: SVGGElement }>();
37
+ #moving = 0;
38
+ /** A mark's size, in the picture's units: a finger's width on a picture shown at its own size. */
39
+ #r = 20;
40
+
41
+ get #x() {
42
+ return this.getAttribute("x") ?? "x";
43
+ }
44
+ get #y() {
45
+ return this.getAttribute("y") ?? "y";
46
+ }
47
+ protected made() {
48
+ const picture = this.querySelector<SVGSVGElement>(':scope > svg[slot="ground"]');
49
+ this.#frame.setAttribute("viewBox", picture?.getAttribute("viewBox") ?? "0 0 1000 1000");
50
+ const box = this.#frame.viewBox.baseVal;
51
+ this.#r = Math.round(Math.min(box.width, box.height) * 0.035);
52
+ this.#frame.setAttribute("role", "img");
53
+ this.#frame.setAttribute("aria-label", this.getAttribute("label-words") ?? this.for);
54
+ for (const child of picture?.children ?? []) this.#ground.append(child.cloneNode(true));
55
+ this.#frame.append(this.#ground, this.#routes, this.#marks);
56
+ const [collection, field] = (this.getAttribute("route") ?? "").split(":");
57
+ if (collection && field)
58
+ specOf(collection, (spec) => {
59
+ this.#route = { items: document.getElementsByTagName(spec.tag) as HTMLCollectionOf<Item>, field };
60
+ this.again();
61
+ });
62
+ // a tap on the picture lays what's chosen there
63
+ this.#ground.addEventListener("pointerdown", (down) => {
64
+ if (down.button !== 0) return;
65
+ this.#ground.addEventListener("pointerup", (up) => void applied(this, this.#tools, null, this.#at(up)), {
66
+ once: true,
67
+ });
68
+ });
69
+ this.root.append(this.#tools.bar, el("div", { className: "plane" }, this.#frame));
70
+ }
71
+ /** A point on the screen, in the picture's units. */
72
+ #at(e: { clientX: number; clientY: number }) {
73
+ const p = new DOMPoint(e.clientX, e.clientY).matrixTransform(this.#frame.getScreenCTM()!.inverse());
74
+ return { [this.#x]: Math.round(p.x), [this.#y]: Math.round(p.y) };
75
+ }
76
+ /** How many of the picture's units a pixel on the screen is. */
77
+ get #scale() {
78
+ const box = this.#frame.viewBox.baseVal;
79
+ return box.width / (this.#frame.getBoundingClientRect().width || box.width);
80
+ }
81
+ protected draw() {
82
+ this.#tools.draw();
83
+ this.#drawRoutes();
84
+ const seen = new Set<number>();
85
+ for (const it of this.items()) {
86
+ const x = Number(it.data[this.#x]),
87
+ y = Number(it.data[this.#y]);
88
+ if (!Number.isFinite(x) || !Number.isFinite(y)) continue;
89
+ seen.add(it.key);
90
+ const words = this.says(it, this.getAttribute("label") ?? "name") || this.named(it),
91
+ // what it may do too: a mark drawn before its `may` came is drawn again, draggable
92
+ shows = `${x} ${y} ${words} ${this.opened(it)} ${it.getAttribute("may")}`,
93
+ k = this.#kept.get(it.key);
94
+ if ((k && k.shows === shows) || this.#moving === it.key) continue;
95
+ const g = this.#mark(it, x, y, words);
96
+ if (k) k.g.replaceWith(g);
97
+ else this.#marks.append(g);
98
+ this.#kept.set(it.key, { shows, g });
99
+ }
100
+ for (const [key, k] of this.#kept) if (!seen.has(key)) (k.g.remove(), this.#kept.delete(key));
101
+ }
102
+ /** Each route: a line from where it starts through the marks it names, in order, and back round to the first. */
103
+ #drawRoutes() {
104
+ const r = this.#route,
105
+ label = this.getAttribute("label") ?? "name";
106
+ if (!r) return;
107
+ const at = new Map(this.items().map((it) => [String(it.data[label]), `${it.data[this.#x]},${it.data[this.#y]}`]));
108
+ const from = (this.getAttribute("from") ?? "").trim().split(/\s+/).filter(Boolean);
109
+ this.#routes.replaceChildren(
110
+ ...[...r.items]
111
+ .filter((it) => it.isConnected)
112
+ .flatMap((it) => {
113
+ const stops = String(it.data[r.field] ?? "")
114
+ .split(/\s+/)
115
+ .flatMap((n) => (at.has(n) ? [at.get(n)!] : []));
116
+ if (!stops.length) return [];
117
+ const line = svg("polyline", {
118
+ class: "route",
119
+ points: [...(from.length === 2 ? [from.join(",")] : []), ...stops, stops[0]!].join(" "),
120
+ "stroke-width": Math.max(1, Math.round(this.#r * 0.15)),
121
+ "stroke-dasharray": `${this.#r * 0.6} ${this.#r * 0.4}`,
122
+ });
123
+ line.style.stroke = hueOf(it.key);
124
+ return [line];
125
+ }),
126
+ );
127
+ }
128
+ /** An item's mark: a dot where its fields put it, its words drawn only when asked for (./plane.css) and always its
129
+ * name to a screen reader; dragged where it may move. */
130
+ #mark(it: Item, x: number, y: number, words: string) {
131
+ const g = svg("g", { class: "mark", "data-key": it.key, transform: `translate(${x} ${y})`, tabindex: 0 });
132
+ g.setAttribute("aria-label", words);
133
+ const label = svg("text", { y: Math.round(this.#r * 0.4), "font-size": Math.round(this.#r * 1.1) });
134
+ label.textContent = words;
135
+ g.append(svg("circle", { r: this.#r }), label);
136
+ const moves = this.may(it, this.#x) && this.may(it, this.#y);
137
+ this.press(
138
+ g as unknown as HTMLElement,
139
+ it,
140
+ moves
141
+ ? (dx, dy, done) => {
142
+ const s = this.#scale,
143
+ nx = Math.round(x + dx * s),
144
+ ny = Math.round(y + dy * s);
145
+ this.#moving = done ? 0 : it.key;
146
+ g.setAttribute("transform", `translate(${nx} ${ny})`);
147
+ if (done && (dx || dy)) void this.change(it, { [this.#x]: nx, [this.#y]: ny });
148
+ }
149
+ : undefined,
150
+ );
151
+ return g;
152
+ }
153
+ }
@@ -0,0 +1,60 @@
1
+ // The Postgres server joined's store is kept on (src/engine.ts's), by its URL with its password, through Bun.sql. Told
2
+ // by bun run test:postgres (tests/postgres/), against a server in Docker: not the gate's, which has no server.
3
+
4
+ import { postgresOf, type Engine } from "./engine.ts";
5
+
6
+ /** A Postgres server, by its URL (JOINED_POSTGRES: postgres://user:password@host:port/database), through Bun.sql: one
7
+ * connection held for the store, and LISTEN on a connection of its own. `fresh`: a database of its own, made as it
8
+ * opens and dropped as it closes (a test's). */
9
+ export async function server(url: string, { fresh = false } = {}): Promise<Engine> {
10
+ const { SQL } = await import("bun");
11
+ const at = new URL(url);
12
+ let made: string | undefined;
13
+ if (fresh) {
14
+ made = `joined_${crypto.randomUUID().replaceAll("-", "")}`;
15
+ const admin = new SQL(url, { max: 1 });
16
+ try {
17
+ await admin.unsafe(`CREATE DATABASE ${made}`);
18
+ } finally {
19
+ await admin.close();
20
+ }
21
+ at.pathname = `/${made}`;
22
+ }
23
+ const sql = new SQL(at.href, { max: 2 });
24
+ let held: Awaited<ReturnType<typeof sql.reserve>>;
25
+ try {
26
+ held = await sql.reserve();
27
+ } catch (e) {
28
+ await sql.close();
29
+ throw e;
30
+ }
31
+ const types = (columns: unknown) =>
32
+ new Map(((columns as { name: string; type: number }[] | undefined) ?? []).map((f) => [f.name, f.type]));
33
+ return postgresOf(
34
+ {
35
+ async query(text, values) {
36
+ const r = (await held.unsafe(text, values as never[])) as unknown as Record<string, unknown>[] & {
37
+ count: number | null;
38
+ columns?: unknown;
39
+ };
40
+ return { rows: [...r], changes: r.count ?? 0, types: types(r.columns) };
41
+ },
42
+ many: (text) => held.unsafe(text).simple(),
43
+ },
44
+ {
45
+ listen: async (channel, heard) => void (await sql.listen(channel, heard)),
46
+ notify: (channel, payload) => held.unsafe("SELECT pg_notify($1, $2)", [channel, payload]),
47
+ async close() {
48
+ held.release();
49
+ await sql.close();
50
+ if (!made) return;
51
+ const admin = new SQL(url, { max: 1 });
52
+ try {
53
+ await admin.unsafe(`DROP DATABASE IF EXISTS ${made} WITH (FORCE)`);
54
+ } finally {
55
+ await admin.close();
56
+ }
57
+ },
58
+ },
59
+ );
60
+ }