@tinyactors/scxmljs 0.1.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 +21 -0
- package/README.md +174 -0
- package/THIRD_PARTY_NOTICES +63 -0
- package/dist/bridge.d.ts +44 -0
- package/dist/bridge.js +254 -0
- package/dist/bridge.js.map +1 -0
- package/dist/clock.d.ts +113 -0
- package/dist/clock.js +310 -0
- package/dist/clock.js.map +1 -0
- package/dist/common.d.ts +15 -0
- package/dist/common.js +14 -0
- package/dist/common.js.map +1 -0
- package/dist/datamodel-base.d.ts +122 -0
- package/dist/datamodel-base.js +268 -0
- package/dist/datamodel-base.js.map +1 -0
- package/dist/datamodel-quickjs.d.ts +21 -0
- package/dist/datamodel-quickjs.js +286 -0
- package/dist/datamodel-quickjs.js.map +1 -0
- package/dist/datamodel-trusted.d.ts +1 -0
- package/dist/datamodel-trusted.js +149 -0
- package/dist/datamodel-trusted.js.map +1 -0
- package/dist/diagnostics.d.ts +39 -0
- package/dist/diagnostics.js +154 -0
- package/dist/diagnostics.js.map +1 -0
- package/dist/element-events.d.ts +85 -0
- package/dist/element-events.js +129 -0
- package/dist/element-events.js.map +1 -0
- package/dist/events.d.ts +31 -0
- package/dist/events.js +33 -0
- package/dist/events.js.map +1 -0
- package/dist/explorer/element.d.ts +150 -0
- package/dist/explorer/element.js +1791 -0
- package/dist/explorer/element.js.map +1 -0
- package/dist/explorer/layout.d.ts +43 -0
- package/dist/explorer/layout.js +50 -0
- package/dist/explorer/layout.js.map +1 -0
- package/dist/explorer/strings.d.ts +275 -0
- package/dist/explorer/strings.js +135 -0
- package/dist/explorer/strings.js.map +1 -0
- package/dist/explorer/styles.d.ts +10 -0
- package/dist/explorer/styles.js +265 -0
- package/dist/explorer/styles.js.map +1 -0
- package/dist/explorer/viewmodel.d.ts +284 -0
- package/dist/explorer/viewmodel.js +481 -0
- package/dist/explorer/viewmodel.js.map +1 -0
- package/dist/explorer.d.ts +21 -0
- package/dist/explorer.js +22 -0
- package/dist/explorer.js.map +1 -0
- package/dist/index.d.ts +34 -0
- package/dist/index.js +39 -0
- package/dist/index.js.map +1 -0
- package/dist/model.d.ts +157 -0
- package/dist/model.js +485 -0
- package/dist/model.js.map +1 -0
- package/dist/session-events.d.ts +136 -0
- package/dist/session-events.js +114 -0
- package/dist/session-events.js.map +1 -0
- package/dist/session.d.ts +412 -0
- package/dist/session.js +1399 -0
- package/dist/session.js.map +1 -0
- package/dist/themes/tinyactors.css +44 -0
- package/dist/trusted.d.ts +15 -0
- package/dist/trusted.js +30 -0
- package/dist/trusted.js.map +1 -0
- package/dist/ui/announcer.d.ts +22 -0
- package/dist/ui/announcer.js +31 -0
- package/dist/ui/announcer.js.map +1 -0
- package/dist/ui/svg.d.ts +8 -0
- package/dist/ui/svg.js +29 -0
- package/dist/ui/svg.js.map +1 -0
- package/dist/ui/theme.d.ts +34 -0
- package/dist/ui/theme.js +151 -0
- package/dist/ui/theme.js.map +1 -0
- package/dist/view/element.d.ts +172 -0
- package/dist/view/element.js +1045 -0
- package/dist/view/element.js.map +1 -0
- package/dist/view/layout.d.ts +154 -0
- package/dist/view/layout.js +1122 -0
- package/dist/view/layout.js.map +1 -0
- package/dist/view/strings.d.ts +72 -0
- package/dist/view/strings.js +48 -0
- package/dist/view/strings.js.map +1 -0
- package/dist/view/styles.d.ts +1 -0
- package/dist/view/styles.js +115 -0
- package/dist/view/styles.js.map +1 -0
- package/dist/view.d.ts +20 -0
- package/dist/view.js +21 -0
- package/dist/view.js.map +1 -0
- package/package.json +87 -0
|
@@ -0,0 +1,1791 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* <scxml-explorer> — explore a running SCXML system at C4-style levels:
|
|
3
|
+
*
|
|
4
|
+
* System machines (root + invoked children) and external services, and who talks to whom
|
|
5
|
+
* Machine one machine: a tree of its states, a FOCUS on one compound state
|
|
6
|
+
* (its children and the transitions between them — never the whole
|
|
7
|
+
* chart), doors in/out of the focus, and the events accepted right now
|
|
8
|
+
* State detail for one state: actions, transitions, invokes, data
|
|
9
|
+
*
|
|
10
|
+
* No canvas, no pan/zoom: navigation is tree + breadcrumbs + drill-in, and the
|
|
11
|
+
* layout collapses to one pane with bottom tabs in narrow containers.
|
|
12
|
+
* Big charts: the tree is windowed, lists are grouped/filterable and cut off
|
|
13
|
+
* with "+N more", the focus switches from diagram to list when it gets crowded.
|
|
14
|
+
*
|
|
15
|
+
* Custom UI goes in through named slots (works with any framework):
|
|
16
|
+
* state:<id> in that state's card
|
|
17
|
+
* detail:<id> in that state's detail pane
|
|
18
|
+
* event:<descriptor> in the row of that accepted event (e.g. a form for its data)
|
|
19
|
+
* service:<alias> in that service's card at the System level
|
|
20
|
+
* toolbar in the header, before the Follow toggle
|
|
21
|
+
* empty-tree, empty-events, empty-detail replace the empty-state messages
|
|
22
|
+
*
|
|
23
|
+
* const x = document.querySelector("scxml-explorer");
|
|
24
|
+
* x.attach({ session, processors, clock }); // then session.start()
|
|
25
|
+
*
|
|
26
|
+
* Properties: `session`, `processors`, `clock` (setting one re-attaches),
|
|
27
|
+
* `follow`, `strings` (translations), `announce`, `announceInterval`.
|
|
28
|
+
* Attributes: `follow="false"` starts without following; `announce="off|sends|all"`.
|
|
29
|
+
*
|
|
30
|
+
* Keyboard: the tree follows the ARIA tree pattern (↑ ↓ ← → Home End, Enter,
|
|
31
|
+
* type-ahead); cards, rows, lanes and doors are focusable (Enter drills into a
|
|
32
|
+
* container or selects a state, Space selects); Space / "." play-pause and step
|
|
33
|
+
* when a PlaybackClock is attached. A polite live region announces steps and
|
|
34
|
+
* sent events.
|
|
35
|
+
* Events (bubbling, composed): `scxml-focus`, `scxml-select`, `scxml-send`
|
|
36
|
+
* (cancelable: `preventDefault()` stops the event from being sent).
|
|
37
|
+
* Styling: `--scxml-*` custom properties and `::part()` — see the README.
|
|
38
|
+
*/
|
|
39
|
+
import { PlaybackClock } from "../clock.js";
|
|
40
|
+
import { isAtomic } from "../model.js";
|
|
41
|
+
import { Announcer } from "../ui/announcer.js";
|
|
42
|
+
import { arrowMarker } from "../ui/svg.js";
|
|
43
|
+
import { lazySheet, themeSheet } from "../ui/theme.js";
|
|
44
|
+
import { placeLabels } from "./layout.js";
|
|
45
|
+
import { defaultStrings } from "./strings.js";
|
|
46
|
+
import { EXPLORER_CSS, NARROW_WIDTH } from "./styles.js";
|
|
47
|
+
import { acceptedEvents, activeExpansion, descendantCount, focusScope, followTarget, groupEvents, isContainer, label, pathTo, SystemTracker, sendableName, total, treeRows, } from "./viewmodel.js";
|
|
48
|
+
const DEFAULT_ROW = 30; // tree row height (px) unless --scxml-row-height says otherwise
|
|
49
|
+
const DIAGRAM_MAX_CHILDREN = 9;
|
|
50
|
+
const LIST_PAGE = 40;
|
|
51
|
+
const DOORS_SHOWN = 6;
|
|
52
|
+
const KIND_ICON = { atomic: "○", compound: "▣", parallel: "∥", final: "◎", history: "H", scxml: "◇" };
|
|
53
|
+
const kindOf = (s) => (s.kind === "state" ? (s.children.length ? "compound" : "atomic") : s.kind);
|
|
54
|
+
const SPEEDS = [
|
|
55
|
+
[0.25, "¼×"],
|
|
56
|
+
[0.5, "½×"],
|
|
57
|
+
[1, "1×"],
|
|
58
|
+
[2, "2×"],
|
|
59
|
+
[4, "4×"],
|
|
60
|
+
];
|
|
61
|
+
/**
|
|
62
|
+
* Server-side rendering and tests import this module where there is no DOM:
|
|
63
|
+
* extend a stand-in then, and only register the element where it can exist.
|
|
64
|
+
*/
|
|
65
|
+
const ElementBase = typeof HTMLElement === "undefined" ? class {
|
|
66
|
+
} : HTMLElement;
|
|
67
|
+
/**
|
|
68
|
+
* `<scxml-explorer>`: explore a running system at the System level (machines,
|
|
69
|
+
* services, messages), the Machine level (a tree, a focus on one compound
|
|
70
|
+
* state, the accepted events) and per state. Registered by importing
|
|
71
|
+
* `@tinyactors/scxmljs/explorer`.
|
|
72
|
+
*
|
|
73
|
+
* @example
|
|
74
|
+
* ```ts
|
|
75
|
+
* import { createSession } from "@tinyactors/scxmljs/trusted";
|
|
76
|
+
* import "@tinyactors/scxmljs/explorer";
|
|
77
|
+
*
|
|
78
|
+
* const session = await createSession(source);
|
|
79
|
+
* document.querySelector("scxml-explorer")!.attach({ session });
|
|
80
|
+
* session.start();
|
|
81
|
+
* ```
|
|
82
|
+
*/
|
|
83
|
+
export class ScxmlExplorer extends ElementBase {
|
|
84
|
+
/** Attributes the element reacts to: `follow` and `announce`. */
|
|
85
|
+
static observedAttributes = ["follow", "announce"];
|
|
86
|
+
#shadow;
|
|
87
|
+
#tracker;
|
|
88
|
+
/** the clock when it can be controlled (play / pause / step) */
|
|
89
|
+
#clock;
|
|
90
|
+
#session;
|
|
91
|
+
#processors = [];
|
|
92
|
+
#clockOption;
|
|
93
|
+
#lastFocus;
|
|
94
|
+
// navigation state
|
|
95
|
+
#level = "machine";
|
|
96
|
+
#machineKey = "";
|
|
97
|
+
#focus;
|
|
98
|
+
#selected;
|
|
99
|
+
#selectedService;
|
|
100
|
+
#follow = true;
|
|
101
|
+
#tab = "focus";
|
|
102
|
+
#inspector = "events";
|
|
103
|
+
#crumbsExpanded = false;
|
|
104
|
+
// view state
|
|
105
|
+
#expanded = new Map();
|
|
106
|
+
#treeFilter = "";
|
|
107
|
+
#activeOnly = false;
|
|
108
|
+
#focusMode = "auto";
|
|
109
|
+
#focusFilter = "";
|
|
110
|
+
#focusShowAll = false;
|
|
111
|
+
#doorsShowAll = { exits: false, entries: false };
|
|
112
|
+
#eventFilter = "";
|
|
113
|
+
#eventScope = "inherited";
|
|
114
|
+
#groupOpen = new Map();
|
|
115
|
+
#machineFilter = "";
|
|
116
|
+
#strings = defaultStrings;
|
|
117
|
+
/** layout breakpoint, measured on the host (container queries decide the CSS) */
|
|
118
|
+
#narrow = false;
|
|
119
|
+
/** the tree's keyboard cursor (roving tabindex) */
|
|
120
|
+
#treeCursor;
|
|
121
|
+
#typeahead = { text: "", at: 0 };
|
|
122
|
+
#seeded = new Set();
|
|
123
|
+
// live region
|
|
124
|
+
#announce = "all";
|
|
125
|
+
#announceInterval = 3000;
|
|
126
|
+
#announcer;
|
|
127
|
+
#announcedSteps = 0;
|
|
128
|
+
// geometry caches
|
|
129
|
+
#hostObserver;
|
|
130
|
+
#diagramObserver;
|
|
131
|
+
#systemObserver;
|
|
132
|
+
#links;
|
|
133
|
+
// live wiring
|
|
134
|
+
#unwire = [];
|
|
135
|
+
#raf = 0;
|
|
136
|
+
// persistent shell elements
|
|
137
|
+
#el;
|
|
138
|
+
constructor() {
|
|
139
|
+
super();
|
|
140
|
+
this.#shadow = this.attachShadow({ mode: "open" });
|
|
141
|
+
this.#shadow.adoptedStyleSheets = [themeSheet(), explorerSheet()];
|
|
142
|
+
this.#buildShell();
|
|
143
|
+
}
|
|
144
|
+
/** Custom-element lifecycle (called by the browser): detaches, keeping the session to re-attach on reconnect. */
|
|
145
|
+
disconnectedCallback() {
|
|
146
|
+
this.#detach();
|
|
147
|
+
this.#hostObserver?.disconnect();
|
|
148
|
+
this.#hostObserver = undefined;
|
|
149
|
+
}
|
|
150
|
+
/** Custom-element lifecycle (called by the browser): starts measuring its width and re-attaches after a move. */
|
|
151
|
+
connectedCallback() {
|
|
152
|
+
// focusable for the playback shortcuts. Set here, not in the constructor: a constructor that
|
|
153
|
+
// adds attributes makes document.createElement("scxml-explorer") fail (HTML spec), which is
|
|
154
|
+
// how React, Vue and most frameworks create elements.
|
|
155
|
+
if (!this.hasAttribute("tabindex"))
|
|
156
|
+
this.tabIndex = 0;
|
|
157
|
+
// one breakpoint, measured on the host; the CSS uses the same width in its container queries
|
|
158
|
+
if (typeof ResizeObserver !== "undefined" && !this.#hostObserver) {
|
|
159
|
+
this.#hostObserver = new ResizeObserver(() => this.#measure());
|
|
160
|
+
this.#hostObserver.observe(this);
|
|
161
|
+
}
|
|
162
|
+
this.#measure();
|
|
163
|
+
// re-attach after being moved in the DOM (disconnectedCallback detached it)
|
|
164
|
+
if (this.#session && !this.#tracker)
|
|
165
|
+
this.#attach();
|
|
166
|
+
}
|
|
167
|
+
/** Custom-element lifecycle (called by the browser) for `follow` and `announce`. */
|
|
168
|
+
attributeChangedCallback(name, _old, value) {
|
|
169
|
+
if (name === "follow")
|
|
170
|
+
this.#setFollow(value !== "false", false);
|
|
171
|
+
if (name === "announce")
|
|
172
|
+
this.#announce = value === "off" || value === "sends" ? value : "all";
|
|
173
|
+
}
|
|
174
|
+
#width = 0;
|
|
175
|
+
/** Width changes re-render (coalesced per frame): the breakpoint and "auto" layouts depend on it. */
|
|
176
|
+
#measure() {
|
|
177
|
+
const width = Math.round(this.getBoundingClientRect().width || this.clientWidth);
|
|
178
|
+
if (width === this.#width)
|
|
179
|
+
return;
|
|
180
|
+
this.#width = width;
|
|
181
|
+
this.#narrow = width > 0 && width <= NARROW_WIDTH;
|
|
182
|
+
this.#schedule();
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* Every user-visible string. Set a partial object to translate or relabel;
|
|
186
|
+
* unset keys keep their English default. See `ExplorerStrings` for the keys.
|
|
187
|
+
*/
|
|
188
|
+
get strings() {
|
|
189
|
+
return this.#strings;
|
|
190
|
+
}
|
|
191
|
+
/** Translate: a partial object; unset keys keep their English default. */
|
|
192
|
+
set strings(strings) {
|
|
193
|
+
this.#strings = { ...defaultStrings, ...strings };
|
|
194
|
+
this.#applyShellStrings();
|
|
195
|
+
this.#schedule();
|
|
196
|
+
}
|
|
197
|
+
/** What the polite live region announces: `"all"` (steps and sent events, default), `"sends"` or `"off"`. */
|
|
198
|
+
get announce() {
|
|
199
|
+
return this.#announce;
|
|
200
|
+
}
|
|
201
|
+
/** Sets the `announce` attribute. */
|
|
202
|
+
set announce(mode) {
|
|
203
|
+
this.setAttribute("announce", mode);
|
|
204
|
+
}
|
|
205
|
+
/** While a clock is playing, announce steps at most this often (ms of real time). Default 3000. */
|
|
206
|
+
get announceInterval() {
|
|
207
|
+
return this.#announceInterval;
|
|
208
|
+
}
|
|
209
|
+
/** Minimum real-time gap between step announcements while playing, in ms. */
|
|
210
|
+
set announceInterval(ms) {
|
|
211
|
+
this.#announceInterval = Math.max(0, ms);
|
|
212
|
+
}
|
|
213
|
+
/** The root session shown. Setting it attaches (with the current `processors` and `clock`). */
|
|
214
|
+
get session() {
|
|
215
|
+
return this.#session;
|
|
216
|
+
}
|
|
217
|
+
/** Attach this session (or detach with `undefined`). */
|
|
218
|
+
set session(session) {
|
|
219
|
+
this.#session = session;
|
|
220
|
+
if (session)
|
|
221
|
+
this.#attach();
|
|
222
|
+
else
|
|
223
|
+
this.detach();
|
|
224
|
+
}
|
|
225
|
+
/** I/O processors to show at the System level. */
|
|
226
|
+
get processors() {
|
|
227
|
+
return this.#processors;
|
|
228
|
+
}
|
|
229
|
+
/** The I/O processors to show; re-attaches if a session is set. */
|
|
230
|
+
set processors(processors) {
|
|
231
|
+
this.#processors = processors;
|
|
232
|
+
if (this.#session)
|
|
233
|
+
this.#attach();
|
|
234
|
+
}
|
|
235
|
+
/** The session's clock; a `PlaybackClock` gets playback controls. */
|
|
236
|
+
get clock() {
|
|
237
|
+
return this.#clockOption;
|
|
238
|
+
}
|
|
239
|
+
/** The clock to control; re-attaches if a session is set. */
|
|
240
|
+
set clock(clock) {
|
|
241
|
+
this.#clockOption = clock;
|
|
242
|
+
if (this.#session)
|
|
243
|
+
this.#attach();
|
|
244
|
+
}
|
|
245
|
+
/** Keep the focus on the part of the machine that is moving. Reflected as `follow="true|false"`. */
|
|
246
|
+
get follow() {
|
|
247
|
+
return this.#follow;
|
|
248
|
+
}
|
|
249
|
+
/** Turn following on or off (also reflected as the `follow` attribute). */
|
|
250
|
+
set follow(on) {
|
|
251
|
+
this.#setFollow(on);
|
|
252
|
+
}
|
|
253
|
+
/** Show a (root) session. Call before or after it starts. */
|
|
254
|
+
attach({ session, processors = [], clock }) {
|
|
255
|
+
this.#session = session;
|
|
256
|
+
this.#processors = processors;
|
|
257
|
+
this.#clockOption = clock;
|
|
258
|
+
this.#attach();
|
|
259
|
+
}
|
|
260
|
+
/** Stop showing the session and release all listeners (the session itself is left alone). */
|
|
261
|
+
detach() {
|
|
262
|
+
this.#detach();
|
|
263
|
+
this.#session = undefined;
|
|
264
|
+
this.#el.centerScroll.replaceChildren();
|
|
265
|
+
this.#el.treeViewport.replaceChildren();
|
|
266
|
+
this.#el.inspectorBody.replaceChildren();
|
|
267
|
+
}
|
|
268
|
+
#attach() {
|
|
269
|
+
const session = this.#session;
|
|
270
|
+
const processors = this.#processors;
|
|
271
|
+
const clock = this.#clockOption ?? session.clock;
|
|
272
|
+
this.#detach();
|
|
273
|
+
this.#clock = clock instanceof PlaybackClock ? clock : undefined;
|
|
274
|
+
this.#el.playback.hidden = !this.#clock;
|
|
275
|
+
if (this.#clock) {
|
|
276
|
+
const unsubscribe = this.#clock.subscribe(() => this.#renderPlayback());
|
|
277
|
+
this.#unwire.push(unsubscribe);
|
|
278
|
+
}
|
|
279
|
+
this.#tracker = new SystemTracker(session, processors, { clock });
|
|
280
|
+
this.#announcedSteps = this.#tracker.stepCount;
|
|
281
|
+
const onChange = () => {
|
|
282
|
+
this.#announceSteps();
|
|
283
|
+
this.#schedule();
|
|
284
|
+
};
|
|
285
|
+
this.#tracker.addEventListener("change", onChange);
|
|
286
|
+
this.#unwire.push(() => this.#tracker?.removeEventListener("change", onChange));
|
|
287
|
+
this.#openMachine(session.sessionId);
|
|
288
|
+
}
|
|
289
|
+
#detach() {
|
|
290
|
+
for (const f of this.#unwire.splice(0))
|
|
291
|
+
f();
|
|
292
|
+
this.#tracker?.dispose();
|
|
293
|
+
this.#tracker = undefined;
|
|
294
|
+
this.#announcer?.dispose();
|
|
295
|
+
this.#diagramObserver?.disconnect();
|
|
296
|
+
this.#systemObserver?.disconnect();
|
|
297
|
+
this.#links = undefined;
|
|
298
|
+
}
|
|
299
|
+
// ───────────────────────────── live region ─────────────────────────────
|
|
300
|
+
/** New steps since the last announcement → one message (the latest step). */
|
|
301
|
+
#announceSteps() {
|
|
302
|
+
const t = this.#tracker;
|
|
303
|
+
if (!t || t.stepCount === this.#announcedSteps)
|
|
304
|
+
return;
|
|
305
|
+
this.#announcedSteps = t.stepCount;
|
|
306
|
+
// a machine starting (no triggering event) isn't news; what happens next is
|
|
307
|
+
if (this.#announce === "all" && t.lastStep?.event !== undefined)
|
|
308
|
+
this.#say(this.#strings.announceStep(t.lastStep));
|
|
309
|
+
}
|
|
310
|
+
/**
|
|
311
|
+
* Write to the live region. Urgent messages (sent events, steps while paused or
|
|
312
|
+
* stepping) go out at once; while a clock runs, steps are throttled to one per
|
|
313
|
+
* `announceInterval`, the latest one winning.
|
|
314
|
+
*/
|
|
315
|
+
#say(text, urgent = false) {
|
|
316
|
+
if (this.#announce === "off")
|
|
317
|
+
return;
|
|
318
|
+
this.#announcer ??= new Announcer(this.#el.live, {
|
|
319
|
+
interval: () => this.#announceInterval,
|
|
320
|
+
paused: () => (this.#clock ? !this.#clock.playing : false),
|
|
321
|
+
});
|
|
322
|
+
this.#announcer.say(text, urgent);
|
|
323
|
+
}
|
|
324
|
+
#setFollow(on, reflect = true) {
|
|
325
|
+
if (on === this.#follow)
|
|
326
|
+
return;
|
|
327
|
+
this.#follow = on;
|
|
328
|
+
if (reflect)
|
|
329
|
+
this.setAttribute("follow", String(on));
|
|
330
|
+
if (on)
|
|
331
|
+
this.#applyFollow();
|
|
332
|
+
this.#schedule();
|
|
333
|
+
}
|
|
334
|
+
#emit(type, detail, cancelable = false) {
|
|
335
|
+
return this.dispatchEvent(new CustomEvent(type, { detail, bubbles: true, composed: true, cancelable }));
|
|
336
|
+
}
|
|
337
|
+
// ───────────────────────────── navigation ─────────────────────────────
|
|
338
|
+
get #machine() {
|
|
339
|
+
return this.#tracker?.machines.get(this.#machineKey);
|
|
340
|
+
}
|
|
341
|
+
#sessionListeners;
|
|
342
|
+
#openMachine(key, focus) {
|
|
343
|
+
const m = this.#tracker?.machines.get(key);
|
|
344
|
+
if (!m)
|
|
345
|
+
return;
|
|
346
|
+
this.#sessionListeners?.();
|
|
347
|
+
this.#machineKey = key;
|
|
348
|
+
const session = m.session;
|
|
349
|
+
this.#focus = focus ?? session.model.root;
|
|
350
|
+
this.#selected = undefined;
|
|
351
|
+
this.#focusFilter = "";
|
|
352
|
+
this.#focusShowAll = false;
|
|
353
|
+
if (!this.#expanded.has(key))
|
|
354
|
+
this.#expanded.set(key, activeExpansion(session.model, (s) => session.isActiveNode(s)));
|
|
355
|
+
const onMacro = () => {
|
|
356
|
+
// the first real configuration expands the active path once, whether or not we follow
|
|
357
|
+
if (!this.#seeded.has(key) && session.configuration.length) {
|
|
358
|
+
this.#seeded.add(key);
|
|
359
|
+
const exp = this.#expanded.get(key);
|
|
360
|
+
if (exp)
|
|
361
|
+
for (const s of session.configuration)
|
|
362
|
+
if (s.children.length)
|
|
363
|
+
exp.add(s);
|
|
364
|
+
}
|
|
365
|
+
if (this.#follow)
|
|
366
|
+
this.#applyFollow();
|
|
367
|
+
this.#schedule();
|
|
368
|
+
};
|
|
369
|
+
session.addEventListener("macrostep", onMacro);
|
|
370
|
+
session.addEventListener("done", onMacro);
|
|
371
|
+
this.#sessionListeners = () => {
|
|
372
|
+
session.removeEventListener("macrostep", onMacro);
|
|
373
|
+
session.removeEventListener("done", onMacro);
|
|
374
|
+
};
|
|
375
|
+
this.#unwire.push(() => this.#sessionListeners?.());
|
|
376
|
+
if (this.#follow)
|
|
377
|
+
this.#applyFollow();
|
|
378
|
+
this.#schedule();
|
|
379
|
+
}
|
|
380
|
+
#applyFollow() {
|
|
381
|
+
const m = this.#machine;
|
|
382
|
+
if (!m || !this.#focus)
|
|
383
|
+
return;
|
|
384
|
+
const session = m.session;
|
|
385
|
+
const next = followTarget(session.configuration, this.#focus);
|
|
386
|
+
// follow only moves within the machine and only to states on the active path
|
|
387
|
+
if (next !== this.#focus && (next.kind === "scxml" || session.isActiveNode(next)))
|
|
388
|
+
this.#focus = next;
|
|
389
|
+
// keep the active path visible in the tree
|
|
390
|
+
const exp = this.#expanded.get(this.#machineKey);
|
|
391
|
+
if (exp)
|
|
392
|
+
for (const s of session.configuration)
|
|
393
|
+
if (s.children.length)
|
|
394
|
+
exp.add(s);
|
|
395
|
+
}
|
|
396
|
+
/** User navigation: stops following, animates with a view transition when available. */
|
|
397
|
+
#navigate(change, keepFollow = false) {
|
|
398
|
+
if (!keepFollow)
|
|
399
|
+
this.#setFollow(false);
|
|
400
|
+
const run = () => {
|
|
401
|
+
change();
|
|
402
|
+
this.#renderNow();
|
|
403
|
+
};
|
|
404
|
+
const doc = document;
|
|
405
|
+
if (doc.startViewTransition && !matchMedia("(prefers-reduced-motion: reduce)").matches)
|
|
406
|
+
doc.startViewTransition(run);
|
|
407
|
+
else
|
|
408
|
+
run();
|
|
409
|
+
}
|
|
410
|
+
#focusOn(s) {
|
|
411
|
+
this.#navigate(() => {
|
|
412
|
+
this.#level = "machine";
|
|
413
|
+
this.#focus = isContainer(s) ? s : (s.parent ?? s);
|
|
414
|
+
this.#selected = isContainer(s) ? undefined : s;
|
|
415
|
+
this.#focusFilter = "";
|
|
416
|
+
this.#focusShowAll = false;
|
|
417
|
+
this.#doorsShowAll = { exits: false, entries: false };
|
|
418
|
+
this.#tab = "focus";
|
|
419
|
+
const exp = this.#expanded.get(this.#machineKey);
|
|
420
|
+
if (exp)
|
|
421
|
+
for (const a of pathTo(s))
|
|
422
|
+
if (a.children.length)
|
|
423
|
+
exp.add(a);
|
|
424
|
+
});
|
|
425
|
+
}
|
|
426
|
+
#setLevel(level) {
|
|
427
|
+
this.#level = level;
|
|
428
|
+
this.#tab = level === "system" ? "system" : "focus";
|
|
429
|
+
}
|
|
430
|
+
#showInspector(which) {
|
|
431
|
+
this.#inspector = which;
|
|
432
|
+
this.#tab = which;
|
|
433
|
+
this.#schedule();
|
|
434
|
+
}
|
|
435
|
+
/** Change view state, then re-render. */
|
|
436
|
+
#update(change) {
|
|
437
|
+
change();
|
|
438
|
+
this.#schedule();
|
|
439
|
+
}
|
|
440
|
+
#drillInto(e, s) {
|
|
441
|
+
e.stopPropagation();
|
|
442
|
+
this.#focusOn(s);
|
|
443
|
+
}
|
|
444
|
+
#select(s) {
|
|
445
|
+
this.#selected = s;
|
|
446
|
+
const session = this.#machine?.session;
|
|
447
|
+
if (session)
|
|
448
|
+
this.#emit("scxml-select", { session, state: s });
|
|
449
|
+
this.#inspector = "detail";
|
|
450
|
+
this.#tab = "detail";
|
|
451
|
+
this.#schedule();
|
|
452
|
+
}
|
|
453
|
+
// ──────────────────────────────── shell ────────────────────────────────
|
|
454
|
+
#buildShell() {
|
|
455
|
+
const h = html;
|
|
456
|
+
const levels = h("div", { class: "levels", part: "levels", role: "group" });
|
|
457
|
+
const crumbs = h("nav", { class: "crumbs", part: "crumbs" });
|
|
458
|
+
const follow = h("button", { class: "follow", part: "follow", type: "button" });
|
|
459
|
+
follow.addEventListener("click", () => this.#setFollow(!this.#follow));
|
|
460
|
+
// tree pane
|
|
461
|
+
const treeCount = h("span", { class: "count" });
|
|
462
|
+
const treeTitle = h("span", { class: "pane-title" });
|
|
463
|
+
const treeSearch = h("input", { type: "search", part: "tree-search" });
|
|
464
|
+
treeSearch.addEventListener("input", () => {
|
|
465
|
+
this.#treeFilter = treeSearch.value;
|
|
466
|
+
this.#treeScroll().scrollTop = 0;
|
|
467
|
+
this.#schedule();
|
|
468
|
+
});
|
|
469
|
+
const activeOnly = h("input", { type: "checkbox" });
|
|
470
|
+
activeOnly.addEventListener("change", () => {
|
|
471
|
+
this.#activeOnly = activeOnly.checked;
|
|
472
|
+
this.#schedule();
|
|
473
|
+
});
|
|
474
|
+
const activeOnlyText = h("span", {});
|
|
475
|
+
const treeViewport = h("div", { class: "tree-viewport", part: "tree", role: "tree" });
|
|
476
|
+
treeViewport.addEventListener("keydown", (e) => this.#treeKey(e));
|
|
477
|
+
const treeScroll = h("div", { class: "pane-scroll" }, treeViewport);
|
|
478
|
+
treeScroll.addEventListener("scroll", () => this.#renderTree(), { passive: true });
|
|
479
|
+
const treePane = h("section", { class: "pane tree-pane", part: "tree-pane", "data-tab": "tree" }, h("div", { class: "pane-head" }, treeTitle, treeCount), h("div", { class: "search" }, treeSearch, h("label", { class: "toggle" }, activeOnly, activeOnlyText)), treeScroll);
|
|
480
|
+
// center pane
|
|
481
|
+
const centerScroll = h("div", { class: "pane-scroll" });
|
|
482
|
+
const center = h("section", { class: "pane center", part: "focus-pane", "data-tab": "focus" }, centerScroll);
|
|
483
|
+
// inspector pane
|
|
484
|
+
const inspectorSwitch = h("div", { class: "mode", role: "group" });
|
|
485
|
+
const eventData = h("input", { placeholder: '{"json": "data"}' });
|
|
486
|
+
const eventSearch = h("input", { type: "search", part: "event-search" });
|
|
487
|
+
eventSearch.addEventListener("input", () => {
|
|
488
|
+
this.#eventFilter = eventSearch.value;
|
|
489
|
+
this.#schedule();
|
|
490
|
+
});
|
|
491
|
+
const scopes = h("div", { class: "scopes", part: "scopes", role: "group" });
|
|
492
|
+
const dataLabel = h("span", {});
|
|
493
|
+
const eventsHead = h("div", {}, h("div", { class: "search" }, eventSearch), scopes, h("label", { class: "data-field", part: "event-data" }, dataLabel, eventData));
|
|
494
|
+
const inspectorBody = h("div", { class: "pane-scroll" });
|
|
495
|
+
const inspector = h("section", { class: "pane inspector", part: "inspector", "data-tab": "events" }, h("div", { class: "pane-head" }, inspectorSwitch), eventsHead, inspectorBody);
|
|
496
|
+
// playback: pause / step / speed for a PlaybackClock
|
|
497
|
+
const playToggle = h("button", { class: "play", part: "play", type: "button" });
|
|
498
|
+
playToggle.addEventListener("click", () => this.#clock?.toggle());
|
|
499
|
+
const stepButton = h("button", { class: "step", part: "step", type: "button" });
|
|
500
|
+
stepButton.addEventListener("click", () => this.#step());
|
|
501
|
+
const speeds = h("div", { class: "mode speeds", part: "speeds", role: "group" });
|
|
502
|
+
const clockTime = h("span", { class: "clock-time", part: "clock" });
|
|
503
|
+
const clockQueue = h("span", { class: "clock-queue" });
|
|
504
|
+
const lastStep = h("span", { class: "last-step", part: "last-step" });
|
|
505
|
+
const playback = h("div", { class: "playback", part: "playback", hidden: "" }, playToggle, stepButton, speeds, clockTime, clockQueue, lastStep);
|
|
506
|
+
this.addEventListener("keydown", (e) => {
|
|
507
|
+
const target = e.composedPath()[0];
|
|
508
|
+
if (!this.#clock || /^(INPUT|TEXTAREA|SELECT|BUTTON)$/.test(target.tagName))
|
|
509
|
+
return;
|
|
510
|
+
// Space on focusable rows and cards is theirs; the shortcuts apply elsewhere in the explorer
|
|
511
|
+
if (target.matches?.("[role=treeitem], [role=button], [role=link]"))
|
|
512
|
+
return;
|
|
513
|
+
if (e.key === " ") {
|
|
514
|
+
e.preventDefault();
|
|
515
|
+
this.#clock.toggle();
|
|
516
|
+
}
|
|
517
|
+
else if (e.key === ".") {
|
|
518
|
+
e.preventDefault();
|
|
519
|
+
this.#step();
|
|
520
|
+
}
|
|
521
|
+
});
|
|
522
|
+
const tabs = h("nav", { class: "tabs", part: "tabs", role: "tablist" });
|
|
523
|
+
const strip = h("div", { class: "events-strip", part: "strip", role: "group" });
|
|
524
|
+
// polite live region: steps and sent events for screen readers (see `announce`)
|
|
525
|
+
const live = h("div", { class: "sr-only", role: "status", "aria-live": "polite", "aria-atomic": "true" });
|
|
526
|
+
const shell = h("div", { class: "shell" }, h("div", { class: "top", part: "top" }, levels, crumbs, h("slot", { name: "toolbar" }), follow), playback, h("div", { class: "body" }, treePane, center, inspector, strip, tabs), live);
|
|
527
|
+
this.#shadow.append(shell);
|
|
528
|
+
this.#el = {
|
|
529
|
+
levels,
|
|
530
|
+
crumbs,
|
|
531
|
+
follow,
|
|
532
|
+
treePane,
|
|
533
|
+
treeCount,
|
|
534
|
+
treeScroll,
|
|
535
|
+
treeViewport,
|
|
536
|
+
center,
|
|
537
|
+
centerScroll,
|
|
538
|
+
inspector,
|
|
539
|
+
inspectorSwitch,
|
|
540
|
+
inspectorBody,
|
|
541
|
+
eventsHead,
|
|
542
|
+
eventData,
|
|
543
|
+
tabs,
|
|
544
|
+
strip,
|
|
545
|
+
live,
|
|
546
|
+
treeTitle,
|
|
547
|
+
treeSearch,
|
|
548
|
+
activeOnlyText,
|
|
549
|
+
eventSearch,
|
|
550
|
+
scopes,
|
|
551
|
+
dataLabel,
|
|
552
|
+
playback,
|
|
553
|
+
playToggle,
|
|
554
|
+
stepButton,
|
|
555
|
+
speeds,
|
|
556
|
+
clockTime,
|
|
557
|
+
clockQueue,
|
|
558
|
+
lastStep,
|
|
559
|
+
};
|
|
560
|
+
this.#applyShellStrings();
|
|
561
|
+
}
|
|
562
|
+
/** The shell's static text, from `strings` (called again when they change). */
|
|
563
|
+
#applyShellStrings() {
|
|
564
|
+
const t = this.#strings;
|
|
565
|
+
const e = this.#el;
|
|
566
|
+
e.levels.setAttribute("aria-label", t.levelLabel);
|
|
567
|
+
// landmarks need unique names: with several explorers on a page, each host's aria-label
|
|
568
|
+
// (if the page gives one) names its breadcrumb
|
|
569
|
+
const host = this.getAttribute("aria-label");
|
|
570
|
+
e.crumbs.setAttribute("aria-label", host ? `${host}: ${t.breadcrumbLabel}` : t.breadcrumbLabel);
|
|
571
|
+
e.follow.textContent = t.follow;
|
|
572
|
+
e.follow.title = t.followHint;
|
|
573
|
+
e.treeTitle.textContent = t.statesPane;
|
|
574
|
+
e.treeSearch.placeholder = t.findState;
|
|
575
|
+
e.treeSearch.setAttribute("aria-label", t.filterStates);
|
|
576
|
+
e.activeOnlyText.textContent = t.activeOnly;
|
|
577
|
+
e.treeViewport.setAttribute("aria-label", t.treeLabel);
|
|
578
|
+
e.eventSearch.placeholder = t.findEvent;
|
|
579
|
+
e.eventSearch.setAttribute("aria-label", t.filterEvents);
|
|
580
|
+
e.scopes.setAttribute("aria-label", t.scopeLabel);
|
|
581
|
+
e.dataLabel.textContent = t.eventData;
|
|
582
|
+
e.eventData.setAttribute("aria-label", t.eventDataLabel);
|
|
583
|
+
e.playToggle.title = t.playHint;
|
|
584
|
+
e.stepButton.textContent = t.step;
|
|
585
|
+
e.stepButton.title = t.stepHint;
|
|
586
|
+
e.speeds.setAttribute("aria-label", t.speedLabel);
|
|
587
|
+
e.clockTime.title = t.virtualTime;
|
|
588
|
+
e.strip.setAttribute("aria-label", t.acceptedEventsLabel);
|
|
589
|
+
}
|
|
590
|
+
#treeScroll() {
|
|
591
|
+
return this.#el.treeScroll;
|
|
592
|
+
}
|
|
593
|
+
/** Tree rows are windowed, so their height must be known: `--scxml-row-height` (px), default 30. */
|
|
594
|
+
#rowHeight() {
|
|
595
|
+
const v = Number.parseFloat(getComputedStyle(this).getPropertyValue("--scxml-row-height"));
|
|
596
|
+
return Number.isFinite(v) && v > 0 ? v : DEFAULT_ROW;
|
|
597
|
+
}
|
|
598
|
+
/** One step: run the clock until any machine in the system completes a macrostep. */
|
|
599
|
+
#step() {
|
|
600
|
+
const clock = this.#clock;
|
|
601
|
+
const tracker = this.#tracker;
|
|
602
|
+
if (!clock || !tracker)
|
|
603
|
+
return;
|
|
604
|
+
const before = tracker.stepCount;
|
|
605
|
+
clock.step(() => tracker.stepCount > before);
|
|
606
|
+
this.#renderNow();
|
|
607
|
+
}
|
|
608
|
+
#renderPlayback() {
|
|
609
|
+
const clock = this.#clock;
|
|
610
|
+
if (!clock)
|
|
611
|
+
return;
|
|
612
|
+
const t = this.#strings;
|
|
613
|
+
const { playToggle, stepButton, speeds, clockTime, clockQueue, lastStep } = this.#el;
|
|
614
|
+
playToggle.textContent = clock.playing ? t.pause : t.play;
|
|
615
|
+
playToggle.setAttribute("aria-pressed", String(!clock.playing));
|
|
616
|
+
stepButton.disabled = clock.pending === 0;
|
|
617
|
+
if (speeds.childElementCount !== SPEEDS.length)
|
|
618
|
+
speeds.replaceChildren(...SPEEDS.map(([v, text]) => button(text, () => (clock.speed = v), { "data-speed": String(v) })));
|
|
619
|
+
for (const b of speeds.children)
|
|
620
|
+
b.setAttribute("aria-pressed", String(Number(b.dataset.speed) === clock.speed));
|
|
621
|
+
clockTime.textContent = t.clockTime(formatTime(clock.now()));
|
|
622
|
+
const next = clock.nextTimerAt;
|
|
623
|
+
const queue = [];
|
|
624
|
+
if (clock.ready)
|
|
625
|
+
queue.push(t.queued(clock.ready));
|
|
626
|
+
if (next !== undefined)
|
|
627
|
+
queue.push(t.nextTimer(formatTime(Math.max(0, next - clock.now()))));
|
|
628
|
+
clockQueue.textContent = queue.join(" · ") || t.idle;
|
|
629
|
+
const ls = this.#tracker?.lastStep;
|
|
630
|
+
lastStep.textContent = ls
|
|
631
|
+
? `${ls.machine}: ${ls.event ?? t.start}${ls.moves.length ? ` · ${ls.moves.slice(0, 2).join(" · ")}${ls.moves.length > 2 ? ` +${ls.moves.length - 2}` : ""}` : ` · ${t.noTransition}`}`
|
|
632
|
+
: "";
|
|
633
|
+
lastStep.title = ls ? [ls.event ?? t.start, ...ls.moves].join("\n") : "";
|
|
634
|
+
}
|
|
635
|
+
// ─────────────────────────────── render ───────────────────────────────
|
|
636
|
+
#schedule() {
|
|
637
|
+
if (this.#raf)
|
|
638
|
+
return;
|
|
639
|
+
this.#raf = requestAnimationFrame(() => {
|
|
640
|
+
this.#raf = 0;
|
|
641
|
+
this.#renderNow();
|
|
642
|
+
});
|
|
643
|
+
}
|
|
644
|
+
#renderNow() {
|
|
645
|
+
if (this.#raf) {
|
|
646
|
+
cancelAnimationFrame(this.#raf);
|
|
647
|
+
this.#raf = 0;
|
|
648
|
+
}
|
|
649
|
+
const m = this.#machine;
|
|
650
|
+
if (!m || !this.#focus)
|
|
651
|
+
return;
|
|
652
|
+
if (this.#focus !== this.#lastFocus) {
|
|
653
|
+
this.#lastFocus = this.#focus;
|
|
654
|
+
this.#emit("scxml-focus", { session: m.session, state: this.#focus });
|
|
655
|
+
}
|
|
656
|
+
this.#renderPlayback();
|
|
657
|
+
this.#renderTop(m);
|
|
658
|
+
this.#renderTree();
|
|
659
|
+
if (this.#level === "system")
|
|
660
|
+
this.#renderSystem();
|
|
661
|
+
else
|
|
662
|
+
this.#renderFocus(m);
|
|
663
|
+
this.#renderInspector(m);
|
|
664
|
+
this.#renderTabs();
|
|
665
|
+
}
|
|
666
|
+
#renderTop(m) {
|
|
667
|
+
const h = html;
|
|
668
|
+
const t = this.#strings;
|
|
669
|
+
const { levels, crumbs, follow } = this.#el;
|
|
670
|
+
levels.replaceChildren(...["system", "machine"].map((lv) => button(lv === "system" ? t.levelSystem : t.levelMachine, () => this.#navigate(() => this.#setLevel(lv), true), {
|
|
671
|
+
"aria-pressed": String(this.#level === lv),
|
|
672
|
+
})));
|
|
673
|
+
follow.setAttribute("aria-pressed", String(this.#follow));
|
|
674
|
+
const list = [
|
|
675
|
+
{
|
|
676
|
+
text: t.levelSystem,
|
|
677
|
+
go: () => this.#navigate(() => this.#setLevel("system"), true),
|
|
678
|
+
current: this.#level === "system",
|
|
679
|
+
},
|
|
680
|
+
];
|
|
681
|
+
if (this.#level === "machine") {
|
|
682
|
+
// machines above this one (invoke chain)
|
|
683
|
+
const chain = [];
|
|
684
|
+
for (let x = m; x; x = x.parentKey ? this.#tracker.machines.get(x.parentKey) : undefined)
|
|
685
|
+
chain.unshift(x);
|
|
686
|
+
for (const x of chain)
|
|
687
|
+
list.push({
|
|
688
|
+
text: x.name,
|
|
689
|
+
go: () => this.#navigate(() => this.#openMachine(x.key)),
|
|
690
|
+
current: x === m && this.#focus === m.session.model.root && !this.#selected,
|
|
691
|
+
});
|
|
692
|
+
for (const s of pathTo(this.#focus).slice(1))
|
|
693
|
+
list.push({ text: label(s), go: () => this.#focusOn(s), current: s === this.#focus && !this.#selected });
|
|
694
|
+
if (this.#selected && this.#selected !== this.#focus)
|
|
695
|
+
list.push({ text: label(this.#selected), go: () => this.#select(this.#selected), current: true });
|
|
696
|
+
}
|
|
697
|
+
let shown = list;
|
|
698
|
+
const narrow = this.#narrow;
|
|
699
|
+
if (!this.#crumbsExpanded && list.length > (narrow ? 2 : 5))
|
|
700
|
+
shown = narrow ? ["…", ...list.slice(-2)] : [list[0], list[1], "…", ...list.slice(-2)];
|
|
701
|
+
crumbs.replaceChildren(...shown.flatMap((c, i) => {
|
|
702
|
+
const sep = i ? [h("span", { class: "sep", "aria-hidden": "true" }, "›")] : [];
|
|
703
|
+
if (c === "…")
|
|
704
|
+
return [
|
|
705
|
+
...sep,
|
|
706
|
+
button("…", () => this.#update(() => (this.#crumbsExpanded = true)), { title: t.showWholePath, "aria-label": t.showWholePath }),
|
|
707
|
+
];
|
|
708
|
+
const b = button(c.text, c.go, { title: c.text, part: parts("crumb", c.current && "current") });
|
|
709
|
+
if (c.current)
|
|
710
|
+
b.setAttribute("aria-current", "page");
|
|
711
|
+
return [...sep, b];
|
|
712
|
+
}));
|
|
713
|
+
crumbs.scrollLeft = crumbs.scrollWidth; // narrow screens: keep the current place visible
|
|
714
|
+
}
|
|
715
|
+
// ── tree ──
|
|
716
|
+
#treeRows = [];
|
|
717
|
+
/** Render the windowed tree. `keepCursor`: a keyboard move just placed the cursor — don't re-sync it. */
|
|
718
|
+
#renderTree(keepCursor = false) {
|
|
719
|
+
const m = this.#machine;
|
|
720
|
+
if (!m)
|
|
721
|
+
return;
|
|
722
|
+
const t = this.#strings;
|
|
723
|
+
const session = m.session;
|
|
724
|
+
const exp = this.#expanded.get(this.#machineKey) ?? new Set();
|
|
725
|
+
this.#treeRows = treeRows(session.model, {
|
|
726
|
+
expanded: exp,
|
|
727
|
+
isActive: (s) => session.isActiveNode(s),
|
|
728
|
+
filter: this.#treeFilter,
|
|
729
|
+
activeOnly: this.#activeOnly,
|
|
730
|
+
});
|
|
731
|
+
const rows = this.#treeRows;
|
|
732
|
+
const total = session.model.states.filter((s) => s.kind !== "scxml" && s.kind !== "history").length;
|
|
733
|
+
this.#el.treeCount.textContent = this.#treeFilter ? t.matchesOf(rows.filter((r) => r.match).length, total) : String(total);
|
|
734
|
+
// the roving tabindex: while the tree has keyboard focus the cursor stays where the user put it;
|
|
735
|
+
// otherwise it tracks the selected / focused state, so tabbing in lands on "where we are"
|
|
736
|
+
const hadFocus = this.#shadow.activeElement?.getAttribute("role") === "treeitem";
|
|
737
|
+
if (!hadFocus && !keepCursor)
|
|
738
|
+
this.#treeCursor = undefined;
|
|
739
|
+
const cursorIndex = this.#treeCursorIndex();
|
|
740
|
+
const ROW = this.#rowHeight();
|
|
741
|
+
const vp = this.#el.treeViewport;
|
|
742
|
+
vp.style.height = `${rows.length * ROW}px`;
|
|
743
|
+
// windowing: only rows in view (plus overscan) exist in the DOM — and always the cursor row,
|
|
744
|
+
// so keyboard focus survives scrolling
|
|
745
|
+
const scroll = this.#el.treeScroll;
|
|
746
|
+
const first = Math.max(0, Math.floor(scroll.scrollTop / ROW) - 8);
|
|
747
|
+
const last = Math.min(rows.length, Math.ceil((scroll.scrollTop + (scroll.clientHeight || 600)) / ROW) + 8);
|
|
748
|
+
const indices = [];
|
|
749
|
+
for (let i = first; i < last; i++)
|
|
750
|
+
indices.push(i);
|
|
751
|
+
if (cursorIndex >= 0 && (cursorIndex < first || cursorIndex >= last))
|
|
752
|
+
indices.push(cursorIndex);
|
|
753
|
+
const h = html;
|
|
754
|
+
const nodes = [];
|
|
755
|
+
for (const i of indices) {
|
|
756
|
+
const r = rows[i];
|
|
757
|
+
const twisty = h("button", { class: "twisty", type: "button", tabindex: "-1", "aria-hidden": "true" }, r.expanded ? "▾" : "▸");
|
|
758
|
+
if (!r.expandable)
|
|
759
|
+
twisty.setAttribute("hidden", "");
|
|
760
|
+
twisty.addEventListener("click", (e) => {
|
|
761
|
+
e.stopPropagation();
|
|
762
|
+
this.#treeCursor = r.node;
|
|
763
|
+
this.#toggleExpanded(r.node);
|
|
764
|
+
});
|
|
765
|
+
const leafActive = r.active && isAtomic(r.node);
|
|
766
|
+
const siblings = r.node.parent?.children ?? [];
|
|
767
|
+
const row = h("div", {
|
|
768
|
+
class: `tree-row${r.onActivePath ? " path" : ""}${leafActive ? " active" : ""}${r.match ? " match" : ""}`,
|
|
769
|
+
part: parts("tree-row", r.onActivePath && "path", leafActive && "active", r.match && "match"),
|
|
770
|
+
role: "treeitem",
|
|
771
|
+
"data-index": String(i),
|
|
772
|
+
tabindex: i === cursorIndex ? "0" : "-1",
|
|
773
|
+
"aria-level": String(r.depth + 1),
|
|
774
|
+
"aria-setsize": String(siblings.length || 1),
|
|
775
|
+
"aria-posinset": String(siblings.indexOf(r.node) + 1 || 1),
|
|
776
|
+
"aria-selected": String(r.node === this.#selected || (r.node === this.#focus && !this.#selected)),
|
|
777
|
+
...(r.expandable ? { "aria-expanded": String(r.expanded) } : {}),
|
|
778
|
+
style: `top:${i * ROW}px; padding-left:${6 + r.depth * 14}px`,
|
|
779
|
+
title: r.node.id,
|
|
780
|
+
}, twisty, h("span", { class: "kind", "aria-hidden": "true" }, KIND_ICON[kindOf(r.node)]), h("span", { class: "name" }, label(r.node)), r.childCount ? h("span", { class: "count", "aria-hidden": "true" }, String(r.childCount)) : "", leafActive ? h("span", { class: "dot", "aria-hidden": "true" }) : "",
|
|
781
|
+
// status in words, not only colour and weight
|
|
782
|
+
leafActive ? srText(`, ${t.active}`) : r.onActivePath ? srText(`, ${t.containsActive}`) : "");
|
|
783
|
+
row.addEventListener("click", () => {
|
|
784
|
+
this.#treeCursor = r.node;
|
|
785
|
+
this.#focusOn(r.node);
|
|
786
|
+
});
|
|
787
|
+
nodes.push(row);
|
|
788
|
+
}
|
|
789
|
+
vp.replaceChildren(...nodes);
|
|
790
|
+
if (!rows.length)
|
|
791
|
+
vp.append(h("div", { class: "empty", part: "empty" }, h("slot", { name: "empty-tree" }, this.#treeFilter ? t.noStateMatches : t.noStates)));
|
|
792
|
+
if (hadFocus && cursorIndex >= 0)
|
|
793
|
+
vp.querySelector(`[data-index="${cursorIndex}"]`)?.focus({ preventScroll: true });
|
|
794
|
+
}
|
|
795
|
+
/** Index of the tree's keyboard cursor among the visible rows (setting a default when needed). */
|
|
796
|
+
#treeCursorIndex() {
|
|
797
|
+
const rows = this.#treeRows;
|
|
798
|
+
if (!rows.length)
|
|
799
|
+
return -1;
|
|
800
|
+
let i = this.#treeCursor ? rows.findIndex((r) => r.node === this.#treeCursor) : -1;
|
|
801
|
+
if (i < 0)
|
|
802
|
+
i = rows.findIndex((r) => r.node === (this.#selected ?? this.#focus));
|
|
803
|
+
if (i < 0)
|
|
804
|
+
i = 0;
|
|
805
|
+
this.#treeCursor = rows[i].node;
|
|
806
|
+
return i;
|
|
807
|
+
}
|
|
808
|
+
#toggleExpanded(node, open) {
|
|
809
|
+
const exp = this.#expanded.get(this.#machineKey);
|
|
810
|
+
if (!exp)
|
|
811
|
+
return;
|
|
812
|
+
const next = open ?? !exp.has(node);
|
|
813
|
+
if (next)
|
|
814
|
+
exp.add(node);
|
|
815
|
+
else
|
|
816
|
+
exp.delete(node);
|
|
817
|
+
this.#renderTree(true);
|
|
818
|
+
}
|
|
819
|
+
/** Move the cursor to a row, scroll it into the window, render and focus it. */
|
|
820
|
+
#moveTreeCursor(index) {
|
|
821
|
+
const rows = this.#treeRows;
|
|
822
|
+
if (!rows.length)
|
|
823
|
+
return;
|
|
824
|
+
const i = Math.max(0, Math.min(rows.length - 1, index));
|
|
825
|
+
this.#treeCursor = rows[i].node;
|
|
826
|
+
const ROW = this.#rowHeight();
|
|
827
|
+
const scroll = this.#el.treeScroll;
|
|
828
|
+
const view = scroll.clientHeight || 0;
|
|
829
|
+
if (i * ROW < scroll.scrollTop)
|
|
830
|
+
scroll.scrollTop = i * ROW;
|
|
831
|
+
else if (view && (i + 1) * ROW > scroll.scrollTop + view)
|
|
832
|
+
scroll.scrollTop = (i + 1) * ROW - view;
|
|
833
|
+
this.#renderTree(true);
|
|
834
|
+
this.#el.treeViewport.querySelector(`[data-index="${i}"]`)?.focus({ preventScroll: true });
|
|
835
|
+
}
|
|
836
|
+
/** The ARIA tree pattern: ↑ ↓ ← → Home End, Enter / Space, and type-ahead. */
|
|
837
|
+
#treeKey(e) {
|
|
838
|
+
const rows = this.#treeRows;
|
|
839
|
+
const i = this.#treeCursorIndex();
|
|
840
|
+
if (i < 0)
|
|
841
|
+
return;
|
|
842
|
+
const r = rows[i];
|
|
843
|
+
let handled = true;
|
|
844
|
+
switch (e.key) {
|
|
845
|
+
case "ArrowDown":
|
|
846
|
+
this.#moveTreeCursor(i + 1);
|
|
847
|
+
break;
|
|
848
|
+
case "ArrowUp":
|
|
849
|
+
this.#moveTreeCursor(i - 1);
|
|
850
|
+
break;
|
|
851
|
+
case "Home":
|
|
852
|
+
this.#moveTreeCursor(0);
|
|
853
|
+
break;
|
|
854
|
+
case "End":
|
|
855
|
+
this.#moveTreeCursor(rows.length - 1);
|
|
856
|
+
break;
|
|
857
|
+
case "ArrowRight":
|
|
858
|
+
if (r.expandable && !r.expanded) {
|
|
859
|
+
this.#toggleExpanded(r.node, true);
|
|
860
|
+
this.#moveTreeCursor(i);
|
|
861
|
+
}
|
|
862
|
+
else if (r.expandable)
|
|
863
|
+
this.#moveTreeCursor(i + 1);
|
|
864
|
+
break;
|
|
865
|
+
case "ArrowLeft": {
|
|
866
|
+
if (r.expandable && r.expanded) {
|
|
867
|
+
this.#toggleExpanded(r.node, false);
|
|
868
|
+
this.#moveTreeCursor(i);
|
|
869
|
+
}
|
|
870
|
+
else {
|
|
871
|
+
const parent = rows.findIndex((x) => x.node === r.node.parent);
|
|
872
|
+
if (parent >= 0)
|
|
873
|
+
this.#moveTreeCursor(parent);
|
|
874
|
+
}
|
|
875
|
+
break;
|
|
876
|
+
}
|
|
877
|
+
case "Enter":
|
|
878
|
+
case " ":
|
|
879
|
+
this.#focusOn(r.node);
|
|
880
|
+
this.#moveTreeCursor(this.#treeRows.findIndex((x) => x.node === r.node));
|
|
881
|
+
break;
|
|
882
|
+
default:
|
|
883
|
+
handled = this.#typeAhead(e, i);
|
|
884
|
+
}
|
|
885
|
+
if (handled) {
|
|
886
|
+
e.preventDefault();
|
|
887
|
+
e.stopPropagation();
|
|
888
|
+
}
|
|
889
|
+
}
|
|
890
|
+
/** Type-ahead: printable keys jump to the next row whose name starts with what was typed. */
|
|
891
|
+
#typeAhead(e, from) {
|
|
892
|
+
if (e.key.length !== 1 || e.ctrlKey || e.metaKey || e.altKey || !/\S/.test(e.key))
|
|
893
|
+
return false;
|
|
894
|
+
const now = Date.now();
|
|
895
|
+
this.#typeahead = { text: now - this.#typeahead.at > 700 ? e.key : this.#typeahead.text + e.key, at: now };
|
|
896
|
+
const q = this.#typeahead.text.toLowerCase();
|
|
897
|
+
const rows = this.#treeRows;
|
|
898
|
+
const start = q.length > 1 ? from : from + 1; // a new letter moves on; a longer prefix may stay
|
|
899
|
+
for (let k = 0; k < rows.length; k++) {
|
|
900
|
+
const j = (start + k) % rows.length;
|
|
901
|
+
if (label(rows[j].node).toLowerCase().startsWith(q)) {
|
|
902
|
+
this.#moveTreeCursor(j);
|
|
903
|
+
return true;
|
|
904
|
+
}
|
|
905
|
+
}
|
|
906
|
+
return true;
|
|
907
|
+
}
|
|
908
|
+
// ── focus ──
|
|
909
|
+
#renderFocus(m) {
|
|
910
|
+
const h = html;
|
|
911
|
+
const t = this.#strings;
|
|
912
|
+
const session = m.session;
|
|
913
|
+
const focus = this.#focus;
|
|
914
|
+
const isActive = (s) => session.isActiveNode(s);
|
|
915
|
+
const scope = focusScope(session.model, focus, isActive);
|
|
916
|
+
const out = this.#el.centerScroll;
|
|
917
|
+
const width = out.clientWidth || this.clientWidth;
|
|
918
|
+
const crowded = scope.children.length > DIAGRAM_MAX_CHILDREN || scope.edges.length > 3 * DIAGRAM_MAX_CHILDREN;
|
|
919
|
+
// auto: a diagram only when it fits — few children and not wider than the pane
|
|
920
|
+
const ranks = estimateRanks(scope.children, scope.edges);
|
|
921
|
+
const tooWide = ranks * (230 + 128) > width + 60;
|
|
922
|
+
const mode = focus.kind === "parallel"
|
|
923
|
+
? "lanes"
|
|
924
|
+
: this.#focusMode === "auto"
|
|
925
|
+
? crowded || tooWide || width < 560
|
|
926
|
+
? "list"
|
|
927
|
+
: "diagram"
|
|
928
|
+
: this.#focusMode;
|
|
929
|
+
const layoutName = { auto: t.layoutAuto, diagram: t.layoutDiagram, list: t.layoutList };
|
|
930
|
+
const modeSwitch = focus.kind === "parallel"
|
|
931
|
+
? ""
|
|
932
|
+
: h("div", { class: "mode", role: "group", "aria-label": t.layoutLabel }, ...["auto", "diagram", "list"].map((md) => button(layoutName[md], () => this.#update(() => (this.#focusMode = md)), {
|
|
933
|
+
"aria-pressed": String(this.#focusMode === md),
|
|
934
|
+
})));
|
|
935
|
+
const status = session.status === "done"
|
|
936
|
+
? h("span", { class: "badge done" }, t.terminated)
|
|
937
|
+
: isActive(focus) || focus.kind === "scxml"
|
|
938
|
+
? h("span", { class: "badge run" }, t.active)
|
|
939
|
+
: h("span", { class: "badge muted" }, t.inactive);
|
|
940
|
+
const header = [
|
|
941
|
+
h("h2", { class: "title", part: "title" }, label(focus)),
|
|
942
|
+
h("p", { class: "subtitle" }, h("span", {}, t.focusSummary(t.kind(kindOf(focus)), scope.children.length, descendantCount(focus))), status, focus.parent ? button(t.up(label(focus.parent)), () => this.#focusOn(focus.parent), { class: "drill" }) : "", h("span", { style: "flex:1" }), modeSwitch),
|
|
943
|
+
];
|
|
944
|
+
let body;
|
|
945
|
+
if (mode === "lanes")
|
|
946
|
+
body = this.#lanes(scope.children, isActive);
|
|
947
|
+
else if (mode === "diagram")
|
|
948
|
+
body = this.#diagram(scope.children, scope.edges);
|
|
949
|
+
else
|
|
950
|
+
body = this.#list(scope.children, scope.edges);
|
|
951
|
+
const doors = (kind) => {
|
|
952
|
+
const all = kind === "exits" ? scope.exits : scope.entries;
|
|
953
|
+
if (!all.length)
|
|
954
|
+
return [];
|
|
955
|
+
const showAll = this.#doorsShowAll[kind];
|
|
956
|
+
const shown = showAll ? all : all.slice(0, DOORS_SHOWN);
|
|
957
|
+
return [
|
|
958
|
+
h("div", { class: "section-title" }, kind === "exits" ? t.leavesTo : t.enteredFrom, h("span", { class: "count" }, String(all.length))),
|
|
959
|
+
h("div", { class: "doors", part: "doors" }, ...shown.map((d) => {
|
|
960
|
+
const live = kind === "exits" && (d.child === focus ? isActive(focus) : isActive(d.child));
|
|
961
|
+
const where = kind === "exits" ? label(d.other) : `${label(d.other)} → ${label(d.child)}`;
|
|
962
|
+
const context = `${t.inState(d.other.parent ? label(d.other.parent) : t.machine)}${kind === "exits" && d.child !== focus ? ` · ${t.fromState(label(d.child))}` : ""}`;
|
|
963
|
+
const el = h("div", {
|
|
964
|
+
class: `door${live ? " live" : ""}`,
|
|
965
|
+
part: parts("door", kind === "exits" ? "exit" : "entry", live && "live"),
|
|
966
|
+
role: "link",
|
|
967
|
+
tabindex: "0",
|
|
968
|
+
title: t.transitions(d.transitions.length),
|
|
969
|
+
"aria-label": `${kind === "exits" ? t.leavesTo : t.enteredFrom}: ${where}, ${context}; ${d.events.join(", ")}`,
|
|
970
|
+
}, h("span", { class: "arrow", "aria-hidden": "true" }, kind === "exits" ? "⇥" : "⇤"), h("span", { class: "where" }, where, h("small", {}, ` ${context}`)), h("span", { class: "events" }, ...chips(d.events, 3, () => { })));
|
|
971
|
+
activate(el, () => this.#focusOn(d.other));
|
|
972
|
+
return el;
|
|
973
|
+
}), all.length > DOORS_SHOWN
|
|
974
|
+
? button(showAll ? t.showFewer : t.more(all.length - DOORS_SHOWN), () => this.#update(() => (this.#doorsShowAll[kind] = !showAll)), {
|
|
975
|
+
class: "more",
|
|
976
|
+
})
|
|
977
|
+
: ""),
|
|
978
|
+
];
|
|
979
|
+
};
|
|
980
|
+
const scroll = out.scrollTop;
|
|
981
|
+
out.replaceChildren(...header, body, ...doors("exits"), ...doors("entries"));
|
|
982
|
+
out.scrollTop = scroll;
|
|
983
|
+
this.#diagramObserver?.disconnect();
|
|
984
|
+
if (mode === "diagram") {
|
|
985
|
+
const draw = () => this.#drawEdges(scope.edges, isActive);
|
|
986
|
+
requestAnimationFrame(draw);
|
|
987
|
+
// the pane resizes (window, container, split view): redraw the arrows, once per frame
|
|
988
|
+
if (typeof ResizeObserver !== "undefined" && this.#diagramEl) {
|
|
989
|
+
let pending = 0;
|
|
990
|
+
this.#diagramObserver = new ResizeObserver(() => {
|
|
991
|
+
if (pending)
|
|
992
|
+
return;
|
|
993
|
+
pending = requestAnimationFrame(() => {
|
|
994
|
+
pending = 0;
|
|
995
|
+
draw();
|
|
996
|
+
});
|
|
997
|
+
});
|
|
998
|
+
this.#diagramObserver.observe(this.#diagramEl);
|
|
999
|
+
}
|
|
1000
|
+
}
|
|
1001
|
+
}
|
|
1002
|
+
/** In the diagram, arrows carry the events: cards only show how many there are. */
|
|
1003
|
+
#card(c, opts = {}) {
|
|
1004
|
+
const h = html;
|
|
1005
|
+
const t = this.#strings;
|
|
1006
|
+
const s = c.node;
|
|
1007
|
+
const waiting = c.active &&
|
|
1008
|
+
isAtomic(s) &&
|
|
1009
|
+
(s.invokes.length > 0 ||
|
|
1010
|
+
s.onentry.some((b) => [...b.getElementsByTagNameNS(s.element.namespaceURI, "send")].some((x) => x.getAttribute("type") && x.getAttribute("type") !== "scxml")));
|
|
1011
|
+
const cls = [
|
|
1012
|
+
"card",
|
|
1013
|
+
c.onActivePath ? "path" : "",
|
|
1014
|
+
c.active && isAtomic(s) ? "active" : "",
|
|
1015
|
+
waiting ? "waiting" : "",
|
|
1016
|
+
s.kind === "final" ? "final" : "",
|
|
1017
|
+
s === this.#selected ? "selected" : "",
|
|
1018
|
+
];
|
|
1019
|
+
const status = waiting ? t.waiting : c.onActivePath ? t.active : "";
|
|
1020
|
+
// a group, not a button: it holds its own controls (drill, event chips), and interactive
|
|
1021
|
+
// elements must not nest. The state's name is the card's primary button.
|
|
1022
|
+
const name = [label(s), t.kind(kindOf(s)), status, c.initial && t.initial].filter(Boolean).join(", ");
|
|
1023
|
+
const select = h("button", { type: "button", class: "name select", "aria-label": name, "aria-pressed": String(s === this.#selected) }, label(s));
|
|
1024
|
+
const card = h("div", {
|
|
1025
|
+
class: cls.filter(Boolean).join(" "),
|
|
1026
|
+
part: parts("card", ...cls.slice(1)),
|
|
1027
|
+
"data-state": s.id,
|
|
1028
|
+
role: "group",
|
|
1029
|
+
"aria-label": name,
|
|
1030
|
+
}, c.initial ? h("span", { class: "initial-mark", title: t.initial }) : "", h("div", { class: "card-head" }, h("span", { class: "kind", "aria-hidden": "true" }, KIND_ICON[kindOf(s)]), select, waiting ? h("span", { class: "badge wait" }, t.waiting) : c.onActivePath ? h("span", { class: "badge run" }, t.active) : ""), opts.compact
|
|
1031
|
+
? ""
|
|
1032
|
+
: h("div", { class: "meta" }, c.descendants ? button(t.inside(c.descendants), (e) => this.#drillInto(e, s), { class: "drill" }) : "", c.invokes ? h("span", {}, t.invokes(c.invokes)) : "", c.internal ? h("span", { title: t.staysInside }, `↺ ${c.internal}`) : "", opts.eventsOnArrows && c.events.length ? h("span", { title: c.events.join("\n") }, t.events(c.events.length)) : ""), opts.compact || opts.eventsOnArrows || !c.events.length
|
|
1033
|
+
? ""
|
|
1034
|
+
: h("div", { class: "events" }, ...chips(c.events, 3, (ev) => this.#sendDescriptor(ev))), h("slot", { name: `state:${s.id}` }));
|
|
1035
|
+
this.#operable(card, s, select);
|
|
1036
|
+
return card;
|
|
1037
|
+
}
|
|
1038
|
+
/**
|
|
1039
|
+
* Click / keyboard behaviour shared by cards, list rows and lane rows:
|
|
1040
|
+
* click and Space select, Enter drills into a container (or selects a leaf),
|
|
1041
|
+
* double-click drills in.
|
|
1042
|
+
*/
|
|
1043
|
+
#operable(el, s, keys = el) {
|
|
1044
|
+
el.addEventListener("click", () => this.#select(s));
|
|
1045
|
+
el.addEventListener("dblclick", () => isContainer(s) && this.#focusOn(s));
|
|
1046
|
+
keys.addEventListener("keydown", (e) => {
|
|
1047
|
+
if (e.target !== keys)
|
|
1048
|
+
return; // keys on other buttons inside belong to them
|
|
1049
|
+
if (e.key === "Enter") {
|
|
1050
|
+
e.preventDefault();
|
|
1051
|
+
if (isContainer(s))
|
|
1052
|
+
this.#focusOn(s);
|
|
1053
|
+
else
|
|
1054
|
+
this.#select(s);
|
|
1055
|
+
}
|
|
1056
|
+
else if (e.key === " ") {
|
|
1057
|
+
e.preventDefault();
|
|
1058
|
+
this.#select(s);
|
|
1059
|
+
}
|
|
1060
|
+
});
|
|
1061
|
+
}
|
|
1062
|
+
#diagram(children, edges) {
|
|
1063
|
+
const h = html;
|
|
1064
|
+
// layered layout: rank = shortest distance from the initial child(ren); unreachable ones last
|
|
1065
|
+
const rank = new Map();
|
|
1066
|
+
const queue = children.filter((c) => c.initial).map((c) => c.node);
|
|
1067
|
+
if (!queue.length && children[0])
|
|
1068
|
+
queue.push(children[0].node);
|
|
1069
|
+
for (const q of queue)
|
|
1070
|
+
rank.set(q, 0);
|
|
1071
|
+
for (let i = 0; i < queue.length; i++) {
|
|
1072
|
+
const n = queue[i];
|
|
1073
|
+
for (const e of edges) {
|
|
1074
|
+
if (e.from !== n || rank.has(e.to))
|
|
1075
|
+
continue;
|
|
1076
|
+
rank.set(e.to, rank.get(n) + 1);
|
|
1077
|
+
queue.push(e.to);
|
|
1078
|
+
}
|
|
1079
|
+
}
|
|
1080
|
+
const maxRank = Math.max(0, ...rank.values());
|
|
1081
|
+
for (const c of children)
|
|
1082
|
+
if (!rank.has(c.node))
|
|
1083
|
+
rank.set(c.node, maxRank + 1);
|
|
1084
|
+
const columns = [];
|
|
1085
|
+
for (const c of children) {
|
|
1086
|
+
const r = rank.get(c.node);
|
|
1087
|
+
const column = columns[r] ?? [];
|
|
1088
|
+
column.push(c);
|
|
1089
|
+
columns[r] = column;
|
|
1090
|
+
}
|
|
1091
|
+
const diagram = h("div", { class: "diagram", part: "diagram" }, ...columns.filter(Boolean).map((col) => h("div", { class: "rank" }, ...col.map((c) => this.#card(c, { eventsOnArrows: true })))));
|
|
1092
|
+
this.#diagramEl = diagram;
|
|
1093
|
+
this.#rank = rank;
|
|
1094
|
+
return diagram;
|
|
1095
|
+
}
|
|
1096
|
+
#diagramEl;
|
|
1097
|
+
#rank = new Map();
|
|
1098
|
+
/**
|
|
1099
|
+
* Draw the arrows between the diagram's cards and place their labels:
|
|
1100
|
+
* deterministic order (live edges first, then document order), each label
|
|
1101
|
+
* at its arrow's middle or the nearest free spot above/below; labels with no
|
|
1102
|
+
* free spot collapse to their event count and open on hover / focus.
|
|
1103
|
+
*/
|
|
1104
|
+
#drawEdges(edges, isActive) {
|
|
1105
|
+
const d = this.#diagramEl;
|
|
1106
|
+
if (!d?.isConnected)
|
|
1107
|
+
return;
|
|
1108
|
+
for (const x of d.querySelectorAll(".edges, .edge-label"))
|
|
1109
|
+
x.remove();
|
|
1110
|
+
const base = d.getBoundingClientRect();
|
|
1111
|
+
const rect = (s) => d.querySelector(`[data-state="${CSS.escape(s.id)}"]`)?.getBoundingClientRect();
|
|
1112
|
+
const svg = document.createElementNS("http://www.w3.org/2000/svg", "svg");
|
|
1113
|
+
svg.setAttribute("class", "edges");
|
|
1114
|
+
svg.setAttribute("part", "edges");
|
|
1115
|
+
svg.setAttribute("aria-hidden", "true");
|
|
1116
|
+
svg.append(arrowMarker("x-head", 10, 6, 10));
|
|
1117
|
+
const ordered = [...edges].sort((a, b) => Number(isActive(b.from)) - Number(isActive(a.from)) || a.from.order - b.from.order || a.to.order - b.to.order);
|
|
1118
|
+
const requests = [];
|
|
1119
|
+
let backIndex = 0;
|
|
1120
|
+
for (const e of ordered) {
|
|
1121
|
+
const a = rect(e.from);
|
|
1122
|
+
const b = rect(e.to);
|
|
1123
|
+
if (!a || !b)
|
|
1124
|
+
continue;
|
|
1125
|
+
const forward = (this.#rank.get(e.to) ?? 0) > (this.#rank.get(e.from) ?? 0);
|
|
1126
|
+
let dPath;
|
|
1127
|
+
let mx;
|
|
1128
|
+
let my;
|
|
1129
|
+
if (forward) {
|
|
1130
|
+
const x1 = a.right - base.left;
|
|
1131
|
+
const y1 = a.top + a.height / 2 - base.top;
|
|
1132
|
+
const x2 = b.left - base.left;
|
|
1133
|
+
const y2 = b.top + b.height / 2 - base.top;
|
|
1134
|
+
const dx = Math.max(24, (x2 - x1) / 2);
|
|
1135
|
+
dPath = `M${x1},${y1} C${x1 + dx},${y1} ${x2 - dx},${y2} ${x2 - 1},${y2}`;
|
|
1136
|
+
mx = (x1 + x2) / 2;
|
|
1137
|
+
my = (y1 + y2) / 2;
|
|
1138
|
+
}
|
|
1139
|
+
else {
|
|
1140
|
+
// back / same-rank edges loop underneath, staggered
|
|
1141
|
+
const x1 = a.left + a.width / 2 - base.left;
|
|
1142
|
+
const y1 = a.bottom - base.top;
|
|
1143
|
+
const x2 = b.left + b.width / 2 - base.left;
|
|
1144
|
+
const y2 = b.bottom - base.top;
|
|
1145
|
+
const drop = 22 + (backIndex++ % 4) * 14;
|
|
1146
|
+
const low = Math.max(y1, y2) + drop;
|
|
1147
|
+
dPath = `M${x1},${y1} C${x1},${low} ${x2},${low} ${x2},${y2 + 1}`;
|
|
1148
|
+
mx = (x1 + x2) / 2;
|
|
1149
|
+
my = low - drop / 4;
|
|
1150
|
+
}
|
|
1151
|
+
const live = isActive(e.from);
|
|
1152
|
+
const p = document.createElementNS("http://www.w3.org/2000/svg", "path");
|
|
1153
|
+
p.setAttribute("d", dPath);
|
|
1154
|
+
p.setAttribute("class", `${live ? "live" : ""}${forward ? "" : " back"}`);
|
|
1155
|
+
p.setAttribute("marker-end", "url(#x-head)");
|
|
1156
|
+
svg.append(p);
|
|
1157
|
+
const text = e.events.length === 1 ? e.events[0] : `${e.events[0]} +${e.events.length - 1}`;
|
|
1158
|
+
// label size: measured by the browser below; this estimate is only for layouts that can't measure
|
|
1159
|
+
requests.push({ cx: mx, cy: my, w: Math.min(120, 14 + text.length * 6.6), h: 18, edge: e, live });
|
|
1160
|
+
}
|
|
1161
|
+
svg.setAttribute("width", String(d.scrollWidth));
|
|
1162
|
+
svg.setAttribute("height", String(d.scrollHeight + 80));
|
|
1163
|
+
d.prepend(svg);
|
|
1164
|
+
const labels = requests.map((r) => {
|
|
1165
|
+
const e = r.edge;
|
|
1166
|
+
const text = e.events.length === 1 ? e.events[0] : `${e.events[0]} +${e.events.length - 1}`;
|
|
1167
|
+
const lab = html("span", {
|
|
1168
|
+
class: `edge-label${r.live ? " live" : ""}`,
|
|
1169
|
+
part: parts("edge-label", r.live && "live"),
|
|
1170
|
+
title: e.events.join("\n"),
|
|
1171
|
+
tabindex: "-1",
|
|
1172
|
+
}, html("span", { class: "full" }, text), html("span", { class: "short" }, String(e.events.length)));
|
|
1173
|
+
d.append(lab);
|
|
1174
|
+
const measured = lab.getBoundingClientRect();
|
|
1175
|
+
if (measured.width)
|
|
1176
|
+
r.w = measured.width;
|
|
1177
|
+
if (measured.height)
|
|
1178
|
+
r.h = measured.height;
|
|
1179
|
+
return lab;
|
|
1180
|
+
});
|
|
1181
|
+
const cards = [...d.querySelectorAll("[data-state]")].map((c) => {
|
|
1182
|
+
const b = c.getBoundingClientRect();
|
|
1183
|
+
return { x: b.left - base.left, y: b.top - base.top, w: b.width, h: b.height };
|
|
1184
|
+
});
|
|
1185
|
+
const placement = placeLabels(requests, cards);
|
|
1186
|
+
placement.forEach((pl, i) => {
|
|
1187
|
+
const lab = labels[i];
|
|
1188
|
+
lab.style.left = `${pl.cx}px`;
|
|
1189
|
+
lab.style.top = `${pl.cy}px`;
|
|
1190
|
+
if (pl.collapsed) {
|
|
1191
|
+
lab.classList.add("collapsed");
|
|
1192
|
+
lab.setAttribute("part", `${lab.getAttribute("part")} collapsed`);
|
|
1193
|
+
}
|
|
1194
|
+
});
|
|
1195
|
+
d.style.paddingBottom = edges.some((e) => (this.#rank.get(e.to) ?? 0) <= (this.#rank.get(e.from) ?? 0)) ? "72px" : "10px";
|
|
1196
|
+
}
|
|
1197
|
+
#list(children, edges) {
|
|
1198
|
+
const h = html;
|
|
1199
|
+
const t = this.#strings;
|
|
1200
|
+
const q = this.#focusFilter.trim().toLowerCase();
|
|
1201
|
+
const activeChildren = new Set(children.filter((c) => c.onActivePath).map((c) => c.node));
|
|
1202
|
+
const neighbours = new Set();
|
|
1203
|
+
for (const e of edges) {
|
|
1204
|
+
if (activeChildren.has(e.from))
|
|
1205
|
+
neighbours.add(e.to);
|
|
1206
|
+
if (activeChildren.has(e.to))
|
|
1207
|
+
neighbours.add(e.from);
|
|
1208
|
+
}
|
|
1209
|
+
const matches = children.filter((c) => !q || c.node.id.toLowerCase().includes(q));
|
|
1210
|
+
const groups = [
|
|
1211
|
+
[t.groupActivePath, matches.filter((c) => activeChildren.has(c.node))],
|
|
1212
|
+
[t.groupOneStep, matches.filter((c) => !activeChildren.has(c.node) && neighbours.has(c.node))],
|
|
1213
|
+
[t.groupElse, matches.filter((c) => !activeChildren.has(c.node) && !neighbours.has(c.node))],
|
|
1214
|
+
];
|
|
1215
|
+
const outgoing = new Map();
|
|
1216
|
+
for (const e of edges) {
|
|
1217
|
+
const list = outgoing.get(e.from) ?? [];
|
|
1218
|
+
list.push(e);
|
|
1219
|
+
outgoing.set(e.from, list);
|
|
1220
|
+
}
|
|
1221
|
+
let budget = this.#focusShowAll ? Infinity : LIST_PAGE;
|
|
1222
|
+
const rows = [];
|
|
1223
|
+
for (const [title, list] of groups) {
|
|
1224
|
+
if (!list.length)
|
|
1225
|
+
continue;
|
|
1226
|
+
rows.push(h("div", { class: "list-group", role: "presentation" }, t.groupCount(title, list.length)));
|
|
1227
|
+
for (const c of list) {
|
|
1228
|
+
if (budget-- <= 0)
|
|
1229
|
+
break;
|
|
1230
|
+
const s = c.node;
|
|
1231
|
+
const outs = outgoing.get(s) ?? [];
|
|
1232
|
+
const rowName = [label(s), t.kind(kindOf(s)), c.onActivePath && t.active].filter(Boolean).join(", ");
|
|
1233
|
+
const select = h("button", { type: "button", class: "name select", "aria-label": rowName }, label(s));
|
|
1234
|
+
const row = h("div", {
|
|
1235
|
+
class: `list-row${c.onActivePath ? " path" : ""}`,
|
|
1236
|
+
part: parts("list-row", c.onActivePath && "path"),
|
|
1237
|
+
"data-state": s.id,
|
|
1238
|
+
role: "group",
|
|
1239
|
+
"aria-label": rowName,
|
|
1240
|
+
}, h("span", { class: "kind", "aria-hidden": "true" }, KIND_ICON[kindOf(s)]), select, h("span", { class: "meta" }, c.onActivePath ? h("span", { class: "badge run" }, t.active) : "", c.descendants
|
|
1241
|
+
? button(`${c.descendants} →`, (e) => this.#drillInto(e, s), { class: "drill", title: t.statesInside(c.descendants) })
|
|
1242
|
+
: ""), outs.length || c.events.length
|
|
1243
|
+
? h("span", { class: "sub" }, ...outs.slice(0, 3).map((e) => h("span", { class: "chip muted", title: e.events.join("\n") }, `→ ${label(e.to)}`)), outs.length > 3 ? h("span", { class: "count" }, t.moreTargets(outs.length - 3)) : "", c.events.length ? h("span", { class: "count" }, `· ${t.events(c.events.length)}`) : "")
|
|
1244
|
+
: "");
|
|
1245
|
+
this.#operable(row, s, select);
|
|
1246
|
+
rows.push(row);
|
|
1247
|
+
}
|
|
1248
|
+
}
|
|
1249
|
+
const hidden = matches.length - Math.min(matches.length, this.#focusShowAll ? Infinity : LIST_PAGE);
|
|
1250
|
+
const search = children.length > 12
|
|
1251
|
+
? (() => {
|
|
1252
|
+
const input = h("input", {
|
|
1253
|
+
type: "search",
|
|
1254
|
+
placeholder: t.findAmongStates(children.length),
|
|
1255
|
+
"aria-label": t.findAmongStates(children.length),
|
|
1256
|
+
value: this.#focusFilter,
|
|
1257
|
+
});
|
|
1258
|
+
input.addEventListener("input", () => {
|
|
1259
|
+
this.#focusFilter = input.value;
|
|
1260
|
+
this.#focusShowAll = false;
|
|
1261
|
+
this.#renderNow();
|
|
1262
|
+
this.#el.centerScroll.querySelector('.search input[type="search"]')?.focus();
|
|
1263
|
+
});
|
|
1264
|
+
return h("div", { class: "search", style: "padding:0 0 10px" }, input);
|
|
1265
|
+
})()
|
|
1266
|
+
: "";
|
|
1267
|
+
return h("div", {}, search, rows.length ? h("div", { class: "list", part: "list" }, ...rows) : h("div", { class: "empty", part: "empty" }, t.noStateMatches), hidden > 0
|
|
1268
|
+
? button(t.showAll(matches.length), () => this.#update(() => (this.#focusShowAll = true)), {
|
|
1269
|
+
class: "more",
|
|
1270
|
+
style: "margin-top:6px",
|
|
1271
|
+
})
|
|
1272
|
+
: "");
|
|
1273
|
+
}
|
|
1274
|
+
#lanes(regions, isActive) {
|
|
1275
|
+
const h = html;
|
|
1276
|
+
const t = this.#strings;
|
|
1277
|
+
const PER_LANE = 7;
|
|
1278
|
+
return h("div", { class: "lanes", part: "lanes" }, ...regions.map((r) => {
|
|
1279
|
+
const kids = [...r.node.children].sort((a, b) => Number(isActive(b)) - Number(isActive(a)) || a.order - b.order);
|
|
1280
|
+
const name = button(label(r.node), () => this.#focusOn(r.node), {
|
|
1281
|
+
class: "name",
|
|
1282
|
+
title: r.node.id,
|
|
1283
|
+
"aria-label": [label(r.node), r.onActivePath && t.active].filter(Boolean).join(", "),
|
|
1284
|
+
});
|
|
1285
|
+
const lane = h("section", { class: `lane${r.onActivePath ? " path" : ""}`, part: parts("lane", r.onActivePath && "path"), "data-state": r.node.id }, h("div", { class: "lane-head" }, h("span", { class: "kind", "aria-hidden": "true" }, KIND_ICON[kindOf(r.node)]), name, r.onActivePath ? h("span", { class: "badge run" }, t.active) : "", h("span", { class: "count" }, String(r.node.children.length))), ...kids.slice(0, PER_LANE).map((k) => {
|
|
1286
|
+
const on = isActive(k);
|
|
1287
|
+
const row = h("div", {
|
|
1288
|
+
class: `lane-row${on ? " active" : ""}`,
|
|
1289
|
+
part: parts("lane-row", on && "active"),
|
|
1290
|
+
role: "button",
|
|
1291
|
+
tabindex: "0",
|
|
1292
|
+
"aria-label": [label(k), t.kind(kindOf(k)), on && t.active].filter(Boolean).join(", "),
|
|
1293
|
+
}, h("span", { class: "kind", "aria-hidden": "true" }, KIND_ICON[kindOf(k)]), h("span", { class: "name" }, label(k)), on
|
|
1294
|
+
? h("span", {
|
|
1295
|
+
class: "dot",
|
|
1296
|
+
"aria-hidden": "true",
|
|
1297
|
+
style: "width:7px;height:7px;border-radius:50%;background:var(--x-run)",
|
|
1298
|
+
})
|
|
1299
|
+
: "");
|
|
1300
|
+
// containers open as the focus, leaves are selected
|
|
1301
|
+
activate(row, () => (isContainer(k) ? this.#focusOn(k) : this.#select(k)));
|
|
1302
|
+
return row;
|
|
1303
|
+
}), kids.length > PER_LANE ? button(t.openLane(kids.length - PER_LANE), () => this.#focusOn(r.node), { class: "more" }) : "");
|
|
1304
|
+
return lane;
|
|
1305
|
+
}));
|
|
1306
|
+
}
|
|
1307
|
+
// ── system ──
|
|
1308
|
+
#renderSystem() {
|
|
1309
|
+
const h = html;
|
|
1310
|
+
const tr = this.#strings;
|
|
1311
|
+
const t = this.#tracker;
|
|
1312
|
+
const machines = t.machineList();
|
|
1313
|
+
const services = [...t.services.values()];
|
|
1314
|
+
const q = this.#machineFilter.trim().toLowerCase();
|
|
1315
|
+
const shownMachines = machines.filter((m) => !q || m.name.toLowerCase().includes(q) || m.activeLeaves.some((l) => l.toLowerCase().includes(q)));
|
|
1316
|
+
const compact = machines.length > 8; // many machines: one line each
|
|
1317
|
+
const talkOf = (m) => [
|
|
1318
|
+
...new Set(t
|
|
1319
|
+
.linksOf(m.key)
|
|
1320
|
+
.filter((l) => l.kind === "message")
|
|
1321
|
+
.map((l) => (l.from === m.key ? l.to : l.from))
|
|
1322
|
+
.map((k) => t.services.get(k)?.label ?? t.machines.get(k)?.name ?? k)),
|
|
1323
|
+
];
|
|
1324
|
+
const machineCard = (m) => {
|
|
1325
|
+
const talk = talkOf(m);
|
|
1326
|
+
const hot = t.linksOf(m.key).some((l) => l.kind === "message" && t.isHot(l));
|
|
1327
|
+
const statusText = tr.status(m.status);
|
|
1328
|
+
const card = h("div", {
|
|
1329
|
+
class: `card machine${compact ? " compact" : ""}${m.status === "running" ? " path" : ""}${hot ? " pulse" : ""}`,
|
|
1330
|
+
part: parts("machine", m.status === "running" && "running", m.status === "done" && "done", hot && "hot"),
|
|
1331
|
+
style: `--depth:${Math.min(m.depth, 4)}`,
|
|
1332
|
+
"data-node": m.key,
|
|
1333
|
+
role: "button",
|
|
1334
|
+
tabindex: "0",
|
|
1335
|
+
"aria-label": [m.name, statusText, m.activeLeaves.join(", ")].filter(Boolean).join(", "),
|
|
1336
|
+
title: [m.invokeid && tr.invokedAs(m.invokeid, m.invokedFrom), talk.length && tr.talksTo(talk.join(", "))]
|
|
1337
|
+
.filter(Boolean)
|
|
1338
|
+
.join("\n"),
|
|
1339
|
+
}, h("div", { class: "card-head" }, h("span", { class: "kind", "aria-hidden": "true" }, m.depth ? "⇲" : "◇"), h("span", { class: "name" }, m.name), h("span", { class: `badge ${m.status === "running" ? "run" : m.status === "done" ? "done" : "muted"}` }, statusText)), m.activeLeaves.length
|
|
1340
|
+
? h("div", { class: "leaves", title: m.activeLeaves.join(", ") }, m.activeLeaves.slice(0, compact ? 1 : 3).join(" · ") +
|
|
1341
|
+
(m.activeLeaves.length > (compact ? 1 : 3) ? ` +${m.activeLeaves.length - (compact ? 1 : 3)}` : ""))
|
|
1342
|
+
: "", !compact && m.invokeid ? h("div", { class: "from" }, tr.invokedAs(m.invokeid, m.invokedFrom)) : "", !compact && talk.length ? h("div", { class: "talks" }, ...chips(talk, 3, () => { }, true)) : "");
|
|
1343
|
+
activate(card, () => this.#navigate(() => {
|
|
1344
|
+
this.#setLevel("machine");
|
|
1345
|
+
this.#openMachine(m.key);
|
|
1346
|
+
}));
|
|
1347
|
+
return card;
|
|
1348
|
+
};
|
|
1349
|
+
const serviceCard = (svc) => {
|
|
1350
|
+
const links = t.linksOf(svc.key);
|
|
1351
|
+
let out = 0;
|
|
1352
|
+
let inn = 0;
|
|
1353
|
+
for (const l of links) {
|
|
1354
|
+
if (l.from === svc.key)
|
|
1355
|
+
inn += total(l);
|
|
1356
|
+
else
|
|
1357
|
+
out += total(l);
|
|
1358
|
+
}
|
|
1359
|
+
const hot = links.some((l) => t.isHot(l));
|
|
1360
|
+
const selected = this.#selectedService === svc.key;
|
|
1361
|
+
const card = h("div", {
|
|
1362
|
+
class: `card service${selected ? " selected" : ""}${hot ? " pulse" : ""}`,
|
|
1363
|
+
part: parts("service", selected && "selected", hot && "hot"),
|
|
1364
|
+
"data-node": svc.key,
|
|
1365
|
+
role: "button",
|
|
1366
|
+
tabindex: "0",
|
|
1367
|
+
"aria-pressed": String(selected),
|
|
1368
|
+
"aria-label": `${svc.label}, ${tr.traffic(out, inn)}`,
|
|
1369
|
+
}, h("div", { class: "card-head" }, h("span", { class: "kind", "aria-hidden": "true" }, "⇄"), h("span", { class: "name" }, svc.label), h("span", { class: "count", title: tr.trafficHint }, tr.traffic(out, inn))), h("div", { class: "type" }, svc.key), h("slot", { name: `service:${svc.label}` }));
|
|
1370
|
+
activate(card, () => {
|
|
1371
|
+
this.#selectedService = svc.key;
|
|
1372
|
+
this.#inspector = "detail";
|
|
1373
|
+
this.#schedule();
|
|
1374
|
+
});
|
|
1375
|
+
return card;
|
|
1376
|
+
};
|
|
1377
|
+
const filter = machines.length > 8
|
|
1378
|
+
? (() => {
|
|
1379
|
+
const input = h("input", {
|
|
1380
|
+
type: "search",
|
|
1381
|
+
placeholder: tr.findAmongMachines(machines.length),
|
|
1382
|
+
"aria-label": tr.findAmongMachines(machines.length),
|
|
1383
|
+
value: this.#machineFilter,
|
|
1384
|
+
});
|
|
1385
|
+
input.addEventListener("input", () => {
|
|
1386
|
+
this.#machineFilter = input.value;
|
|
1387
|
+
this.#renderNow();
|
|
1388
|
+
this.#el.centerScroll.querySelector('.search input[type="search"]')?.focus();
|
|
1389
|
+
});
|
|
1390
|
+
return h("div", { class: "search", style: "padding:0" }, input);
|
|
1391
|
+
})()
|
|
1392
|
+
: "";
|
|
1393
|
+
const sys = h("div", { class: "system", part: "system" }, h("div", { class: "col" }, h("div", { class: "col-title" }, tr.machines, h("span", { class: "count" }, String(machines.length))), filter, ...shownMachines.map(machineCard)), h("div", { class: "col" }, h("div", { class: "col-title" }, tr.services, h("span", { class: "count" }, String(services.length))), ...services.map(serviceCard)));
|
|
1394
|
+
const out = this.#el.centerScroll;
|
|
1395
|
+
const scroll = out.scrollTop;
|
|
1396
|
+
out.replaceChildren(h("h2", { class: "title", part: "title" }, tr.system), h("p", { class: "subtitle" }, tr.systemSummary(machines.length, services.length, [...t.links.values()].reduce((n, l) => n + total(l), 0))), sys);
|
|
1397
|
+
out.scrollTop = scroll;
|
|
1398
|
+
// Links: geometry only changes when the cards' layout does. Reuse the drawn SVG while the
|
|
1399
|
+
// layout key is the same (only "hot" classes change); redraw on structural change or resize.
|
|
1400
|
+
const layoutKey = JSON.stringify([
|
|
1401
|
+
compact,
|
|
1402
|
+
q,
|
|
1403
|
+
shownMachines.map((m) => [m.key, m.activeLeaves.length > 0, !compact && !!m.invokeid, !compact && talkOf(m).length > 0]),
|
|
1404
|
+
services.map((svc) => svc.key),
|
|
1405
|
+
[...t.links.values()].map((l) => `${l.from}>${l.to}:${l.kind}`),
|
|
1406
|
+
]);
|
|
1407
|
+
if (typeof ResizeObserver !== "undefined" && !this.#systemObserver) {
|
|
1408
|
+
this.#systemObserver = new ResizeObserver(() => {
|
|
1409
|
+
this.#links = undefined;
|
|
1410
|
+
if (this.#level === "system")
|
|
1411
|
+
this.#schedule();
|
|
1412
|
+
});
|
|
1413
|
+
this.#systemObserver.observe(this.#el.centerScroll);
|
|
1414
|
+
}
|
|
1415
|
+
const cached = this.#links;
|
|
1416
|
+
if (cached && cached.key === layoutKey) {
|
|
1417
|
+
for (const path of cached.svg.querySelectorAll("path[data-link]")) {
|
|
1418
|
+
const link = t.links.get(path.dataset.link);
|
|
1419
|
+
path.classList.toggle("hot", !!link && link.kind === "message" && t.isHot(link));
|
|
1420
|
+
}
|
|
1421
|
+
sys.prepend(cached.svg);
|
|
1422
|
+
}
|
|
1423
|
+
else
|
|
1424
|
+
requestAnimationFrame(() => this.#drawLinks(sys, layoutKey));
|
|
1425
|
+
}
|
|
1426
|
+
#drawLinks(sys, key) {
|
|
1427
|
+
if (!sys.isConnected || !this.#tracker)
|
|
1428
|
+
return;
|
|
1429
|
+
const base = sys.getBoundingClientRect();
|
|
1430
|
+
if (base.width < 500)
|
|
1431
|
+
return;
|
|
1432
|
+
const svg = document.createElementNS("http://www.w3.org/2000/svg", "svg");
|
|
1433
|
+
svg.setAttribute("class", "links");
|
|
1434
|
+
svg.setAttribute("aria-hidden", "true");
|
|
1435
|
+
const pos = (k) => sys.querySelector(`[data-node="${CSS.escape(k)}"]`)?.getBoundingClientRect();
|
|
1436
|
+
for (const [id, l] of this.#tracker.links) {
|
|
1437
|
+
const a = pos(l.from);
|
|
1438
|
+
const b = pos(l.to);
|
|
1439
|
+
if (!a || !b)
|
|
1440
|
+
continue;
|
|
1441
|
+
const p = document.createElementNS("http://www.w3.org/2000/svg", "path");
|
|
1442
|
+
p.dataset.link = id;
|
|
1443
|
+
if (l.kind === "invoke") {
|
|
1444
|
+
// parent → child, along the left edge
|
|
1445
|
+
const x1 = a.left - base.left + 8;
|
|
1446
|
+
const y1 = a.bottom - base.top;
|
|
1447
|
+
const x2 = b.left - base.left;
|
|
1448
|
+
const y2 = b.top + 14 - base.top;
|
|
1449
|
+
p.setAttribute("d", `M${x1},${y1} L${x1},${y2} L${x2},${y2}`);
|
|
1450
|
+
p.setAttribute("class", "invoke");
|
|
1451
|
+
}
|
|
1452
|
+
else {
|
|
1453
|
+
// machine ↔ machine messages: the invoke line and the cards' "talks to" chips already say it
|
|
1454
|
+
if (!this.#tracker.services.has(l.to) && !this.#tracker.services.has(l.from))
|
|
1455
|
+
continue;
|
|
1456
|
+
const [m, svc] = this.#tracker.services.has(l.to) ? [a, b] : [b, a];
|
|
1457
|
+
const x1 = m.right - base.left;
|
|
1458
|
+
const y1 = m.top + m.height / 2 - base.top;
|
|
1459
|
+
const x2 = svc.left - base.left;
|
|
1460
|
+
const y2 = svc.top + svc.height / 2 - base.top;
|
|
1461
|
+
const dx = (x2 - x1) / 2;
|
|
1462
|
+
p.setAttribute("d", `M${x1},${y1} C${x1 + dx},${y1} ${x2 - dx},${y2} ${x2},${y2}`);
|
|
1463
|
+
if (this.#tracker.isHot(l))
|
|
1464
|
+
p.setAttribute("class", "hot");
|
|
1465
|
+
}
|
|
1466
|
+
svg.append(p);
|
|
1467
|
+
}
|
|
1468
|
+
svg.setAttribute("width", String(sys.scrollWidth));
|
|
1469
|
+
svg.setAttribute("height", String(sys.scrollHeight));
|
|
1470
|
+
sys.querySelector(".links")?.remove();
|
|
1471
|
+
sys.prepend(svg);
|
|
1472
|
+
this.#links = { key, svg };
|
|
1473
|
+
}
|
|
1474
|
+
// ── inspector: accepted events / state detail ──
|
|
1475
|
+
#renderInspector(m) {
|
|
1476
|
+
const h = html;
|
|
1477
|
+
const t = this.#strings;
|
|
1478
|
+
const session = m.session;
|
|
1479
|
+
const focus = this.#focus;
|
|
1480
|
+
const all = session.status === "running" ? acceptedEvents(session, focus) : [];
|
|
1481
|
+
const counts = {
|
|
1482
|
+
here: all.filter((e) => e.scope === "here").length,
|
|
1483
|
+
inherited: all.filter((e) => e.scope !== "elsewhere").length,
|
|
1484
|
+
all: all.length,
|
|
1485
|
+
};
|
|
1486
|
+
this.#el.inspectorSwitch.replaceChildren(button(t.accepts(counts.inherited), () => this.#showInspector("events"), {
|
|
1487
|
+
"aria-pressed": String(this.#inspector === "events"),
|
|
1488
|
+
}), button(this.#level === "system" ? t.serviceTab : t.stateTab, () => this.#showInspector("detail"), {
|
|
1489
|
+
"aria-pressed": String(this.#inspector === "detail"),
|
|
1490
|
+
}));
|
|
1491
|
+
this.#el.eventsHead.style.display = this.#inspector === "events" ? "" : "none";
|
|
1492
|
+
this.#el.scopes.replaceChildren(...[
|
|
1493
|
+
["here", t.scopeHere(counts.here)],
|
|
1494
|
+
["inherited", t.scopeInherited(counts.inherited)],
|
|
1495
|
+
["all", t.scopeAll(counts.all)],
|
|
1496
|
+
].map(([k, text]) => button(text, () => this.#update(() => (this.#eventScope = k)), { "aria-pressed": String(this.#eventScope === k) })));
|
|
1497
|
+
const body = this.#el.inspectorBody;
|
|
1498
|
+
const scroll = body.scrollTop;
|
|
1499
|
+
if (this.#inspector === "events")
|
|
1500
|
+
body.replaceChildren(this.#eventsList(all));
|
|
1501
|
+
else if (this.#level === "system")
|
|
1502
|
+
body.replaceChildren(this.#serviceDetail());
|
|
1503
|
+
else
|
|
1504
|
+
body.replaceChildren(this.#stateDetail(this.#selected ?? focus, session));
|
|
1505
|
+
body.scrollTop = scroll;
|
|
1506
|
+
// narrow layout: the most relevant events as a strip
|
|
1507
|
+
const strip = all.filter((e) => e.scope !== "elsewhere");
|
|
1508
|
+
this.#el.strip.replaceChildren(...strip.slice(0, 8).map((e) => {
|
|
1509
|
+
const name = sendableName(e.descriptor);
|
|
1510
|
+
const c = h("button", {
|
|
1511
|
+
class: `chip${name ? "" : " muted"}`,
|
|
1512
|
+
type: "button",
|
|
1513
|
+
title: e.transitions.map((x) => `${label(x.source)} → ${x.targets.map(label).join(" ") || t.internal}`).join("\n"),
|
|
1514
|
+
...(name ? { "aria-label": t.sendEvent(name) } : { disabled: "" }),
|
|
1515
|
+
}, e.descriptor);
|
|
1516
|
+
if (name)
|
|
1517
|
+
c.addEventListener("click", () => this.#send(name));
|
|
1518
|
+
return c;
|
|
1519
|
+
}), strip.length > 8 || all.length > strip.length
|
|
1520
|
+
? button(t.allEvents(all.length), () => this.#showInspector("events"), { class: "more" })
|
|
1521
|
+
: "");
|
|
1522
|
+
}
|
|
1523
|
+
#eventsList(all) {
|
|
1524
|
+
const h = html;
|
|
1525
|
+
const t = this.#strings;
|
|
1526
|
+
const q = this.#eventFilter.trim().toLowerCase();
|
|
1527
|
+
const inScope = all.filter((e) => (this.#eventScope === "all" || (this.#eventScope === "here" ? e.scope === "here" : e.scope !== "elsewhere")) &&
|
|
1528
|
+
(!q || e.descriptor.toLowerCase().includes(q)));
|
|
1529
|
+
if (!inScope.length)
|
|
1530
|
+
return h("div", { class: "empty", part: "empty" }, h("slot", { name: "empty-events" }, all.length ? t.noEventMatches : this.#machine?.session.status === "done" ? t.machineTerminated : t.nothingAccepted));
|
|
1531
|
+
const groups = groupEvents(inScope);
|
|
1532
|
+
const autoOpen = inScope.length <= 14;
|
|
1533
|
+
return h("div", { part: "events" }, ...groups.map((g) => {
|
|
1534
|
+
const open = this.#groupOpen.get(g.prefix) ?? (autoOpen || g.events.length <= 4 || !!q);
|
|
1535
|
+
const details = h("details", { class: "ev-group", part: "event-group", ...(open ? { open: "" } : {}) }, h("summary", {}, g.prefix === "·" ? t.noPrefix : `${g.prefix}.*`, h("span", { class: "count" }, String(g.events.length))), ...g.events.map((e) => this.#eventRow(e)));
|
|
1536
|
+
details.addEventListener("toggle", () => this.#groupOpen.set(g.prefix, details.open));
|
|
1537
|
+
return details;
|
|
1538
|
+
}));
|
|
1539
|
+
}
|
|
1540
|
+
#eventRow(e) {
|
|
1541
|
+
const h = html;
|
|
1542
|
+
const t = this.#strings;
|
|
1543
|
+
const x = e.transitions[0];
|
|
1544
|
+
const name = sendableName(e.descriptor);
|
|
1545
|
+
const send = button(t.send, () => name && this.#send(name), {
|
|
1546
|
+
class: "send",
|
|
1547
|
+
part: "send",
|
|
1548
|
+
title: name ? t.sendEvent(name) : t.cannotSendWildcard,
|
|
1549
|
+
"aria-label": name ? t.sendEvent(name) : `${e.descriptor}: ${t.cannotSendWildcard}`,
|
|
1550
|
+
});
|
|
1551
|
+
if (!name)
|
|
1552
|
+
send.setAttribute("disabled", "");
|
|
1553
|
+
return h("div", { class: `ev-row scope-${e.scope}`, part: parts("event-row", e.scope) }, h("span", { class: "ev", title: e.descriptor }, e.descriptor), send, h("span", {
|
|
1554
|
+
class: "how",
|
|
1555
|
+
title: e.transitions
|
|
1556
|
+
.map((y) => `${label(y.source)}${y.guarded ? ` [${y.transition.cond}]` : ""} → ${y.targets.map(label).join(" ") || t.internal}`)
|
|
1557
|
+
.join("\n"),
|
|
1558
|
+
},
|
|
1559
|
+
// the scope in words, not only the colour of the event name
|
|
1560
|
+
e.scope !== "here" ? h("span", { class: "scope-tag" }, e.scope === "inherited" ? t.inherited : t.elsewhere) : "", `${label(x.source)} `, x.guarded ? h("span", { class: "guard" }, `[${truncate(x.transition.cond ?? "", 28)}] `) : "", `→ ${x.targets.map(label).join(", ") || t.stays}`, e.transitions.length > 1 ? ` · +${e.transitions.length - 1}` : ""), h("slot", { name: `event:${e.descriptor}` }));
|
|
1561
|
+
}
|
|
1562
|
+
#sendDescriptor(descriptor) {
|
|
1563
|
+
const name = sendableName(descriptor);
|
|
1564
|
+
if (name)
|
|
1565
|
+
this.#send(name);
|
|
1566
|
+
}
|
|
1567
|
+
#send(name) {
|
|
1568
|
+
const machine = this.#machine;
|
|
1569
|
+
const session = machine?.session;
|
|
1570
|
+
if (!session)
|
|
1571
|
+
return;
|
|
1572
|
+
let data;
|
|
1573
|
+
const raw = this.#el.eventData.value.trim();
|
|
1574
|
+
if (raw) {
|
|
1575
|
+
try {
|
|
1576
|
+
data = JSON.parse(raw);
|
|
1577
|
+
}
|
|
1578
|
+
catch {
|
|
1579
|
+
this.#el.eventData.setCustomValidity(this.#strings.invalidJson);
|
|
1580
|
+
this.#el.eventData.reportValidity();
|
|
1581
|
+
return;
|
|
1582
|
+
}
|
|
1583
|
+
}
|
|
1584
|
+
this.#el.eventData.setCustomValidity("");
|
|
1585
|
+
if (!this.#emit("scxml-send", { session, name, data }, true))
|
|
1586
|
+
return;
|
|
1587
|
+
this.#setFollow(true);
|
|
1588
|
+
if (this.#announce !== "off")
|
|
1589
|
+
this.#say(this.#strings.announceSent(name, machine.name), true);
|
|
1590
|
+
session.send(name, data);
|
|
1591
|
+
}
|
|
1592
|
+
#stateDetail(s, session) {
|
|
1593
|
+
const h = html;
|
|
1594
|
+
const t = this.#strings;
|
|
1595
|
+
const TX_PAGE = 30;
|
|
1596
|
+
const incoming = session.model.states.flatMap((x) => x.transitions.filter((tr) => tr.targets.includes(s)));
|
|
1597
|
+
const actions = (blocks, tag) => blocks.flatMap((b) => Array.from(b.children).map((a) => h("li", {}, h("b", {}, tag), describeAction(a))));
|
|
1598
|
+
const kv = (k, v) => [h("dt", {}, k), h("dd", {}, v)];
|
|
1599
|
+
const txRows = s.transitions
|
|
1600
|
+
.slice(0, TX_PAGE)
|
|
1601
|
+
.map((tr) => h("tr", {}, h("td", { class: "ev" }, tr.events.join(" ") || "ε"), h("td", { class: "cond" }, tr.cond ?? ""), h("td", { class: "to" }, tr.targets.length ? tr.targets.map(label).join(", ") : tr.type === "internal" ? t.internal : `(${t.stays})`)));
|
|
1602
|
+
return h("div", { class: "detail", part: "detail" }, h("h2", { class: "title", style: "font-size:20px" }, label(s)), h("p", { class: "subtitle" }, pathTo(s).slice(1, -1).map(label).join(" › ") || t.topLevel, session.isActiveNode(s) ? h("span", { class: "badge run" }, t.active) : ""), h("dl", { class: "kv" }, ...kv(t.detailKind, t.kind(kindOf(s))), ...kv(t.detailId, s.id), ...(s.children.length ? kv(t.detailStates, t.directAndInside(s.children.length, descendantCount(s))) : []), ...(s.initial?.targets.length ? kv(t.detailInitial, s.initial.targets.map(label).join(", ")) : []), ...(s.historyType ? kv(t.detailHistory, s.historyType) : []), ...(s.data.length ? kv(t.detailData, s.data.map((d) => d.getAttribute("id")).join(", ")) : [])), isContainer(s) && s !== this.#focus
|
|
1603
|
+
? button(t.focusState(label(s)), () => this.#focusOn(s), { class: "drill", style: "margin-top:8px" })
|
|
1604
|
+
: "",
|
|
1605
|
+
// authoring warnings about this state (model.warnings): likely mistakes that are still valid SCXML
|
|
1606
|
+
...(() => {
|
|
1607
|
+
const warnings = session.model.warnings.filter((w) => w.state === s);
|
|
1608
|
+
if (!warnings.length)
|
|
1609
|
+
return [];
|
|
1610
|
+
return [
|
|
1611
|
+
h("div", {}, h("div", { class: "section-title" }, t.warningsTitle, h("span", { class: "count" }, String(warnings.length))), h("ul", { class: "warnings" }, ...warnings.map((w) => h("li", { part: "warning", "data-code": w.code }, w.message)))),
|
|
1612
|
+
];
|
|
1613
|
+
})(), s.onentry.length || s.onexit.length
|
|
1614
|
+
? h("div", {}, h("div", { class: "section-title" }, t.actions), h("ul", { class: "actions" }, ...actions(s.onentry, t.entry), ...actions(s.onexit, t.exit)))
|
|
1615
|
+
: "", s.invokes.length
|
|
1616
|
+
? h("div", {}, h("div", { class: "section-title" }, t.invokesTitle), h("ul", { class: "actions" }, ...s.invokes.map((i) => h("li", {}, h("b", {}, i.element.getAttribute("type") ?? "scxml"), i.element.getAttribute("id") ?? i.element.getAttribute("src") ?? t.inline))))
|
|
1617
|
+
: "", s.transitions.length
|
|
1618
|
+
? h("div", {}, h("div", { class: "section-title" }, t.transitionsTitle, h("span", { class: "count" }, String(s.transitions.length))), h("table", { class: "tx" }, h("thead", {}, h("tr", {}, h("th", { scope: "col" }, t.columnEvent), h("th", { scope: "col" }, t.columnIf), h("th", { scope: "col" }, t.columnTo))), h("tbody", {}, ...txRows)), s.transitions.length > TX_PAGE ? h("p", { class: "count" }, t.more(s.transitions.length - TX_PAGE)) : "")
|
|
1619
|
+
: "", incoming.length
|
|
1620
|
+
? h("div", {}, h("div", { class: "section-title" }, t.enteredFrom, h("span", { class: "count" }, String(incoming.length))), h("div", { class: "talks" }, ...incoming.slice(0, 12).map((tr) => {
|
|
1621
|
+
const c = h("button", { class: "chip", type: "button", title: tr.events.join(" ") || "ε" }, `${label(tr.source)} ${tr.events[0] ? `(${tr.events[0]})` : ""}`);
|
|
1622
|
+
c.addEventListener("click", () => this.#focusOn(tr.source));
|
|
1623
|
+
return c;
|
|
1624
|
+
}), incoming.length > 12 ? h("span", { class: "count" }, `+${incoming.length - 12}`) : ""))
|
|
1625
|
+
: "", h("slot", { name: `detail:${s.id}` }));
|
|
1626
|
+
}
|
|
1627
|
+
#serviceDetail() {
|
|
1628
|
+
const h = html;
|
|
1629
|
+
const tr = this.#strings;
|
|
1630
|
+
const t = this.#tracker;
|
|
1631
|
+
const key = this.#selectedService;
|
|
1632
|
+
const svc = key ? t.services.get(key) : undefined;
|
|
1633
|
+
if (!svc)
|
|
1634
|
+
return h("div", { class: "empty", part: "empty" }, h("slot", { name: "empty-detail" }, tr.selectService));
|
|
1635
|
+
const traffic = svc.processor.traffic;
|
|
1636
|
+
const links = t.linksOf(svc.key);
|
|
1637
|
+
return h("div", { class: "detail", part: "detail" }, h("h2", { class: "title", style: "font-size:20px" }, svc.label), h("p", { class: "subtitle" }, svc.key), h("div", { class: "section-title" }, tr.talksWith), h("ul", { class: "actions" }, ...links.map((l) => {
|
|
1638
|
+
const other = l.from === svc.key ? l.to : l.from;
|
|
1639
|
+
const events = [...l.observed].sort((a, b) => b[1] - a[1]);
|
|
1640
|
+
return h("li", {}, h("b", {}, l.from === svc.key ? "→" : "←"), `${t.machines.get(other)?.name ?? other}: `, events
|
|
1641
|
+
.slice(0, 4)
|
|
1642
|
+
.map(([e, n]) => `${e}×${n}`)
|
|
1643
|
+
.join(", ") || [...l.declared].join(", "));
|
|
1644
|
+
})), traffic?.length
|
|
1645
|
+
? h("div", {}, h("div", { class: "section-title" }, tr.recentTraffic, h("span", { class: "count" }, String(traffic.length))), h("ul", { class: "traffic" }, ...traffic
|
|
1646
|
+
.slice(-40)
|
|
1647
|
+
.reverse()
|
|
1648
|
+
.map((x) => h("li", {}, h("span", { class: "dir", "aria-hidden": "true" }, x.direction === "out" ? "↗" : "↙"), `${x.event} · ${t.machines.get(x.sessionId)?.name ?? x.sessionId}`))))
|
|
1649
|
+
: "");
|
|
1650
|
+
}
|
|
1651
|
+
// ── tabs (narrow layout) ──
|
|
1652
|
+
#renderTabs() {
|
|
1653
|
+
const s = this.#strings;
|
|
1654
|
+
const tabs = [
|
|
1655
|
+
["system", s.tabSystem],
|
|
1656
|
+
["tree", s.tabStates],
|
|
1657
|
+
["focus", s.tabFocus],
|
|
1658
|
+
["events", s.tabEvents],
|
|
1659
|
+
["detail", s.tabDetail],
|
|
1660
|
+
];
|
|
1661
|
+
const paneFor = {
|
|
1662
|
+
system: this.#el.center,
|
|
1663
|
+
tree: this.#el.treePane,
|
|
1664
|
+
focus: this.#el.center,
|
|
1665
|
+
events: this.#el.inspector,
|
|
1666
|
+
detail: this.#el.inspector,
|
|
1667
|
+
};
|
|
1668
|
+
for (const p of [this.#el.treePane, this.#el.center, this.#el.inspector])
|
|
1669
|
+
p.toggleAttribute("data-tab-active", paneFor[this.#tab] === p);
|
|
1670
|
+
this.#el.tabs.replaceChildren(...tabs.map(([t, text]) => button(text, () => {
|
|
1671
|
+
this.#tab = t;
|
|
1672
|
+
if (t === "system")
|
|
1673
|
+
this.#level = "system";
|
|
1674
|
+
if (t === "focus")
|
|
1675
|
+
this.#level = "machine";
|
|
1676
|
+
if (t === "events" || t === "detail")
|
|
1677
|
+
this.#inspector = t;
|
|
1678
|
+
this.#renderNow();
|
|
1679
|
+
}, { role: "tab", part: parts("tab", this.#tab === t && "selected"), "aria-selected": String(this.#tab === t) })));
|
|
1680
|
+
}
|
|
1681
|
+
}
|
|
1682
|
+
// ───────────────────────────────── helpers ─────────────────────────────────
|
|
1683
|
+
const explorerSheet = lazySheet(EXPLORER_CSS);
|
|
1684
|
+
/** Make a focusable element behave like a button: click, Enter and Space all run `fn`. */
|
|
1685
|
+
function activate(el, fn) {
|
|
1686
|
+
el.addEventListener("click", fn);
|
|
1687
|
+
el.addEventListener("keydown", (e) => {
|
|
1688
|
+
if (e.target !== el || (e.key !== "Enter" && e.key !== " "))
|
|
1689
|
+
return;
|
|
1690
|
+
e.preventDefault();
|
|
1691
|
+
fn();
|
|
1692
|
+
});
|
|
1693
|
+
}
|
|
1694
|
+
/** Text only screen readers see: status in words where the screen shows colour or shape. */
|
|
1695
|
+
function srText(text) {
|
|
1696
|
+
return html("span", { class: "sr-only" }, text);
|
|
1697
|
+
}
|
|
1698
|
+
/** A `part` attribute: the base name plus modifiers that apply ("card path active"). */
|
|
1699
|
+
function parts(base, ...modifiers) {
|
|
1700
|
+
return [base, ...modifiers.filter((m) => !!m)].join(" ");
|
|
1701
|
+
}
|
|
1702
|
+
function html(tag, attrs = {}, ...children) {
|
|
1703
|
+
const el = document.createElement(tag);
|
|
1704
|
+
for (const [k, v] of Object.entries(attrs)) {
|
|
1705
|
+
if (k === "value" && "value" in el)
|
|
1706
|
+
el.value = v;
|
|
1707
|
+
// through the CSSOM, not the style attribute: a strict Content-Security-Policy (no 'unsafe-inline') allows it
|
|
1708
|
+
else if (k === "style")
|
|
1709
|
+
el.style.cssText = v;
|
|
1710
|
+
else
|
|
1711
|
+
el.setAttribute(k, v);
|
|
1712
|
+
}
|
|
1713
|
+
for (const c of children)
|
|
1714
|
+
if (c)
|
|
1715
|
+
el.append(c);
|
|
1716
|
+
return el;
|
|
1717
|
+
}
|
|
1718
|
+
function button(text, onClick, attrs = {}) {
|
|
1719
|
+
const b = html("button", { type: "button", ...attrs }, text);
|
|
1720
|
+
b.addEventListener("click", onClick);
|
|
1721
|
+
return b;
|
|
1722
|
+
}
|
|
1723
|
+
/** Event chips with an overflow counter: never more than `max` visible. */
|
|
1724
|
+
function chips(events, max, onClick, muted = false) {
|
|
1725
|
+
const out = events.slice(0, max).map((ev) => {
|
|
1726
|
+
// muted chips are labels, not controls (they sit inside other controls)
|
|
1727
|
+
const c = muted
|
|
1728
|
+
? html("span", { class: "chip muted", title: ev }, ev)
|
|
1729
|
+
: html("button", { class: "chip", type: "button", title: ev }, ev);
|
|
1730
|
+
if (!muted)
|
|
1731
|
+
c.addEventListener("click", (e) => {
|
|
1732
|
+
e.stopPropagation();
|
|
1733
|
+
onClick(ev);
|
|
1734
|
+
});
|
|
1735
|
+
return c;
|
|
1736
|
+
});
|
|
1737
|
+
if (events.length > max)
|
|
1738
|
+
out.push(html("span", { class: "count", title: events.slice(max).join("\n") }, `+${events.length - max}`));
|
|
1739
|
+
return out;
|
|
1740
|
+
}
|
|
1741
|
+
/** Number of layers the diagram would need (longest shortest-path from the initial child, +1 for unreachable ones). */
|
|
1742
|
+
function estimateRanks(children, edges) {
|
|
1743
|
+
const rank = new Map();
|
|
1744
|
+
const queue = children.filter((c) => c.initial).map((c) => c.node);
|
|
1745
|
+
if (!queue.length && children[0])
|
|
1746
|
+
queue.push(children[0].node);
|
|
1747
|
+
for (const q of queue)
|
|
1748
|
+
rank.set(q, 0);
|
|
1749
|
+
for (let i = 0; i < queue.length; i++) {
|
|
1750
|
+
const n = queue[i];
|
|
1751
|
+
for (const e of edges) {
|
|
1752
|
+
if (e.from !== n || rank.has(e.to))
|
|
1753
|
+
continue;
|
|
1754
|
+
rank.set(e.to, rank.get(n) + 1);
|
|
1755
|
+
queue.push(e.to);
|
|
1756
|
+
}
|
|
1757
|
+
}
|
|
1758
|
+
const max = Math.max(0, ...rank.values()) + 1;
|
|
1759
|
+
return rank.size < children.length ? max + 1 : max;
|
|
1760
|
+
}
|
|
1761
|
+
function formatTime(ms) {
|
|
1762
|
+
const s = ms / 1000;
|
|
1763
|
+
return s < 60 ? `${s.toFixed(1)} s` : `${Math.floor(s / 60)}:${String(Math.floor(s % 60)).padStart(2, "0")}`;
|
|
1764
|
+
}
|
|
1765
|
+
function truncate(s, n) {
|
|
1766
|
+
return s.length > n ? `${s.slice(0, n - 1)}…` : s;
|
|
1767
|
+
}
|
|
1768
|
+
function describeAction(a) {
|
|
1769
|
+
const at = (n) => a.getAttribute(n);
|
|
1770
|
+
switch (a.localName) {
|
|
1771
|
+
case "send":
|
|
1772
|
+
return `send ${at("type") && at("type") !== "scxml" ? `${at("type")}:` : ""}${at("event") ?? at("eventexpr") ?? ""}${at("target") ? ` → ${at("target")}` : ""}${at("delay") || at("delayexpr") ? " ⏱" : ""}`;
|
|
1773
|
+
case "log":
|
|
1774
|
+
return `log ${at("label") ?? ""}`;
|
|
1775
|
+
case "assign":
|
|
1776
|
+
return `${at("location")} ← ${truncate(at("expr") ?? "…", 30)}`;
|
|
1777
|
+
case "raise":
|
|
1778
|
+
return `raise ${at("event")}`;
|
|
1779
|
+
case "cancel":
|
|
1780
|
+
return `cancel ${at("sendid") ?? ""}`;
|
|
1781
|
+
case "if":
|
|
1782
|
+
return `if ${truncate(at("cond") ?? "", 30)} …`;
|
|
1783
|
+
case "foreach":
|
|
1784
|
+
return `foreach ${at("item")} in ${at("array")}`;
|
|
1785
|
+
default:
|
|
1786
|
+
return a.localName;
|
|
1787
|
+
}
|
|
1788
|
+
}
|
|
1789
|
+
if (typeof customElements !== "undefined" && !customElements.get("scxml-explorer"))
|
|
1790
|
+
customElements.define("scxml-explorer", ScxmlExplorer);
|
|
1791
|
+
//# sourceMappingURL=element.js.map
|