-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtree.ts
464 lines (365 loc) · 10.4 KB
/
tree.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
import * as d3 from 'd3'
import { parse } from './parse-ctors.web'
import { clamp, className, escapeRegExp, not } from './utils'
// MODULE AUGMENTATION
declare module 'd3-hierarchy' {
interface HierarchyNode<Datum> {
_children: this['children']
isLeaf: boolean
totalDescendants: number
}
interface HierarchyPointNode<Datum> {
isCollapsed: () => boolean
_x: number
_y: number
}
}
// DOMAIN TYPES
type Datum = {
path: string
children: Array<Datum>
}
type DatumLink = d3.HierarchyPointLink<Datum>
type DatumNode = d3.HierarchyPointNode<Datum>
// ALIAS TYPES
type Circle = SVGCircleElement
type Div = HTMLDivElement
type G = SVGGElement
type Input = HTMLInputElement
type Path = SVGPathElement
type Rect = SVGRectElement
type SVG = SVGSVGElement
type Text = SVGTextElement
type NumberPair = [number, number]
// CONTROLS
const circleRadius = 8
const duration = 300
const nodeSize: NumberPair = [4.5 * circleRadius, 300]
const scaleExtent: NumberPair = [0.2, 2]
const pad = 0.5 * circleRadius
const maxSearchOptions = 20
// D3 GENERATORS
const treeGenerator = d3.tree<Datum>().nodeSize(nodeSize)
const linkGenerator = d3
.linkHorizontal<DatumLink, DatumNode>()
.x(d => d.x)
.y(d => d.y)
const zoomBehavior = d3
.zoom<SVG, DatumNode>()
.scaleExtent(scaleExtent)
.on('zoom', () => {
const { x, y, k } = d3.event.transform
$zoomPanGroup.attr('transform', `translate(${x}, ${y}) scale(${k})`)
})
// PROCESSES
// focus node
function focusNode(datum: DatumNode) {
focusedNodes = datum.ancestors()
zoomBehavior.transform(
$tree.transition().duration(1.5 * duration),
d3.zoomIdentity.translate(
0.5 * parseInt($tree.attr('width')) - datum.x,
0.5 * parseInt($tree.attr('height')) - datum.y
)
)
renderTree(root)
}
// load data
function loadData(): Promise<Array<Datum>> {
let mode = new URLSearchParams(location.search).get('mode')
if (mode === null || not(['core', 'node', 'browser'].includes(mode))) {
mode = 'core'
}
// load ctors from iframe
if (mode === 'browser') {
const iframe = document.createElement('iframe')
iframe.style.display = 'none'
document.body.append(iframe)
return Promise.resolve(parse(iframe.contentWindow!.window))
}
return d3.json(`./ctors.${mode}.json`)
}
// render tree
function renderTree(base: DatumNode) {
treeGenerator(root)
root.descendants().forEach(d => {
// flip layout
const { x, y } = d
d.x = y
d.y = x
})
// links
const $links = $linksGroup
.selectAll<Path, DatumLink>('path')
.data(root.links().filter(l => l.source.depth > 0), l => String(l.target.id))
// links@enter
$links
.enter()
.append<Path>('path')
.attr('stroke-opacity', 0)
.attr('d', () => {
const o = { x: base._x, y: base._y } as DatumNode
return linkGenerator({ source: o, target: o })
})
// links@enter+update
.merge($links)
.attr('class', l => className(['links', focusedNodes.includes(l.target) && 'focused']))
.transition()
.duration(duration)
.attr('stroke-opacity', 1)
.attr('d', linkGenerator)
// links@exit
$links
.exit<DatumLink>()
.transition()
.duration(duration)
.attr('stroke-opacity', 0)
.attr('d', l => {
const ancestor = l.source.ancestors().find(a => a.isCollapsed()) || root
return linkGenerator({ source: ancestor, target: ancestor })
})
.remove()
// nodes
const $nodes = $nodesGroup
.selectAll<G, DatumNode>('g')
.data(root.descendants().filter(d => d.depth > 0), d => String(d.id))
// nodes@enter
const $nodesEnter = $nodes
.enter()
.append<G>('g')
.attr('class', 'nodes')
.attr('fill-opacity', 0)
.attr('transform', `translate(${base._x}, ${base._y})`)
.on('dblclick', () => {
d3.event.stopImmediatePropagation()
})
// nodes@enter#labelboxes
$nodesEnter.append<Rect>('rect').attr('rx', 4)
// nodes@enter#labels
$nodesEnter
.append<Text>('text')
.attr('dy', 4)
.on('mousedown', () => {
d3.event.stopImmediatePropagation()
})
// nodes@enter#circles
$nodesEnter
.append<Circle>('circle')
.attr('r', circleRadius)
.attr('stroke-width', circleRadius / 2)
.on('click', d => {
if (d3.event.shiftKey && d.parent && d.parent.children) {
d.parent.children.forEach(child => {
child.children = undefined
})
} else {
// toggle children
d.children = d.isCollapsed() ? d._children : undefined
}
// save positions of next base node
d._x = d.x
d._y = d.y
renderTree(d)
// bring new base node into focus
const datum = d3.select<Circle, DatumNode>(d3.event.target).datum()
focusNode(datum)
})
// nodes@enter+update
const $nodesEnterUpdate = $nodesEnter.merge($nodes).attr('class', d =>
className([
// prettier-multiline
'nodes',
d.isLeaf && 'leaf',
d.isCollapsed() && 'collapsed',
focusedNodes.includes(d) && 'focused',
])
)
$nodesEnterUpdate
.transition()
.duration(duration)
.attr('fill-opacity', 1)
.attr('transform', d => `translate(${d.x}, ${d.y})`)
// nodes@enter+update#labels
$nodesEnterUpdate
.selectAll<Text, DatumNode>('text')
.text(
d =>
d.data.path + (d.isCollapsed() ? ` #${d._children!.length} | #${d.totalDescendants}` : '')
)
.attr('dx', d => (circleRadius + pad) * (d.isLeaf || d.isCollapsed() ? 1 : -1))
// nodes@enter+update#labelboxes
$nodesEnterUpdate.nodes().forEach(node => {
const $node = d3.select<G, DatumNode>(node)
const text = $node.select<Text>('text').node()!
const bbox = text.getBBox()
$node
.select('rect')
.attr('x', bbox.x - pad)
.attr('y', bbox.y - 0.5 * pad)
.attr('width', bbox.width + 2 * pad)
.attr('height', bbox.height + pad)
})
// nodes@enter+update#circles
$nodesEnterUpdate.selectAll<Circle, DatumNode>('circle')
// nodes@exit
$nodes
.exit<DatumNode>()
.transition()
.duration(duration)
.attr('fill-opacity', 0)
.attr('transform', d => {
const ancestor = d.ancestors().find(a => a.isCollapsed()) || root
return `translate(${ancestor.x}, ${ancestor.y})`
})
.remove()
}
// render search
function renderSearch() {
let searchOptionsData: Array<DatumNode> = []
if (searchQuery.length > 0) {
const data = root.descendants()
const patterns = [
// match exact
new RegExp('^' + searchQuery + '$', 'i'),
// match starts with
new RegExp('^' + searchQuery, 'i'),
// match middle
new RegExp(searchQuery, 'i'),
// match fuzzy
new RegExp(searchQuery.split('').join('.{0,2}'), 'i'),
]
const matches = patterns.map(p => data.filter(d => d.data.path.match(p))).flat()
const uniqueMatches = Array.from(new Set(matches))
searchOptionsData = uniqueMatches
.slice(0, maxSearchOptions)
.sort((a, b) => (a.data.path < b.data.path ? -1 : a.data.path > b.data.path ? 1 : 0))
}
const $searchOptions = $search
.selectAll<Div, DatumNode>('div')
.data(searchOptionsData, d => String(d.id))
const totalChoices = $searchOptions.size()
searchChoice = clamp(searchChoice, -1, totalChoices - 1)
// search-options@enter
$searchOptions
.enter()
.append('div')
// search-options@enter+update
.merge($searchOptions)
.attr('class', (_, i) => className([i === searchChoice && 'choice']))
.attr('title', d => d.data.path)
.text(d => d.data.path)
// search-options@exit
$searchOptions.exit().remove()
if (searchChoice === -1) {
$searchInput.property('value', searchQuery)
$searchInput.node()!.scrollIntoView(true)
} else {
const datum = searchOptionsData[searchChoice]
$searchInput.property('value', datum.data.path)
focusNode(datum)
$searchOptions.nodes()[searchChoice].scrollIntoView(false)
}
}
// resize svg
function resize() {
$tree.attr('width', window.innerWidth).attr('height', window.innerHeight)
}
// D3 SELECTIONS
const $tree = d3
.select<SVG, DatumNode>('#tree')
.on('mousedown', () => {
const selection = window.getSelection()
if (selection) {
selection.removeAllRanges()
}
})
.call(zoomBehavior)
const $zoomPanGroup = $tree.append<G>('g').style('will-change', 'transform')
const $linksGroup = $zoomPanGroup.append<G>('g')
const $nodesGroup = $zoomPanGroup.append<G>('g')
const $search = d3
.select<Div, never>('#search')
.on('keydown', () => {
const e = d3.event as KeyboardEvent
switch (e.key) {
case 'ArrowDown':
e.preventDefault()
searchChoice += 1
break
case 'ArrowUp':
e.preventDefault()
searchChoice -= 1
break
case 'Enter':
e.preventDefault()
searchQuery = ''
break
case 'Escape':
e.preventDefault()
searchQuery = ''
searchChoice = -1
break
}
renderSearch()
})
.on('mousedown', () => {
const e = d3.event as MouseEvent
searchChoice = $search
.selectAll<Div, DatumNode>('div')
.nodes()
.indexOf(e.target as Div)
})
.on('mouseup', () => {
renderSearch()
searchQuery = ''
searchChoice = -1
renderSearch()
})
const $searchInput = $search.select<Input>('input').on('input', () => {
searchQuery = escapeRegExp($searchInput.property('value'))
searchChoice = -1
renderSearch()
})
// STATE
let focusedNodes: Array<DatumNode> = []
let searchQuery = ''
let searchChoice = -1
let root: DatumNode // root node
// LISTENERS
window.addEventListener('resize', () => {
resize()
renderTree(root)
})
window.addEventListener('keydown', e => {
const isMacOS = navigator.platform.startsWith('Mac')
const isFindCommand = e.key === 'f' && (isMacOS ? e.metaKey : e.ctrlKey)
if (isFindCommand) {
e.preventDefault()
$searchInput.node()!.focus()
}
})
// MAIN
loadData().then(ctors => {
root = treeGenerator(
d3.hierarchy({
path: '',
children: ctors,
})
)
// root node
root.descendants().forEach((d, i) => {
// @ts-ignore
d.id = i
// save children for recovering collapsed nodes
d._children = d.children
d.isLeaf = d._children === undefined
d.totalDescendants = d.isLeaf ? 0 : d.descendants().length - 1
d.isCollapsed = () => not(d._children === d.children)
})
// save positions of root node as next base node
root._x = 0
root._y = 0
resize()
renderTree(root)
zoomBehavior.translateBy($tree, 200 - nodeSize[1], 0.5 * Number($tree.attr('height')))
})