@molecule/app-feature-graph-view-angular 0.0.0-stage → 1.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 +202 -0
- package/README.md +708 -2
- package/dist/graph-view.component.d.ts +123 -0
- package/dist/graph-view.component.d.ts.map +1 -0
- package/dist/graph-view.component.js +393 -0
- package/dist/graph-view.component.js.map +1 -0
- package/dist/index.d.ts +67 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +67 -0
- package/dist/index.js.map +1 -0
- package/dist/layout.d.ts +101 -0
- package/dist/layout.d.ts.map +1 -0
- package/dist/layout.js +239 -0
- package/dist/layout.js.map +1 -0
- package/dist/types.d.ts +99 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/package.json +73 -4
package/README.md
CHANGED
|
@@ -1,3 +1,709 @@
|
|
|
1
|
-
|
|
1
|
+
<!--
|
|
2
|
+
AUTO-GENERATED — DO NOT EDIT THIS FILE.
|
|
3
|
+
Generated by `mlcl sync-docs` from the package's src/index.ts JSDoc + mlcl/registry.json.
|
|
4
|
+
Edits here are overwritten on the next commit (molecule's pre-commit hook regenerates).
|
|
5
|
+
To change this document, edit the module-level JSDoc in src/index.ts.
|
|
6
|
+
Generated: 2026-11-21T12:46:30.340Z
|
|
7
|
+
-->
|
|
2
8
|
|
|
3
|
-
|
|
9
|
+
# @molecule/app-feature-graph-view-angular
|
|
10
|
+
|
|
11
|
+
Angular force-directed graph view (Obsidian-style) — the counterpart of
|
|
12
|
+
`@molecule/app-feature-graph-view-react`: the same exports, the same props
|
|
13
|
+
(React names), and the same DOM for the same input.
|
|
14
|
+
|
|
15
|
+
Exports:
|
|
16
|
+
|
|
17
|
+
- `<GraphView>` (`div[mol-graph-view]`) — top-level node-and-edge
|
|
18
|
+
visualization with built-in `force`, `circular`, and `grid` layouts,
|
|
19
|
+
click + selection support, and pluggable node / edge templates.
|
|
20
|
+
- `GraphNode`, `GraphEdge`, `GraphLayout`, `GraphPoint`, `PositionedNode`,
|
|
21
|
+
`GraphNodeRenderer`, `GraphEdgeRenderer`, `GraphViewProps` types.
|
|
22
|
+
- Pure layout helpers (`forceLayout`, `circularLayout`, `gridLayout`,
|
|
23
|
+
`layoutNodes`, `boundingBox`, `createRng`).
|
|
24
|
+
|
|
25
|
+
Used by note-taking and other apps to visualize page / note linkages
|
|
26
|
+
the same way Obsidian does — a force-directed map of which notes link
|
|
27
|
+
to which.
|
|
28
|
+
|
|
29
|
+
> **Auto-generated, AI-first package reference** for the [molecule.dev](https://molecule.dev) ecosystem.
|
|
30
|
+
> It is written to be read by coding agents as much as by people, and is generated from this
|
|
31
|
+
> package's source — edit `src/index.ts` JSDoc, not this file.
|
|
32
|
+
|
|
33
|
+
## Quick Start
|
|
34
|
+
|
|
35
|
+
```ts
|
|
36
|
+
import { Component } from '@angular/core'
|
|
37
|
+
import { GraphView, type GraphEdge, type GraphNode } from '@molecule/app-feature-graph-view-angular'
|
|
38
|
+
|
|
39
|
+
const noteNodes: GraphNode[] = [
|
|
40
|
+
{ id: 'a', label: 'Alpha' },
|
|
41
|
+
{ id: 'b', label: 'Beta', group: 'project', weight: 2 },
|
|
42
|
+
]
|
|
43
|
+
const linkEdges: GraphEdge[] = [{ id: 'e1', source: 'a', target: 'b' }]
|
|
44
|
+
|
|
45
|
+
@Component({
|
|
46
|
+
selector: 'app-note-graph',
|
|
47
|
+
standalone: true,
|
|
48
|
+
imports: [GraphView],
|
|
49
|
+
template: `<div mol-graph-view [nodes]="nodes" [edges]="edges" [onNodeClick]="open"></div>`,
|
|
50
|
+
})
|
|
51
|
+
export class NoteGraph {
|
|
52
|
+
nodes = noteNodes
|
|
53
|
+
edges = linkEdges
|
|
54
|
+
open(node: GraphNode): void {
|
|
55
|
+
console.log(`open ${node.id}`)
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## Type
|
|
61
|
+
|
|
62
|
+
`feature`
|
|
63
|
+
|
|
64
|
+
## Installation
|
|
65
|
+
|
|
66
|
+
```bash
|
|
67
|
+
npm install @molecule/app-feature-graph-view-angular @angular/common @angular/core @angular/platform-browser @molecule/app-angular @molecule/app-ui @molecule/app-ui-angular
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
## API
|
|
71
|
+
|
|
72
|
+
### Interfaces
|
|
73
|
+
|
|
74
|
+
#### `ForceLayoutOptions`
|
|
75
|
+
|
|
76
|
+
Force-layout tunables.
|
|
77
|
+
|
|
78
|
+
```typescript
|
|
79
|
+
interface ForceLayoutOptions {
|
|
80
|
+
/** Hard cap on iterations. Defaults to {@link DEFAULT_FORCE_ITERATIONS}. */
|
|
81
|
+
iterations?: number
|
|
82
|
+
/** Repulsive constant between every pair of nodes. */
|
|
83
|
+
repulsion?: number
|
|
84
|
+
/** Spring (Hooke) constant for edges. */
|
|
85
|
+
spring?: number
|
|
86
|
+
/** Ideal edge length (rest distance). */
|
|
87
|
+
restLength?: number
|
|
88
|
+
/** Velocity damping per step (0–1). */
|
|
89
|
+
damping?: number
|
|
90
|
+
/** Optional deterministic seed (for tests). Defaults to a fixed value. */
|
|
91
|
+
seed?: number
|
|
92
|
+
}
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
#### `GraphPoint`
|
|
96
|
+
|
|
97
|
+
A 2-D point in graph (world) coordinates.
|
|
98
|
+
|
|
99
|
+
```typescript
|
|
100
|
+
interface GraphPoint {
|
|
101
|
+
/** X coordinate. */
|
|
102
|
+
x: number
|
|
103
|
+
/** Y coordinate. */
|
|
104
|
+
y: number
|
|
105
|
+
}
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
#### `GraphNode`
|
|
109
|
+
|
|
110
|
+
Single node in the graph.
|
|
111
|
+
|
|
112
|
+
`group` is an opaque grouping discriminator (e.g. tag, folder) the
|
|
113
|
+
caller can use for colouring; `weight` scales the rendered radius.
|
|
114
|
+
|
|
115
|
+
```typescript
|
|
116
|
+
interface GraphNode {
|
|
117
|
+
/** Unique node id. */
|
|
118
|
+
id: string
|
|
119
|
+
/** Human-readable label rendered next to the node. */
|
|
120
|
+
label: string
|
|
121
|
+
/** Optional grouping discriminator (e.g. tag id). */
|
|
122
|
+
group?: string
|
|
123
|
+
/** Optional weight; default 1. Scales the rendered radius. */
|
|
124
|
+
weight?: number
|
|
125
|
+
}
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
#### `GraphEdge`
|
|
129
|
+
|
|
130
|
+
Edge connecting two nodes by id.
|
|
131
|
+
|
|
132
|
+
```typescript
|
|
133
|
+
interface GraphEdge {
|
|
134
|
+
/** Unique edge id. */
|
|
135
|
+
id: string
|
|
136
|
+
/** Source node id. */
|
|
137
|
+
source: string
|
|
138
|
+
/** Target node id. */
|
|
139
|
+
target: string
|
|
140
|
+
/** Optional weight; default 1. Stronger edges pull harder under force layout. */
|
|
141
|
+
weight?: number
|
|
142
|
+
}
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
#### `PositionedNode`
|
|
146
|
+
|
|
147
|
+
Internal positioned node used by the renderer; the `x`/`y` coordinates
|
|
148
|
+
are in world (graph) space, with `(0, 0)` at the canvas centre.
|
|
149
|
+
|
|
150
|
+
```typescript
|
|
151
|
+
interface PositionedNode extends GraphNode {
|
|
152
|
+
/** World-space X coordinate. */
|
|
153
|
+
x: number
|
|
154
|
+
/** World-space Y coordinate. */
|
|
155
|
+
y: number
|
|
156
|
+
}
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
#### `GraphViewProps`
|
|
160
|
+
|
|
161
|
+
Props accepted by {@link GraphView} — the same props
|
|
162
|
+
`@molecule/app-feature-graph-view-react` accepts. The renderer props are
|
|
163
|
+
Angular templates (a node cannot carry a function), so they are written in
|
|
164
|
+
the host template, not bound as values.
|
|
165
|
+
|
|
166
|
+
```typescript
|
|
167
|
+
interface GraphViewProps {
|
|
168
|
+
/** Nodes to render. */
|
|
169
|
+
nodes: GraphNode[]
|
|
170
|
+
/** Edges to render. */
|
|
171
|
+
edges: GraphEdge[]
|
|
172
|
+
/** Fired when the user clicks (or activates) a node. */
|
|
173
|
+
onNodeClick?: (node: GraphNode) => void
|
|
174
|
+
/** Currently-selected node id (highlighted in the view). */
|
|
175
|
+
selectedNodeId?: string
|
|
176
|
+
/** Layout strategy. Defaults to `'force'`. */
|
|
177
|
+
layout?: GraphLayout
|
|
178
|
+
/** Custom node renderer (overrides the default circle + label). */
|
|
179
|
+
nodeRenderer?: GraphNodeRenderer
|
|
180
|
+
/** Custom edge renderer (overrides the default `<line>`). */
|
|
181
|
+
edgeRenderer?: GraphEdgeRenderer
|
|
182
|
+
/** Force-layout tunables; ignored for non-force layouts. */
|
|
183
|
+
forceOptions?: ForceLayoutOptions
|
|
184
|
+
/** Extra classes merged onto the outer wrapper. */
|
|
185
|
+
className?: string
|
|
186
|
+
}
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
### Types
|
|
190
|
+
|
|
191
|
+
#### `GraphLayout`
|
|
192
|
+
|
|
193
|
+
Available layout strategies.
|
|
194
|
+
|
|
195
|
+
```typescript
|
|
196
|
+
type GraphLayout = 'force' | 'circular' | 'grid'
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
#### `GraphNodeRenderer`
|
|
200
|
+
|
|
201
|
+
Renderer for an individual node: an Angular template rendered through
|
|
202
|
+
`ngTemplateOutlet`, with the positioned node as `a0` (`$implicit` is not
|
|
203
|
+
used, so the template reads `let-node="a0"`).
|
|
204
|
+
|
|
205
|
+
```typescript
|
|
206
|
+
type GraphNodeRenderer = TemplateRef<{ a0: PositionedNode }>
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
#### `GraphEdgeRenderer`
|
|
210
|
+
|
|
211
|
+
Renderer for an individual edge: a template rendered with the edge as `a0`,
|
|
212
|
+
the source node as `a1` and the target node as `a2`.
|
|
213
|
+
|
|
214
|
+
```typescript
|
|
215
|
+
type GraphEdgeRenderer = TemplateRef<{
|
|
216
|
+
a0: GraphEdge
|
|
217
|
+
a1: PositionedNode
|
|
218
|
+
a2: PositionedNode
|
|
219
|
+
}>
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
### Classes
|
|
223
|
+
|
|
224
|
+
#### `GraphView`
|
|
225
|
+
|
|
226
|
+
Force-directed graph view (Obsidian-style).
|
|
227
|
+
|
|
228
|
+
Renders nodes + edges into an SVG, computing positions via a built-in
|
|
229
|
+
minimal velocity-Verlet simulation. Three layouts are supported —
|
|
230
|
+
`force` (default), `circular` and `grid` — and either nodes or edges
|
|
231
|
+
can be overridden via custom templates.
|
|
232
|
+
|
|
233
|
+
The host IS the outer `<div>` (`div[mol-graph-view]`), matching React's root
|
|
234
|
+
exactly. Style is driven by `getClassMap()`. Inline styles are reserved for
|
|
235
|
+
SVG geometry — `transform`, viewBox computation, stroke widths — which
|
|
236
|
+
classes can't express.
|
|
237
|
+
|
|
238
|
+
```typescript
|
|
239
|
+
@Component({
|
|
240
|
+
selector: 'div[mol-graph-view]',
|
|
241
|
+
standalone: true,
|
|
242
|
+
imports: [NgTemplateOutlet],
|
|
243
|
+
inputs: [
|
|
244
|
+
'nodes',
|
|
245
|
+
'edges',
|
|
246
|
+
'onNodeClick',
|
|
247
|
+
'selectedNodeId',
|
|
248
|
+
'layout',
|
|
249
|
+
'nodeRenderer',
|
|
250
|
+
'edgeRenderer',
|
|
251
|
+
'forceOptions',
|
|
252
|
+
'className',
|
|
253
|
+
],
|
|
254
|
+
host: {
|
|
255
|
+
role: 'application',
|
|
256
|
+
'[attr.aria-label]': 'ariaLabel()',
|
|
257
|
+
'data-mol-id': 'graph-view',
|
|
258
|
+
'[class]': 'classes()',
|
|
259
|
+
'[style.width]': '"100%"',
|
|
260
|
+
'[style.height]': '"100%"',
|
|
261
|
+
'[style.overflow]': '"hidden"',
|
|
262
|
+
},
|
|
263
|
+
template: `@if (positionedNodes().length === 0) {
|
|
264
|
+
<div
|
|
265
|
+
data-mol-id="graph-view-empty"
|
|
266
|
+
role="status"
|
|
267
|
+
[class]="emptyClasses()"
|
|
268
|
+
[style.inset]="'0px'"
|
|
269
|
+
[style.display]="'grid'"
|
|
270
|
+
[style.place-items]="'center'"
|
|
271
|
+
>
|
|
272
|
+
{{ emptyLabel() }}
|
|
273
|
+
</div>
|
|
274
|
+
} @else {
|
|
275
|
+
<svg
|
|
276
|
+
data-mol-id="graph-view-svg"
|
|
277
|
+
width="100%"
|
|
278
|
+
height="100%"
|
|
279
|
+
[attr.viewBox]="viewBox()"
|
|
280
|
+
preserveAspectRatio="xMidYMid meet"
|
|
281
|
+
[style.display]="'block'"
|
|
282
|
+
>
|
|
283
|
+
<g data-mol-id="graph-view-edges">
|
|
284
|
+
@for (item of renderableEdges(); track item.edge.id) {
|
|
285
|
+
@if (edgeRenderer) {
|
|
286
|
+
<g [attr.data-mol-id]="'graph-view-edge-' + item.edge.id">
|
|
287
|
+
<ng-container
|
|
288
|
+
[ngTemplateOutlet]="edgeRenderer"
|
|
289
|
+
[ngTemplateOutletContext]="{ a0: item.edge, a1: item.src, a2: item.tgt }"
|
|
290
|
+
/>
|
|
291
|
+
</g>
|
|
292
|
+
} @else {
|
|
293
|
+
<line
|
|
294
|
+
[attr.data-mol-id]="'graph-view-edge-' + item.edge.id"
|
|
295
|
+
[attr.x1]="item.src.x"
|
|
296
|
+
[attr.y1]="item.src.y"
|
|
297
|
+
[attr.x2]="item.tgt.x"
|
|
298
|
+
[attr.y2]="item.tgt.y"
|
|
299
|
+
stroke="currentColor"
|
|
300
|
+
stroke-width="1"
|
|
301
|
+
opacity="0.4"
|
|
302
|
+
/>
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
</g>
|
|
306
|
+
<g data-mol-id="graph-view-nodes">
|
|
307
|
+
@for (node of positionedNodes(); track node.id) {
|
|
308
|
+
<g
|
|
309
|
+
[attr.data-mol-id]="'graph-view-node-hit-' + node.id"
|
|
310
|
+
role="button"
|
|
311
|
+
[attr.aria-label]="nodeAria(node)"
|
|
312
|
+
tabindex="0"
|
|
313
|
+
(click)="activate(node)"
|
|
314
|
+
(keydown)="keydown($event, node)"
|
|
315
|
+
[style.cursor]="onNodeClick ? 'pointer' : 'default'"
|
|
316
|
+
[style.outline]="'none'"
|
|
317
|
+
>
|
|
318
|
+
@if (nodeRenderer) {
|
|
319
|
+
<ng-container
|
|
320
|
+
[ngTemplateOutlet]="nodeRenderer"
|
|
321
|
+
[ngTemplateOutletContext]="{ a0: node }"
|
|
322
|
+
/>
|
|
323
|
+
} @else {
|
|
324
|
+
<g
|
|
325
|
+
[attr.data-mol-id]="'graph-view-node-' + node.id"
|
|
326
|
+
[attr.data-selected]="isSelected(node) ? 'true' : 'false'"
|
|
327
|
+
[attr.transform]="'translate(' + node.x + ', ' + node.y + ')'"
|
|
328
|
+
>
|
|
329
|
+
<circle
|
|
330
|
+
[attr.r]="radius(node)"
|
|
331
|
+
fill="currentColor"
|
|
332
|
+
[attr.opacity]="isSelected(node) ? 1 : 0.85"
|
|
333
|
+
stroke="currentColor"
|
|
334
|
+
[attr.stroke-width]="isSelected(node) ? 2 : 0"
|
|
335
|
+
/>
|
|
336
|
+
<text
|
|
337
|
+
[attr.x]="radius(node) + 4"
|
|
338
|
+
y="4"
|
|
339
|
+
font-size="11"
|
|
340
|
+
fill="currentColor"
|
|
341
|
+
[attr.opacity]="isSelected(node) ? 1 : 0.75"
|
|
342
|
+
[style.pointer-events]="'none'"
|
|
343
|
+
[style.user-select]="'none'"
|
|
344
|
+
>
|
|
345
|
+
{{ node.label }}
|
|
346
|
+
</text>
|
|
347
|
+
</g>
|
|
348
|
+
}
|
|
349
|
+
</g>
|
|
350
|
+
}
|
|
351
|
+
</g>
|
|
352
|
+
</svg>
|
|
353
|
+
}`,
|
|
354
|
+
})
|
|
355
|
+
export class GraphView {
|
|
356
|
+
/** Nodes to render. */
|
|
357
|
+
nodes?: GraphNode[]
|
|
358
|
+
/** Edges to render. */
|
|
359
|
+
edges?: GraphEdge[]
|
|
360
|
+
/** Fired when the user clicks (or activates) a node. */
|
|
361
|
+
onNodeClick?: (node: GraphNode) => void
|
|
362
|
+
/** Currently-selected node id (highlighted in the view). */
|
|
363
|
+
selectedNodeId?: string
|
|
364
|
+
/** Layout strategy (defaults to `'force'` at read time). */
|
|
365
|
+
layout?: GraphLayout
|
|
366
|
+
/** Custom node renderer template. */
|
|
367
|
+
nodeRenderer?: GraphNodeRenderer
|
|
368
|
+
/** Custom edge renderer template. */
|
|
369
|
+
edgeRenderer?: GraphEdgeRenderer
|
|
370
|
+
/** Force-layout tunables; ignored for non-force layouts. */
|
|
371
|
+
forceOptions?: ForceLayoutOptions
|
|
372
|
+
/** Extra classes merged onto the outer wrapper. */
|
|
373
|
+
className?: string
|
|
374
|
+
|
|
375
|
+
/** The bonded translation function. */
|
|
376
|
+
private readonly i18n = useTranslation()
|
|
377
|
+
|
|
378
|
+
/**
|
|
379
|
+
* The current class map.
|
|
380
|
+
* @returns The UIClassMap.
|
|
381
|
+
*/
|
|
382
|
+
cm(): UIClassMap {
|
|
383
|
+
return getClassMap()
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
/**
|
|
387
|
+
* Computes the wrapper class list.
|
|
388
|
+
* @returns The class string.
|
|
389
|
+
*/
|
|
390
|
+
classes(): string {
|
|
391
|
+
return this.cm().cn(this.cm().position('relative'), this.cm().surfaceSecondary, this.className)
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
/**
|
|
395
|
+
* Computes the empty-state class list.
|
|
396
|
+
* @returns The class string.
|
|
397
|
+
*/
|
|
398
|
+
emptyClasses(): string {
|
|
399
|
+
return this.cm().cn(this.cm().position('absolute'), this.cm().sp('p', 6))
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
/**
|
|
403
|
+
* The canvas' accessible name.
|
|
404
|
+
* @returns The translated label.
|
|
405
|
+
*/
|
|
406
|
+
ariaLabel(): string {
|
|
407
|
+
return this.i18n.t('graphView.aria.canvas', {}, { defaultValue: 'Graph view' })
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
/**
|
|
411
|
+
* The empty-state message.
|
|
412
|
+
* @returns The translated message.
|
|
413
|
+
*/
|
|
414
|
+
emptyLabel(): string {
|
|
415
|
+
return this.i18n.t('graphView.empty', {}, { defaultValue: 'No nodes to display' })
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
/**
|
|
419
|
+
* One node hit group's accessible name.
|
|
420
|
+
* @param node - The node.
|
|
421
|
+
* @returns The translated label.
|
|
422
|
+
*/
|
|
423
|
+
nodeAria(node: GraphNode): string {
|
|
424
|
+
return this.i18n.t(
|
|
425
|
+
'graphView.aria.node',
|
|
426
|
+
{ label: node.label },
|
|
427
|
+
{ defaultValue: 'Node {{label}}' },
|
|
428
|
+
)
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
/**
|
|
432
|
+
* World-space positions for every node, from the chosen layout. The force
|
|
433
|
+
* simulation is deterministic given a fixed seed, so repeated reads (change
|
|
434
|
+
* detection runs several) produce identical coordinates.
|
|
435
|
+
* @returns The positioned nodes.
|
|
436
|
+
*/
|
|
437
|
+
positionedNodes(): PositionedNode[] {
|
|
438
|
+
return layoutNodes(
|
|
439
|
+
this.nodes ?? [],
|
|
440
|
+
this.edges ?? [],
|
|
441
|
+
this.layout ?? 'force',
|
|
442
|
+
this.forceOptions,
|
|
443
|
+
)
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
/**
|
|
447
|
+
* The edges whose endpoints both exist, with both endpoints resolved.
|
|
448
|
+
* @returns The renderable edges, in input order.
|
|
449
|
+
*/
|
|
450
|
+
renderableEdges(): RenderableEdge[] {
|
|
451
|
+
const byId = new Map<string, PositionedNode>()
|
|
452
|
+
for (const node of this.positionedNodes()) byId.set(node.id, node)
|
|
453
|
+
const out: RenderableEdge[] = []
|
|
454
|
+
for (const edge of this.edges ?? []) {
|
|
455
|
+
const src = byId.get(edge.source)
|
|
456
|
+
const tgt = byId.get(edge.target)
|
|
457
|
+
if (src && tgt) out.push({ edge, src, tgt })
|
|
458
|
+
}
|
|
459
|
+
return out
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
/**
|
|
463
|
+
* The SVG viewBox that fits the layout bounds.
|
|
464
|
+
* @returns The viewBox string.
|
|
465
|
+
*/
|
|
466
|
+
viewBox(): string {
|
|
467
|
+
const box = boundingBox(this.positionedNodes(), 40)
|
|
468
|
+
return box
|
|
469
|
+
? `${box.minX} ${box.minY} ${Math.max(1, box.maxX - box.minX)} ${Math.max(1, box.maxY - box.minY)}`
|
|
470
|
+
: '-100 -100 200 200'
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
/**
|
|
474
|
+
* Whether a node is the selected one.
|
|
475
|
+
* @param node - The node.
|
|
476
|
+
* @returns True when it matches `selectedNodeId`.
|
|
477
|
+
*/
|
|
478
|
+
isSelected(node: GraphNode): boolean {
|
|
479
|
+
return node.id === this.selectedNodeId
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
/**
|
|
483
|
+
* The rendered radius of a node, scaled by its weight.
|
|
484
|
+
* @param node - The node.
|
|
485
|
+
* @returns The radius in world units.
|
|
486
|
+
*/
|
|
487
|
+
radius(node: GraphNode): number {
|
|
488
|
+
return NODE_RADIUS * Math.sqrt(Math.max(0.25, node.weight ?? 1))
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
/**
|
|
492
|
+
* Calls `onNodeClick` for a node.
|
|
493
|
+
* @param node - The node.
|
|
494
|
+
*/
|
|
495
|
+
activate(node: GraphNode): void {
|
|
496
|
+
this.onNodeClick?.(node)
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
/**
|
|
500
|
+
* Activates a node on Enter or Space, as React's keydown handler does.
|
|
501
|
+
* @param event - The keyboard event.
|
|
502
|
+
* @param node - The node.
|
|
503
|
+
*/
|
|
504
|
+
keydown(event: KeyboardEvent, node: GraphNode): void {
|
|
505
|
+
if (event.key === 'Enter' || event.key === ' ') {
|
|
506
|
+
event.preventDefault()
|
|
507
|
+
this.onNodeClick?.(node)
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
```
|
|
512
|
+
|
|
513
|
+
### Functions
|
|
514
|
+
|
|
515
|
+
#### `createRng(seed)`
|
|
516
|
+
|
|
517
|
+
Tiny deterministic PRNG (mulberry32). Allows reproducible initial
|
|
518
|
+
positions in tests without depending on `Math.random()`.
|
|
519
|
+
|
|
520
|
+
```typescript
|
|
521
|
+
function createRng(seed: number): () => number
|
|
522
|
+
```
|
|
523
|
+
|
|
524
|
+
- `seed` — 32-bit unsigned seed.
|
|
525
|
+
|
|
526
|
+
**Returns:** A function returning floats in `[0, 1)`.
|
|
527
|
+
|
|
528
|
+
#### `circularLayout(nodes, radius)`
|
|
529
|
+
|
|
530
|
+
Place nodes evenly around the unit circle (scaled by `radius`).
|
|
531
|
+
|
|
532
|
+
```typescript
|
|
533
|
+
function circularLayout(nodes: GraphNode[], radius: number): PositionedNode[]
|
|
534
|
+
```
|
|
535
|
+
|
|
536
|
+
- `nodes` — Nodes to position.
|
|
537
|
+
- `radius` — Circle radius in world units.
|
|
538
|
+
|
|
539
|
+
**Returns:** Positioned nodes, in input order.
|
|
540
|
+
|
|
541
|
+
#### `gridLayout(nodes, spacing)`
|
|
542
|
+
|
|
543
|
+
Place nodes on a square-ish grid, centred on the origin.
|
|
544
|
+
|
|
545
|
+
```typescript
|
|
546
|
+
function gridLayout(nodes: GraphNode[], spacing: number): PositionedNode[]
|
|
547
|
+
```
|
|
548
|
+
|
|
549
|
+
- `nodes` — Nodes to position.
|
|
550
|
+
- `spacing` — Pixel spacing between adjacent grid cells.
|
|
551
|
+
|
|
552
|
+
**Returns:** Positioned nodes, in input order.
|
|
553
|
+
|
|
554
|
+
#### `forceLayout(nodes, edges, options)`
|
|
555
|
+
|
|
556
|
+
Run a minimal velocity-Verlet force-directed simulation.
|
|
557
|
+
|
|
558
|
+
Implements:
|
|
559
|
+
|
|
560
|
+
- Pairwise repulsion (Coulomb-like, O(n²)) between every node pair.
|
|
561
|
+
- Spring (Hooke) attraction along each edge toward `restLength`.
|
|
562
|
+
- Per-step velocity damping for stability.
|
|
563
|
+
|
|
564
|
+
Stops early once the maximum per-node displacement-squared drops below
|
|
565
|
+
{@link CONVERGENCE_EPSILON}, or after `iterations` steps — whichever
|
|
566
|
+
comes first.
|
|
567
|
+
|
|
568
|
+
```typescript
|
|
569
|
+
function forceLayout(
|
|
570
|
+
nodes: GraphNode[],
|
|
571
|
+
edges: GraphEdge[],
|
|
572
|
+
options?: ForceLayoutOptions,
|
|
573
|
+
): PositionedNode[]
|
|
574
|
+
```
|
|
575
|
+
|
|
576
|
+
- `nodes` — Nodes to lay out.
|
|
577
|
+
- `edges` — Edges (used only for attractive springs).
|
|
578
|
+
- `options` — Force-layout tunables.
|
|
579
|
+
|
|
580
|
+
**Returns:** Positioned nodes, in input order.
|
|
581
|
+
|
|
582
|
+
#### `layoutNodes(nodes, edges, layout, options)`
|
|
583
|
+
|
|
584
|
+
Compute world-space positions for every node using the chosen layout.
|
|
585
|
+
|
|
586
|
+
```typescript
|
|
587
|
+
function layoutNodes(
|
|
588
|
+
nodes: GraphNode[],
|
|
589
|
+
edges: GraphEdge[],
|
|
590
|
+
layout: GraphLayout,
|
|
591
|
+
options?: ForceLayoutOptions,
|
|
592
|
+
): PositionedNode[]
|
|
593
|
+
```
|
|
594
|
+
|
|
595
|
+
- `nodes` — Nodes to position.
|
|
596
|
+
- `edges` — Edges (consumed only by `force`).
|
|
597
|
+
- `layout` — Layout strategy.
|
|
598
|
+
- `options` — Optional force-layout tunables (ignored for non-force).
|
|
599
|
+
|
|
600
|
+
**Returns:** Positioned nodes.
|
|
601
|
+
|
|
602
|
+
#### `boundingBox(nodes, padding)`
|
|
603
|
+
|
|
604
|
+
Compute the world-space bounding box of a set of positioned nodes,
|
|
605
|
+
with a small padding for visual breathing room.
|
|
606
|
+
|
|
607
|
+
```typescript
|
|
608
|
+
function boundingBox(
|
|
609
|
+
nodes: PositionedNode[],
|
|
610
|
+
padding?: number,
|
|
611
|
+
): { minX: number; minY: number; maxX: number; maxY: number } | null
|
|
612
|
+
```
|
|
613
|
+
|
|
614
|
+
- `nodes` — Positioned nodes.
|
|
615
|
+
- `padding` — Padding (world units) added to every side. Default 40.
|
|
616
|
+
|
|
617
|
+
**Returns:** Bounding box `{ minX, minY, maxX, maxY }` — or `null` if empty.
|
|
618
|
+
|
|
619
|
+
### Constants
|
|
620
|
+
|
|
621
|
+
#### `DEFAULT_FORCE_ITERATIONS`
|
|
622
|
+
|
|
623
|
+
Default cap on force-layout iterations.
|
|
624
|
+
|
|
625
|
+
```typescript
|
|
626
|
+
const DEFAULT_FORCE_ITERATIONS: 200
|
|
627
|
+
```
|
|
628
|
+
|
|
629
|
+
#### `DEFAULT_REPULSION`
|
|
630
|
+
|
|
631
|
+
Default repulsion strength used by `forceLayout`.
|
|
632
|
+
|
|
633
|
+
```typescript
|
|
634
|
+
const DEFAULT_REPULSION: 800
|
|
635
|
+
```
|
|
636
|
+
|
|
637
|
+
#### `DEFAULT_SPRING`
|
|
638
|
+
|
|
639
|
+
Default spring stiffness used by `forceLayout`.
|
|
640
|
+
|
|
641
|
+
```typescript
|
|
642
|
+
const DEFAULT_SPRING: 0.05
|
|
643
|
+
```
|
|
644
|
+
|
|
645
|
+
#### `DEFAULT_REST_LENGTH`
|
|
646
|
+
|
|
647
|
+
Default ideal edge length used by `forceLayout`.
|
|
648
|
+
|
|
649
|
+
```typescript
|
|
650
|
+
const DEFAULT_REST_LENGTH: 80
|
|
651
|
+
```
|
|
652
|
+
|
|
653
|
+
#### `DEFAULT_DAMPING`
|
|
654
|
+
|
|
655
|
+
Default velocity damping used by `forceLayout`.
|
|
656
|
+
|
|
657
|
+
```typescript
|
|
658
|
+
const DEFAULT_DAMPING: 0.85
|
|
659
|
+
```
|
|
660
|
+
|
|
661
|
+
#### `CONVERGENCE_EPSILON`
|
|
662
|
+
|
|
663
|
+
Convergence threshold (max per-node displacement squared).
|
|
664
|
+
|
|
665
|
+
```typescript
|
|
666
|
+
const CONVERGENCE_EPSILON: 0.01
|
|
667
|
+
```
|
|
668
|
+
|
|
669
|
+
## Injection Notes
|
|
670
|
+
|
|
671
|
+
### Requirements
|
|
672
|
+
|
|
673
|
+
Peer dependencies:
|
|
674
|
+
|
|
675
|
+
- `@angular/common` ^22.0.0
|
|
676
|
+
- `@angular/core` ^22.0.0
|
|
677
|
+
- `@angular/platform-browser` ^22.0.0
|
|
678
|
+
- `@molecule/app-angular` ^3.5.0
|
|
679
|
+
- `@molecule/app-ui` ^1.0.1
|
|
680
|
+
- `@molecule/app-ui-angular` ^1.1.0
|
|
681
|
+
|
|
682
|
+
### Runtime Dependencies
|
|
683
|
+
|
|
684
|
+
- `@angular/common`
|
|
685
|
+
- `@angular/core`
|
|
686
|
+
- `@angular/platform-browser`
|
|
687
|
+
- `@molecule/app-angular`
|
|
688
|
+
- `@molecule/app-ui`
|
|
689
|
+
- `@molecule/app-ui-angular`
|
|
690
|
+
|
|
691
|
+
The root element fills 100% of its parent — the PARENT must have an
|
|
692
|
+
explicit height (e.g. a fixed-height panel or a flex/grid track) or
|
|
693
|
+
the graph renders zero-tall and appears blank.
|
|
694
|
+
|
|
695
|
+
Interaction surface is click / keyboard-activate + `selectedNodeId`
|
|
696
|
+
highlighting only. Nodes are NOT draggable and there is no built-in
|
|
697
|
+
pan / zoom — the SVG `viewBox` auto-fits the layout bounds.
|
|
698
|
+
|
|
699
|
+
The renderer props are Angular templates, rendered with `ngTemplateOutlet`:
|
|
700
|
+
`nodeRenderer` receives the positioned node as `a0`; `edgeRenderer` receives
|
|
701
|
+
the edge as `a0`, the source node as `a1` and the target node as `a2`.
|
|
702
|
+
|
|
703
|
+
Layout is deterministic (seeded RNG), computed once per
|
|
704
|
+
`(nodes, edges, layout, forceOptions)` change. `forceOptions` is
|
|
705
|
+
ignored by the `circular` and `grid` layouts.
|
|
706
|
+
|
|
707
|
+
## Translations
|
|
708
|
+
|
|
709
|
+
Translation strings are provided by `@molecule/app-locales-feature-graph-view`.
|