@delurio/cli 0.5.0 → 0.5.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 +8 -0
- package/README.md +1 -1
- package/dist/html.js +5 -4
- package/dist/model.d.ts +21 -1
- package/dist/model.js +327 -57
- package/dist/pan.d.ts +2 -0
- package/dist/pan.js +252 -0
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,14 @@
|
|
|
2
2
|
|
|
3
3
|
While the version is 0.x, a release that can judge the same graphs differently (a new check, a stricter schema) bumps the minor; a fix that judges nothing differently bumps the patch.
|
|
4
4
|
|
|
5
|
+
## 0.5.2 (2026-11-03)
|
|
6
|
+
|
|
7
|
+
- `page`: the flow's diagram opens fitted to a box as wide as the window, and moves like a design tool's canvas: drag it to move (the hand), Ctrl or ⌘ with the wheel, or a pinch, to zoom at the pointer, and the wheel alone to move it until its edge, after which the page scrolls on. Buttons and keys: − and + (zoom), the zoom level (click: 100%, Shift+0), Fit (Shift+1), Full window (F; Esc leaves it); arrows move it. A click that does not drag still opens a step's screen. Before, a large flow was shrunk into the page's column (QAuth's consent flow to 7%) or scrolled sideways at full size. Without scripts the page keeps the old diagram. Nothing is judged differently.
|
|
8
|
+
|
|
9
|
+
## 0.5.1 (2026-11-03)
|
|
10
|
+
|
|
11
|
+
- `check`, `diff` and `page` explore a large flow about four times faster and in a third of the memory, and judge every graph exactly as before: the same situations in the same order, the same shortest paths and the same findings. On QAuth's consent flow (5.4 million situations from 580,608 starts) `check` took 122 s and 2.9 GB, and now takes 34 s and 1 GB. A situation is kept as a number instead of a string; guards, assertions and effects are read once instead of at every situation; and an assertion is not tested in the states where the state alone rules it out.
|
|
12
|
+
|
|
5
13
|
## 0.5.0 (2026-11-01)
|
|
6
14
|
|
|
7
15
|
- A domain may be split across the files of its directory (SPEC.md §5): every `*.yaml` there that says `kind: domain`, read as one, `domain.yaml` first and then the others by name. Maps merge key by key at any depth, `policies` and `invariants` join, and any other value is given in one file only. `check` (the `schema` and `changes` rules), `diff` and `page` read the merged domain; before, a domain file not named `domain.yaml` was read by none of them.
|
package/README.md
CHANGED
|
@@ -56,7 +56,7 @@ npx @delurio/cli check graphs --strict
|
|
|
56
56
|
npx @delurio/cli page examples/qauth-authorize --out viz/qauth-authorize
|
|
57
57
|
```
|
|
58
58
|
|
|
59
|
-
The page shows the flow in words: its main story, every way it ends, its guarantees and whether they hold, its screens as wireframes, and its diagram. `examples/qauth-authorize` is a small graph to try it on.
|
|
59
|
+
The page shows the flow in words: its main story, every way it ends, its guarantees and whether they hold, its screens as wireframes, and its diagram, which opens fitted to the window and moves like a design tool's canvas (drag to move, Ctrl or ⌘ + scroll or a pinch to zoom, Shift+1 to fit). `examples/qauth-authorize` is a small graph to try it on.
|
|
60
60
|
|
|
61
61
|
Given a folder of graphs instead, it writes the page of every graph under it, each at its own path, and `index.html` listing them all with what their check found:
|
|
62
62
|
|
package/dist/html.js
CHANGED
|
@@ -3,6 +3,7 @@ import { identifiers, parseExpr } from './expr.js';
|
|
|
3
3
|
import { collectEdges, exitLine, kindOf, pressLabel, responseLine } from './edges.js';
|
|
4
4
|
import { narrate, stepsTo } from './story.js';
|
|
5
5
|
import { kitCss } from './kit.js';
|
|
6
|
+
import { PAN_CSS, PAN_SCRIPT } from './pan.js';
|
|
6
7
|
const esc = (s) => s.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"');
|
|
7
8
|
const code = (s) => `<code>${esc(s)}</code>`;
|
|
8
9
|
export const CSS = `
|
|
@@ -172,7 +173,7 @@ export function renderPage(p) {
|
|
|
172
173
|
const heading = g.flow.title ?? humanize(g.flow.id);
|
|
173
174
|
const out = [];
|
|
174
175
|
out.push('<!doctype html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1">');
|
|
175
|
-
out.push(`<title>${esc(heading)}</title><style>${CSS}${kit === null ? '' : kitCss(kit, 'page')}</style></head><body><main>`);
|
|
176
|
+
out.push(`<title>${esc(heading)}</title><style>${CSS}${PAN_CSS}${kit === null ? '' : kitCss(kit, 'page')}</style></head><body><main>`);
|
|
176
177
|
out.push(`<p class="eyebrow">${g.flow.status === undefined ? '' : `<span class="tag">${esc(g.flow.status)}</span> `}<span class="mono">${esc(g.flow.id)}</span></p>`);
|
|
177
178
|
out.push(`<h1>${esc(heading)}</h1>`);
|
|
178
179
|
if (g.flow.summary !== undefined)
|
|
@@ -196,8 +197,8 @@ export function renderPage(p) {
|
|
|
196
197
|
out.push(`</ol><p class="result">Result: ${esc(main.result)}.</p></section>`);
|
|
197
198
|
}
|
|
198
199
|
out.push('<h2>The flow</h2>');
|
|
199
|
-
out.push('<div class="views"><input type="checkbox" id="show-all"><label for="show-all">Show failure paths and “the user closes the page
|
|
200
|
-
out.push(`<div class="scroll panel-main">${p.spineMain}</div><div class="scroll panel-full">${p.spineFull}</div></div>`);
|
|
200
|
+
out.push('<div class="views pz-host"><input type="checkbox" id="show-all"><label for="show-all">Show failure paths and “the user closes the page”</label>');
|
|
201
|
+
out.push(`<div class="scroll panel-main pz">${p.spineMain}</div><div class="scroll panel-full pz">${p.spineFull}</div></div>`);
|
|
201
202
|
out.push(legend(g, ex, main !== null));
|
|
202
203
|
const covers = Object.entries(g.flow.states).filter(([, s]) => (s.includes ?? []).length > 0);
|
|
203
204
|
if (covers.length > 0) {
|
|
@@ -279,6 +280,6 @@ export function renderPage(p) {
|
|
|
279
280
|
out.push(`<details><summary>All transitions (${Object.values(g.flow.states).reduce((n, s) => n + (s.always?.length ?? 0) + Object.values(s.on ?? {}).reduce((k, spec) => k + alternatives(spec).length, 0), 0)})</summary>${transitionsTable(g, ex)}</details>`);
|
|
280
281
|
const from = g.ui.absent === true ? 'flow.yaml (it has no screens)' : `flow.yaml, ui.yaml${kit === null ? ' and the catalog' : `, the catalog and the kit ${esc(kit.id)}`}`;
|
|
281
282
|
out.push(`<footer>delurio graph-viz · generated from ${from} · no AI, no network: the same input gives the same bytes.</footer>`);
|
|
282
|
-
out.push(
|
|
283
|
+
out.push(`</main>${PAN_SCRIPT}</body></html>`);
|
|
283
284
|
return out.join('\n') + '\n';
|
|
284
285
|
}
|
package/dist/model.d.ts
CHANGED
|
@@ -18,11 +18,31 @@ export declare class Model {
|
|
|
18
18
|
readonly flow: Flow;
|
|
19
19
|
readonly vars: string[];
|
|
20
20
|
readonly domains: Scalar[][];
|
|
21
|
+
private readonly at;
|
|
22
|
+
private readonly compiled;
|
|
23
|
+
private readonly ready;
|
|
24
|
+
private readonly ways;
|
|
21
25
|
constructor(flow: Flow);
|
|
22
|
-
private
|
|
26
|
+
private compile;
|
|
27
|
+
private expr;
|
|
23
28
|
test(expr: string, state: string, ctx: Scalar[]): boolean;
|
|
29
|
+
/**
|
|
30
|
+
* Whether evaluating `src` can never throw: it names only the state, variables, and guards that parse and nest no
|
|
31
|
+
* deeper than evaluation allows. Only then may its pairs be tested in another order than the exploration's.
|
|
32
|
+
*/
|
|
33
|
+
pure(src: string): boolean;
|
|
34
|
+
/**
|
|
35
|
+
* The value of `src` in `state` whatever the context holds, when the state alone decides it (`state in [...] and
|
|
36
|
+
* ...` outside those states); undefined when the context matters. Folds only what evaluation would compute the same
|
|
37
|
+
* way: literals, the state, and the side of an `and` / `or` that decides it before the other is read.
|
|
38
|
+
*/
|
|
39
|
+
inState(src: string, state: string): Value | undefined;
|
|
40
|
+
/** `test` for one expression at many pairs: read once, then run at each. */
|
|
41
|
+
tester(expr: string): (state: string, ctx: Scalar[]) => boolean;
|
|
24
42
|
value(expr: string, state: string, ctx: Scalar[]): Value;
|
|
43
|
+
private effect;
|
|
25
44
|
private apply;
|
|
45
|
+
private waysOf;
|
|
26
46
|
steps(state: string, ctx: Scalar[]): Step[];
|
|
27
47
|
initials(): Scalar[][];
|
|
28
48
|
scenario(ctx: Scalar[]): Record<string, Scalar>;
|
package/dist/model.js
CHANGED
|
@@ -1,61 +1,235 @@
|
|
|
1
1
|
import { alternatives } from './graph.js';
|
|
2
|
-
import {
|
|
2
|
+
import { parseExpr } from './expr.js';
|
|
3
3
|
export class ModelError extends Error {
|
|
4
4
|
}
|
|
5
|
+
// The model explores millions of (state, context) pairs on a large flow (QAuth's consent: 5.4 million), so every guard,
|
|
6
|
+
// assertion and effect is read once and kept ready, instead of being looked up by name at every pair.
|
|
5
7
|
export class Model {
|
|
6
8
|
flow;
|
|
7
9
|
vars;
|
|
8
10
|
domains;
|
|
11
|
+
at; // a variable's position in a context
|
|
12
|
+
compiled = new Map();
|
|
13
|
+
ready = new Map();
|
|
14
|
+
ways = new Map();
|
|
9
15
|
constructor(flow) {
|
|
10
16
|
this.flow = flow;
|
|
11
17
|
this.vars = Object.keys(flow.context);
|
|
12
18
|
this.domains = this.vars.map((v) => flow.context[v].values);
|
|
19
|
+
this.at = new Map(this.vars.map((v, i) => [v, i]));
|
|
13
20
|
}
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
21
|
+
// what evaluate(parseExpr(src), lookup) gave, with the same errors, raised only when the name is reached
|
|
22
|
+
compile(e, depth) {
|
|
23
|
+
switch (e.k) {
|
|
24
|
+
case 'lit': {
|
|
25
|
+
const v = e.v;
|
|
26
|
+
return () => v;
|
|
27
|
+
}
|
|
28
|
+
case 'id': {
|
|
29
|
+
const name = e.name;
|
|
30
|
+
if (name === 'state')
|
|
31
|
+
return (state) => state;
|
|
32
|
+
const i = this.at.get(name);
|
|
33
|
+
if (i !== undefined)
|
|
34
|
+
return (_, ctx) => ctx[i];
|
|
35
|
+
const guard = this.flow.guards[name];
|
|
36
|
+
if (guard === undefined)
|
|
37
|
+
return (state) => {
|
|
38
|
+
throw new ModelError(`unknown name "${name}" (state ${state})`);
|
|
39
|
+
};
|
|
40
|
+
if (depth > 8)
|
|
41
|
+
return () => {
|
|
42
|
+
throw new ModelError(`guard ${name} nests too deep`);
|
|
43
|
+
};
|
|
44
|
+
let inner = null; // compiled when first reached, as the lookup read it
|
|
45
|
+
return (state, ctx) => (inner ??= this.compile(parseExpr(guard), depth + 1))(state, ctx);
|
|
46
|
+
}
|
|
47
|
+
case 'list': {
|
|
48
|
+
const items = e.items.map((x) => this.compile(x, depth));
|
|
49
|
+
return (state, ctx) => items.map((f) => f(state, ctx));
|
|
50
|
+
}
|
|
51
|
+
case 'not': {
|
|
52
|
+
const f = this.compile(e.e, depth);
|
|
53
|
+
return (state, ctx) => !f(state, ctx);
|
|
54
|
+
}
|
|
55
|
+
case 'and': {
|
|
56
|
+
const l = this.compile(e.l, depth);
|
|
57
|
+
const r = this.compile(e.r, depth);
|
|
58
|
+
return (state, ctx) => Boolean(l(state, ctx)) && Boolean(r(state, ctx));
|
|
59
|
+
}
|
|
60
|
+
case 'or': {
|
|
61
|
+
const l = this.compile(e.l, depth);
|
|
62
|
+
const r = this.compile(e.r, depth);
|
|
63
|
+
return (state, ctx) => Boolean(l(state, ctx)) || Boolean(r(state, ctx));
|
|
64
|
+
}
|
|
65
|
+
case 'cmp': {
|
|
66
|
+
const l = this.compile(e.l, depth);
|
|
67
|
+
const r = this.compile(e.r, depth);
|
|
68
|
+
if (e.op === '==')
|
|
69
|
+
return (state, ctx) => l(state, ctx) === r(state, ctx);
|
|
70
|
+
if (e.op === '!=')
|
|
71
|
+
return (state, ctx) => l(state, ctx) !== r(state, ctx);
|
|
72
|
+
const negate = e.op === 'not in';
|
|
73
|
+
// a list of literals, the usual right side, is the same list every time: made once
|
|
74
|
+
const fixed = e.r.k === 'list' && e.r.items.every((x) => x.k === 'lit') ? e.r.items.map((x) => x.v) : null;
|
|
75
|
+
if (fixed !== null)
|
|
76
|
+
return (state, ctx) => {
|
|
77
|
+
const x = l(state, ctx);
|
|
78
|
+
let inList = false;
|
|
79
|
+
for (let j = 0; j < fixed.length && !inList; j++)
|
|
80
|
+
inList = fixed[j] === x;
|
|
81
|
+
return negate ? !inList : inList;
|
|
82
|
+
};
|
|
83
|
+
return (state, ctx) => {
|
|
84
|
+
const x = l(state, ctx);
|
|
85
|
+
const list = r(state, ctx);
|
|
86
|
+
const inList = Array.isArray(list) && list.some((y) => y === x);
|
|
87
|
+
return negate ? !inList : inList;
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
expr(src) {
|
|
93
|
+
let f = this.compiled.get(src);
|
|
94
|
+
if (f === undefined) {
|
|
95
|
+
f = this.compile(parseExpr(src), 0);
|
|
96
|
+
this.compiled.set(src, f);
|
|
97
|
+
}
|
|
98
|
+
return f;
|
|
28
99
|
}
|
|
29
100
|
test(expr, state, ctx) {
|
|
30
|
-
return Boolean(this.
|
|
101
|
+
return Boolean(this.expr(expr)(state, ctx));
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Whether evaluating `src` can never throw: it names only the state, variables, and guards that parse and nest no
|
|
105
|
+
* deeper than evaluation allows. Only then may its pairs be tested in another order than the exploration's.
|
|
106
|
+
*/
|
|
107
|
+
pure(src) {
|
|
108
|
+
const ok = (e, depth, seen) => {
|
|
109
|
+
switch (e.k) {
|
|
110
|
+
case 'lit':
|
|
111
|
+
return true;
|
|
112
|
+
case 'id': {
|
|
113
|
+
if (e.name === 'state' || this.at.has(e.name))
|
|
114
|
+
return true;
|
|
115
|
+
const guard = this.flow.guards[e.name];
|
|
116
|
+
if (guard === undefined || depth > 8 || seen.has(e.name))
|
|
117
|
+
return false;
|
|
118
|
+
try {
|
|
119
|
+
return ok(parseExpr(guard), depth + 1, new Set([...seen, e.name]));
|
|
120
|
+
}
|
|
121
|
+
catch {
|
|
122
|
+
return false;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
case 'list':
|
|
126
|
+
return e.items.every((x) => ok(x, depth, seen));
|
|
127
|
+
case 'not':
|
|
128
|
+
return ok(e.e, depth, seen);
|
|
129
|
+
default:
|
|
130
|
+
return ok(e.l, depth, seen) && ok(e.r, depth, seen);
|
|
131
|
+
}
|
|
132
|
+
};
|
|
133
|
+
try {
|
|
134
|
+
return ok(parseExpr(src), 0, new Set());
|
|
135
|
+
}
|
|
136
|
+
catch {
|
|
137
|
+
return false;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* The value of `src` in `state` whatever the context holds, when the state alone decides it (`state in [...] and
|
|
142
|
+
* ...` outside those states); undefined when the context matters. Folds only what evaluation would compute the same
|
|
143
|
+
* way: literals, the state, and the side of an `and` / `or` that decides it before the other is read.
|
|
144
|
+
*/
|
|
145
|
+
inState(src, state) {
|
|
146
|
+
const fold = (e) => {
|
|
147
|
+
switch (e.k) {
|
|
148
|
+
case 'lit':
|
|
149
|
+
return { v: e.v };
|
|
150
|
+
case 'id':
|
|
151
|
+
return e.name === 'state' ? { v: state } : undefined;
|
|
152
|
+
case 'list': {
|
|
153
|
+
const items = e.items.map(fold);
|
|
154
|
+
return items.every((x) => x !== undefined) ? { v: items.map((x) => x.v) } : undefined;
|
|
155
|
+
}
|
|
156
|
+
case 'not': {
|
|
157
|
+
const x = fold(e.e);
|
|
158
|
+
return x === undefined ? undefined : { v: !x.v };
|
|
159
|
+
}
|
|
160
|
+
case 'and':
|
|
161
|
+
case 'or': {
|
|
162
|
+
const l = fold(e.l);
|
|
163
|
+
if (l === undefined)
|
|
164
|
+
return undefined;
|
|
165
|
+
if (e.k === 'and' ? !l.v : Boolean(l.v))
|
|
166
|
+
return { v: e.k === 'or' };
|
|
167
|
+
const r = fold(e.r);
|
|
168
|
+
return r === undefined ? undefined : { v: Boolean(r.v) };
|
|
169
|
+
}
|
|
170
|
+
case 'cmp': {
|
|
171
|
+
const l = fold(e.l);
|
|
172
|
+
const r = fold(e.r);
|
|
173
|
+
if (l === undefined || r === undefined)
|
|
174
|
+
return undefined;
|
|
175
|
+
if (e.op === '==')
|
|
176
|
+
return { v: l.v === r.v };
|
|
177
|
+
if (e.op === '!=')
|
|
178
|
+
return { v: l.v !== r.v };
|
|
179
|
+
const inList = Array.isArray(r.v) && r.v.some((y) => y === l.v);
|
|
180
|
+
return { v: e.op === 'in' ? inList : !inList };
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
};
|
|
184
|
+
return fold(parseExpr(src))?.v;
|
|
185
|
+
}
|
|
186
|
+
/** `test` for one expression at many pairs: read once, then run at each. */
|
|
187
|
+
tester(expr) {
|
|
188
|
+
const f = this.expr(expr);
|
|
189
|
+
return (state, ctx) => Boolean(f(state, ctx));
|
|
31
190
|
}
|
|
32
191
|
value(expr, state, ctx) {
|
|
33
|
-
return
|
|
192
|
+
return this.expr(expr)(state, ctx);
|
|
34
193
|
}
|
|
35
|
-
|
|
36
|
-
let
|
|
37
|
-
|
|
194
|
+
effect(name) {
|
|
195
|
+
let r = this.ready.get(name);
|
|
196
|
+
if (r === undefined) {
|
|
38
197
|
const effect = this.flow.effects[name];
|
|
39
198
|
if (effect === undefined)
|
|
40
199
|
throw new ModelError(`unknown effect ${name}`);
|
|
200
|
+
r = {
|
|
201
|
+
// a name that is not a variable set nothing a context holds
|
|
202
|
+
set: Object.entries(effect.set ?? {}).flatMap(([k, v]) => (this.at.has(k) ? [[this.at.get(k), v]] : [])),
|
|
203
|
+
havoc: (effect.havoc ?? []).map((h) => {
|
|
204
|
+
const i = this.vars.indexOf(h);
|
|
205
|
+
return [i, this.domains[i]];
|
|
206
|
+
}),
|
|
207
|
+
};
|
|
208
|
+
this.ready.set(name, r);
|
|
209
|
+
}
|
|
210
|
+
return r;
|
|
211
|
+
}
|
|
212
|
+
apply(names, ctx) {
|
|
213
|
+
let outs = [ctx];
|
|
214
|
+
for (const name of names ?? []) {
|
|
215
|
+
const { set, havoc } = this.effect(name);
|
|
41
216
|
const next = [];
|
|
42
217
|
for (const base of outs) {
|
|
43
218
|
const c = base.slice();
|
|
44
|
-
for (const [
|
|
45
|
-
c[
|
|
46
|
-
const havoc = effect.havoc ?? [];
|
|
219
|
+
for (const [i, v] of set)
|
|
220
|
+
c[i] = v;
|
|
47
221
|
if (havoc.length === 0) {
|
|
48
222
|
next.push(c);
|
|
49
223
|
continue;
|
|
50
224
|
}
|
|
225
|
+
// every combination of the havoc variables' values, the first variable slowest, as the nested lists gave them
|
|
51
226
|
let combos = [[]];
|
|
52
|
-
for (const
|
|
53
|
-
combos = combos.flatMap((p) =>
|
|
227
|
+
for (const [, values] of havoc)
|
|
228
|
+
combos = combos.flatMap((p) => values.map((v) => [...p, v]));
|
|
54
229
|
for (const combo of combos) {
|
|
55
230
|
const c2 = c.slice();
|
|
56
|
-
havoc.
|
|
57
|
-
c2[
|
|
58
|
-
});
|
|
231
|
+
for (let j = 0; j < havoc.length; j++)
|
|
232
|
+
c2[havoc[j][0]] = combo[j];
|
|
59
233
|
next.push(c2);
|
|
60
234
|
}
|
|
61
235
|
}
|
|
@@ -63,43 +237,64 @@ export class Model {
|
|
|
63
237
|
}
|
|
64
238
|
return outs;
|
|
65
239
|
}
|
|
240
|
+
// a state's ways out, read once: `always` as one group tried in order, else one group per event
|
|
241
|
+
waysOf(state) {
|
|
242
|
+
let w = this.ways.get(state);
|
|
243
|
+
if (w === undefined) {
|
|
244
|
+
const st = this.flow.states[state];
|
|
245
|
+
const way = (event, a, i) => ({
|
|
246
|
+
event,
|
|
247
|
+
target: a.target,
|
|
248
|
+
guard: a.guard,
|
|
249
|
+
effect: a.effect,
|
|
250
|
+
taken: `${state}|${event ?? 'always'}|${i}`,
|
|
251
|
+
});
|
|
252
|
+
w =
|
|
253
|
+
st.type === 'final'
|
|
254
|
+
? { always: false, groups: [] }
|
|
255
|
+
: st.always !== undefined
|
|
256
|
+
? { always: true, groups: [st.always.map((a, i) => way(null, a, i))] }
|
|
257
|
+
: { always: false, groups: Object.entries(st.on ?? {}).map(([event, spec]) => alternatives(spec).map((a, i) => way(event, a, i))) };
|
|
258
|
+
this.ways.set(state, w);
|
|
259
|
+
}
|
|
260
|
+
return w;
|
|
261
|
+
}
|
|
66
262
|
steps(state, ctx) {
|
|
67
|
-
const
|
|
263
|
+
const { always, groups } = this.waysOf(state);
|
|
68
264
|
const out = [];
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
for (let i = 0; i < st.always.length; i++) {
|
|
73
|
-
const a = st.always[i];
|
|
265
|
+
for (const alts of groups) {
|
|
266
|
+
let chosen = false;
|
|
267
|
+
for (const a of alts) {
|
|
74
268
|
if (a.guard === undefined || this.test(a.guard, state, ctx)) {
|
|
75
269
|
for (const c of this.apply(a.effect, ctx))
|
|
76
|
-
out.push({ event:
|
|
77
|
-
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
throw new ModelError(`\`always\` in ${state} is not exhaustive for ${JSON.stringify(Object.fromEntries(this.vars.map((v, i) => [v, ctx[i]])))}`);
|
|
81
|
-
}
|
|
82
|
-
for (const [event, spec] of Object.entries(st.on ?? {})) {
|
|
83
|
-
const alts = alternatives(spec);
|
|
84
|
-
for (let i = 0; i < alts.length; i++) {
|
|
85
|
-
const a = alts[i];
|
|
86
|
-
if (a.guard === undefined || this.test(a.guard, state, ctx)) {
|
|
87
|
-
for (const c of this.apply(a.effect, ctx))
|
|
88
|
-
out.push({ event, target: a.target, ctx: c, taken: `${state}|${event}|${i}` });
|
|
270
|
+
out.push({ event: a.event, target: a.target, ctx: c, taken: a.taken });
|
|
271
|
+
chosen = true;
|
|
89
272
|
break;
|
|
90
273
|
}
|
|
91
274
|
}
|
|
275
|
+
if (always && !chosen)
|
|
276
|
+
throw new ModelError(`\`always\` in ${state} is not exhaustive for ${JSON.stringify(Object.fromEntries(this.vars.map((v, i) => [v, ctx[i]])))}`);
|
|
92
277
|
}
|
|
93
278
|
return out;
|
|
94
279
|
}
|
|
95
280
|
initials() {
|
|
96
|
-
|
|
97
|
-
this.vars.
|
|
281
|
+
// every combination of the starting values, the first variable slowest: the order the nested lists gave
|
|
282
|
+
const choices = this.vars.map((v, i) => {
|
|
98
283
|
const initial = this.flow.context[v].initial;
|
|
99
|
-
|
|
100
|
-
combos = combos.flatMap((p) => values.map((x) => [...p, x]));
|
|
284
|
+
return initial === 'any' ? this.domains[i] : [initial];
|
|
101
285
|
});
|
|
102
|
-
|
|
286
|
+
const total = choices.reduce((n, c) => n * c.length, 1);
|
|
287
|
+
const out = new Array(total);
|
|
288
|
+
for (let k = 0; k < total; k++) {
|
|
289
|
+
const c = new Array(choices.length);
|
|
290
|
+
let rest = k;
|
|
291
|
+
for (let i = choices.length - 1; i >= 0; i--) {
|
|
292
|
+
c[i] = choices[i][rest % choices[i].length];
|
|
293
|
+
rest = Math.floor(rest / choices[i].length);
|
|
294
|
+
}
|
|
295
|
+
out[k] = c;
|
|
296
|
+
}
|
|
297
|
+
return out;
|
|
103
298
|
}
|
|
104
299
|
scenario(ctx) {
|
|
105
300
|
const out = {};
|
|
@@ -110,19 +305,56 @@ export class Model {
|
|
|
110
305
|
return out;
|
|
111
306
|
}
|
|
112
307
|
}
|
|
308
|
+
// A (state, context) pair as one number: the state's position, then each variable's value by its position in the
|
|
309
|
+
// variable's values, as digits of a mixed radix. A Set keeps numbers far faster than the strings they replace.
|
|
310
|
+
// Two pairs are the same exactly when their old keys, `${state}|${ctx.join(',')}`, are: a value is placed by its text,
|
|
311
|
+
// as join wrote it. A flow whose pairs do not fit below 2^53, a value or state name that would make the old key
|
|
312
|
+
// ambiguous, or a value outside its variable's values, keeps the old key.
|
|
313
|
+
function keyer(m) {
|
|
314
|
+
const text = (state, ctx) => `${state}|${ctx.join(',')}`;
|
|
315
|
+
const written = (v) => (v === null || v === undefined ? '' : String(v));
|
|
316
|
+
const names = Object.keys(m.flow.states);
|
|
317
|
+
const size = m.domains.reduce((n, values) => n * Math.max(values.length, 1), Math.max(names.length, 1));
|
|
318
|
+
if (!Number.isSafeInteger(size) || names.some((s) => s.includes('|')) || m.domains.some((values) => values.some((v) => written(v).includes(','))))
|
|
319
|
+
return text;
|
|
320
|
+
const states = new Map(names.map((s, i) => [s, i]));
|
|
321
|
+
const byText = m.domains.map((values) => {
|
|
322
|
+
const d = new Map();
|
|
323
|
+
values.forEach((v, i) => {
|
|
324
|
+
if (!d.has(written(v)))
|
|
325
|
+
d.set(written(v), i);
|
|
326
|
+
});
|
|
327
|
+
return d;
|
|
328
|
+
});
|
|
329
|
+
const byValue = m.domains.map((values, i) => new Map(values.map((v) => [v, byText[i].get(written(v))])));
|
|
330
|
+
const radix = m.domains.map((values) => Math.max(values.length, 1));
|
|
331
|
+
return (state, ctx) => {
|
|
332
|
+
let key = states.get(state);
|
|
333
|
+
if (key === undefined || ctx.length !== radix.length)
|
|
334
|
+
return text(state, ctx);
|
|
335
|
+
for (let i = 0; i < radix.length; i++) {
|
|
336
|
+
const d = byValue[i].get(ctx[i]) ?? byText[i].get(written(ctx[i]));
|
|
337
|
+
if (d === undefined)
|
|
338
|
+
return text(state, ctx);
|
|
339
|
+
key = key * radix[i] + d;
|
|
340
|
+
}
|
|
341
|
+
return key;
|
|
342
|
+
};
|
|
343
|
+
}
|
|
113
344
|
// Every reachable (state, context) pair, breadth first, so the first hit of a state is a shortest path to it.
|
|
114
345
|
// `from` and `keep` narrow the search: the replay starts only from situations the app is bound to and takes only steps it can drive.
|
|
115
346
|
export function explore(m, from = m.initials(), keep) {
|
|
116
347
|
const index = new Set();
|
|
348
|
+
const key = keyer(m);
|
|
117
349
|
const configs = [];
|
|
118
350
|
const taken = new Set();
|
|
119
351
|
const atState = new Map();
|
|
120
352
|
const branchCounts = new Map();
|
|
121
353
|
const push = (state, ctx, parent, event) => {
|
|
122
|
-
const
|
|
123
|
-
if (index.has(
|
|
354
|
+
const k = key(state, ctx);
|
|
355
|
+
if (index.has(k))
|
|
124
356
|
return;
|
|
125
|
-
index.add(
|
|
357
|
+
index.add(k);
|
|
126
358
|
configs.push({ state, ctx, parent, event });
|
|
127
359
|
};
|
|
128
360
|
for (const c of from)
|
|
@@ -141,7 +373,8 @@ export function explore(m, from = m.initials(), keep) {
|
|
|
141
373
|
const always = m.flow.states[state].always;
|
|
142
374
|
if (always !== undefined && steps.length > 0) {
|
|
143
375
|
const counts = branchCounts.get(state) ?? always.map(() => 0);
|
|
144
|
-
|
|
376
|
+
const taken0 = steps[0].taken;
|
|
377
|
+
counts[Number(taken0.slice(taken0.lastIndexOf('|') + 1))] += 1;
|
|
145
378
|
branchCounts.set(state, counts);
|
|
146
379
|
}
|
|
147
380
|
}
|
|
@@ -157,9 +390,46 @@ export function pathTo(m, ex, index) {
|
|
|
157
390
|
}
|
|
158
391
|
return { events: events.reverse(), start: m.scenario(c.ctx) };
|
|
159
392
|
}
|
|
393
|
+
// the first pair, in exploration order, where `src` holds; -1 when none does. An assertion that cannot throw is tested
|
|
394
|
+
// only in the states where the state alone does not rule it out: the first hit of each such state, the earliest of them.
|
|
395
|
+
function firstHit(m, ex, src, byState) {
|
|
396
|
+
// read where the exploration reads it, so an expression that throws throws at the same pair as before
|
|
397
|
+
if (!m.pure(src))
|
|
398
|
+
return ex.configs.findIndex((c) => m.test(src, c.state, c.ctx));
|
|
399
|
+
const test = m.tester(src);
|
|
400
|
+
let hit = -1;
|
|
401
|
+
for (const [state, at] of byState()) {
|
|
402
|
+
const fixed = m.inState(src, state);
|
|
403
|
+
if (fixed !== undefined && !fixed)
|
|
404
|
+
continue;
|
|
405
|
+
for (const i of at) {
|
|
406
|
+
if (hit >= 0 && i > hit)
|
|
407
|
+
break;
|
|
408
|
+
if (fixed !== undefined || test(state, ex.configs[i].ctx)) {
|
|
409
|
+
hit = i;
|
|
410
|
+
break;
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
return hit;
|
|
415
|
+
}
|
|
160
416
|
export function checkAssertions(m, ex) {
|
|
417
|
+
let groups = null;
|
|
418
|
+
const byState = () => {
|
|
419
|
+
if (groups === null) {
|
|
420
|
+
groups = new Map();
|
|
421
|
+
ex.configs.forEach((c, i) => {
|
|
422
|
+
const list = groups?.get(c.state);
|
|
423
|
+
if (list === undefined)
|
|
424
|
+
groups?.set(c.state, [i]);
|
|
425
|
+
else
|
|
426
|
+
list.push(i);
|
|
427
|
+
});
|
|
428
|
+
}
|
|
429
|
+
return groups;
|
|
430
|
+
};
|
|
161
431
|
return (m.flow.assertions ?? []).map((a) => {
|
|
162
|
-
const hit =
|
|
432
|
+
const hit = firstHit(m, ex, a.never, byState);
|
|
163
433
|
if (hit < 0)
|
|
164
434
|
return { id: a.id, never: a.never, say: a.say };
|
|
165
435
|
return { id: a.id, never: a.never, say: a.say, violation: { ...pathTo(m, ex, hit), state: ex.configs[hit].state } };
|
package/dist/pan.d.ts
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
export declare const PAN_CSS = "\n.pz-on { position: relative; height: 78vh; min-height: 360px; overflow: hidden; margin-left: calc(50% - 50vw + 24px); margin-right: calc(50% - 50vw + 24px); cursor: grab; touch-action: none; user-select: none; -webkit-user-select: none; }\n.pz-on.pz-grabbing, .pz-on.pz-grabbing a { cursor: grabbing; }\n.pz-on:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }\n.pz-on svg.spine { width: 100%; height: 100%; max-width: none; padding: 0; margin: 0; }\n.pz-on a { cursor: pointer; -webkit-user-drag: none; }\n.pz-bar { position: absolute; top: 10px; right: 10px; display: flex; gap: 4px; cursor: default; }\n.pz-bar button { font: 13px system-ui, sans-serif; min-width: 32px; height: 30px; padding: 0 10px; border: 1px solid var(--line); border-radius: 8px; background: var(--surface); color: var(--ink); cursor: pointer; }\n.pz-bar button:hover { border-color: var(--muted); } .pz-bar button:focus-visible { outline: 2px solid var(--focus); outline-offset: 1px; }\n.pz-bar .pz-pct { min-width: 58px; font-variant-numeric: tabular-nums; }\n.pz-hint { position: absolute; left: 10px; bottom: 10px; margin: 0; padding: 2px 8px; border-radius: 6px; background: var(--surface); color: var(--muted); font-size: 12px; pointer-events: none; }\nhtml.pz-lock, html.pz-lock body { overflow: hidden; }\n.pz-host.pz-full { position: fixed; inset: 0; z-index: 100; margin: 0; padding: 10px 12px 12px; background: var(--bg); display: grid; grid-template-columns: auto 1fr; grid-template-rows: auto minmax(0, 1fr); align-items: center; }\n.pz-host.pz-full { row-gap: 10px; } .pz-host.pz-full > label { margin: 0; }\n.pz-host.pz-full > .pz-on { grid-column: 1 / -1; grid-row: 2; align-self: stretch; height: auto; min-height: 0; margin: 0; }\n.pz-on.pz-full { position: fixed; inset: 0; z-index: 100; height: auto; min-height: 0; margin: 0; border-radius: 0; }\n";
|
|
2
|
+
export declare const PAN_SCRIPT = "<script>(() => {\n const PAD = 24, MAX = 4;\n for (const box of document.querySelectorAll('.pz')) {\n const svg = box.querySelector('svg.spine');\n if (!svg || !svg.viewBox.baseVal || !svg.viewBox.baseVal.width) continue;\n const W = svg.viewBox.baseVal.width, H = svg.viewBox.baseVal.height;\n const host = box.closest('.pz-host') || box;\n svg.removeAttribute('style');\n box.classList.add('pz-on');\n box.tabIndex = 0;\n\n let s = 1, x = 0, y = 0, vw = 0, vh = 0, fitted = true;\n const bar = document.createElement('div');\n bar.className = 'pz-bar';\n const button = (text, title, act) => {\n const b = document.createElement('button');\n b.type = 'button';\n b.textContent = text;\n b.title = title;\n b.addEventListener('click', act);\n bar.append(b);\n return b;\n };\n const fitScale = () => Math.max(Math.min((vw - 2 * PAD) / W, (vh - 2 * PAD) / H), 0.001);\n const minScale = () => Math.min(fitScale(), 1) / 2;\n // where the box's top left may be on an axis, in the diagram's units. Snug: centred when the diagram fits on it,\n // else from PAD before its start to PAD past its end. The hand may go further, until the diagram's edge reaches\n // the middle of the box, so it is never lost.\n const snug = (len, view) => {\n const p = PAD / s;\n return len + 2 * p <= view ? [(len - view) / 2, (len - view) / 2] : [-p, len + p - view];\n };\n const free = (len, view) => {\n const [lo, hi] = snug(len, view);\n return [Math.min(lo, -view / 2), Math.max(hi, len - view / 2)];\n };\n const within = (v, [lo, hi]) => Math.min(Math.max(v, lo), hi);\n const clamp = () => {\n x = within(x, free(W, vw / s));\n y = within(y, free(H, vh / s));\n };\n const draw = () => {\n clamp();\n svg.setAttribute('viewBox', x + ' ' + y + ' ' + vw / s + ' ' + vh / s);\n pct.textContent = Math.round(s * 100) + '%';\n };\n const zoomAt = (px, py, factor) => {\n const ns = Math.min(MAX, Math.max(minScale(), s * factor));\n const dx = x + px / s, dy = y + py / s;\n s = ns;\n x = dx - px / s;\n y = dy - py / s;\n fitted = false;\n draw();\n };\n const fit = () => {\n s = fitScale();\n x = (W - vw / s) / 2;\n y = (H - vh / s) / 2;\n fitted = true;\n draw();\n };\n const actual = () => zoomAt(vw / 2, vh / 2, 1 / s);\n const setFull = (on) => {\n host.classList.toggle('pz-full', on);\n document.documentElement.classList.toggle('pz-lock', on);\n for (const b of host.querySelectorAll('.pz-fs')) b.textContent = on ? 'Exit full window' : 'Full window';\n if (on) box.focus({ preventScroll: true });\n };\n\n button('\\u2212', 'Zoom out (\\u2212)', () => zoomAt(vw / 2, vh / 2, 0.8));\n const pct = button('100%', 'Show at 100% (Shift+0)', actual);\n pct.className = 'pz-pct';\n button('+', 'Zoom in (+)', () => zoomAt(vw / 2, vh / 2, 1.25));\n button('Fit', 'Fit the whole diagram (Shift+1)', fit);\n button('Full window', 'Give the diagram the whole window (F; Esc leaves it)', () => setFull(!host.classList.contains('pz-full'))).className = 'pz-fs';\n const hint = document.createElement('p');\n hint.className = 'pz-hint';\n hint.textContent = 'Drag to move \\u00b7 Ctrl or \\u2318 + scroll, or pinch, to zoom \\u00b7 Shift+1 fits';\n box.append(bar, hint);\n\n // a new size keeps a fitted diagram fitted, and keeps the centre of one zoomed by hand; a hidden box (the other\n // view) keeps its place for when it shows again\n new ResizeObserver(() => {\n const nw = box.clientWidth, nh = box.clientHeight;\n if (!nw || !nh) return;\n const first = !vw, cx = x + vw / (2 * s), cy = y + vh / (2 * s);\n vw = nw;\n vh = nh;\n if (first || fitted) return fit();\n x = cx - vw / (2 * s);\n y = cy - vh / (2 * s);\n draw();\n }).observe(box);\n\n let gesture = false;\n box.addEventListener('wheel', (e) => {\n if (!vw) return;\n const r = box.getBoundingClientRect();\n const unit = e.deltaMode === 1 ? 16 : e.deltaMode === 2 ? vh : 1;\n if (e.ctrlKey || e.metaKey) {\n e.preventDefault();\n if (gesture) return;\n const d = Math.max(-50, Math.min(50, e.deltaY * unit));\n zoomAt(e.clientX - r.left, e.clientY - r.top, Math.exp(-d * 0.006));\n return;\n }\n let dx = e.deltaX * unit, dy = e.deltaY * unit;\n if (e.shiftKey && dx === 0) { dx = dy; dy = 0; }\n // the wheel moves the diagram only within its snug range (or back toward it); at the end of that range, or on a\n // diagram that fits, it scrolls the page instead\n const [xl, xh] = snug(W, vw / s), [yl, yh] = snug(H, vh / s);\n const nx = within(x + dx / s, [Math.min(xl, x), Math.max(xh, x)]);\n const ny = within(y + dy / s, [Math.min(yl, y), Math.max(yh, y)]);\n if (Math.abs(nx - x) * s < 0.5 && Math.abs(ny - y) * s < 0.5) return;\n e.preventDefault();\n x = nx;\n y = ny;\n fitted = false;\n draw();\n }, { passive: false });\n // Safari's pinch on a trackpad\n box.addEventListener('gesturestart', (e) => { e.preventDefault(); gesture = true; box.dataset.g = '1'; });\n box.addEventListener('gesturechange', (e) => {\n e.preventDefault();\n const r = box.getBoundingClientRect();\n zoomAt(e.clientX - r.left, e.clientY - r.top, e.scale / Number(box.dataset.g));\n box.dataset.g = String(e.scale);\n });\n box.addEventListener('gestureend', (e) => { e.preventDefault(); gesture = false; });\n\n // one pointer moves the diagram, two (a pinch on a touch screen) zoom it; a press that does not move is a click\n const pts = new Map();\n let drag = null, pinch = null, moved = false;\n const capture = (id) => {\n try { if (!box.hasPointerCapture(id)) box.setPointerCapture(id); } catch { /* the pointer is already gone */ }\n };\n const two = () => {\n const [a, b] = [...pts.values()];\n return { mx: (a.x + b.x) / 2, my: (a.y + b.y) / 2, d: Math.hypot(a.x - b.x, a.y - b.y) || 1 };\n };\n box.addEventListener('pointerdown', (e) => {\n if (e.target.closest('.pz-bar')) return;\n if (e.pointerType === 'mouse' && e.button !== 0 && e.button !== 1) return;\n if (e.button === 1) e.preventDefault();\n pts.set(e.pointerId, { x: e.clientX, y: e.clientY });\n if (pts.size === 1) {\n drag = { id: e.pointerId, sx: e.clientX, sy: e.clientY, x, y };\n moved = false;\n } else if (pts.size === 2) {\n pinch = two();\n drag = null;\n moved = true;\n for (const id of pts.keys()) capture(id);\n }\n });\n box.addEventListener('pointermove', (e) => {\n const p = pts.get(e.pointerId);\n if (!p) return;\n p.x = e.clientX;\n p.y = e.clientY;\n if (pinch && pts.size >= 2) {\n const n = two(), r = box.getBoundingClientRect();\n x -= (n.mx - pinch.mx) / s;\n y -= (n.my - pinch.my) / s;\n zoomAt(n.mx - r.left, n.my - r.top, n.d / pinch.d);\n pinch = n;\n return;\n }\n if (!drag || drag.id !== e.pointerId) return;\n const dx = e.clientX - drag.sx, dy = e.clientY - drag.sy;\n if (!moved && Math.hypot(dx, dy) < 4) return;\n if (!moved) {\n moved = true;\n capture(e.pointerId);\n box.classList.add('pz-grabbing');\n }\n x = drag.x - dx / s;\n y = drag.y - dy / s;\n fitted = false;\n draw();\n });\n const up = (e) => {\n if (!pts.delete(e.pointerId)) return;\n if (pts.size < 2) pinch = null;\n if (pts.size === 1) {\n // the finger left on the screen after a pinch carries on moving, from where it is now\n const [[id, p]] = [...pts];\n drag = { id, sx: p.x, sy: p.y, x, y };\n } else if (pts.size === 0) {\n drag = null;\n box.classList.remove('pz-grabbing');\n }\n };\n box.addEventListener('pointerup', up);\n box.addEventListener('pointercancel', up);\n box.addEventListener('dragstart', (e) => e.preventDefault());\n box.addEventListener('click', (e) => {\n if (moved) {\n e.preventDefault();\n e.stopPropagation();\n moved = false;\n return;\n }\n // a step's link leads to its section further down the page, which the full window would hide\n if (e.target.closest('a') && host.classList.contains('pz-full')) setFull(false);\n }, true);\n\n box.addEventListener('keydown', (e) => {\n moved = false;\n if (e.altKey || e.ctrlKey || e.metaKey || e.target.closest('.pz-bar') && (e.key === 'Enter' || e.key === ' ')) return;\n const step = e.shiftKey ? 240 : 60;\n if (e.shiftKey && e.code === 'Digit1') fit();\n else if (e.shiftKey && e.code === 'Digit0') actual();\n else if (e.key === '+' || e.key === '=') zoomAt(vw / 2, vh / 2, 1.25);\n else if (e.key === '-' || e.key === '_') zoomAt(vw / 2, vh / 2, 0.8);\n else if (e.key === 'ArrowLeft') { x -= step / s; fitted = false; draw(); }\n else if (e.key === 'ArrowRight') { x += step / s; fitted = false; draw(); }\n else if (e.key === 'ArrowUp') { y -= step / s; fitted = false; draw(); }\n else if (e.key === 'ArrowDown') { y += step / s; fitted = false; draw(); }\n else if (e.key === 'f' || e.key === 'F') setFull(!host.classList.contains('pz-full'));\n else if (e.key === 'Escape' && host.classList.contains('pz-full')) setFull(false);\n else return;\n e.preventDefault();\n });\n document.addEventListener('keydown', (e) => {\n if (e.key === 'Escape' && host.classList.contains('pz-full') && !box.contains(document.activeElement)) setFull(false);\n });\n }\n})();</script>";
|
package/dist/pan.js
ADDED
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
// The diagram as a canvas to move around, as in a design tool: it opens fitted to its box, a drag moves it (the hand),
|
|
2
|
+
// Ctrl or ⌘ with the wheel, or a pinch, zooms at the pointer, and the wheel alone moves it until an edge, after which
|
|
3
|
+
// the page scrolls on. Shift+1 fits it again, Shift+0 shows it at 100%, and "Full window" gives it the whole window.
|
|
4
|
+
// Without scripts the page keeps its plain diagram, which scrolls sideways.
|
|
5
|
+
export const PAN_CSS = `
|
|
6
|
+
.pz-on { position: relative; height: 78vh; min-height: 360px; overflow: hidden; margin-left: calc(50% - 50vw + 24px); margin-right: calc(50% - 50vw + 24px); cursor: grab; touch-action: none; user-select: none; -webkit-user-select: none; }
|
|
7
|
+
.pz-on.pz-grabbing, .pz-on.pz-grabbing a { cursor: grabbing; }
|
|
8
|
+
.pz-on:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }
|
|
9
|
+
.pz-on svg.spine { width: 100%; height: 100%; max-width: none; padding: 0; margin: 0; }
|
|
10
|
+
.pz-on a { cursor: pointer; -webkit-user-drag: none; }
|
|
11
|
+
.pz-bar { position: absolute; top: 10px; right: 10px; display: flex; gap: 4px; cursor: default; }
|
|
12
|
+
.pz-bar button { font: 13px system-ui, sans-serif; min-width: 32px; height: 30px; padding: 0 10px; border: 1px solid var(--line); border-radius: 8px; background: var(--surface); color: var(--ink); cursor: pointer; }
|
|
13
|
+
.pz-bar button:hover { border-color: var(--muted); } .pz-bar button:focus-visible { outline: 2px solid var(--focus); outline-offset: 1px; }
|
|
14
|
+
.pz-bar .pz-pct { min-width: 58px; font-variant-numeric: tabular-nums; }
|
|
15
|
+
.pz-hint { position: absolute; left: 10px; bottom: 10px; margin: 0; padding: 2px 8px; border-radius: 6px; background: var(--surface); color: var(--muted); font-size: 12px; pointer-events: none; }
|
|
16
|
+
html.pz-lock, html.pz-lock body { overflow: hidden; }
|
|
17
|
+
.pz-host.pz-full { position: fixed; inset: 0; z-index: 100; margin: 0; padding: 10px 12px 12px; background: var(--bg); display: grid; grid-template-columns: auto 1fr; grid-template-rows: auto minmax(0, 1fr); align-items: center; }
|
|
18
|
+
.pz-host.pz-full { row-gap: 10px; } .pz-host.pz-full > label { margin: 0; }
|
|
19
|
+
.pz-host.pz-full > .pz-on { grid-column: 1 / -1; grid-row: 2; align-self: stretch; height: auto; min-height: 0; margin: 0; }
|
|
20
|
+
.pz-on.pz-full { position: fixed; inset: 0; z-index: 100; height: auto; min-height: 0; margin: 0; border-radius: 0; }
|
|
21
|
+
`;
|
|
22
|
+
// plain JavaScript, run as the page loads; it reads no data and sends nothing
|
|
23
|
+
export const PAN_SCRIPT = `<script>(() => {
|
|
24
|
+
const PAD = 24, MAX = 4;
|
|
25
|
+
for (const box of document.querySelectorAll('.pz')) {
|
|
26
|
+
const svg = box.querySelector('svg.spine');
|
|
27
|
+
if (!svg || !svg.viewBox.baseVal || !svg.viewBox.baseVal.width) continue;
|
|
28
|
+
const W = svg.viewBox.baseVal.width, H = svg.viewBox.baseVal.height;
|
|
29
|
+
const host = box.closest('.pz-host') || box;
|
|
30
|
+
svg.removeAttribute('style');
|
|
31
|
+
box.classList.add('pz-on');
|
|
32
|
+
box.tabIndex = 0;
|
|
33
|
+
|
|
34
|
+
let s = 1, x = 0, y = 0, vw = 0, vh = 0, fitted = true;
|
|
35
|
+
const bar = document.createElement('div');
|
|
36
|
+
bar.className = 'pz-bar';
|
|
37
|
+
const button = (text, title, act) => {
|
|
38
|
+
const b = document.createElement('button');
|
|
39
|
+
b.type = 'button';
|
|
40
|
+
b.textContent = text;
|
|
41
|
+
b.title = title;
|
|
42
|
+
b.addEventListener('click', act);
|
|
43
|
+
bar.append(b);
|
|
44
|
+
return b;
|
|
45
|
+
};
|
|
46
|
+
const fitScale = () => Math.max(Math.min((vw - 2 * PAD) / W, (vh - 2 * PAD) / H), 0.001);
|
|
47
|
+
const minScale = () => Math.min(fitScale(), 1) / 2;
|
|
48
|
+
// where the box's top left may be on an axis, in the diagram's units. Snug: centred when the diagram fits on it,
|
|
49
|
+
// else from PAD before its start to PAD past its end. The hand may go further, until the diagram's edge reaches
|
|
50
|
+
// the middle of the box, so it is never lost.
|
|
51
|
+
const snug = (len, view) => {
|
|
52
|
+
const p = PAD / s;
|
|
53
|
+
return len + 2 * p <= view ? [(len - view) / 2, (len - view) / 2] : [-p, len + p - view];
|
|
54
|
+
};
|
|
55
|
+
const free = (len, view) => {
|
|
56
|
+
const [lo, hi] = snug(len, view);
|
|
57
|
+
return [Math.min(lo, -view / 2), Math.max(hi, len - view / 2)];
|
|
58
|
+
};
|
|
59
|
+
const within = (v, [lo, hi]) => Math.min(Math.max(v, lo), hi);
|
|
60
|
+
const clamp = () => {
|
|
61
|
+
x = within(x, free(W, vw / s));
|
|
62
|
+
y = within(y, free(H, vh / s));
|
|
63
|
+
};
|
|
64
|
+
const draw = () => {
|
|
65
|
+
clamp();
|
|
66
|
+
svg.setAttribute('viewBox', x + ' ' + y + ' ' + vw / s + ' ' + vh / s);
|
|
67
|
+
pct.textContent = Math.round(s * 100) + '%';
|
|
68
|
+
};
|
|
69
|
+
const zoomAt = (px, py, factor) => {
|
|
70
|
+
const ns = Math.min(MAX, Math.max(minScale(), s * factor));
|
|
71
|
+
const dx = x + px / s, dy = y + py / s;
|
|
72
|
+
s = ns;
|
|
73
|
+
x = dx - px / s;
|
|
74
|
+
y = dy - py / s;
|
|
75
|
+
fitted = false;
|
|
76
|
+
draw();
|
|
77
|
+
};
|
|
78
|
+
const fit = () => {
|
|
79
|
+
s = fitScale();
|
|
80
|
+
x = (W - vw / s) / 2;
|
|
81
|
+
y = (H - vh / s) / 2;
|
|
82
|
+
fitted = true;
|
|
83
|
+
draw();
|
|
84
|
+
};
|
|
85
|
+
const actual = () => zoomAt(vw / 2, vh / 2, 1 / s);
|
|
86
|
+
const setFull = (on) => {
|
|
87
|
+
host.classList.toggle('pz-full', on);
|
|
88
|
+
document.documentElement.classList.toggle('pz-lock', on);
|
|
89
|
+
for (const b of host.querySelectorAll('.pz-fs')) b.textContent = on ? 'Exit full window' : 'Full window';
|
|
90
|
+
if (on) box.focus({ preventScroll: true });
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
button('\\u2212', 'Zoom out (\\u2212)', () => zoomAt(vw / 2, vh / 2, 0.8));
|
|
94
|
+
const pct = button('100%', 'Show at 100% (Shift+0)', actual);
|
|
95
|
+
pct.className = 'pz-pct';
|
|
96
|
+
button('+', 'Zoom in (+)', () => zoomAt(vw / 2, vh / 2, 1.25));
|
|
97
|
+
button('Fit', 'Fit the whole diagram (Shift+1)', fit);
|
|
98
|
+
button('Full window', 'Give the diagram the whole window (F; Esc leaves it)', () => setFull(!host.classList.contains('pz-full'))).className = 'pz-fs';
|
|
99
|
+
const hint = document.createElement('p');
|
|
100
|
+
hint.className = 'pz-hint';
|
|
101
|
+
hint.textContent = 'Drag to move \\u00b7 Ctrl or \\u2318 + scroll, or pinch, to zoom \\u00b7 Shift+1 fits';
|
|
102
|
+
box.append(bar, hint);
|
|
103
|
+
|
|
104
|
+
// a new size keeps a fitted diagram fitted, and keeps the centre of one zoomed by hand; a hidden box (the other
|
|
105
|
+
// view) keeps its place for when it shows again
|
|
106
|
+
new ResizeObserver(() => {
|
|
107
|
+
const nw = box.clientWidth, nh = box.clientHeight;
|
|
108
|
+
if (!nw || !nh) return;
|
|
109
|
+
const first = !vw, cx = x + vw / (2 * s), cy = y + vh / (2 * s);
|
|
110
|
+
vw = nw;
|
|
111
|
+
vh = nh;
|
|
112
|
+
if (first || fitted) return fit();
|
|
113
|
+
x = cx - vw / (2 * s);
|
|
114
|
+
y = cy - vh / (2 * s);
|
|
115
|
+
draw();
|
|
116
|
+
}).observe(box);
|
|
117
|
+
|
|
118
|
+
let gesture = false;
|
|
119
|
+
box.addEventListener('wheel', (e) => {
|
|
120
|
+
if (!vw) return;
|
|
121
|
+
const r = box.getBoundingClientRect();
|
|
122
|
+
const unit = e.deltaMode === 1 ? 16 : e.deltaMode === 2 ? vh : 1;
|
|
123
|
+
if (e.ctrlKey || e.metaKey) {
|
|
124
|
+
e.preventDefault();
|
|
125
|
+
if (gesture) return;
|
|
126
|
+
const d = Math.max(-50, Math.min(50, e.deltaY * unit));
|
|
127
|
+
zoomAt(e.clientX - r.left, e.clientY - r.top, Math.exp(-d * 0.006));
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
let dx = e.deltaX * unit, dy = e.deltaY * unit;
|
|
131
|
+
if (e.shiftKey && dx === 0) { dx = dy; dy = 0; }
|
|
132
|
+
// the wheel moves the diagram only within its snug range (or back toward it); at the end of that range, or on a
|
|
133
|
+
// diagram that fits, it scrolls the page instead
|
|
134
|
+
const [xl, xh] = snug(W, vw / s), [yl, yh] = snug(H, vh / s);
|
|
135
|
+
const nx = within(x + dx / s, [Math.min(xl, x), Math.max(xh, x)]);
|
|
136
|
+
const ny = within(y + dy / s, [Math.min(yl, y), Math.max(yh, y)]);
|
|
137
|
+
if (Math.abs(nx - x) * s < 0.5 && Math.abs(ny - y) * s < 0.5) return;
|
|
138
|
+
e.preventDefault();
|
|
139
|
+
x = nx;
|
|
140
|
+
y = ny;
|
|
141
|
+
fitted = false;
|
|
142
|
+
draw();
|
|
143
|
+
}, { passive: false });
|
|
144
|
+
// Safari's pinch on a trackpad
|
|
145
|
+
box.addEventListener('gesturestart', (e) => { e.preventDefault(); gesture = true; box.dataset.g = '1'; });
|
|
146
|
+
box.addEventListener('gesturechange', (e) => {
|
|
147
|
+
e.preventDefault();
|
|
148
|
+
const r = box.getBoundingClientRect();
|
|
149
|
+
zoomAt(e.clientX - r.left, e.clientY - r.top, e.scale / Number(box.dataset.g));
|
|
150
|
+
box.dataset.g = String(e.scale);
|
|
151
|
+
});
|
|
152
|
+
box.addEventListener('gestureend', (e) => { e.preventDefault(); gesture = false; });
|
|
153
|
+
|
|
154
|
+
// one pointer moves the diagram, two (a pinch on a touch screen) zoom it; a press that does not move is a click
|
|
155
|
+
const pts = new Map();
|
|
156
|
+
let drag = null, pinch = null, moved = false;
|
|
157
|
+
const capture = (id) => {
|
|
158
|
+
try { if (!box.hasPointerCapture(id)) box.setPointerCapture(id); } catch { /* the pointer is already gone */ }
|
|
159
|
+
};
|
|
160
|
+
const two = () => {
|
|
161
|
+
const [a, b] = [...pts.values()];
|
|
162
|
+
return { mx: (a.x + b.x) / 2, my: (a.y + b.y) / 2, d: Math.hypot(a.x - b.x, a.y - b.y) || 1 };
|
|
163
|
+
};
|
|
164
|
+
box.addEventListener('pointerdown', (e) => {
|
|
165
|
+
if (e.target.closest('.pz-bar')) return;
|
|
166
|
+
if (e.pointerType === 'mouse' && e.button !== 0 && e.button !== 1) return;
|
|
167
|
+
if (e.button === 1) e.preventDefault();
|
|
168
|
+
pts.set(e.pointerId, { x: e.clientX, y: e.clientY });
|
|
169
|
+
if (pts.size === 1) {
|
|
170
|
+
drag = { id: e.pointerId, sx: e.clientX, sy: e.clientY, x, y };
|
|
171
|
+
moved = false;
|
|
172
|
+
} else if (pts.size === 2) {
|
|
173
|
+
pinch = two();
|
|
174
|
+
drag = null;
|
|
175
|
+
moved = true;
|
|
176
|
+
for (const id of pts.keys()) capture(id);
|
|
177
|
+
}
|
|
178
|
+
});
|
|
179
|
+
box.addEventListener('pointermove', (e) => {
|
|
180
|
+
const p = pts.get(e.pointerId);
|
|
181
|
+
if (!p) return;
|
|
182
|
+
p.x = e.clientX;
|
|
183
|
+
p.y = e.clientY;
|
|
184
|
+
if (pinch && pts.size >= 2) {
|
|
185
|
+
const n = two(), r = box.getBoundingClientRect();
|
|
186
|
+
x -= (n.mx - pinch.mx) / s;
|
|
187
|
+
y -= (n.my - pinch.my) / s;
|
|
188
|
+
zoomAt(n.mx - r.left, n.my - r.top, n.d / pinch.d);
|
|
189
|
+
pinch = n;
|
|
190
|
+
return;
|
|
191
|
+
}
|
|
192
|
+
if (!drag || drag.id !== e.pointerId) return;
|
|
193
|
+
const dx = e.clientX - drag.sx, dy = e.clientY - drag.sy;
|
|
194
|
+
if (!moved && Math.hypot(dx, dy) < 4) return;
|
|
195
|
+
if (!moved) {
|
|
196
|
+
moved = true;
|
|
197
|
+
capture(e.pointerId);
|
|
198
|
+
box.classList.add('pz-grabbing');
|
|
199
|
+
}
|
|
200
|
+
x = drag.x - dx / s;
|
|
201
|
+
y = drag.y - dy / s;
|
|
202
|
+
fitted = false;
|
|
203
|
+
draw();
|
|
204
|
+
});
|
|
205
|
+
const up = (e) => {
|
|
206
|
+
if (!pts.delete(e.pointerId)) return;
|
|
207
|
+
if (pts.size < 2) pinch = null;
|
|
208
|
+
if (pts.size === 1) {
|
|
209
|
+
// the finger left on the screen after a pinch carries on moving, from where it is now
|
|
210
|
+
const [[id, p]] = [...pts];
|
|
211
|
+
drag = { id, sx: p.x, sy: p.y, x, y };
|
|
212
|
+
} else if (pts.size === 0) {
|
|
213
|
+
drag = null;
|
|
214
|
+
box.classList.remove('pz-grabbing');
|
|
215
|
+
}
|
|
216
|
+
};
|
|
217
|
+
box.addEventListener('pointerup', up);
|
|
218
|
+
box.addEventListener('pointercancel', up);
|
|
219
|
+
box.addEventListener('dragstart', (e) => e.preventDefault());
|
|
220
|
+
box.addEventListener('click', (e) => {
|
|
221
|
+
if (moved) {
|
|
222
|
+
e.preventDefault();
|
|
223
|
+
e.stopPropagation();
|
|
224
|
+
moved = false;
|
|
225
|
+
return;
|
|
226
|
+
}
|
|
227
|
+
// a step's link leads to its section further down the page, which the full window would hide
|
|
228
|
+
if (e.target.closest('a') && host.classList.contains('pz-full')) setFull(false);
|
|
229
|
+
}, true);
|
|
230
|
+
|
|
231
|
+
box.addEventListener('keydown', (e) => {
|
|
232
|
+
moved = false;
|
|
233
|
+
if (e.altKey || e.ctrlKey || e.metaKey || e.target.closest('.pz-bar') && (e.key === 'Enter' || e.key === ' ')) return;
|
|
234
|
+
const step = e.shiftKey ? 240 : 60;
|
|
235
|
+
if (e.shiftKey && e.code === 'Digit1') fit();
|
|
236
|
+
else if (e.shiftKey && e.code === 'Digit0') actual();
|
|
237
|
+
else if (e.key === '+' || e.key === '=') zoomAt(vw / 2, vh / 2, 1.25);
|
|
238
|
+
else if (e.key === '-' || e.key === '_') zoomAt(vw / 2, vh / 2, 0.8);
|
|
239
|
+
else if (e.key === 'ArrowLeft') { x -= step / s; fitted = false; draw(); }
|
|
240
|
+
else if (e.key === 'ArrowRight') { x += step / s; fitted = false; draw(); }
|
|
241
|
+
else if (e.key === 'ArrowUp') { y -= step / s; fitted = false; draw(); }
|
|
242
|
+
else if (e.key === 'ArrowDown') { y += step / s; fitted = false; draw(); }
|
|
243
|
+
else if (e.key === 'f' || e.key === 'F') setFull(!host.classList.contains('pz-full'));
|
|
244
|
+
else if (e.key === 'Escape' && host.classList.contains('pz-full')) setFull(false);
|
|
245
|
+
else return;
|
|
246
|
+
e.preventDefault();
|
|
247
|
+
});
|
|
248
|
+
document.addEventListener('keydown', (e) => {
|
|
249
|
+
if (e.key === 'Escape' && host.classList.contains('pz-full') && !box.contains(document.activeElement)) setFull(false);
|
|
250
|
+
});
|
|
251
|
+
}
|
|
252
|
+
})();</script>`;
|
package/package.json
CHANGED