@yaks/render 0.0.0-stage → 0.2.36

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 (53) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +396 -2
  3. package/_dist/archetype.d.ts +3 -0
  4. package/_dist/archetype.d.ts.map +1 -0
  5. package/_dist/component.d.ts +15 -0
  6. package/_dist/component.d.ts.map +1 -0
  7. package/_dist/edit.d.ts +17 -0
  8. package/_dist/edit.d.ts.map +1 -0
  9. package/_dist/host.d.ts +59 -0
  10. package/_dist/host.d.ts.map +1 -0
  11. package/_dist/mod.d.ts +72 -0
  12. package/_dist/mod.d.ts.map +1 -0
  13. package/_dist/namespace.d.ts +12 -0
  14. package/_dist/namespace.d.ts.map +1 -0
  15. package/_dist/prepare.d.ts +11 -0
  16. package/_dist/prepare.d.ts.map +1 -0
  17. package/_dist/types.d.ts +66 -0
  18. package/_dist/types.d.ts.map +1 -0
  19. package/_dist/views.d.ts +88 -0
  20. package/_dist/views.d.ts.map +1 -0
  21. package/archetype.js +56 -0
  22. package/archetype.js.map +1 -0
  23. package/archetype.ts +76 -0
  24. package/component.js +39 -0
  25. package/component.js.map +1 -0
  26. package/component.ts +84 -0
  27. package/deno.json +1 -0
  28. package/edit.js +88 -0
  29. package/edit.js.map +1 -0
  30. package/edit.ts +113 -0
  31. package/host.js +191 -0
  32. package/host.js.map +1 -0
  33. package/host.ts +277 -0
  34. package/mod.js +158 -0
  35. package/mod.js.map +1 -0
  36. package/mod.ts +328 -0
  37. package/namespace.js +90 -0
  38. package/namespace.js.map +1 -0
  39. package/namespace.ts +121 -0
  40. package/package.json +31 -4
  41. package/prepare.js +32 -0
  42. package/prepare.js.map +1 -0
  43. package/prepare.ts +55 -0
  44. package/prop.js +37 -0
  45. package/prop.js.map +1 -0
  46. package/prop.ts +46 -0
  47. package/types.js +7 -0
  48. package/types.js.map +1 -0
  49. package/types.ts +116 -0
  50. package/views.js +333 -0
  51. package/views.js.map +1 -0
  52. package/views.ts +389 -0
  53. package/vocab.json +29 -0
package/views.ts ADDED
@@ -0,0 +1,389 @@
1
+ // The views every entity has, whatever it is made of: a `Tile` that stands for
2
+ // it in a list, a `Page` that shows it whole, the `Facts` its components state,
3
+ // and a `Comment` for an entity aimed at another. Portable @yaks/render
4
+ // renderers — they build with the caller's `h`, so a terminal prints them
5
+ // through @yaks/text or paints them through @yaks/tui (dressed by `sheet`, in a
6
+ // theme's colours), and a browser mounts them through @yaks/preact.
7
+ //
8
+ // Inline parts are separated by a space of their own: a browser lays them out
9
+ // with CSS and ignores it, and a terminal has nothing else to go by.
10
+ //
11
+ // Every match here is the catch-all (`true`) or one component, so a package
12
+ // that knows its own components better wins by specificity: @yaks/doc's
13
+ // `Title` and `Body`, @yaks/task's `Status`. A view nobody registered renders
14
+ // nothing, which is how a page with no body shows no body.
15
+ //
16
+ // What a view cannot know from one bundle arrives in the context, as a
17
+ // {@link Shown}: how an id reads, where a link goes, what a referenced entity
18
+ // is called, how a time reads — and the other entities a page gathers (its
19
+ // relations and its comments), drawn through `show` so they go through the
20
+ // same registry as everything else.
21
+ //
22
+ // A long text in a list is a passage (`passage`), not the text whole: a
23
+ // package that owns words (an entry's, a comment's, a document's content)
24
+ // registers its `Tile` with it.
25
+
26
+ import { parse } from '@yaks/query/core'
27
+ import { snippet } from '@yaks/graph/hit'
28
+ import {
29
+ type Bundle,
30
+ type Child,
31
+ define,
32
+ type H,
33
+ type Registry,
34
+ type RenderContext,
35
+ type Renderer,
36
+ type Rendering,
37
+ } from './mod.js'
38
+
39
+ /** One group of related entities on a page: `requires`, `requires this`. */
40
+ export type Related = { title: string; items: Bundle[] }
41
+
42
+ /** What the caller supplies so a view can show an entity among others. */
43
+ export type Shown<Node> = {
44
+ /** the id a person reads: `T-9` */
45
+ id: (b: Bundle) => string
46
+ /** the kind it displays as: `task` */
47
+ kind: (b: Bundle) => string
48
+ /** what to call an entity a property names, by eid */
49
+ name: (eid: string) => string
50
+ /** where a link to an entity goes, by eid; absent where nothing links */
51
+ link?: (eid: string) => string | undefined
52
+ /** how a moment reads */
53
+ when: (at: string) => string
54
+ /** another entity, drawn through the same registry */
55
+ show: (b: Bundle, view: string) => Node | null
56
+ /** the relation an edge states: `contains` */
57
+ relation?: (b: Bundle) => string | undefined
58
+ /** a reference's complete bundle, when this host holds it */
59
+ get?: (eid: string) => Bundle | undefined
60
+ /** the rows a query answers, when this host holds them: a host that can
61
+ * read more reads what a view asked for and draws it again */
62
+ query?: (q: string) => Bundle[] | undefined
63
+ /** Entities pointing here through a relation, where the host holds its links. */
64
+ related?: (eid: string, relation: string) => Bundle[]
65
+ /** the groups of entities related to this one, for `Page` */
66
+ relations?: Related[]
67
+ /** the comments aimed at this one, oldest first, for `Page` */
68
+ comments?: Bundle[]
69
+ }
70
+
71
+ type Ctx<Node> = RenderContext<Node> & Shown<Node>
72
+
73
+ // The context a renderer is handed carries the caller's fields unchecked; this
74
+ // is where they are read back as what the caller promised.
75
+ let shown = <Node>(ctx: RenderContext<Node>) => ctx as Ctx<Node>
76
+
77
+ // Components a page states elsewhere, and the edge an entity may itself be.
78
+ let SHOWN = new Set(['entity', 'doc', 'comment', 'edge'])
79
+ let EID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/
80
+ let AT = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}/
81
+
82
+ // One stated value: an entity named by a property is a link to it, a moment
83
+ // reads as the caller says, and anything structured is its JSON.
84
+ let value = <Node>(h: H<Node>, s: Ctx<Node>, v: unknown): Child<Node> => {
85
+ if (typeof v == 'string' && EID.test(v)) {
86
+ return s.show(s.get?.(v) ?? { entity: { eid: v } }, 'Reference.Inline')
87
+ }
88
+ if (typeof v == 'string' && AT.test(v)) {
89
+ return h('time', { datetime: v }, s.when(v))
90
+ }
91
+ return v && typeof v == 'object' ? JSON.stringify(v) : String(v)
92
+ }
93
+
94
+ let section = <Node>(h: H<Node>, title: string, ...kids: Child<Node>[]) =>
95
+ h(
96
+ 'section',
97
+ { class: 'Section' },
98
+ h('h2', { class: 'Section_Title' }, title),
99
+ ...kids,
100
+ )
101
+
102
+ // An entity with no title of its own is called by its id — except beside its
103
+ // id in a `Tile`, where it is called by its kind rather than by its id twice.
104
+ let title = <Node>(b: Bundle, h: H<Node>, ctx: RenderContext<Node>): Node =>
105
+ h(
106
+ 'span',
107
+ null,
108
+ ctx.in == 'Reference'
109
+ ? shown(ctx).name(b.entity.eid)
110
+ : ctx.in == 'Tile'
111
+ ? shown(ctx).kind(b)
112
+ : shown(ctx).id(b),
113
+ )
114
+
115
+ // An edge is titled by the sentence it states: `T-1 requires T-2`.
116
+ let sentence = <Node>(
117
+ b: Bundle,
118
+ h: H<Node>,
119
+ ctx: RenderContext<Node>,
120
+ ): Node => {
121
+ let s = shown(ctx)
122
+ let { from, to } = b.edge as { from?: string; to?: string }
123
+ return h(
124
+ 'span',
125
+ null,
126
+ from ? value(h, s, from) : '?',
127
+ ' ',
128
+ s.relation?.(b) ?? s.kind(b),
129
+ ' ',
130
+ to ? value(h, s, to) : '?',
131
+ )
132
+ }
133
+
134
+ let inline = <Node>(b: Bundle, h: H<Node>, ctx: RenderContext<Node>): Node =>
135
+ h(
136
+ 'a',
137
+ { href: shown(ctx).link?.(b.entity.eid) },
138
+ ctx.render?.('Title', { in: 'Reference' }),
139
+ )
140
+
141
+ let tile = <Node>(b: Bundle, h: H<Node>, ctx: RenderContext<Node>): Node => {
142
+ let s = shown(ctx)
143
+ return h(
144
+ 'a',
145
+ { class: 'Tile', href: s.link?.(b.entity.eid) },
146
+ h('span', { class: 'Tile_Id' }, s.id(b)),
147
+ ' ',
148
+ h('span', { class: 'Tile_Title' }, ctx.render?.('Title', { in: 'Tile' })),
149
+ ' ',
150
+ ctx.render?.('Status'),
151
+ )
152
+ }
153
+
154
+ // Each component the page does not state elsewhere, one row each: its name,
155
+ // then its properties with a value. A component with no properties is a mark,
156
+ // and
157
+ // its row says only that it is there.
158
+ let facts = <Node>(b: Bundle, h: H<Node>, ctx: RenderContext<Node>): Node => {
159
+ let s = shown(ctx)
160
+ let rows = Object.entries(b)
161
+ .filter(([name, comp]) =>
162
+ !SHOWN.has(name) && !name.startsWith('$') && !!comp &&
163
+ typeof comp == 'object'
164
+ )
165
+ .map(([name, comp]) => {
166
+ let props = Object.entries(comp as Record<string, unknown>)
167
+ .filter(([, v]) => v != null && v !== '')
168
+ return [
169
+ h('dt', null, name),
170
+ h(
171
+ 'dd',
172
+ null,
173
+ props.length
174
+ ? props.map(([prop, v], i) => [
175
+ i ? ' · ' : '',
176
+ h('span', { class: 'Facts_Prop' }, prop),
177
+ ' ',
178
+ value(h, s, v),
179
+ ])
180
+ : '✓',
181
+ ),
182
+ ]
183
+ })
184
+ return h('dl', { class: 'Facts' }, rows)
185
+ }
186
+
187
+ let comment = <Node>(b: Bundle, h: H<Node>, ctx: RenderContext<Node>): Node => {
188
+ let s = shown(ctx)
189
+ let made = (b.created ?? {}) as { by?: string; at?: string }
190
+ return h(
191
+ 'article',
192
+ { class: 'Comment', id: s.id(b) },
193
+ h(
194
+ 'header',
195
+ { class: 'Comment_Head' },
196
+ made.by ? value(h, s, made.by) : 'someone',
197
+ made.at ? [' · ', value(h, s, made.at)] : null,
198
+ ),
199
+ ctx.render?.('Body'),
200
+ )
201
+ }
202
+
203
+ /** Where a passage reads its words, and what it says of them: the texts the
204
+ * words may be in, the first that matches what was sought giving the passage;
205
+ * the title, when the words are a document's own; what kind of words they
206
+ * are, when not plain prose (`tool result`); and the entity they sit in, with
207
+ * the word that says how (`in` a transcript, `on` what a comment is aimed
208
+ * at). */
209
+ export type Said = {
210
+ texts: (b: Bundle) => unknown[]
211
+ title?: (b: Bundle) => unknown
212
+ what?: (b: Bundle) => string | undefined
213
+ where?: (b: Bundle) => [word: string, eid: string] | undefined
214
+ }
215
+
216
+ // Who made an entity and when, as its `created` says.
217
+ let made = (b: Bundle) => {
218
+ let { by, at } = (b.created ?? {}) as { by?: unknown; at?: unknown }
219
+ return {
220
+ by: typeof by == 'string' && by ? by : undefined,
221
+ at: typeof at == 'string' && at ? at : undefined,
222
+ }
223
+ }
224
+
225
+ // The runs a snippet sets between \x01 and \x02, each a match.
226
+ let marks = <Node>(h: H<Node>, cut: string): Child<Node>[] =>
227
+ cut.split('\x01').flatMap((chunk, i) => {
228
+ if (!i) return chunk ? [chunk] : []
229
+ let [found, rest = ''] = chunk.split('\x02')
230
+ return [h('mark', { class: 'Match' }, found), ...rest ? [rest] : []]
231
+ })
232
+
233
+ // What an entity is called in a line of text: its `Title`, where this host
234
+ // holds it, so a package that names its entities better than by their id
235
+ // (a session by what it works on) is heard; else what the host calls it.
236
+ let called = <Node>(s: Ctx<Node>, eid: string): Child<Node> => {
237
+ let held = s.get?.(eid)
238
+ return (held && s.show(held, 'Title')) ?? s.name(eid)
239
+ }
240
+
241
+ /**
242
+ * A long text as one row of a list (@yaks/ui's `Passage`): a line saying who
243
+ * said it, when and where, then the words around what was sought, every match
244
+ * marked; with nothing sought, the words it opens with. What was sought is the
245
+ * caller's `ctx.words`, the words of its query. The row names the entities it
246
+ * mentions, so it asks its host for them (`needs`).
247
+ *
248
+ * ```ts
249
+ * import { define } from '@yaks/render'
250
+ * import { passage } from '@yaks/render/views'
251
+ * import { parse } from '@yaks/query'
252
+ * import { plain, tree } from '@yaks/text'
253
+ * import { loadVocab } from '@yaks/vocab'
254
+ * import { equal } from '@yaks/testing'
255
+ *
256
+ * let said = passage({
257
+ * texts: (b) => [(b.note as { text?: string }).text],
258
+ * where: (b) => ['on', String((b.note as { on?: string }).on)],
259
+ * })
260
+ * let views = define([{ view: 'Tile', match: parse('.note'), ...said }])
261
+ * let note = {
262
+ * entity: { eid: 'n1' },
263
+ * note: { text: 'Results are often miles long.', on: 't1' },
264
+ * created: { by: 'p1', at: '2026-10-23T18:26:21Z' },
265
+ * }
266
+ * let ctx = {
267
+ * name: (eid: string) => ({ p1: 'Jeff', t1: 'T-9' })[eid] ?? eid,
268
+ * when: () => '2 hours ago',
269
+ * words: ['miles'],
270
+ * }
271
+ * let vocab = loadVocab({
272
+ * $defs: {
273
+ * note: {
274
+ * component: true,
275
+ * properties: { text: { type: 'string' }, on: { type: 'string' } },
276
+ * },
277
+ * },
278
+ * })
279
+ * equal(
280
+ * plain(tree(views, note, 'Tile', vocab, ctx)),
281
+ * 'Jeff · 2 hours ago · on T-9 Results are often miles long.',
282
+ * )
283
+ * equal(said.needs!(note), ['p1', 't1'])
284
+ * ```
285
+ */
286
+ export let passage = (
287
+ said: Said,
288
+ ): { render: Rendering; needs: NonNullable<Renderer['needs']> } => ({
289
+ needs: (b) =>
290
+ [made(b).by, said.where?.(b)?.[1]].filter((e): e is string => !!e),
291
+ render: <Node>(b: Bundle, h: H<Node>, ctx: RenderContext<Node>): Node => {
292
+ let s = shown(ctx)
293
+ let words = Array.isArray(ctx.words) ? ctx.words.map(String) : []
294
+ let cuts = said.texts(b)
295
+ .filter((t): t is string => typeof t == 'string' && !!t.trim())
296
+ .map((t) => snippet(t, words))
297
+ let cut = cuts.find((c) => c.includes('\x01')) ?? cuts[0] ?? ''
298
+ let { by, at } = made(b)
299
+ let title = said.title?.(b)
300
+ let what = said.what?.(b)
301
+ let where = said.where?.(b)
302
+ let parts = [
303
+ typeof title == 'string' && title &&
304
+ h('span', { class: 'Passage_Title' }, ...marks(h, snippet(title, words))),
305
+ by && h('span', { class: 'Passage_Who' }, s.name(by)),
306
+ what && h('span', null, what),
307
+ at && h('time', { datetime: at }, s.when(at)),
308
+ where && h('span', null, `${where[0]} `, called(s, where[1])),
309
+ ].filter((p): p is Node => !!p)
310
+ return h(
311
+ 'a',
312
+ { class: 'Passage', href: s.link?.(b.entity.eid) },
313
+ h(
314
+ 'span',
315
+ { class: 'Passage_By' },
316
+ ...parts.flatMap((p, i) =>
317
+ i ? [' ', h('span', { class: 'Passage_Sep' }, '·'), ' ', p] : [p]
318
+ ),
319
+ ),
320
+ ' ',
321
+ h('span', { class: 'Passage_Text' }, ...marks(h, cut)),
322
+ )
323
+ },
324
+ })
325
+
326
+ let page = <Node>(b: Bundle, h: H<Node>, ctx: RenderContext<Node>): Node => {
327
+ let s = shown(ctx)
328
+ let comments = s.comments ?? []
329
+ let tiles = (items: Bundle[]) =>
330
+ h(
331
+ 'ul',
332
+ { class: 'Section_List' },
333
+ items.map((i) => h('li', null, s.show(i, 'Tile'))),
334
+ )
335
+ return h(
336
+ 'article',
337
+ { class: 'Page' },
338
+ h(
339
+ 'header',
340
+ { class: 'Page_Head' },
341
+ h('span', { class: 'Page_Kind' }, s.kind(b)),
342
+ ' ',
343
+ h('span', { class: 'Page_Id' }, s.id(b)),
344
+ ' ',
345
+ ctx.render?.('Status'),
346
+ ),
347
+ h('h1', { class: 'Page_Title' }, ctx.render?.('Title')),
348
+ h('div', { class: 'Page_Body' }, ctx.render?.('Body')),
349
+ (s.relations ?? [])
350
+ .filter((r) => r.items.length)
351
+ .map((r) => section(h, r.title, tiles(r.items))),
352
+ comments.length
353
+ ? section(
354
+ h,
355
+ `${comments.length} comment${comments.length == 1 ? '' : 's'}`,
356
+ comments.map((c) => s.show(c, 'Comment')),
357
+ )
358
+ : null,
359
+ section(h, 'Details', ctx.render?.('Facts')),
360
+ )
361
+ }
362
+
363
+ /** The views any entity has. Register a package's own views ahead of these. */
364
+ export let views: Registry = define([
365
+ { view: 'Title', match: true, render: title },
366
+ { view: 'Title', match: parse('.edge'), render: sentence },
367
+ { view: 'Tile', match: true, render: tile },
368
+ { view: 'Inline', match: true, render: inline },
369
+ { view: 'Facts', match: true, render: facts },
370
+ { view: 'Comment', match: parse('.comment'), render: comment },
371
+ { view: 'Page', match: true, render: page },
372
+ ])
373
+
374
+ /** How these views dress in a terminal (@yaks/tui's painter): their own class
375
+ * names, in the colours of a theme (@yaks/ui `Colors`), named rather than
376
+ * imported: @yaks/tui builds on this package. A page's head, title and body
377
+ * and each section end with a blank line, where a browser has margins. */
378
+ export let sheet = (
379
+ c: Record<'dim' | 'heading' | 'muted', string>,
380
+ ): Record<string, { fg?: string; gap?: boolean }> => ({
381
+ Page_Head: { fg: c.dim, gap: true },
382
+ Page_Title: { gap: true },
383
+ Page_Body: { gap: true },
384
+ Section: { gap: true },
385
+ Section_Title: { fg: c.heading },
386
+ Facts_Prop: { fg: c.muted },
387
+ Tile_Id: { fg: c.dim },
388
+ Comment_Head: { fg: c.dim },
389
+ })
package/vocab.json ADDED
@@ -0,0 +1,29 @@
1
+ {
2
+ "$id": "urn:uuid:419c8402-7791-4ea1-a4a1-75c4558b967b",
3
+ "$defs": {
4
+ "prop": {
5
+ "$id": "urn:uuid:b5f5bc11-a900-4348-8f55-9bf1c02e5fff",
6
+ "component": true,
7
+ "journal": false,
8
+ "type": "object",
9
+ "properties": {
10
+ "comp": {
11
+ "$id": "urn:uuid:ce5084b3-b0f4-440f-97f1-fa8cd4a022b9",
12
+ "type": "string"
13
+ },
14
+ "prop": {
15
+ "$id": "urn:uuid:f0930224-6e2d-492d-b1e0-cbadde5a8b59",
16
+ "type": "string"
17
+ },
18
+ "type": {
19
+ "$id": "urn:uuid:11a31394-83d8-4671-964b-ba1b4d04edc2",
20
+ "type": "string"
21
+ },
22
+ "ref": {
23
+ "$id": "urn:uuid:ba5ebc4f-6d3e-4d98-bb73-449f50c1990a",
24
+ "type": "string"
25
+ }
26
+ }
27
+ }
28
+ }
29
+ }