11<script setup lang="ts">
22import type { HierarchyLink , HierarchyNode } from ' d3-hierarchy'
3- import type { ModuleListItem , SessionContext } from ' ../../types/data'
3+ import type { ModuleImport , ModuleListItem , SessionContext } from ' ../../types/data'
44import { useEventListener } from ' @vueuse/core'
55import { hierarchy , tree } from ' d3-hierarchy'
66import { linkHorizontal , linkVertical } from ' d3-shape'
@@ -10,8 +10,15 @@ const props = defineProps<{
1010 session: SessionContext
1111 modules: ModuleListItem []
1212}>()
13- type Link = HierarchyLink <ModuleListItem > & {
13+
14+ interface Node {
15+ module: ModuleListItem
16+ import? : ModuleImport
17+ }
18+
19+ type Link = HierarchyLink <Node > & {
1420 id: string
21+ import? : ModuleImport
1522}
1623
1724const graphRender = ref <' normal' | ' mini' >(' normal' )
@@ -31,9 +38,9 @@ const height = ref(window.innerHeight)
3138const scale = ref (1 )
3239const nodesRefMap = shallowReactive (new Map <string , HTMLDivElement >())
3340
34- const nodes = shallowRef <HierarchyNode <ModuleListItem >[]>([])
41+ const nodes = shallowRef <HierarchyNode <Node >[]>([])
3542const links = shallowRef <Link []>([])
36- const nodesMap = shallowReactive (new Map <string , HierarchyNode <ModuleListItem >>())
43+ const nodesMap = shallowReactive (new Map <string , HierarchyNode <Node >>())
3744const linksMap = shallowReactive (new Map <string , Link >())
3845
3946const modulesMap = computed (() => {
@@ -62,29 +69,34 @@ function calculateGraph() {
6269 height .value = window .innerHeight
6370
6471 const seen = new Set <ModuleListItem >()
65- const root = hierarchy <ModuleListItem >(
66- { id: ' ~root' } as any ,
67- (node ) => {
68- if (node .id === ' ~root' ) {
72+ const root = hierarchy <Node >(
73+ { module: { id: ' ~root' } } as any ,
74+ (parent ) => {
75+ if (parent . module .id === ' ~root' ) {
6976 rootModules .value .forEach (x => seen .add (x ))
70- return rootModules .value
77+ return rootModules .value . map ( x => ({ module: x }))
7178 }
72- const modules = node . imports . map (( x ) => {
73- const module = modulesMap . value . get ( x . id )
74- if ( module ) {
75- if (seen . has ( module )) {
79+ const modules = parent . module . imports
80+ . map (( x ) : Node | undefined => {
81+ const module = modulesMap . value . get ( x . id )
82+ if (! module )
7683 return undefined
77- }
84+ if (seen .has (module ))
85+ return undefined
86+
7887 seen .add (module )
79- }
80- return module
81- }).filter (x => x !== undefined )
88+ return {
89+ module ,
90+ import: x ,
91+ }
92+ })
93+ .filter (x => x !== undefined )
8294 return modules
8395 },
8496 )
8597
8698 // Calculate the layout
87- const layout = tree <ModuleListItem >()
99+ const layout = tree <Node >()
88100 .nodeSize ([SPACING .height , SPACING .width + SPACING .gap ])
89101 layout (root )
90102
@@ -111,14 +123,15 @@ function calculateGraph() {
111123 nodes .value = _nodes
112124 nodesMap .clear ()
113125 for (const node of _nodes ) {
114- nodesMap .set (node .data .id , node )
126+ nodesMap .set (node .data .module . id , node )
115127 }
116128 const _links = root .links ()
117- .filter (x => x .source .data .id !== ' ~root' )
118- .map ((x ) => {
129+ .filter (x => x .source .data .module . id !== ' ~root' )
130+ .map ((x ): Link => {
119131 return {
120132 ... x ,
121- id: ` ${x .source .data .id }|${x .target .data .id } ` ,
133+ import: x .source .data .import ,
134+ id: ` ${x .source .data .module .id }|${x .target .data .module .id } ` ,
122135 }
123136 })
124137 linksMap .clear ()
@@ -221,6 +234,7 @@ onMounted(() => {
221234 :key =" link.id"
222235 :d =" generateLink(link)!"
223236 :class =" getLinkColor(link)"
237+ :stroke-dasharray =" link.import?.kind === 'dynamic-import' ? '3 6' : undefined"
224238 fill =" none"
225239 />
226240 </g >
@@ -238,17 +252,17 @@ onMounted(() => {
238252 </svg> -->
239253 <template
240254 v-for =" node of nodes "
241- :key =" node .data .id "
255+ :key =" node .data .module . id "
242256 >
243- <template v-if =" node .data .id !== ' ~root' " >
257+ <template v-if =" node .data .module . id !== ' ~root' " >
244258 <DisplayModuleId
245- :id =" node.data.id"
246- :ref =" (el: any) => nodesRefMap.set(node.data.id, el?.$el)"
259+ :id =" node.data.module. id"
260+ :ref =" (el: any) => nodesRefMap.set(node.data.module. id, el?.$el)"
247261 absolute hover =" bg-active" block px2 p1 bg-glass z-graph-node
248262 border =" ~ base rounded"
249263 :link =" true"
250264 :session =" session"
251- :pkg =" node.data"
265+ :pkg =" node.data.module "
252266 :minimal =" true"
253267 :style =" {
254268 left: `${node.x}px`,
0 commit comments