@stacksjs/components 0.2.855 → 0.2.857
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/ContextMenu-43g3w5tp.stx +188 -0
- package/dist/DataGrid-7rt7v865.stx +376 -0
- package/dist/GraphView-0t3t9mec.stx +320 -0
- package/dist/JsonTree-satc4010.stx +317 -0
- package/dist/PreviewFrame-erbgvzj8.stx +257 -0
- package/dist/SplitPane-7sqfdwqa.stx +159 -0
- package/dist/component-api/component-api.js +36 -0
- package/dist/context-menu/context-menu.js +575 -0
- package/dist/data-grid/data-grid.js +1244 -0
- package/dist/graph-view/graph-view.js +1408 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +22 -20
- package/dist/json-tree/json-tree.js +1121 -0
- package/dist/preview-frame/preview-frame.js +356 -0
- package/dist/split-pane/split-pane.js +364 -0
- package/dist/ui/context-menu/ContextMenu.d.ts +14 -0
- package/dist/ui/context-menu/context-menu.d.ts +8 -0
- package/dist/ui/context-menu/core.d.ts +74 -0
- package/dist/ui/context-menu/index.d.ts +11 -0
- package/dist/ui/context-menu/view.d.ts +36 -0
- package/dist/ui/data-grid/DataGrid.d.ts +30 -0
- package/dist/ui/data-grid/core.d.ts +141 -0
- package/dist/ui/data-grid/data-grid.d.ts +9 -0
- package/dist/ui/data-grid/index.d.ts +20 -0
- package/dist/ui/data-grid/view.d.ts +56 -0
- package/dist/ui/graph-view/GraphView.d.ts +21 -0
- package/dist/ui/graph-view/core/edges.d.ts +34 -0
- package/dist/ui/graph-view/core/force.d.ts +10 -0
- package/dist/ui/graph-view/core/graph.d.ts +83 -0
- package/dist/ui/graph-view/core/index.d.ts +5 -0
- package/dist/ui/graph-view/core/layered.d.ts +24 -0
- package/dist/ui/graph-view/core/viewport.d.ts +34 -0
- package/dist/ui/graph-view/graph-view.d.ts +9 -0
- package/dist/ui/graph-view/index.d.ts +21 -0
- package/dist/ui/graph-view/view.d.ts +51 -0
- package/dist/ui/json-tree/JsonTree.d.ts +18 -0
- package/dist/ui/json-tree/core.d.ts +108 -0
- package/dist/ui/json-tree/index.d.ts +18 -0
- package/dist/ui/json-tree/json-tree.d.ts +10 -0
- package/dist/ui/json-tree/tinker.d.ts +84 -0
- package/dist/ui/json-tree/view.d.ts +59 -0
- package/dist/ui/preview-frame/PreviewFrame.d.ts +44 -0
- package/dist/ui/preview-frame/core.d.ts +81 -0
- package/dist/ui/preview-frame/index.d.ts +19 -0
- package/dist/ui/preview-frame/preview-frame.d.ts +8 -0
- package/dist/ui/preview-frame/view.d.ts +37 -0
- package/dist/ui/split-pane/SplitPane.d.ts +18 -0
- package/dist/ui/split-pane/core.d.ts +58 -0
- package/dist/ui/split-pane/index.d.ts +15 -0
- package/dist/ui/split-pane/split-pane.d.ts +8 -0
- package/dist/ui/split-pane/view.d.ts +27 -0
- package/dist/ui/virtual-table/index.d.ts +6 -0
- package/dist/utils/component-api.d.ts +4 -0
- package/package.json +38 -3
- package/src/index.ts +8 -0
- package/src/ui/code-editor/README.md +2 -1
- package/src/ui/context-menu/ContextMenu.stx +188 -0
- package/src/ui/context-menu/README.md +117 -0
- package/src/ui/context-menu/context-menu.ts +8 -0
- package/src/ui/context-menu/core.ts +250 -0
- package/src/ui/context-menu/index.ts +16 -0
- package/src/ui/context-menu/view.ts +540 -0
- package/src/ui/data-grid/DataGrid.stx +376 -0
- package/src/ui/data-grid/README.md +204 -0
- package/src/ui/data-grid/core.ts +813 -0
- package/src/ui/data-grid/data-grid.ts +9 -0
- package/src/ui/data-grid/index.ts +34 -0
- package/src/ui/data-grid/view.ts +806 -0
- package/src/ui/graph-view/GraphView.stx +320 -0
- package/src/ui/graph-view/README.md +210 -0
- package/src/ui/graph-view/core/edges.ts +180 -0
- package/src/ui/graph-view/core/force.ts +237 -0
- package/src/ui/graph-view/core/graph.ts +277 -0
- package/src/ui/graph-view/core/index.ts +5 -0
- package/src/ui/graph-view/core/layered.ts +256 -0
- package/src/ui/graph-view/core/viewport.ts +141 -0
- package/src/ui/graph-view/graph-view.ts +9 -0
- package/src/ui/graph-view/index.ts +35 -0
- package/src/ui/graph-view/view.ts +809 -0
- package/src/ui/json-tree/JsonTree.stx +317 -0
- package/src/ui/json-tree/README.md +193 -0
- package/src/ui/json-tree/core.ts +588 -0
- package/src/ui/json-tree/index.ts +28 -0
- package/src/ui/json-tree/json-tree.ts +10 -0
- package/src/ui/json-tree/tinker.ts +349 -0
- package/src/ui/json-tree/view.ts +665 -0
- package/src/ui/preview-frame/PreviewFrame.stx +257 -0
- package/src/ui/preview-frame/README.md +107 -0
- package/src/ui/preview-frame/core.ts +195 -0
- package/src/ui/preview-frame/index.ts +31 -0
- package/src/ui/preview-frame/preview-frame.ts +8 -0
- package/src/ui/preview-frame/view.ts +336 -0
- package/src/ui/split-pane/README.md +87 -0
- package/src/ui/split-pane/SplitPane.stx +159 -0
- package/src/ui/split-pane/core.ts +200 -0
- package/src/ui/split-pane/index.ts +24 -0
- package/src/ui/split-pane/split-pane.ts +8 -0
- package/src/ui/split-pane/view.ts +305 -0
- package/src/ui/virtual-table/index.ts +6 -0
- package/src/utils/component-api.ts +72 -0
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A layered layout: every edge points the same way, as far as the graph allows.
|
|
3
|
+
*
|
|
4
|
+
* The classic four steps, each kept small. Break cycles by turning round the
|
|
5
|
+
* edges a depth-first walk finds pointing back at its own path. Give each node
|
|
6
|
+
* the layer one past its deepest predecessor. Reorder each layer by where its
|
|
7
|
+
* neighbours sit, a few times each way, keeping the order with the fewest
|
|
8
|
+
* crossings. Then space the layers out and centre each one.
|
|
9
|
+
*
|
|
10
|
+
* Nothing here is random, so the same graph always comes out the same.
|
|
11
|
+
*/
|
|
12
|
+
import type { GraphModel, Positions, Rect } from './graph'
|
|
13
|
+
|
|
14
|
+
export type LayeredDirection = 'LR' | 'TB'
|
|
15
|
+
|
|
16
|
+
export interface LayeredOptions {
|
|
17
|
+
/** `LR` runs left to right, `TB` top to bottom. Default `LR`. */
|
|
18
|
+
direction?: LayeredDirection
|
|
19
|
+
/** Space between two nodes in the same layer. Default 24. */
|
|
20
|
+
nodeGap?: number
|
|
21
|
+
/** Space between two layers. Default 80. */
|
|
22
|
+
layerGap?: number
|
|
23
|
+
/** Space between two unconnected parts of the graph. Default 48. */
|
|
24
|
+
componentGap?: number
|
|
25
|
+
/** Passes of reordering, each one down and up. Default 4. */
|
|
26
|
+
sweeps?: number
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface LayeredResult {
|
|
30
|
+
positions: Positions
|
|
31
|
+
/** The layer of each node, counted within its own connected part. */
|
|
32
|
+
layerOf: Map<string, number>
|
|
33
|
+
/** Edges drawn against the flow because they closed a cycle. */
|
|
34
|
+
reversed: Set<string>
|
|
35
|
+
/** The connected parts, each as its layers in drawing order. */
|
|
36
|
+
components: string[][][]
|
|
37
|
+
bounds: Rect | null
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
interface Link {
|
|
41
|
+
from: string
|
|
42
|
+
to: string
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** The connected parts of the graph, in the order their first node was added. */
|
|
46
|
+
export function connectedComponents(graph: GraphModel): string[][] {
|
|
47
|
+
const seen = new Set<string>()
|
|
48
|
+
const parts: string[][] = []
|
|
49
|
+
for (const node of graph.nodes) {
|
|
50
|
+
if (seen.has(node.id))
|
|
51
|
+
continue
|
|
52
|
+
const part: string[] = []
|
|
53
|
+
const queue = [node.id]
|
|
54
|
+
seen.add(node.id)
|
|
55
|
+
while (queue.length) {
|
|
56
|
+
const id = queue.shift()!
|
|
57
|
+
part.push(id)
|
|
58
|
+
for (const next of graph.neighbours(id)) {
|
|
59
|
+
if (!seen.has(next)) {
|
|
60
|
+
seen.add(next)
|
|
61
|
+
queue.push(next)
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
parts.push(part)
|
|
66
|
+
}
|
|
67
|
+
// Insertion order inside each part too, so the result does not depend on
|
|
68
|
+
// which node the walk happened to reach first.
|
|
69
|
+
const order = new Map(graph.nodes.map((node, index) => [node.id, index]))
|
|
70
|
+
for (const part of parts)
|
|
71
|
+
part.sort((a, b) => order.get(a)! - order.get(b)!)
|
|
72
|
+
return parts
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* The edges that have to be turned round for the graph to have no cycle: the
|
|
77
|
+
* ones a depth-first walk finds leading back to a node it is still inside.
|
|
78
|
+
*/
|
|
79
|
+
export function findBackEdges(graph: GraphModel): Set<string> {
|
|
80
|
+
const out = new Map<string, Array<{ id: string, to: string }>>()
|
|
81
|
+
for (const node of graph.nodes)
|
|
82
|
+
out.set(node.id, [])
|
|
83
|
+
for (const edge of graph.edges) {
|
|
84
|
+
if (edge.source !== edge.target)
|
|
85
|
+
out.get(edge.source)!.push({ id: edge.id, to: edge.target })
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const reversed = new Set<string>()
|
|
89
|
+
const state = new Map<string, 1 | 2>()
|
|
90
|
+
for (const root of graph.nodes) {
|
|
91
|
+
if (state.has(root.id))
|
|
92
|
+
continue
|
|
93
|
+
// An explicit stack: an object graph can be deeper than the call stack.
|
|
94
|
+
const stack: Array<{ id: string, next: number }> = [{ id: root.id, next: 0 }]
|
|
95
|
+
state.set(root.id, 1)
|
|
96
|
+
while (stack.length) {
|
|
97
|
+
const frame = stack[stack.length - 1]
|
|
98
|
+
const edges = out.get(frame.id)!
|
|
99
|
+
if (frame.next >= edges.length) {
|
|
100
|
+
state.set(frame.id, 2)
|
|
101
|
+
stack.pop()
|
|
102
|
+
continue
|
|
103
|
+
}
|
|
104
|
+
const edge = edges[frame.next++]
|
|
105
|
+
const seen = state.get(edge.to)
|
|
106
|
+
if (seen === 1)
|
|
107
|
+
reversed.add(edge.id)
|
|
108
|
+
else if (seen === undefined) {
|
|
109
|
+
state.set(edge.to, 1)
|
|
110
|
+
stack.push({ id: edge.to, next: 0 })
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
return reversed
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** Crossings between the links of two neighbouring layers, for a given order. */
|
|
118
|
+
function crossings(links: Link[], place: Map<string, number>): number {
|
|
119
|
+
const pairs = links.map(link => [place.get(link.from)!, place.get(link.to)!] as const).sort((a, b) => a[0] - b[0] || a[1] - b[1])
|
|
120
|
+
let count = 0
|
|
121
|
+
for (let i = 0; i < pairs.length; i++) {
|
|
122
|
+
for (let j = i + 1; j < pairs.length; j++) {
|
|
123
|
+
if (pairs[i][0] < pairs[j][0] && pairs[i][1] > pairs[j][1])
|
|
124
|
+
count++
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
return count
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
export function layeredLayout(graph: GraphModel, options: LayeredOptions = {}): LayeredResult {
|
|
131
|
+
const direction = options.direction === 'TB' ? 'TB' : 'LR'
|
|
132
|
+
const nodeGap = options.nodeGap ?? 24
|
|
133
|
+
const layerGap = options.layerGap ?? 80
|
|
134
|
+
const componentGap = options.componentGap ?? 48
|
|
135
|
+
const sweeps = options.sweeps ?? 4
|
|
136
|
+
const horizontal = direction === 'LR'
|
|
137
|
+
|
|
138
|
+
const reversed = findBackEdges(graph)
|
|
139
|
+
// The acyclic version of the graph: one link per pair, each pointing forward.
|
|
140
|
+
const links: Link[] = []
|
|
141
|
+
const seenLinks = new Set<string>()
|
|
142
|
+
for (const edge of graph.edges) {
|
|
143
|
+
if (edge.source === edge.target)
|
|
144
|
+
continue
|
|
145
|
+
const from = reversed.has(edge.id) ? edge.target : edge.source
|
|
146
|
+
const to = reversed.has(edge.id) ? edge.source : edge.target
|
|
147
|
+
const key = `${from}\u0000${to}`
|
|
148
|
+
if (!seenLinks.has(key)) {
|
|
149
|
+
seenLinks.add(key)
|
|
150
|
+
links.push({ from, to })
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
const forward = new Map<string, string[]>()
|
|
154
|
+
const backward = new Map<string, string[]>()
|
|
155
|
+
for (const node of graph.nodes) {
|
|
156
|
+
forward.set(node.id, [])
|
|
157
|
+
backward.set(node.id, [])
|
|
158
|
+
}
|
|
159
|
+
for (const link of links) {
|
|
160
|
+
forward.get(link.from)!.push(link.to)
|
|
161
|
+
backward.get(link.to)!.push(link.from)
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// Longest path: a node goes one layer past its deepest predecessor.
|
|
165
|
+
const layerOf = new Map<string, number>()
|
|
166
|
+
const waiting = new Map<string, number>()
|
|
167
|
+
const ready: string[] = []
|
|
168
|
+
for (const node of graph.nodes) {
|
|
169
|
+
waiting.set(node.id, backward.get(node.id)!.length)
|
|
170
|
+
if (backward.get(node.id)!.length === 0) {
|
|
171
|
+
ready.push(node.id)
|
|
172
|
+
layerOf.set(node.id, 0)
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
for (let cursor = 0; cursor < ready.length; cursor++) {
|
|
176
|
+
const id = ready[cursor]
|
|
177
|
+
for (const next of forward.get(id)!) {
|
|
178
|
+
layerOf.set(next, Math.max(layerOf.get(next) ?? 0, layerOf.get(id)! + 1))
|
|
179
|
+
const left = waiting.get(next)! - 1
|
|
180
|
+
waiting.set(next, left)
|
|
181
|
+
if (left === 0)
|
|
182
|
+
ready.push(next)
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
const positions: Positions = new Map()
|
|
187
|
+
const components: string[][][] = []
|
|
188
|
+
let offset = 0
|
|
189
|
+
|
|
190
|
+
for (const part of connectedComponents(graph)) {
|
|
191
|
+
const layers: string[][] = []
|
|
192
|
+
for (const id of part) {
|
|
193
|
+
const layer = layerOf.get(id) ?? 0
|
|
194
|
+
while (layers.length <= layer)
|
|
195
|
+
layers.push([])
|
|
196
|
+
layers[layer].push(id)
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
const inPart = new Set(part)
|
|
200
|
+
const adjacent = links.filter(link => inPart.has(link.from) && layerOf.get(link.to)! - layerOf.get(link.from)! === 1)
|
|
201
|
+
const place = new Map<string, number>()
|
|
202
|
+
const index = (): void => {
|
|
203
|
+
for (const layer of layers)
|
|
204
|
+
layer.forEach((id, position) => place.set(id, layer.length > 1 ? position / (layer.length - 1) : 0.5))
|
|
205
|
+
}
|
|
206
|
+
const reorder = (layer: string[], others: Map<string, string[]>): void => {
|
|
207
|
+
const weight = new Map<string, number>()
|
|
208
|
+
layer.forEach((id, position) => {
|
|
209
|
+
const around = others.get(id)!
|
|
210
|
+
const here = layer.length > 1 ? position / (layer.length - 1) : 0.5
|
|
211
|
+
weight.set(id, around.length ? around.reduce((sum, other) => sum + place.get(other)!, 0) / around.length : here)
|
|
212
|
+
})
|
|
213
|
+
const before = new Map(layer.map((id, position) => [id, position]))
|
|
214
|
+
layer.sort((a, b) => weight.get(a)! - weight.get(b)! || before.get(a)! - before.get(b)!)
|
|
215
|
+
layer.forEach((id, position) => place.set(id, layer.length > 1 ? position / (layer.length - 1) : 0.5))
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
index()
|
|
219
|
+
let best = layers.map(layer => [...layer])
|
|
220
|
+
let fewest = crossings(adjacent, place)
|
|
221
|
+
for (let sweep = 0; sweep < sweeps && fewest > 0; sweep++) {
|
|
222
|
+
for (let layer = 1; layer < layers.length; layer++)
|
|
223
|
+
reorder(layers[layer], backward)
|
|
224
|
+
for (let layer = layers.length - 2; layer >= 0; layer--)
|
|
225
|
+
reorder(layers[layer], forward)
|
|
226
|
+
const count = crossings(adjacent, place)
|
|
227
|
+
if (count < fewest) {
|
|
228
|
+
fewest = count
|
|
229
|
+
best = layers.map(layer => [...layer])
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
// Along the flow, a layer is as thick as its thickest node. Across it, the
|
|
234
|
+
// nodes are stacked with a gap and the stack is centred on the part's axis.
|
|
235
|
+
const along = (id: string): number => (horizontal ? graph.size(id).width : graph.size(id).height)
|
|
236
|
+
const across = (id: string): number => (horizontal ? graph.size(id).height : graph.size(id).width)
|
|
237
|
+
const extents = best.map(layer => layer.reduce((sum, id) => sum + across(id), 0) + nodeGap * Math.max(0, layer.length - 1))
|
|
238
|
+
const widest = Math.max(0, ...extents)
|
|
239
|
+
let flow = 0
|
|
240
|
+
best.forEach((layer, layerIndex) => {
|
|
241
|
+
const thickness = Math.max(0, ...layer.map(along))
|
|
242
|
+
let cursor = offset + (widest - extents[layerIndex]) / 2
|
|
243
|
+
for (const id of layer) {
|
|
244
|
+
const centre = cursor + across(id) / 2
|
|
245
|
+
positions.set(id, horizontal ? { x: flow + thickness / 2, y: centre } : { x: centre, y: flow + thickness / 2 })
|
|
246
|
+
cursor += across(id) + nodeGap
|
|
247
|
+
}
|
|
248
|
+
flow += thickness + layerGap
|
|
249
|
+
})
|
|
250
|
+
|
|
251
|
+
components.push(best)
|
|
252
|
+
offset += widest + componentGap
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
return { positions, layerOf, reversed, components, bounds: graph.bounds(positions) }
|
|
256
|
+
}
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Where the graph is on screen: a pan and a zoom. No DOM.
|
|
3
|
+
*
|
|
4
|
+
* A point on screen is the graph point times `k`, plus `x` and `y`.
|
|
5
|
+
*/
|
|
6
|
+
import type { GraphModel, Point, Positions, Rect, Size } from './graph'
|
|
7
|
+
|
|
8
|
+
export interface Viewport {
|
|
9
|
+
x: number
|
|
10
|
+
y: number
|
|
11
|
+
k: number
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export type Heading = 'left' | 'right' | 'up' | 'down'
|
|
15
|
+
|
|
16
|
+
export const identityViewport: Viewport = { x: 0, y: 0, k: 1 }
|
|
17
|
+
|
|
18
|
+
export function clampZoom(k: number, min: number, max: number): number {
|
|
19
|
+
return Math.min(Math.max(k, min), max)
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function toScreen(view: Viewport, point: Point): Point {
|
|
23
|
+
return { x: point.x * view.k + view.x, y: point.y * view.k + view.y }
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function toGraph(view: Viewport, point: Point): Point {
|
|
27
|
+
return { x: (point.x - view.x) / view.k, y: (point.y - view.y) / view.k }
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function pan(view: Viewport, dx: number, dy: number): Viewport {
|
|
31
|
+
return { x: view.x + dx, y: view.y + dy, k: view.k }
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** Zoom by a factor, keeping the graph point under a screen point where it is. */
|
|
35
|
+
export function zoomAt(view: Viewport, point: Point, factor: number, min = 0.1, max = 4): Viewport {
|
|
36
|
+
const k = clampZoom(view.k * factor, min, max)
|
|
37
|
+
const ratio = k / view.k
|
|
38
|
+
return { x: point.x - (point.x - view.x) * ratio, y: point.y - (point.y - view.y) * ratio, k }
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* The view that shows all of `bounds` in a box of `size`, centred, with
|
|
43
|
+
* `padding` clear on every side. It never zooms in past `max`, so a graph of
|
|
44
|
+
* three nodes is not blown up to fill the window.
|
|
45
|
+
*/
|
|
46
|
+
export function fit(bounds: Rect | null, size: Size, padding = 24, min = 0.1, max = 1): Viewport {
|
|
47
|
+
if (!bounds || size.width <= 0 || size.height <= 0)
|
|
48
|
+
return { ...identityViewport }
|
|
49
|
+
const roomX = Math.max(1, size.width - padding * 2)
|
|
50
|
+
const roomY = Math.max(1, size.height - padding * 2)
|
|
51
|
+
const k = clampZoom(Math.min(roomX / Math.max(1, bounds.width), roomY / Math.max(1, bounds.height)), min, max)
|
|
52
|
+
return {
|
|
53
|
+
x: size.width / 2 - (bounds.x + bounds.width / 2) * k,
|
|
54
|
+
y: size.height / 2 - (bounds.y + bounds.height / 2) * k,
|
|
55
|
+
k,
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** The view with a graph point in the middle of the box, at the same zoom. */
|
|
60
|
+
export function centreOn(view: Viewport, point: Point, size: Size): Viewport {
|
|
61
|
+
return { x: size.width / 2 - point.x * view.k, y: size.height / 2 - point.y * view.k, k: view.k }
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** The smallest pan that brings a graph box fully inside the screen box. */
|
|
65
|
+
export function reveal(view: Viewport, box: Rect, size: Size, margin = 16): Viewport {
|
|
66
|
+
const left = box.x * view.k + view.x
|
|
67
|
+
const top = box.y * view.k + view.y
|
|
68
|
+
const right = left + box.width * view.k
|
|
69
|
+
const bottom = top + box.height * view.k
|
|
70
|
+
let dx = 0
|
|
71
|
+
let dy = 0
|
|
72
|
+
if (left < margin)
|
|
73
|
+
dx = margin - left
|
|
74
|
+
else if (right > size.width - margin)
|
|
75
|
+
dx = size.width - margin - right
|
|
76
|
+
if (top < margin)
|
|
77
|
+
dy = margin - top
|
|
78
|
+
else if (bottom > size.height - margin)
|
|
79
|
+
dy = size.height - margin - bottom
|
|
80
|
+
return dx === 0 && dy === 0 ? view : pan(view, dx, dy)
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* The node an arrow key moves to: the nearest one that way, where a node
|
|
85
|
+
* straight ahead beats a closer one off to the side. Null at the edge of the
|
|
86
|
+
* graph.
|
|
87
|
+
*/
|
|
88
|
+
export function nearestInDirection(positions: Positions, from: string, heading: Heading): string | null {
|
|
89
|
+
const origin = positions.get(from)
|
|
90
|
+
if (!origin)
|
|
91
|
+
return null
|
|
92
|
+
const ux = heading === 'left' ? -1 : heading === 'right' ? 1 : 0
|
|
93
|
+
const uy = heading === 'up' ? -1 : heading === 'down' ? 1 : 0
|
|
94
|
+
|
|
95
|
+
let best: string | null = null
|
|
96
|
+
let bestScore = Number.POSITIVE_INFINITY
|
|
97
|
+
for (const [id, point] of positions) {
|
|
98
|
+
if (id === from)
|
|
99
|
+
continue
|
|
100
|
+
const dx = point.x - origin.x
|
|
101
|
+
const dy = point.y - origin.y
|
|
102
|
+
const ahead = dx * ux + dy * uy
|
|
103
|
+
if (ahead <= 0.5)
|
|
104
|
+
continue
|
|
105
|
+
const aside = Math.abs(dx * uy) + Math.abs(dy * ux)
|
|
106
|
+
// Nothing more than about 63 degrees off the heading counts as "that way".
|
|
107
|
+
if (aside > ahead * 2)
|
|
108
|
+
continue
|
|
109
|
+
const score = ahead + aside * 2
|
|
110
|
+
if (score < bestScore) {
|
|
111
|
+
bestScore = score
|
|
112
|
+
best = id
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
return best
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/** The node whose centre is nearest a graph point. For a first focus. */
|
|
119
|
+
export function nearestTo(positions: Positions, point: Point): string | null {
|
|
120
|
+
let best: string | null = null
|
|
121
|
+
let bestDistance = Number.POSITIVE_INFINITY
|
|
122
|
+
for (const [id, at] of positions) {
|
|
123
|
+
const distance = (at.x - point.x) ** 2 + (at.y - point.y) ** 2
|
|
124
|
+
if (distance < bestDistance) {
|
|
125
|
+
bestDistance = distance
|
|
126
|
+
best = id
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
return best
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** The node box under a graph point, the one added last when two overlap. */
|
|
133
|
+
export function nodeAt(graph: GraphModel, positions: Positions, point: Point): string | null {
|
|
134
|
+
let hit: string | null = null
|
|
135
|
+
for (const [id, at] of positions) {
|
|
136
|
+
const size = graph.size(id)
|
|
137
|
+
if (Math.abs(point.x - at.x) <= size.width / 2 && Math.abs(point.y - at.y) <= size.height / 2)
|
|
138
|
+
hit = id
|
|
139
|
+
}
|
|
140
|
+
return hit
|
|
141
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The graph view without its stx template.
|
|
3
|
+
*
|
|
4
|
+
* `@stacksjs/components/graph-view`. The model, the two layouts, the edge
|
|
5
|
+
* geometry and the viewport arithmetic (`./core`, no DOM), and the canvas that
|
|
6
|
+
* draws them into an SVG inside any element.
|
|
7
|
+
*/
|
|
8
|
+
export * from './core'
|
|
9
|
+
export * from './view'
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { GraphEdge, GraphNode, LayeredDirection } from './core'
|
|
2
|
+
import type { GraphLayoutName } from './view'
|
|
3
|
+
|
|
4
|
+
export * from './core'
|
|
5
|
+
export { default as GraphView } from './GraphView.stx'
|
|
6
|
+
export * from './view'
|
|
7
|
+
|
|
8
|
+
export interface GraphViewProps {
|
|
9
|
+
/** `{ id, label?, kind?, detail?, expandable?, data? }`. Bound with `:nodes`. */
|
|
10
|
+
nodes?: GraphNode[]
|
|
11
|
+
/** `{ source, target, label?, directed?, id? }`. Bound with `:edges`. */
|
|
12
|
+
edges?: GraphEdge[]
|
|
13
|
+
/** `layered` points every edge one way; `force` lets the nodes find their own places. Default `layered`. */
|
|
14
|
+
layout?: GraphLayoutName
|
|
15
|
+
/** The flow of the layered layout: `LR` or `TB`. Default `LR`. */
|
|
16
|
+
direction?: LayeredDirection
|
|
17
|
+
/** The id of the selected node. */
|
|
18
|
+
selected?: string
|
|
19
|
+
/** A CSS length. Default `24rem`. */
|
|
20
|
+
height?: string
|
|
21
|
+
/** Default 0.1. */
|
|
22
|
+
minZoom?: number
|
|
23
|
+
/** Default 4. */
|
|
24
|
+
maxZoom?: number
|
|
25
|
+
/** Show the zoom, fit and layout buttons. Default true. */
|
|
26
|
+
toolbar?: boolean
|
|
27
|
+
/** Suppress the browser's own menu on a right click. */
|
|
28
|
+
contextMenu?: boolean
|
|
29
|
+
/** Move nodes to a new layout over a moment. Default true. */
|
|
30
|
+
animate?: boolean
|
|
31
|
+
/** The accessible name of the graph. Default `Graph`. */
|
|
32
|
+
label?: string
|
|
33
|
+
id?: string
|
|
34
|
+
className?: string
|
|
35
|
+
}
|