Files
Thing/src/modules/editor/components/LinkGraph.vue
T
2026-10-10 14:29:48 +08:00

215 lines
6.7 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<script setup lang="ts">
/**
* 双链图谱(P5-4):把 `editor_link_graph` 的内部链接边画成力导向图。
*
* 布局是**一次性计算**(不跑动画):简化 Fruchterman-Reingold 迭代若干轮后
* 归一化到 viewBox,再用静态 SVG 渲染。理由是图谱打开频率低、节点规模小,
* 省掉 requestAnimationFrame 循环能让组件保持「无副作用」、易于推理。
*
* 点击节点 → 打开对应笔记。当前笔记节点高亮。
*/
import { computed, onMounted } from 'vue'
import { Loader2, Share2 } from '@lucide/vue'
import { ScrollArea } from '@/components/ui/scroll-area'
import { StateBlock } from '@/components/common'
import { useEditorStore } from '@/stores/editorStore'
const emit = defineEmits<{ open: [relPath: string] }>()
const store = useEditorStore()
/** 节点上限:超过则只画前 N 个(避免 O(n²) 迭代在大库上卡住主线程) */
const NODE_LIMIT = 400
const WIDTH = 520
const HEIGHT = 520
const PADDING = 24
const ITERATIONS = 180
interface GNode {
id: string
label: string
x: number
y: number
deg: number
}
interface GEdge {
a: number
b: number
}
const layout = computed(() => {
const index = new Map<string, number>()
const nodes: GNode[] = []
const addNode = (id: string): number => {
const found = index.get(id)
if (found !== undefined) return found
if (nodes.length >= NODE_LIMIT) return -1
const label = (id.split('/').pop() ?? id).replace(/\.md$/i, '')
index.set(id, nodes.length)
nodes.push({ id, label, x: 0, y: 0, deg: 0 })
return nodes.length - 1
}
const edges: GEdge[] = []
for (const e of store.linkGraph) {
const a = addNode(e.from)
const b = addNode(e.to)
if (a < 0 || b < 0) continue
edges.push({ a, b })
nodes[a]!.deg += 1
nodes[b]!.deg += 1
}
if (!nodes.length) return { nodes: [], edges: [], truncated: false }
const n = nodes.length
// 初始环形布局
for (let i = 0; i < n; i++) {
const angle = (i / n) * Math.PI * 2
nodes[i]!.x = Math.cos(angle) * 200
nodes[i]!.y = Math.sin(angle) * 200
}
// 简化 FR:斥力 O(n²) + 沿边引力
const k = Math.sqrt((400 * 400) / Math.max(n, 1))
const temp0 = 80
for (let it = 0; it < ITERATIONS; it++) {
const temp = temp0 * (1 - it / ITERATIONS)
const dx = new Float64Array(n)
const dy = new Float64Array(n)
for (let i = 0; i < n; i++) {
for (let j = i + 1; j < n; j++) {
let vx = nodes[i]!.x - nodes[j]!.x
let vy = nodes[i]!.y - nodes[j]!.y
let d2 = vx * vx + vy * vy
if (d2 < 0.01) {
vx = 0.1 * (i - j) || 0.1
vy = 0.1
d2 = vx * vx + vy * vy
}
const d = Math.sqrt(d2)
const f = (k * k) / d
dx[i] += (vx / d) * f
dy[i] += (vy / d) * f
dx[j] -= (vx / d) * f
dy[j] -= (vy / d) * f
}
}
for (const e of edges) {
const vx = nodes[e.a]!.x - nodes[e.b]!.x
const vy = nodes[e.a]!.y - nodes[e.b]!.y
const d = Math.max(Math.sqrt(vx * vx + vy * vy), 0.01)
const f = (d * d) / k
dx[e.a] -= (vx / d) * f
dy[e.a] -= (vy / d) * f
dx[e.b] += (vx / d) * f
dy[e.b] += (vy / d) * f
}
for (let i = 0; i < n; i++) {
const len = Math.max(Math.sqrt(dx[i]! * dx[i]! + dy[i]! * dy[i]!), 0.001)
const step = Math.min(len, temp) / len
nodes[i]!.x += dx[i]! * step
nodes[i]!.y += dy[i]! * step
}
}
// 归一化到 viewBox(等比缩放 + 居中)
let minX = Infinity
let minY = Infinity
let maxX = -Infinity
let maxY = -Infinity
for (const nd of nodes) {
minX = Math.min(minX, nd.x)
minY = Math.min(minY, nd.y)
maxX = Math.max(maxX, nd.x)
maxY = Math.max(maxY, nd.y)
}
const scale = Math.min(
(WIDTH - PADDING * 2) / Math.max(maxX - minX, 1),
(HEIGHT - PADDING * 2) / Math.max(maxY - minY, 1)
)
const ox = (WIDTH - (maxX - minX) * scale) / 2 - minX * scale
const oy = (HEIGHT - (maxY - minY) * scale) / 2 - minY * scale
return {
nodes: nodes.map(nd => ({ ...nd, x: nd.x * scale + ox, y: nd.y * scale + oy })),
edges,
truncated: store.linkGraph.length > 0 && nodes.length >= NODE_LIMIT
}
})
const radius = (deg: number): number => Math.min(3 + deg * 1.2, 9)
const showLabels = computed(() => layout.value.nodes.length <= 60)
const isCurrent = (id: string): boolean => store.currentRelPath === id
onMounted(() => {
void store.loadLinkGraph()
})
</script>
<template>
<div class="flex h-full min-h-0 flex-col">
<div class="flex h-6 shrink-0 items-center gap-1.5 border-b border-border px-2 text-[11px] text-muted-foreground">
<Share2 class="size-3" />
<span>双链图谱</span>
<span class="ml-auto tabular-nums">{{ layout.nodes.length }} 节点 · {{ layout.edges.length }} 边</span>
</div>
<div v-if="store.loadingGraph && !layout.nodes.length" class="flex items-center justify-center gap-2 p-4">
<Loader2 class="size-3.5 animate-spin text-muted-foreground" />
<span class="text-xs text-muted-foreground">正在构建图谱…</span>
</div>
<StateBlock
v-else-if="!layout.nodes.length"
variant="empty"
title="还没有双链"
description="在笔记里写 [[另一篇]] 建立链接,图谱会显示它们的关系"
:min-height="140"
/>
<ScrollArea v-else class="min-h-0 flex-1">
<div class="px-1.5 py-2">
<svg :viewBox="`0 0 ${WIDTH} ${HEIGHT}`" class="w-full select-none">
<line
v-for="(edge, i) in layout.edges"
:key="`e${i}`"
:x1="layout.nodes[edge.a]?.x"
:y1="layout.nodes[edge.a]?.y"
:x2="layout.nodes[edge.b]?.x"
:y2="layout.nodes[edge.b]?.y"
class="stroke-border"
stroke-width="1"
/>
<g
v-for="node in layout.nodes"
:key="node.id"
class="cursor-pointer"
@click="emit('open', node.id)"
>
<title>{{ node.id }}</title>
<circle
:cx="node.x"
:cy="node.y"
:r="radius(node.deg)"
:class="isCurrent(node.id) ? 'fill-primary' : 'fill-muted-foreground'"
/>
<text
v-if="showLabels"
:x="node.x"
:y="node.y - radius(node.deg) - 2"
text-anchor="middle"
class="fill-foreground"
font-size="9"
>
{{ node.label }}
</text>
</g>
</svg>
<p v-if="layout.truncated" class="px-1 pt-1 text-[11px] text-muted-foreground">
节点较多,仅显示前 {{ NODE_LIMIT }} 个。
</p>
</div>
</ScrollArea>
</div>
</template>