@skillstech/thunderlang 1.4.0 → 1.4.2
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.
- package/CHANGELOG.md +47 -0
- package/dist/core.cjs +222 -33
- package/dist/index.cjs +1 -1
- package/dist/learn.cjs +3355 -1184
- package/package.json +15 -2
- package/src/core.d.ts +4 -0
- package/src/core.mjs +2 -0
- package/src/emit.mjs +1 -1
- package/src/learn-lessons.mjs +145 -0
- package/src/learn.mjs +17 -0
- package/src/react/reading.css +221 -0
- package/src/react/reading.d.mts +25 -0
- package/src/react/reading.mjs +249 -0
- package/src/reading-layout.d.mts +68 -0
- package/src/reading-layout.mjs +182 -0
- package/src/write-across.mjs +13 -3
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
// @skillstech/thunderlang/react: the Thunder drawing kit. One set of components that OpenThunder, thunderlang.dev,
|
|
2
|
+
// and the Skills Tech Talk course all use, so a reading looks and reads the same everywhere. Every word and its
|
|
3
|
+
// order come from readingLayout (../reading-layout.mjs); these components only turn that data into elements.
|
|
4
|
+
// Styling: plain `tl-` class names and one stylesheet (@skillstech/thunderlang/react/reading.css) whose colors are
|
|
5
|
+
// CSS variables, so each app sets its own (STT's --language-accent is honored). No JSX, no build step, no Tailwind:
|
|
6
|
+
// React is the only dependency, and it is the app's own (a peer dependency).
|
|
7
|
+
import { createElement as h, Fragment, useLayoutEffect, useRef, useState } from 'react';
|
|
8
|
+
import { THUNDER_VISUAL } from '../visual.mjs';
|
|
9
|
+
import { readingLayout, thingCard, dataCard, ioBoxes, detailsLayout, flowEdgeLabel, READING_LABELS } from '../reading-layout.mjs';
|
|
10
|
+
|
|
11
|
+
export { readingLayout, thingCard, dataCard, ioBoxes, detailsLayout, flowEdgeLabel, READING_LABELS };
|
|
12
|
+
|
|
13
|
+
/** A Thunder symbol, from the grammar's own path data. Its color is the CSS variable for its grammar color. */
|
|
14
|
+
export function ThunderSymbol({ id, size = 28, label }) {
|
|
15
|
+
const s = THUNDER_VISUAL.symbols[id];
|
|
16
|
+
if (!s) return null;
|
|
17
|
+
return h('svg', {
|
|
18
|
+
width: size, height: size, viewBox: '0 0 44 44', fill: 'none', stroke: 'currentColor', strokeWidth: 2.6, strokeLinejoin: 'round', strokeLinecap: 'round',
|
|
19
|
+
strokeDasharray: s.dashed ? '4 4' : undefined, className: `tl-symbol tl-c-${s.color}`,
|
|
20
|
+
role: label ? 'img' : undefined, 'aria-label': label, 'aria-hidden': label ? undefined : true, focusable: 'false',
|
|
21
|
+
},
|
|
22
|
+
...s.paths.map((d, i) => h('path', { key: `p${i}`, d })),
|
|
23
|
+
...(s.circles ?? []).map(([cx, cy, r], i) => h('circle', { key: `c${i}`, cx, cy, r, fill: s.fill ? 'currentColor' : 'none' })));
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const knowsItem = (k) => h('li', { key: k.name, className: 'tl-knows-item' },
|
|
27
|
+
h('span', { className: 'tl-knows-name' }, k.label),
|
|
28
|
+
k.detail ? h('span', { className: 'tl-knows-detail' }, `: ${k.detail}`) : null);
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* A Thing (a class): what it knows and what it can do. With `onOpen`, each ability and "How a new … starts" is a
|
|
32
|
+
* button that calls onOpen(memberId); without it they are plain text.
|
|
33
|
+
* card: thingCard(model, thing), or pass `model` and `thing` and it is worked out here.
|
|
34
|
+
*/
|
|
35
|
+
export function ThingCard({ card, model, thing, onOpen, selected = false, headingLevel = 2 }) {
|
|
36
|
+
const c = card ?? thingCard(model, thing);
|
|
37
|
+
const hid = `tl-thing-${c.id}`;
|
|
38
|
+
const ability = (m) => h(Fragment, null,
|
|
39
|
+
h(ThunderSymbol, { id: m.symbol, size: 24 }),
|
|
40
|
+
h('span', { className: 'tl-ability-label' }, m.label),
|
|
41
|
+
onOpen ? h('span', { className: 'tl-ability-go', 'aria-hidden': true }, '→') : null);
|
|
42
|
+
return h('section', { 'aria-labelledby': hid, className: `tl-card tl-thing${selected ? ' tl-selected' : ''}` },
|
|
43
|
+
h('div', { className: 'tl-card-head' },
|
|
44
|
+
h(ThunderSymbol, { id: 'thing', size: 44, label: 'Thing' }),
|
|
45
|
+
h('div', null,
|
|
46
|
+
h('p', { className: 'tl-kicker' }, c.heading),
|
|
47
|
+
h(`h${headingLevel}`, { id: hid, className: 'tl-card-title' }, c.label))),
|
|
48
|
+
c.doc ? h('p', { className: 'tl-doc' }, c.doc) : null,
|
|
49
|
+
c.knows.items.length ? h(Fragment, null,
|
|
50
|
+
h(`h${headingLevel + 1}`, { className: 'tl-section-label' }, c.knows.heading),
|
|
51
|
+
h('ul', { className: 'tl-knows' }, c.knows.items.map(knowsItem))) : null,
|
|
52
|
+
c.can.items.length ? h(Fragment, null,
|
|
53
|
+
h(`h${headingLevel + 1}`, { className: 'tl-section-label' }, c.can.heading),
|
|
54
|
+
h('ul', { className: 'tl-abilities' }, c.can.items.map((m) => h('li', { key: m.id },
|
|
55
|
+
onOpen
|
|
56
|
+
? h('button', { type: 'button', className: 'tl-ability tl-ability-button', onClick: () => onOpen(m.id) }, ability(m))
|
|
57
|
+
: h('div', { className: 'tl-ability' }, ability(m)))))) : null,
|
|
58
|
+
c.starts.id && onOpen
|
|
59
|
+
? h('button', { type: 'button', className: 'tl-starts tl-starts-button', onClick: () => onOpen(c.starts.id) }, h(ThunderSymbol, { id: 'setup', size: 18 }), c.starts.text)
|
|
60
|
+
: h('p', { className: 'tl-starts' }, h(ThunderSymbol, { id: 'setup', size: 18 }), c.starts.text));
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** Data: a record (facts, like an ID card) or choices (one allowed value). */
|
|
64
|
+
export function DataCard({ card, thing, headingLevel = 3 }) {
|
|
65
|
+
const c = card ?? dataCard(thing);
|
|
66
|
+
return h('section', { className: `tl-card tl-data tl-data-${c.kind}` },
|
|
67
|
+
h('div', { className: 'tl-card-head' },
|
|
68
|
+
h(ThunderSymbol, { id: c.symbol, size: 30, label: c.symbolLabel }),
|
|
69
|
+
h('div', null,
|
|
70
|
+
h('p', { className: 'tl-kicker tl-kicker-quiet' }, c.heading),
|
|
71
|
+
h(`h${headingLevel}`, { className: 'tl-card-title tl-card-title-small' }, c.label))),
|
|
72
|
+
c.doc ? h('p', { className: 'tl-doc tl-doc-small' }, c.doc) : null,
|
|
73
|
+
c.choices.length ? h('ul', { className: 'tl-choices' }, c.choices.map((x) => h('li', { key: x, className: 'tl-choice' }, x))) : null,
|
|
74
|
+
c.knows.length ? h('ul', { className: 'tl-knows tl-knows-small' }, c.knows.map(knowsItem)) : null);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** A "line N" button that opens the original code there, or nothing without onLine or a line. */
|
|
78
|
+
const LineButton = ({ line, onLine, className = 'tl-line' }) => (line == null || !onLine ? null
|
|
79
|
+
: h('button', { type: 'button', className, onClick: () => onLine(line), title: 'See the original code at this line' }, READING_LABELS.openLine(line)));
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* What goes in and out of one action or rule: You give it, You get back, It can stop with, and "Part of <Thing>".
|
|
83
|
+
* card: ioBoxes(model, member), or pass `model` and `member`.
|
|
84
|
+
* onLine(line) makes each stop's line a button; onThing(thingId) makes "Part of …" a button back to its card.
|
|
85
|
+
* children: anything the app adds under the boxes (an AI summary, nearby rules).
|
|
86
|
+
*/
|
|
87
|
+
export function IOBoxes({ card, model, member, onLine, onThing, headingLevel = 3, children }) {
|
|
88
|
+
const c = card ?? ioBoxes(model, member);
|
|
89
|
+
const H = `h${headingLevel}`;
|
|
90
|
+
return h('section', { className: 'tl-card tl-io-card', 'aria-label': READING_LABELS.io },
|
|
91
|
+
c.doc ? h('p', { className: 'tl-doc tl-io-doc' }, c.doc) : null,
|
|
92
|
+
h('div', { className: 'tl-io' },
|
|
93
|
+
h('section', { className: 'tl-io-box tl-io-give' },
|
|
94
|
+
h(H, { className: 'tl-io-label' }, h('span', { 'aria-hidden': true }, '→ '), c.give.heading),
|
|
95
|
+
c.give.items.length
|
|
96
|
+
? h('ul', { className: 'tl-pills' }, c.give.items.map((g) => h('li', { key: g.label, className: 'tl-pill tl-pill-fact' }, g.label, g.kind ? h('span', { className: 'tl-pill-kind' }, `: ${g.kind}`) : null)))
|
|
97
|
+
: h('p', { className: 'tl-quiet' }, c.give.empty)),
|
|
98
|
+
h('section', { className: 'tl-io-box tl-io-back' },
|
|
99
|
+
h(H, { className: 'tl-io-label' }, h('span', { 'aria-hidden': true }, '⇥ '), c.getBack.heading),
|
|
100
|
+
c.getBack.items.length ? h('ul', { className: 'tl-pills' }, c.getBack.items.map((t) => h('li', { key: t, className: 'tl-pill tl-pill-outcome' }, t))) : h('p', { className: 'tl-quiet' }, c.getBack.empty))),
|
|
101
|
+
c.stops.items.length ? h('section', { className: 'tl-io-box tl-io-stops' },
|
|
102
|
+
h(H, { className: 'tl-io-label' }, h(ThunderSymbol, { id: 'stop', size: 14 }), ' ', c.stops.heading),
|
|
103
|
+
h('ul', { className: 'tl-stop-list' }, c.stops.items.map((x, i) => h('li', { key: i, className: 'tl-stop-row' },
|
|
104
|
+
h(ThunderSymbol, { id: 'stop', size: 14 }), h('span', { className: 'tl-stop-text' }, x.text), h(LineButton, { line: x.line, onLine }))))) : null,
|
|
105
|
+
children ?? null,
|
|
106
|
+
c.partOf ? (onThing
|
|
107
|
+
? h('button', { type: 'button', className: 'tl-part-of tl-part-of-button', onClick: () => onThing(c.partOf.id) }, h(ThunderSymbol, { id: 'thing', size: 18 }), c.partOf.text)
|
|
108
|
+
: h('p', { className: 'tl-part-of' }, h(ThunderSymbol, { id: 'thing', size: 18 }), c.partOf.text)) : null);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* The Big Picture, zoomed by `focus` (see readingLayout): Things and Data, or, for an action or rule, its IO boxes.
|
|
113
|
+
*/
|
|
114
|
+
export function BigPicture({ model, layout, focus = null, onOpen, onLine, onThing, selectedId, headingLevel = 2, children }) {
|
|
115
|
+
const l = layout ?? readingLayout(model, { focus });
|
|
116
|
+
return h('div', { className: 'tl-reading tl-big-picture' }, l.sections.map((s) => h('section', { key: s.id, className: `tl-bp-section tl-bp-${s.id}`, 'aria-label': s.heading },
|
|
117
|
+
s.id === 'io'
|
|
118
|
+
? s.cards.map((c) => h(IOBoxes, { key: c.id, card: c, onLine, onThing, headingLevel: headingLevel + 1 }, children))
|
|
119
|
+
: h('div', { className: s.id === 'things' ? 'tl-bp-things' : 'tl-bp-data' }, s.cards.map((c) => (c.kind === 'thing'
|
|
120
|
+
? h(ThingCard, { key: c.id, card: c, onOpen, selected: c.id === selectedId, headingLevel })
|
|
121
|
+
: h(DataCard, { key: c.id, card: c, headingLevel: headingLevel + 1 })))))));
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
const doesOf = (does) => THUNDER_VISUAL.does[does ?? ''] ?? { mark: '·', label: 'Does', color: 'structure' };
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Flow: one action or rule, step by step. Every node sits at the lane and row the compiler gives it, every edge is
|
|
128
|
+
* the compiler's (Yes / No / For each / Done / If it fails), each node's mark and color come from DOES. Steps Thunder
|
|
129
|
+
* could only show as code are dashed. The app adds only the interaction:
|
|
130
|
+
* flow: member.flow (or readLevel(model, 'flow', id).flow)
|
|
131
|
+
* onLine(line): each node's line opens the original code. onUse(memberId): a step that uses a rule opens it.
|
|
132
|
+
* marks + markAs: Thunder Diff highlights nodes as 'added' (New) or 'removed' (Gone).
|
|
133
|
+
* Edges are measured from the drawn nodes, so they appear after the first paint (none in server rendering).
|
|
134
|
+
*/
|
|
135
|
+
export function Flow({ flow, onLine, onUse, useLabel, marks, markAs }) {
|
|
136
|
+
const box = useRef(null);
|
|
137
|
+
const refs = useRef(new Map());
|
|
138
|
+
const [pos, setPos] = useState(() => new Map());
|
|
139
|
+
const [size, setSize] = useState({ w: 0, h: 0 });
|
|
140
|
+
const lanes = Math.max(1, ...flow.nodes.map((n) => n.lane + 1));
|
|
141
|
+
useLayoutEffect(() => {
|
|
142
|
+
const measure = () => {
|
|
143
|
+
const root = box.current?.getBoundingClientRect();
|
|
144
|
+
if (!root) return;
|
|
145
|
+
const next = new Map();
|
|
146
|
+
for (const [id, el] of refs.current) { const r = el.getBoundingClientRect(); next.set(id, { x: r.left - root.left, y: r.top - root.top, w: r.width, h: r.height }); }
|
|
147
|
+
setPos(next);
|
|
148
|
+
setSize({ w: box.current.clientWidth, h: box.current.scrollHeight });
|
|
149
|
+
};
|
|
150
|
+
measure();
|
|
151
|
+
const ro = typeof ResizeObserver !== 'undefined' ? new ResizeObserver(measure) : null;
|
|
152
|
+
if (ro && box.current) ro.observe(box.current);
|
|
153
|
+
return () => ro?.disconnect();
|
|
154
|
+
}, [flow]);
|
|
155
|
+
const edges = flow.edges.map((e, i) => {
|
|
156
|
+
const a = pos.get(e.from), b = pos.get(e.to);
|
|
157
|
+
if (!a || !b) return null;
|
|
158
|
+
const same = Math.abs(a.x - b.x) < 4;
|
|
159
|
+
const x1 = same ? a.x + a.w / 2 : a.x + a.w, y1 = same ? a.y + a.h : a.y + a.h / 2;
|
|
160
|
+
const x2 = same ? b.x + b.w / 2 : b.x + (b.x > a.x ? 0 : b.w), y2 = same ? b.y : b.y + b.h / 2;
|
|
161
|
+
const d = same ? `M${x1} ${y1} L${x2} ${y2 - 2}` : `M${x1} ${y1} C${(x1 + x2) / 2} ${y1}, ${(x1 + x2) / 2} ${y2}, ${x2 + (b.x > a.x ? -2 : 2)} ${y2}`;
|
|
162
|
+
const label = flowEdgeLabel(e);
|
|
163
|
+
const lx = same ? x1 + 8 : (x1 + x2) / 2, ly = same ? (y1 + y2) / 2 + 4 : Math.min(y1, y2) - 6 + Math.abs(y2 - y1) / 2;
|
|
164
|
+
return h('g', { key: i, className: `tl-edge tl-edge-${e.label ?? 'next'}` }, h('path', { d, markerEnd: 'url(#tl-arrow)' }), label ? h('text', { x: lx, y: ly }, label) : null);
|
|
165
|
+
});
|
|
166
|
+
return h('div', { className: 'tl-reading tl-flow', ref: box, style: { gridTemplateColumns: `repeat(${lanes}, minmax(200px, 1fr))` } },
|
|
167
|
+
h('svg', { className: 'tl-edges', width: size.w, height: size.h, 'aria-hidden': true },
|
|
168
|
+
h('defs', null, h('marker', { id: 'tl-arrow', viewBox: '0 0 10 10', refX: 9, refY: 5, markerWidth: 7, markerHeight: 7, orient: 'auto-start-reverse' }, h('path', { d: 'M0 0 L10 5 L0 10 z' }))),
|
|
169
|
+
edges),
|
|
170
|
+
flow.nodes.map((n) => {
|
|
171
|
+
const does = doesOf(n.does);
|
|
172
|
+
const color = n.kind === 'stop' ? 'failure' : n.kind === 'decision' ? 'question' : does.color;
|
|
173
|
+
const symbol = n.kind === 'stop' ? 'stop' : n.kind === 'decision' ? 'rule' : n.kind === 'start' ? 'action' : null;
|
|
174
|
+
const mark = marks?.has(n.id) ? markAs : undefined;
|
|
175
|
+
return h('div', {
|
|
176
|
+
key: n.id, ref: (el) => { if (el) refs.current.set(n.id, el); else refs.current.delete(n.id); },
|
|
177
|
+
className: `tl-node tl-node-${n.kind} tl-tone-${color}${n.code || n.kind === 'code' ? ' tl-node-code' : ''}${mark ? ` tl-mark-${mark}` : ''}`,
|
|
178
|
+
style: { gridRow: n.row + 1, gridColumn: n.lane + 1 },
|
|
179
|
+
},
|
|
180
|
+
h('span', { className: 'tl-node-mark', 'aria-hidden': true, title: does.label }, symbol ? h(ThunderSymbol, { id: symbol, size: 15 }) : does.mark),
|
|
181
|
+
mark ? h('span', { className: `tl-badge tl-badge-${mark}` }, mark === 'added' ? 'New' : 'Gone') : null,
|
|
182
|
+
h('span', { className: 'tl-node-text', title: n.text }, n.kind === 'code' ? h('code', null, n.text) : n.text),
|
|
183
|
+
n.uses && onUse ? h('button', { type: 'button', className: 'tl-node-use', onClick: () => onUse(n.uses), title: 'Open the rule this step uses, and try it' }, `${useLabel?.(n.uses) ?? READING_LABELS.openRule} →`) : null,
|
|
184
|
+
h(LineButton, { line: n.line, onLine }));
|
|
185
|
+
}));
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
function Whens({ when }) {
|
|
189
|
+
if (!when.length) return h('span', { className: 'tl-quiet' }, READING_LABELS.details.always);
|
|
190
|
+
return h(Fragment, null, when.map((w, k) => h('span', { key: k, className: 'tl-cond' },
|
|
191
|
+
h('span', { className: 'tl-cond-q' }, w.question), ' ',
|
|
192
|
+
h('span', { className: `tl-ans tl-ans-${w.answer === 'Yes' ? 'yes' : w.answer === 'No' ? 'no' : 'each'}` }, w.answer),
|
|
193
|
+
k < when.length - 1 ? h('span', { className: 'tl-cond-and', 'aria-hidden': true }, '·') : null)));
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* Details: the same action sorted into facts, each with its line. `view` is readLevel(model, 'details', memberId)
|
|
198
|
+
* (or pass `layout` from detailsLayout). Sections appear only when the compiler gives them.
|
|
199
|
+
* onLine(line) opens the original code. rules: the app's own cards for the rules it relies on, drawn in place of
|
|
200
|
+
* the plain "It relies on" list (OpenThunder's try-it cards).
|
|
201
|
+
*/
|
|
202
|
+
export function Details({ view, layout, onLine, rules, headingLevel = 3 }) {
|
|
203
|
+
const d = layout ?? detailsLayout(view);
|
|
204
|
+
const H = `h${headingLevel}`;
|
|
205
|
+
const card = (cls, heading, icon, ...body) => h('section', { className: `tl-card tl-details-card${cls ? ` ${cls}` : ''}` },
|
|
206
|
+
h(H, { className: 'tl-details-h' }, icon, heading), ...body);
|
|
207
|
+
const doesMark = (does, mark) => { const x = doesOf(does); return h('span', { className: `tl-does tl-c-${x.color}`, title: x.label }, mark || x.mark); };
|
|
208
|
+
return h('div', { className: 'tl-reading tl-details' },
|
|
209
|
+
h('div', { className: 'tl-stats', 'aria-label': d.heading }, d.stats.map((s) => h('span', { key: s.id, className: `tl-stat tl-stat-${s.id}` }, h('b', null, s.count), ` ${s.text}`))),
|
|
210
|
+
d.give ? h('div', { className: 'tl-io' },
|
|
211
|
+
card('tl-io-give', d.give.heading, h('span', { 'aria-hidden': true }, '→ '),
|
|
212
|
+
d.give.items.length ? h('ul', { className: 'tl-inputs' }, d.give.items.map((i) => h('li', { key: i.label },
|
|
213
|
+
h('span', { className: 'tl-pill tl-pill-fact' }, i.label),
|
|
214
|
+
i.lines.length && onLine ? h('span', { className: 'tl-used' }, `${READING_LABELS.details.usedOn} `, i.lines.map((l, k) => h(Fragment, { key: l }, k > 0 ? ', ' : null,
|
|
215
|
+
h('button', { type: 'button', className: 'tl-line tl-line-inline', onClick: () => onLine(l), title: 'See this line' }, l)))) : null)))
|
|
216
|
+
: h('p', { className: 'tl-quiet' }, d.give.empty)),
|
|
217
|
+
card('tl-io-back', d.getBack.heading, h('span', { 'aria-hidden': true }, '⇥ '),
|
|
218
|
+
d.getBack.items.length ? h('ul', { className: 'tl-rows' }, d.getBack.items.map((g, i) => h('li', { key: i },
|
|
219
|
+
h('div', { className: 'tl-row-top' }, h('span', { className: 'tl-strong' }, g.text), h(LineButton, { line: g.line, onLine })),
|
|
220
|
+
g.when ? h('div', { className: 'tl-when' }, h(Whens, { when: g.when })) : null)))
|
|
221
|
+
: h('p', { className: 'tl-quiet' }, d.getBack.empty))) : null,
|
|
222
|
+
d.stops ? card('tl-details-stops', d.stops.heading, h(ThunderSymbol, { id: 'stop', size: 15 }),
|
|
223
|
+
d.stops.items.length ? h('ul', { className: 'tl-rows' }, d.stops.items.map((s, i) => h('li', { key: i, className: 'tl-stop-row-detail' },
|
|
224
|
+
h('div', { className: 'tl-row-top' }, h('b', null, s.error), s.message ? h('span', { className: 'tl-message' }, `"${s.message}"`) : null, h(LineButton, { line: s.line, onLine })),
|
|
225
|
+
h('div', { className: 'tl-when' }, h(Whens, { when: s.when })))))
|
|
226
|
+
: h('p', { className: 'tl-quiet' }, d.stops.empty)) : null,
|
|
227
|
+
d.changes ? card(null, d.changes.heading, h('span', { 'aria-hidden': true }, '= '),
|
|
228
|
+
h('ul', { className: 'tl-rows' }, d.changes.items.map((c, i) => h('li', { key: i },
|
|
229
|
+
h('div', { className: 'tl-row-top' }, doesMark(c.does), h('span', { className: 'tl-strong' }, c.text), h(LineButton, { line: c.line, onLine })),
|
|
230
|
+
c.when.length ? h('div', { className: 'tl-when' }, h(Whens, { when: c.when })) : null)))) : null,
|
|
231
|
+
d.answers ? card(null, d.answers.heading, h(ThunderSymbol, { id: 'rule', size: 15 }),
|
|
232
|
+
h('ol', { className: 'tl-answers' }, d.answers.items.map((a, i) => h('li', { key: i }, h('b', null, a.answer), ' ', h('span', { className: 'tl-quiet' }, a.when))))) : null,
|
|
233
|
+
d.limits ? card(null, d.limits.heading, h(ThunderSymbol, { id: 'rule', size: 15 }),
|
|
234
|
+
h('p', { className: 'tl-quiet tl-limits-note' }, d.limits.note),
|
|
235
|
+
h('div', { className: 'tl-limits' }, d.limits.items.map((r, i) => h('div', { key: i, className: 'tl-limit' }, h('span', { className: 'tl-range' }, r.range), h('b', null, r.answer))))) : null,
|
|
236
|
+
d.steps ? card(null, d.steps.heading, h('span', { 'aria-hidden': true }, '≡ '),
|
|
237
|
+
h('ul', { className: 'tl-steps' }, d.steps.items.map((s) => h('li', { key: s.id, className: s.notExplained ? 'tl-step-code' : undefined, style: { paddingLeft: 4 + s.depth * 18 } },
|
|
238
|
+
doesMark(s.does, s.mark),
|
|
239
|
+
h('span', { className: 'tl-step-text' }, s.notExplained ? h('code', null, s.text) : s.text, s.via ? h('span', { className: 'tl-quiet' }, ` · ${s.via}`) : null),
|
|
240
|
+
h(LineButton, { line: s.line, onLine }))))) : null,
|
|
241
|
+
d.reliesOn && !rules ? card(null, d.reliesOn.heading, h(ThunderSymbol, { id: 'rule', size: 15 }),
|
|
242
|
+
h('ul', { className: 'tl-rows' }, d.reliesOn.items.map((r) => h('li', { key: r.id },
|
|
243
|
+
h('div', { className: 'tl-row-top' }, h(ThunderSymbol, { id: r.symbol, size: 14 }), h('b', null, r.label)),
|
|
244
|
+
r.says ? h('div', { className: 'tl-quiet' }, r.says) : null)))) : null,
|
|
245
|
+
rules ?? null,
|
|
246
|
+
d.usedBy ? h('p', { className: 'tl-quiet' }, `${d.usedBy.heading}: ${d.usedBy.items.map((u) => u.label).join(', ')}.`) : null,
|
|
247
|
+
d.openQuestions ? card('tl-details-open', d.openQuestions.heading, null, h('ul', { className: 'tl-rows' }, d.openQuestions.items.map((q) => h('li', { key: q }, q)))) : null,
|
|
248
|
+
d.source ? h('p', { className: 'tl-source' }, d.source) : null);
|
|
249
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import type { ThunderModel, Thing, Member } from './model.d.mts';
|
|
2
|
+
|
|
3
|
+
export const READING_LAYOUT_SCHEMA: 'thunder-reading-layout-v1';
|
|
4
|
+
export const READING_LABELS: Readonly<{
|
|
5
|
+
thing: string; knows: string; can: string; starts: (label: string) => string; startsWithKnows: (label: string) => string;
|
|
6
|
+
record: string; choices: string; recordSymbol: string; choicesSymbol: string; things: string; data: string;
|
|
7
|
+
give: string; getBack: string; stops: string; givesNothing: string; getsNothing: string; partOf: (label: string) => string; io: string;
|
|
8
|
+
edges: Readonly<Record<'yes' | 'no' | 'each' | 'done' | 'error' | 'nested' | 'case', string>>;
|
|
9
|
+
openLine: (line: number) => string; openRule: string;
|
|
10
|
+
details: Readonly<{
|
|
11
|
+
stops: string; noStops: string; noInputs: string; nothingBack: string; changes: string; answers: string; limits: (fact: string) => string;
|
|
12
|
+
limitsHow: string; steps: string; reliesOn: string; usedBy: string; openQuestions: string; always: string; otherwise: string;
|
|
13
|
+
when: (w: string) => string; usedOn: string; atAGlance: string;
|
|
14
|
+
}>;
|
|
15
|
+
}>;
|
|
16
|
+
export type ThunderSymbolId = 'app' | 'area' | 'file' | 'thing' | 'data' | 'record' | 'choices' | 'action' | 'rule' | 'steps' | 'setup' | 'stop' | 'not-read' | 'resource' | 'permission';
|
|
17
|
+
export interface KnowsLine { name: string; label: string; detail: string | null; secret?: true; line: number | null }
|
|
18
|
+
export interface ThingCardLayout {
|
|
19
|
+
kind: 'thing'; id: string; label: string; doc: string | null; heading: string; symbol: 'thing';
|
|
20
|
+
knows: { heading: string; items: KnowsLine[] };
|
|
21
|
+
can: { heading: string; items: { id: string; label: string; symbol: ThunderSymbolId }[] };
|
|
22
|
+
/** id: the setup member to open, or null when a new one starts with what it knows. */
|
|
23
|
+
starts: { id: string | null; text: string };
|
|
24
|
+
}
|
|
25
|
+
export interface DataCardLayout {
|
|
26
|
+
kind: 'record' | 'choices'; id: string; label: string; doc: string | null; heading: string;
|
|
27
|
+
symbol: 'record' | 'choices'; symbolLabel: string; choices: string[]; knows: KnowsLine[];
|
|
28
|
+
}
|
|
29
|
+
export interface IOBoxesLayout {
|
|
30
|
+
kind: 'io'; id: string; label: string; doc: string | null; symbol: ThunderSymbolId;
|
|
31
|
+
give: { heading: string; items: { label: string; kind: string | null }[]; empty: string };
|
|
32
|
+
/** text: what the code declares; items: that, else the Flow's end steps (what to draw). */
|
|
33
|
+
getBack: { heading: string; text: string | null; items: string[]; empty: string };
|
|
34
|
+
stops: { heading: string; items: { text: string; line: number | null }[] };
|
|
35
|
+
/** The Thing it belongs to, to link back to its card. */
|
|
36
|
+
partOf: { id: string; text: string } | null;
|
|
37
|
+
}
|
|
38
|
+
export interface ReadingLayout {
|
|
39
|
+
schema: 'thunder-reading-layout-v1';
|
|
40
|
+
focus: { kind: 'app' | 'file' | 'thing' | 'member' | 'unknown'; id: string | null };
|
|
41
|
+
sections: Array<{ id: 'things'; heading: string; cards: ThingCardLayout[] } | { id: 'data'; heading: string; cards: DataCardLayout[] } | { id: 'io'; heading: string; cards: IOBoxesLayout[] }>;
|
|
42
|
+
}
|
|
43
|
+
export interface When { question: string; answer: string }
|
|
44
|
+
export interface DetailsLayout {
|
|
45
|
+
id: string | null; title: string | null; heading: string;
|
|
46
|
+
stats: { id: 'inputs' | 'steps' | 'stops' | 'changes' | 'reliesOn' | 'notExplained'; count: number; text: string }[];
|
|
47
|
+
give: { heading: string; items: { label: string; lines: number[] }[]; empty: string } | null;
|
|
48
|
+
getBack: { heading: string; items: { text: string; line: number | null; when: When[] | null }[]; empty: string } | null;
|
|
49
|
+
stops: { heading: string; items: { error: string; message: string | null; line: number | null; when: When[] }[]; empty: string } | null;
|
|
50
|
+
changes: { heading: string; items: { text: string; does: string | null; line: number | null; when: When[] }[] } | null;
|
|
51
|
+
answers: { heading: string; items: { answer: string; when: string }[] } | null;
|
|
52
|
+
limits: { heading: string; note: string; items: { range: string; answer: string }[] } | null;
|
|
53
|
+
steps: { heading: string; items: { id: string; text: string; does: string | null; mark: string | null; depth: number; line: number | null; notExplained: boolean; via: string | null }[] } | null;
|
|
54
|
+
reliesOn: { heading: string; items: { id: string; label: string; symbol: ThunderSymbolId; says: string }[] } | null;
|
|
55
|
+
usedBy: { heading: string; items: { id: string; label: string }[] } | null;
|
|
56
|
+
openQuestions: { heading: string; items: string[] } | null;
|
|
57
|
+
source: string | null;
|
|
58
|
+
}
|
|
59
|
+
/** What a Flow edge says, or null for a plain next step. */
|
|
60
|
+
export function flowEdgeLabel(edge: { label?: string | null; text?: string }): string | null;
|
|
61
|
+
/** Details sorted for drawing; `view` is readLevel(model, 'details', memberId). */
|
|
62
|
+
export function detailsLayout(view: Record<string, any>): DetailsLayout;
|
|
63
|
+
export function memberSymbolId(member: { kind: string }): ThunderSymbolId;
|
|
64
|
+
export function thingCard(model: ThunderModel, thing: Thing): ThingCardLayout;
|
|
65
|
+
export function dataCard(thing: Thing): DataCardLayout;
|
|
66
|
+
export function ioBoxes(model: ThunderModel, member: Member): IOBoxesLayout;
|
|
67
|
+
/** The Big Picture, zoomed by focus: none or a file (Things, then records, then choices), a Thing (its card, then its file's Data), or an action/rule (its IO boxes). */
|
|
68
|
+
export function readingLayout(model: ThunderModel, options?: { focus?: string | null }): ReadingLayout;
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
// What a Thunder reading shows, and in what order, as plain data: the words on every card, which list comes first,
|
|
2
|
+
// and what each entry opens. Every app that draws a reading (OpenThunder, thunderlang.dev, the Skills Tech Talk
|
|
3
|
+
// course) draws from this, so the three say the same thing in the same order; the drawing kit
|
|
4
|
+
// (@skillstech/thunderlang/react) only turns it into elements. Pure, deterministic, browser-safe, no React.
|
|
5
|
+
|
|
6
|
+
export const READING_LAYOUT_SCHEMA = 'thunder-reading-layout-v1';
|
|
7
|
+
|
|
8
|
+
/** Every fixed word a reading shows. Change a label here and every app changes with it. */
|
|
9
|
+
export const READING_LABELS = Object.freeze({
|
|
10
|
+
thing: 'A Thing',
|
|
11
|
+
knows: 'It knows',
|
|
12
|
+
can: 'It can',
|
|
13
|
+
starts: (label) => `How a new ${label} starts`,
|
|
14
|
+
startsWithKnows: (label) => `A new ${label} starts with what it knows above`,
|
|
15
|
+
record: 'Data: a record',
|
|
16
|
+
choices: 'Data: choices',
|
|
17
|
+
recordSymbol: 'Record',
|
|
18
|
+
choicesSymbol: 'Choices',
|
|
19
|
+
things: 'Things',
|
|
20
|
+
data: 'Data',
|
|
21
|
+
give: 'You give it',
|
|
22
|
+
getBack: 'You get back',
|
|
23
|
+
stops: 'It can stop with',
|
|
24
|
+
givesNothing: 'Nothing',
|
|
25
|
+
getsNothing: 'Nothing it hands back',
|
|
26
|
+
partOf: (label) => `Part of ${label}`,
|
|
27
|
+
io: 'What goes in and out',
|
|
28
|
+
// Flow: the words on each kind of edge. An edge label not listed here is shown as the compiler wrote it.
|
|
29
|
+
edges: Object.freeze({ yes: 'Yes', no: 'No', each: 'For each', done: 'Done', error: 'If it fails', nested: 'Runs on its own', case: 'When' }),
|
|
30
|
+
openLine: (line) => `line ${line}`,
|
|
31
|
+
openRule: 'Open the rule',
|
|
32
|
+
// Details: what each section is called.
|
|
33
|
+
details: Object.freeze({
|
|
34
|
+
stops: 'When it stops', noStops: 'Thunder found no place where it stops early.',
|
|
35
|
+
noInputs: 'Thunder found no inputs. Check Original if it reads a request or a file.', nothingBack: 'Thunder found nothing it gives back.',
|
|
36
|
+
changes: 'What it changes, saves or sends', answers: 'Its answers', limits: (fact) => `Exact limits for ${fact}`,
|
|
37
|
+
limitsHow: 'Found by running the rule on every whole number around each limit.',
|
|
38
|
+
steps: 'Every step, with its line', reliesOn: 'It relies on', usedBy: 'Used by', openQuestions: 'Questions the code cannot answer',
|
|
39
|
+
always: 'Always', otherwise: 'otherwise', when: (w) => `when ${w}`, usedOn: 'used on', atAGlance: 'At a glance',
|
|
40
|
+
}),
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
/** The symbol (thunder-visual-v1) for a member, as every view draws it. */
|
|
44
|
+
export const memberSymbolId = (m) => (m.kind === 'action' || m.kind === 'pipeline' ? 'action' : m.kind === 'rule' ? 'rule' : m.kind === 'setup' ? 'setup' : m.kind === 'not-read' ? 'not-read' : 'steps');
|
|
45
|
+
|
|
46
|
+
/** One "It knows" line: the name, and its kind and starting value in words when the code says them. */
|
|
47
|
+
const knowsLine = (k) => {
|
|
48
|
+
const detail = [k.kind, k.start].filter(Boolean).join(', ');
|
|
49
|
+
return { name: k.name, label: k.label, detail: detail || null, ...(k.secret ? { secret: true } : {}), line: k.line ?? null };
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
/** A Thing card: what it knows, what it can do (each a door to open), and how a new one starts. */
|
|
53
|
+
export function thingCard(model, thing) {
|
|
54
|
+
const members = new Map(model.members.map((m) => [m.id, m]));
|
|
55
|
+
const can = thing.can.map((id) => members.get(id)).filter(Boolean);
|
|
56
|
+
const setup = can.find((m) => m.kind === 'setup') ?? null;
|
|
57
|
+
return {
|
|
58
|
+
kind: 'thing', id: thing.id, label: thing.label, doc: thing.doc ?? null,
|
|
59
|
+
heading: READING_LABELS.thing, symbol: 'thing',
|
|
60
|
+
knows: { heading: READING_LABELS.knows, items: thing.knows.map(knowsLine) },
|
|
61
|
+
can: { heading: READING_LABELS.can, items: can.filter((m) => m !== setup).map((m) => ({ id: m.id, label: `${m.label}${m.yesNo ? '?' : ''}`, symbol: memberSymbolId(m) })) },
|
|
62
|
+
starts: setup ? { id: setup.id, text: READING_LABELS.starts(thing.label) } : { id: null, text: READING_LABELS.startsWithKnows(thing.label) },
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** A Data card: a record (facts, like an ID card) or choices (one allowed value). */
|
|
67
|
+
export function dataCard(thing) {
|
|
68
|
+
const record = thing.kind === 'record';
|
|
69
|
+
return {
|
|
70
|
+
kind: record ? 'record' : 'choices', id: thing.id, label: thing.label, doc: thing.doc ?? null,
|
|
71
|
+
heading: record ? READING_LABELS.record : READING_LABELS.choices,
|
|
72
|
+
symbol: record ? 'record' : 'choices', symbolLabel: record ? READING_LABELS.recordSymbol : READING_LABELS.choicesSymbol,
|
|
73
|
+
choices: [...thing.choices],
|
|
74
|
+
knows: thing.knows.map(knowsLine),
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** What an action gives back: its declared result, else the end steps of its Flow, each once. */
|
|
79
|
+
const givesBackOf = (member) => {
|
|
80
|
+
const list = member.youGetBack ? [member.youGetBack] : (member.flow?.nodes ?? []).filter((n) => n.kind === 'end').map((n) => n.text.replace(/^Gives back\s+/i, ''));
|
|
81
|
+
return [...new Set(list.filter(Boolean))];
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* What goes in and out of one action or rule: "You give it" (each input, with its kind when the code says it), "You
|
|
86
|
+
* get back", "It can stop with" (every Stop in its flow, with its line), and "Part of <Thing>" back to its card.
|
|
87
|
+
*/
|
|
88
|
+
export function ioBoxes(model, member) {
|
|
89
|
+
const owner = member.owner ? model.things.find((t) => t.id === member.owner) : null;
|
|
90
|
+
const kinds = member.inputKinds ?? {};
|
|
91
|
+
const stops = (member.flow?.nodes ?? []).filter((n) => n.kind === 'stop').map((n) => ({ text: n.text.replace(/^Stop:\s*/, ''), line: n.line ?? null }));
|
|
92
|
+
return {
|
|
93
|
+
kind: 'io', id: member.id, label: `${member.label}${member.yesNo ? '?' : ''}`, doc: member.doc ?? null, symbol: memberSymbolId(member),
|
|
94
|
+
give: { heading: READING_LABELS.give, items: member.youGive.map((label) => ({ label, kind: kinds[label] ?? null })), empty: READING_LABELS.givesNothing },
|
|
95
|
+
// What it gives back: what the code declares, else the Flow's end steps (the same facts Flow and Details show).
|
|
96
|
+
getBack: { heading: READING_LABELS.getBack, text: member.youGetBack ?? null, items: givesBackOf(member), empty: READING_LABELS.getsNothing },
|
|
97
|
+
stops: { heading: READING_LABELS.stops, items: stops },
|
|
98
|
+
partOf: owner ? { id: owner.id, text: READING_LABELS.partOf(owner.label) } : null,
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** What a Flow edge says ("Yes", "For each", a case's own words), or null for a plain next step. */
|
|
103
|
+
export function flowEdgeLabel(edge) {
|
|
104
|
+
if (!edge.label || edge.label === 'next') return null;
|
|
105
|
+
if (edge.label === 'case') return edge.text ?? READING_LABELS.edges.case;
|
|
106
|
+
return READING_LABELS.edges[edge.label] ?? edge.text ?? edge.label;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const count = (n, one, many) => ({ count: n, text: n === 1 ? one : many });
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Details, sorted for drawing: the counts at a glance, then You give it / You get back, When it stops, What it
|
|
113
|
+
* changes, Its answers, Exact limits, Every step, It relies on, Used by, and Questions the code cannot answer.
|
|
114
|
+
* `view` is readLevel(model, 'details', memberId). A section is present only when the compiler gave it (null when
|
|
115
|
+
* not), so nothing is invented. What it gives back comes from the same facts as the Big Picture: givesBack, else
|
|
116
|
+
* youGetBack, else the Flow's end steps.
|
|
117
|
+
*/
|
|
118
|
+
export function detailsLayout(view) {
|
|
119
|
+
const L = READING_LABELS.details;
|
|
120
|
+
const inputs = view.inputs ?? (view.youGive ?? []).map((label) => ({ label, lines: [] }));
|
|
121
|
+
const givesBack = view.givesBack?.length ? view.givesBack.map((g) => ({ text: g.text, line: g.line ?? null, when: g.when ?? null }))
|
|
122
|
+
: view.youGetBack ? [{ text: view.youGetBack, line: null, when: null }]
|
|
123
|
+
: (view.flow?.nodes ?? []).filter((n) => n.kind === 'end').map((n) => ({ text: n.text, line: n.line ?? null, when: null }));
|
|
124
|
+
const steps = view.steps ?? [];
|
|
125
|
+
const stops = view.stops ?? null;
|
|
126
|
+
const changes = view.changes ?? null;
|
|
127
|
+
const reliesOn = view.reliesOn ?? (view.uses ?? []).map((u) => ({ id: u.id, label: u.label, symbol: 'rule', says: '' }));
|
|
128
|
+
const stats = [
|
|
129
|
+
...(inputs.length ? [{ id: 'inputs', ...count(inputs.length, 'thing you give it', 'things you give it') }] : []),
|
|
130
|
+
{ id: 'steps', ...count(steps.length, 'step', 'steps') },
|
|
131
|
+
...(stops ? [{ id: 'stops', ...count(stops.length, 'way to stop', 'ways to stop') }] : []),
|
|
132
|
+
...(changes ? [{ id: 'changes', ...count(changes.length, 'change', 'changes') }] : []),
|
|
133
|
+
...(reliesOn.length ? [{ id: 'reliesOn', count: reliesOn.length, text: 'relies on' }] : []),
|
|
134
|
+
...((view.notExplained ?? 0) > 0 ? [{ id: 'notExplained', count: view.notExplained, text: 'not explained yet' }] : []),
|
|
135
|
+
];
|
|
136
|
+
return {
|
|
137
|
+
id: view.id ?? null, title: view.title ?? null, heading: L.atAGlance, stats,
|
|
138
|
+
give: inputs.length || givesBack.length ? { heading: READING_LABELS.give, items: inputs.map((i) => ({ label: i.label, lines: i.lines ?? [] })), empty: L.noInputs } : null,
|
|
139
|
+
getBack: inputs.length || givesBack.length ? { heading: READING_LABELS.getBack, items: givesBack, empty: L.nothingBack } : null,
|
|
140
|
+
stops: stops ? { heading: L.stops, items: stops.map((s) => ({ error: s.error, message: s.message ?? null, line: s.line ?? null, when: s.when ?? [] })), empty: L.noStops } : null,
|
|
141
|
+
changes: changes?.length ? { heading: L.changes, items: changes.map((c) => ({ text: c.text, does: c.does ?? null, line: c.line ?? null, when: c.when ?? [] })) } : null,
|
|
142
|
+
answers: view.answers?.length ? { heading: L.answers, items: view.answers.map((a) => ({ answer: a.answer, when: a.when === 'otherwise' ? L.otherwise : L.when(a.when) })) } : null,
|
|
143
|
+
limits: view.limits ? { heading: L.limits(view.limits.fact), note: L.limitsHow, items: view.limits.ranges.map((r) => ({ range: r.from == null ? `${r.to} or less` : r.to == null ? `${r.from} or more` : r.from === r.to ? `${r.from}` : `${r.from} to ${r.to}`, answer: r.answer })) } : null,
|
|
144
|
+
steps: steps.length ? { heading: L.steps, items: steps.map((s) => ({ id: s.id, text: s.text, does: s.does ?? null, mark: s.mark ?? null, depth: s.depth ?? 0, line: s.line ?? null, notExplained: !!s.notExplained, via: s.via ?? null })) } : null,
|
|
145
|
+
reliesOn: reliesOn.length ? { heading: L.reliesOn, items: reliesOn.map((r) => ({ id: r.id, label: r.label, symbol: r.symbol ?? 'rule', says: r.says ?? '' })) } : null,
|
|
146
|
+
usedBy: view.usedBy?.length ? { heading: L.usedBy, items: view.usedBy.map((u) => ({ id: u.id, label: u.label })) } : null,
|
|
147
|
+
openQuestions: view.openQuestions?.length ? { heading: L.openQuestions, items: [...view.openQuestions] } : null,
|
|
148
|
+
source: view.source ?? null,
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
const sectionsFor = (model, things) => {
|
|
153
|
+
const cards = things.filter((t) => t.kind === 'thing').map((t) => thingCard(model, t));
|
|
154
|
+
const data = [...things.filter((t) => t.kind === 'record'), ...things.filter((t) => t.kind === 'choices')].map(dataCard);
|
|
155
|
+
return [
|
|
156
|
+
...(cards.length ? [{ id: 'things', heading: READING_LABELS.things, cards }] : []),
|
|
157
|
+
...(data.length ? [{ id: 'data', heading: READING_LABELS.data, cards: data }] : []),
|
|
158
|
+
];
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* The Big Picture, which zooms with what is in focus (the tabs stay Big Picture / Flow / Details / Text / Original):
|
|
163
|
+
* no focus: every Thing, then every record, then every set of choices
|
|
164
|
+
* a file's path: the same, for that file
|
|
165
|
+
* a Thing's id: its card, then the Data in its file
|
|
166
|
+
* an action or rule: its "You give it / You get back / It can stop with" boxes, with "Part of <Thing>"
|
|
167
|
+
* Resources are infrastructure and have their own view. `focus.kind` says which one it was ('unknown' when the id is
|
|
168
|
+
* not in the model: nothing is drawn rather than something else).
|
|
169
|
+
*/
|
|
170
|
+
export function readingLayout(model, { focus = null } = {}) {
|
|
171
|
+
const base = { schema: READING_LAYOUT_SCHEMA };
|
|
172
|
+
if (focus == null) return { ...base, focus: { kind: 'app', id: null }, sections: sectionsFor(model, model.things) };
|
|
173
|
+
const member = model.members.find((m) => m.id === focus);
|
|
174
|
+
if (member) return { ...base, focus: { kind: 'member', id: focus }, sections: [{ id: 'io', heading: READING_LABELS.io, cards: [ioBoxes(model, member)] }] };
|
|
175
|
+
const thing = model.things.find((t) => t.id === focus);
|
|
176
|
+
if (thing) {
|
|
177
|
+
const rest = model.things.filter((t) => t !== thing && t.file === thing.file && t.kind !== 'thing');
|
|
178
|
+
return { ...base, focus: { kind: 'thing', id: focus }, sections: sectionsFor(model, [thing, ...rest]) };
|
|
179
|
+
}
|
|
180
|
+
if (model.files.some((f) => f.path === focus)) return { ...base, focus: { kind: 'file', id: focus }, sections: sectionsFor(model, model.things.filter((t) => t.file === focus)) };
|
|
181
|
+
return { ...base, focus: { kind: 'unknown', id: focus }, sections: [] };
|
|
182
|
+
}
|
package/src/write-across.mjs
CHANGED
|
@@ -10,8 +10,10 @@
|
|
|
10
10
|
//
|
|
11
11
|
// Which rules count as the same: decisions whose rule has the same condition, comparing names as words
|
|
12
12
|
// (orderTotal, OrderTotal, and order_total are the same name), numbers by value, and comparisons and text exactly.
|
|
13
|
-
//
|
|
14
|
-
//
|
|
13
|
+
// The same condition in a decision whose name shares no word with the chosen one (couponAllowed next to
|
|
14
|
+
// shippingFee, both "subtotal >= 50") is a different rule that happens to use the same limit, so it is listed, not
|
|
15
|
+
// changed. A rule with the same shape where only the names or only the values differ is listed as "not included,
|
|
16
|
+
// check by hand", never changed. Test files are never edited; those that mention the rule's old values are listed.
|
|
15
17
|
//
|
|
16
18
|
// Deterministic and browser-safe: it takes file contents and returns new contents. It never runs anything.
|
|
17
19
|
import { readCode, sayCondition } from './read.mjs';
|
|
@@ -26,6 +28,10 @@ const editable = (t) => t.t === 'num' || t.t === 'str' || (t.t === 'op' && ['<',
|
|
|
26
28
|
const keyOf = (t) => (t.t === 'id' ? `id:${words(t.v)}` : t.t === 'str' ? `str:${JSON.stringify(t.v)}` : `${t.t}:${t.v}`);
|
|
27
29
|
const shapeOf = (t) => (t.t === 'id' ? 'id' : editable(t) ? (t.t === 'op' ? 'cmp' : t.t) : `${t.t}:${t.v}`);
|
|
28
30
|
const join = (toks) => toks.map((t) => (t.t === 'str' ? JSON.stringify(t.v) : String(t.v))).join(' ');
|
|
31
|
+
// Words that say nothing about which rule a function is (get_shipping_fee and ShippingFee share "shipping").
|
|
32
|
+
const FILLER = new Set(['get', 'is', 'has', 'can', 'should', 'calc', 'calculate', 'compute', 'check', 'find', 'the', 'a', 'an', 'of', 'for', 'to', 'by']);
|
|
33
|
+
const nameWords = (name) => new Set(words(name).split(' ').filter((w) => w && !FILLER.has(w)));
|
|
34
|
+
const sharesWord = (a, b) => { const A = nameWords(a); return [...nameWords(b)].some((w) => A.has(w)); };
|
|
29
35
|
const toks = (when) => { try { return tokenize(when); } catch { return null; } };
|
|
30
36
|
|
|
31
37
|
/** Every decision rule in the parts' source files (test files left out), read once. */
|
|
@@ -75,7 +81,11 @@ export function findSameRule(files, { decision, rule }) {
|
|
|
75
81
|
if (x === anchor) continue;
|
|
76
82
|
const k = x.toks.map(keyOf).join(' ');
|
|
77
83
|
const said = sayCondition(x.when);
|
|
78
|
-
if (k === key) {
|
|
84
|
+
if (k === key) {
|
|
85
|
+
if (sharesWord(x.decision, anchor.decision)) same.push({ ...x, why: `the same condition: ${said} (names compared as words)` });
|
|
86
|
+
else notIncluded.push({ ...x, why: `the same condition (${said}) in ${x.decision}, whose name shares no word with ${anchor.decision}; it may be a different rule that happens to use the same limit` });
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
79
89
|
if (x.toks.map(shapeOf).join(' ') !== shape) continue;
|
|
80
90
|
const xNames = x.toks.filter((t) => t.t === 'id').map(keyOf).join(' ');
|
|
81
91
|
const xValues = x.toks.filter((t) => t.t !== 'id').map(keyOf).join(' ');
|