@f-ewald/components 1.41.0 → 1.42.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,724 @@
1
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6
+ };
7
+ import { LitElement, css, html, nothing, svg } from "lit";
8
+ import { customElement, property, query, state } from "lit/decorators.js";
9
+ import { curveBasis, line } from "d3-shape";
10
+ import { tokens } from "./tokens.js";
11
+ import { iconArrowLongDown, iconArrowLongRight, iconArrowsPointingIn, iconMagnifyingGlassMinus, iconMagnifyingGlassPlus, iconViewfinderCircle, } from "./icons.js";
12
+ import "./icon-button.js";
13
+ import { approximateTextWidth, DAG_LABEL_OFFSET, DAG_LABEL_OFFSET_PLAIN, DAG_NODE_HEIGHT, layoutDag, } from "./utils/dag-layout.js";
14
+ const ICON_SIZE = 14;
15
+ const MIN_SCALE = 0.05;
16
+ const MAX_SCALE = 3;
17
+ const FIT_PADDING = 24;
18
+ /** Below this fitted zoom labels get hard to read, so the selected node is shown at 100% instead. */
19
+ const READABLE_SCALE = 0.6;
20
+ const DRAG_THRESHOLD = 4;
21
+ const KEY_PAN = 48;
22
+ const KEY_ZOOM = 1.25;
23
+ const CONTROLS = ["zoom", "fit", "center", "direction"];
24
+ const route = line()
25
+ .x((point) => point.x)
26
+ .y((point) => point.y)
27
+ .curve(curveBasis);
28
+ /**
29
+ * Interactive layered drawing of a directed graph — dependencies, pipelines, or any node-link data.
30
+ *
31
+ * Nodes are placed in layers (via dagre) so edges flow left to right (or top to bottom with
32
+ * `direction="TB"`); cycles are allowed, and the edges closing them are drawn dashed and point backward.
33
+ * Each node is a compact card sized to its label, with an optional icon, color, and border style from
34
+ * `kinds`. The whole graph is shown when it fits at a readable zoom; otherwise the selected node is centered at
35
+ * 100%. Drag to pan; pinch or Ctrl/⌘ + wheel to zoom (a plain wheel scrolls the page). With keyboard
36
+ * focus on the graph, arrow keys pan, `+`/`-` zoom, and `0` fits the graph. Clicking a node, or pressing
37
+ * Enter/Space on it, selects it. A toolbar in the top-right corner offers the `controls` you enable.
38
+ *
39
+ * Set `static` to present a fixed picture: the graph is always fitted into view, panning and zooming
40
+ * are off, and the toolbar is hidden; nodes can still be selected.
41
+ *
42
+ * Colors can be changed with these custom properties: `--dag-node-background`, `--dag-node-text`,
43
+ * `--dag-edge-color`, `--dag-selected-color`, and `--dag-highlight-color`; `--dag-graph-height` sets the
44
+ * height (default `24rem`).
45
+ *
46
+ * @element dag-graph
47
+ * @fires node-select - A node was clicked or activated with the keyboard; `detail` is `{ id }`.
48
+ * @fires direction-change - The toolbar's direction control flipped `direction`; `detail` is `{ direction }`.
49
+ * @slot empty - Shown when there are no nodes.
50
+ */
51
+ let DagGraph = class DagGraph extends LitElement {
52
+ constructor() {
53
+ super(...arguments);
54
+ /** Nodes to draw. */
55
+ this.nodes = [];
56
+ /** Directed edges between `nodes`; edges with an unknown endpoint or to the node itself are ignored. */
57
+ this.edges = [];
58
+ /** Presentation per node `kind`: legend label, color, icon, and border style. */
59
+ this.kinds = {};
60
+ /**
61
+ * Id of the selected node. Selecting a node updates it; when it changes to a node outside the visible area,
62
+ * the view follows.
63
+ */
64
+ this.selected = null;
65
+ /** Layer direction: `LR` (left to right) or `TB` (top to bottom). */
66
+ this.direction = "LR";
67
+ /** Accessible name of the graph; the node and edge counts are appended. */
68
+ this.label = "Directed graph";
69
+ /** Shows a legend of `kinds` below the graph. */
70
+ this.legend = false;
71
+ /**
72
+ * Space-separated toolbar controls to show: any of `zoom`, `fit`, `center`, and `direction`, in that
73
+ * order. An empty value hides the toolbar.
74
+ */
75
+ this.controls = "zoom fit center";
76
+ /** Presents a static picture: always fitted, with panning, zooming, and the toolbar turned off. */
77
+ this.isStatic = false;
78
+ this._view = { x: 0, y: 0, k: 1 };
79
+ this._hovered = null;
80
+ this._panning = false;
81
+ this._layout = { nodes: [], edges: [] };
82
+ this._positions = new Map();
83
+ this._size = { width: 0, height: 0 };
84
+ this._viewReady = false;
85
+ this._lastSelected = null;
86
+ this._lastDirection = null;
87
+ this._measure = approximateTextWidth;
88
+ this._measureFont = "";
89
+ this._resizeObserver = null;
90
+ this._drag = null;
91
+ this._suppressClick = false;
92
+ }
93
+ static { this.styles = [
94
+ tokens,
95
+ css `
96
+ :host {
97
+ display: flex;
98
+ flex-direction: column;
99
+ height: var(--dag-graph-height, 24rem);
100
+ font-family: var(
101
+ --ui-font,
102
+ ui-sans-serif,
103
+ system-ui,
104
+ sans-serif,
105
+ "Apple Color Emoji",
106
+ "Segoe UI Emoji",
107
+ "Segoe UI Symbol",
108
+ "Noto Color Emoji"
109
+ );
110
+ font-size: var(--ui-font-size-xs, 0.6875rem);
111
+ color: var(--ui-text, #0f172a);
112
+ }
113
+ .canvas {
114
+ position: relative;
115
+ flex: 1;
116
+ min-height: 0;
117
+ }
118
+ svg {
119
+ display: block;
120
+ width: 100%;
121
+ height: 100%;
122
+ cursor: grab;
123
+ touch-action: none;
124
+ user-select: none;
125
+ outline: none;
126
+ }
127
+ svg.panning {
128
+ cursor: grabbing;
129
+ }
130
+ :host([static]) svg {
131
+ cursor: default;
132
+ }
133
+ .toolbar {
134
+ position: absolute;
135
+ top: 0.5rem;
136
+ right: 0.5rem;
137
+ display: flex;
138
+ gap: 0.25rem;
139
+ padding: 0.25rem;
140
+ background:
141
+ var(--ui-button-secondary-background, none),
142
+ var(--ui-surface, #ffffff);
143
+ border: var(--ui-border-width, 1px) solid var(--ui-button-secondary-border, var(--ui-border, #e2e8f0));
144
+ border-radius: var(--ui-radius, 0.5rem);
145
+ box-shadow:
146
+ var(--ui-button-highlight, 0 0 0 0 transparent),
147
+ var(--ui-shadow, 0 4px 6px -1px rgb(0 0 0 / 0.1), 0 2px 4px -2px rgb(0 0 0 / 0.1));
148
+ }
149
+ svg:focus-visible {
150
+ box-shadow: var(--ui-focus-ring, 0 0 0 3px rgb(79 70 229 / 0.35));
151
+ }
152
+ .edge {
153
+ fill: none;
154
+ stroke: var(--dag-edge-color, var(--ui-text-muted, #64748b));
155
+ stroke-opacity: 0.45;
156
+ stroke-width: 1;
157
+ vector-effect: non-scaling-stroke;
158
+ }
159
+ .edge.backward {
160
+ stroke-dasharray: 4 3;
161
+ }
162
+ .edges.emphasis .edge {
163
+ stroke-opacity: 0.08;
164
+ }
165
+ .edges.emphasis .edge.incident {
166
+ stroke-opacity: 0.9;
167
+ }
168
+ .edge.highlighted,
169
+ .edges.emphasis .edge.highlighted {
170
+ stroke: var(--dag-highlight-color, var(--ui-danger, #dc2626));
171
+ stroke-opacity: 1;
172
+ stroke-width: 2;
173
+ }
174
+ .arrow {
175
+ fill: var(--dag-edge-color, var(--ui-text-muted, #64748b));
176
+ fill-opacity: 0.7;
177
+ }
178
+ .arrow.highlighted {
179
+ fill: var(--dag-highlight-color, var(--ui-danger, #dc2626));
180
+ fill-opacity: 1;
181
+ }
182
+ .node {
183
+ cursor: pointer;
184
+ outline: none;
185
+ }
186
+ .gloss-start {
187
+ stop-color: var(--ui-button-secondary-gloss-start, var(--ui-surface, #ffffff));
188
+ }
189
+ .gloss-end {
190
+ stop-color: var(--ui-button-secondary-gloss-end, var(--ui-surface, #ffffff));
191
+ }
192
+ .node rect {
193
+ rx: var(--ui-radius-sm, 0.25rem);
194
+ fill: var(--dag-node-background, url(#node-fill));
195
+ stroke: color-mix(
196
+ in srgb,
197
+ var(--dag-kind-color, var(--ui-primary, #4f46e5)) 55%,
198
+ var(--dag-node-background, var(--ui-surface, #ffffff))
199
+ );
200
+ stroke-width: 1;
201
+ }
202
+ .node.dashed rect {
203
+ stroke-dasharray: 4 2;
204
+ }
205
+ .node.muted rect {
206
+ fill: var(--ui-surface-muted, #f8fafc);
207
+ stroke: var(--ui-text-muted, #64748b);
208
+ stroke-opacity: 0.6;
209
+ stroke-dasharray: 1.5 2;
210
+ }
211
+ .node.muted .icon,
212
+ .node.muted .label {
213
+ opacity: 0.6;
214
+ }
215
+ .node.selected rect {
216
+ fill: color-mix(
217
+ in srgb,
218
+ var(--dag-selected-color, var(--ui-warning, #d97706)) 15%,
219
+ var(--dag-node-background, var(--ui-surface, #ffffff))
220
+ );
221
+ stroke: var(--dag-selected-color, var(--ui-warning, #d97706));
222
+ stroke-opacity: 1;
223
+ stroke-width: 1.5;
224
+ }
225
+ .node.highlighted rect {
226
+ stroke: var(--dag-highlight-color, var(--ui-danger, #dc2626));
227
+ stroke-opacity: 1;
228
+ stroke-width: 1.5;
229
+ }
230
+ .node:hover rect,
231
+ .node:focus-visible rect {
232
+ stroke: var(--ui-primary, #4f46e5);
233
+ stroke-opacity: 1;
234
+ stroke-width: 2;
235
+ }
236
+ .icon {
237
+ color: var(--dag-kind-color, var(--ui-primary, #4f46e5));
238
+ }
239
+ .label {
240
+ fill: var(--dag-node-text, var(--ui-text, #0f172a));
241
+ dominant-baseline: central;
242
+ pointer-events: none;
243
+ }
244
+ .empty {
245
+ position: absolute;
246
+ inset: 0;
247
+ display: grid;
248
+ place-items: center;
249
+ margin: 0;
250
+ font-size: var(--ui-font-size, 0.875rem);
251
+ color: var(--ui-text-muted, #64748b);
252
+ pointer-events: none;
253
+ }
254
+ .legend {
255
+ display: flex;
256
+ flex-wrap: wrap;
257
+ gap: 0.25rem 1rem;
258
+ margin: 0;
259
+ padding: 0.5rem 0 0;
260
+ list-style: none;
261
+ font-size: var(--ui-font-size-sm, 0.75rem);
262
+ color: var(--ui-text-muted, #64748b);
263
+ }
264
+ .legend li {
265
+ display: inline-flex;
266
+ align-items: center;
267
+ gap: 0.25rem;
268
+ }
269
+ .legend .swatch {
270
+ display: inline-flex;
271
+ line-height: 0;
272
+ color: var(--dag-kind-color, var(--ui-primary, #4f46e5));
273
+ }
274
+ @media (forced-colors: active) {
275
+ .node:focus-visible rect,
276
+ .node.selected rect {
277
+ stroke: Highlight;
278
+ }
279
+ svg:focus-visible {
280
+ outline: 2px solid CanvasText;
281
+ }
282
+ }
283
+ `,
284
+ ]; }
285
+ connectedCallback() {
286
+ super.connectedCallback();
287
+ this._resizeObserver = new ResizeObserver((entries) => this._resize(entries[0]?.contentRect));
288
+ }
289
+ disconnectedCallback() {
290
+ super.disconnectedCallback();
291
+ this._resizeObserver?.disconnect();
292
+ this._resizeObserver = null;
293
+ }
294
+ firstUpdated() {
295
+ const canvas = this.renderRoot.querySelector(".canvas");
296
+ if (canvas)
297
+ this._resizeObserver?.observe(canvas);
298
+ }
299
+ willUpdate(changed) {
300
+ if (changed.has("nodes") || changed.has("edges") || changed.has("direction") || changed.has("kinds")) {
301
+ this._relayout();
302
+ }
303
+ }
304
+ updated(changed) {
305
+ if (changed.has("isStatic") && this.isStatic)
306
+ this.fit();
307
+ if (changed.has("selected") && !this._layoutChanged(changed))
308
+ this._revealSelected();
309
+ }
310
+ /** Fits the whole graph into view, never enlarging it beyond 100%. */
311
+ fit() {
312
+ if (!this._layout.nodes.length || !this._size.width)
313
+ return;
314
+ const left = Math.min(...this._layout.nodes.map((node) => node.x - node.width / 2));
315
+ const right = Math.max(...this._layout.nodes.map((node) => node.x + node.width / 2));
316
+ const top = Math.min(...this._layout.nodes.map((node) => node.y)) - DAG_NODE_HEIGHT / 2;
317
+ const bottom = Math.max(...this._layout.nodes.map((node) => node.y)) + DAG_NODE_HEIGHT / 2;
318
+ const k = this._clamp(Math.min(1, (this._size.width - 2 * FIT_PADDING) / Math.max(right - left, 1), (this._size.height - 2 * FIT_PADDING) / Math.max(bottom - top, 1)));
319
+ this._centerOn({ x: (left + right) / 2, y: (top + bottom) / 2 }, k);
320
+ }
321
+ /** Centers the view on a node (the selected one by default) at 100% zoom. */
322
+ center(id = this.selected) {
323
+ const node = id ? this._positions.get(id) : undefined;
324
+ if (node)
325
+ this._centerOn(node, 1);
326
+ }
327
+ /** Zooms by `factor` (e.g. `1.25` or `0.8`) around the center of the view. */
328
+ zoomBy(factor) {
329
+ this._zoomAround(this._size.width / 2, this._size.height / 2, factor);
330
+ }
331
+ render() {
332
+ const nodeCount = this._layout.nodes.length;
333
+ const summary = `${this.label}: ${nodeCount} ${nodeCount === 1 ? "node" : "nodes"}, `
334
+ + `${this._layout.edges.length} ${this._layout.edges.length === 1 ? "edge" : "edges"}`;
335
+ const { x, y, k } = this._view;
336
+ return html `
337
+ <div class="canvas">
338
+ <svg
339
+ class=${this._panning ? "panning" : ""}
340
+ role="group"
341
+ tabindex=${this.isStatic ? nothing : "0"}
342
+ aria-label=${summary}
343
+ @pointerdown=${this._onPointerDown}
344
+ @pointermove=${this._onPointerMove}
345
+ @pointerup=${this._onPointerUp}
346
+ @pointercancel=${this._onPointerUp}
347
+ @wheel=${this._onWheel}
348
+ @keydown=${this._onKeyDown}
349
+ >
350
+ <defs>
351
+ <marker id="arrow" viewBox="0 -4 8 8" refX="8" refY="0" markerWidth="7" markerHeight="7" orient="auto">
352
+ <path class="arrow" d="M0,-4L8,0L0,4Z" />
353
+ </marker>
354
+ <marker id="arrow-highlighted" viewBox="0 -4 8 8" refX="8" refY="0" markerWidth="7" markerHeight="7"
355
+ orient="auto">
356
+ <path class="arrow highlighted" d="M0,-4L8,0L0,4Z" />
357
+ </marker>
358
+ <linearGradient id="node-fill" x1="0" y1="0" x2="0" y2="1">
359
+ <stop class="gloss-start" offset="0%" />
360
+ <stop class="gloss-end" offset="100%" />
361
+ </linearGradient>
362
+ </defs>
363
+ <g transform="translate(${x},${y}) scale(${k})">
364
+ <g class="edges ${this._hovered ? "emphasis" : ""}">${this._renderEdges()}</g>
365
+ <g class="nodes">${this._layout.nodes.map((node) => this._renderNode(node))}</g>
366
+ </g>
367
+ </svg>
368
+ ${nodeCount ? nothing : html `<p class="empty"><slot name="empty">No nodes to show.</slot></p>`}
369
+ ${this._renderToolbar()}
370
+ </div>
371
+ ${this.legend ? this._renderLegend() : nothing}
372
+ `;
373
+ }
374
+ /** The enabled `controls`, in their fixed order; none in static mode or without nodes. */
375
+ get _controls() {
376
+ if (this.isStatic || !this._layout.nodes.length)
377
+ return [];
378
+ const enabled = new Set(this.controls.split(/\s+/));
379
+ return CONTROLS.filter((control) => enabled.has(control));
380
+ }
381
+ _renderToolbar() {
382
+ const controls = this._controls;
383
+ if (!controls.length)
384
+ return nothing;
385
+ const vertical = this.direction === "TB";
386
+ const button = (control, label, icon, action) => html `
387
+ <icon-button data-control=${control} label=${label} .icon=${icon} @click=${action}></icon-button>`;
388
+ return html `<div class="toolbar" role="toolbar" aria-label="Graph view">
389
+ ${controls.map((control) => {
390
+ switch (control) {
391
+ case "zoom":
392
+ return html `${button("zoom-in", "Zoom in", iconMagnifyingGlassPlus(18), () => this.zoomBy(KEY_ZOOM))}
393
+ ${button("zoom-out", "Zoom out", iconMagnifyingGlassMinus(18), () => this.zoomBy(1 / KEY_ZOOM))}`;
394
+ case "fit":
395
+ return button("fit", "Fit graph", iconArrowsPointingIn(18), () => this.fit());
396
+ case "center":
397
+ return html `<icon-button
398
+ data-control="center"
399
+ label="Center selected node"
400
+ .icon=${iconViewfinderCircle(18)}
401
+ ?disabled=${!this.selected || !this._positions.has(this.selected)}
402
+ @click=${() => this.center()}
403
+ ></icon-button>`;
404
+ case "direction":
405
+ return button("direction", vertical ? "Lay out left to right" : "Lay out top to bottom", vertical ? iconArrowLongRight(18) : iconArrowLongDown(18), () => this._toggleDirection());
406
+ }
407
+ })}
408
+ </div>`;
409
+ }
410
+ _toggleDirection() {
411
+ this.direction = this.direction === "LR" ? "TB" : "LR";
412
+ this.dispatchEvent(new CustomEvent("direction-change", {
413
+ detail: { direction: this.direction },
414
+ bubbles: true,
415
+ composed: true,
416
+ }));
417
+ }
418
+ _renderEdges() {
419
+ const highlighted = new Set(this.edges.filter((edge) => edge.highlighted).map((edge) => this._edgeKey(edge)));
420
+ const names = new Map(this.nodes.map((node) => [node.id, node.label]));
421
+ return this._layout.edges.map((edge) => {
422
+ const isHighlighted = highlighted.has(this._edgeKey(edge));
423
+ const incident = edge.source === this._hovered || edge.target === this._hovered;
424
+ const classes = [
425
+ "edge",
426
+ edge.backward ? "backward" : "",
427
+ isHighlighted ? "highlighted" : "",
428
+ incident ? "incident" : "",
429
+ ].filter(Boolean).join(" ");
430
+ return svg `<path class=${classes} data-source=${edge.source} data-target=${edge.target}
431
+ d=${route(edge.points) ?? ""} marker-end="url(#${isHighlighted ? "arrow-highlighted" : "arrow"})">
432
+ <title>${names.get(edge.source)} → ${names.get(edge.target)}</title></path>`;
433
+ });
434
+ }
435
+ _renderNode(position) {
436
+ const node = this.nodes.find((candidate) => candidate.id === position.id);
437
+ const kind = node.kind ? this.kinds[node.kind] : undefined;
438
+ const classes = [
439
+ "node",
440
+ kind?.dashed ? "dashed" : "",
441
+ node.muted ? "muted" : "",
442
+ node.highlighted ? "highlighted" : "",
443
+ node.id === this.selected ? "selected" : "",
444
+ ].filter(Boolean).join(" ");
445
+ const left = -position.width / 2;
446
+ const offset = kind?.icon ? DAG_LABEL_OFFSET : DAG_LABEL_OFFSET_PLAIN;
447
+ const name = kind?.label ? `${node.label} (${kind.label})` : node.label;
448
+ return svg `<g class=${classes} data-id=${node.id} role="button" tabindex="0"
449
+ aria-pressed=${node.id === this.selected ? "true" : "false"} aria-label=${name}
450
+ style=${kind?.color ? `--dag-kind-color: ${kind.color}` : nothing}
451
+ transform="translate(${position.x},${position.y})"
452
+ @click=${() => this._activate(node.id)}
453
+ @keydown=${(event) => this._onNodeKey(event, node.id)}
454
+ @pointerenter=${() => { this._hovered = node.id; }}
455
+ @pointerleave=${() => { this._hovered = null; }}
456
+ @focus=${() => { this._hovered = node.id; }}
457
+ @blur=${() => { this._hovered = null; }}>
458
+ <title>${name}</title>
459
+ <rect x=${left} y=${-DAG_NODE_HEIGHT / 2} width=${position.width} height=${DAG_NODE_HEIGHT}></rect>
460
+ ${kind?.icon
461
+ ? svg `<g class="icon" transform="translate(${left + 6},${-ICON_SIZE / 2})">${kind.icon(ICON_SIZE)}</g>`
462
+ : nothing}
463
+ <text class="label" x=${left + offset}>${position.label}</text>
464
+ </g>`;
465
+ }
466
+ _renderLegend() {
467
+ const entries = Object.entries(this.kinds).filter(([, kind]) => kind.label);
468
+ if (!entries.length)
469
+ return nothing;
470
+ return html `<ul class="legend" aria-label="Legend">
471
+ ${entries.map(([key, kind]) => html `<li data-kind=${key}
472
+ style=${kind.color ? `--dag-kind-color: ${kind.color}` : nothing}>
473
+ ${kind.icon ? html `<span class="swatch" aria-hidden="true">${kind.icon(ICON_SIZE)}</span>` : nothing}
474
+ ${kind.label}</li>`)}
475
+ </ul>`;
476
+ }
477
+ _edgeKey(edge) {
478
+ return `${edge.source}\u0000${edge.target}`;
479
+ }
480
+ _layoutChanged(changed) {
481
+ return changed.has("nodes") || changed.has("edges") || changed.has("direction") || changed.has("kinds");
482
+ }
483
+ /** Lays the graph out again, keeping the selected node where it was on screen. */
484
+ _relayout() {
485
+ const previous = this.selected ? this._positions.get(this.selected) : undefined;
486
+ const previousSelected = this._lastSelected;
487
+ this._updateMeasure();
488
+ const inputs = this.nodes.map((node) => ({
489
+ id: node.id,
490
+ label: node.label,
491
+ hasIcon: Boolean(node.kind && this.kinds[node.kind]?.icon),
492
+ }));
493
+ this._layout = layoutDag(inputs, this.edges, this.direction, this._measure);
494
+ this._positions = new Map(this._layout.nodes.map((node) => [node.id, node]));
495
+ const directionChanged = this._lastDirection !== null && this._lastDirection !== this.direction;
496
+ this._lastSelected = this.selected;
497
+ this._lastDirection = this.direction;
498
+ if (!this._size.width)
499
+ return;
500
+ if (!this._viewReady) {
501
+ this._initView();
502
+ return;
503
+ }
504
+ const current = this.selected ? this._positions.get(this.selected) : undefined;
505
+ if (current && previous && previousSelected === this.selected && !directionChanged && !this.isStatic) {
506
+ const { x, y, k } = this._view;
507
+ this._view = { k, x: x + k * (previous.x - current.x), y: y + k * (previous.y - current.y) };
508
+ }
509
+ else {
510
+ this._frame();
511
+ }
512
+ }
513
+ _initView() {
514
+ this._viewReady = true;
515
+ this._frame();
516
+ }
517
+ /**
518
+ * Shows the whole graph when it fits at a readable zoom (always, when static); otherwise centers the selected
519
+ * node at 100%.
520
+ */
521
+ _frame() {
522
+ this.fit();
523
+ if (this.isStatic)
524
+ return;
525
+ if (this._view.k < READABLE_SCALE && this.selected && this._positions.has(this.selected))
526
+ this.center();
527
+ }
528
+ /** Brings the selected node into view if it is outside the visible area. */
529
+ _revealSelected() {
530
+ const node = this.selected ? this._positions.get(this.selected) : undefined;
531
+ if (!node || !this._size.width || this.isStatic)
532
+ return;
533
+ const { x, y, k } = this._view;
534
+ const screenX = x + k * node.x;
535
+ const screenY = y + k * node.y;
536
+ const visible = screenX > 0 && screenX < this._size.width && screenY > 0 && screenY < this._size.height;
537
+ if (!visible)
538
+ this.center();
539
+ }
540
+ /** Measures labels with the host's actual font, so themes with other fonts still size cards correctly. */
541
+ _updateMeasure() {
542
+ const style = getComputedStyle(this);
543
+ const font = `${style.fontSize} ${style.fontFamily}`;
544
+ if (font === this._measureFont)
545
+ return;
546
+ this._measureFont = font;
547
+ const context = document.createElement("canvas").getContext("2d");
548
+ if (!context) {
549
+ this._measure = approximateTextWidth;
550
+ return;
551
+ }
552
+ context.font = font;
553
+ const widths = new Map();
554
+ this._measure = (text) => {
555
+ if (!widths.has(text))
556
+ widths.set(text, context.measureText(text).width);
557
+ return widths.get(text);
558
+ };
559
+ }
560
+ _resize(rect) {
561
+ if (!rect)
562
+ return;
563
+ const { width, height } = rect;
564
+ const previous = this._size;
565
+ this._size = { width, height };
566
+ if (!width || !height)
567
+ return;
568
+ if (!this._viewReady) {
569
+ if (this._layout.nodes.length)
570
+ this._initView();
571
+ return;
572
+ }
573
+ if (this.isStatic) {
574
+ this.fit();
575
+ return;
576
+ }
577
+ this._view = {
578
+ ...this._view,
579
+ x: this._view.x + (width - previous.width) / 2,
580
+ y: this._view.y + (height - previous.height) / 2,
581
+ };
582
+ }
583
+ _clamp(k) {
584
+ return Math.min(MAX_SCALE, Math.max(MIN_SCALE, k));
585
+ }
586
+ _centerOn(point, k) {
587
+ this._view = { k, x: this._size.width / 2 - k * point.x, y: this._size.height / 2 - k * point.y };
588
+ }
589
+ _zoomAround(screenX, screenY, factor) {
590
+ const { x, y, k } = this._view;
591
+ const next = this._clamp(k * factor);
592
+ const ratio = next / k;
593
+ this._view = { k: next, x: screenX - (screenX - x) * ratio, y: screenY - (screenY - y) * ratio };
594
+ }
595
+ _activate(id) {
596
+ if (this._suppressClick) {
597
+ this._suppressClick = false;
598
+ return;
599
+ }
600
+ this.selected = id;
601
+ this.dispatchEvent(new CustomEvent("node-select", { detail: { id }, bubbles: true, composed: true }));
602
+ }
603
+ _onNodeKey(event, id) {
604
+ if (event.key !== "Enter" && event.key !== " ")
605
+ return;
606
+ event.preventDefault();
607
+ event.stopPropagation();
608
+ this._activate(id);
609
+ }
610
+ _onPointerDown(event) {
611
+ if (event.button !== 0 || this.isStatic)
612
+ return;
613
+ this._suppressClick = false;
614
+ this._drag = { pointerId: event.pointerId, x: event.clientX, y: event.clientY, view: this._view, moved: false };
615
+ }
616
+ _onPointerMove(event) {
617
+ const drag = this._drag;
618
+ if (!drag || drag.pointerId !== event.pointerId)
619
+ return;
620
+ const dx = event.clientX - drag.x;
621
+ const dy = event.clientY - drag.y;
622
+ if (!drag.moved && Math.hypot(dx, dy) < DRAG_THRESHOLD)
623
+ return;
624
+ if (!drag.moved) {
625
+ drag.moved = true;
626
+ this._panning = true;
627
+ this._svg.setPointerCapture(event.pointerId);
628
+ }
629
+ this._view = { ...drag.view, x: drag.view.x + dx, y: drag.view.y + dy };
630
+ }
631
+ _onPointerUp(event) {
632
+ const drag = this._drag;
633
+ if (!drag || drag.pointerId !== event.pointerId)
634
+ return;
635
+ this._suppressClick = drag.moved;
636
+ this._drag = null;
637
+ this._panning = false;
638
+ if (this._svg.hasPointerCapture(event.pointerId))
639
+ this._svg.releasePointerCapture(event.pointerId);
640
+ }
641
+ /** Pinch gestures and Ctrl/⌘ + wheel zoom; a plain wheel is left to the page so it can scroll. */
642
+ _onWheel(event) {
643
+ if ((!event.ctrlKey && !event.metaKey) || this.isStatic)
644
+ return;
645
+ event.preventDefault();
646
+ const bounds = this._svg.getBoundingClientRect();
647
+ // d3-zoom's wheel scale: pixel, line, and page deltas, amplified for pinch (which reports ctrlKey and
648
+ // small deltas); one event zooms at most 2x either way.
649
+ const unit = event.deltaMode === 1 ? 0.05 : event.deltaMode ? 1 : 0.002;
650
+ const steps = Math.max(-1, Math.min(1, -event.deltaY * unit * (event.ctrlKey ? 10 : 1)));
651
+ this._zoomAround(event.clientX - bounds.left, event.clientY - bounds.top, 2 ** steps);
652
+ }
653
+ _onKeyDown(event) {
654
+ if (event.altKey || event.ctrlKey || event.metaKey || this.isStatic)
655
+ return;
656
+ const pan = {
657
+ ArrowLeft: [KEY_PAN, 0],
658
+ ArrowRight: [-KEY_PAN, 0],
659
+ ArrowUp: [0, KEY_PAN],
660
+ ArrowDown: [0, -KEY_PAN],
661
+ };
662
+ if (event.key in pan) {
663
+ const [dx, dy] = pan[event.key];
664
+ this._view = { ...this._view, x: this._view.x + dx, y: this._view.y + dy };
665
+ }
666
+ else if (event.key === "+" || event.key === "=") {
667
+ this.zoomBy(KEY_ZOOM);
668
+ }
669
+ else if (event.key === "-") {
670
+ this.zoomBy(1 / KEY_ZOOM);
671
+ }
672
+ else if (event.key === "0") {
673
+ this.fit();
674
+ }
675
+ else {
676
+ return;
677
+ }
678
+ event.preventDefault();
679
+ }
680
+ };
681
+ __decorate([
682
+ property({ type: Array })
683
+ ], DagGraph.prototype, "nodes", void 0);
684
+ __decorate([
685
+ property({ type: Array })
686
+ ], DagGraph.prototype, "edges", void 0);
687
+ __decorate([
688
+ property({ attribute: false })
689
+ ], DagGraph.prototype, "kinds", void 0);
690
+ __decorate([
691
+ property({ reflect: true })
692
+ ], DagGraph.prototype, "selected", void 0);
693
+ __decorate([
694
+ property({ reflect: true })
695
+ ], DagGraph.prototype, "direction", void 0);
696
+ __decorate([
697
+ property()
698
+ ], DagGraph.prototype, "label", void 0);
699
+ __decorate([
700
+ property({ type: Boolean, reflect: true })
701
+ ], DagGraph.prototype, "legend", void 0);
702
+ __decorate([
703
+ property({ reflect: true })
704
+ ], DagGraph.prototype, "controls", void 0);
705
+ __decorate([
706
+ property({ type: Boolean, reflect: true, attribute: "static" })
707
+ ], DagGraph.prototype, "isStatic", void 0);
708
+ __decorate([
709
+ state()
710
+ ], DagGraph.prototype, "_view", void 0);
711
+ __decorate([
712
+ state()
713
+ ], DagGraph.prototype, "_hovered", void 0);
714
+ __decorate([
715
+ state()
716
+ ], DagGraph.prototype, "_panning", void 0);
717
+ __decorate([
718
+ query("svg")
719
+ ], DagGraph.prototype, "_svg", void 0);
720
+ DagGraph = __decorate([
721
+ customElement("dag-graph")
722
+ ], DagGraph);
723
+ export { DagGraph };
724
+ //# sourceMappingURL=dag-graph.js.map