@blueshed/joined 0.1.20 → 0.2.0

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 (71) hide show
  1. package/.claude/skills/joined/SKILL.md +1 -1
  2. package/.claude/skills/joined/reference.md +96 -17
  3. package/docs/design.md +31 -8
  4. package/examples/cafe/kitchen.ts +2 -2
  5. package/examples/desk/card.ts +43 -0
  6. package/examples/desk/desk.css +17 -0
  7. package/examples/desk/desk.json +41 -0
  8. package/examples/desk/generated/elements.ts +69 -0
  9. package/examples/desk/generated/queries.ts +33 -0
  10. package/examples/desk/generated/rules.ts +94 -0
  11. package/examples/desk/index.html +34 -0
  12. package/examples/desk/lanes.css +126 -0
  13. package/examples/desk/lanes.ts +133 -0
  14. package/examples/desk/migrations/001-desk.ts +8 -0
  15. package/examples/desk/page.ts +29 -0
  16. package/examples/desk/pulse.ts +20 -0
  17. package/examples/desk/routes.ts +9 -0
  18. package/examples/desk/steps.ts +32 -0
  19. package/examples/desk/stories.jsonl +18 -0
  20. package/examples/desk/tally.css +62 -0
  21. package/examples/desk/tally.ts +76 -0
  22. package/examples/desk/thumbs.ts +45 -0
  23. package/examples/desk/trust.ts +24 -0
  24. package/examples/desk/words.ts +17 -0
  25. package/examples/trip/bar.ts +1 -1
  26. package/examples/trip/calendar-map.css +2 -0
  27. package/examples/trip/drags.ts +3 -3
  28. package/examples/trip/drawer.ts +5 -5
  29. package/examples/trip/event-form.ts +8 -7
  30. package/examples/trip/holding.ts +1 -1
  31. package/examples/trip/ics.ts +22 -112
  32. package/examples/trip/line-stops.ts +1 -1
  33. package/examples/trip/line-words.ts +1 -1
  34. package/examples/trip/line.ts +1 -1
  35. package/examples/trip/map.ts +1 -1
  36. package/examples/trip/month.ts +1 -1
  37. package/examples/trip/other-weeks.ts +1 -1
  38. package/examples/trip/page.ts +2 -2
  39. package/examples/trip/peek.ts +1 -1
  40. package/examples/trip/routes.ts +3 -3
  41. package/examples/trip/shell.ts +3 -3
  42. package/examples/trip/stays.ts +1 -1
  43. package/examples/trip/week.ts +1 -1
  44. package/examples/trip/writes.ts +1 -1
  45. package/examples/trips/card.ts +1 -1
  46. package/examples/trips/map.ts +1 -1
  47. package/examples/weeks/derive.ts +1 -1
  48. package/package.json +7 -1
  49. package/src/attachment.ts +7 -2
  50. package/src/demo.css +29 -26
  51. package/src/demo.ts +2 -2
  52. package/src/draft.ts +23 -8
  53. package/src/files.ts +24 -8
  54. package/{examples/trip/mercator.ts → src/geo.ts} +13 -3
  55. package/src/icon-names.ts +4 -0
  56. package/src/ics.ts +130 -0
  57. package/src/members/panel.css +208 -201
  58. package/src/members/panel.ts +17 -8
  59. package/src/outbox.ts +70 -0
  60. package/src/page.css +76 -70
  61. package/src/page.ts +79 -14
  62. package/src/parts.css +575 -428
  63. package/src/parts.ts +18 -5
  64. package/{examples/trip → src}/place-search.ts +5 -5
  65. package/{examples/trip → src}/places.ts +9 -7
  66. package/src/rank.ts +20 -0
  67. package/src/scrim.ts +81 -0
  68. package/src/server.ts +28 -1
  69. package/{examples/trip → src}/time.ts +37 -5
  70. package/src/tokens.css +5 -0
  71. package/src/tokens.json +35 -0
package/src/draft.ts CHANGED
@@ -1,8 +1,13 @@
1
1
  // A draft: a form holding changes to one document element until Save (its button `go`), put back by
2
2
  // Never mind (`never`). Each named control is held against the value the document has, so a change
3
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.
4
+ // says whether it's kept in its note, and why, if a write was refused; Save is off until there's something to keep
5
+ // and it's ready to be kept. In its drawer (or modal) it has its ×, made at the end of its head where the form has
6
+ // none, which Escape, and on a phone a tap on the dimmed page (./scrim.ts), press too. Every drawer that changes
7
+ // something is one: a page that wants it imports this; what its changes are as element ops is the page's own, and a
8
+ // drawer that holds no fields against the document says its state by show() alone.
9
+
10
+ import { iconButton } from "./icons.ts";
6
11
 
7
12
  type Value = string | boolean;
8
13
  type Control = HTMLInputElement;
@@ -43,11 +48,13 @@ export class Draft {
43
48
  return changed;
44
49
  }
45
50
  /** 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") {
51
+ * kept against anything. Save is off, too, until what it holds is ready to keep (a new one's title given). A refusal
52
+ * is said until it's taken back. */
53
+ show(state: "kept" | "changed" | "new", ready = true) {
48
54
  this.changed = state === "changed";
49
55
  for (const name of ["go", "never"])
50
- (this.form.elements.namedItem(name) as HTMLButtonElement).disabled = state === "kept";
56
+ (this.form.elements.namedItem(name) as HTMLButtonElement).disabled =
57
+ state === "kept" || (name === "go" && !ready);
51
58
  if (!this.note.classList.contains("refused"))
52
59
  this.note.textContent = state === "changed" ? "Not kept yet: Save keeps it, Never mind puts it back." : "";
53
60
  }
@@ -56,11 +63,19 @@ export class Draft {
56
63
  this.note.classList.toggle("refused", !!why);
57
64
  this.note.textContent = why;
58
65
  }
59
- /** The drawer (or modal) it's in: its × (`close`) and Escape close it; Never mind (`never`) puts back what it holds. */
66
+ /** The drawer (or modal) it's in: its × (`close`, made at the end of its head where the form has none) and Escape
67
+ * close it; Never mind (`never`) puts back what it holds. */
60
68
  inside(box: HTMLElement, close: () => void, never: () => void) {
61
- (this.form.elements.namedItem("close") as HTMLButtonElement).addEventListener("click", close);
69
+ let x = this.form.elements.namedItem("close") as HTMLButtonElement | null;
70
+ if (!x)
71
+ ((x = Object.assign(iconButton("close", "Close"), { name: "close" })),
72
+ this.form.querySelector("header")!.append(x));
73
+ x.addEventListener("click", close);
62
74
  (this.form.elements.namedItem("never") as HTMLButtonElement).addEventListener("click", never);
63
- box.addEventListener("keydown", (e) => (e as KeyboardEvent).key === "Escape" && !e.defaultPrevented && close());
75
+ // taken as its own, so the page's Escape (./scrim.ts) doesn't close another
76
+ box.addEventListener("keydown", (e) => {
77
+ if ((e as KeyboardEvent).key === "Escape" && !e.defaultPrevented) (e.preventDefault(), close());
78
+ });
64
79
  }
65
80
  #control = (name: string) => this.form.elements.namedItem(name) as Control;
66
81
  }
package/src/files.ts CHANGED
@@ -30,7 +30,22 @@ let making: Promise<unknown> = Promise.resolve();
30
30
  /** An SVG, by how it opens: its XML line, comments and doctype, if any, then its <svg>. A page holding one isn't. */
31
31
  const SVG = /^\s*(?:<\?xml[^>]*\?>\s*|<!--[\s\S]*?-->\s*|<!DOCTYPE[^>[]*(?:\[[\s\S]*?\])?>\s*)*<svg[\s>]/i;
32
32
  /** What each kind kept is served as. */
33
- const TYPES: Record<string, string> = { pdf: "application/pdf", svg: "image/svg+xml", webp: "image/webp" };
33
+ const TYPES: Record<string, string> = {
34
+ pdf: "application/pdf",
35
+ svg: "image/svg+xml",
36
+ webp: "image/webp",
37
+ md: "text/markdown; charset=utf-8",
38
+ };
39
+ /** Markdown is text, which says nothing of itself: a file is one where its name says so (.md, .markdown) and it's text
40
+ * (UTF-8, with no NUL in it). */
41
+ function markdown(named: string, bytes: Uint8Array) {
42
+ if (!/\.(md|markdown)$/i.test(named)) return false;
43
+ try {
44
+ return !new TextDecoder("utf-8", { fatal: true }).decode(bytes).includes("\0");
45
+ } catch {
46
+ return false;
47
+ }
48
+ }
34
49
  /** An SVG opened by its path is a page of the site's own: sandboxed, nothing in it runs or loads anything. */
35
50
  const SANDBOX = "sandbox; default-src 'none'; style-src 'unsafe-inline'";
36
51
  /** A kept file's name: its content's SHA-256, and its type's extension (the compiler's file form, after `file:`). */
@@ -80,14 +95,14 @@ export function filesAt(where: string, s3?: S3Options): Files {
80
95
  /** A file kept: its value for a field; or refused, why, in a word for the page. */
81
96
  export type Kept = { file: string } | { refused: string };
82
97
 
83
- /** A file sent, kept by its content's hash in `files`: a photo made a WebP, an SVG and a PDF as they are; anything
84
- * else refused. */
85
- export async function keep(files: Files, bytes: Uint8Array): Promise<Kept> {
98
+ /** A file sent, kept by its content's hash in `files`: a photo made a WebP, an SVG and a PDF as they are, and a
99
+ * Markdown file as it is where it's `named` so; anything else refused. */
100
+ export async function keep(files: Files, bytes: Uint8Array, named = ""): Promise<Kept> {
86
101
  if (!bytes.length) return { refused: "That file is empty." };
87
102
  if (bytes.length > MOST) return { refused: TOO_BIG };
88
103
  const head = new TextDecoder().decode(bytes.subarray(0, 1024));
89
104
  let out = bytes,
90
- ext = head.startsWith("%PDF-") ? "pdf" : SVG.test(head) ? "svg" : "";
105
+ ext = head.startsWith("%PDF-") ? "pdf" : SVG.test(head) ? "svg" : markdown(named, bytes) ? "md" : "";
91
106
  if (!ext)
92
107
  try {
93
108
  const made = making.then(() =>
@@ -104,7 +119,7 @@ export async function keep(files: Files, bytes: Uint8Array): Promise<Kept> {
104
119
  refused:
105
120
  (e as { code?: string }).code === "ERR_IMAGE_TOO_MANY_PIXELS"
106
121
  ? `That picture is too big: under ${PIXELS / 1_000_000} megapixels, please.`
107
- : "joined keeps a photo (JPEG, PNG, WebP or GIF), an SVG or a PDF: that's none of them.",
122
+ : "joined keeps a photo (JPEG, PNG, WebP or GIF), an SVG, a PDF or a Markdown file (.md): that's none of them.",
108
123
  };
109
124
  }
110
125
  const name = `${new Bun.CryptoHasher("sha256").update(out).digest("hex")}.${ext}`;
@@ -129,7 +144,7 @@ export const putsFile = (
129
144
  );
130
145
  });
131
146
 
132
- /** The routes: a file sent for a document (`POST /files?doc=board:1`, its bytes the body), by whoever `may` lets (why
147
+ /** The routes: a file sent for a document (`POST /files?doc=board:1`, its bytes the body, `&name=` its file's name), by whoever `may` lets (why
133
148
  * not, or null), one that says it's too big refused before it's read; and a file kept, by its name, with its type,
134
149
  * cached for good. */
135
150
  export function routes(files: Files, may: (req: Request, doc: string) => Promise<string | null>) {
@@ -140,7 +155,8 @@ export function routes(files: Files, may: (req: Request, doc: string) => Promise
140
155
  if (why) return Response.json({ refused: why }, { status: 403 });
141
156
  if (Number(req.headers.get("content-length")) > MOST)
142
157
  return Response.json({ refused: TOO_BIG }, { status: 413 });
143
- const kept = await keep(files, new Uint8Array(await req.arrayBuffer()));
158
+ const url = new URL(req.url);
159
+ const kept = await keep(files, new Uint8Array(await req.arrayBuffer()), url.searchParams.get("name") ?? "");
144
160
  return Response.json(kept, { status: "refused" in kept ? 422 : 200 });
145
161
  },
146
162
  },
@@ -1,5 +1,6 @@
1
- // The trip's places: a geo: URI (geo:35.68,139.76), and Web Mercator, a place as pixels in the world drawn
2
- // at zoom z in OpenStreetMap's 256px tiles, and back.
1
+ // Places on the page (joined/geo): a geo: URI (geo:35.68,139.76), the link that opens one in a maps app, and Web
2
+ // Mercator, a place as pixels in the world drawn at zoom z in OpenStreetMap's 256px tiles, and back. Pure: the trip's
3
+ // map is drawn by it, and an app's map, pin or "Open in Maps" takes it as it is.
3
4
 
4
5
  export const TILE = 256;
5
6
 
@@ -7,6 +8,15 @@ export const geo = (c?: string) => {
7
8
  const m = /^geo:(-?[\d.]+),(-?[\d.]+)/.exec(c ?? "");
8
9
  return m ? { lat: Number(m[1]), lng: Number(m[2]) } : null;
9
10
  };
11
+ /** A point as a geo: URI, to `dp` places (4: about 10m). */
12
+ export const geoOf = ({ lat, lng }: { lat: number; lng: number }, dp = 4) =>
13
+ `geo:${lat.toFixed(dp)},${lng.toFixed(dp)}`;
14
+ /** The link that opens a place in a maps app: Google's, as cardlet and idea-cards give it, which a phone opens in its
15
+ * maps app and a laptop on the web; none where there's no place. */
16
+ export const mapsLink = (place: string | undefined) => {
17
+ const at = geo(place);
18
+ return at ? `https://www.google.com/maps/search/?api=1&query=${at.lat},${at.lng}` : null;
19
+ };
10
20
  export const world = (lat: number, lng: number, z: number) => {
11
21
  const size = TILE * 2 ** z,
12
22
  sin = Math.sin((lat * Math.PI) / 180);
@@ -15,7 +25,7 @@ export const world = (lat: number, lng: number, z: number) => {
15
25
  export const placeAt = (x: number, y: number, z: number) => {
16
26
  const size = TILE * 2 ** z,
17
27
  lat = (Math.atan(Math.sinh(Math.PI * (1 - (2 * y) / size))) * 180) / Math.PI;
18
- return `geo:${lat.toFixed(4)},${((x / size) * 360 - 180).toFixed(4)}`;
28
+ return geoOf({ lat, lng: (x / size) * 360 - 180 });
19
29
  };
20
30
  /** The box round some points, its least and most x and y: by a loop, as a spread of very many overflows the stack. */
21
31
  export const bounds = (pts: { x: number; y: number }[]) => {
package/src/icon-names.ts CHANGED
@@ -22,6 +22,8 @@ import {
22
22
  LockOpen,
23
23
  Pencil,
24
24
  Play,
25
+ ThumbsDown,
26
+ ThumbsUp,
25
27
  LogIn,
26
28
  LogOut,
27
29
  Maximize2,
@@ -48,6 +50,7 @@ export const ICONS = {
48
50
  check: Check,
49
51
  close: X,
50
52
  copy: Copy,
53
+ down: ThumbsDown,
51
54
  download: Download,
52
55
  edit: Pencil,
53
56
  grip: GripVertical,
@@ -69,6 +72,7 @@ export const ICONS = {
69
72
  stay: BedDouble,
70
73
  "sign-in": LogIn,
71
74
  upload: Upload,
75
+ up: ThumbsUp,
72
76
  you: User,
73
77
  "zoom-in": ZoomIn,
74
78
  "zoom-out": ZoomOut,
package/src/ics.ts ADDED
@@ -0,0 +1,130 @@
1
+ // .ics (RFC 5545) at the edge (joined/ics): events go out as a calendar file (one to download, a feed to subscribe
2
+ // to, an event to add to my calendar), and a file coming in becomes events to add. A start is a wall clock, in its
3
+ // zone where it has one (DTSTART;TZID=Asia/Tokyo), so a calendar anywhere puts it at that hour there, or floating
4
+ // where it hasn't; its length a DURATION (or DTEND), its place a LOCATION and a GEO. It knows no app: who's on an
5
+ // event, and how they answered, is the app's to say (the trip's, examples/trip/ics.ts).
6
+
7
+ /** An event as a calendar holds it: its start a wall clock, its length a duration, its place a geo: value. */
8
+ export type IcsEvent = {
9
+ title: string;
10
+ start: string;
11
+ duration: string;
12
+ location?: string;
13
+ coordinates?: string;
14
+ zone?: string;
15
+ };
16
+ /** Someone on an event going out: their name where it's known, their answer, and who they are (a URI). */
17
+ export type Attendee = { name?: string; answer: "ACCEPTED" | "TENTATIVE" | "NEEDS-ACTION" | "DECLINED"; uri: string };
18
+ /** An event going out: its uid (the same each time, so a calendar holding it changes it, not adds it again), and a
19
+ * description, a link, and who's on it, where it has them. */
20
+ export type IcsOut = IcsEvent & { uid: string; description?: string; url?: string; attendees?: Attendee[] };
21
+
22
+ const esc = (s: string) =>
23
+ s
24
+ .replace(/\\/g, "\\\\")
25
+ .replace(/\n/g, "\\n")
26
+ .replace(/([,;])/g, "\\$1");
27
+ const unesc = (s: string) => s.replace(/\\(.)/g, (_, c: string) => (c.toLowerCase() === "n" ? "\n" : c));
28
+ /** A line folded at 75 bytes, as RFC 5545 asks (a folded line's leading space one of them), never inside a letter:
29
+ * a line in Japanese is three bytes a letter. */
30
+ const bytes = new TextEncoder();
31
+ const fold = (line: string) => {
32
+ const parts: string[] = [];
33
+ let part = "",
34
+ size = 0;
35
+ for (const c of line) {
36
+ const n = bytes.encode(c).length;
37
+ if (size + n > (parts.length ? 74 : 75)) {
38
+ parts.push(part);
39
+ [part, size] = ["", 0];
40
+ }
41
+ part += c;
42
+ size += n;
43
+ }
44
+ return [...parts, part].join("\r\n ");
45
+ };
46
+ const stamp = (local: string) => local.replace(/[-:]/g, ""); // 2026-11-12T19:00:00 → 20261112T190000
47
+ const local = (v: string) => {
48
+ const [, y, mo, d, h = "00", mi = "00", s = "00"] = /^(\d{4})(\d{2})(\d{2})(?:T(\d{2})(\d{2})(\d{2}))?/.exec(v) ?? [];
49
+ return y ? `${y}-${mo}-${d}T${h}:${mi}:${s}` : null;
50
+ };
51
+ const minutes = (from: string, to: string) => (Date.parse(`${to}Z`) - Date.parse(`${from}Z`)) / 60_000;
52
+
53
+ /** A start a calendar can read: a wall clock, to the second; and a length: days, or hours, minutes and seconds. */
54
+ const WHEN = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}$/,
55
+ LENGTH = /^P(?=\d|T\d)(\d+D)?(T(?=\d)(\d+H)?(\d+M)?(\d+S)?)?$/,
56
+ ZONE = /^[A-Za-z][A-Za-z0-9_+/-]*$/; // a zone as the world names it, and nothing a line could break on
57
+
58
+ /** The events as a calendar file: one whose start or length a calendar couldn't read (one kept from
59
+ * before the database typed them) is left out, rather than the file being one no calendar opens. */
60
+ export function toIcs(events: IcsOut[], name: string): string {
61
+ const now = new Date().toISOString().replace(/[-:]/g, "").slice(0, 15) + "Z";
62
+ const lines = ["BEGIN:VCALENDAR", "VERSION:2.0", "PRODID:-//blueshed//joined//EN", `X-WR-CALNAME:${esc(name)}`];
63
+ for (const e of events.filter((e) => WHEN.test(e.start) && LENGTH.test(e.duration))) {
64
+ lines.push(
65
+ "BEGIN:VEVENT",
66
+ `UID:${esc(e.uid)}`,
67
+ `DTSTAMP:${now}`,
68
+ e.zone && ZONE.test(e.zone) ? `DTSTART;TZID=${e.zone}:${stamp(e.start)}` : `DTSTART:${stamp(e.start)}`,
69
+ `DURATION:${e.duration}`,
70
+ `SUMMARY:${esc(e.title)}`,
71
+ );
72
+ if (e.location) lines.push(`LOCATION:${esc(e.location)}`);
73
+ if (e.description) lines.push(`DESCRIPTION:${esc(e.description)}`);
74
+ if (e.url && /^https?:\/\/[^\s]+$/.test(e.url)) lines.push(`URL:${e.url}`);
75
+ // its place's digits as they were given (35.6720), so a trip comes back as it went out: not mercator's geo,
76
+ // which reads them as numbers (35.672)
77
+ const geo = /^geo:(-?[\d.]+),(-?[\d.]+)/.exec(e.coordinates ?? "");
78
+ if (geo) lines.push(`GEO:${geo[1]};${geo[2]}`);
79
+ // who's on it, by name where it's known (quoted, so a comma or a colon in it is only a name)
80
+ for (const a of e.attendees ?? []) {
81
+ const cn = (a.name ?? "").replace(/["\r\n]/g, "");
82
+ lines.push(`ATTENDEE;${cn ? `CN="${cn}";` : ""}PARTSTAT=${a.answer}:${a.uri.replace(/[\s]/g, "")}`);
83
+ }
84
+ lines.push("END:VEVENT");
85
+ }
86
+ return [...lines, "END:VCALENDAR"].map(fold).join("\r\n") + "\r\n";
87
+ }
88
+
89
+ /** The time zones the world names (Asia/Tokyo), as this runtime knows them. */
90
+ const ZONES = new Set([...Intl.supportedValuesOf("timeZone"), "UTC"]);
91
+
92
+ export function fromIcs(text: string): IcsEvent[] {
93
+ const lines = text.replace(/\r?\n[ \t]/g, "").split(/\r?\n/);
94
+ const events: IcsEvent[] = [];
95
+ let at: Record<string, string> | null = null;
96
+ for (const line of lines) {
97
+ if (line === "BEGIN:VEVENT") at = {};
98
+ else if (line === "END:VEVENT" && at) {
99
+ const start = local(at.DTSTART ?? "");
100
+ if (start) {
101
+ const end = at.DTEND ? local(at.DTEND) : null;
102
+ const day = /^\d{8}$/.test(at.DTSTART!);
103
+ const duration = at.DURATION ?? (end ? `PT${minutes(start, end)}M` : day ? "P1D" : "PT0M");
104
+ const geo = at.GEO?.split(";");
105
+ // its zone: one the world names (an Outlook name, Tokyo Standard Time, is let go: floating), or UTC's Z, a
106
+ // time in UTC read as a wall clock in UTC
107
+ const zone = at.TZID && ZONES.has(at.TZID) ? at.TZID : /Z$/.test(at.DTSTART!) ? "UTC" : undefined;
108
+ events.push({
109
+ title: unesc(at.SUMMARY ?? "An event"),
110
+ start,
111
+ duration,
112
+ ...(at.LOCATION ? { location: unesc(at.LOCATION) } : {}),
113
+ ...(geo?.length === 2 ? { coordinates: `geo:${geo[0]},${geo[1]}` } : {}),
114
+ ...(zone ? { zone } : {}),
115
+ });
116
+ }
117
+ at = null;
118
+ } else if (at) {
119
+ const colon = line.indexOf(":");
120
+ if (colon > 0) {
121
+ const [name, ...params] = line.slice(0, colon).split(";");
122
+ at[name!.toUpperCase()] = line.slice(colon + 1);
123
+ // a start's zone, as its TZID names it
124
+ const tz = params.find((p) => /^TZID=/i.test(p));
125
+ if (name!.toUpperCase() === "DTSTART" && tz) at.TZID = tz.slice(5).replace(/^"|"$/g, "");
126
+ }
127
+ }
128
+ }
129
+ return events;
130
+ }