@ruckc/truss-graph 1.48.1 → 1.49.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/dist/Graph.svelte +72 -15
- package/dist/Graph.svelte.d.ts +34 -0
- package/dist/index.d.ts +1 -0
- package/dist/labels.d.ts +6 -0
- package/dist/labels.js +23 -0
- package/package.json +1 -1
package/dist/Graph.svelte
CHANGED
|
@@ -1,4 +1,57 @@
|
|
|
1
|
+
<script module lang="ts">
|
|
2
|
+
export type GraphLabels = {
|
|
3
|
+
/** Accessible name of the graph when it is interactive and not editable (an `aria-label` attribute replaces it). */
|
|
4
|
+
graph: string;
|
|
5
|
+
/** Accessible name of an editable graph. */
|
|
6
|
+
graphEditor: string;
|
|
7
|
+
/** Accessible name of a static graph (`interactive={false}`). */
|
|
8
|
+
graphStatic: string;
|
|
9
|
+
/** Accessible name of an edge: the ids of its two nodes. */
|
|
10
|
+
edge: (from: string, to: string) => string;
|
|
11
|
+
/** Name of a category in the legend when `categories` does not name it; `n` counts from 1. */
|
|
12
|
+
category: (n: number) => string;
|
|
13
|
+
/** Name of a node or edge kind in the legend when the `legend` prop does not name it; `kind` is the kind (`success`, `danger`, ...). */
|
|
14
|
+
kind: (kind: string) => string;
|
|
15
|
+
/** Accessible name of the legend. */
|
|
16
|
+
legend: string;
|
|
17
|
+
/** Tooltip of a legend entry that shows or hides the edges of one kind. */
|
|
18
|
+
toggleEdges: (kind: string) => string;
|
|
19
|
+
/** Accessible name of the badge that expands a collapsed node: its label and how many nodes it hides. */
|
|
20
|
+
expand: (label: string, hidden: number) => string;
|
|
21
|
+
/** Accessible name of the minimap. */
|
|
22
|
+
minimap: string;
|
|
23
|
+
/** Accessible name of the toolbar of zoom buttons. */
|
|
24
|
+
controls: string;
|
|
25
|
+
/** Accessible name of the zoom-in button. */
|
|
26
|
+
zoomIn: string;
|
|
27
|
+
/** Accessible name of the zoom-out button. */
|
|
28
|
+
zoomOut: string;
|
|
29
|
+
/** Accessible name of the fit-to-view button. */
|
|
30
|
+
fitToView: string;
|
|
31
|
+
/** Accessible name of the auto-layout button. */
|
|
32
|
+
autoLayout: string;
|
|
33
|
+
};
|
|
34
|
+
const DEFAULTS: GraphLabels = {
|
|
35
|
+
graph: 'Graph. Arrow keys move between nodes, Enter collapses or expands, plus and minus zoom, 0 fits to view.',
|
|
36
|
+
graphEditor: 'Graph editor. Arrow keys move selected nodes, Delete removes, plus and minus zoom, 0 fits to view.',
|
|
37
|
+
graphStatic: 'Graph. Arrow keys move between nodes, Enter collapses or expands.',
|
|
38
|
+
edge: (from, to) => `Edge ${from} to ${to}`,
|
|
39
|
+
category: (n) => `Category ${n}`,
|
|
40
|
+
kind: (kind) => kind.charAt(0).toUpperCase() + kind.slice(1),
|
|
41
|
+
legend: 'Legend',
|
|
42
|
+
toggleEdges: (kind) => `Show or hide ${kind} edges`,
|
|
43
|
+
expand: (label, hidden) => `Expand ${label}, ${hidden} hidden`,
|
|
44
|
+
minimap: 'Minimap: drag to pan the view',
|
|
45
|
+
controls: 'Graph controls',
|
|
46
|
+
zoomIn: 'Zoom in',
|
|
47
|
+
zoomOut: 'Zoom out',
|
|
48
|
+
fitToView: 'Fit to view',
|
|
49
|
+
autoLayout: 'Auto layout'
|
|
50
|
+
};
|
|
51
|
+
</script>
|
|
52
|
+
|
|
1
53
|
<script lang="ts">
|
|
54
|
+
import { useLabels } from './labels.js';
|
|
2
55
|
import { cssvars } from './cssvars.js';
|
|
3
56
|
import SsrStyle from './SsrStyle.svelte';
|
|
4
57
|
const uid = $props.id();
|
|
@@ -43,6 +96,7 @@
|
|
|
43
96
|
onnodedblclick,
|
|
44
97
|
onnodehover,
|
|
45
98
|
onnodecontextmenu,
|
|
99
|
+
labels,
|
|
46
100
|
class: klass = '',
|
|
47
101
|
...rest
|
|
48
102
|
}: Omit<HTMLAttributes<HTMLDivElement>, 'children' | 'class' | 'role' | 'tabindex' | 'onchange' | 'onpointerdown' | 'onkeydown'> & {
|
|
@@ -106,8 +160,12 @@
|
|
|
106
160
|
onnodehover?: (node: GraphNode | null) => void;
|
|
107
161
|
/** Right-click, or ContextMenu / Shift+F10 on a focused node. The native menu is suppressed when this is set (except over inputs and links inside custom nodes). */
|
|
108
162
|
onnodecontextmenu?: (node: GraphNode, event: MouseEvent | KeyboardEvent) => void;
|
|
163
|
+
/** Replaces the built-in texts (translation). Missing keys keep the defaults. */
|
|
164
|
+
labels?: Partial<GraphLabels>;
|
|
109
165
|
} = $props();
|
|
110
166
|
|
|
167
|
+
const L = useLabels('Graph', DEFAULTS, () => labels);
|
|
168
|
+
|
|
111
169
|
// a static graph never edits, whatever `editable` says
|
|
112
170
|
const edit = $derived(editable && interactive);
|
|
113
171
|
let root: HTMLDivElement;
|
|
@@ -238,7 +296,7 @@
|
|
|
238
296
|
const needsObstacles = $derived(!big);
|
|
239
297
|
// strings built in script: interpolating them in markup would add unreachable `?? ''` branches
|
|
240
298
|
const px = (v: number) => `${v}px`;
|
|
241
|
-
const edgeAria = (e: GraphEdge) =>
|
|
299
|
+
const edgeAria = (e: GraphEdge) => L.edge(e.from, e.to);
|
|
242
300
|
const routeType = (t: EdgeType): EdgeType => (big && t === 'avoid' ? 'smoothstep' : t);
|
|
243
301
|
|
|
244
302
|
const findPort = (n: GraphNode, id: string | undefined, kind: 'in' | 'out') => {
|
|
@@ -505,7 +563,6 @@
|
|
|
505
563
|
|
|
506
564
|
// ---- legend
|
|
507
565
|
const { class: legendCls, ...legendRest } = $derived(legendAttrs ?? {});
|
|
508
|
-
const cap = (s: string) => s.charAt(0).toUpperCase() + s.slice(1);
|
|
509
566
|
// Graph-local copy of the chart palette: `--trc-s1..8` when the charts stylesheet is loaded, built-in values otherwise
|
|
510
567
|
const SLOTS = 8;
|
|
511
568
|
const slotVar = (c: number) => `var(--trg-s${((Math.trunc(c) % SLOTS) + SLOTS) % SLOTS + 1})`;
|
|
@@ -514,10 +571,10 @@
|
|
|
514
571
|
if (!legend) return null;
|
|
515
572
|
const kinds = (xs: { kind?: NodeKind }[]) => [...new Set(xs.map((x) => x.kind ?? 'neutral'))];
|
|
516
573
|
// coloured nodes are not described by their kind
|
|
517
|
-
const nodeRows = legend === true ? kinds(live.filter((n) => colorOf(n) == null)).map((x) => [x,
|
|
518
|
-
const edgeRows = legend === true ? kinds(edges).map((x) => [x,
|
|
574
|
+
const nodeRows = legend === true ? kinds(live.filter((n) => colorOf(n) == null)).map((x) => [x, L.kind(x)] as [string, string]) : Object.entries(legend.nodes ?? {}) as [string, string][];
|
|
575
|
+
const edgeRows = legend === true ? kinds(edges).map((x) => [x, L.kind(x)] as [string, string]) : Object.entries(legend.edges ?? {}) as [string, string][];
|
|
519
576
|
const used = legend === true || legend.categories !== false ? [...new Set(live.filter((n) => n.color == null && n.category != null).map((n) => n.category!))].sort((a, b) => a - b) : [];
|
|
520
|
-
const catRows = used.map((c) => [c, categories?.[c] ??
|
|
577
|
+
const catRows = used.map((c) => [c, categories?.[c] ?? L.category(c + 1)] as [number, string]);
|
|
521
578
|
return nodeRows.length || edgeRows.length || catRows.length ? { nodeRows, edgeRows, catRows } : null;
|
|
522
579
|
});
|
|
523
580
|
const toggleEdgeKind = (kind: string) => (hiddenEdgeKinds = hiddenEdgeKinds.includes(kind) ? hiddenEdgeKinds.filter((x) => x !== kind) : [...hiddenEdgeKinds, kind]);
|
|
@@ -596,7 +653,7 @@
|
|
|
596
653
|
<!-- svelte-ignore a11y_no_noninteractive_tabindex, a11y_no_noninteractive_element_interactions -->
|
|
597
654
|
<div
|
|
598
655
|
{...rest} class={'trg ' + klass} data-sv={uid} bind:this={root} bind:clientWidth={rw} bind:clientHeight={rh} role="application" tabindex="0" data-panning={panning} data-static={!interactive}
|
|
599
|
-
aria-label={rest['aria-label'] ?? (!interactive ?
|
|
656
|
+
aria-label={rest['aria-label'] ?? (!interactive ? L.graphStatic : editable ? L.graphEditor : L.graph)}
|
|
600
657
|
{@attach cssvars(() => ({ 'height': widthFit ? px(widthFit.h) : typeof height === 'number' ? `${height}px` : height }))}
|
|
601
658
|
{@attach cssvars(() => ({ '--tx': px(view.tx) }))} {@attach cssvars(() => ({ '--ty': px(view.ty) }))} {@attach cssvars(() => ({ '--k': view.k }))} {@attach cssvars(() => ({ '--trg-lines': Math.max(1, labelLines) }))}
|
|
602
659
|
onpointerdown={bgDown} onkeydown={onKey}
|
|
@@ -671,7 +728,7 @@
|
|
|
671
728
|
{/each}
|
|
672
729
|
{#if !lod && hiding.counts.has(n.id)}
|
|
673
730
|
{@const hidden = hiding.counts.get(n.id)!}
|
|
674
|
-
<button type="button" class="expander nodrag" aria-label={
|
|
731
|
+
<button type="button" class="expander nodrag" aria-label={L.expand(n.label, hidden)} onclick={(e) => { e.stopPropagation(); expand(n.id); }}>{`+${hidden}`}</button>
|
|
675
732
|
{/if}
|
|
676
733
|
</div>
|
|
677
734
|
{/each}
|
|
@@ -680,7 +737,7 @@
|
|
|
680
737
|
{#if mm}
|
|
681
738
|
<div class="minimap" data-pos={minimapPosition} {@attach cssvars(() => ({ 'width': `${MW}px` }))} {@attach cssvars(() => ({ 'height': `${MH}px` }))}>
|
|
682
739
|
<!-- svelte-ignore a11y_no_static_element_interactions -->
|
|
683
|
-
<svg width={mm.w} height={mm.h} role="img" aria-label=
|
|
740
|
+
<svg width={mm.w} height={mm.h} role="img" aria-label={L.minimap} onpointerdown={mmPan}>
|
|
684
741
|
{#each big ? [] : geo as g (g.e.id)}
|
|
685
742
|
<!-- `geo` only holds edges whose two ends are both in `byId` -->
|
|
686
743
|
{@const a = byId.get(g.e.from)!}{@const bn = byId.get(g.e.to)!}
|
|
@@ -695,7 +752,7 @@
|
|
|
695
752
|
{/if}
|
|
696
753
|
|
|
697
754
|
{#if legendData}
|
|
698
|
-
<div class={['legend', legendCls]} role="group" aria-label=
|
|
755
|
+
<div class={['legend', legendCls]} role="group" aria-label={L.legend} {...legendRest}>
|
|
699
756
|
{#each legendData.nodeRows as [kind, label] (kind)}
|
|
700
757
|
<span class="lg-item"><span class="lg-node" data-kind={kind}></span>{label}</span>
|
|
701
758
|
{/each}
|
|
@@ -703,7 +760,7 @@
|
|
|
703
760
|
<span class="lg-item"><span class="lg-node" {@attach cssvars(() => ({ '--c': slotVar(cat) }))}></span>{label}</span>
|
|
704
761
|
{/each}
|
|
705
762
|
{#each legendData.edgeRows as [kind, label] (kind)}
|
|
706
|
-
<button type="button" class="lg-item lg-edge" data-kind={kind} aria-pressed={!hiddenEdgeKinds.includes(kind)} title={
|
|
763
|
+
<button type="button" class="lg-item lg-edge" data-kind={kind} aria-pressed={!hiddenEdgeKinds.includes(kind)} title={L.toggleEdges(label)} onclick={() => toggleEdgeKind(kind)}>
|
|
707
764
|
<span class="lg-line"></span>{label}
|
|
708
765
|
</button>
|
|
709
766
|
{/each}
|
|
@@ -714,11 +771,11 @@
|
|
|
714
771
|
<div class="trg-overlay" bind:this={overlay}></div>
|
|
715
772
|
|
|
716
773
|
{#if interactive}
|
|
717
|
-
<div class="controls" role="toolbar" aria-label=
|
|
718
|
-
<Button variant="minimal" intent="neutral" size="sm" icon="plus" aria-label=
|
|
719
|
-
<Button variant="minimal" intent="neutral" size="sm" icon="minus" aria-label=
|
|
720
|
-
<Button variant="minimal" intent="neutral" size="sm" icon="expand" aria-label=
|
|
721
|
-
{#if edit}<Button variant="minimal" intent="neutral" size="sm" icon="refresh" aria-label=
|
|
774
|
+
<div class="controls" role="toolbar" aria-label={L.controls}>
|
|
775
|
+
<Button variant="minimal" intent="neutral" size="sm" icon="plus" aria-label={L.zoomIn} onclick={() => zoomCenter(1.2)} />
|
|
776
|
+
<Button variant="minimal" intent="neutral" size="sm" icon="minus" aria-label={L.zoomOut} onclick={() => zoomCenter(1 / 1.2)} />
|
|
777
|
+
<Button variant="minimal" intent="neutral" size="sm" icon="expand" aria-label={L.fitToView} onclick={() => fit()} />
|
|
778
|
+
{#if edit}<Button variant="minimal" intent="neutral" size="sm" icon="refresh" aria-label={L.autoLayout} onclick={() => layout()} />{/if}
|
|
722
779
|
</div>
|
|
723
780
|
{/if}
|
|
724
781
|
</div>
|
package/dist/Graph.svelte.d.ts
CHANGED
|
@@ -1,3 +1,35 @@
|
|
|
1
|
+
export type GraphLabels = {
|
|
2
|
+
/** Accessible name of the graph when it is interactive and not editable (an `aria-label` attribute replaces it). */
|
|
3
|
+
graph: string;
|
|
4
|
+
/** Accessible name of an editable graph. */
|
|
5
|
+
graphEditor: string;
|
|
6
|
+
/** Accessible name of a static graph (`interactive={false}`). */
|
|
7
|
+
graphStatic: string;
|
|
8
|
+
/** Accessible name of an edge: the ids of its two nodes. */
|
|
9
|
+
edge: (from: string, to: string) => string;
|
|
10
|
+
/** Name of a category in the legend when `categories` does not name it; `n` counts from 1. */
|
|
11
|
+
category: (n: number) => string;
|
|
12
|
+
/** Name of a node or edge kind in the legend when the `legend` prop does not name it; `kind` is the kind (`success`, `danger`, ...). */
|
|
13
|
+
kind: (kind: string) => string;
|
|
14
|
+
/** Accessible name of the legend. */
|
|
15
|
+
legend: string;
|
|
16
|
+
/** Tooltip of a legend entry that shows or hides the edges of one kind. */
|
|
17
|
+
toggleEdges: (kind: string) => string;
|
|
18
|
+
/** Accessible name of the badge that expands a collapsed node: its label and how many nodes it hides. */
|
|
19
|
+
expand: (label: string, hidden: number) => string;
|
|
20
|
+
/** Accessible name of the minimap. */
|
|
21
|
+
minimap: string;
|
|
22
|
+
/** Accessible name of the toolbar of zoom buttons. */
|
|
23
|
+
controls: string;
|
|
24
|
+
/** Accessible name of the zoom-in button. */
|
|
25
|
+
zoomIn: string;
|
|
26
|
+
/** Accessible name of the zoom-out button. */
|
|
27
|
+
zoomOut: string;
|
|
28
|
+
/** Accessible name of the fit-to-view button. */
|
|
29
|
+
fitToView: string;
|
|
30
|
+
/** Accessible name of the auto-layout button. */
|
|
31
|
+
autoLayout: string;
|
|
32
|
+
};
|
|
1
33
|
import { type Snippet } from 'svelte';
|
|
2
34
|
import type { HTMLAttributes } from 'svelte/elements';
|
|
3
35
|
import { type EdgeType, type GraphEdge, type GraphLayout, type GraphLayoutOptions, type GraphLegend, type GraphNode, type HighlightDirection, type NodeRenderer } from './types.js';
|
|
@@ -68,6 +100,8 @@ type $$ComponentProps = Omit<HTMLAttributes<HTMLDivElement>, 'children' | 'class
|
|
|
68
100
|
onnodehover?: (node: GraphNode | null) => void;
|
|
69
101
|
/** Right-click, or ContextMenu / Shift+F10 on a focused node. The native menu is suppressed when this is set (except over inputs and links inside custom nodes). */
|
|
70
102
|
onnodecontextmenu?: (node: GraphNode, event: MouseEvent | KeyboardEvent) => void;
|
|
103
|
+
/** Replaces the built-in texts (translation). Missing keys keep the defaults. */
|
|
104
|
+
labels?: Partial<GraphLabels>;
|
|
71
105
|
};
|
|
72
106
|
declare const Graph: import("svelte").Component<$$ComponentProps, {
|
|
73
107
|
fit: (padding?: number) => void;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
export { default as Graph } from './Graph.svelte';
|
|
2
|
+
export type { GraphLabels } from './Graph.svelte';
|
|
2
3
|
export { autoLayout, forceLayout, radialLayout } from './layout.js';
|
|
3
4
|
export { routeEdge, portAnchor, defaultPorts } from './routing.js';
|
|
4
5
|
export type { GraphNode, GraphEdge, GraphPort, NodeKind, NodeCtx, NodeRenderer, EdgeType, Side, NodeShape, GraphLayout, GraphLegend, HighlightDirection, GraphLayoutOptions } from './types.js';
|
package/dist/labels.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/** One label: a string, or a function for a string with values (`(n: number) => \`${n} selected\``). That covers plurals and word order without an ICU dependency. */
|
|
2
|
+
export type LabelValue = string | ((...args: never[]) => string);
|
|
3
|
+
/** @internal The labels of a component: `prop` over the context over `configureLabels` over `defaults`, read at the moment a key is used. Call during component initialisation (it reads context). */
|
|
4
|
+
export declare function useLabels<T extends object>(component: string, defaults: T, prop: () => Partial<T> | undefined): T;
|
|
5
|
+
/** @internal One app-wide label for code outside a component (no context there): `configureLabels` over `fallback`. */
|
|
6
|
+
export declare function labelFor<T extends LabelValue>(component: string, key: string, fallback: T): T;
|
package/dist/labels.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { getContext } from 'svelte';
|
|
2
|
+
const KEY = Symbol.for('@ruckc/truss-ui:labels');
|
|
3
|
+
const STATE = Symbol.for('@ruckc/truss-ui:labels-config');
|
|
4
|
+
// What `configureLabels` set for a component. Read-only here (no copy of this file has to create the state), and `Object(undefined)` is `{}`.
|
|
5
|
+
const config = (component) => Object(Object(globalThis[STATE])[component]);
|
|
6
|
+
// The strongest defined value wins (layers run from the weakest to the strongest). This reader is written without branches on purpose
|
|
7
|
+
// (`filter`, `Object(x)`, no `?.` / `??` / `if`): it is copied into every package, and V8 coverage counts each short-circuit as a branch,
|
|
8
|
+
// so a copy would need a test under every combination of context and config to reach the per-file coverage floor.
|
|
9
|
+
const none = () => undefined;
|
|
10
|
+
const pick = (...layers) => layers.filter((v) => v !== undefined).at(-1);
|
|
11
|
+
/** @internal The labels of a component: `prop` over the context over `configureLabels` over `defaults`, read at the moment a key is used. Call during component initialisation (it reads context). */
|
|
12
|
+
export function useLabels(component, defaults, prop) {
|
|
13
|
+
const lookup = [getContext(KEY), none].filter(Boolean)[0];
|
|
14
|
+
const out = {};
|
|
15
|
+
for (const k of Object.keys(defaults))
|
|
16
|
+
Object.defineProperty(out, k, { enumerable: true, get: () => pick(labelFor(component, k, defaults[k]), Object(lookup(component))[k], Object(prop())[k]) });
|
|
17
|
+
return out;
|
|
18
|
+
}
|
|
19
|
+
/** @internal One app-wide label for code outside a component (no context there): `configureLabels` over `fallback`. */
|
|
20
|
+
export function labelFor(component, key, fallback) {
|
|
21
|
+
return pick(fallback, config(component)[key]);
|
|
22
|
+
}
|
|
23
|
+
// </reader>
|