@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,1045 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `<scxml-view>` — a whole statechart at once, drop-in.
|
|
3
|
+
*
|
|
4
|
+
* <scxml-view src="traffic-light.scxml"></scxml-view>
|
|
5
|
+
*
|
|
6
|
+
* <scxml-view>
|
|
7
|
+
* <script type="application/scxml+xml"> <scxml …> … </scxml> </script>
|
|
8
|
+
* </scxml-view>
|
|
9
|
+
*
|
|
10
|
+
* Without any JavaScript it loads the chart, compiles and validates it, runs
|
|
11
|
+
* it and draws it: nested boxes, orthogonal transitions, the active
|
|
12
|
+
* configuration highlighted, transitions that just fired lit up. Transition
|
|
13
|
+
* labels are buttons that send their event.
|
|
14
|
+
*
|
|
15
|
+
* The data model: SANDBOXED by default (QuickJS, safe for any source), or the
|
|
16
|
+
* host's engine with the `trusted` attribute (no WebAssembly download — only
|
|
17
|
+
* for charts you trust). Both are loaded on demand with `import()`, so a page
|
|
18
|
+
* that hands the element its own `session` downloads neither.
|
|
19
|
+
*
|
|
20
|
+
* Attributes: `src`, `trusted`, `autostart` ("false" = draw without running),
|
|
21
|
+
* `interactive` ("false" = labels aren't buttons), `data` (JSON initial values
|
|
22
|
+
* for `<data>`), `direction` ("auto" | "right" | "down"), `max-states` (fold
|
|
23
|
+
* large charts beyond this many boxes, default 150), `announce`
|
|
24
|
+
* ("all" | "sends" | "off"), `fit` (scale down to the element's width),
|
|
25
|
+
* `warnings` ("off" hides the list of authoring warnings — see `model.warnings`).
|
|
26
|
+
*
|
|
27
|
+
* Properties: `session` (a host-created session; the element then creates,
|
|
28
|
+
* starts and disposes nothing), `options` (extra SessionOptions for sessions
|
|
29
|
+
* the element creates: I/O processors, invokers, loader…), `clock` (e.g. a
|
|
30
|
+
* PlaybackClock: shows play / pause / step controls), `strings`, `source`
|
|
31
|
+
* (SCXML text instead of `src`), `model`, and `reload()`.
|
|
32
|
+
*
|
|
33
|
+
* Events (bubbling, composed): `scxml-load` { session, model },
|
|
34
|
+
* `scxml-error` { message, problems?, error }, `scxml-send` { session, name }
|
|
35
|
+
* (cancelable; fired before a label sends its event).
|
|
36
|
+
*
|
|
37
|
+
* Styling: the shared `--scxml-*` tokens and these parts — `frame`, `canvas`,
|
|
38
|
+
* `state` (+ `atomic` `compound` `parallel` `parallel-region` `final`
|
|
39
|
+
* `history` `collapsed` `active`), `state-name`, `transition` / `edge-label`
|
|
40
|
+
* (+ `live` `fired`), `event`, `cond`, `internal`, `edge` (+ `live` `fired`),
|
|
41
|
+
* `initial`, `expand`, `collapse`, `notice`, `error`, `controls`, `play`,
|
|
42
|
+
* `step`, `speeds`, `clock`, `warnings`, `warning`.
|
|
43
|
+
*/
|
|
44
|
+
import { PlaybackClock } from "../clock.js";
|
|
45
|
+
import { compile, isCompound } from "../model.js";
|
|
46
|
+
import { parseSCXML, SCXMLSession } from "../session.js";
|
|
47
|
+
import { Announcer } from "../ui/announcer.js";
|
|
48
|
+
import { arrowMarker } from "../ui/svg.js";
|
|
49
|
+
import { lazySheet, NARROW_WIDTH, themeSheet } from "../ui/theme.js";
|
|
50
|
+
import { autoCollapse, layoutChart } from "./layout.js";
|
|
51
|
+
import { defaultViewStrings } from "./strings.js";
|
|
52
|
+
import { VIEW_CSS } from "./styles.js";
|
|
53
|
+
const SCXML_MIME = "application/scxml+xml";
|
|
54
|
+
/** Default floor for scaling a too-wide diagram down (without `fit`); see `--scxml-min-scale`. */
|
|
55
|
+
const DEFAULT_MIN_SCALE = 0.65;
|
|
56
|
+
const FIRED_MS = 900;
|
|
57
|
+
const DEFAULT_MAX_STATES = 150;
|
|
58
|
+
const MAX_INTERNAL_SHOWN = 3;
|
|
59
|
+
const SPEEDS = [
|
|
60
|
+
[0.25, "¼×"],
|
|
61
|
+
[0.5, "½×"],
|
|
62
|
+
[1, "1×"],
|
|
63
|
+
[2, "2×"],
|
|
64
|
+
[4, "4×"],
|
|
65
|
+
];
|
|
66
|
+
// importing this module where there is no DOM (server-side rendering, Node) must work
|
|
67
|
+
const ElementBase = typeof HTMLElement === "undefined" ? class {
|
|
68
|
+
} : HTMLElement;
|
|
69
|
+
const sheet = lazySheet(VIEW_CSS);
|
|
70
|
+
/** Load a data model engine on demand (dynamic import: never in the element's own bundle). */
|
|
71
|
+
async function engine(trusted) {
|
|
72
|
+
if (trusted)
|
|
73
|
+
return (await import("../datamodel-trusted.js")).trustedDataModel;
|
|
74
|
+
const quickjs = await import("../datamodel-quickjs.js");
|
|
75
|
+
await quickjs.loadQuickJS();
|
|
76
|
+
return quickjs.sandboxedDataModel;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* `<scxml-view>`: a whole statechart, drawn and running, with no JavaScript
|
|
80
|
+
* of your own. Registered by importing `@tinyactors/scxmljs/view`.
|
|
81
|
+
*
|
|
82
|
+
* @example
|
|
83
|
+
* ```html
|
|
84
|
+
* <script type="module">import "@tinyactors/scxmljs/view";</script>
|
|
85
|
+
* <scxml-view src="traffic-light.scxml"></scxml-view>
|
|
86
|
+
* ```
|
|
87
|
+
*/
|
|
88
|
+
export class ScxmlView extends ElementBase {
|
|
89
|
+
/** Attributes the element reacts to. */
|
|
90
|
+
static observedAttributes = ["src", "trusted", "autostart", "interactive", "data", "direction", "max-states", "fit", "warnings"];
|
|
91
|
+
#shadow;
|
|
92
|
+
#el;
|
|
93
|
+
// what's shown
|
|
94
|
+
#session;
|
|
95
|
+
#owned = false;
|
|
96
|
+
#model;
|
|
97
|
+
#source;
|
|
98
|
+
#options = {};
|
|
99
|
+
#clock;
|
|
100
|
+
#strings = defaultViewStrings;
|
|
101
|
+
#generation = 0;
|
|
102
|
+
#loadQueued = false;
|
|
103
|
+
#loadForced = false;
|
|
104
|
+
// layout state
|
|
105
|
+
#layout;
|
|
106
|
+
#direction = "right";
|
|
107
|
+
#collapsed = new Set();
|
|
108
|
+
#foldsFor;
|
|
109
|
+
#boxes = new Map();
|
|
110
|
+
#labels = new Map();
|
|
111
|
+
#paths = new Map();
|
|
112
|
+
#chips = new Map();
|
|
113
|
+
// live state
|
|
114
|
+
#fired = new Map();
|
|
115
|
+
#entered = [];
|
|
116
|
+
/** The configuration as of the last microstep; still known after the session terminates. */
|
|
117
|
+
#last = new Set();
|
|
118
|
+
#steps = 0;
|
|
119
|
+
#unwire = [];
|
|
120
|
+
#announcer;
|
|
121
|
+
#ro;
|
|
122
|
+
#raf = 0;
|
|
123
|
+
#decayRaf = 0;
|
|
124
|
+
#width = 0;
|
|
125
|
+
constructor() {
|
|
126
|
+
super();
|
|
127
|
+
if (typeof document === "undefined")
|
|
128
|
+
return;
|
|
129
|
+
this.#shadow = this.attachShadow({ mode: "open" });
|
|
130
|
+
this.#shadow.adoptedStyleSheets = [themeSheet(), sheet()];
|
|
131
|
+
this.#buildShell();
|
|
132
|
+
}
|
|
133
|
+
// ─────────────────────────────── attributes ───────────────────────────────
|
|
134
|
+
/** The chart's URL (the `src` attribute). Relative `<script src>` / `<data src>` / `<invoke src>` resolve against it. */
|
|
135
|
+
get src() {
|
|
136
|
+
return this.getAttribute("src") ?? "";
|
|
137
|
+
}
|
|
138
|
+
/** Sets the `src` attribute (loads the chart). */
|
|
139
|
+
set src(v) {
|
|
140
|
+
this.setAttribute("src", v);
|
|
141
|
+
}
|
|
142
|
+
/** Run the chart in the host's JS engine instead of the QuickJS sandbox (no WebAssembly; trusted charts only). */
|
|
143
|
+
get trusted() {
|
|
144
|
+
return this.hasAttribute("trusted");
|
|
145
|
+
}
|
|
146
|
+
/** Toggles the `trusted` attribute (reloads). */
|
|
147
|
+
set trusted(v) {
|
|
148
|
+
this.toggleAttribute("trusted", v);
|
|
149
|
+
}
|
|
150
|
+
/** Start the session the element creates (default true; `autostart="false"` only draws the chart). */
|
|
151
|
+
get autostart() {
|
|
152
|
+
return this.getAttribute("autostart") !== "false";
|
|
153
|
+
}
|
|
154
|
+
/** Sets the `autostart` attribute. */
|
|
155
|
+
set autostart(v) {
|
|
156
|
+
this.setAttribute("autostart", String(v));
|
|
157
|
+
}
|
|
158
|
+
/** Transition labels are buttons that send their event (default true). */
|
|
159
|
+
get interactive() {
|
|
160
|
+
return this.getAttribute("interactive") !== "false";
|
|
161
|
+
}
|
|
162
|
+
/** Sets the `interactive` attribute. */
|
|
163
|
+
set interactive(v) {
|
|
164
|
+
this.setAttribute("interactive", String(v));
|
|
165
|
+
}
|
|
166
|
+
/** "right" (layers left to right), "down", or "auto" (down when narrower than the shared breakpoint). */
|
|
167
|
+
get direction() {
|
|
168
|
+
const d = this.getAttribute("direction");
|
|
169
|
+
return d === "right" || d === "down" ? d : "auto";
|
|
170
|
+
}
|
|
171
|
+
/** Sets the `direction` attribute (re-lays out). */
|
|
172
|
+
set direction(v) {
|
|
173
|
+
this.setAttribute("direction", v);
|
|
174
|
+
}
|
|
175
|
+
/** Fold the largest compound states until at most this many boxes are drawn (default 150). */
|
|
176
|
+
get maxStates() {
|
|
177
|
+
const n = Number(this.getAttribute("max-states"));
|
|
178
|
+
return Number.isFinite(n) && n > 0 ? n : DEFAULT_MAX_STATES;
|
|
179
|
+
}
|
|
180
|
+
/** Sets the `max-states` attribute (folds again). */
|
|
181
|
+
set maxStates(n) {
|
|
182
|
+
this.setAttribute("max-states", String(n));
|
|
183
|
+
}
|
|
184
|
+
/** What the polite live region announces: "all" (steps and sent events, default), "sends" or "off". */
|
|
185
|
+
get announce() {
|
|
186
|
+
const a = this.getAttribute("announce");
|
|
187
|
+
return a === "off" || a === "sends" ? a : "all";
|
|
188
|
+
}
|
|
189
|
+
/** Sets the `announce` attribute. */
|
|
190
|
+
set announce(v) {
|
|
191
|
+
this.setAttribute("announce", v);
|
|
192
|
+
}
|
|
193
|
+
/** "show" (default) lists the chart's authoring warnings (`model.warnings`) above the diagram; "off" hides them. */
|
|
194
|
+
get warnings() {
|
|
195
|
+
return this.getAttribute("warnings") === "off" ? "off" : "show";
|
|
196
|
+
}
|
|
197
|
+
/** Sets the `warnings` attribute. */
|
|
198
|
+
set warnings(v) {
|
|
199
|
+
this.setAttribute("warnings", v);
|
|
200
|
+
}
|
|
201
|
+
/** Scale the diagram down to the element's width. */
|
|
202
|
+
get fit() {
|
|
203
|
+
return this.hasAttribute("fit");
|
|
204
|
+
}
|
|
205
|
+
/** Toggles the `fit` attribute. */
|
|
206
|
+
set fit(v) {
|
|
207
|
+
this.toggleAttribute("fit", v);
|
|
208
|
+
}
|
|
209
|
+
// ─────────────────────────────── properties ───────────────────────────────
|
|
210
|
+
/** The session shown. Setting one hands the element a host-owned session (it won't be started or disposed). */
|
|
211
|
+
get session() {
|
|
212
|
+
return this.#session;
|
|
213
|
+
}
|
|
214
|
+
/** Show your own session (the element never starts or disposes it); `undefined` goes back to `src` / inline source. */
|
|
215
|
+
set session(s) {
|
|
216
|
+
this.#generation++;
|
|
217
|
+
this.#teardownSession();
|
|
218
|
+
if (s)
|
|
219
|
+
this.#attach(s, false);
|
|
220
|
+
else
|
|
221
|
+
this.#scheduleLoad();
|
|
222
|
+
}
|
|
223
|
+
/** The compiled chart shown. */
|
|
224
|
+
get model() {
|
|
225
|
+
return this.#model;
|
|
226
|
+
}
|
|
227
|
+
/** Extra options for sessions the element creates (I/O processors, invokers, loader, data model…). Setting them reloads. */
|
|
228
|
+
get options() {
|
|
229
|
+
return this.#options;
|
|
230
|
+
}
|
|
231
|
+
/** Options for sessions the element creates; reloads unless a host session is shown. */
|
|
232
|
+
set options(o) {
|
|
233
|
+
this.#options = o ?? {};
|
|
234
|
+
if (this.#owned || !this.#session)
|
|
235
|
+
this.#scheduleLoad();
|
|
236
|
+
}
|
|
237
|
+
/** The clock for sessions the element creates; a PlaybackClock adds play / pause / step controls. Setting it reloads. */
|
|
238
|
+
get clock() {
|
|
239
|
+
return this.#clock;
|
|
240
|
+
}
|
|
241
|
+
/** The clock for sessions the element creates; reloads unless a host session is shown. */
|
|
242
|
+
set clock(c) {
|
|
243
|
+
this.#clock = c;
|
|
244
|
+
if (this.#owned || !this.#session)
|
|
245
|
+
this.#scheduleLoad();
|
|
246
|
+
}
|
|
247
|
+
/** SCXML text to show instead of `src` / inline source. */
|
|
248
|
+
get source() {
|
|
249
|
+
return this.#source;
|
|
250
|
+
}
|
|
251
|
+
/** Show this SCXML text (reloads). */
|
|
252
|
+
set source(text) {
|
|
253
|
+
this.#source = text;
|
|
254
|
+
this.#scheduleLoad();
|
|
255
|
+
}
|
|
256
|
+
/** Translations: a partial override of the English defaults. */
|
|
257
|
+
get strings() {
|
|
258
|
+
return this.#strings;
|
|
259
|
+
}
|
|
260
|
+
/** Translate: a partial object; unset keys keep their English default. */
|
|
261
|
+
set strings(s) {
|
|
262
|
+
this.#strings = { ...defaultViewStrings, ...s, kinds: { ...defaultViewStrings.kinds, ...s?.kinds } };
|
|
263
|
+
this.#relayout();
|
|
264
|
+
}
|
|
265
|
+
/** Load the chart again (re-reading `src` / inline source) and start a fresh session. */
|
|
266
|
+
reload() {
|
|
267
|
+
this.#scheduleLoad(true);
|
|
268
|
+
}
|
|
269
|
+
// ─────────────────────────────── lifecycle ───────────────────────────────
|
|
270
|
+
/** Custom-element lifecycle (called by the browser): loads the chart when first connected. */
|
|
271
|
+
connectedCallback() {
|
|
272
|
+
if (typeof ResizeObserver !== "undefined" && !this.#ro) {
|
|
273
|
+
this.#ro = new ResizeObserver(() => this.#onResize());
|
|
274
|
+
this.#ro.observe(this);
|
|
275
|
+
}
|
|
276
|
+
if (this.#session && !this.#owned)
|
|
277
|
+
this.#relayout();
|
|
278
|
+
else if (!this.#session)
|
|
279
|
+
this.#scheduleLoad();
|
|
280
|
+
}
|
|
281
|
+
/** Custom-element lifecycle (called by the browser): disposes an element-owned session unless the element was only moved. */
|
|
282
|
+
disconnectedCallback() {
|
|
283
|
+
this.#ro?.disconnect();
|
|
284
|
+
this.#ro = undefined;
|
|
285
|
+
// an element moved within the page is re-connected in the same task: keep its session then
|
|
286
|
+
queueMicrotask(() => {
|
|
287
|
+
if (this.isConnected)
|
|
288
|
+
return;
|
|
289
|
+
this.#generation++;
|
|
290
|
+
this.#teardownSession();
|
|
291
|
+
this.#announcer?.dispose();
|
|
292
|
+
});
|
|
293
|
+
}
|
|
294
|
+
/** Custom-element lifecycle (called by the browser) for the observed attributes. */
|
|
295
|
+
attributeChangedCallback(name, old, value) {
|
|
296
|
+
if (old === value || !this.isConnected)
|
|
297
|
+
return;
|
|
298
|
+
switch (name) {
|
|
299
|
+
case "src":
|
|
300
|
+
case "trusted":
|
|
301
|
+
case "data":
|
|
302
|
+
case "autostart":
|
|
303
|
+
if (this.#owned || !this.#session)
|
|
304
|
+
this.#scheduleLoad();
|
|
305
|
+
break;
|
|
306
|
+
case "max-states":
|
|
307
|
+
this.#foldsFor = undefined;
|
|
308
|
+
this.#relayout();
|
|
309
|
+
break;
|
|
310
|
+
default:
|
|
311
|
+
this.#relayout();
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
#scheduleLoad(force = false) {
|
|
315
|
+
if (!this.isConnected || (this.#session && !this.#owned && !force))
|
|
316
|
+
return;
|
|
317
|
+
this.#loadForced ||= force;
|
|
318
|
+
if (this.#loadQueued)
|
|
319
|
+
return;
|
|
320
|
+
this.#loadQueued = true;
|
|
321
|
+
// properties set right after creation (options, clock…) still apply to this load
|
|
322
|
+
queueMicrotask(() => {
|
|
323
|
+
const forced = this.#loadForced;
|
|
324
|
+
this.#loadQueued = false;
|
|
325
|
+
this.#loadForced = false;
|
|
326
|
+
if (!this.isConnected)
|
|
327
|
+
return;
|
|
328
|
+
// a host session assigned after the load was queued (append, then set `session`) wins
|
|
329
|
+
if (!forced && this.#session && !this.#owned)
|
|
330
|
+
return;
|
|
331
|
+
void this.#load();
|
|
332
|
+
});
|
|
333
|
+
}
|
|
334
|
+
async #load() {
|
|
335
|
+
const generation = ++this.#generation;
|
|
336
|
+
this.#teardownSession();
|
|
337
|
+
this.#showMessage("loading");
|
|
338
|
+
try {
|
|
339
|
+
const source = await this.#readSource();
|
|
340
|
+
if (generation !== this.#generation)
|
|
341
|
+
return;
|
|
342
|
+
if (!source) {
|
|
343
|
+
this.#el.diagram.replaceChildren(h("p", { class: "empty", part: "empty" }, this.#strings.noSource));
|
|
344
|
+
return;
|
|
345
|
+
}
|
|
346
|
+
const { text, base } = source;
|
|
347
|
+
const opts = this.#options;
|
|
348
|
+
const root = parseSCXML(text, opts.domParser);
|
|
349
|
+
const loader = opts.loader ?? ((src) => fetchText(new URL(src.replace(/^file:/, ""), base).href));
|
|
350
|
+
const model = await compile(root, { loader });
|
|
351
|
+
const datamodel = opts.datamodel ?? (await engine(this.trusted));
|
|
352
|
+
if (generation !== this.#generation)
|
|
353
|
+
return;
|
|
354
|
+
const data = { ...this.#dataAttribute(), ...opts.data };
|
|
355
|
+
const session = new SCXMLSession(model, { ...opts, loader, datamodel, data, clock: this.#clock });
|
|
356
|
+
this.#attach(session, true);
|
|
357
|
+
this.#emit("scxml-load", { session, model });
|
|
358
|
+
if (this.autostart)
|
|
359
|
+
session.start();
|
|
360
|
+
this.#renderState();
|
|
361
|
+
}
|
|
362
|
+
catch (error) {
|
|
363
|
+
if (generation === this.#generation)
|
|
364
|
+
this.#fail(error);
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
async #readSource() {
|
|
368
|
+
const base = typeof document !== "undefined" ? document.baseURI : "";
|
|
369
|
+
if (this.#source != null)
|
|
370
|
+
return { text: this.#source, base };
|
|
371
|
+
const src = this.getAttribute("src");
|
|
372
|
+
if (src) {
|
|
373
|
+
const url = new URL(src, base).href;
|
|
374
|
+
return { text: await fetchText(url), base: url };
|
|
375
|
+
}
|
|
376
|
+
const inline = () => this.querySelector(`script[type="${SCXML_MIME}"]`)?.textContent;
|
|
377
|
+
let text = inline();
|
|
378
|
+
// while the parser is still inside <scxml-view>, its children may not exist yet
|
|
379
|
+
if (text == null && document.readyState === "loading") {
|
|
380
|
+
await new Promise((r) => document.addEventListener("DOMContentLoaded", r, { once: true }));
|
|
381
|
+
text = inline();
|
|
382
|
+
}
|
|
383
|
+
// nothing to show (yet): a host that sets `session` later is the normal framework flow,
|
|
384
|
+
// so this is a quiet hint in the frame, not an error event
|
|
385
|
+
if (text == null || !text.trim())
|
|
386
|
+
return null;
|
|
387
|
+
return { text, base };
|
|
388
|
+
}
|
|
389
|
+
#dataAttribute() {
|
|
390
|
+
const raw = this.getAttribute("data");
|
|
391
|
+
if (!raw)
|
|
392
|
+
return {};
|
|
393
|
+
try {
|
|
394
|
+
const v = JSON.parse(raw);
|
|
395
|
+
return v && typeof v === "object" ? v : {};
|
|
396
|
+
}
|
|
397
|
+
catch {
|
|
398
|
+
throw new ViewSourceError(`The data attribute is not valid JSON: ${raw}`);
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
#attach(session, owned) {
|
|
402
|
+
this.#session = session;
|
|
403
|
+
this.#owned = owned;
|
|
404
|
+
this.#model = session.model;
|
|
405
|
+
this.#fired.clear();
|
|
406
|
+
this.#steps = 0;
|
|
407
|
+
this.#last = new Set(session.configuration);
|
|
408
|
+
const onMicro = (e) => {
|
|
409
|
+
const now = session.clock.now();
|
|
410
|
+
for (const t of e.transitions)
|
|
411
|
+
if (t.element)
|
|
412
|
+
this.#fired.set(t, now);
|
|
413
|
+
this.#entered.push(...e.entered);
|
|
414
|
+
for (const s of e.exited)
|
|
415
|
+
this.#last.delete(s);
|
|
416
|
+
for (const s of e.entered)
|
|
417
|
+
this.#last.add(s);
|
|
418
|
+
};
|
|
419
|
+
const onMacro = (e) => {
|
|
420
|
+
this.#steps++;
|
|
421
|
+
const entered = this.#entered.filter((s) => !s.children.length).map((s) => (s.generatedId ? s.kind : s.id));
|
|
422
|
+
this.#entered = [];
|
|
423
|
+
if (this.announce === "all" && e.event)
|
|
424
|
+
this.#say(this.#strings.announceStep(e.event.name, entered));
|
|
425
|
+
this.#renderState();
|
|
426
|
+
};
|
|
427
|
+
const onDone = () => this.#renderState();
|
|
428
|
+
session.addEventListener("microstep", onMicro);
|
|
429
|
+
session.addEventListener("macrostep", onMacro);
|
|
430
|
+
session.addEventListener("done", onDone);
|
|
431
|
+
this.#unwire.push(() => {
|
|
432
|
+
session.removeEventListener("microstep", onMicro);
|
|
433
|
+
session.removeEventListener("macrostep", onMacro);
|
|
434
|
+
session.removeEventListener("done", onDone);
|
|
435
|
+
});
|
|
436
|
+
const clock = session.clock;
|
|
437
|
+
if (clock instanceof PlaybackClock)
|
|
438
|
+
this.#unwire.push(clock.subscribe(() => this.#renderState()));
|
|
439
|
+
this.#el.error.hidden = true;
|
|
440
|
+
this.#relayout();
|
|
441
|
+
}
|
|
442
|
+
#teardownSession() {
|
|
443
|
+
for (const f of this.#unwire.splice(0))
|
|
444
|
+
f();
|
|
445
|
+
cancelAnimationFrame(this.#decayRaf);
|
|
446
|
+
if (this.#owned)
|
|
447
|
+
this.#session?.dispose();
|
|
448
|
+
this.#session = undefined;
|
|
449
|
+
this.#owned = false;
|
|
450
|
+
this.#model = undefined;
|
|
451
|
+
this.#layout = undefined;
|
|
452
|
+
}
|
|
453
|
+
#fail(error) {
|
|
454
|
+
this.#teardownSession();
|
|
455
|
+
const problems = error?.problems;
|
|
456
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
457
|
+
const e = this.#el;
|
|
458
|
+
e.diagram.replaceChildren();
|
|
459
|
+
e.controls.hidden = true;
|
|
460
|
+
e.notice.hidden = true;
|
|
461
|
+
e.error.hidden = false;
|
|
462
|
+
e.error.replaceChildren(h("h2", {}, this.#strings.errorTitle), problems?.length ? h("ul", {}, ...problems.map((p) => h("li", {}, p))) : h("p", {}, message));
|
|
463
|
+
this.#emit("scxml-error", { message, problems, error });
|
|
464
|
+
}
|
|
465
|
+
#showMessage(kind) {
|
|
466
|
+
this.#el.error.hidden = true;
|
|
467
|
+
this.#el.diagram.replaceChildren(h("div", { class: "loading" }, kind === "loading" ? this.#strings.loading : ""));
|
|
468
|
+
}
|
|
469
|
+
#say(text, urgent = false) {
|
|
470
|
+
if (this.announce === "off")
|
|
471
|
+
return;
|
|
472
|
+
this.#announcer ??= new Announcer(this.#el.live, {
|
|
473
|
+
interval: () => 3000,
|
|
474
|
+
paused: () => {
|
|
475
|
+
const c = this.#session?.clock;
|
|
476
|
+
return c instanceof PlaybackClock ? !c.playing : false;
|
|
477
|
+
},
|
|
478
|
+
});
|
|
479
|
+
this.#announcer.say(text, urgent);
|
|
480
|
+
}
|
|
481
|
+
#emit(type, detail, cancelable = false) {
|
|
482
|
+
return this.dispatchEvent(new CustomEvent(type, { detail, bubbles: true, composed: true, cancelable }));
|
|
483
|
+
}
|
|
484
|
+
// ───────────────────────────────── shell ─────────────────────────────────
|
|
485
|
+
#buildShell() {
|
|
486
|
+
const t = this.#strings;
|
|
487
|
+
const play = h("button", { class: "play", type: "button", part: "play", title: t.playHint });
|
|
488
|
+
const step = h("button", { class: "step", type: "button", part: "step", title: t.stepHint }, t.step);
|
|
489
|
+
const speeds = h("div", { class: "speeds", role: "group", part: "speeds", "aria-label": t.speedLabel });
|
|
490
|
+
const clock = h("span", { class: "clock", part: "clock" });
|
|
491
|
+
play.addEventListener("click", () => this.#playback()?.toggle());
|
|
492
|
+
step.addEventListener("click", () => this.#step());
|
|
493
|
+
const controls = h("div", { class: "controls", part: "controls", hidden: "" }, play, step, speeds, clock);
|
|
494
|
+
const notice = h("div", { class: "notice", part: "notice", hidden: "" });
|
|
495
|
+
const warnings = h("details", { class: "warnings", part: "warnings", hidden: "" });
|
|
496
|
+
const error = h("div", { class: "error", part: "error", role: "alert", hidden: "" });
|
|
497
|
+
const diagram = h("div", { class: "diagram", role: "group" });
|
|
498
|
+
// focusable: a scroll container must be keyboard-scrollable, and it takes the playback shortcuts
|
|
499
|
+
const canvas = h("div", { class: "canvas", part: "canvas", tabindex: "0" }, diagram);
|
|
500
|
+
const live = h("div", { class: "sr-only", role: "status", "aria-live": "polite", "aria-atomic": "true" });
|
|
501
|
+
const frame = h("div", { class: "frame", part: "frame" }, controls, notice, warnings, error, canvas, live);
|
|
502
|
+
this.#shadow.append(frame);
|
|
503
|
+
this.#el = { frame, controls, play, step, speeds, clock, notice, warnings, error, canvas, diagram, live };
|
|
504
|
+
this.addEventListener("keydown", (e) => {
|
|
505
|
+
const clockNow = this.#playback();
|
|
506
|
+
const target = e.composedPath()[0];
|
|
507
|
+
if (!clockNow || /^(INPUT|TEXTAREA|SELECT|BUTTON)$/.test(target?.tagName ?? ""))
|
|
508
|
+
return;
|
|
509
|
+
if (e.key === " ") {
|
|
510
|
+
e.preventDefault();
|
|
511
|
+
clockNow.toggle();
|
|
512
|
+
}
|
|
513
|
+
else if (e.key === ".") {
|
|
514
|
+
e.preventDefault();
|
|
515
|
+
this.#step();
|
|
516
|
+
}
|
|
517
|
+
});
|
|
518
|
+
}
|
|
519
|
+
#playback() {
|
|
520
|
+
const c = this.#session?.clock;
|
|
521
|
+
return c instanceof PlaybackClock ? c : undefined;
|
|
522
|
+
}
|
|
523
|
+
#step() {
|
|
524
|
+
const clock = this.#playback();
|
|
525
|
+
if (!clock)
|
|
526
|
+
return;
|
|
527
|
+
const before = this.#steps;
|
|
528
|
+
clock.step(() => this.#steps > before);
|
|
529
|
+
this.#renderState();
|
|
530
|
+
}
|
|
531
|
+
#onResize() {
|
|
532
|
+
const w = this.getBoundingClientRect().width;
|
|
533
|
+
const was = this.#width;
|
|
534
|
+
this.#width = w;
|
|
535
|
+
if (!this.#layout)
|
|
536
|
+
return;
|
|
537
|
+
const crossed = was === 0 || was <= NARROW_WIDTH !== w <= NARROW_WIDTH;
|
|
538
|
+
if (this.direction === "auto" && crossed)
|
|
539
|
+
this.#relayout();
|
|
540
|
+
else
|
|
541
|
+
this.#applyFit();
|
|
542
|
+
}
|
|
543
|
+
// ──────────────────────────────── rendering ────────────────────────────────
|
|
544
|
+
#relayout() {
|
|
545
|
+
if (!this.#model || this.#raf)
|
|
546
|
+
return;
|
|
547
|
+
if (typeof requestAnimationFrame === "undefined")
|
|
548
|
+
return this.#renderAll();
|
|
549
|
+
this.#raf = requestAnimationFrame(() => {
|
|
550
|
+
this.#raf = 0;
|
|
551
|
+
this.#renderAll();
|
|
552
|
+
});
|
|
553
|
+
}
|
|
554
|
+
/** Measure every box and label, lay the chart out, and draw it. */
|
|
555
|
+
#renderAll() {
|
|
556
|
+
if (this.#raf) {
|
|
557
|
+
cancelAnimationFrame(this.#raf);
|
|
558
|
+
this.#raf = 0;
|
|
559
|
+
}
|
|
560
|
+
const model = this.#model;
|
|
561
|
+
if (!model)
|
|
562
|
+
return;
|
|
563
|
+
const e = this.#el;
|
|
564
|
+
const t = this.#strings;
|
|
565
|
+
this.#direction = this.direction === "auto" ? (this.#width > 0 && this.#width <= NARROW_WIDTH ? "down" : "right") : this.direction;
|
|
566
|
+
if (this.#foldsFor !== model) {
|
|
567
|
+
// fold around the active configuration, so what's running stays visible
|
|
568
|
+
const session = this.#session;
|
|
569
|
+
const active = new Set(session?.status === "running" ? model.states.filter((s) => session.isActiveNode(s)) : []);
|
|
570
|
+
this.#collapsed = autoCollapse(model.root, this.maxStates, active);
|
|
571
|
+
this.#foldsFor = model;
|
|
572
|
+
}
|
|
573
|
+
this.#boxes.clear();
|
|
574
|
+
this.#labels.clear();
|
|
575
|
+
this.#paths.clear();
|
|
576
|
+
this.#chips.clear();
|
|
577
|
+
e.diagram.replaceChildren();
|
|
578
|
+
e.diagram.setAttribute("aria-label", t.diagramLabel(model.name || "statechart"));
|
|
579
|
+
// 1. boxes, drawn parents first; hidden (folded) states get none
|
|
580
|
+
const visible = [];
|
|
581
|
+
const walk = (s) => {
|
|
582
|
+
for (const k of [...s.children, ...s.history]) {
|
|
583
|
+
visible.push(k);
|
|
584
|
+
if (!this.#collapsed.has(k))
|
|
585
|
+
walk(k);
|
|
586
|
+
}
|
|
587
|
+
};
|
|
588
|
+
walk(model.root);
|
|
589
|
+
for (const s of visible) {
|
|
590
|
+
const el = this.#box(s);
|
|
591
|
+
this.#boxes.set(s, el);
|
|
592
|
+
e.diagram.append(el);
|
|
593
|
+
}
|
|
594
|
+
// 2. label templates, measured once per transition
|
|
595
|
+
const labelSizes = new Map();
|
|
596
|
+
const measureHost = h("div", { style: "position:absolute;left:0;top:0;visibility:hidden" });
|
|
597
|
+
e.diagram.append(measureHost);
|
|
598
|
+
for (const s of visible) {
|
|
599
|
+
for (const tr of s.transitions) {
|
|
600
|
+
if (!tr.targets.length || !labelText(tr))
|
|
601
|
+
continue;
|
|
602
|
+
const el = this.#label(tr);
|
|
603
|
+
measureHost.append(el);
|
|
604
|
+
labelSizes.set(tr, measured(el, estimateLabel(tr)));
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
measureHost.remove();
|
|
608
|
+
// 3. layout
|
|
609
|
+
const layout = layoutChart(model.root, {
|
|
610
|
+
direction: this.#direction,
|
|
611
|
+
collapsed: this.#collapsed,
|
|
612
|
+
labelSize: (tr) => labelSizes.get(tr) ?? { w: 0, h: 0 },
|
|
613
|
+
measure: (s, collapsed) => this.#measure(s, collapsed),
|
|
614
|
+
});
|
|
615
|
+
this.#layout = layout;
|
|
616
|
+
// 4. position boxes
|
|
617
|
+
for (const b of layout.boxes) {
|
|
618
|
+
const el = this.#boxes.get(b.node);
|
|
619
|
+
if (!el)
|
|
620
|
+
continue;
|
|
621
|
+
Object.assign(el.style, { left: `${b.rect.x}px`, top: `${b.rect.y}px`, width: `${b.rect.w}px`, height: `${b.rect.h}px` });
|
|
622
|
+
}
|
|
623
|
+
// 5. edges, initial markers and labels
|
|
624
|
+
const NS = "http://www.w3.org/2000/svg";
|
|
625
|
+
const svg = document.createElementNS(NS, "svg");
|
|
626
|
+
svg.setAttribute("class", "edges");
|
|
627
|
+
svg.setAttribute("width", String(layout.width));
|
|
628
|
+
svg.setAttribute("height", String(layout.height));
|
|
629
|
+
svg.setAttribute("aria-hidden", "true");
|
|
630
|
+
svg.append(arrowMarker("arrow", 10, 7, 9.5));
|
|
631
|
+
for (const edge of layout.edges) {
|
|
632
|
+
const path = document.createElementNS(NS, "path");
|
|
633
|
+
path.setAttribute("class", "edge");
|
|
634
|
+
path.setAttribute("part", "edge");
|
|
635
|
+
path.setAttribute("d", pathData(edge.points));
|
|
636
|
+
path.setAttribute("marker-end", "url(#arrow)");
|
|
637
|
+
svg.append(path);
|
|
638
|
+
(this.#paths.get(edge.transition) ?? this.#paths.set(edge.transition, []).get(edge.transition)).push(path);
|
|
639
|
+
if (edge.label) {
|
|
640
|
+
const label = this.#label(edge.transition);
|
|
641
|
+
Object.assign(label.style, { left: `${edge.label.x}px`, top: `${edge.label.y}px`, width: `${edge.label.w}px` });
|
|
642
|
+
e.diagram.append(label);
|
|
643
|
+
(this.#labels.get(edge.transition) ?? this.#labels.set(edge.transition, []).get(edge.transition)).push(label);
|
|
644
|
+
}
|
|
645
|
+
}
|
|
646
|
+
for (const m of layout.initials) {
|
|
647
|
+
const dot = document.createElementNS(NS, "circle");
|
|
648
|
+
dot.setAttribute("class", "initial");
|
|
649
|
+
dot.setAttribute("part", "initial");
|
|
650
|
+
dot.setAttribute("cx", String(m.at.x));
|
|
651
|
+
dot.setAttribute("cy", String(m.at.y));
|
|
652
|
+
dot.setAttribute("r", "4.5");
|
|
653
|
+
const line = document.createElementNS(NS, "path");
|
|
654
|
+
line.setAttribute("class", "initial-line");
|
|
655
|
+
line.setAttribute("d", pathData(m.points));
|
|
656
|
+
line.setAttribute("marker-end", "url(#arrow)");
|
|
657
|
+
svg.append(line, dot);
|
|
658
|
+
}
|
|
659
|
+
e.diagram.prepend(svg);
|
|
660
|
+
Object.assign(e.diagram.style, { width: `${layout.width}px`, height: `${layout.height}px` });
|
|
661
|
+
// 6. folded groups, authoring warnings
|
|
662
|
+
this.#renderNotice();
|
|
663
|
+
this.#renderWarnings();
|
|
664
|
+
this.#applyFit();
|
|
665
|
+
this.#renderState();
|
|
666
|
+
}
|
|
667
|
+
#kind(s) {
|
|
668
|
+
return s.kind === "parallel"
|
|
669
|
+
? "parallel"
|
|
670
|
+
: s.kind === "final"
|
|
671
|
+
? "final"
|
|
672
|
+
: s.kind === "history"
|
|
673
|
+
? "history"
|
|
674
|
+
: isCompound(s)
|
|
675
|
+
? "compound"
|
|
676
|
+
: "atomic";
|
|
677
|
+
}
|
|
678
|
+
#box(s) {
|
|
679
|
+
const t = this.#strings;
|
|
680
|
+
const kind = this.#kind(s);
|
|
681
|
+
const collapsed = this.#collapsed.has(s);
|
|
682
|
+
const container = !collapsed && (kind === "compound" || kind === "parallel");
|
|
683
|
+
const region = s.parent?.kind === "parallel";
|
|
684
|
+
const el = h("div", {
|
|
685
|
+
class: [
|
|
686
|
+
"box",
|
|
687
|
+
container ? `container depth-${s.depth % 2 ? "odd" : "even"}` : "leaf",
|
|
688
|
+
kind,
|
|
689
|
+
region && "region",
|
|
690
|
+
collapsed && "collapsed",
|
|
691
|
+
]
|
|
692
|
+
.filter(Boolean)
|
|
693
|
+
.join(" "),
|
|
694
|
+
part: parts("state", kind, region && "parallel-region", collapsed && "collapsed"),
|
|
695
|
+
role: "group",
|
|
696
|
+
"data-state": s.id,
|
|
697
|
+
});
|
|
698
|
+
const display = s.generatedId ? `(${t.kinds[kind]})` : s.id;
|
|
699
|
+
if (kind === "history") {
|
|
700
|
+
el.textContent = s.historyType === "deep" ? "H*" : "H";
|
|
701
|
+
el.title = display;
|
|
702
|
+
return el;
|
|
703
|
+
}
|
|
704
|
+
const name = h("div", { class: "name", part: "state-name", title: display }, h("span", { class: "dot", "aria-hidden": "true" }), display);
|
|
705
|
+
if (kind === "parallel")
|
|
706
|
+
name.append(h("span", { class: "kind" }, "∥"));
|
|
707
|
+
const internal = this.#internal(s);
|
|
708
|
+
if (container) {
|
|
709
|
+
el.append(h("div", { class: "head" }, name, ...(internal ? [internal] : [])));
|
|
710
|
+
if (this.#expandedFolds.has(s)) {
|
|
711
|
+
const fold = h("button", { class: "collapse", type: "button", part: "collapse" }, t.collapse);
|
|
712
|
+
fold.addEventListener("click", () => {
|
|
713
|
+
this.#collapsed.add(s);
|
|
714
|
+
this.#expandedFolds.delete(s);
|
|
715
|
+
this.#renderAll();
|
|
716
|
+
});
|
|
717
|
+
el.append(fold);
|
|
718
|
+
}
|
|
719
|
+
return el;
|
|
720
|
+
}
|
|
721
|
+
el.append(name);
|
|
722
|
+
if (internal)
|
|
723
|
+
el.append(internal);
|
|
724
|
+
if (collapsed) {
|
|
725
|
+
const inside = countInside(s);
|
|
726
|
+
const expand = h("button", { class: "expand", type: "button", part: "expand" }, t.expand);
|
|
727
|
+
expand.addEventListener("click", () => {
|
|
728
|
+
this.#collapsed.delete(s);
|
|
729
|
+
this.#expandedFolds.add(s);
|
|
730
|
+
this.#renderAll();
|
|
731
|
+
});
|
|
732
|
+
el.append(h("span", { class: "badge-count" }, t.statesInside(inside)), expand);
|
|
733
|
+
}
|
|
734
|
+
return el;
|
|
735
|
+
}
|
|
736
|
+
#expandedFolds = new Set();
|
|
737
|
+
/** Targetless transitions of a state, as chips inside its box. */
|
|
738
|
+
#internal(s) {
|
|
739
|
+
const list = s.transitions.filter((tr) => !tr.targets.length);
|
|
740
|
+
if (!list.length)
|
|
741
|
+
return undefined;
|
|
742
|
+
const wrap = h("div", { class: "internal", part: "internal" });
|
|
743
|
+
for (const tr of list.slice(0, MAX_INTERNAL_SHOWN)) {
|
|
744
|
+
const chip = this.#label(tr, true);
|
|
745
|
+
wrap.append(chip);
|
|
746
|
+
this.#chips.set(tr, chip);
|
|
747
|
+
}
|
|
748
|
+
if (list.length > MAX_INTERNAL_SHOWN)
|
|
749
|
+
wrap.append(h("span", { class: "more" }, this.#strings.moreInternal(list.length - MAX_INTERNAL_SHOWN)));
|
|
750
|
+
return wrap;
|
|
751
|
+
}
|
|
752
|
+
/** A transition's label: a button that sends its event (or a plain label). */
|
|
753
|
+
#label(tr, chip = false) {
|
|
754
|
+
const t = this.#strings;
|
|
755
|
+
const name = tr.events.map(sendableName).find((n) => !!n);
|
|
756
|
+
const interactive = this.interactive && !!name;
|
|
757
|
+
const el = h(interactive ? "button" : "span", {
|
|
758
|
+
class: chip ? "chip" : "label",
|
|
759
|
+
part: parts("transition", "edge-label"),
|
|
760
|
+
...(interactive ? { type: "button" } : {}),
|
|
761
|
+
});
|
|
762
|
+
const events = tr.events.length ? tr.events : [t.eventless];
|
|
763
|
+
events.forEach((ev, i) => {
|
|
764
|
+
if (i)
|
|
765
|
+
el.append(" ");
|
|
766
|
+
el.append(h("span", { part: "event" }, ev));
|
|
767
|
+
});
|
|
768
|
+
if (tr.cond)
|
|
769
|
+
el.append(h("span", { class: "cond", part: "cond" }, `[${truncate(tr.cond, 22)}]`));
|
|
770
|
+
const targets = tr.targets.map((s) => s.id).join(", ");
|
|
771
|
+
el.setAttribute("aria-label", t.transitionLabel(tr.events.join(" ") || t.eventless, targets));
|
|
772
|
+
el.title = [tr.events.join(" ") || t.eventless, tr.cond && `[${tr.cond}]`, targets && `→ ${targets}`].filter(Boolean).join(" ");
|
|
773
|
+
if (interactive)
|
|
774
|
+
el.addEventListener("click", () => {
|
|
775
|
+
const session = this.#session;
|
|
776
|
+
if (!session || !name)
|
|
777
|
+
return;
|
|
778
|
+
if (!this.#emit("scxml-send", { session, name }, true))
|
|
779
|
+
return;
|
|
780
|
+
session.send(name);
|
|
781
|
+
if (this.announce !== "off")
|
|
782
|
+
this.#say(t.announceSent(name), true);
|
|
783
|
+
});
|
|
784
|
+
return el;
|
|
785
|
+
}
|
|
786
|
+
#measure(s, collapsed) {
|
|
787
|
+
const el = this.#boxes.get(s);
|
|
788
|
+
const kind = this.#kind(s);
|
|
789
|
+
if (s.kind === "scxml")
|
|
790
|
+
return { w: 0, h: 0 };
|
|
791
|
+
if (kind === "history")
|
|
792
|
+
return { w: 26, h: 26 };
|
|
793
|
+
if (!el)
|
|
794
|
+
return estimateBox(s, collapsed);
|
|
795
|
+
const container = !collapsed && (kind === "compound" || kind === "parallel");
|
|
796
|
+
if (container) {
|
|
797
|
+
const head = el.querySelector(".head");
|
|
798
|
+
return head ? measured(head, estimateBox(s, false)) : estimateBox(s, false);
|
|
799
|
+
}
|
|
800
|
+
el.style.width = "max-content";
|
|
801
|
+
const size = measured(el, estimateBox(s, collapsed));
|
|
802
|
+
el.style.width = "";
|
|
803
|
+
// + the active dot, hidden while measuring (7px and a 6px gap), so an active box doesn't truncate its name
|
|
804
|
+
return { w: Math.max(size.w + 13, 64), h: Math.max(size.h, 34) };
|
|
805
|
+
}
|
|
806
|
+
#renderNotice() {
|
|
807
|
+
const e = this.#el;
|
|
808
|
+
const t = this.#strings;
|
|
809
|
+
if (!this.#collapsed.size) {
|
|
810
|
+
e.notice.hidden = true;
|
|
811
|
+
return;
|
|
812
|
+
}
|
|
813
|
+
let hidden = 0;
|
|
814
|
+
for (const c of this.#collapsed)
|
|
815
|
+
hidden += countInside(c);
|
|
816
|
+
const all = h("button", { type: "button" }, t.expandAll);
|
|
817
|
+
all.addEventListener("click", () => {
|
|
818
|
+
for (const c of this.#collapsed)
|
|
819
|
+
this.#expandedFolds.add(c);
|
|
820
|
+
this.#collapsed.clear();
|
|
821
|
+
this.#renderAll();
|
|
822
|
+
});
|
|
823
|
+
e.notice.replaceChildren(h("span", {}, t.foldedNotice(hidden, this.#collapsed.size)), all);
|
|
824
|
+
e.notice.hidden = false;
|
|
825
|
+
}
|
|
826
|
+
/** The chart's authoring warnings, collapsed by default; each one focuses the state it's about. */
|
|
827
|
+
#renderWarnings() {
|
|
828
|
+
const box = this.#el.warnings;
|
|
829
|
+
const list = this.#model?.warnings ?? [];
|
|
830
|
+
if (!list.length || this.warnings === "off") {
|
|
831
|
+
box.hidden = true;
|
|
832
|
+
box.replaceChildren();
|
|
833
|
+
return;
|
|
834
|
+
}
|
|
835
|
+
const t = this.#strings;
|
|
836
|
+
const items = list.map((w) => {
|
|
837
|
+
const item = h("li", { part: "warning", "data-code": w.code });
|
|
838
|
+
const target = w.state ? this.#boxes.get(w.state) : undefined;
|
|
839
|
+
if (target) {
|
|
840
|
+
const go = h("button", { type: "button", title: t.showWarningState }, w.message);
|
|
841
|
+
go.addEventListener("click", () => {
|
|
842
|
+
if (!target.hasAttribute("tabindex"))
|
|
843
|
+
target.setAttribute("tabindex", "-1"); // focusable from script only
|
|
844
|
+
target.scrollIntoView?.({ block: "nearest", inline: "nearest" });
|
|
845
|
+
target.focus();
|
|
846
|
+
});
|
|
847
|
+
item.append(go);
|
|
848
|
+
}
|
|
849
|
+
else
|
|
850
|
+
item.append(w.message);
|
|
851
|
+
return item;
|
|
852
|
+
});
|
|
853
|
+
box.replaceChildren(h("summary", {}, t.warningsSummary(list.length)), h("ul", {}, ...items));
|
|
854
|
+
box.hidden = false;
|
|
855
|
+
}
|
|
856
|
+
/**
|
|
857
|
+
* A diagram wider than the element is scaled down: with `fit` all the way to the element's
|
|
858
|
+
* width, otherwise no further than `--scxml-min-scale` (default 0.65, so labels stay legible),
|
|
859
|
+
* and the rest scrolls. `--scxml-min-scale: 1` turns the automatic scaling off.
|
|
860
|
+
*/
|
|
861
|
+
#applyFit() {
|
|
862
|
+
const d = this.#el.diagram;
|
|
863
|
+
const layout = this.#layout;
|
|
864
|
+
const available = this.#el.canvas.clientWidth - 24;
|
|
865
|
+
let scale = 1;
|
|
866
|
+
if (layout && available > 0 && available < layout.width) {
|
|
867
|
+
const raw = Number.parseFloat(getComputedStyle(this).getPropertyValue("--scxml-min-scale"));
|
|
868
|
+
const floor = this.fit ? 0 : Number.isFinite(raw) ? Math.min(1, Math.max(0.1, raw)) : DEFAULT_MIN_SCALE;
|
|
869
|
+
scale = Math.min(1, Math.max(floor, available / layout.width));
|
|
870
|
+
}
|
|
871
|
+
// transform, not `zoom`: WebKit's zoom shrinks the boxes but not their text. A transform
|
|
872
|
+
// doesn't change the layout box, so negative margins give the scroll area the scaled size.
|
|
873
|
+
d.dataset.scale = String(scale);
|
|
874
|
+
if (!layout || scale >= 1) {
|
|
875
|
+
for (const p of ["transform", "transform-origin", "margin-right", "margin-bottom"])
|
|
876
|
+
d.style.removeProperty(p);
|
|
877
|
+
return;
|
|
878
|
+
}
|
|
879
|
+
d.style.setProperty("transform-origin", "0 0");
|
|
880
|
+
d.style.setProperty("transform", `scale(${scale})`);
|
|
881
|
+
d.style.setProperty("margin-right", `${12 - layout.width * (1 - scale)}px`);
|
|
882
|
+
d.style.setProperty("margin-bottom", `${12 - layout.height * (1 - scale)}px`);
|
|
883
|
+
}
|
|
884
|
+
// ───────────────────────────── live state ─────────────────────────────
|
|
885
|
+
/** Classes, parts and accessible names that depend on the configuration and the clock. */
|
|
886
|
+
#renderState() {
|
|
887
|
+
const session = this.#session;
|
|
888
|
+
const t = this.#strings;
|
|
889
|
+
const e = this.#el;
|
|
890
|
+
const clock = this.#playback();
|
|
891
|
+
e.controls.hidden = !clock;
|
|
892
|
+
if (clock)
|
|
893
|
+
this.#renderControls(clock);
|
|
894
|
+
if (!session || !this.#layout)
|
|
895
|
+
return;
|
|
896
|
+
const running = session.status === "running";
|
|
897
|
+
const now = session.clock.now();
|
|
898
|
+
for (const [tr, at] of this.#fired)
|
|
899
|
+
if (now - at >= FIRED_MS || now < at)
|
|
900
|
+
this.#fired.delete(tr);
|
|
901
|
+
// a terminated session has an empty configuration (the spec exits every state): keep showing
|
|
902
|
+
// where it ended, marked `reached` instead of `active`
|
|
903
|
+
const done = session.status === "done";
|
|
904
|
+
for (const [node, el] of this.#boxes) {
|
|
905
|
+
const kind = this.#kind(node);
|
|
906
|
+
const on = !done && session.isActiveNode(node);
|
|
907
|
+
const reached = done && this.#last.has(node);
|
|
908
|
+
el.classList.toggle("active", on);
|
|
909
|
+
el.classList.toggle("reached", reached);
|
|
910
|
+
el.setAttribute("part", parts("state", kind, node.parent?.kind === "parallel" && "parallel-region", this.#collapsed.has(node) && "collapsed", on && "active", reached && "reached"));
|
|
911
|
+
el.setAttribute("aria-label", t.stateLabel(node.generatedId ? t.kinds[kind] : node.id, t.kinds[kind], on, reached));
|
|
912
|
+
}
|
|
913
|
+
const liveOf = (tr) => running && session.isActiveNode(tr.source);
|
|
914
|
+
const paint = (tr, el, base) => {
|
|
915
|
+
const live = liveOf(tr);
|
|
916
|
+
const fired = this.#fired.has(tr);
|
|
917
|
+
el.classList.toggle("live", live);
|
|
918
|
+
el.classList.toggle("fired", fired);
|
|
919
|
+
el.setAttribute("part", parts(...base, live && "live", fired && "fired"));
|
|
920
|
+
if (el.localName === "button") {
|
|
921
|
+
const button = el;
|
|
922
|
+
const title = button.title.split("\n")[0];
|
|
923
|
+
button.disabled = !live;
|
|
924
|
+
button.title = live || !running ? title : `${title}\n${t.transitionInactive}`;
|
|
925
|
+
}
|
|
926
|
+
};
|
|
927
|
+
for (const [tr, list] of this.#labels)
|
|
928
|
+
for (const el of list)
|
|
929
|
+
paint(tr, el, ["transition", "edge-label"]);
|
|
930
|
+
for (const [tr, el] of this.#chips)
|
|
931
|
+
paint(tr, el, ["transition", "edge-label", "internal"]);
|
|
932
|
+
for (const [tr, list] of this.#paths)
|
|
933
|
+
for (const p of list)
|
|
934
|
+
paint(tr, p, ["edge"]);
|
|
935
|
+
// fired highlights fade on the session's clock: a real-time clock needs frames to see time pass
|
|
936
|
+
const c = session.clock;
|
|
937
|
+
if (this.#fired.size &&
|
|
938
|
+
!(c instanceof PlaybackClock) &&
|
|
939
|
+
c.constructor.name !== "VirtualClock" &&
|
|
940
|
+
typeof requestAnimationFrame !== "undefined") {
|
|
941
|
+
cancelAnimationFrame(this.#decayRaf);
|
|
942
|
+
this.#decayRaf = requestAnimationFrame(() => this.#renderState());
|
|
943
|
+
}
|
|
944
|
+
if (session.status === "done" && this.#collapsed.size === 0) {
|
|
945
|
+
e.notice.hidden = false;
|
|
946
|
+
e.notice.replaceChildren(h("span", {}, t.terminated));
|
|
947
|
+
}
|
|
948
|
+
}
|
|
949
|
+
#renderControls(clock) {
|
|
950
|
+
const e = this.#el;
|
|
951
|
+
const t = this.#strings;
|
|
952
|
+
e.play.textContent = clock.playing ? t.pause : t.play;
|
|
953
|
+
e.play.setAttribute("aria-pressed", String(!clock.playing));
|
|
954
|
+
e.step.textContent = t.step;
|
|
955
|
+
e.step.disabled = clock.pending === 0;
|
|
956
|
+
if (e.speeds.childElementCount !== SPEEDS.length)
|
|
957
|
+
e.speeds.replaceChildren(...SPEEDS.map(([v, text]) => {
|
|
958
|
+
const b = h("button", { type: "button", "data-speed": String(v) }, text);
|
|
959
|
+
b.addEventListener("click", () => {
|
|
960
|
+
clock.speed = v;
|
|
961
|
+
});
|
|
962
|
+
return b;
|
|
963
|
+
}));
|
|
964
|
+
for (const b of e.speeds.children)
|
|
965
|
+
b.setAttribute("aria-pressed", String(Number(b.dataset.speed) === clock.speed));
|
|
966
|
+
e.clock.textContent = t.clockTime(`${(clock.now() / 1000).toFixed(1)} s`);
|
|
967
|
+
}
|
|
968
|
+
}
|
|
969
|
+
// ───────────────────────────────── helpers ─────────────────────────────────
|
|
970
|
+
/** Thrown when the element has nothing to show or its attributes are malformed. */
|
|
971
|
+
export class ViewSourceError extends Error {
|
|
972
|
+
/** Always `"SCXML_VIEW_SOURCE"`. */
|
|
973
|
+
code = "SCXML_VIEW_SOURCE";
|
|
974
|
+
constructor(message) {
|
|
975
|
+
super(message);
|
|
976
|
+
this.name = "ViewSourceError";
|
|
977
|
+
}
|
|
978
|
+
}
|
|
979
|
+
function h(tag, attrs = {}, ...children) {
|
|
980
|
+
const el = document.createElement(tag);
|
|
981
|
+
for (const [k, v] of Object.entries(attrs)) {
|
|
982
|
+
if (v === false || v == null)
|
|
983
|
+
continue;
|
|
984
|
+
// through the CSSOM, not the style attribute: a strict Content-Security-Policy (no 'unsafe-inline') allows it
|
|
985
|
+
if (k === "style")
|
|
986
|
+
el.style.cssText = String(v);
|
|
987
|
+
else
|
|
988
|
+
el.setAttribute(k, v);
|
|
989
|
+
}
|
|
990
|
+
el.append(...children);
|
|
991
|
+
return el;
|
|
992
|
+
}
|
|
993
|
+
const parts = (...names) => names.filter(Boolean).join(" ");
|
|
994
|
+
const truncate = (s, n) => (s.length > n ? `${s.slice(0, n - 1)}…` : s);
|
|
995
|
+
/** The event name a label sends: "a.b.*" → "a.b", "*" → none. */
|
|
996
|
+
function sendableName(descriptor) {
|
|
997
|
+
if (descriptor === "*")
|
|
998
|
+
return undefined;
|
|
999
|
+
return descriptor.replace(/\.\*$/, "");
|
|
1000
|
+
}
|
|
1001
|
+
function labelText(tr) {
|
|
1002
|
+
const ev = tr.events.join(" ");
|
|
1003
|
+
const cond = tr.cond ? `[${truncate(tr.cond, 22)}]` : "";
|
|
1004
|
+
return [ev, cond].filter(Boolean).join(" ");
|
|
1005
|
+
}
|
|
1006
|
+
/** Size estimates, for when the element can't measure (no layout engine, e.g. happy-dom). */
|
|
1007
|
+
function estimateLabel(tr) {
|
|
1008
|
+
const text = labelText(tr);
|
|
1009
|
+
return text ? { w: Math.ceil(text.length * 6.8 + 18), h: 20 } : { w: 0, h: 0 };
|
|
1010
|
+
}
|
|
1011
|
+
function estimateBox(s, collapsed) {
|
|
1012
|
+
const name = s.generatedId ? s.kind : s.id;
|
|
1013
|
+
const internal = s.transitions.filter((t) => !t.targets.length);
|
|
1014
|
+
const chipsW = internal.slice(0, MAX_INTERNAL_SHOWN).reduce((w, t) => w + estimateLabel(t).w + 4, 0);
|
|
1015
|
+
const w = Math.max(64, name.length * 7.6 + 34 + (s.kind === "parallel" ? 16 : 0), Math.min(chipsW, 260) + 22);
|
|
1016
|
+
const chipRows = internal.length ? Math.ceil(Math.max(chipsW, 1) / 260) : 0;
|
|
1017
|
+
const isContainer = !collapsed && (s.kind === "parallel" || s.children.length > 0);
|
|
1018
|
+
if (isContainer)
|
|
1019
|
+
return { w, h: 26 + chipRows * 24 };
|
|
1020
|
+
return { w: collapsed ? Math.max(w, 120) : w, h: 34 + chipRows * 24 + (collapsed ? 50 : 0) };
|
|
1021
|
+
}
|
|
1022
|
+
/** Layout size (offsetWidth/Height): unlike getBoundingClientRect it ignores the scale transform. */
|
|
1023
|
+
function measured(el, fallback) {
|
|
1024
|
+
const w = Math.max(el.offsetWidth, el.scrollWidth);
|
|
1025
|
+
const h = Math.max(el.offsetHeight, el.scrollHeight);
|
|
1026
|
+
return w > 0 && h > 0 ? { w: w + 1, h } : fallback;
|
|
1027
|
+
}
|
|
1028
|
+
function countInside(s) {
|
|
1029
|
+
let n = 0;
|
|
1030
|
+
for (const k of [...s.children, ...s.history])
|
|
1031
|
+
n += 1 + countInside(k);
|
|
1032
|
+
return n;
|
|
1033
|
+
}
|
|
1034
|
+
function pathData(points) {
|
|
1035
|
+
return points.map((p, i) => `${i ? "L" : "M"}${Math.round(p.x * 10) / 10},${Math.round(p.y * 10) / 10}`).join(" ");
|
|
1036
|
+
}
|
|
1037
|
+
async function fetchText(url) {
|
|
1038
|
+
const res = await fetch(url);
|
|
1039
|
+
if (!res.ok)
|
|
1040
|
+
throw new ViewSourceError(`Could not load ${url}: HTTP ${res.status}`);
|
|
1041
|
+
return res.text();
|
|
1042
|
+
}
|
|
1043
|
+
if (typeof customElements !== "undefined" && !customElements.get("scxml-view"))
|
|
1044
|
+
customElements.define("scxml-view", ScxmlView);
|
|
1045
|
+
//# sourceMappingURL=element.js.map
|