|
| 1 | +<script setup lang="ts"> |
| 2 | +import type { Asset as AssetInfo } from '@rolldown/debug' |
| 3 | +import type { HierarchyLink, HierarchyNode } from 'd3-hierarchy' |
| 4 | +import { linkHorizontal, linkVertical } from 'd3-shape' |
| 5 | +import { computed, onMounted, shallowRef, useTemplateRef, watch } from 'vue' |
| 6 | +
|
| 7 | +const props = defineProps<{ |
| 8 | + importers?: AssetInfo[] |
| 9 | + imports?: AssetInfo[] |
| 10 | +}>() |
| 11 | +
|
| 12 | +interface Node { |
| 13 | + asset: AssetInfo |
| 14 | +} |
| 15 | +
|
| 16 | +type Link = HierarchyLink<Node> & { |
| 17 | + id: string |
| 18 | +} |
| 19 | +
|
| 20 | +type LinkPoint = 'importer-start' | 'importer-end' | 'import-start' | 'import-end' |
| 21 | +
|
| 22 | +const MAX_LINKS = 20 |
| 23 | +const SPACING = { |
| 24 | + width: 400, |
| 25 | + height: 50, |
| 26 | + padding: 4, |
| 27 | + marginX: 8, |
| 28 | + border: 1, |
| 29 | + margin: 8, |
| 30 | + dot: 16, |
| 31 | + dotOffset: 80, |
| 32 | +} |
| 33 | +
|
| 34 | +const container = useTemplateRef<HTMLDivElement>('container') |
| 35 | +const links = shallowRef<Link[]>([]) |
| 36 | +
|
| 37 | +const normalizedMaxLinks = computed(() => { |
| 38 | + return Math.min(Math.max(props.importers?.length || 0, props.imports?.length || 0), MAX_LINKS) |
| 39 | +}) |
| 40 | +
|
| 41 | +const importersMaxLength = computed(() => Math.min(props.importers?.length || 0, MAX_LINKS)) |
| 42 | +const importsMaxLength = computed(() => Math.min(props.imports?.length || 0, MAX_LINKS)) |
| 43 | +const nodesHeight = computed(() => SPACING.height * normalizedMaxLinks.value + SPACING.padding * (normalizedMaxLinks.value + 1) + SPACING.border * 2) |
| 44 | +
|
| 45 | +const importersVerticalOffset = computed(() => { |
| 46 | + const diff = Math.max(0, importsMaxLength.value - importersMaxLength.value) |
| 47 | + const offset = (diff * (SPACING.height + SPACING.padding)) / 2 |
| 48 | + return Math.min(offset, nodesHeight.value / 2) |
| 49 | +}) |
| 50 | +
|
| 51 | +const importsVerticalOffset = computed(() => { |
| 52 | + const diff = Math.max(0, importersMaxLength.value - importsMaxLength.value) |
| 53 | + const offset = (diff * (SPACING.height + SPACING.padding)) / 2 |
| 54 | + return Math.min(offset, nodesHeight.value / 2) |
| 55 | +}) |
| 56 | +
|
| 57 | +const createLinkHorizontal = linkHorizontal() |
| 58 | + .x(d => d[0]) |
| 59 | + .y(d => d[1]) |
| 60 | +
|
| 61 | +const createLinkVertical = linkVertical() |
| 62 | + .x(d => d[0]) |
| 63 | + .y(d => d[1]) |
| 64 | +
|
| 65 | +function generateLink(link: Link) { |
| 66 | + if (link.target.x! <= link.source.x!) { |
| 67 | + return createLinkVertical({ |
| 68 | + source: [link.source.x!, link.source.y!], |
| 69 | + target: [link.target.x!, link.target.y!], |
| 70 | + }) |
| 71 | + } |
| 72 | + return createLinkHorizontal({ |
| 73 | + source: [link.source.x!, link.source.y!], |
| 74 | + target: [link.target.x!, link.target.y!], |
| 75 | + }) |
| 76 | +} |
| 77 | +
|
| 78 | +function getLinkColor(_link: Link) { |
| 79 | + return 'stroke-#8882' |
| 80 | +} |
| 81 | +
|
| 82 | +const dotNodeMargin = computed(() => `${nodesHeight.value / 2 - SPACING.dot / 2}px ${SPACING.dotOffset}px 0 ${props.importers?.length ? SPACING.dotOffset : 0}px`) |
| 83 | +const linkStartX = computed(() => props.importers?.length ? SPACING.width + SPACING.marginX : SPACING.marginX) |
| 84 | +const dotStartX = computed(() => props.importers?.length ? linkStartX.value + SPACING.dotOffset : linkStartX.value) |
| 85 | +const dotStartY = computed(() => (SPACING.height * normalizedMaxLinks.value + ((normalizedMaxLinks.value + 1) * SPACING.padding)) / 2) |
| 86 | +
|
| 87 | +function calculateLinkX(type: LinkPoint) { |
| 88 | + switch (type) { |
| 89 | + case 'importer-start': |
| 90 | + return linkStartX.value |
| 91 | + case 'importer-end': |
| 92 | + return dotStartX.value |
| 93 | + case 'import-start': |
| 94 | + return dotStartX.value + SPACING.dot |
| 95 | + case 'import-end': |
| 96 | + return props.importers?.length ? linkStartX.value + SPACING.dotOffset * 2 + SPACING.dot : linkStartX.value + SPACING.dotOffset + SPACING.dot |
| 97 | + } |
| 98 | +} |
| 99 | +
|
| 100 | +function calculateLinkY(type: LinkPoint, i?: number) { |
| 101 | + switch (type) { |
| 102 | + case 'importer-start': |
| 103 | + return ((SPACING.height + SPACING.padding) * i!) + (SPACING.height / 2 + SPACING.padding) + importersVerticalOffset.value |
| 104 | + case 'import-end': |
| 105 | + return ((SPACING.height + SPACING.padding) * i!) + (SPACING.height / 2 + SPACING.padding) + importsVerticalOffset.value |
| 106 | + case 'importer-end': |
| 107 | + case 'import-start': |
| 108 | + return dotStartY.value |
| 109 | + } |
| 110 | +} |
| 111 | +
|
| 112 | +function generateLinks() { |
| 113 | + links.value = [] |
| 114 | +
|
| 115 | + // importers (left -> current asset) |
| 116 | + if (props.importers?.length) { |
| 117 | + const _importersLinks = Array.from({ length: importersMaxLength.value }, (_, i) => { |
| 118 | + return { |
| 119 | + id: `importer-${i}`, |
| 120 | + source: { |
| 121 | + x: calculateLinkX('importer-start'), |
| 122 | + y: calculateLinkY('importer-start', i), |
| 123 | + } as HierarchyNode<Node>, |
| 124 | + target: { |
| 125 | + x: calculateLinkX('importer-end'), |
| 126 | + y: calculateLinkY('importer-end'), |
| 127 | + } as HierarchyNode<Node>, |
| 128 | + } |
| 129 | + }) |
| 130 | + links.value.push(..._importersLinks) |
| 131 | + } |
| 132 | +
|
| 133 | + // imports (current asset -> right) |
| 134 | + if (props.imports?.length) { |
| 135 | + const _importsLinks = Array.from({ length: importsMaxLength.value }, (_, i) => { |
| 136 | + return { |
| 137 | + id: `import-${i}`, |
| 138 | + source: { |
| 139 | + x: calculateLinkX('import-start'), |
| 140 | + y: calculateLinkY('import-start'), |
| 141 | + } as HierarchyNode<Node>, |
| 142 | + target: { |
| 143 | + x: calculateLinkX('import-end'), |
| 144 | + y: calculateLinkY('import-end', i), |
| 145 | + } as HierarchyNode<Node>, |
| 146 | + } |
| 147 | + }) |
| 148 | + links.value.push(..._importsLinks) |
| 149 | + } |
| 150 | +} |
| 151 | +
|
| 152 | +onMounted(() => { |
| 153 | + watch( |
| 154 | + () => [props.importers, props.imports], |
| 155 | + generateLinks, |
| 156 | + { immediate: true }, |
| 157 | + ) |
| 158 | +}) |
| 159 | +</script> |
| 160 | + |
| 161 | +<template> |
| 162 | + <div |
| 163 | + v-if="importers?.length || imports?.length" |
| 164 | + ref="container" |
| 165 | + w-full relative select-none |
| 166 | + > |
| 167 | + <!-- nodes --> |
| 168 | + <div flex px2> |
| 169 | + <!-- importers --> |
| 170 | + <div |
| 171 | + v-if="importers?.length" |
| 172 | + py1 |
| 173 | + :style="{ |
| 174 | + width: `${SPACING.width}px`, |
| 175 | + marginTop: `${importersVerticalOffset}px`, |
| 176 | + }" |
| 177 | + > |
| 178 | + <template v-for="(importer, i) of importers" :key="importer.filename"> |
| 179 | + <NuxtLink |
| 180 | + :to="{ query: { asset: importer.filename } }" |
| 181 | + hover="bg-active" block px2 p1 bg-base |
| 182 | + z-graph-node |
| 183 | + border="~ base rounded" |
| 184 | + font-mono text-sm |
| 185 | + :style="{ |
| 186 | + width: `${SPACING.width}px`, |
| 187 | + height: `${SPACING.height}px`, |
| 188 | + overflow: 'hidden', |
| 189 | + marginBottom: `${i === importers!.length - 1 ? 0 : SPACING.padding}px`, |
| 190 | + display: 'flex', |
| 191 | + alignItems: 'center', |
| 192 | + gap: '0.25rem', |
| 193 | + }" |
| 194 | + > |
| 195 | + <DisplayFileIcon :filename="importer.filename" /> |
| 196 | + <span overflow-hidden text-ellipsis> |
| 197 | + {{ importer.filename }} |
| 198 | + </span> |
| 199 | + </NuxtLink> |
| 200 | + </template> |
| 201 | + </div> |
| 202 | + |
| 203 | + <!-- dot: current asset --> |
| 204 | + <div |
| 205 | + bg-base rounded-full border-3 font-mono border-active :style="{ |
| 206 | + margin: dotNodeMargin, |
| 207 | + width: `${SPACING.dot}px`, |
| 208 | + height: `${SPACING.dot}px`, |
| 209 | + }" |
| 210 | + /> |
| 211 | + |
| 212 | + <!-- imports --> |
| 213 | + <div |
| 214 | + v-if="imports?.length" |
| 215 | + py1 |
| 216 | + :style="{ |
| 217 | + width: `${SPACING.width}px`, |
| 218 | + marginTop: `${importsVerticalOffset}px`, |
| 219 | + }" |
| 220 | + > |
| 221 | + <template v-for="(_import, i) of imports" :key="_import.filename"> |
| 222 | + <NuxtLink |
| 223 | + :to="{ query: { asset: _import.filename } }" |
| 224 | + hover="bg-active" block px2 p1 bg-base |
| 225 | + z-graph-node |
| 226 | + border="~ base rounded" |
| 227 | + font-mono text-sm |
| 228 | + :style="{ |
| 229 | + width: `${SPACING.width}px`, |
| 230 | + height: `${SPACING.height}px`, |
| 231 | + overflow: 'hidden', |
| 232 | + marginBottom: `${i === imports!.length - 1 ? 0 : SPACING.padding}px`, |
| 233 | + display: 'flex', |
| 234 | + alignItems: 'center', |
| 235 | + gap: '0.25rem', |
| 236 | + }" |
| 237 | + > |
| 238 | + <DisplayFileIcon :filename="_import.filename" /> |
| 239 | + <span overflow-hidden text-ellipsis> |
| 240 | + {{ _import.filename }} |
| 241 | + </span> |
| 242 | + </NuxtLink> |
| 243 | + </template> |
| 244 | + </div> |
| 245 | + </div> |
| 246 | + |
| 247 | + <!-- links --> |
| 248 | + <svg |
| 249 | + pointer-events-none absolute left-0 top-0 z-graph-link w-full |
| 250 | + :style="{ |
| 251 | + height: `${nodesHeight}px`, |
| 252 | + }" |
| 253 | + > |
| 254 | + <g> |
| 255 | + <path |
| 256 | + v-for="link of links" |
| 257 | + :key="link.id" |
| 258 | + :d="generateLink(link)!" |
| 259 | + :class="getLinkColor(link)" |
| 260 | + fill="none" |
| 261 | + /> |
| 262 | + </g> |
| 263 | + </svg> |
| 264 | + </div> |
| 265 | +</template> |
0 commit comments