trebol 0.1.1 → 0.1.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.
Files changed (84) hide show
  1. package/README.md +10 -1
  2. package/dist/cli.js +13747 -11409
  3. package/dist/fonts/OFL.txt +93 -0
  4. package/dist/fonts/inter-cyrillic-ext.woff2 +0 -0
  5. package/dist/fonts/inter-cyrillic.woff2 +0 -0
  6. package/dist/fonts/inter-greek-ext.woff2 +0 -0
  7. package/dist/fonts/inter-greek.woff2 +0 -0
  8. package/dist/fonts/inter-latin-ext.woff2 +0 -0
  9. package/dist/fonts/inter-latin.woff2 +0 -0
  10. package/dist/fonts/inter-vietnamese.woff2 +0 -0
  11. package/dist/lib/core/boot.js +2 -1
  12. package/dist/lib/core/func.d.ts +4 -0
  13. package/dist/lib/core/natives/control.js +1 -1
  14. package/dist/lib/core/natives/series.js +11 -1
  15. package/dist/lib/core/natives/text.js +3 -0
  16. package/dist/lib/host/browser.js +951 -58
  17. package/dist/lib/host/devlog.d.ts +11 -0
  18. package/dist/lib/host/devlog.js +13 -0
  19. package/dist/lib/host/devtools.d.ts +23 -0
  20. package/dist/lib/host/devtools.js +626 -0
  21. package/dist/lib/host/fonts/OFL.txt +93 -0
  22. package/dist/lib/host/fonts/inter-cyrillic-ext.woff2 +0 -0
  23. package/dist/lib/host/fonts/inter-cyrillic.woff2 +0 -0
  24. package/dist/lib/host/fonts/inter-greek-ext.woff2 +0 -0
  25. package/dist/lib/host/fonts/inter-greek.woff2 +0 -0
  26. package/dist/lib/host/fonts/inter-latin-ext.woff2 +0 -0
  27. package/dist/lib/host/fonts/inter-latin.woff2 +0 -0
  28. package/dist/lib/host/fonts/inter-vietnamese.woff2 +0 -0
  29. package/dist/lib/host/fonts.d.ts +11 -0
  30. package/dist/lib/host/fonts.js +29 -0
  31. package/dist/lib/host/index.d.ts +2 -0
  32. package/dist/lib/host/index.js +3 -1
  33. package/dist/lib/host/js.d.ts +5 -0
  34. package/dist/lib/host/js.js +130 -0
  35. package/dist/lib/host/markdown.d.ts +2 -0
  36. package/dist/lib/host/markdown.js +10 -0
  37. package/dist/lib/host/modules.d.ts +4 -0
  38. package/dist/lib/host/modules.js +39 -1
  39. package/dist/lib/host/net.js +9 -1
  40. package/dist/lib/host/orbit.d.ts +121 -0
  41. package/dist/lib/host/orbit.js +374 -0
  42. package/dist/lib/host/rows.d.ts +14 -0
  43. package/dist/lib/host/rows.js +29 -0
  44. package/dist/lib/host/scheduler.d.ts +6 -0
  45. package/dist/lib/host/scheduler.js +60 -36
  46. package/dist/lib/host/theme.js +207 -2
  47. package/dist/lib/ui/i18n.trb +6 -0
  48. package/dist/lib/ui/test.trb +565 -0
  49. package/dist/lib/ui/ui.trb +523 -82
  50. package/dist/trebol.js +1346 -124
  51. package/editors/vscode/README.md +15 -0
  52. package/editors/vscode/language-configuration.json +28 -0
  53. package/editors/vscode/package.json +42 -0
  54. package/editors/vscode/syntaxes/trebol.tmLanguage.json +237 -0
  55. package/package.json +7 -3
  56. package/src/core/boot.ts +1 -1
  57. package/src/core/func.ts +4 -0
  58. package/src/core/natives/control.ts +1 -1
  59. package/src/core/natives/series.ts +9 -1
  60. package/src/core/natives/text.ts +2 -0
  61. package/src/host/browser.ts +962 -51
  62. package/src/host/devlog.ts +16 -0
  63. package/src/host/devtools.ts +641 -0
  64. package/src/host/fonts/OFL.txt +93 -0
  65. package/src/host/fonts/inter-cyrillic-ext.woff2 +0 -0
  66. package/src/host/fonts/inter-cyrillic.woff2 +0 -0
  67. package/src/host/fonts/inter-greek-ext.woff2 +0 -0
  68. package/src/host/fonts/inter-greek.woff2 +0 -0
  69. package/src/host/fonts/inter-latin-ext.woff2 +0 -0
  70. package/src/host/fonts/inter-latin.woff2 +0 -0
  71. package/src/host/fonts/inter-vietnamese.woff2 +0 -0
  72. package/src/host/fonts.ts +39 -0
  73. package/src/host/index.ts +3 -1
  74. package/src/host/js.ts +127 -0
  75. package/src/host/markdown.ts +9 -0
  76. package/src/host/modules.ts +33 -1
  77. package/src/host/net.ts +9 -1
  78. package/src/host/orbit.ts +432 -0
  79. package/src/host/rows.ts +30 -0
  80. package/src/host/scheduler.ts +62 -36
  81. package/src/host/theme.ts +207 -2
  82. package/src/ui/i18n.trb +6 -0
  83. package/src/ui/test.trb +565 -0
  84. package/src/ui/ui.trb +523 -82
@@ -0,0 +1,374 @@
1
+ // An orbit: one node of a graph in the centre and its neighbours on rings around it.
2
+ //
3
+ // orbit "Organization" [
4
+ // focus pleno
5
+ // nodes [pleno "Pleno municipal" [primary count 9] marta "Marta Ibáñez" [accent]]
6
+ // edges [pleno marta "Members" marta pleno "Holder" [dashed]]
7
+ // trail [ayuntamiento organos]
8
+ // ]
9
+ //
10
+ // The person moves through the graph one node at a time; the app gives the
11
+ // node in the centre and what is around it each time. This module reads the
12
+ // block, places the nodes and builds each bubble as plain elements in unit
13
+ // space (radius 1), scaled by its radius; the browser host animates them.
14
+ import { K, I64, spell, text } from "../core/index.js";
15
+ import { svgEl, drawColor } from "./draw.js";
16
+ const HINT_WORDS = ['many', 'dashed', 'count', 'steps', 'at'];
17
+ const EDGE_LOOKS = ['solid', 'dashed', 'faint'];
18
+ /** What tells nodes apart: an id is a word, an integer, a string or an issue. */
19
+ export function idKey(c) {
20
+ switch (c.t) {
21
+ case K.Word:
22
+ case K.LitWord: return `w:${spell(c.sym)}`;
23
+ case K.Integer: return `i:${I64.toString(c.v)}`;
24
+ case K.String: return `s:${text(c)}`;
25
+ case K.Issue: return `#:${spell(c.sym)}`;
26
+ default: return null;
27
+ }
28
+ }
29
+ const isText = (c) => c !== undefined && (c.t === K.String || c.t === K.Map);
30
+ const wordOf = (c) => (c !== undefined && c.t === K.Word ? spell(c.sym) : null);
31
+ const intOf = (c) => (c !== undefined && c.t === K.Integer ? I64.toNumber(c.v) : null);
32
+ const cellsOf = (b) => b.s.data.slice(b.i);
33
+ class OrbitError extends Error {
34
+ }
35
+ /** The orbit a block describes, and what was wrong with it; what is wrong is left out. */
36
+ export function readOrbit(data) {
37
+ const spec = { focus: null, nodes: new Map(), edges: [], trail: [] };
38
+ if (data.t !== K.Block)
39
+ return { spec, error: data.t === K.None ? null : 'orbit: an orbit takes a block' };
40
+ try {
41
+ const d = cellsOf(data);
42
+ for (let i = 0; i < d.length; i++) {
43
+ const w = wordOf(d[i]);
44
+ const arg = d[++i];
45
+ if (w === 'focus') {
46
+ const k = arg === undefined ? null : idKey(arg);
47
+ if (k === null)
48
+ throw new OrbitError('focus takes the id of a node');
49
+ spec.focus = k;
50
+ }
51
+ else if (w === 'nodes' && arg?.t === K.Block)
52
+ readNodes(cellsOf(arg), spec);
53
+ else if (w === 'edges' && arg?.t === K.Block)
54
+ readEdges(cellsOf(arg), spec);
55
+ else if (w === 'trail' && arg?.t === K.Block) {
56
+ for (const c of cellsOf(arg)) {
57
+ const k = idKey(c);
58
+ if (k === null)
59
+ throw new OrbitError('trail takes the ids of nodes');
60
+ spec.trail.push(k);
61
+ }
62
+ }
63
+ else
64
+ throw new OrbitError('an orbit takes focus id, nodes [...], edges [...] and trail [...]');
65
+ }
66
+ return { spec, error: null };
67
+ }
68
+ catch (e) {
69
+ if (e instanceof OrbitError)
70
+ return { spec, error: `orbit: ${e.message}` };
71
+ throw e;
72
+ }
73
+ }
74
+ function readNodes(d, spec) {
75
+ for (let i = 0; i < d.length; i++) {
76
+ const id = d[i];
77
+ const key = idKey(id);
78
+ if (key === null || !isText(d[i + 1]))
79
+ throw new OrbitError('nodes takes an id and a title for each, then a block of hints if it has any');
80
+ const node = { key, id, title: d[++i], color: 'var(--tb-primary)', count: null, many: false, dashed: false, steps: 0, at: 0 };
81
+ if (d[i + 1]?.t === K.Block)
82
+ readHints(cellsOf(d[++i]), node);
83
+ spec.nodes.set(key, node);
84
+ }
85
+ }
86
+ function readHints(h, node) {
87
+ for (let i = 0; i < h.length; i++) {
88
+ const w = wordOf(h[i]);
89
+ if (w === 'many')
90
+ node.many = true;
91
+ else if (w === 'dashed')
92
+ node.dashed = true;
93
+ else if (w === 'count') {
94
+ const n = intOf(h[++i]);
95
+ if (n === null)
96
+ throw new OrbitError('count takes an integer');
97
+ node.count = n;
98
+ }
99
+ else if (w === 'steps') {
100
+ const n = intOf(h[++i]);
101
+ const at = wordOf(h[i + 1]) === 'at' ? intOf(h[i += 2]) : 0;
102
+ if (n === null || n < 1 || at === null)
103
+ throw new OrbitError('steps takes how many, then at and the current one: steps 5 at 2');
104
+ node.steps = n;
105
+ node.at = Math.max(0, Math.min(n, at));
106
+ }
107
+ else {
108
+ const c = w === null || HINT_WORDS.includes(w) ? undefined : drawColor(w);
109
+ if (c === undefined || c === null)
110
+ throw new OrbitError(`a node's hints are a role or color, many, dashed, count n and steps n at k, not ${w ?? 'that'}`);
111
+ node.color = c;
112
+ }
113
+ }
114
+ }
115
+ function readEdges(d, spec) {
116
+ for (let i = 0; i < d.length; i++) {
117
+ const from = idKey(d[i]);
118
+ const to = d[i + 1] === undefined ? null : idKey(d[++i]);
119
+ if (from === null || to === null)
120
+ throw new OrbitError('edges takes two ids for each, then a label and a block with solid, dashed or faint if it has them');
121
+ const label = isText(d[i + 1]) ? d[++i] : null;
122
+ let look = 'solid';
123
+ if (d[i + 1]?.t === K.Block) {
124
+ const b = cellsOf(d[++i]);
125
+ const w = wordOf(b[0]);
126
+ if (b.length !== 1 || w === null || !EDGE_LOOKS.includes(w))
127
+ throw new OrbitError('an edge looks solid, dashed or faint');
128
+ look = w;
129
+ }
130
+ spec.edges.push({ from, to, label, look });
131
+ }
132
+ }
133
+ export const FOCUS_R = 92;
134
+ /** Neighbours on show at once; past that, a "+N more" bubble pages through them. */
135
+ export const PAGE = 30;
136
+ /** Past this many neighbours, edge labels show only on hover. */
137
+ export const MANY = 18;
138
+ const TRAIL_SHOWN = 4;
139
+ /** The key of the "+N more" bubble: no id gives it. */
140
+ export const MORE = '\u0000more';
141
+ /** The neighbours of the focus: every node given that is neither it nor in the trail, in the order given. */
142
+ export function neighbours(spec) {
143
+ const back = new Set(spec.trail);
144
+ return [...spec.nodes.keys()].filter(k => k !== spec.focus && !back.has(k));
145
+ }
146
+ /** Places the focus, its neighbours on rings (the given page of them) and the last few of the trail. */
147
+ export function layoutOrbit(spec, page = 0) {
148
+ const placed = [];
149
+ if (spec.focus === null)
150
+ return { placed, box: { x0: -FOCUS_R, y0: -FOCUS_R, x1: FOCUS_R, y1: FOCUS_R }, left: 0, pages: 1 };
151
+ placed.push({ key: spec.focus, role: 'focus', x: 0, y: 0, r: FOCUS_R, o: 1 });
152
+ let near = neighbours(spec);
153
+ const pages = Math.max(1, Math.ceil(near.length / PAGE));
154
+ let left = 0;
155
+ if (near.length > PAGE) {
156
+ const start = (page % pages) * PAGE;
157
+ left = near.length - Math.min(near.length, start + PAGE);
158
+ near = near.slice(start, start + PAGE);
159
+ }
160
+ const count = near.length + (pages > 1 ? 1 : 0);
161
+ // fewer neighbours, bigger bubbles
162
+ const r = Math.max(24, Math.min(62, 66 - count * 1.6));
163
+ const slots = [];
164
+ let R = FOCUS_R + r + 120;
165
+ let outer = FOCUS_R;
166
+ for (let i = 0, ring = 0; i < count; ring++) {
167
+ const fits = Math.max(6, Math.floor((2 * Math.PI * R) / (2 * r + 30)));
168
+ const here = Math.min(fits, count - i);
169
+ for (let k = 0; k < here; k++) {
170
+ // every other ring turns half a step, so bubbles do not line up behind each other
171
+ const a = -Math.PI / 2 + (2 * Math.PI * k) / here + (ring % 2 === 1 ? Math.PI / here : 0);
172
+ slots.push({ x: Math.cos(a) * R, y: Math.sin(a) * R });
173
+ }
174
+ i += here;
175
+ outer = R + r;
176
+ R += 2 * r + 46;
177
+ }
178
+ near.forEach((key, j) => placed.push({ key, role: 'near', ...slots[j], r, o: 1 }));
179
+ if (pages > 1)
180
+ placed.push({ key: MORE, role: 'more', ...slots[near.length], r, o: 1 });
181
+ // the trail goes up and to the left, the most recent nearest, each smaller and fainter
182
+ const back = spec.trail.filter(k => k !== spec.focus && spec.nodes.has(k)).slice(-TRAIL_SHOWN).reverse();
183
+ const dir = -Math.PI * 0.9;
184
+ const base = outer + 150;
185
+ back.forEach((key, j) => {
186
+ const d = base + j * 120;
187
+ placed.push({ key, role: 'trail', x: Math.cos(dir) * d, y: Math.sin(dir) * d, r: 46 - j * 7, o: 0.8 - j * 0.17 });
188
+ });
189
+ // the camera fits every bubble, with room for the titles beside the small ones
190
+ const box = { x0: Infinity, y0: Infinity, x1: -Infinity, y1: -Infinity };
191
+ for (const p of placed) {
192
+ const w = p.r * 1.5 + 40;
193
+ const h = p.r * 1.5 + 24;
194
+ box.x0 = Math.min(box.x0, p.x - w);
195
+ box.x1 = Math.max(box.x1, p.x + w);
196
+ box.y0 = Math.min(box.y0, p.y - h);
197
+ box.y1 = Math.max(box.y1, p.y + h);
198
+ }
199
+ return { placed, box, left, pages };
200
+ }
201
+ /** The edges to draw between bubbles on show: those given between the focus and its neighbours, and the trail's dotted line. */
202
+ export function shownEdges(spec, placed) {
203
+ const role = new Map(placed.map(p => [p.key, p.role]));
204
+ const out = [];
205
+ const seen = new Set();
206
+ for (const e of spec.edges) {
207
+ const a = role.get(e.from);
208
+ const b = role.get(e.to);
209
+ if ((a !== 'focus' && a !== 'near') || (b !== 'focus' && b !== 'near') || e.from === e.to)
210
+ continue;
211
+ // one line per pair: the first edge given between them says how it looks
212
+ const pair = [e.from, e.to].sort().join('\u0001');
213
+ if (seen.has(pair))
214
+ continue;
215
+ seen.add(pair);
216
+ // between two neighbours the line bends outwards, so it does not cross the focus
217
+ out.push({ ...e, curve: a === 'near' && b === 'near' ? 0.35 : 0, trail: false });
218
+ }
219
+ if (role.has(MORE))
220
+ out.push({ from: spec.focus, to: MORE, label: null, look: 'faint', curve: 0, trail: false });
221
+ let prev = spec.focus;
222
+ for (const p of placed) {
223
+ if (p.role !== 'trail')
224
+ continue;
225
+ out.push({ from: prev, to: p.key, label: null, look: 'faint', curve: 0, trail: true });
226
+ prev = p.key;
227
+ }
228
+ return out;
229
+ }
230
+ /** The label of the edge between the focus and a neighbour, either way round; null if none. */
231
+ export function edgeLabel(spec, key) {
232
+ for (const e of spec.edges) {
233
+ if (e.label !== null && ((e.from === spec.focus && e.to === key) || (e.to === spec.focus && e.from === key)))
234
+ return e.label;
235
+ }
236
+ return null;
237
+ }
238
+ // --- a bubble ---------------------------------------------------------------------------------
239
+ /** A title in lines of at most `max` characters, three lines at most. */
240
+ export function wrapTitle(title, max) {
241
+ const lines = [];
242
+ let line = '';
243
+ for (const w of title.split(/\s+/).filter(w => w !== '')) {
244
+ if (line !== '' && [...`${line} ${w}`].length > max) {
245
+ lines.push(line);
246
+ line = w;
247
+ }
248
+ else
249
+ line = line === '' ? w : `${line} ${w}`;
250
+ }
251
+ if (line !== '')
252
+ lines.push(line);
253
+ if (lines.length > 3) {
254
+ lines.length = 3;
255
+ lines[2] = `${[...lines[2]].slice(0, Math.max(1, max - 1)).join('')}…`;
256
+ }
257
+ return lines;
258
+ }
259
+ /** An arc of a circle of radius r from angle a0 to a1, as path data. */
260
+ export function arcPath(r, a0, a1) {
261
+ const p = (a) => `${round(Math.cos(a) * r)},${round(Math.sin(a) * r)}`;
262
+ return `M${p(a0)} A${r},${r} 0 ${a1 - a0 > Math.PI ? 1 : 0} 1 ${p(a1)}`;
263
+ }
264
+ const round = (n) => String(Math.round(n * 1000) / 1000);
265
+ /** What a bubble draws, in unit space: halo, body, its rings, moons, title and count. */
266
+ export function bubble(b) {
267
+ const n = b.node;
268
+ const c = b.color;
269
+ const focus = b.role === 'focus';
270
+ const out = [
271
+ svgEl('circle', { class: 'tb-orbit-halo', r: 1.45, fill: `url(#${b.halo})` }),
272
+ svgEl('circle', {
273
+ class: 'tb-orbit-body', r: 1, fill: `url(#${b.body})`, stroke: c, 'stroke-width': focus ? 2 : 1.4,
274
+ ...(n?.dashed === true ? { 'stroke-dasharray': '4 4' } : {}),
275
+ ...(b.role === 'trail' ? { 'stroke-opacity': 0.6 } : {}),
276
+ }),
277
+ ];
278
+ // a node that stands for many wears a second ring
279
+ if (n?.many === true)
280
+ out.push(svgEl('circle', { class: 'tb-orbit-line', r: 1.09, stroke: c, 'stroke-opacity': 0.55 }));
281
+ // steps: those done filled, the current one thicker and pulsing, those to come faint
282
+ if (n !== null && n.steps > 0) {
283
+ const gap = n.steps > 1 ? 0.09 : 0;
284
+ for (let j = 0; j < n.steps; j++) {
285
+ const a0 = -Math.PI / 2 + (2 * Math.PI * j) / n.steps + gap / 2;
286
+ const a1 = a0 + (2 * Math.PI) / n.steps - gap;
287
+ const now = j + 1 === n.at;
288
+ out.push(svgEl('path', {
289
+ class: now ? 'tb-orbit-step tb-orbit-now' : 'tb-orbit-step', d: arcPath(1.16, a0, n.steps > 1 ? a1 : a0 + 2 * Math.PI - 0.001), stroke: c,
290
+ 'stroke-opacity': j + 1 < n.at ? 0.9 : now ? 1 : 0.18, 'stroke-width': now ? 4 : 3,
291
+ }));
292
+ }
293
+ }
294
+ // moons: one per thing it holds, up to 14, turning slowly
295
+ const count = n?.count ?? 0;
296
+ if (count > 0 && !focus && b.role !== 'more') {
297
+ const k = Math.min(count, 14);
298
+ const moons = [];
299
+ for (let j = 0; j < k; j++) {
300
+ const a = (2 * Math.PI * j) / k;
301
+ moons.push(svgEl('circle', { cx: Math.cos(a) * 1.24, cy: Math.sin(a) * 1.24, r: 0.05, fill: c, 'fill-opacity': 0.8 }));
302
+ }
303
+ out.push(svgEl('g', { class: 'tb-orbit-spin tb-orbit-fast' }, moons));
304
+ }
305
+ // titles inside big bubbles, under small ones, never smaller on screen than the page's small text
306
+ const px = 1 / (b.r * b.scale);
307
+ const inside = focus || (b.r >= 44 && 12 * px <= 0.26);
308
+ const fs = focus ? Math.max(0.2, 14 * px) : inside ? Math.max(0.24, 12 * px) : 12.5 * px;
309
+ const lines = wrapTitle(b.title, focus ? Math.max(8, Math.floor(3.2 / fs)) : inside ? 13 : 18);
310
+ const top = inside ? -((lines.length - 1) * fs * 1.15) / 2 : b.above ? -1.34 - fs * 0.6 - (lines.length - 1) * fs * 1.15 : 1.34 + fs * 0.6;
311
+ out.push(svgEl('text', { class: 'tb-orbit-title', 'font-size': fs, 'font-weight': focus ? 600 : 500 }, lines.map((l, j) => svgEl('tspan', { x: 0, y: top + j * fs * 1.15 }, [], l))));
312
+ if (count > 0 && inside) {
313
+ const y = focus ? top - fs * 1.2 : top + (lines.length - 1) * fs * 1.15 + 0.3;
314
+ out.push(svgEl('text', { class: 'tb-orbit-count', 'font-size': focus ? Math.max(0.12, 10 * px) : Math.max(0.16, 10 * px), y }, [], String(count)));
315
+ }
316
+ // the focus has two rings turning slowly, one each way
317
+ if (focus) {
318
+ out.push(svgEl('g', { class: 'tb-orbit-spin' }, [svgEl('circle', { class: 'tb-orbit-line', r: 1.24, stroke: c, 'stroke-opacity': 0.35, 'stroke-dasharray': '2 10' })]));
319
+ out.push(svgEl('g', { class: 'tb-orbit-spin tb-orbit-rev' }, [svgEl('circle', { class: 'tb-orbit-line', r: 1.42, stroke: c, 'stroke-opacity': 0.18, 'stroke-dasharray': '40 14 4 14' })]));
320
+ }
321
+ return out;
322
+ }
323
+ /** The two gradients of a color: a body lighter in the middle and a soft halo. */
324
+ export function gradients(color, body, halo) {
325
+ const stop = (offset, opacity) => svgEl('stop', { offset, 'stop-color': color, 'stop-opacity': opacity });
326
+ return [
327
+ svgEl('radialGradient', { id: body, cx: '50%', cy: '40%', r: '60%' }, [stop('0%', 0.05), stop('75%', 0.16), stop('100%', 0.38)]),
328
+ svgEl('radialGradient', { id: halo }, [stop('55%', 0), stop('72%', 0.22), stop('100%', 0)]),
329
+ ];
330
+ }
331
+ /** Where an edge's line goes, from bubble a to bubble b, cut at their borders; and the middle and angle for its label. */
332
+ export function edgePath(a, b, curve) {
333
+ const dx = b.x - a.x;
334
+ const dy = b.y - a.y;
335
+ const len = Math.hypot(dx, dy) || 1;
336
+ const ux = dx / len;
337
+ const uy = dy / len;
338
+ const x1 = a.x + ux * a.r * 1.04;
339
+ const y1 = a.y + uy * a.r * 1.04;
340
+ const x2 = b.x - ux * b.r * 1.04;
341
+ const y2 = b.y - uy * b.r * 1.04;
342
+ let mx = (x1 + x2) / 2;
343
+ let my = (y1 + y2) / 2;
344
+ let d = `M${round(x1)},${round(y1)} L${round(x2)},${round(y2)}`;
345
+ if (curve !== 0) {
346
+ const l = Math.hypot(x2 - x1, y2 - y1) || 1;
347
+ let nx = -(y2 - y1) / l;
348
+ let ny = (x2 - x1) / l;
349
+ // bend away from the centre
350
+ if (nx * mx + ny * my < 0) {
351
+ nx = -nx;
352
+ ny = -ny;
353
+ }
354
+ const cx = mx + nx * l * curve;
355
+ const cy = my + ny * l * curve;
356
+ d = `M${round(x1)},${round(y1)} Q${round(cx)},${round(cy)} ${round(x2)},${round(y2)}`;
357
+ mx = (x1 + 2 * cx + x2) / 4;
358
+ my = (y1 + 2 * cy + y2) / 4;
359
+ }
360
+ // labels never read upside down
361
+ let angle = (Math.atan2(y2 - y1, x2 - x1) * 180) / Math.PI;
362
+ if (angle > 90)
363
+ angle -= 180;
364
+ if (angle < -90)
365
+ angle += 180;
366
+ return { d, x: mx, y: my, angle };
367
+ }
368
+ /** The ease of a move: slow at both ends. */
369
+ export const ease = (k) => (k < 0.5 ? 4 * k * k * k : 1 - Math.pow(-2 * k + 2, 3) / 2);
370
+ /** Where the camera goes to show the whole box in a view of w by h: centred on it, zoomed to fit, within limits. */
371
+ export function fitCamera(w, h, box) {
372
+ const s = Math.max(0.15, Math.min(1.25, w / (box.x1 - box.x0), h / (box.y1 - box.y0)));
373
+ return { x: -((box.x0 + box.x1) / 2) * s, y: -((box.y0 + box.y1) / 2) * s, s };
374
+ }
@@ -13,3 +13,17 @@ export declare function groupRow(c: Cell): {
13
13
  } | null;
14
14
  /** Sorts the rows between the heads and feet of groups, each run on its own; those stay where they are. */
15
15
  export declare function sortGroups(rows: Cell[], col: Sym, desc: boolean, language: string, shows: (v: Cell) => string): Cell[];
16
+ /** From this many rows on, a table or list draws only the ones near the screen. */
17
+ export declare const DRAW_ALL_UP_TO = 200;
18
+ /**
19
+ * The rows to draw, [first, end), of `count` rows each about `height` pixels tall,
20
+ * when the screen shows from `top` to `top + seen` pixels measured from the first
21
+ * row's top. The rows drawn now, `now`, are kept while they cover what is seen.
22
+ */
23
+ export declare function rowWindow(count: number, height: number, top: number, seen: number, now?: {
24
+ first: number;
25
+ end: number;
26
+ }): {
27
+ first: number;
28
+ end: number;
29
+ };
@@ -78,3 +78,32 @@ function sortRows(rows, col, desc, language, shows) {
78
78
  })
79
79
  .map(x => x.row);
80
80
  }
81
+ // --- drawing only the rows near the screen ---------------------------------------------
82
+ /** From this many rows on, a table or list draws only the ones near the screen. */
83
+ export const DRAW_ALL_UP_TO = 200;
84
+ /** Rows drawn beyond each edge of the screen, so a short scroll needs no new rows. */
85
+ const BEYOND = 30;
86
+ /** Rows drawn at least, even when the rows are all out of sight. */
87
+ const AT_LEAST = 60;
88
+ /**
89
+ * The rows to draw, [first, end), of `count` rows each about `height` pixels tall,
90
+ * when the screen shows from `top` to `top + seen` pixels measured from the first
91
+ * row's top. The rows drawn now, `now`, are kept while they cover what is seen.
92
+ */
93
+ export function rowWindow(count, height, top, seen, now) {
94
+ const h = Math.max(1, height);
95
+ const clamp = (n) => Math.min(count, Math.max(0, n));
96
+ const from = clamp(Math.floor(top / h));
97
+ const to = clamp(Math.ceil((top + seen) / h));
98
+ if (now !== undefined && now.end <= count && now.end - now.first >= Math.min(count, AT_LEAST) &&
99
+ now.first <= from && to <= now.end)
100
+ return now;
101
+ let first = clamp(from - BEYOND);
102
+ let end = clamp(to + BEYOND);
103
+ if (end - first < AT_LEAST) {
104
+ if (first === 0 || end < count)
105
+ end = clamp(first + AT_LEAST);
106
+ first = clamp(end - AT_LEAST);
107
+ }
108
+ return { first, end };
109
+ }
@@ -37,6 +37,12 @@ export declare function spawn(sch: Scheduler, s: Series<Cell>, i?: number): Host
37
37
  * `value` for a message. The block is copied, so handlers never share state.
38
38
  */
39
39
  export declare function spawnHandler(sch: Scheduler, body: BlockCell, words: Record<string, Cell>): HostTask;
40
+ /**
41
+ * Starts a task and runs it at once, before any other, until it ends or waits;
42
+ * a long one goes on in turn with the rest. The browser host runs reactions
43
+ * this way, between a round and the repaint that follows it.
44
+ */
45
+ export declare function runNow(sch: Scheduler, s: Series<Cell>, i?: number): HostTask;
40
46
  /** A rejected promise's reason as a Trebol error!. */
41
47
  export declare function asError(e: unknown): ObjectCell;
42
48
  /** Cancels a task: aborts what it waits on; watchers see a `cancelled` error. */
@@ -100,52 +100,76 @@ function finish(sch, ht, state) {
100
100
  for (const w of ht.watchers.splice(0))
101
101
  w(ht);
102
102
  }
103
+ /** Runs one slice of a ready task; true if it yielded and has more to do. */
104
+ function slice(sch, ht) {
105
+ const r = run(ht.task, SLICE);
106
+ switch (r.k) {
107
+ case 'done':
108
+ ht.result = r.v;
109
+ finish(sch, ht, 'done');
110
+ break;
111
+ case 'error':
112
+ ht.error = r.e;
113
+ finish(sch, ht, 'failed');
114
+ break;
115
+ case 'yield':
116
+ return true;
117
+ case 'await':
118
+ ht.state = 'waiting';
119
+ r.p.then(v => {
120
+ if (ht.state !== 'waiting')
121
+ return;
122
+ ht.abort = null;
123
+ resume(ht.task, v);
124
+ ht.state = 'ready';
125
+ sch.ready.push(ht);
126
+ schedule(sch);
127
+ }, (e) => {
128
+ if (ht.state !== 'waiting')
129
+ return;
130
+ ht.abort = null;
131
+ resumeError(ht.task, asError(e));
132
+ ht.state = 'ready';
133
+ sch.ready.push(ht);
134
+ schedule(sch);
135
+ });
136
+ break;
137
+ }
138
+ return false;
139
+ }
103
140
  /** Runs ready tasks, one slice each, until none is ready; then yields. */
104
141
  function pump(sch) {
105
142
  sch.pumping = false;
106
143
  const batch = sch.ready.splice(0);
107
144
  for (const ht of batch) {
108
- if (ht.state !== 'ready')
109
- continue;
110
- const r = run(ht.task, SLICE);
111
- switch (r.k) {
112
- case 'done':
113
- ht.result = r.v;
114
- finish(sch, ht, 'done');
115
- break;
116
- case 'error':
117
- ht.error = r.e;
118
- finish(sch, ht, 'failed');
119
- break;
120
- case 'yield':
121
- sch.ready.push(ht);
122
- break;
123
- case 'await':
124
- ht.state = 'waiting';
125
- r.p.then(v => {
126
- if (ht.state !== 'waiting')
127
- return;
128
- ht.abort = null;
129
- resume(ht.task, v);
130
- ht.state = 'ready';
131
- sch.ready.push(ht);
132
- schedule(sch);
133
- }, (e) => {
134
- if (ht.state !== 'waiting')
135
- return;
136
- ht.abort = null;
137
- resumeError(ht.task, asError(e));
138
- ht.state = 'ready';
139
- sch.ready.push(ht);
140
- schedule(sch);
141
- });
142
- break;
143
- }
145
+ if (ht.state === 'ready' && slice(sch, ht))
146
+ sch.ready.push(ht);
144
147
  }
145
148
  sch.afterPump?.();
146
149
  if (sch.ready.length > 0)
147
150
  schedule(sch);
148
151
  }
152
+ /** Slices a task started with `runNow` may take before it waits its turn like the others. */
153
+ const NOW_SLICES = 10;
154
+ /**
155
+ * Starts a task and runs it at once, before any other, until it ends or waits;
156
+ * a long one goes on in turn with the rest. The browser host runs reactions
157
+ * this way, between a round and the repaint that follows it.
158
+ */
159
+ export function runNow(sch, s, i = 0) {
160
+ const task = newTask(sch.rt, s, i);
161
+ const ht = { id: nextId++, task, state: 'ready', result: UNSET, error: null, abort: null, watchers: [] };
162
+ sch.byTask.set(task, ht);
163
+ sch.live++;
164
+ let more = true;
165
+ for (let n = 0; more && n < NOW_SLICES; n++)
166
+ more = slice(sch, ht);
167
+ if (more) {
168
+ sch.ready.push(ht);
169
+ schedule(sch);
170
+ }
171
+ return ht;
172
+ }
149
173
  /** A rejected promise's reason as a Trebol error!. */
150
174
  export function asError(e) {
151
175
  if (typeof e === 'object' && e !== null && e.t === K.Error)