Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2eb2a37

Browse files
runyasakantfu
andauthored
feat: component graph node name toogle (#797)
Co-authored-by: Anthony Fu <[email protected]>
1 parent 15e8926 commit 2eb2a37

File tree

1 file changed

+24
-8
lines changed

1 file changed

+24
-8
lines changed

packages/devtools/client/components/ComponentsGraph.vue

+24-8
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
<script setup lang="ts">
22
import type { Component, NuxtLayout, NuxtPage } from 'nuxt/schema'
3-
import type { Data, Node, Options } from 'vis-network'
3+
import type { Data, DataSetNodes, Node, Options } from 'vis-network'
44
import type { ComponentRelationship } from '~/../src/types'
55
import { useDebounce } from '@vueuse/core'
6+
import { DataSet } from 'vis-data'
67
import { Network } from 'vis-network'
78
import { computed, onMounted, ref, shallowRef, watch } from 'vue'
89
import { getColorMode } from '~/composables/client'
@@ -67,7 +68,6 @@ const data = computed<Data>(() => {
6768
const page = pages.value?.find(i => i.file === rel.id)
6869
const layout = layouts.value?.find(i => i.file === rel.id)
6970
70-
const path = rel.id.replace(/\?.*$/, '').replace(/#.*$/, '')
7171
const group = rel.id.includes('/node_modules/')
7272
? 'lib'
7373
: component
@@ -101,7 +101,7 @@ const data = computed<Data>(() => {
101101
102102
return {
103103
id: rel.id,
104-
label: path.split('/').splice(-1)[0].replace(/\.\w+$/, ''),
104+
label: getComponentName(rel.id),
105105
group,
106106
shape,
107107
size: 15 + Math.min(rel.deps.length / 2, 8),
@@ -132,7 +132,7 @@ const data = computed<Data>(() => {
132132
})))
133133
134134
return {
135-
nodes,
135+
nodes: new DataSet(nodes),
136136
edges,
137137
}
138138
})
@@ -193,16 +193,32 @@ onMounted(() => {
193193
194194
network.on('click', (e) => {
195195
const id = e.nodes?.[0]
196-
const node = (data.value.nodes as any[])?.find(i => i.id === id)?.extra
197-
if (node)
198-
selected.value = node
196+
const node = ((data.value.nodes as DataSetNodes).get(id) as any).extra
197+
selected.value = node
199198
})
200199
201200
watch(data, () => {
202201
network.setData(data.value)
203202
})
204203
})
205204
205+
function onCloseDrawer() {
206+
selected.value = undefined
207+
}
208+
209+
function getComponentName(path: string) {
210+
const component = props.components.find(i => i.filePath === path)
211+
if (component)
212+
return component.pascalName
213+
return path
214+
.replace(/.*\/components\//, '')
215+
.replace(/\.vue$/, '')
216+
.replace(/\/index$/, '')
217+
.split('/')
218+
.map(part => part.charAt(0).toUpperCase() + part.slice(1))
219+
.join('')
220+
}
221+
206222
function setFilter() {
207223
selectedFilter.value = selected.value?.relationship
208224
selected.value = undefined
@@ -273,7 +289,7 @@ function setFilter() {
273289
:model-value="!!(selected && selected.component)"
274290
:top="navbar"
275291
border="t l base" w-80
276-
@close="selected = undefined"
292+
@close="onCloseDrawer"
277293
>
278294
<div v-if="selected && selected.component" py4 pt4 flex="~ col">
279295
<ComponentDetails

0 commit comments

Comments
 (0)