ui-router-devtools 0.0.1-alpha.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +9 -0
- package/README.md +203 -0
- package/dist/console.d.ts +5 -0
- package/dist/console.d.ts.map +1 -0
- package/dist/console.js +47 -0
- package/dist/console.js.map +1 -0
- package/dist/define.d.ts +8 -0
- package/dist/define.d.ts.map +1 -0
- package/dist/define.js +4 -0
- package/dist/define.js.map +1 -0
- package/dist/edges.d.ts +25 -0
- package/dist/edges.d.ts.map +1 -0
- package/dist/edges.js +28 -0
- package/dist/edges.js.map +1 -0
- package/dist/element/chips.d.ts +17 -0
- package/dist/element/chips.d.ts.map +1 -0
- package/dist/element/chips.js +134 -0
- package/dist/element/chips.js.map +1 -0
- package/dist/element/glyphs.d.ts +34 -0
- package/dist/element/glyphs.d.ts.map +1 -0
- package/dist/element/glyphs.js +107 -0
- package/dist/element/glyphs.js.map +1 -0
- package/dist/element/inspector.d.ts +7 -0
- package/dist/element/inspector.d.ts.map +1 -0
- package/dist/element/inspector.js +183 -0
- package/dist/element/inspector.js.map +1 -0
- package/dist/element/links.d.ts +4 -0
- package/dist/element/links.d.ts.map +1 -0
- package/dist/element/links.js +87 -0
- package/dist/element/links.js.map +1 -0
- package/dist/element/log.d.ts +12 -0
- package/dist/element/log.d.ts.map +1 -0
- package/dist/element/log.js +272 -0
- package/dist/element/log.js.map +1 -0
- package/dist/element/map.d.ts +5 -0
- package/dist/element/map.d.ts.map +1 -0
- package/dist/element/map.js +5 -0
- package/dist/element/map.js.map +1 -0
- package/dist/element/mapview.d.ts +17 -0
- package/dist/element/mapview.d.ts.map +1 -0
- package/dist/element/mapview.js +158 -0
- package/dist/element/mapview.js.map +1 -0
- package/dist/element/measure.d.ts +3 -0
- package/dist/element/measure.d.ts.map +1 -0
- package/dist/element/measure.js +9 -0
- package/dist/element/measure.js.map +1 -0
- package/dist/element/nested.d.ts +45 -0
- package/dist/element/nested.d.ts.map +1 -0
- package/dist/element/nested.js +686 -0
- package/dist/element/nested.js.map +1 -0
- package/dist/element/pack.d.ts +75 -0
- package/dist/element/pack.d.ts.map +1 -0
- package/dist/element/pack.js +281 -0
- package/dist/element/pack.js.map +1 -0
- package/dist/element/panel.d.ts +160 -0
- package/dist/element/panel.d.ts.map +1 -0
- package/dist/element/panel.js +1562 -0
- package/dist/element/panel.js.map +1 -0
- package/dist/element/popover.d.ts +12 -0
- package/dist/element/popover.d.ts.map +1 -0
- package/dist/element/popover.js +38 -0
- package/dist/element/popover.js.map +1 -0
- package/dist/element/route.d.ts +75 -0
- package/dist/element/route.d.ts.map +1 -0
- package/dist/element/route.js +883 -0
- package/dist/element/route.js.map +1 -0
- package/dist/element/scrubber.d.ts +4 -0
- package/dist/element/scrubber.d.ts.map +1 -0
- package/dist/element/scrubber.js +67 -0
- package/dist/element/scrubber.js.map +1 -0
- package/dist/element/styles.d.ts +3 -0
- package/dist/element/styles.d.ts.map +1 -0
- package/dist/element/styles.js +2084 -0
- package/dist/element/styles.js.map +1 -0
- package/dist/element/timeline-styles.d.ts +3 -0
- package/dist/element/timeline-styles.d.ts.map +1 -0
- package/dist/element/timeline-styles.js +286 -0
- package/dist/element/timeline-styles.js.map +1 -0
- package/dist/element/timeline.d.ts +9 -0
- package/dist/element/timeline.d.ts.map +1 -0
- package/dist/element/timeline.js +225 -0
- package/dist/element/timeline.js.map +1 -0
- package/dist/element/tree.d.ts +24 -0
- package/dist/element/tree.d.ts.map +1 -0
- package/dist/element/tree.js +149 -0
- package/dist/element/tree.js.map +1 -0
- package/dist/element/viewmap-styles.d.ts +3 -0
- package/dist/element/viewmap-styles.d.ts.map +1 -0
- package/dist/element/viewmap-styles.js +282 -0
- package/dist/element/viewmap-styles.js.map +1 -0
- package/dist/element/viewmap.d.ts +39 -0
- package/dist/element/viewmap.d.ts.map +1 -0
- package/dist/element/viewmap.js +406 -0
- package/dist/element/viewmap.js.map +1 -0
- package/dist/element/viewport.d.ts +31 -0
- package/dist/element/viewport.d.ts.map +1 -0
- package/dist/element/viewport.js +37 -0
- package/dist/element/viewport.js.map +1 -0
- package/dist/element/waterfall.d.ts +85 -0
- package/dist/element/waterfall.d.ts.map +1 -0
- package/dist/element/waterfall.js +284 -0
- package/dist/element/waterfall.js.map +1 -0
- package/dist/fold.d.ts +27 -0
- package/dist/fold.d.ts.map +1 -0
- package/dist/fold.js +104 -0
- package/dist/fold.js.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -0
- package/dist/lazy.d.ts +57 -0
- package/dist/lazy.d.ts.map +1 -0
- package/dist/lazy.js +63 -0
- package/dist/lazy.js.map +1 -0
- package/dist/lit-ui-router.d.ts +24 -0
- package/dist/lit-ui-router.d.ts.map +1 -0
- package/dist/lit-ui-router.js +102 -0
- package/dist/lit-ui-router.js.map +1 -0
- package/dist/notation.d.ts +28 -0
- package/dist/notation.d.ts.map +1 -0
- package/dist/notation.js +105 -0
- package/dist/notation.js.map +1 -0
- package/dist/performance.d.ts +5 -0
- package/dist/performance.d.ts.map +1 -0
- package/dist/performance.js +66 -0
- package/dist/performance.js.map +1 -0
- package/dist/plugin.d.ts +32 -0
- package/dist/plugin.d.ts.map +1 -0
- package/dist/plugin.js +21 -0
- package/dist/plugin.js.map +1 -0
- package/dist/recorder.d.ts +30 -0
- package/dist/recorder.d.ts.map +1 -0
- package/dist/recorder.js +117 -0
- package/dist/recorder.js.map +1 -0
- package/dist/schema.d.ts +2 -0
- package/dist/schema.d.ts.map +1 -0
- package/dist/schema.js +2 -0
- package/dist/schema.js.map +1 -0
- package/dist/session-schema.d.ts +31 -0
- package/dist/session-schema.d.ts.map +1 -0
- package/dist/session-schema.js +314 -0
- package/dist/session-schema.js.map +1 -0
- package/dist/session.schema.json +1172 -0
- package/dist/shared/event-schema.d.ts +225 -0
- package/dist/shared/event-schema.d.ts.map +1 -0
- package/dist/shared/event-schema.js +2 -0
- package/dist/shared/event-schema.js.map +1 -0
- package/dist/shared/redaction.d.ts +19 -0
- package/dist/shared/redaction.d.ts.map +1 -0
- package/dist/shared/redaction.js +56 -0
- package/dist/shared/redaction.js.map +1 -0
- package/dist/shared/resolve-timing.d.ts +6 -0
- package/dist/shared/resolve-timing.d.ts.map +1 -0
- package/dist/shared/resolve-timing.js +61 -0
- package/dist/shared/resolve-timing.js.map +1 -0
- package/dist/shared/router-events.d.ts +10 -0
- package/dist/shared/router-events.d.ts.map +1 -0
- package/dist/shared/router-events.js +296 -0
- package/dist/shared/router-events.js.map +1 -0
- package/dist/shared/thrown.d.ts +5 -0
- package/dist/shared/thrown.d.ts.map +1 -0
- package/dist/shared/thrown.js +12 -0
- package/dist/shared/thrown.js.map +1 -0
- package/dist/shared/transition-source.d.ts +4 -0
- package/dist/shared/transition-source.d.ts.map +1 -0
- package/dist/shared/transition-source.js +2 -0
- package/dist/shared/transition-source.js.map +1 -0
- package/dist/shared/views.d.ts +10 -0
- package/dist/shared/views.d.ts.map +1 -0
- package/dist/shared/views.js +67 -0
- package/dist/shared/views.js.map +1 -0
- package/dist/spans.d.ts +15 -0
- package/dist/spans.d.ts.map +1 -0
- package/dist/spans.js +24 -0
- package/dist/spans.js.map +1 -0
- package/dist/testing.d.ts +32 -0
- package/dist/testing.d.ts.map +1 -0
- package/dist/testing.js +28 -0
- package/dist/testing.js.map +1 -0
- package/dist/ui-router-devtools.d.ts +36 -0
- package/dist/ui-router-devtools.d.ts.map +1 -0
- package/dist/ui-router-devtools.js +3 -0
- package/dist/ui-router-devtools.js.map +1 -0
- package/dist/views.d.ts +44 -0
- package/dist/views.d.ts.map +1 -0
- package/dist/views.js +43 -0
- package/dist/views.js.map +1 -0
- package/package.json +123 -2
- package/src/console.ts +104 -0
- package/src/define.ts +12 -0
- package/src/edges.ts +64 -0
- package/src/element/chips.ts +187 -0
- package/src/element/glyphs.ts +218 -0
- package/src/element/inspector.ts +315 -0
- package/src/element/links.ts +136 -0
- package/src/element/log.ts +410 -0
- package/src/element/map.ts +22 -0
- package/src/element/mapview.ts +224 -0
- package/src/element/measure.ts +13 -0
- package/src/element/nested.ts +1124 -0
- package/src/element/pack.ts +526 -0
- package/src/element/panel.ts +2410 -0
- package/src/element/popover.ts +64 -0
- package/src/element/route.ts +1481 -0
- package/src/element/scrubber.ts +103 -0
- package/src/element/styles.ts +2084 -0
- package/src/element/timeline-styles.ts +287 -0
- package/src/element/timeline.ts +392 -0
- package/src/element/tree.ts +271 -0
- package/src/element/viewmap-styles.ts +283 -0
- package/src/element/viewmap.ts +733 -0
- package/src/element/viewport.ts +112 -0
- package/src/element/waterfall.ts +585 -0
- package/src/fold.ts +151 -0
- package/src/index.ts +40 -0
- package/src/lazy.ts +134 -0
- package/src/lit-ui-router.ts +194 -0
- package/src/notation.ts +181 -0
- package/src/performance.ts +142 -0
- package/src/plugin.ts +57 -0
- package/src/recorder.ts +228 -0
- package/src/schema.ts +32 -0
- package/src/session-schema.ts +362 -0
- package/src/spans.ts +53 -0
- package/src/testing.ts +74 -0
- package/src/types/import-meta-env.d.ts +9 -0
- package/src/ui-router-devtools.ts +39 -0
- package/src/views.ts +116 -0
|
@@ -0,0 +1,1124 @@
|
|
|
1
|
+
import { html, nothing, svg, type TemplateResult } from 'lit';
|
|
2
|
+
import { repeat } from 'lit/directives/repeat.js';
|
|
3
|
+
import type { Row, DevtoolsPanel } from './panel.ts';
|
|
4
|
+
import type { StateInfo } from '../schema.ts';
|
|
5
|
+
import { ancestorsOf, bareName, type Tx } from '../fold.ts';
|
|
6
|
+
import { edgesOf, edgeWeight } from '../edges.ts';
|
|
7
|
+
import { segment, truncateMiddle } from '../notation.ts';
|
|
8
|
+
import {
|
|
9
|
+
depthClass,
|
|
10
|
+
foldGlyph,
|
|
11
|
+
markGlyph,
|
|
12
|
+
markLabel,
|
|
13
|
+
stateMarks,
|
|
14
|
+
type StateMark,
|
|
15
|
+
} from './glyphs.ts';
|
|
16
|
+
import {
|
|
17
|
+
chooseRung,
|
|
18
|
+
drawnIn,
|
|
19
|
+
fits,
|
|
20
|
+
LARGE_METRICS,
|
|
21
|
+
METRICS,
|
|
22
|
+
pack,
|
|
23
|
+
ROOMY_METRICS,
|
|
24
|
+
SMALL_METRICS,
|
|
25
|
+
type Box,
|
|
26
|
+
type Drawn,
|
|
27
|
+
type Metrics,
|
|
28
|
+
type Placed,
|
|
29
|
+
type Rung,
|
|
30
|
+
type Size,
|
|
31
|
+
} from './pack.ts';
|
|
32
|
+
import { renderMinimap, viewKey } from './mapview.ts';
|
|
33
|
+
import { textWidth } from './measure.ts';
|
|
34
|
+
import {
|
|
35
|
+
route,
|
|
36
|
+
visibleEnd,
|
|
37
|
+
type Rect,
|
|
38
|
+
type RouteBox,
|
|
39
|
+
type Routed,
|
|
40
|
+
type RouteEdge,
|
|
41
|
+
} from './route.ts';
|
|
42
|
+
import { placeChips, type Chip } from './chips.ts';
|
|
43
|
+
import type { Avoid } from './popover.ts';
|
|
44
|
+
import { facts, label, onTreeKey, words } from './tree.ts';
|
|
45
|
+
|
|
46
|
+
const LEAD = 20;
|
|
47
|
+
|
|
48
|
+
/** How long boxes take to settle after a re-pack; edges draw once they have. */
|
|
49
|
+
const SETTLE = 200;
|
|
50
|
+
|
|
51
|
+
/** How long a withdrawn edge stays drawn. */
|
|
52
|
+
const WITHDRAW = 160;
|
|
53
|
+
|
|
54
|
+
interface Options {
|
|
55
|
+
collapse: boolean;
|
|
56
|
+
merge: boolean;
|
|
57
|
+
truncate: boolean;
|
|
58
|
+
metrics: Metrics;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const OPEN = { collapse: false, merge: false, truncate: false };
|
|
62
|
+
|
|
63
|
+
const SHUT = { collapse: true, merge: true, truncate: true };
|
|
64
|
+
|
|
65
|
+
const OPTIONS: Record<Rung, Options> = {
|
|
66
|
+
large: { ...OPEN, metrics: LARGE_METRICS },
|
|
67
|
+
roomy: { ...OPEN, metrics: ROOMY_METRICS },
|
|
68
|
+
expanded: { ...OPEN, metrics: METRICS },
|
|
69
|
+
collapsed: { ...OPEN, collapse: true, metrics: METRICS },
|
|
70
|
+
merged: { ...SHUT, truncate: false, metrics: METRICS },
|
|
71
|
+
truncated: { ...SHUT, metrics: METRICS },
|
|
72
|
+
small: { ...SHUT, metrics: SMALL_METRICS },
|
|
73
|
+
// Nothing fits, so the type stays the dock's, names stay whole and the viewport pans.
|
|
74
|
+
pan: { ...SHUT, truncate: false, metrics: METRICS },
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
/** The type steps a diagram can be drawn at; zooming moves between them, so type and hairlines are never scaled. */
|
|
78
|
+
export const ZOOMS: readonly Metrics[] = [
|
|
79
|
+
SMALL_METRICS,
|
|
80
|
+
METRICS,
|
|
81
|
+
ROOMY_METRICS,
|
|
82
|
+
LARGE_METRICS,
|
|
83
|
+
];
|
|
84
|
+
|
|
85
|
+
/** How far a room's width over height may move from the one a layout was packed for before the layout is packed again. */
|
|
86
|
+
const REPACK_RATIO = 1.25;
|
|
87
|
+
|
|
88
|
+
const STEP = new Map([
|
|
89
|
+
[LARGE_METRICS, 'large'],
|
|
90
|
+
[ROOMY_METRICS, 'roomy'],
|
|
91
|
+
[SMALL_METRICS, 'small'],
|
|
92
|
+
]);
|
|
93
|
+
|
|
94
|
+
/** How far the header's fixed parts are enlarged with the type; the caption rung keeps them. */
|
|
95
|
+
export const scaleOf = (metrics: Metrics): number =>
|
|
96
|
+
Math.max(1, metrics.type / METRICS.type);
|
|
97
|
+
|
|
98
|
+
export interface NestedLayout {
|
|
99
|
+
rung: Rung;
|
|
100
|
+
options: Options;
|
|
101
|
+
metrics: Metrics;
|
|
102
|
+
placed: Placed;
|
|
103
|
+
/** Every drawn box, relative to the diagram's origin; the edge router and the popover read these. */
|
|
104
|
+
rects: ReadonlyMap<string, Rect>;
|
|
105
|
+
/** Drawn boxes in tree order, for the arrow keys. */
|
|
106
|
+
rows: Row[];
|
|
107
|
+
/** Names of the compounds drawn with their children. */
|
|
108
|
+
open: ReadonlySet<string>;
|
|
109
|
+
/** The monospace face the labels were measured in. */
|
|
110
|
+
family: string;
|
|
111
|
+
rtl: boolean;
|
|
112
|
+
/** The layout's place in `ZOOMS`. */
|
|
113
|
+
step: number;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
interface Build {
|
|
117
|
+
host: DevtoolsPanel;
|
|
118
|
+
children: Map<string | null, StateInfo[]>;
|
|
119
|
+
/** Compounds on the active path or holding the selection; these never collapse on their own. */
|
|
120
|
+
keep: ReadonlySet<string>;
|
|
121
|
+
options: Options;
|
|
122
|
+
family: string;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
const isOpen = ({ host, keep, options }: Build, name: string): boolean =>
|
|
126
|
+
host._mapOpen.get(name) ?? (!options.collapse || keep.has(name));
|
|
127
|
+
|
|
128
|
+
const countUnder = (build: Build, name: string): number =>
|
|
129
|
+
(build.children.get(name) ?? []).reduce(
|
|
130
|
+
(total, child) => total + 1 + countUnder(build, child.name),
|
|
131
|
+
0,
|
|
132
|
+
);
|
|
133
|
+
|
|
134
|
+
const nameOf = (state: StateInfo, options: Options): string =>
|
|
135
|
+
options.truncate ? truncateMiddle(segment(state.name)) : segment(state.name);
|
|
136
|
+
|
|
137
|
+
const labelWidth = (
|
|
138
|
+
build: Build,
|
|
139
|
+
state: StateInfo,
|
|
140
|
+
collapsed: boolean,
|
|
141
|
+
merging: boolean,
|
|
142
|
+
): number => {
|
|
143
|
+
const { host, options, family } = build;
|
|
144
|
+
const size = options.metrics.type;
|
|
145
|
+
const scale = scaleOf(options.metrics);
|
|
146
|
+
const weight = state.name === host._model.current.name ? 700 : 400;
|
|
147
|
+
const marks = stateMarks(state, host._model.states).length;
|
|
148
|
+
const font = (w: number) => `${w} ${size}px ${family}`;
|
|
149
|
+
|
|
150
|
+
// The count is measured like the name, so a header is never a fraction of a pixel short and elided.
|
|
151
|
+
const count = collapsed
|
|
152
|
+
? 8 * scale + textWidth(String(countUnder(build, state.name)), font(400))
|
|
153
|
+
: 0;
|
|
154
|
+
|
|
155
|
+
const name = textWidth(nameOf(state, options), font(weight));
|
|
156
|
+
|
|
157
|
+
// A merging header is the glyph, the name and the chevron that leads to the next one.
|
|
158
|
+
if (merging) return Math.ceil(44 * scale + name);
|
|
159
|
+
|
|
160
|
+
const fixed =
|
|
161
|
+
36 +
|
|
162
|
+
(marks ? 4 + marks * 16 : 0) +
|
|
163
|
+
(state.future ? 40 : 0) +
|
|
164
|
+
((build.children.get(state.name) ?? []).length ? 20 : 0);
|
|
165
|
+
|
|
166
|
+
// Padding, the glyph and its gap, then the name and whatever trails it.
|
|
167
|
+
return Math.ceil(fixed * scale + name + count);
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
const buildBox = (build: Build, state: StateInfo): Box => {
|
|
171
|
+
const kids = build.children.get(state.name) ?? [];
|
|
172
|
+
const open = kids.length > 0 && isOpen(build, state.name);
|
|
173
|
+
const children = open ? kids.map((kid) => buildBox(build, kid)) : [];
|
|
174
|
+
|
|
175
|
+
const merge =
|
|
176
|
+
build.options.merge &&
|
|
177
|
+
children.length === 1 &&
|
|
178
|
+
children[0].children.length > 0;
|
|
179
|
+
|
|
180
|
+
for (const child of children)
|
|
181
|
+
if (!merge && state.redirectTo === bareName(child.name)) child.lead = LEAD;
|
|
182
|
+
|
|
183
|
+
return {
|
|
184
|
+
name: state.name,
|
|
185
|
+
label: labelWidth(build, state, kids.length > 0 && !open, merge),
|
|
186
|
+
merge,
|
|
187
|
+
children,
|
|
188
|
+
};
|
|
189
|
+
};
|
|
190
|
+
|
|
191
|
+
const keepOpen = (host: DevtoolsPanel): Set<string> => {
|
|
192
|
+
const { states, current } = host._model;
|
|
193
|
+
const tx = host._inspect === 'tx' ? host._tx(host._selectedTx) : undefined;
|
|
194
|
+
const origin = tx && host._origin(tx);
|
|
195
|
+
|
|
196
|
+
const names = [current.name, tx?.from, tx?.to, origin?.from, origin?.to];
|
|
197
|
+
|
|
198
|
+
const keep = new Set<string>();
|
|
199
|
+
|
|
200
|
+
for (const name of names) {
|
|
201
|
+
if (!name) continue;
|
|
202
|
+
keep.add(name);
|
|
203
|
+
ancestorsOf(states, name).forEach((ancestor) => keep.add(ancestor));
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
// A selected box stays as it is drawn, so selecting moves nothing; only what hides it opens.
|
|
207
|
+
if (host._selectedState)
|
|
208
|
+
ancestorsOf(states, host._selectedState).forEach((a) => keep.add(a));
|
|
209
|
+
|
|
210
|
+
return keep;
|
|
211
|
+
};
|
|
212
|
+
|
|
213
|
+
const rowsOf = (
|
|
214
|
+
boxes: readonly Box[],
|
|
215
|
+
states: ReadonlyMap<string, StateInfo>,
|
|
216
|
+
children: Build['children'],
|
|
217
|
+
depth = 1,
|
|
218
|
+
): Row[] =>
|
|
219
|
+
boxes.flatMap((box, index) => [
|
|
220
|
+
{
|
|
221
|
+
state: states.get(box.name)!,
|
|
222
|
+
depth,
|
|
223
|
+
hasChildren: (children.get(box.name) ?? []).length > 0,
|
|
224
|
+
setsize: boxes.length,
|
|
225
|
+
posinset: index + 1,
|
|
226
|
+
},
|
|
227
|
+
...rowsOf(box.children, states, children, depth + 1),
|
|
228
|
+
]);
|
|
229
|
+
|
|
230
|
+
const openOf = (boxes: readonly Box[]): string[] =>
|
|
231
|
+
boxes.flatMap((box) =>
|
|
232
|
+
box.children.length ? [box.name, ...openOf(box.children)] : [],
|
|
233
|
+
);
|
|
234
|
+
|
|
235
|
+
const cache = new WeakMap<
|
|
236
|
+
DevtoolsPanel,
|
|
237
|
+
{ content: string; key: string; layout: NestedLayout; room: Size }
|
|
238
|
+
>();
|
|
239
|
+
|
|
240
|
+
const sameAspect = (a: Size, b: Size): boolean => {
|
|
241
|
+
const ratio = (a.width * b.height) / Math.max(1, a.height * b.width);
|
|
242
|
+
|
|
243
|
+
return ratio < REPACK_RATIO && ratio > 1 / REPACK_RATIO;
|
|
244
|
+
};
|
|
245
|
+
|
|
246
|
+
const rectsOf = (
|
|
247
|
+
placed: Placed,
|
|
248
|
+
metrics: Metrics,
|
|
249
|
+
origin: { x: number; y: number },
|
|
250
|
+
into: Map<string, Rect> = new Map(),
|
|
251
|
+
): Map<string, Rect> => {
|
|
252
|
+
for (const item of drawnIn(placed, metrics)) {
|
|
253
|
+
const at = { x: origin.x + item.x, y: origin.y + item.y };
|
|
254
|
+
into.set(item.box.name, { ...at, width: item.width, height: item.height });
|
|
255
|
+
rectsOf(item.placed, metrics, at, into);
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
return into;
|
|
259
|
+
};
|
|
260
|
+
|
|
261
|
+
// The diagram's own box: a single top-level state is drawn at the origin, several share it.
|
|
262
|
+
const allRects = (
|
|
263
|
+
placed: Placed,
|
|
264
|
+
metrics: Metrics,
|
|
265
|
+
rtl: boolean,
|
|
266
|
+
): Map<string, Rect> => {
|
|
267
|
+
const rects = new Map<string, Rect>();
|
|
268
|
+
const origin = { x: 0, y: 0 };
|
|
269
|
+
|
|
270
|
+
if (placed.box)
|
|
271
|
+
rects.set(placed.box.name, {
|
|
272
|
+
...origin,
|
|
273
|
+
width: placed.width,
|
|
274
|
+
height: placed.height,
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
rectsOf(placed, metrics, origin, rects);
|
|
278
|
+
|
|
279
|
+
// Boxes are placed from the inline start; the router works in physical coordinates.
|
|
280
|
+
if (rtl)
|
|
281
|
+
for (const [name, rect] of rects)
|
|
282
|
+
rects.set(name, { ...rect, x: placed.width - rect.x - rect.width });
|
|
283
|
+
|
|
284
|
+
return rects;
|
|
285
|
+
};
|
|
286
|
+
|
|
287
|
+
/**
|
|
288
|
+
* Packs the state tree for the Map viewport, stepping down the fit ladder until it fits.
|
|
289
|
+
* A resize that leaves the rung and the room's shape as they were keeps the layout, and only the view of it moves.
|
|
290
|
+
*/
|
|
291
|
+
export const layoutNested = (host: DevtoolsPanel): NestedLayout => {
|
|
292
|
+
const { states, current } = host._model;
|
|
293
|
+
const keep = keepOpen(host);
|
|
294
|
+
const style = getComputedStyle(host);
|
|
295
|
+
// State names are set in the monospace face, so that is the face measured.
|
|
296
|
+
const family = style.getPropertyValue('--uird-font-mono');
|
|
297
|
+
const rtl = style.direction === 'rtl';
|
|
298
|
+
|
|
299
|
+
// The Map viewport is the scroller's content box; the panel's 12px padding is the diagram's outer margin.
|
|
300
|
+
const viewport = host._mapSize;
|
|
301
|
+
|
|
302
|
+
const children = host._children();
|
|
303
|
+
|
|
304
|
+
// Only a compound without a hand-set fold can change the packing, so a selection that opens nothing keeps the layout.
|
|
305
|
+
const opened = [...keep].filter(
|
|
306
|
+
(name) => children.get(name)?.length && !host._mapOpen.has(name),
|
|
307
|
+
);
|
|
308
|
+
|
|
309
|
+
const content = JSON.stringify([
|
|
310
|
+
states,
|
|
311
|
+
current.name,
|
|
312
|
+
opened,
|
|
313
|
+
[...host._mapOpen],
|
|
314
|
+
family,
|
|
315
|
+
rtl,
|
|
316
|
+
host._max,
|
|
317
|
+
host._mapZoom,
|
|
318
|
+
]);
|
|
319
|
+
|
|
320
|
+
const key = content + JSON.stringify(viewport);
|
|
321
|
+
|
|
322
|
+
const cached = cache.get(host);
|
|
323
|
+
|
|
324
|
+
if (cached?.key === key) return cached.layout;
|
|
325
|
+
|
|
326
|
+
const boxesWith = (options: Options) => {
|
|
327
|
+
const build = { host, children, keep, options, family };
|
|
328
|
+
|
|
329
|
+
return (children.get(null) ?? []).map((state) => buildBox(build, state));
|
|
330
|
+
};
|
|
331
|
+
|
|
332
|
+
const sizeAt = (rung: Rung, room: Size): Size =>
|
|
333
|
+
pack(boxesWith(OPTIONS[rung]), room, OPTIONS[rung].metrics);
|
|
334
|
+
|
|
335
|
+
// The ladder holds its rung across a resize only; a changed tree or selection starts from the top.
|
|
336
|
+
const same = cached?.content === content ? cached : undefined;
|
|
337
|
+
const rung = chooseRung(sizeAt, viewport, same && host._rung, host._max);
|
|
338
|
+
const zoom = host._mapZoom === null ? undefined : ZOOMS[host._mapZoom];
|
|
339
|
+
|
|
340
|
+
host._rung = rung;
|
|
341
|
+
|
|
342
|
+
if (
|
|
343
|
+
same?.layout.rung === rung &&
|
|
344
|
+
sameAspect(same.room, viewport) &&
|
|
345
|
+
(zoom || rung === 'pan' || fits(same.layout.placed, viewport))
|
|
346
|
+
) {
|
|
347
|
+
cache.set(host, { ...same, key });
|
|
348
|
+
|
|
349
|
+
return same.layout;
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
// A zoomed diagram is packed for a room scaled with its type, so it keeps the arrangement the fitted one has.
|
|
353
|
+
const options = zoom ? { ...OPTIONS[rung], metrics: zoom } : OPTIONS[rung];
|
|
354
|
+
const scale = options.metrics.type / OPTIONS[rung].metrics.type;
|
|
355
|
+
const boxes = boxesWith(options);
|
|
356
|
+
|
|
357
|
+
const placed = pack(
|
|
358
|
+
boxes,
|
|
359
|
+
{ width: viewport.width * scale, height: viewport.height * scale },
|
|
360
|
+
options.metrics,
|
|
361
|
+
);
|
|
362
|
+
|
|
363
|
+
const byName = new Map(states.map((state) => [state.name, state]));
|
|
364
|
+
|
|
365
|
+
const layout = {
|
|
366
|
+
rung,
|
|
367
|
+
options,
|
|
368
|
+
metrics: options.metrics,
|
|
369
|
+
placed,
|
|
370
|
+
rects: allRects(placed, options.metrics, rtl),
|
|
371
|
+
rows: rowsOf(boxes, byName, children),
|
|
372
|
+
open: new Set(openOf(boxes)),
|
|
373
|
+
family,
|
|
374
|
+
rtl,
|
|
375
|
+
step: ZOOMS.indexOf(options.metrics),
|
|
376
|
+
};
|
|
377
|
+
|
|
378
|
+
cache.set(host, { content, key, layout, room: viewport });
|
|
379
|
+
|
|
380
|
+
return layout;
|
|
381
|
+
};
|
|
382
|
+
|
|
383
|
+
// Edges
|
|
384
|
+
|
|
385
|
+
type EdgeStyle = 'ok' | 'redirect' | 'fault';
|
|
386
|
+
|
|
387
|
+
interface MapEdge extends RouteEdge {
|
|
388
|
+
style: EdgeStyle;
|
|
389
|
+
/** Transitions seen along the line, and against it when it runs both ways; 0 for a selected transition's own edge. */
|
|
390
|
+
count: number;
|
|
391
|
+
back: number;
|
|
392
|
+
/** The transition has not finished. */
|
|
393
|
+
running?: boolean;
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
const styleOf = (tx: Tx): EdgeStyle => {
|
|
397
|
+
const outcome = tx.result?.outcome;
|
|
398
|
+
|
|
399
|
+
if (outcome === 'error' || outcome === 'invalid') return 'fault';
|
|
400
|
+
|
|
401
|
+
return outcome === 'redirected' ? 'redirect' : 'ok';
|
|
402
|
+
};
|
|
403
|
+
|
|
404
|
+
interface Wanted {
|
|
405
|
+
from: string;
|
|
406
|
+
to: string;
|
|
407
|
+
style: EdgeStyle;
|
|
408
|
+
count?: number;
|
|
409
|
+
running?: boolean;
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
// A selected transition shows its own edge and, for a redirect, a dashed one from its origin.
|
|
413
|
+
const txEdges = (host: DevtoolsPanel, tx: Tx): Wanted[] => {
|
|
414
|
+
const origin = host._origin(tx);
|
|
415
|
+
|
|
416
|
+
return [
|
|
417
|
+
...(origin
|
|
418
|
+
? [{ from: origin.from, to: origin.to, style: 'redirect' as const }]
|
|
419
|
+
: []),
|
|
420
|
+
{ from: tx.from, to: tx.to, style: styleOf(tx), running: !tx.result },
|
|
421
|
+
];
|
|
422
|
+
};
|
|
423
|
+
|
|
424
|
+
// A selected state shows the traffic it has seen, one edge per pair of states.
|
|
425
|
+
const stateEdges = (host: DevtoolsPanel, name: string): Wanted[] =>
|
|
426
|
+
edgesOf(host._model).flatMap((edge) => {
|
|
427
|
+
const count = edge.outcomes.success ?? 0;
|
|
428
|
+
|
|
429
|
+
return count && (edge.from === name || edge.to === name)
|
|
430
|
+
? [{ from: edge.from, to: edge.to, style: 'ok' as const, count }]
|
|
431
|
+
: [];
|
|
432
|
+
});
|
|
433
|
+
|
|
434
|
+
const wantedEdges = (host: DevtoolsPanel): Wanted[] => {
|
|
435
|
+
const tx = host._inspect === 'tx' ? host._tx(host._selectedTx) : undefined;
|
|
436
|
+
|
|
437
|
+
if (tx) return txEdges(host, tx);
|
|
438
|
+
|
|
439
|
+
return host._inspect === 'state' && host._selectedState
|
|
440
|
+
? stateEdges(host, host._selectedState)
|
|
441
|
+
: [];
|
|
442
|
+
};
|
|
443
|
+
|
|
444
|
+
// Ends hidden in a collapsed box move to that box, and edges that now share a pair merge.
|
|
445
|
+
const mapEdges = (
|
|
446
|
+
host: DevtoolsPanel,
|
|
447
|
+
drawn: ReadonlySet<string>,
|
|
448
|
+
): MapEdge[] => {
|
|
449
|
+
const parents = new Map(host._model.states.map((s) => [s.name, s.parent]));
|
|
450
|
+
|
|
451
|
+
const end = (name: string) =>
|
|
452
|
+
visibleEnd(
|
|
453
|
+
name,
|
|
454
|
+
(at) => parents.get(at),
|
|
455
|
+
(at) => drawn.has(at),
|
|
456
|
+
);
|
|
457
|
+
|
|
458
|
+
const edges = new Map<string, MapEdge>();
|
|
459
|
+
|
|
460
|
+
for (const wanted of wantedEdges(host)) {
|
|
461
|
+
const from = end(wanted.from);
|
|
462
|
+
const to = end(wanted.to);
|
|
463
|
+
|
|
464
|
+
if (!from || !to) continue;
|
|
465
|
+
|
|
466
|
+
const id = JSON.stringify([from.id, to.id, wanted.style]);
|
|
467
|
+
const edge = edges.get(id);
|
|
468
|
+
|
|
469
|
+
edges.set(id, {
|
|
470
|
+
id,
|
|
471
|
+
from: from.id,
|
|
472
|
+
to: to.id,
|
|
473
|
+
style: wanted.style,
|
|
474
|
+
count: (edge?.count ?? 0) + (wanted.count ?? 0),
|
|
475
|
+
back: 0,
|
|
476
|
+
running: wanted.running,
|
|
477
|
+
fromStub: edge?.fromStub || from.stub,
|
|
478
|
+
toStub: edge?.toStub || to.stub,
|
|
479
|
+
});
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
const nested = (a: string, b: string) =>
|
|
483
|
+
ancestorsOf(host._model.states, a).includes(b) ||
|
|
484
|
+
ancestorsOf(host._model.states, b).includes(a);
|
|
485
|
+
|
|
486
|
+
return twoWay([...edges.values()], nested).map((edge) => ({
|
|
487
|
+
...edge,
|
|
488
|
+
width: edge.count ? edgeWeight(Math.max(edge.count, edge.back)) : 2,
|
|
489
|
+
}));
|
|
490
|
+
};
|
|
491
|
+
|
|
492
|
+
// Opposite traffic between one pair shares a line with a head at each end, so a box has one port per neighbour.
|
|
493
|
+
// A compound and a box inside it keep an arrow each way: those are stubs, too short for two heads.
|
|
494
|
+
const twoWay = (
|
|
495
|
+
edges: readonly MapEdge[],
|
|
496
|
+
nested: (a: string, b: string) => boolean,
|
|
497
|
+
): MapEdge[] => {
|
|
498
|
+
const byId = new Map(edges.map((edge) => [edge.id, edge]));
|
|
499
|
+
|
|
500
|
+
return edges.flatMap((edge) => {
|
|
501
|
+
const reverse = byId.get(JSON.stringify([edge.to, edge.from, edge.style]));
|
|
502
|
+
|
|
503
|
+
if (!reverse || reverse === edge || nested(edge.from, edge.to))
|
|
504
|
+
return [edge];
|
|
505
|
+
|
|
506
|
+
// The pair is drawn once, from the end that sorts first.
|
|
507
|
+
return edge.from > edge.to
|
|
508
|
+
? []
|
|
509
|
+
: [
|
|
510
|
+
{
|
|
511
|
+
...edge,
|
|
512
|
+
both: true,
|
|
513
|
+
back: reverse.count,
|
|
514
|
+
fromStub: edge.fromStub || reverse.toStub,
|
|
515
|
+
toStub: edge.toStub || reverse.fromStub,
|
|
516
|
+
},
|
|
517
|
+
];
|
|
518
|
+
});
|
|
519
|
+
};
|
|
520
|
+
|
|
521
|
+
type Drawing = Routed & { edge: MapEdge };
|
|
522
|
+
|
|
523
|
+
interface ChipPart {
|
|
524
|
+
text: string;
|
|
525
|
+
/** The way this count's traffic travels; a lone count has no arrow. */
|
|
526
|
+
dir?: number;
|
|
527
|
+
/** Where the part starts inside the chip. */
|
|
528
|
+
x: number;
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
interface CountChip extends Chip {
|
|
532
|
+
parts: ChipPart[];
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
interface Routes {
|
|
536
|
+
routed: Drawing[];
|
|
537
|
+
chips: CountChip[];
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
const routes = new WeakMap<
|
|
541
|
+
DevtoolsPanel,
|
|
542
|
+
Routes & { key: string; layout: NestedLayout }
|
|
543
|
+
>();
|
|
544
|
+
|
|
545
|
+
const CHIP = { height: 12, pad: 3, arrow: 6, gap: 4 };
|
|
546
|
+
|
|
547
|
+
const CHIP_FONT = '500 11px';
|
|
548
|
+
|
|
549
|
+
// A chip says how often a line was travelled, when more than once: one count, or one each way with its arrow.
|
|
550
|
+
const chipCounts = (
|
|
551
|
+
{ count, back, both }: MapEdge,
|
|
552
|
+
dir: number,
|
|
553
|
+
): Omit<ChipPart, 'x'>[] => {
|
|
554
|
+
if (Math.max(count, back) < 2) return [];
|
|
555
|
+
|
|
556
|
+
if (!both) return [{ text: `×${count}` }];
|
|
557
|
+
|
|
558
|
+
const forward = { text: String(count), dir };
|
|
559
|
+
const against = { text: String(back), dir: (dir + 2) % 4 };
|
|
560
|
+
|
|
561
|
+
// The count going west or north comes first.
|
|
562
|
+
return dir >= 2 ? [forward, against] : [against, forward];
|
|
563
|
+
};
|
|
564
|
+
|
|
565
|
+
interface ChipText {
|
|
566
|
+
parts: ChipPart[];
|
|
567
|
+
width: number;
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
const chipParts = (edge: MapEdge, dir: number, family: string): ChipText => {
|
|
571
|
+
let x = CHIP.pad - CHIP.gap;
|
|
572
|
+
|
|
573
|
+
const parts = chipCounts(edge, dir).map((part) => {
|
|
574
|
+
const start = x + CHIP.gap;
|
|
575
|
+
|
|
576
|
+
x =
|
|
577
|
+
start +
|
|
578
|
+
(part.dir === undefined ? 0 : CHIP.arrow) +
|
|
579
|
+
textWidth(part.text, `${CHIP_FONT} ${family}`);
|
|
580
|
+
|
|
581
|
+
return { ...part, x: start };
|
|
582
|
+
});
|
|
583
|
+
|
|
584
|
+
return { parts, width: Math.ceil(x + CHIP.pad) };
|
|
585
|
+
};
|
|
586
|
+
|
|
587
|
+
const countChips = (
|
|
588
|
+
routed: readonly Drawing[],
|
|
589
|
+
boxes: readonly RouteBox[],
|
|
590
|
+
family: string,
|
|
591
|
+
): CountChip[] => {
|
|
592
|
+
const byId = new Map(routed.map((r) => [r.id, r.edge]));
|
|
593
|
+
|
|
594
|
+
const wants = routed.flatMap(({ id, edge }) => {
|
|
595
|
+
const { parts, width } = chipParts(edge, 0, family);
|
|
596
|
+
|
|
597
|
+
return parts.length ? [{ id, width, height: CHIP.height }] : [];
|
|
598
|
+
});
|
|
599
|
+
|
|
600
|
+
return placeChips(routed, boxes, wants).map((chip) => ({
|
|
601
|
+
...chip,
|
|
602
|
+
parts: chipParts(byId.get(chip.id)!, chip.dir, family).parts,
|
|
603
|
+
}));
|
|
604
|
+
};
|
|
605
|
+
|
|
606
|
+
const routeEdges = (host: DevtoolsPanel, layout: NestedLayout): Routes => {
|
|
607
|
+
const { rects } = layout;
|
|
608
|
+
const edges = mapEdges(host, new Set(rects.keys()));
|
|
609
|
+
const key = JSON.stringify(edges);
|
|
610
|
+
const cached = routes.get(host);
|
|
611
|
+
|
|
612
|
+
if (cached?.key === key && cached.layout === layout) return cached;
|
|
613
|
+
|
|
614
|
+
const parents = new Map(host._model.states.map((s) => [s.name, s.parent]));
|
|
615
|
+
|
|
616
|
+
const boxes: RouteBox[] = [...rects].map(([id, rect]) => ({
|
|
617
|
+
id,
|
|
618
|
+
parent: parents.get(id) ?? null,
|
|
619
|
+
rect,
|
|
620
|
+
head: Math.min(layout.metrics.head, rect.height),
|
|
621
|
+
}));
|
|
622
|
+
|
|
623
|
+
const byId = new Map(edges.map((edge) => [edge.id, edge]));
|
|
624
|
+
|
|
625
|
+
const routed = route(boxes, edges, {
|
|
626
|
+
dpr: globalThis.devicePixelRatio || 1,
|
|
627
|
+
}).map((r) => ({ ...r, edge: byId.get(r.id)! }));
|
|
628
|
+
|
|
629
|
+
const drawn = {
|
|
630
|
+
routed,
|
|
631
|
+
chips: countChips(routed, boxes, layout.family),
|
|
632
|
+
};
|
|
633
|
+
|
|
634
|
+
routes.set(host, { key, layout, ...drawn });
|
|
635
|
+
|
|
636
|
+
return drawn;
|
|
637
|
+
};
|
|
638
|
+
|
|
639
|
+
interface Shown extends Routes {
|
|
640
|
+
layout: NestedLayout;
|
|
641
|
+
/** Edges no longer wanted, kept until they have withdrawn. */
|
|
642
|
+
leaving: (Drawing & { until: number })[];
|
|
643
|
+
/** Counts re-packs, so an edge that survives one is redrawn once its boxes have settled. */
|
|
644
|
+
pass: number;
|
|
645
|
+
timer?: ReturnType<typeof setTimeout>;
|
|
646
|
+
}
|
|
647
|
+
|
|
648
|
+
const shown = new WeakMap<DevtoolsPanel, Shown>();
|
|
649
|
+
|
|
650
|
+
interface EdgesToDraw extends Routes {
|
|
651
|
+
leaving: Drawing[];
|
|
652
|
+
/** The boxes were re-packed since the last render. */
|
|
653
|
+
moved: boolean;
|
|
654
|
+
pass: number;
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
// An edge that is no longer wanted withdraws before it goes; a re-pack drops it at once, since its boxes have moved.
|
|
658
|
+
const edgesToDraw = (
|
|
659
|
+
host: DevtoolsPanel,
|
|
660
|
+
layout: NestedLayout,
|
|
661
|
+
): EdgesToDraw => {
|
|
662
|
+
const { routed, chips } = routeEdges(host, layout);
|
|
663
|
+
const before = shown.get(host);
|
|
664
|
+
const moved = before !== undefined && before.layout !== layout;
|
|
665
|
+
const now = performance.now();
|
|
666
|
+
const ids = new Set(routed.map((r) => r.id));
|
|
667
|
+
|
|
668
|
+
const leaving =
|
|
669
|
+
!before || moved
|
|
670
|
+
? []
|
|
671
|
+
: [
|
|
672
|
+
...before.leaving.filter((r) => r.until > now),
|
|
673
|
+
...before.routed
|
|
674
|
+
.filter((r) => !before.leaving.some((gone) => gone.id === r.id))
|
|
675
|
+
.map((r) => ({ ...r, until: now + WITHDRAW })),
|
|
676
|
+
].filter((r) => !ids.has(r.id));
|
|
677
|
+
|
|
678
|
+
const pass = (before?.pass ?? 0) + (moved ? 1 : 0);
|
|
679
|
+
|
|
680
|
+
clearTimeout(before?.timer);
|
|
681
|
+
|
|
682
|
+
shown.set(host, {
|
|
683
|
+
layout,
|
|
684
|
+
routed,
|
|
685
|
+
chips,
|
|
686
|
+
leaving,
|
|
687
|
+
pass,
|
|
688
|
+
timer: leaving.length
|
|
689
|
+
? setTimeout(() => host.requestUpdate(), WITHDRAW)
|
|
690
|
+
: undefined,
|
|
691
|
+
});
|
|
692
|
+
|
|
693
|
+
return { routed, chips, leaving, moved, pass };
|
|
694
|
+
};
|
|
695
|
+
|
|
696
|
+
/** The drawn box that stands for the state the inspector popover sits beside. */
|
|
697
|
+
export const anchorRect = (
|
|
698
|
+
host: DevtoolsPanel,
|
|
699
|
+
layout: NestedLayout,
|
|
700
|
+
): Rect | undefined => {
|
|
701
|
+
const parents = new Map(host._model.states.map((s) => [s.name, s.parent]));
|
|
702
|
+
const name = host._anchorState;
|
|
703
|
+
|
|
704
|
+
const end =
|
|
705
|
+
name === null
|
|
706
|
+
? undefined
|
|
707
|
+
: visibleEnd(
|
|
708
|
+
name,
|
|
709
|
+
(at) => parents.get(at),
|
|
710
|
+
(at) => layout.rects.has(at),
|
|
711
|
+
);
|
|
712
|
+
|
|
713
|
+
return end && layout.rects.get(end.id);
|
|
714
|
+
};
|
|
715
|
+
|
|
716
|
+
/** What the inspector popover should keep clear of: first the drawn edges and the boxes they end at, then every pill. */
|
|
717
|
+
export const keepClear = (
|
|
718
|
+
host: DevtoolsPanel,
|
|
719
|
+
layout: NestedLayout,
|
|
720
|
+
): Avoid[] => {
|
|
721
|
+
const pad = 6;
|
|
722
|
+
|
|
723
|
+
const drawn = routeEdges(host, layout).routed.flatMap(({ edge, points }) => [
|
|
724
|
+
...[edge.from, edge.to].flatMap((id) => layout.rects.get(id) ?? []),
|
|
725
|
+
...points.slice(1).map((point, index) => {
|
|
726
|
+
const prev = points[index];
|
|
727
|
+
|
|
728
|
+
return {
|
|
729
|
+
x: Math.min(prev.x, point.x) - pad,
|
|
730
|
+
y: Math.min(prev.y, point.y) - pad,
|
|
731
|
+
width: Math.abs(point.x - prev.x) + pad * 2,
|
|
732
|
+
height: Math.abs(point.y - prev.y) + pad * 2,
|
|
733
|
+
};
|
|
734
|
+
}),
|
|
735
|
+
]);
|
|
736
|
+
|
|
737
|
+
const pills = [...layout.rects].flatMap(([name, rect]) =>
|
|
738
|
+
layout.open.has(name) ? [] : [rect],
|
|
739
|
+
);
|
|
740
|
+
|
|
741
|
+
return [
|
|
742
|
+
...drawn.map((rect) => ({ ...rect, weight: 20 })),
|
|
743
|
+
...pills.map((rect) => ({ ...rect, weight: 1 })),
|
|
744
|
+
];
|
|
745
|
+
};
|
|
746
|
+
|
|
747
|
+
// The mask draws an edge in from its start and withdraws it toward its end; the path runs to the arrowhead's tip.
|
|
748
|
+
const renderEdge = (
|
|
749
|
+
{ edge, kind, d, points, head, tail, caps }: Drawing,
|
|
750
|
+
index: number,
|
|
751
|
+
leaving: boolean,
|
|
752
|
+
frame: Size,
|
|
753
|
+
) => {
|
|
754
|
+
const id = `uird-edge-${leaving ? 'out' : 'in'}-${index}`;
|
|
755
|
+
const reveal = `M${points.map((point) => `${point.x} ${point.y}`).join('L')}`;
|
|
756
|
+
|
|
757
|
+
const tip =
|
|
758
|
+
edge.style === 'redirect' ? 'M-6.5 -3.5L0 0L-6.5 3.5' : 'M0 0L-7 -3.5V3.5z';
|
|
759
|
+
|
|
760
|
+
return svg`<g class="nedge edge-${edge.style} w${edge.width ?? 1} ${kind === 'fallback' ? 'fallback' : ''} ${edge.running ? 'running' : ''} ${leaving ? 'leaving' : ''}">
|
|
761
|
+
<mask id=${id} maskUnits="userSpaceOnUse" x="-24" y="-24" width=${frame.width + 48} height=${frame.height + 48}>
|
|
762
|
+
<path class="reveal" pathLength="1" d=${reveal} />
|
|
763
|
+
</mask>
|
|
764
|
+
<g mask="url(#${id})">
|
|
765
|
+
<path class="halo" d=${d} /><path class="line" d=${d} />
|
|
766
|
+
${[head, ...(tail ? [tail] : [])].map(
|
|
767
|
+
(at) =>
|
|
768
|
+
svg`<path class="head" transform="translate(${at.x} ${at.y}) rotate(${at.angle})" d=${tip} />`,
|
|
769
|
+
)}
|
|
770
|
+
${caps.map(
|
|
771
|
+
(cap) =>
|
|
772
|
+
svg`<path class="cap" d=${cap.upright ? `M${cap.x} ${cap.y - 4}v8` : `M${cap.x - 4} ${cap.y}h8`} />`,
|
|
773
|
+
)}
|
|
774
|
+
</g>
|
|
775
|
+
</g>`;
|
|
776
|
+
};
|
|
777
|
+
|
|
778
|
+
const renderChip = ({ x, y, width, height, parts }: CountChip) =>
|
|
779
|
+
svg`<g class="chip" transform="translate(${x} ${y})">
|
|
780
|
+
<rect width=${width} height=${height} rx="2" />
|
|
781
|
+
${parts.map(
|
|
782
|
+
(part) =>
|
|
783
|
+
svg`${
|
|
784
|
+
part.dir === undefined
|
|
785
|
+
? nothing
|
|
786
|
+
: svg`<path transform="translate(${part.x + 2.5} ${height / 2}) rotate(${part.dir * 90})" d="M-2 -3L2 0L-2 3z" />`
|
|
787
|
+
}<text x=${part.x + (part.dir === undefined ? 0 : CHIP.arrow)} y=${height / 2}>${part.text}</text>`,
|
|
788
|
+
)}
|
|
789
|
+
</g>`;
|
|
790
|
+
|
|
791
|
+
// Boxes
|
|
792
|
+
|
|
793
|
+
interface View {
|
|
794
|
+
host: DevtoolsPanel;
|
|
795
|
+
layout: NestedLayout;
|
|
796
|
+
states: ReadonlyMap<string, StateInfo>;
|
|
797
|
+
build: Build;
|
|
798
|
+
/** A number per state, for the ids that tie a header to its group. */
|
|
799
|
+
ids: ReadonlyMap<string, number>;
|
|
800
|
+
/** Each state's depth in the declared tree, which its identity colour follows. */
|
|
801
|
+
depths: ReadonlyMap<string, number>;
|
|
802
|
+
focus: string | undefined;
|
|
803
|
+
pivot: string | undefined;
|
|
804
|
+
/** Drawn boxes tied to the view selected from the path under the Map, with how. */
|
|
805
|
+
linked: ReadonlyMap<string, string>;
|
|
806
|
+
}
|
|
807
|
+
|
|
808
|
+
const initialMark = svg`<svg class="initial" viewBox="0 0 20 12" width="20" height="12" aria-hidden="true">
|
|
809
|
+
<circle cx="4" cy="6" r="3" /><path d="M4 6H13" /><path class="tip" d="M20 6l-8 -4v8z" />
|
|
810
|
+
</svg>`;
|
|
811
|
+
|
|
812
|
+
type Kind = 'open' | 'collapsed' | 'leaf';
|
|
813
|
+
|
|
814
|
+
const renderMarks = (marks: readonly StateMark[]) =>
|
|
815
|
+
marks.length
|
|
816
|
+
? html`<span class="marks" aria-hidden="true"
|
|
817
|
+
>${marks.map(
|
|
818
|
+
(mark) =>
|
|
819
|
+
html`<span class="mark" title=${markLabel(mark)}
|
|
820
|
+
>${markGlyph(mark.mark)}</span
|
|
821
|
+
>`,
|
|
822
|
+
)}</span
|
|
823
|
+
>`
|
|
824
|
+
: nothing;
|
|
825
|
+
|
|
826
|
+
// The pointer's way to close or open a compound; the arrow keys do the same on the treeitem.
|
|
827
|
+
const renderFold = (
|
|
828
|
+
view: View,
|
|
829
|
+
box: Box,
|
|
830
|
+
kind: Kind,
|
|
831
|
+
chain: string | undefined,
|
|
832
|
+
) => {
|
|
833
|
+
// A merged header has one button, at its end, and it closes the box the header belongs to.
|
|
834
|
+
if (kind === 'leaf' || box.merge) return nothing;
|
|
835
|
+
|
|
836
|
+
const open = kind === 'open';
|
|
837
|
+
const target = chain ?? box.name;
|
|
838
|
+
|
|
839
|
+
return html`<button
|
|
840
|
+
class="fold"
|
|
841
|
+
tabindex="-1"
|
|
842
|
+
aria-label="${open ? 'Collapse' : 'Expand'} ${segment(target)}"
|
|
843
|
+
title=${open ? 'Collapse' : 'Expand'}
|
|
844
|
+
@click=${(event: MouseEvent) => {
|
|
845
|
+
// A fold that a re-pack put under the second click of a double-click on a box is not what was pressed.
|
|
846
|
+
if (event.detail > 1 && view.host._pressed) return;
|
|
847
|
+
|
|
848
|
+
event.stopPropagation();
|
|
849
|
+
view.host._pressed = null;
|
|
850
|
+
nestedKeys(view.host, view.layout).toggle(target, open);
|
|
851
|
+
}}
|
|
852
|
+
>
|
|
853
|
+
${foldGlyph(open)}
|
|
854
|
+
</button>`;
|
|
855
|
+
};
|
|
856
|
+
|
|
857
|
+
// The header is the treeitem and owns the group beside it. A compound is labelled, since a name from content would take in the owned group.
|
|
858
|
+
const renderHead = (
|
|
859
|
+
view: View,
|
|
860
|
+
box: Box,
|
|
861
|
+
state: StateInfo,
|
|
862
|
+
kind: Kind,
|
|
863
|
+
chain: string | undefined,
|
|
864
|
+
) => {
|
|
865
|
+
const { host, layout, build } = view;
|
|
866
|
+
const role = host._role(state);
|
|
867
|
+
const isPivot = role === 'retained' && state.name === view.pivot;
|
|
868
|
+
const marks = stateMarks(state, host._model.states);
|
|
869
|
+
const name = nameOf(state, layout.options);
|
|
870
|
+
const full = segment(state.name);
|
|
871
|
+
const current = state.name === host._model.current.name;
|
|
872
|
+
const id = view.ids.get(box.name) ?? 0;
|
|
873
|
+
|
|
874
|
+
const described = words(state, role, isPivot, marks);
|
|
875
|
+
const hidden = countUnder(build, state.name);
|
|
876
|
+
|
|
877
|
+
// A leaf is named by its own text; a label repeats the header's visible text, so the two never disagree.
|
|
878
|
+
const labelled = kind === 'collapsed' ? `${full} ${hidden}` : full;
|
|
879
|
+
const unlabelled = kind === 'leaf' && name === full;
|
|
880
|
+
|
|
881
|
+
const tie = view.linked.get(box.name);
|
|
882
|
+
|
|
883
|
+
const description = [
|
|
884
|
+
described,
|
|
885
|
+
...(kind === 'collapsed' ? [`${hidden} states inside`] : []),
|
|
886
|
+
...(tie ? [tie] : []),
|
|
887
|
+
].join(', ');
|
|
888
|
+
|
|
889
|
+
/* oxlint-disable lit-a11y/role-supports-aria-attr -- aria-description is a global attribute in ARIA 1.3 */
|
|
890
|
+
return html`<div
|
|
891
|
+
class="head"
|
|
892
|
+
role="treeitem"
|
|
893
|
+
title=${state.name}
|
|
894
|
+
tabindex=${box.name === view.focus ? 0 : -1}
|
|
895
|
+
aria-label=${unlabelled ? nothing : labelled}
|
|
896
|
+
aria-owns=${kind === 'open' ? `kids-${id}` : nothing}
|
|
897
|
+
aria-expanded=${kind === 'leaf' ? nothing : kind === 'open' ? 'true' : 'false'}
|
|
898
|
+
aria-current=${current ? 'true' : nothing}
|
|
899
|
+
aria-selected=${host._selectedState === box.name}
|
|
900
|
+
aria-description=${description}
|
|
901
|
+
>
|
|
902
|
+
${label(state, role, isPivot, [], name, false)}${
|
|
903
|
+
kind === 'collapsed'
|
|
904
|
+
? // The space keeps the visible text the same as the label, "name count".
|
|
905
|
+
html` <span class="count num">${hidden}</span>`
|
|
906
|
+
: nothing
|
|
907
|
+
}${renderMarks(marks)}${renderFold(view, box, kind, chain)}
|
|
908
|
+
</div>`;
|
|
909
|
+
/* oxlint-enable lit-a11y/role-supports-aria-attr */
|
|
910
|
+
};
|
|
911
|
+
|
|
912
|
+
const boxClasses = (view: View, item: Drawn, kind: Kind): string => {
|
|
913
|
+
const { box, placed } = item;
|
|
914
|
+
const state = view.states.get(box.name)!;
|
|
915
|
+
|
|
916
|
+
return [
|
|
917
|
+
`role-${view.host._role(state)}`,
|
|
918
|
+
depthClass(view.depths.get(box.name) ?? 1),
|
|
919
|
+
facts(state, view.host._model.current.name),
|
|
920
|
+
kind,
|
|
921
|
+
view.host._selectedState === box.name && 'selected',
|
|
922
|
+
view.linked.has(box.name) && 'linked',
|
|
923
|
+
box.merge && 'merging',
|
|
924
|
+
placed.indent > 0 && 'merged',
|
|
925
|
+
box.lead && 'led',
|
|
926
|
+
]
|
|
927
|
+
.filter(Boolean)
|
|
928
|
+
.join(' ');
|
|
929
|
+
};
|
|
930
|
+
|
|
931
|
+
const kindOf = (view: View, box: Box): Kind => {
|
|
932
|
+
if (view.layout.open.has(box.name)) return 'open';
|
|
933
|
+
|
|
934
|
+
return (view.build.children.get(box.name) ?? []).length
|
|
935
|
+
? 'collapsed'
|
|
936
|
+
: 'leaf';
|
|
937
|
+
};
|
|
938
|
+
|
|
939
|
+
// Boxes are keyed by state, so a re-pack moves an element instead of replacing it.
|
|
940
|
+
const renderBoxes = (view: View, placed: Placed, chain?: string) =>
|
|
941
|
+
repeat(
|
|
942
|
+
drawnIn(placed, view.layout.metrics),
|
|
943
|
+
(item) => item.box.name,
|
|
944
|
+
(item) => renderBox(view, item, chain),
|
|
945
|
+
);
|
|
946
|
+
|
|
947
|
+
// `chain` names the outermost box of a merged header; the one fold button at its end closes that box.
|
|
948
|
+
const renderBox = (view: View, item: Drawn, chain?: string): TemplateResult => {
|
|
949
|
+
const { box, placed } = item;
|
|
950
|
+
const state = view.states.get(box.name)!;
|
|
951
|
+
const kind = kindOf(view, box);
|
|
952
|
+
|
|
953
|
+
return html`<div
|
|
954
|
+
role="none"
|
|
955
|
+
class="box ${boxClasses(view, item, kind)}"
|
|
956
|
+
data-name=${box.name}
|
|
957
|
+
style="inset-inline-start:${item.x}px;inset-block-start:${item.y}px;inline-size:${item.width}px;block-size:${item.height}px;--indent:${placed.indent}px"
|
|
958
|
+
>
|
|
959
|
+
${box.lead ? initialMark : nothing}${renderHead(view, box, state, kind, chain)}${
|
|
960
|
+
kind === 'open'
|
|
961
|
+
? html`<div
|
|
962
|
+
class="kids"
|
|
963
|
+
role="group"
|
|
964
|
+
id="kids-${view.ids.get(box.name) ?? 0}"
|
|
965
|
+
>
|
|
966
|
+
${renderBoxes(view, placed, box.merge ? (chain ?? box.name) : undefined)}
|
|
967
|
+
</div>`
|
|
968
|
+
: nothing
|
|
969
|
+
}
|
|
970
|
+
</div>`;
|
|
971
|
+
};
|
|
972
|
+
|
|
973
|
+
const boxName = (event: Event): string | undefined =>
|
|
974
|
+
// SAFETY: the listeners sit on the tree, whose event targets are its own elements
|
|
975
|
+
(event.target as Element).closest<HTMLElement>('.box')?.dataset.name;
|
|
976
|
+
|
|
977
|
+
const nestedKeys = (host: DevtoolsPanel, layout: NestedLayout) => ({
|
|
978
|
+
expanded: (row: Row) => layout.open.has(row.state.name),
|
|
979
|
+
toggle: (name: string, collapse: boolean) => {
|
|
980
|
+
host._mapOpen = new Map([...host._mapOpen, [name, !collapse]]);
|
|
981
|
+
},
|
|
982
|
+
});
|
|
983
|
+
|
|
984
|
+
// A tied state hidden in a collapsed compound is marked on the box that holds it.
|
|
985
|
+
const linkedBoxes = (
|
|
986
|
+
host: DevtoolsPanel,
|
|
987
|
+
layout: NestedLayout,
|
|
988
|
+
): Map<string, string> => {
|
|
989
|
+
const linked = new Map<string, string>();
|
|
990
|
+
|
|
991
|
+
for (const [name, tie] of host._linkedStates()) {
|
|
992
|
+
const drawn = [name, ...ancestorsOf(host._model.states, name)].filter(
|
|
993
|
+
(each) => layout.rects.has(each),
|
|
994
|
+
);
|
|
995
|
+
|
|
996
|
+
for (const each of drawn.slice(0, 1)) linked.set(each, tie);
|
|
997
|
+
}
|
|
998
|
+
|
|
999
|
+
return linked;
|
|
1000
|
+
};
|
|
1001
|
+
|
|
1002
|
+
/** The Map as statechart containment: a box per state, child states inside their parent. */
|
|
1003
|
+
export const renderNested = (
|
|
1004
|
+
host: DevtoolsPanel,
|
|
1005
|
+
layout: NestedLayout,
|
|
1006
|
+
): TemplateResult => {
|
|
1007
|
+
const { states, current } = host._model;
|
|
1008
|
+
const { rows, placed, options } = layout;
|
|
1009
|
+
const drawn = new Set(rows.map((row) => row.state.name));
|
|
1010
|
+
const { routed, chips, leaving, moved, pass } = edgesToDraw(host, layout);
|
|
1011
|
+
const frame = { width: placed.width, height: placed.height };
|
|
1012
|
+
|
|
1013
|
+
const top: Drawn[] = placed.box
|
|
1014
|
+
? [{ box: placed.box, placed, x: 0, y: 0, ...frame }]
|
|
1015
|
+
: [];
|
|
1016
|
+
|
|
1017
|
+
const focus =
|
|
1018
|
+
host._focusState && drawn.has(host._focusState)
|
|
1019
|
+
? host._focusState
|
|
1020
|
+
: (rows.find((r) => r.state.name === current.name) ?? rows[0])?.state
|
|
1021
|
+
.name;
|
|
1022
|
+
|
|
1023
|
+
const view: View = {
|
|
1024
|
+
host,
|
|
1025
|
+
layout,
|
|
1026
|
+
states: new Map(states.map((state) => [state.name, state])),
|
|
1027
|
+
ids: new Map(states.map((state, index) => [state.name, index])),
|
|
1028
|
+
build: {
|
|
1029
|
+
host,
|
|
1030
|
+
children: host._children(),
|
|
1031
|
+
keep: new Set(),
|
|
1032
|
+
options,
|
|
1033
|
+
family: '',
|
|
1034
|
+
},
|
|
1035
|
+
depths: new Map(rows.map((row) => [row.state.name, row.depth])),
|
|
1036
|
+
focus,
|
|
1037
|
+
pivot: host._focusTx?.retained.at(-1),
|
|
1038
|
+
linked: linkedBoxes(host, layout),
|
|
1039
|
+
};
|
|
1040
|
+
|
|
1041
|
+
const onKey = (event: KeyboardEvent) => {
|
|
1042
|
+
if (event.key === 'Escape' && host._inspect !== 'none') {
|
|
1043
|
+
host._clearSelection();
|
|
1044
|
+
event.preventDefault();
|
|
1045
|
+
|
|
1046
|
+
return;
|
|
1047
|
+
}
|
|
1048
|
+
|
|
1049
|
+
if (viewKey(host, layout, event)) {
|
|
1050
|
+
event.preventDefault();
|
|
1051
|
+
|
|
1052
|
+
return;
|
|
1053
|
+
}
|
|
1054
|
+
|
|
1055
|
+
onTreeKey(host, event, rows, focus, nestedKeys(host, layout));
|
|
1056
|
+
};
|
|
1057
|
+
|
|
1058
|
+
// The first click can re-pack the diagram, so a double-click keeps the box its first click was on.
|
|
1059
|
+
const select = (event: MouseEvent) => {
|
|
1060
|
+
if (event.detail > 1) {
|
|
1061
|
+
if (host._pressed) host._selectState(host._pressed, true);
|
|
1062
|
+
|
|
1063
|
+
return;
|
|
1064
|
+
}
|
|
1065
|
+
|
|
1066
|
+
host._pressed = boxName(event) ?? null;
|
|
1067
|
+
|
|
1068
|
+
if (host._pressed) host._selectState(host._pressed);
|
|
1069
|
+
};
|
|
1070
|
+
|
|
1071
|
+
const go = () => {
|
|
1072
|
+
if (host._pressed) host._goState(host._pressed);
|
|
1073
|
+
};
|
|
1074
|
+
|
|
1075
|
+
const focusIn = (event: Event) => {
|
|
1076
|
+
const name = boxName(event);
|
|
1077
|
+
|
|
1078
|
+
if (!name) return;
|
|
1079
|
+
host._focusState = name;
|
|
1080
|
+
|
|
1081
|
+
// Focus that arrives by keyboard pans its box into view; a click never moves what is under the pointer.
|
|
1082
|
+
// SAFETY: the listener sits on the tree, whose event targets are its own elements
|
|
1083
|
+
if ((event.target as Element).matches(':focus-visible'))
|
|
1084
|
+
host._reveal(layout, name);
|
|
1085
|
+
};
|
|
1086
|
+
|
|
1087
|
+
const room = host._room(layout);
|
|
1088
|
+
const at = host._viewOf(layout);
|
|
1089
|
+
// The tree is laid out at the room's inline start, so the view is taken from there.
|
|
1090
|
+
const from = layout.rtl ? room.width - placed.width : 0;
|
|
1091
|
+
|
|
1092
|
+
/* oxlint-disable lit-a11y/click-events-have-key-events -- the tree's keydown handler moves focus and selects for every treeitem */
|
|
1093
|
+
return html`<div
|
|
1094
|
+
class="nested ${STEP.get(options.metrics) ?? ''}"
|
|
1095
|
+
role="tree"
|
|
1096
|
+
aria-label="State map"
|
|
1097
|
+
style="inline-size:${placed.width}px;block-size:${placed.height}px;translate:${at.x - from}px ${at.y}px"
|
|
1098
|
+
@keydown=${onKey}
|
|
1099
|
+
@click=${select}
|
|
1100
|
+
@dblclick=${go}
|
|
1101
|
+
@focusin=${focusIn}
|
|
1102
|
+
>
|
|
1103
|
+
${top.length ? renderBox(view, top[0]) : renderBoxes(view, placed)}
|
|
1104
|
+
<svg
|
|
1105
|
+
class="nedges"
|
|
1106
|
+
aria-hidden="true"
|
|
1107
|
+
style="--uird-edge-delay:${moved ? SETTLE : 0}ms"
|
|
1108
|
+
>
|
|
1109
|
+
${repeat(
|
|
1110
|
+
routed,
|
|
1111
|
+
(r) => `${pass} ${r.id}`,
|
|
1112
|
+
(r, index) => renderEdge(r, index, false, frame),
|
|
1113
|
+
)}
|
|
1114
|
+
${leaving.map((r, index) => renderEdge(r, index, true, frame))}
|
|
1115
|
+
${repeat(
|
|
1116
|
+
chips,
|
|
1117
|
+
(chip) => `${pass} ${chip.id}`,
|
|
1118
|
+
(chip) => renderChip(chip),
|
|
1119
|
+
)}
|
|
1120
|
+
</svg>
|
|
1121
|
+
</div>
|
|
1122
|
+
${renderMinimap(host, layout, at, room)}`;
|
|
1123
|
+
/* oxlint-enable lit-a11y/click-events-have-key-events */
|
|
1124
|
+
};
|