@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,809 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A graph in the DOM: an SVG that can be panned, zoomed and walked by keyboard.
|
|
3
|
+
*
|
|
4
|
+
* One group carries the pan and zoom, so moving the view is a single attribute
|
|
5
|
+
* however many nodes there are. Edges sit under nodes, and edge labels over
|
|
6
|
+
* both layers of lines so a label is never struck through.
|
|
7
|
+
*
|
|
8
|
+
* Text only ever arrives through `textContent`. A label is data.
|
|
9
|
+
*/
|
|
10
|
+
import type { EdgeShape, GraphEdge, GraphNode, Heading, LayeredDirection, Point, Positions, ResolvedEdge, Size, Viewport } from './core'
|
|
11
|
+
import { centreOn, edgeShape, fit, fitLabel, forceLayout, GraphModel, labelOf, layeredLayout, maxLabelLength, nearestInDirection, nearestTo, pan, parallelOffsets, reveal, toGraph, zoomAt } from './core'
|
|
12
|
+
|
|
13
|
+
export type GraphLayoutName = 'layered' | 'force'
|
|
14
|
+
|
|
15
|
+
export interface GraphCanvasOptions {
|
|
16
|
+
nodes?: readonly GraphNode[]
|
|
17
|
+
edges?: readonly GraphEdge[]
|
|
18
|
+
layout?: GraphLayoutName
|
|
19
|
+
direction?: LayeredDirection
|
|
20
|
+
/** The id of the node to start selected. */
|
|
21
|
+
selected?: string | null
|
|
22
|
+
minZoom?: number
|
|
23
|
+
maxZoom?: number
|
|
24
|
+
/** The accessible name of the graph. */
|
|
25
|
+
label?: string
|
|
26
|
+
/** Suppress the browser's own menu on a right click, for a host that shows its own. */
|
|
27
|
+
contextMenu?: boolean
|
|
28
|
+
/** Move nodes to a new layout over a moment, rather than at once. Default true. */
|
|
29
|
+
animate?: boolean
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface GraphSelectDetail {
|
|
33
|
+
node: GraphNode | null
|
|
34
|
+
neighbours: GraphNode[]
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface GraphCanvasCallbacks {
|
|
38
|
+
onSelect?: (detail: GraphSelectDetail) => void
|
|
39
|
+
onExpand?: (detail: { node: GraphNode }) => void
|
|
40
|
+
onOpen?: (detail: { node: GraphNode }) => void
|
|
41
|
+
onMove?: (detail: { node: GraphNode, x: number, y: number }) => void
|
|
42
|
+
onViewport?: (detail: Viewport) => void
|
|
43
|
+
onContext?: (detail: { node: GraphNode | null, x: number, y: number }) => void
|
|
44
|
+
onLayout?: (detail: { layout: GraphLayoutName }) => void
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const SVG = 'http://www.w3.org/2000/svg'
|
|
48
|
+
const DRAG_THRESHOLD = 3
|
|
49
|
+
const DOUBLE_CLICK = 350
|
|
50
|
+
const MOVE_TIME = 240
|
|
51
|
+
const LABEL_CHAR = 6.2
|
|
52
|
+
|
|
53
|
+
interface EdgeParts {
|
|
54
|
+
group: SVGGElement
|
|
55
|
+
path: SVGPathElement
|
|
56
|
+
label: SVGGElement | null
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
interface Gesture {
|
|
60
|
+
pointer: number
|
|
61
|
+
node: string | null
|
|
62
|
+
startX: number
|
|
63
|
+
startY: number
|
|
64
|
+
origin: Point
|
|
65
|
+
view: Viewport
|
|
66
|
+
moved: boolean
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function make<K extends keyof SVGElementTagNameMap>(name: K, attributes: Record<string, string | number> = {}): SVGElementTagNameMap[K] {
|
|
70
|
+
const element = document.createElementNS(SVG, name)
|
|
71
|
+
for (const key in attributes)
|
|
72
|
+
element.setAttribute(key, String(attributes[key]))
|
|
73
|
+
return element
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
let instances = 0
|
|
77
|
+
|
|
78
|
+
export class GraphCanvas {
|
|
79
|
+
/** How long the last layout and the last full draw took, in milliseconds. */
|
|
80
|
+
readonly timings = { layout: 0, render: 0 }
|
|
81
|
+
|
|
82
|
+
private model = new GraphModel()
|
|
83
|
+
/** Where each node belongs. */
|
|
84
|
+
private positions: Positions = new Map()
|
|
85
|
+
/** Where each node is drawn right now, which differs while nodes are moving. */
|
|
86
|
+
private shown: Positions = new Map()
|
|
87
|
+
private readonly pinned = new Set<string>()
|
|
88
|
+
private view: Viewport = { x: 0, y: 0, k: 1 }
|
|
89
|
+
private size: Size = { width: 0, height: 0 }
|
|
90
|
+
private layoutName: GraphLayoutName
|
|
91
|
+
private direction: LayeredDirection
|
|
92
|
+
private selected: string | null = null
|
|
93
|
+
private autoFit = true
|
|
94
|
+
private animate: boolean
|
|
95
|
+
private minZoom: number
|
|
96
|
+
private maxZoom: number
|
|
97
|
+
private readonly contextMenu: boolean
|
|
98
|
+
|
|
99
|
+
private readonly prefix: string
|
|
100
|
+
private readonly surface: HTMLElement
|
|
101
|
+
private readonly status: HTMLElement | null
|
|
102
|
+
private readonly svg: SVGSVGElement
|
|
103
|
+
private readonly stage: SVGGElement
|
|
104
|
+
private readonly edgeLayer: SVGGElement
|
|
105
|
+
private readonly nodeLayer: SVGGElement
|
|
106
|
+
private readonly labelLayer: SVGGElement
|
|
107
|
+
private readonly nodeEls = new Map<string, SVGGElement>()
|
|
108
|
+
private readonly edgeEls = new Map<string, EdgeParts>()
|
|
109
|
+
private readonly idOf = new WeakMap<Element, string>()
|
|
110
|
+
private offsets = new Map<string, number>()
|
|
111
|
+
private serial = 0
|
|
112
|
+
|
|
113
|
+
private gesture: Gesture | null = null
|
|
114
|
+
private pinch: { k: number, x: number, y: number } | null = null
|
|
115
|
+
private lastClick = { id: '', at: 0 }
|
|
116
|
+
private lastExpand = 0
|
|
117
|
+
private frame = 0
|
|
118
|
+
private observer: ResizeObserver | null = null
|
|
119
|
+
private readonly disposers: Array<() => void> = []
|
|
120
|
+
|
|
121
|
+
constructor(private readonly root: HTMLElement, options: GraphCanvasOptions = {}, private readonly callbacks: GraphCanvasCallbacks = {}) {
|
|
122
|
+
this.prefix = `${root.id || 'graph'}-${++instances}`
|
|
123
|
+
this.layoutName = options.layout === 'force' ? 'force' : 'layered'
|
|
124
|
+
this.direction = options.direction === 'TB' ? 'TB' : 'LR'
|
|
125
|
+
this.minZoom = options.minZoom ?? 0.1
|
|
126
|
+
this.maxZoom = options.maxZoom ?? 4
|
|
127
|
+
this.contextMenu = Boolean(options.contextMenu)
|
|
128
|
+
this.animate = options.animate !== false
|
|
129
|
+
|
|
130
|
+
this.surface = root.querySelector<HTMLElement>('[data-gv="surface"]') ?? root
|
|
131
|
+
this.status = root.querySelector<HTMLElement>('[data-gv="status"]')
|
|
132
|
+
if (options.label)
|
|
133
|
+
this.surface.setAttribute('aria-label', options.label)
|
|
134
|
+
|
|
135
|
+
this.svg = make('svg', { 'class': 'stx-gv-svg', 'aria-hidden': 'false', 'focusable': 'false' })
|
|
136
|
+
// Whatever is written under here is data, never a template to evaluate.
|
|
137
|
+
;(this.svg as unknown as { __stx_inert_content: boolean }).__stx_inert_content = true
|
|
138
|
+
const defs = make('defs')
|
|
139
|
+
for (const name of ['arrow', 'arrow-active']) {
|
|
140
|
+
const marker = make('marker', { id: `${this.prefix}-${name}`, viewBox: '0 0 10 10', refX: 9, refY: 5, markerWidth: 7, markerHeight: 7, orient: 'auto-start-reverse', class: `stx-gv-${name}` })
|
|
141
|
+
marker.append(make('path', { d: 'M 0 0 L 10 5 L 0 10 z' }))
|
|
142
|
+
defs.append(marker)
|
|
143
|
+
}
|
|
144
|
+
this.stage = make('g', { class: 'stx-gv-stage' })
|
|
145
|
+
this.edgeLayer = make('g', { class: 'stx-gv-edges' })
|
|
146
|
+
this.nodeLayer = make('g', { class: 'stx-gv-nodes' })
|
|
147
|
+
this.labelLayer = make('g', { class: 'stx-gv-edge-labels' })
|
|
148
|
+
this.stage.append(this.edgeLayer, this.nodeLayer, this.labelLayer)
|
|
149
|
+
this.svg.append(defs, this.stage)
|
|
150
|
+
this.surface.append(this.svg)
|
|
151
|
+
|
|
152
|
+
this.listen()
|
|
153
|
+
this.measure()
|
|
154
|
+
this.setGraph(options.nodes ?? [], options.edges ?? [])
|
|
155
|
+
if (options.selected)
|
|
156
|
+
this.select(options.selected, { silent: true })
|
|
157
|
+
|
|
158
|
+
if (typeof ResizeObserver !== 'undefined') {
|
|
159
|
+
this.observer = new ResizeObserver(() => {
|
|
160
|
+
this.measure()
|
|
161
|
+
if (this.autoFit)
|
|
162
|
+
this.fit()
|
|
163
|
+
})
|
|
164
|
+
this.observer.observe(this.surface)
|
|
165
|
+
}
|
|
166
|
+
root.setAttribute('data-gv-ready', '')
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/** The model behind the drawing. */
|
|
170
|
+
graph(): GraphModel {
|
|
171
|
+
return this.model
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
getViewport(): Viewport {
|
|
175
|
+
return { ...this.view }
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
getLayout(): GraphLayoutName {
|
|
179
|
+
return this.layoutName
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
getSelected(): string | null {
|
|
183
|
+
return this.selected
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/** Where every node belongs, as a plain object. */
|
|
187
|
+
getPositions(): Record<string, Point> {
|
|
188
|
+
return Object.fromEntries([...this.positions].map(([id, point]) => [id, { ...point }]))
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/** Replace the whole graph. */
|
|
192
|
+
setGraph(nodes: readonly GraphNode[], edges: readonly GraphEdge[]): void {
|
|
193
|
+
this.stop()
|
|
194
|
+
this.model = new GraphModel(nodes ?? [], edges ?? [])
|
|
195
|
+
this.positions = new Map()
|
|
196
|
+
this.pinned.clear()
|
|
197
|
+
if (this.selected && !this.model.has(this.selected))
|
|
198
|
+
this.selected = null
|
|
199
|
+
this.layout(false)
|
|
200
|
+
this.shown = new Map(this.positions)
|
|
201
|
+
this.render()
|
|
202
|
+
this.autoFit = true
|
|
203
|
+
this.fit()
|
|
204
|
+
this.mark()
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* Add to the graph around a node: the answer to an expand. What the graph
|
|
209
|
+
* already has is ignored. The new nodes grow out of the node they came from.
|
|
210
|
+
*/
|
|
211
|
+
addNeighbours(id: string, nodes: readonly GraphNode[], edges: readonly GraphEdge[]): { nodes: string[], edges: string[] } {
|
|
212
|
+
const added = this.model.merge(nodes ?? [], edges ?? [])
|
|
213
|
+
if (added.nodes.length === 0 && added.edges.length === 0)
|
|
214
|
+
return added
|
|
215
|
+
|
|
216
|
+
const from = this.shown.get(id) ?? this.positions.get(id) ?? toGraph(this.view, { x: this.size.width / 2, y: this.size.height / 2 })
|
|
217
|
+
for (const node of added.nodes)
|
|
218
|
+
this.shown.set(node, { ...from })
|
|
219
|
+
this.layout(true)
|
|
220
|
+
this.sync()
|
|
221
|
+
this.moveToPlace()
|
|
222
|
+
this.mark()
|
|
223
|
+
if (added.nodes.length)
|
|
224
|
+
this.announce(`${added.nodes.length} ${added.nodes.length === 1 ? 'node' : 'nodes'} added${this.model.has(id) ? ` to ${labelOf(this.model.node(id)!)}` : ''}`)
|
|
225
|
+
return added
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/** Change what a node says about itself, such as `expandable`. */
|
|
229
|
+
updateNode(id: string, patch: Partial<Omit<GraphNode, 'id'>>): void {
|
|
230
|
+
if (!this.model.updateNode(id, patch))
|
|
231
|
+
return
|
|
232
|
+
const old = this.nodeEls.get(id)
|
|
233
|
+
if (old) {
|
|
234
|
+
old.remove()
|
|
235
|
+
this.nodeEls.delete(id)
|
|
236
|
+
}
|
|
237
|
+
this.sync()
|
|
238
|
+
this.draw()
|
|
239
|
+
this.mark()
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
setLayout(name: GraphLayoutName): void {
|
|
243
|
+
const next = name === 'force' ? 'force' : 'layered'
|
|
244
|
+
if (next === this.layoutName)
|
|
245
|
+
return
|
|
246
|
+
this.layoutName = next
|
|
247
|
+
this.relayout()
|
|
248
|
+
this.callbacks.onLayout?.({ layout: next })
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
setDirection(direction: LayeredDirection): void {
|
|
252
|
+
const next = direction === 'TB' ? 'TB' : 'LR'
|
|
253
|
+
if (next === this.direction)
|
|
254
|
+
return
|
|
255
|
+
this.direction = next
|
|
256
|
+
if (this.layoutName === 'layered')
|
|
257
|
+
this.relayout()
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/** Select a node, or nothing with null. Says who it connects to. */
|
|
261
|
+
select(id: string | null, options: { silent?: boolean } = {}): void {
|
|
262
|
+
const next = id != null && this.model.has(id) ? id : null
|
|
263
|
+
if (next === this.selected)
|
|
264
|
+
return
|
|
265
|
+
this.selected = next
|
|
266
|
+
this.mark()
|
|
267
|
+
if (next)
|
|
268
|
+
this.describe(next)
|
|
269
|
+
if (!options.silent)
|
|
270
|
+
this.callbacks.onSelect?.(this.selection())
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/** Select a node and bring it to the middle. */
|
|
274
|
+
focusNode(id: string): void {
|
|
275
|
+
if (!this.model.has(id))
|
|
276
|
+
return
|
|
277
|
+
this.select(id)
|
|
278
|
+
const at = this.positions.get(id)
|
|
279
|
+
if (at) {
|
|
280
|
+
this.autoFit = false
|
|
281
|
+
this.setView(centreOn(this.view, at, this.size))
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
/** Show the whole graph. */
|
|
286
|
+
fit(): void {
|
|
287
|
+
this.measure()
|
|
288
|
+
this.setView(fit(this.model.bounds(this.positions), this.size, 32, this.minZoom, Math.min(1, this.maxZoom)))
|
|
289
|
+
this.autoFit = true
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
zoomIn(): void {
|
|
293
|
+
this.zoomBy(1.25)
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
zoomOut(): void {
|
|
297
|
+
this.zoomBy(0.8)
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
/** Zoom by a factor about a point on screen, the middle when none is given. */
|
|
301
|
+
zoomBy(factor: number, point?: Point): void {
|
|
302
|
+
this.autoFit = false
|
|
303
|
+
this.setView(zoomAt(this.view, point ?? { x: this.size.width / 2, y: this.size.height / 2 }, factor, this.minZoom, this.maxZoom))
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
panBy(dx: number, dy: number): void {
|
|
307
|
+
this.autoFit = false
|
|
308
|
+
this.setView(pan(this.view, dx, dy))
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
setZoomLimits(min: number, max: number): void {
|
|
312
|
+
this.minZoom = min
|
|
313
|
+
this.maxZoom = max
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
destroy(): void {
|
|
317
|
+
this.stop()
|
|
318
|
+
this.observer?.disconnect()
|
|
319
|
+
for (const dispose of this.disposers) dispose()
|
|
320
|
+
this.svg.remove()
|
|
321
|
+
this.root.removeAttribute('data-gv-ready')
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
private selection(): GraphSelectDetail {
|
|
325
|
+
const node = this.selected ? this.model.node(this.selected) ?? null : null
|
|
326
|
+
return { node, neighbours: node ? this.model.neighbours(node.id).map(id => this.model.node(id)!) : [] }
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
private measure(): void {
|
|
330
|
+
const box = this.surface.getBoundingClientRect()
|
|
331
|
+
this.size = { width: box.width, height: box.height }
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
/** Work out where every node belongs. `warm` keeps what is already placed. */
|
|
335
|
+
private layout(warm: boolean): void {
|
|
336
|
+
const started = performance.now()
|
|
337
|
+
if (this.layoutName === 'layered') {
|
|
338
|
+
this.positions = layeredLayout(this.model, { direction: this.direction }).positions
|
|
339
|
+
}
|
|
340
|
+
else {
|
|
341
|
+
this.positions = forceLayout(this.model, {
|
|
342
|
+
positions: warm && this.positions.size ? this.positions : null,
|
|
343
|
+
pinned: warm ? this.pinned : null,
|
|
344
|
+
})
|
|
345
|
+
}
|
|
346
|
+
this.timings.layout = performance.now() - started
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
private relayout(): void {
|
|
350
|
+
this.pinned.clear()
|
|
351
|
+
this.layout(false)
|
|
352
|
+
this.sync()
|
|
353
|
+
this.moveToPlace()
|
|
354
|
+
if (this.autoFit)
|
|
355
|
+
this.fit()
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
/** Create and remove elements until the SVG holds exactly the graph, then draw. */
|
|
359
|
+
private render(): void {
|
|
360
|
+
const started = performance.now()
|
|
361
|
+
this.sync()
|
|
362
|
+
this.draw()
|
|
363
|
+
this.timings.render = performance.now() - started
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
private sync(): void {
|
|
367
|
+
const limit = maxLabelLength(this.model.metrics)
|
|
368
|
+
|
|
369
|
+
for (const [id, element] of this.nodeEls) {
|
|
370
|
+
if (!this.model.has(id)) {
|
|
371
|
+
element.remove()
|
|
372
|
+
this.nodeEls.delete(id)
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
for (const node of this.model.nodes) {
|
|
376
|
+
let group = this.nodeEls.get(node.id)
|
|
377
|
+
if (!group) {
|
|
378
|
+
group = this.buildNode(node, limit)
|
|
379
|
+
this.nodeEls.set(node.id, group)
|
|
380
|
+
this.idOf.set(group, node.id)
|
|
381
|
+
this.nodeLayer.append(group)
|
|
382
|
+
}
|
|
383
|
+
const degree = this.model.degree(node.id)
|
|
384
|
+
group.setAttribute('aria-label', `${labelOf(node)}, ${degree} ${degree === 1 ? 'connection' : 'connections'}`)
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
for (const [id, parts] of this.edgeEls) {
|
|
388
|
+
if (!this.model.edge(id)) {
|
|
389
|
+
parts.group.remove()
|
|
390
|
+
parts.label?.remove()
|
|
391
|
+
this.edgeEls.delete(id)
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
for (const edge of this.model.edges) {
|
|
395
|
+
if (!this.edgeEls.has(edge.id))
|
|
396
|
+
this.edgeEls.set(edge.id, this.buildEdge(edge))
|
|
397
|
+
}
|
|
398
|
+
this.offsets = parallelOffsets(this.model.edges)
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
private buildNode(node: GraphNode, limit: number): SVGGElement {
|
|
402
|
+
const size = this.model.size(node.id)
|
|
403
|
+
const group = make('g', { 'class': 'stx-gv-node', 'id': `${this.prefix}-node-${this.serial++}`, 'role': 'group', 'data-node': node.id })
|
|
404
|
+
if (node.kind)
|
|
405
|
+
group.setAttribute('data-kind', node.kind)
|
|
406
|
+
if (node.expandable === true)
|
|
407
|
+
group.setAttribute('data-expandable', '')
|
|
408
|
+
|
|
409
|
+
const title = make('title')
|
|
410
|
+
title.textContent = node.detail ? `${labelOf(node)}: ${node.detail}` : labelOf(node)
|
|
411
|
+
const box = make('rect', { x: -size.width / 2, y: -size.height / 2, width: size.width, height: size.height, rx: 6, ry: 6 })
|
|
412
|
+
const mark = node.expandable === true ? (this.model.metrics.charWidth + 4) / 2 : 0
|
|
413
|
+
const label = make('text', { 'class': 'stx-gv-node-label', 'x': -mark, 'y': node.detail ? -6 : 0, 'text-anchor': 'middle', 'dominant-baseline': 'central' })
|
|
414
|
+
label.textContent = fitLabel(labelOf(node), limit)
|
|
415
|
+
group.append(title, box, label)
|
|
416
|
+
|
|
417
|
+
if (node.detail) {
|
|
418
|
+
const detail = make('text', { 'class': 'stx-gv-node-detail', 'x': -mark, 'y': 9, 'text-anchor': 'middle', 'dominant-baseline': 'central' })
|
|
419
|
+
detail.textContent = fitLabel(node.detail, Math.floor(limit / 0.85))
|
|
420
|
+
group.append(detail)
|
|
421
|
+
}
|
|
422
|
+
if (node.expandable === true) {
|
|
423
|
+
const plus = make('text', { 'class': 'stx-gv-node-mark', 'x': size.width / 2 - 10, 'y': 0, 'text-anchor': 'middle', 'dominant-baseline': 'central', 'aria-hidden': 'true' })
|
|
424
|
+
plus.textContent = '+'
|
|
425
|
+
group.append(plus)
|
|
426
|
+
}
|
|
427
|
+
return group
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
private buildEdge(edge: ResolvedEdge): EdgeParts {
|
|
431
|
+
const group = make('g', { 'class': 'stx-gv-edge', 'data-edge': edge.id })
|
|
432
|
+
const path = make('path', { fill: 'none' })
|
|
433
|
+
if (edge.directed !== false)
|
|
434
|
+
path.setAttribute('marker-end', `url(#${this.prefix}-arrow)`)
|
|
435
|
+
group.append(path)
|
|
436
|
+
this.edgeLayer.append(group)
|
|
437
|
+
|
|
438
|
+
let label: SVGGElement | null = null
|
|
439
|
+
if (edge.label) {
|
|
440
|
+
const text = fitLabel(String(edge.label), 32)
|
|
441
|
+
const width = Math.ceil(text.length * LABEL_CHAR) + 8
|
|
442
|
+
label = make('g', { 'class': 'stx-gv-edge-label', 'data-edge': edge.id })
|
|
443
|
+
const plate = make('rect', { x: -width / 2, y: -8, width, height: 16, rx: 3, ry: 3 })
|
|
444
|
+
const words = make('text', { 'text-anchor': 'middle', 'dominant-baseline': 'central' })
|
|
445
|
+
words.textContent = text
|
|
446
|
+
label.append(plate, words)
|
|
447
|
+
this.labelLayer.append(label)
|
|
448
|
+
}
|
|
449
|
+
return { group, path, label }
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
/** Write every node and edge at where it is shown. */
|
|
453
|
+
private draw(): void {
|
|
454
|
+
for (const [id, group] of this.nodeEls) {
|
|
455
|
+
const at = this.shown.get(id) ?? this.positions.get(id)
|
|
456
|
+
if (at)
|
|
457
|
+
group.setAttribute('transform', `translate(${at.x} ${at.y})`)
|
|
458
|
+
}
|
|
459
|
+
for (const edge of this.model.edges)
|
|
460
|
+
this.drawEdge(edge)
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
private shape(edge: ResolvedEdge): EdgeShape | null {
|
|
464
|
+
const source = this.shown.get(edge.source) ?? this.positions.get(edge.source)
|
|
465
|
+
const target = this.shown.get(edge.target) ?? this.positions.get(edge.target)
|
|
466
|
+
if (!source || !target)
|
|
467
|
+
return null
|
|
468
|
+
return edgeShape(
|
|
469
|
+
{ source, sourceSize: this.model.size(edge.source), target, targetSize: this.model.size(edge.target) },
|
|
470
|
+
{ curve: this.layoutName === 'layered' ? 'bezier' : 'straight', direction: this.direction, offset: this.offsets.get(edge.id) ?? 0, inset: edge.directed !== false ? 1 : 0 },
|
|
471
|
+
)
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
private drawEdge(edge: ResolvedEdge): void {
|
|
475
|
+
const parts = this.edgeEls.get(edge.id)
|
|
476
|
+
const shape = parts && this.shape(edge)
|
|
477
|
+
if (!parts || !shape)
|
|
478
|
+
return
|
|
479
|
+
parts.path.setAttribute('d', shape.d)
|
|
480
|
+
parts.label?.setAttribute('transform', `translate(${Math.round(shape.label.x * 10) / 10} ${Math.round(shape.label.y * 10) / 10})`)
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
/** Move the nodes from where they are shown to where they belong. */
|
|
484
|
+
private moveToPlace(): void {
|
|
485
|
+
this.stop()
|
|
486
|
+
const still = typeof matchMedia === 'function' && matchMedia('(prefers-reduced-motion: reduce)').matches
|
|
487
|
+
if (!this.animate || still || typeof requestAnimationFrame !== 'function') {
|
|
488
|
+
this.shown = new Map(this.positions)
|
|
489
|
+
this.draw()
|
|
490
|
+
return
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
const from = new Map<string, Point>()
|
|
494
|
+
for (const [id, to] of this.positions)
|
|
495
|
+
from.set(id, this.shown.get(id) ?? to)
|
|
496
|
+
const started = performance.now()
|
|
497
|
+
const step = (now: number): void => {
|
|
498
|
+
const t = Math.min(1, (now - started) / MOVE_TIME)
|
|
499
|
+
const eased = 1 - (1 - t) ** 3
|
|
500
|
+
for (const [id, to] of this.positions) {
|
|
501
|
+
const a = from.get(id)!
|
|
502
|
+
this.shown.set(id, { x: a.x + (to.x - a.x) * eased, y: a.y + (to.y - a.y) * eased })
|
|
503
|
+
}
|
|
504
|
+
this.draw()
|
|
505
|
+
this.frame = t < 1 ? requestAnimationFrame(step) : 0
|
|
506
|
+
if (t >= 1)
|
|
507
|
+
this.shown = new Map(this.positions)
|
|
508
|
+
}
|
|
509
|
+
this.frame = requestAnimationFrame(step)
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
private stop(): void {
|
|
513
|
+
if (this.frame)
|
|
514
|
+
cancelAnimationFrame(this.frame)
|
|
515
|
+
this.frame = 0
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
private setView(next: Viewport): void {
|
|
519
|
+
this.view = next
|
|
520
|
+
this.stage.setAttribute('transform', `translate(${next.x} ${next.y}) scale(${next.k})`)
|
|
521
|
+
this.callbacks.onViewport?.({ ...next })
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
/** Mark the selected node, its edges and its neighbours; the stylesheet dims the rest. */
|
|
525
|
+
private mark(): void {
|
|
526
|
+
const selected = this.selected
|
|
527
|
+
const near = new Set(selected ? this.model.neighbours(selected) : [])
|
|
528
|
+
this.root.toggleAttribute('data-has-selection', Boolean(selected))
|
|
529
|
+
for (const [id, group] of this.nodeEls) {
|
|
530
|
+
group.toggleAttribute('data-selected', id === selected)
|
|
531
|
+
group.toggleAttribute('data-neighbour', near.has(id))
|
|
532
|
+
if (id === selected)
|
|
533
|
+
group.setAttribute('aria-current', 'true')
|
|
534
|
+
else
|
|
535
|
+
group.removeAttribute('aria-current')
|
|
536
|
+
}
|
|
537
|
+
for (const edge of this.model.edges) {
|
|
538
|
+
const parts = this.edgeEls.get(edge.id)
|
|
539
|
+
if (!parts)
|
|
540
|
+
continue
|
|
541
|
+
const active = selected != null && (edge.source === selected || edge.target === selected)
|
|
542
|
+
parts.group.toggleAttribute('data-active', active)
|
|
543
|
+
parts.label?.toggleAttribute('data-active', active)
|
|
544
|
+
if (edge.directed !== false)
|
|
545
|
+
parts.path.setAttribute('marker-end', `url(#${this.prefix}-${active ? 'arrow-active' : 'arrow'})`)
|
|
546
|
+
}
|
|
547
|
+
const element = selected ? this.nodeEls.get(selected) : null
|
|
548
|
+
if (element)
|
|
549
|
+
this.surface.setAttribute('aria-activedescendant', element.id)
|
|
550
|
+
else
|
|
551
|
+
this.surface.removeAttribute('aria-activedescendant')
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
private describe(id: string): void {
|
|
555
|
+
const node = this.model.node(id)
|
|
556
|
+
if (!node)
|
|
557
|
+
return
|
|
558
|
+
const names = this.model.neighbours(id).map(other => labelOf(this.model.node(other)!))
|
|
559
|
+
const shown = names.slice(0, 5).join(', ')
|
|
560
|
+
const rest = names.length > 5 ? ` and ${names.length - 5} more` : ''
|
|
561
|
+
this.announce(names.length === 0
|
|
562
|
+
? `${labelOf(node)}, no connections`
|
|
563
|
+
: `${labelOf(node)}, ${names.length} ${names.length === 1 ? 'connection' : 'connections'}: ${shown}${rest}`)
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
private announce(text: string): void {
|
|
567
|
+
if (this.status)
|
|
568
|
+
this.status.textContent = text
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
private expand(id: string, open = false): void {
|
|
572
|
+
const node = this.model.node(id)
|
|
573
|
+
if (!node)
|
|
574
|
+
return
|
|
575
|
+
this.lastExpand = performance.now()
|
|
576
|
+
// A node with nothing more to load is opened instead.
|
|
577
|
+
if (open || node.expandable === false)
|
|
578
|
+
this.callbacks.onOpen?.({ node })
|
|
579
|
+
else
|
|
580
|
+
this.callbacks.onExpand?.({ node })
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
private nodeUnder(target: EventTarget | null): string | null {
|
|
584
|
+
const element = target instanceof Element ? target.closest('.stx-gv-node') : null
|
|
585
|
+
return element ? this.idOf.get(element) ?? null : null
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
private local(event: { clientX: number, clientY: number }): Point {
|
|
589
|
+
const box = this.surface.getBoundingClientRect()
|
|
590
|
+
return { x: event.clientX - box.left, y: event.clientY - box.top }
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
private listen(): void {
|
|
594
|
+
const on = <K extends keyof HTMLElementEventMap>(type: K, handler: (event: HTMLElementEventMap[K]) => void, options?: AddEventListenerOptions): void => {
|
|
595
|
+
this.surface.addEventListener(type, handler as EventListener, options)
|
|
596
|
+
this.disposers.push(() => this.surface.removeEventListener(type, handler as EventListener, options))
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
on('pointerdown', event => this.onPointerDown(event))
|
|
600
|
+
on('pointermove', event => this.onPointerMove(event))
|
|
601
|
+
on('pointerup', event => this.onPointerUp(event))
|
|
602
|
+
on('pointercancel', () => this.endGesture())
|
|
603
|
+
on('dblclick', (event) => {
|
|
604
|
+
// The pointer handlers already saw a double click made with a pointer.
|
|
605
|
+
const id = this.nodeUnder(event.target)
|
|
606
|
+
if (id && performance.now() - this.lastExpand > DOUBLE_CLICK)
|
|
607
|
+
this.expand(id, event.metaKey || event.ctrlKey)
|
|
608
|
+
})
|
|
609
|
+
on('wheel', event => this.onWheel(event), { passive: false })
|
|
610
|
+
on('keydown', event => this.onKeyDown(event))
|
|
611
|
+
on('contextmenu', (event) => {
|
|
612
|
+
if (this.contextMenu)
|
|
613
|
+
event.preventDefault()
|
|
614
|
+
const id = this.nodeUnder(event.target)
|
|
615
|
+
if (id)
|
|
616
|
+
this.select(id)
|
|
617
|
+
this.callbacks.onContext?.({ node: id ? this.model.node(id)! : null, x: event.clientX, y: event.clientY })
|
|
618
|
+
})
|
|
619
|
+
|
|
620
|
+
// A trackpad pinch in WebKit arrives as gesture events, not as a wheel.
|
|
621
|
+
const gesture = (type: string, handler: (event: Event & { scale: number, clientX: number, clientY: number }) => void): void => {
|
|
622
|
+
this.surface.addEventListener(type, handler as EventListener)
|
|
623
|
+
this.disposers.push(() => this.surface.removeEventListener(type, handler as EventListener))
|
|
624
|
+
}
|
|
625
|
+
gesture('gesturestart', (event) => {
|
|
626
|
+
event.preventDefault()
|
|
627
|
+
const point = this.local(event)
|
|
628
|
+
this.pinch = { k: this.view.k, x: point.x, y: point.y }
|
|
629
|
+
})
|
|
630
|
+
gesture('gesturechange', (event) => {
|
|
631
|
+
if (!this.pinch)
|
|
632
|
+
return
|
|
633
|
+
event.preventDefault()
|
|
634
|
+
this.zoomBy((this.pinch.k * event.scale) / this.view.k, { x: this.pinch.x, y: this.pinch.y })
|
|
635
|
+
})
|
|
636
|
+
gesture('gestureend', () => {
|
|
637
|
+
this.pinch = null
|
|
638
|
+
})
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
private onPointerDown(event: PointerEvent): void {
|
|
642
|
+
if (event.button !== 0)
|
|
643
|
+
return
|
|
644
|
+
const node = this.nodeUnder(event.target)
|
|
645
|
+
this.surface.focus({ preventScroll: true })
|
|
646
|
+
try {
|
|
647
|
+
this.surface.setPointerCapture(event.pointerId)
|
|
648
|
+
}
|
|
649
|
+
catch {}
|
|
650
|
+
this.gesture = {
|
|
651
|
+
pointer: event.pointerId,
|
|
652
|
+
node,
|
|
653
|
+
startX: event.clientX,
|
|
654
|
+
startY: event.clientY,
|
|
655
|
+
origin: node ? { ...(this.positions.get(node) ?? { x: 0, y: 0 }) } : { x: 0, y: 0 },
|
|
656
|
+
view: { ...this.view },
|
|
657
|
+
moved: false,
|
|
658
|
+
}
|
|
659
|
+
}
|
|
660
|
+
|
|
661
|
+
private onPointerMove(event: PointerEvent): void {
|
|
662
|
+
const gesture = this.gesture
|
|
663
|
+
if (!gesture || event.pointerId !== gesture.pointer)
|
|
664
|
+
return
|
|
665
|
+
const dx = event.clientX - gesture.startX
|
|
666
|
+
const dy = event.clientY - gesture.startY
|
|
667
|
+
if (!gesture.moved) {
|
|
668
|
+
if (Math.abs(dx) < DRAG_THRESHOLD && Math.abs(dy) < DRAG_THRESHOLD)
|
|
669
|
+
return
|
|
670
|
+
gesture.moved = true
|
|
671
|
+
this.stop()
|
|
672
|
+
this.shown = new Map(this.positions)
|
|
673
|
+
this.root.setAttribute('data-dragging', gesture.node ? 'node' : 'pan')
|
|
674
|
+
}
|
|
675
|
+
|
|
676
|
+
if (gesture.node) {
|
|
677
|
+
const at = { x: gesture.origin.x + dx / this.view.k, y: gesture.origin.y + dy / this.view.k }
|
|
678
|
+
this.positions.set(gesture.node, at)
|
|
679
|
+
this.shown.set(gesture.node, at)
|
|
680
|
+
this.nodeEls.get(gesture.node)?.setAttribute('transform', `translate(${at.x} ${at.y})`)
|
|
681
|
+
for (const edge of this.model.edgesOf(gesture.node))
|
|
682
|
+
this.drawEdge(edge)
|
|
683
|
+
}
|
|
684
|
+
else {
|
|
685
|
+
this.autoFit = false
|
|
686
|
+
this.setView(pan(gesture.view, dx, dy))
|
|
687
|
+
}
|
|
688
|
+
}
|
|
689
|
+
|
|
690
|
+
private onPointerUp(event: PointerEvent): void {
|
|
691
|
+
const gesture = this.gesture
|
|
692
|
+
if (!gesture || event.pointerId !== gesture.pointer)
|
|
693
|
+
return
|
|
694
|
+
this.endGesture()
|
|
695
|
+
|
|
696
|
+
if (gesture.moved) {
|
|
697
|
+
if (gesture.node) {
|
|
698
|
+
// A node put somewhere by hand stays there when the forces run again.
|
|
699
|
+
this.pinned.add(gesture.node)
|
|
700
|
+
this.autoFit = false
|
|
701
|
+
const at = this.positions.get(gesture.node)!
|
|
702
|
+
this.callbacks.onMove?.({ node: this.model.node(gesture.node)!, x: at.x, y: at.y })
|
|
703
|
+
}
|
|
704
|
+
return
|
|
705
|
+
}
|
|
706
|
+
|
|
707
|
+
if (!gesture.node) {
|
|
708
|
+
this.select(null)
|
|
709
|
+
return
|
|
710
|
+
}
|
|
711
|
+
const now = performance.now()
|
|
712
|
+
const double = this.lastClick.id === gesture.node && now - this.lastClick.at < DOUBLE_CLICK
|
|
713
|
+
this.lastClick = double ? { id: '', at: 0 } : { id: gesture.node, at: now }
|
|
714
|
+
this.select(gesture.node)
|
|
715
|
+
if (double)
|
|
716
|
+
this.expand(gesture.node, event.metaKey || event.ctrlKey)
|
|
717
|
+
}
|
|
718
|
+
|
|
719
|
+
private endGesture(): void {
|
|
720
|
+
const gesture = this.gesture
|
|
721
|
+
this.gesture = null
|
|
722
|
+
this.root.removeAttribute('data-dragging')
|
|
723
|
+
if (gesture) {
|
|
724
|
+
try {
|
|
725
|
+
this.surface.releasePointerCapture(gesture.pointer)
|
|
726
|
+
}
|
|
727
|
+
catch {}
|
|
728
|
+
}
|
|
729
|
+
}
|
|
730
|
+
|
|
731
|
+
private onWheel(event: WheelEvent): void {
|
|
732
|
+
event.preventDefault()
|
|
733
|
+
if (event.ctrlKey || event.metaKey) {
|
|
734
|
+
// A pinch arrives as a wheel with Control held, in small steps.
|
|
735
|
+
this.zoomBy(Math.exp(-event.deltaY * (event.deltaMode === 1 ? 0.05 : 0.01)), this.local(event))
|
|
736
|
+
return
|
|
737
|
+
}
|
|
738
|
+
const unit = event.deltaMode === 1 ? 16 : 1
|
|
739
|
+
this.panBy(-event.deltaX * unit, -event.deltaY * unit)
|
|
740
|
+
}
|
|
741
|
+
|
|
742
|
+
private onKeyDown(event: KeyboardEvent): void {
|
|
743
|
+
const headings: Record<string, Heading> = { ArrowLeft: 'left', ArrowRight: 'right', ArrowUp: 'up', ArrowDown: 'down' }
|
|
744
|
+
const heading = headings[event.key]
|
|
745
|
+
|
|
746
|
+
if (heading) {
|
|
747
|
+
event.preventDefault()
|
|
748
|
+
const next = this.selected
|
|
749
|
+
? nearestInDirection(this.positions, this.selected, heading)
|
|
750
|
+
: nearestTo(this.positions, toGraph(this.view, { x: this.size.width / 2, y: this.size.height / 2 }))
|
|
751
|
+
if (next) {
|
|
752
|
+
this.select(next)
|
|
753
|
+
this.bringIntoView(next)
|
|
754
|
+
}
|
|
755
|
+
return
|
|
756
|
+
}
|
|
757
|
+
|
|
758
|
+
const mod = event.metaKey || event.ctrlKey
|
|
759
|
+
if (event.key === 'Enter' || event.key === ' ') {
|
|
760
|
+
if (!this.selected)
|
|
761
|
+
return
|
|
762
|
+
event.preventDefault()
|
|
763
|
+
this.expand(this.selected, mod)
|
|
764
|
+
}
|
|
765
|
+
else if (mod || event.altKey) {
|
|
766
|
+
// Everything below is a bare key; leave the browser's shortcuts alone.
|
|
767
|
+
}
|
|
768
|
+
else if (event.key === '+' || event.key === '=') {
|
|
769
|
+
event.preventDefault()
|
|
770
|
+
this.zoomIn()
|
|
771
|
+
}
|
|
772
|
+
else if (event.key === '-' || event.key === '_') {
|
|
773
|
+
event.preventDefault()
|
|
774
|
+
this.zoomOut()
|
|
775
|
+
}
|
|
776
|
+
else if (event.key === '0') {
|
|
777
|
+
event.preventDefault()
|
|
778
|
+
this.fit()
|
|
779
|
+
}
|
|
780
|
+
else if (event.key === 'f' || event.key === 'F') {
|
|
781
|
+
if (!this.selected)
|
|
782
|
+
return
|
|
783
|
+
event.preventDefault()
|
|
784
|
+
this.focusNode(this.selected)
|
|
785
|
+
}
|
|
786
|
+
else if (event.key === 'o' || event.key === 'O') {
|
|
787
|
+
if (!this.selected)
|
|
788
|
+
return
|
|
789
|
+
event.preventDefault()
|
|
790
|
+
this.expand(this.selected, true)
|
|
791
|
+
}
|
|
792
|
+
else if (event.key === 'Escape' && this.selected) {
|
|
793
|
+
event.preventDefault()
|
|
794
|
+
this.select(null)
|
|
795
|
+
}
|
|
796
|
+
}
|
|
797
|
+
|
|
798
|
+
private bringIntoView(id: string): void {
|
|
799
|
+
const at = this.positions.get(id)
|
|
800
|
+
if (!at)
|
|
801
|
+
return
|
|
802
|
+
const size = this.model.size(id)
|
|
803
|
+
const next = reveal(this.view, { x: at.x - size.width / 2, y: at.y - size.height / 2, width: size.width, height: size.height }, this.size)
|
|
804
|
+
if (next !== this.view) {
|
|
805
|
+
this.autoFit = false
|
|
806
|
+
this.setView(next)
|
|
807
|
+
}
|
|
808
|
+
}
|
|
809
|
+
}
|