-
-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathmoduleGraph.ts
More file actions
411 lines (355 loc) · 11.3 KB
/
moduleGraph.ts
File metadata and controls
411 lines (355 loc) · 11.3 KB
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
import type { ComputedRefWithControl } from '@vueuse/core'
import type { HierarchyLink, HierarchyNode } from 'd3-hierarchy'
import type { ComputedRef, InjectionKey, MaybeRef, Ref, ShallowReactive, ShallowRef } from 'vue'
import type { ModuleListItem } from '~~/shared/types'
import { computedWithControl, onKeyPressed, useEventListener, useMagicKeys } from '@vueuse/core'
import { hierarchy, tree } from 'd3-hierarchy'
import { linkHorizontal, linkVertical } from 'd3-shape'
import Fuse from 'fuse.js'
import { computed, inject, nextTick, provide, ref, shallowReactive, shallowRef, unref } from 'vue'
import { useZoomElement } from './zoomElement'
export interface ModuleGraphNode<M, I> {
module: M
import?: I
expanded?: boolean
hasChildren?: boolean
}
export interface ModuleGraphSpacing {
width: MaybeRef<number>
height: MaybeRef<number>
linkOffset: MaybeRef<number>
margin: MaybeRef<number>
gap: MaybeRef<number>
}
export type ModuleGraphLink<M, I> = HierarchyLink<ModuleGraphNode<M, I>> & {
id: string
import?: I
}
interface ModuleGraphGenerateBaseOptions {
spacing: ModuleGraphSpacing
isFirstCalculateGraph: Ref<boolean>
container: Ref<HTMLDivElement>
nodesRefMap: ShallowReactive<Map<string, HTMLDivElement>>
width: Ref<number>
height: Ref<number>
scale: Ref<number, number>
childToParentMap: ShallowReactive<Map<string, string>>
collapsedNodes: ShallowReactive<Set<string>>
}
interface ModuleGraphOptions<M, I> {
spacing: ModuleGraphSpacing
modules: Ref<M[]> | ComputedRef<M[]>
generateGraph: (options: ModuleGraphGenerateBaseOptions & {
nodes: ShallowRef<HierarchyNode<ModuleGraphNode<M, I>>[], HierarchyNode<ModuleGraphNode<M, I>>[]>
links: ShallowRef<ModuleGraphLink<M, I>[], ModuleGraphLink<M, I>[]>
hierarchy: typeof hierarchy
tree: typeof tree
modulesMap: ComputedRef<Map<string, M>>
nodesMap: ShallowReactive<Map<string, HierarchyNode<ModuleGraphNode<M, I>>>>
linksMap: ShallowReactive<Map<string, ModuleGraphLink<M, I>>>
focusOn: (id: string, animated?: boolean) => void
}) => (focusOnFirstRootNode?: boolean) => void
}
const ViteDevToolsModuleGraphStateSymbol: InjectionKey<ModuleGraphGenerateBaseOptions & {
nodes: ShallowRef<HierarchyNode<ModuleGraphNode<any, any>>[], HierarchyNode<ModuleGraphNode<any, any>>[]>
links: ShallowRef<ModuleGraphLink<any, any>[], ModuleGraphLink<any, any>[]>
calculateGraph: (focusOnFirstRootNode?: boolean) => void
ZOOM_MIN: number
ZOOM_MAX: number
zoomIn: (factor?: number) => void
zoomOut: (factor?: number) => void
}> = Symbol.for('ViteDevToolsModuleGraphState')
export const createLinkHorizontal = linkHorizontal()
.x(d => d[0])
.y(d => d[1])
export const createLinkVertical = linkVertical()
.x(d => d[0])
.y(d => d[1])
export function generateModuleGraphLink<M, I>(link: ModuleGraphLink<M, I>, spacing?: ModuleGraphSpacing) {
if (!spacing) {
if (link.target.x! <= link.source.x!) {
return createLinkVertical({
source: [link.source.x!, link.source.y!],
target: [link.target.x!, link.target.y!],
})
}
return createLinkHorizontal({
source: [link.source.x!, link.source.y!],
target: [link.target.x!, link.target.y!],
})
}
else {
if (link.target.x! <= link.source.x!) {
return createLinkVertical({
source: [link.source.x! + unref(spacing.width) / 2 - unref(spacing.linkOffset), link.source.y!],
target: [link.target.x! - unref(spacing.width) / 2 + unref(spacing.linkOffset), link.target.y!],
})
}
return createLinkHorizontal({
source: [link.source.x! + unref(spacing.width) / 2 - unref(spacing.linkOffset), link.source.y!],
target: [link.target.x! - unref(spacing.width) / 2 + unref(spacing.linkOffset), link.target.y!],
})
}
}
// @unocss-include
export function getModuleGraphLinkColor<M, I>(_link: ModuleGraphLink<M, I>) {
return 'stroke-#8885'
}
export function createModuleGraph<M extends { id: string }, I>(options: ModuleGraphOptions<M, I>) {
const ZOOM_MIN = 0.4
const ZOOM_MAX = 2
const container = ref<HTMLDivElement>() as Ref<HTMLDivElement>
const width = ref(window.innerWidth)
const height = ref(window.innerHeight)
const nodesRefMap = shallowReactive(new Map<string, HTMLDivElement>())
const collapsedNodes = shallowReactive(new Set<string>())
const isFirstCalculateGraph = ref(true)
const childToParentMap = shallowReactive(new Map<string, string>())
const nodes = shallowRef<HierarchyNode<ModuleGraphNode<M, I>>[]>([])
const links = shallowRef<ModuleGraphLink<M, I>[]>([])
const nodesMap = shallowReactive(new Map<string, HierarchyNode<ModuleGraphNode<M, I>>>())
const linksMap = shallowReactive(new Map<string, ModuleGraphLink<M, I>>())
const modulesMap = computed(() => {
const map = new Map<string, M>()
for (const module of options.modules.value) {
map.set(module.id, module)
}
return map
})
const { control } = useMagicKeys()
const { scale, zoomIn, zoomOut } = useZoomElement(container, {
wheel: control,
minScale: ZOOM_MIN,
maxScale: ZOOM_MAX,
})
function focusOn(id: string, animated = true) {
const el = nodesRefMap.get(id)
el?.scrollIntoView({
block: 'center',
inline: 'center',
behavior: animated ? 'smooth' : 'instant',
})
}
const _calculateGraph = options.generateGraph({
isFirstCalculateGraph,
hierarchy,
tree,
nodesRefMap,
container,
modulesMap,
nodes,
links,
scale,
nodesMap,
linksMap,
width,
height,
childToParentMap,
focusOn,
collapsedNodes,
spacing: options.spacing,
})
provide(ViteDevToolsModuleGraphStateSymbol, {
spacing: options.spacing,
calculateGraph: _calculateGraph,
isFirstCalculateGraph,
container,
width,
height,
scale,
nodes,
links,
nodesRefMap,
collapsedNodes,
childToParentMap,
ZOOM_MIN,
ZOOM_MAX,
zoomIn,
zoomOut,
})
}
export function useModuleGraph() {
const state = inject(ViteDevToolsModuleGraphStateSymbol)!
return state
}
export function useToggleGraphNodeExpanded<M extends { id: string, imports: unknown[] }>(options: {
modules: MaybeRef<M[]>
}) {
const { nodesRefMap, container, calculateGraph, collapsedNodes } = inject(ViteDevToolsModuleGraphStateSymbol)!
const isGraphNodeToggling = ref(false)
function restoreScrollPosition(id: string, beforePosition: { x: number, y: number }) {
// Ensure this runs after the nextTick inside calculateGraph completes (width and height are computed)
nextTick(() => {
nextTick(() => {
const newNode = nodesRefMap.get(id)
if (newNode && beforePosition && container.value) {
const containerRect = container.value.getBoundingClientRect()
const newRect = newNode.getBoundingClientRect()
const viewportDiffX = newRect.left - containerRect.left - beforePosition.x
const viewportDiffY = newRect.top - containerRect.top - beforePosition.y
container.value.scrollLeft += viewportDiffX
container.value.scrollTop += viewportDiffY
}
})
})
}
function toggleNode(id: string) {
if (isGraphNodeToggling.value)
return
isGraphNodeToggling.value = true
const node = nodesRefMap.get(id)
let prevNodeOffset: null | { x: number, y: number } = null
// Record position relative to the scroll container to avoid drift after reflow
if (node && container.value) {
const containerRect = container.value.getBoundingClientRect()
const rect = node.getBoundingClientRect()
prevNodeOffset = {
x: rect.left - containerRect.left,
y: rect.top - containerRect.top,
}
}
if (collapsedNodes.has(id)) {
collapsedNodes.delete(id)
}
else {
collapsedNodes.add(id)
}
calculateGraph()
// Adjust scroll position after layout changes
if (prevNodeOffset) {
restoreScrollPosition(id, prevNodeOffset)
}
isGraphNodeToggling.value = false
}
function expandAll() {
if (isGraphNodeToggling.value)
return
isGraphNodeToggling.value = true
collapsedNodes.clear()
calculateGraph()
setTimeout(() => {
isGraphNodeToggling.value = false
}, 300)
}
function collapseAll() {
if (isGraphNodeToggling.value)
return
isGraphNodeToggling.value = true
unref(options.modules).forEach((module) => {
if (module.imports.length > 0) {
collapsedNodes.add(module.id)
}
})
calculateGraph()
setTimeout(() => {
isGraphNodeToggling.value = false
}, 300)
}
return {
isGraphNodeToggling,
toggleNode,
expandAll,
collapseAll,
}
}
export function useGraphZoom() {
const state = inject(ViteDevToolsModuleGraphStateSymbol)!
const { scale, zoomIn, zoomOut, ZOOM_MIN, ZOOM_MAX } = state
onKeyPressed(['-', '_'], (e) => {
if (e.ctrlKey)
zoomOut()
})
onKeyPressed(['=', '+'], (e) => {
if (e.ctrlKey)
zoomIn()
})
return {
ZOOM_MIN,
ZOOM_MAX,
scale,
zoomIn,
zoomOut,
}
}
export function useGraphDraggingScroll() {
const { container } = inject(ViteDevToolsModuleGraphStateSymbol)!
const isGrabbing = ref(false)
const SCROLLBAR_THICKNESS = 20
let x = 0
let y = 0
function init() {
useEventListener(container, 'mousedown', (e) => {
// prevent dragging when clicking on scrollbar
const rect = container.value!.getBoundingClientRect()
const distRight = rect.right - e.clientX
const distBottom = rect.bottom - e.clientY
if (distRight <= SCROLLBAR_THICKNESS || distBottom <= SCROLLBAR_THICKNESS) {
return
}
isGrabbing.value = true
x = container.value!.scrollLeft + e.pageX
y = container.value!.scrollTop + e.pageY
})
useEventListener(container, 'contextmenu', e => e.preventDefault())
useEventListener('mouseleave', () => isGrabbing.value = false)
useEventListener('mouseup', () => isGrabbing.value = false)
useEventListener('mousemove', (e) => {
if (!isGrabbing.value)
return
e.preventDefault()
container.value!.scrollLeft = x - e.pageX
container.value!.scrollTop = y - e.pageY
})
}
return {
init,
isGrabbing,
}
}
export interface ModulePathSelector {
state: { value: { search: string, selected: string | null } }
modules: ComputedRef<ModuleListItem[]>
fuse: Ref<ComputedRefWithControl<Fuse<ModuleListItem>> | undefined>
initSelector: (modules: ComputedRef<ModuleListItem[]>) => void
select: (module: ModuleListItem) => void
clear: () => void
}
export function useModulePathSelector(options: {
getModules: () => ModuleListItem[]
}): ModulePathSelector {
const state = ref<{
search: string
selected: string | null
}>({
search: '',
selected: null,
})
const fuse = ref<ComputedRefWithControl<Fuse<ModuleListItem>>>()
const modules = computed(options.getModules)
function initSelector(modules: ComputedRef<ModuleListItem[]>) {
fuse.value = computedWithControl(
modules,
() => new Fuse(modules.value, {
includeScore: true,
keys: ['id'],
ignoreLocation: true,
threshold: 0.4,
}),
)
}
function select(node: ModuleListItem) {
state.value.selected = node.id
state.value.search = ''
}
function clear() {
state.value.selected = null
}
return {
state,
modules,
fuse,
initSelector,
select,
clear,
}
}