feat: Enhance graph interaction and visualization in WikiBrowser component

- Added a search overlay for graph nodes, allowing users to filter and select nodes more efficiently.
- Introduced a legend overlay to clarify node types and provide toggle functionality for arrow visibility on graph edges.
- Improved SVG rendering for graph elements, including enhanced arrow designs and added drop shadow effects for better visual distinction.
- Updated state management to support new graph interaction features, enhancing user experience and interactivity.

These changes collectively improve the usability and visual appeal of the graph interface in the WikiBrowser component.
This commit is contained in:
wizardchen
2026-04-08 19:59:38 +08:00
parent 3e651cfa61
commit f1c9876461
+369 -104
View File
@@ -4,6 +4,54 @@
<template v-if="view === 'graph'"> <template v-if="view === 'graph'">
<div class="wiki-graph"> <div class="wiki-graph">
<div ref="graphRef" class="wiki-graph-canvas"></div> <div ref="graphRef" class="wiki-graph-canvas"></div>
<!-- Graph Search Overlay -->
<div v-if="graphReady" class="wiki-graph-search">
<t-select
v-model="graphSearchValue"
filterable
:options="graphSearchOptions"
:placeholder="$t('knowledgeEditor.wikiBrowser.searchPlaceholder')"
@change="handleGraphSearchSelect"
@enter="handleGraphSearchEnter"
:popup-props="{ zIndex: 100 }"
class="graph-search-select"
>
<template #prefixIcon><t-icon name="search" /></template>
</t-select>
</div>
<!-- Legend Overlay -->
<div v-if="graphReady" class="wiki-graph-legend">
<div class="legend-items">
<div class="legend-item">
<span class="legend-dot" style="background: #0052d9"></span>
{{ $t('knowledgeEditor.wikiBrowser.filterSummary') }}
</div>
<div class="legend-item">
<span class="legend-dot" style="background: #2ba471"></span>
{{ $t('knowledgeEditor.wikiBrowser.filterEntity') }}
</div>
<div class="legend-item">
<span class="legend-dot" style="background: #e37318"></span>
{{ $t('knowledgeEditor.wikiBrowser.filterConcept') }}
</div>
<div class="legend-item">
<span class="legend-dot" style="background: #0594fa"></span>
{{ $t('knowledgeEditor.wikiBrowser.filterSynthesis') }}
</div>
<div class="legend-item">
<span class="legend-dot" style="background: #d54941"></span>
{{ $t('knowledgeEditor.wikiBrowser.filterComparison') }}
</div>
</div>
<div class="legend-divider"></div>
<div class="legend-action" @click="toggleArrows" :class="{ active: showArrows }">
<span class="legend-action-icon">→</span>
<span>{{ showArrows ? $t('knowledgeEditor.wikiBrowser.hideArrows') : $t('knowledgeEditor.wikiBrowser.showArrows') }}</span>
</div>
</div>
<div v-if="!graphReady" class="wiki-reader-empty"> <div v-if="!graphReady" class="wiki-reader-empty">
<t-loading v-if="graphLoading" /> <t-loading v-if="graphLoading" />
<div v-else class="wiki-empty-icon"> <div v-else class="wiki-empty-icon">
@@ -20,6 +68,9 @@
:footer="false" :footer="false"
placement="right" placement="right"
:attach="false" :attach="false"
:show-overlay="false"
:close-btn="true"
class="wiki-graph-drawer"
> >
<template v-if="graphDrawerPage"> <template v-if="graphDrawerPage">
<div class="wiki-reader-meta" style="margin-bottom: 16px;"> <div class="wiki-reader-meta" style="margin-bottom: 16px;">
@@ -221,12 +272,12 @@ const selectedPage = ref<WikiPage | null>(null)
const stats = ref<WikiStats | null>(null) const stats = ref<WikiStats | null>(null)
const graphData = ref<WikiGraphData | null>(null) const graphData = ref<WikiGraphData | null>(null)
const searchQuery = ref('') const searchQuery = ref('')
const graphSearchValue = ref('')
const graphRef = ref<HTMLElement | null>(null) const graphRef = ref<HTMLElement | null>(null)
const loading = ref(false) const loading = ref(false)
const graphLoading = ref(false) const graphLoading = ref(false)
const graphReady = ref(false) const graphReady = ref(false)
const showArrows = ref(true) const showArrows = ref(true)
const graphEdgeEls = ref<{ line: SVGLineElement; bidir: boolean }[]>([])
const collapsedGroups = reactive<Record<string, boolean>>({}) const collapsedGroups = reactive<Record<string, boolean>>({})
const graphDrawerVisible = ref(false) const graphDrawerVisible = ref(false)
const graphDrawerPage = ref<WikiPage | null>(null) const graphDrawerPage = ref<WikiPage | null>(null)
@@ -317,7 +368,7 @@ function handleGraphDrawerClick(e: MouseEvent) {
if (target.classList.contains('wiki-content-link')) { if (target.classList.contains('wiki-content-link')) {
e.preventDefault() e.preventDefault()
const slug = target.getAttribute('data-slug') const slug = target.getAttribute('data-slug')
if (slug) openGraphDrawer(slug) if (slug) handleGraphSearchSelect(slug)
} }
} }
@@ -441,7 +492,7 @@ async function doSearch() {
function toggleArrows() { function toggleArrows() {
showArrows.value = !showArrows.value showArrows.value = !showArrows.value
for (const e of graphEdgeEls.value) { for (const e of graphEdgeElsRef) {
if (showArrows.value) { if (showArrows.value) {
e.line.setAttribute('marker-end', 'url(#arrow-end)') e.line.setAttribute('marker-end', 'url(#arrow-end)')
if (e.bidir) e.line.setAttribute('marker-start', 'url(#arrow-start)') if (e.bidir) e.line.setAttribute('marker-start', 'url(#arrow-start)')
@@ -483,7 +534,17 @@ let graphNodes: GNode[] = []
let graphSvg: SVGSVGElement | null = null let graphSvg: SVGSVGElement | null = null
let graphAnimFrame = 0 let graphAnimFrame = 0
// Used for graph search centering interaction
let graphPanZoomRef: {
setScale: (s: number) => void,
setTranslate: (x: number, y: number) => void,
apply: () => void,
flyTo: (x: number, y: number, s?: number, duration?: number) => void,
getScale: () => number
} | null = null
const graphHighlightSlug = ref<string | null>(null) const graphHighlightSlug = ref<string | null>(null)
const graphSelectedSlug = ref<string | null>(null)
// Color map for node types // Color map for node types
const nodeColorMap: Record<string, string> = { const nodeColorMap: Record<string, string> = {
@@ -565,7 +626,7 @@ function renderGraph() {
markerEnd.setAttribute('markerHeight', '6') markerEnd.setAttribute('markerHeight', '6')
markerEnd.setAttribute('orient', 'auto') markerEnd.setAttribute('orient', 'auto')
const arrowPath = document.createElementNS('http://www.w3.org/2000/svg', 'path') const arrowPath = document.createElementNS('http://www.w3.org/2000/svg', 'path')
arrowPath.setAttribute('d', 'M0,0 L10,3 L0,6') arrowPath.setAttribute('d', 'M0,0 L10,3 L0,6 L2,3 Z')
arrowPath.setAttribute('fill', '#c0c4cc') arrowPath.setAttribute('fill', '#c0c4cc')
markerEnd.appendChild(arrowPath) markerEnd.appendChild(arrowPath)
defs.appendChild(markerEnd) defs.appendChild(markerEnd)
@@ -580,7 +641,7 @@ function renderGraph() {
markerStart.setAttribute('markerHeight', '6') markerStart.setAttribute('markerHeight', '6')
markerStart.setAttribute('orient', 'auto') markerStart.setAttribute('orient', 'auto')
const arrowPathStart = document.createElementNS('http://www.w3.org/2000/svg', 'path') const arrowPathStart = document.createElementNS('http://www.w3.org/2000/svg', 'path')
arrowPathStart.setAttribute('d', 'M10,0 L0,3 L10,6') arrowPathStart.setAttribute('d', 'M10,0 L0,3 L10,6 L8,3 Z')
arrowPathStart.setAttribute('fill', '#c0c4cc') arrowPathStart.setAttribute('fill', '#c0c4cc')
markerStart.appendChild(arrowPathStart) markerStart.appendChild(arrowPathStart)
defs.appendChild(markerStart) defs.appendChild(markerStart)
@@ -596,12 +657,22 @@ function renderGraph() {
m.setAttribute('markerHeight', '6') m.setAttribute('markerHeight', '6')
m.setAttribute('orient', 'auto') m.setAttribute('orient', 'auto')
const p = document.createElementNS('http://www.w3.org/2000/svg', 'path') const p = document.createElementNS('http://www.w3.org/2000/svg', 'path')
p.setAttribute('d', id.includes('end') ? 'M0,0 L10,3 L0,6' : 'M10,0 L0,3 L10,6') p.setAttribute('d', id.includes('end') ? 'M0,0 L10,3 L0,6 L2,3 Z' : 'M10,0 L0,3 L10,6 L8,3 Z')
p.setAttribute('fill', '#0052d9') p.setAttribute('fill', '#0052d9')
m.appendChild(p) m.appendChild(p)
defs.appendChild(m) defs.appendChild(m)
} }
// Drop shadow filter for nodes
const filter = document.createElementNS('http://www.w3.org/2000/svg', 'filter')
filter.setAttribute('id', 'node-shadow')
filter.setAttribute('x', '-20%')
filter.setAttribute('y', '-20%')
filter.setAttribute('width', '140%')
filter.setAttribute('height', '140%')
filter.innerHTML = `<feDropShadow dx="0" dy="2" stdDeviation="3" flood-color="#000" flood-opacity="0.15"/>`
defs.appendChild(filter)
svg.appendChild(defs) svg.appendChild(defs)
// Detect bidirectional edges (A→B and B→A both exist) // Detect bidirectional edges (A→B and B→A both exist)
@@ -627,6 +698,7 @@ function renderGraph() {
line.setAttribute('stroke-width', '1.2') line.setAttribute('stroke-width', '1.2')
line.setAttribute('stroke-opacity', '0.4') line.setAttribute('stroke-opacity', '0.4')
line.setAttribute('marker-end', 'url(#arrow-end)') line.setAttribute('marker-end', 'url(#arrow-end)')
line.style.transition = 'stroke 0.2s, stroke-width 0.2s, stroke-opacity 0.2s'
if (bidir) { if (bidir) {
line.setAttribute('marker-start', 'url(#arrow-start)') line.setAttribute('marker-start', 'url(#arrow-start)')
} }
@@ -635,42 +707,92 @@ function renderGraph() {
} }
// Create SVG elements for nodes // Create SVG elements for nodes
const nodeEls: { g: SVGGElement; circle: SVGCircleElement; text: SVGTextElement; node: GNode }[] = [] const nodeEls: { g: SVGGElement; circle: SVGCircleElement; text: SVGTextElement; activeRing: SVGCircleElement; node: GNode }[] = []
for (const n of graphNodes) { for (const n of graphNodes) {
const g = document.createElementNS('http://www.w3.org/2000/svg', 'g') const g = document.createElementNS('http://www.w3.org/2000/svg', 'g')
g.style.cursor = 'pointer' g.style.cursor = 'pointer'
const circle = document.createElementNS('http://www.w3.org/2000/svg', 'circle')
const r = nodeRadius(n) const r = nodeRadius(n)
// Pulse ring for selected state
const activeRing = document.createElementNS('http://www.w3.org/2000/svg', 'circle')
activeRing.setAttribute('r', String(r + 5))
activeRing.setAttribute('fill', 'none')
activeRing.setAttribute('stroke', nodeColorMap[n.type] || '#8c8c8c')
activeRing.setAttribute('stroke-width', '2')
activeRing.style.opacity = '0'
activeRing.style.transition = 'opacity 0.2s'
activeRing.classList.add('node-active-ring')
g.appendChild(activeRing)
const circle = document.createElementNS('http://www.w3.org/2000/svg', 'circle')
circle.setAttribute('r', String(r)) circle.setAttribute('r', String(r))
circle.setAttribute('fill', nodeColorMap[n.type] || '#8c8c8c') circle.setAttribute('fill', nodeColorMap[n.type] || '#8c8c8c')
circle.setAttribute('stroke', '#fff') circle.setAttribute('stroke', '#fff')
circle.setAttribute('stroke-width', '2') circle.setAttribute('stroke-width', '2')
circle.setAttribute('filter', 'url(#node-shadow)')
circle.style.transition = 'r 0.2s, stroke-width 0.2s, opacity 0.2s' circle.style.transition = 'r 0.2s, stroke-width 0.2s, opacity 0.2s'
g.appendChild(circle) g.appendChild(circle)
// Text label wrapper for better readability
const textBg = document.createElementNS('http://www.w3.org/2000/svg', 'rect')
g.appendChild(textBg) // we'll size this after we know text size
const text = document.createElementNS('http://www.w3.org/2000/svg', 'text') const text = document.createElementNS('http://www.w3.org/2000/svg', 'text')
text.setAttribute('text-anchor', 'middle') text.setAttribute('text-anchor', 'middle')
text.setAttribute('dy', String(r + 14)) text.setAttribute('dy', String(r + 14))
text.setAttribute('font-size', '11') text.setAttribute('font-size', '11')
text.setAttribute('fill', 'var(--td-text-color-secondary)') text.setAttribute('fill', 'var(--td-text-color-secondary)')
text.setAttribute('pointer-events', 'none') text.setAttribute('pointer-events', 'none')
text.style.textShadow = '0 1px 3px var(--td-bg-color-container), 0 -1px 3px var(--td-bg-color-container), 1px 0 3px var(--td-bg-color-container), -1px 0 3px var(--td-bg-color-container)'
text.textContent = n.title.length > 14 ? n.title.substring(0, 14) + '…' : n.title text.textContent = n.title.length > 14 ? n.title.substring(0, 14) + '…' : n.title
g.appendChild(text) g.appendChild(text)
// Hover highlight // Hover highlight
g.addEventListener('mouseenter', () => { g.addEventListener('mouseenter', () => {
graphHighlightSlug.value = n.slug // Don't change highlight on hover if we have a selected node
applyHighlight(n.slug, adjacency, nodeEls, edgeEls) if (!graphSelectedSlug.value) {
graphHighlightSlug.value = n.slug
applyHighlight(n.slug, adjacency, nodeEls, edgeEls)
} else if (graphSelectedSlug.value !== n.slug) {
// If a node is selected, hover over others should just add to highlight
// but we'll keep the selected one as the main focus
applyHighlight(graphSelectedSlug.value, adjacency, nodeEls, edgeEls, n.slug)
}
}) })
g.addEventListener('mouseleave', () => { g.addEventListener('mouseleave', () => {
graphHighlightSlug.value = null // Only clear if we were just highlighting on hover, not selected
clearHighlight(nodeEls, edgeEls) if (!graphSelectedSlug.value) {
graphHighlightSlug.value = null
clearHighlight(nodeEls, edgeEls)
} else {
// Restore pure selected state
applyHighlight(graphSelectedSlug.value, adjacency, nodeEls, edgeEls)
}
}) })
// Click to open drawer // Click to select & open drawer directly
g.addEventListener('click', (e) => { g.addEventListener('click', (e) => {
e.stopPropagation() e.stopPropagation()
// Select and highlight
graphSelectedSlug.value = n.slug
applyHighlight(n.slug, adjacency, nodeEls, edgeEls)
// Auto pan to center the node, shifted left for drawer
if (graphPanZoomRef) {
const container = graphRef.value
if (container) {
const width = container.clientWidth
const height = container.clientHeight
graphPanZoomRef.flyTo(
width / 2 - n.x * graphPanZoomRef.getScale() - 240,
height / 2 - n.y * graphPanZoomRef.getScale()
)
}
}
// Open drawer (it will handle drawer visibility and fetching content)
openGraphDrawer(n.slug) openGraphDrawer(n.slug)
}) })
@@ -678,7 +800,7 @@ function renderGraph() {
setupDrag(g, n, nodeMap, edgeEls, nodeEls, nodeRadius) setupDrag(g, n, nodeMap, edgeEls, nodeEls, nodeRadius)
nodeG.appendChild(g) nodeG.appendChild(g)
nodeEls.push({ g, circle, text, node: n }) nodeEls.push({ g, circle, text, activeRing, node: n })
} }
// Pan & zoom on SVG background // Pan & zoom on SVG background
@@ -763,11 +885,10 @@ function renderGraph() {
} }
} }
// Draw legend // Store node and edge refs for search and arrow toggle
drawLegend(svg, width) graphNodeElsRef = nodeEls
graphEdgeElsRef = edgeEls.map(e => ({ line: e.line, source: e.source, target: e.target, bidir: e.bidir }))
// Store edge refs for arrow toggle graphAdjacencyRef = adjacency
graphEdgeEls.value = edgeEls.map(e => ({ line: e.line, bidir: e.bidir }))
graphAnimFrame = requestAnimationFrame(tick) graphAnimFrame = requestAnimationFrame(tick)
graphReady.value = true graphReady.value = true
@@ -796,7 +917,7 @@ function setupDrag(
g: SVGGElement, node: GNode, g: SVGGElement, node: GNode,
nodeMap: Map<string, GNode>, nodeMap: Map<string, GNode>,
edgeEls: { line: SVGLineElement; source: string; target: string; bidir: boolean }[], edgeEls: { line: SVGLineElement; source: string; target: string; bidir: boolean }[],
nodeEls: { g: SVGGElement; circle: SVGCircleElement; text: SVGTextElement; node: GNode }[], nodeEls: { g: SVGGElement; circle: SVGCircleElement; text: SVGTextElement; activeRing: SVGCircleElement; node: GNode }[],
nodeRadius: (n: GNode) => number, nodeRadius: (n: GNode) => number,
) { ) {
let dragging = false let dragging = false
@@ -865,11 +986,38 @@ function setupPanZoom(svg: SVGSVGElement, rootG: SVGGElement) {
let translateX = 0, translateY = 0 let translateX = 0, translateY = 0
let panning = false let panning = false
let panStartX = 0, panStartY = 0 let panStartX = 0, panStartY = 0
let dragStartX = 0, dragStartY = 0
function applyTransform() { function applyTransform() {
rootG.setAttribute('transform', `translate(${translateX},${translateY}) scale(${scale})`) rootG.setAttribute('transform', `translate(${translateX},${translateY}) scale(${scale})`)
} }
// Export methods for programmatic pan/zoom
let animId = 0
graphPanZoomRef = {
setScale: (s: number) => { scale = s },
setTranslate: (x: number, y: number) => { translateX = x; translateY = y },
apply: applyTransform,
getScale: () => scale,
flyTo: (tx: number, ty: number, s?: number, duration = 400) => {
cancelAnimationFrame(animId)
const startX = translateX, startY = translateY, startScale = scale
const targetScale = s || scale
const startTime = performance.now()
const animate = (time: number) => {
let t = (time - startTime) / duration
if (t > 1) t = 1
const ease = 1 - Math.pow(1 - t, 3) // cubic ease out
translateX = startX + (tx - startX) * ease
translateY = startY + (ty - startY) * ease
scale = startScale + (targetScale - startScale) * ease
applyTransform()
if (t < 1) animId = requestAnimationFrame(animate)
}
animId = requestAnimationFrame(animate)
}
}
// Zoom with mouse wheel // Zoom with mouse wheel
svg.addEventListener('wheel', (e) => { svg.addEventListener('wheel', (e) => {
e.preventDefault() e.preventDefault()
@@ -894,6 +1042,8 @@ function setupPanZoom(svg: SVGSVGElement, rootG: SVGGElement) {
panning = true panning = true
panStartX = e.clientX - translateX panStartX = e.clientX - translateX
panStartY = e.clientY - translateY panStartY = e.clientY - translateY
dragStartX = e.clientX
dragStartY = e.clientY
svg.style.cursor = 'grabbing' svg.style.cursor = 'grabbing'
} }
}) })
@@ -905,10 +1055,21 @@ function setupPanZoom(svg: SVGSVGElement, rootG: SVGGElement) {
applyTransform() applyTransform()
}) })
window.addEventListener('mouseup', () => { window.addEventListener('mouseup', (e) => {
if (panning) { if (panning) {
panning = false panning = false
svg.style.cursor = 'default' svg.style.cursor = 'default'
// If we barely moved, consider it a click to clear selection
const dx = e.clientX - dragStartX
const dy = e.clientY - dragStartY
if (Math.abs(dx) < 5 && Math.abs(dy) < 5) {
if ((e.target as Element).tagName === 'svg' || (e.target as Element).tagName === 'SVG') {
graphSelectedSlug.value = null
graphDrawerVisible.value = false
clearHighlight(graphNodeElsRef, graphEdgeElsRef)
}
}
} }
}) })
} }
@@ -917,28 +1078,49 @@ function setupPanZoom(svg: SVGSVGElement, rootG: SVGGElement) {
function applyHighlight( function applyHighlight(
slug: string, slug: string,
adjacency: Map<string, Set<string>>, adjacency: Map<string, Set<string>>,
nodeEls: { g: SVGGElement; circle: SVGCircleElement; text: SVGTextElement; node: GNode }[], nodeEls: { g: SVGGElement; circle: SVGCircleElement; text: SVGTextElement; activeRing: SVGCircleElement; node: GNode }[],
edgeEls: { line: SVGLineElement; source: string; target: string; bidir: boolean }[], edgeEls: { line: SVGLineElement; source: string; target: string; bidir: boolean }[],
hoverSlug?: string
) { ) {
const neighbors = adjacency.get(slug) || new Set() const neighbors = adjacency.get(slug) || new Set()
for (const { g, circle, node } of nodeEls) { const hoverNeighbors = hoverSlug ? (adjacency.get(hoverSlug) || new Set()) : new Set()
for (const { g, circle, activeRing, node } of nodeEls) {
if (node.slug === slug) { if (node.slug === slug) {
circle.setAttribute('r', String(Math.max(6, Math.min(18, 6 + node.linkCount * 1.5)) + 3)) circle.setAttribute('r', String(Math.max(6, Math.min(18, 6 + node.linkCount * 1.5)) + 3))
circle.setAttribute('stroke-width', '3') circle.setAttribute('stroke-width', '3')
g.style.opacity = '1' g.style.opacity = '1'
} else if (neighbors.has(node.slug)) { } else if (hoverSlug && node.slug === hoverSlug) {
circle.setAttribute('r', String(Math.max(6, Math.min(18, 6 + node.linkCount * 1.5)) + 3))
circle.setAttribute('stroke-width', '3')
g.style.opacity = '1'
} else if (neighbors.has(node.slug) || (hoverSlug && hoverNeighbors.has(node.slug))) {
circle.setAttribute('r', String(Math.max(6, Math.min(18, 6 + node.linkCount * 1.5))))
circle.setAttribute('stroke-width', '2')
g.style.opacity = '1' g.style.opacity = '1'
} else { } else {
circle.setAttribute('r', String(Math.max(6, Math.min(18, 6 + node.linkCount * 1.5))))
circle.setAttribute('stroke-width', '2')
g.style.opacity = '0.2' g.style.opacity = '0.2'
} }
if (node.slug === graphSelectedSlug.value) {
activeRing.style.opacity = '1'
} else {
activeRing.style.opacity = '0'
}
} }
for (const e of edgeEls) { for (const e of edgeEls) {
if (e.source === slug || e.target === slug) { if (e.source === slug || e.target === slug || (hoverSlug && (e.source === hoverSlug || e.target === hoverSlug))) {
e.line.setAttribute('stroke-opacity', '0.9') e.line.setAttribute('stroke-opacity', '0.9')
e.line.setAttribute('stroke-width', '2') e.line.setAttribute('stroke-width', '2')
// Determine which node is driving the highlight color
const focusSlug = (hoverSlug && (e.source === hoverSlug || e.target === hoverSlug)) ? hoverSlug : slug
const hlColor = nodeColorMap[ const hlColor = nodeColorMap[
nodeEls.find(n => n.node.slug === slug)?.node.type || '' nodeEls.find(n => n.node.slug === focusSlug)?.node.type || ''
] || '#0052d9' ] || '#0052d9'
e.line.setAttribute('stroke', hlColor) e.line.setAttribute('stroke', hlColor)
e.line.setAttribute('marker-end', 'url(#arrow-end-hl)') e.line.setAttribute('marker-end', 'url(#arrow-end-hl)')
if (e.bidir) e.line.setAttribute('marker-start', 'url(#arrow-start-hl)') if (e.bidir) e.line.setAttribute('marker-start', 'url(#arrow-start-hl)')
@@ -953,13 +1135,19 @@ function applyHighlight(
} }
function clearHighlight( function clearHighlight(
nodeEls: { g: SVGGElement; circle: SVGCircleElement; text: SVGTextElement; node: GNode }[], nodeEls: { g: SVGGElement; circle: SVGCircleElement; text: SVGTextElement; activeRing: SVGCircleElement; node: GNode }[],
edgeEls: { line: SVGLineElement; source: string; target: string; bidir: boolean }[], edgeEls: { line: SVGLineElement; source: string; target: string; bidir: boolean }[],
) { ) {
for (const { g, circle, node } of nodeEls) { if (graphSelectedSlug.value) {
applyHighlight(graphSelectedSlug.value, graphAdjacencyRef, nodeEls, edgeEls)
return
}
for (const { g, circle, activeRing, node } of nodeEls) {
circle.setAttribute('r', String(Math.max(6, Math.min(18, 6 + node.linkCount * 1.5)))) circle.setAttribute('r', String(Math.max(6, Math.min(18, 6 + node.linkCount * 1.5))))
circle.setAttribute('stroke-width', '2') circle.setAttribute('stroke-width', '2')
g.style.opacity = '1' g.style.opacity = '1'
activeRing.style.opacity = '0'
} }
for (const e of edgeEls) { for (const e of edgeEls) {
e.line.setAttribute('stroke', '#c0c4cc') e.line.setAttribute('stroke', '#c0c4cc')
@@ -971,92 +1159,64 @@ function clearHighlight(
} }
} }
// ─── Legend ─── const graphSearchOptions = computed(() => {
function drawLegend(svg: SVGSVGElement, width: number) { if (!graphData.value?.nodes) return []
const legendG = document.createElementNS('http://www.w3.org/2000/svg', 'g') return graphData.value.nodes.map(n => ({
legendG.setAttribute('transform', `translate(${width - 140}, 16)`) label: n.title,
value: n.slug
}))
})
// Background let graphNodeElsRef: { g: SVGGElement; circle: SVGCircleElement; text: SVGTextElement; activeRing: SVGCircleElement; node: GNode }[] = []
const bg = document.createElementNS('http://www.w3.org/2000/svg', 'rect') let graphEdgeElsRef: { line: SVGLineElement; source: string; target: string; bidir: boolean }[] = []
bg.setAttribute('x', '-8'); bg.setAttribute('y', '-4') let graphAdjacencyRef = new Map<string, Set<string>>()
bg.setAttribute('width', '132'); bg.setAttribute('rx', '6')
bg.setAttribute('fill', 'var(--td-bg-color-container)')
bg.setAttribute('fill-opacity', '0.9')
bg.setAttribute('stroke', 'var(--td-component-stroke)')
bg.setAttribute('stroke-width', '1')
legendG.appendChild(bg)
const types = [ function handleGraphSearchSelect(value: string) {
{ type: 'summary', label: t('knowledgeEditor.wikiBrowser.filterSummary') }, if (!value) return
{ type: 'entity', label: t('knowledgeEditor.wikiBrowser.filterEntity') },
{ type: 'concept', label: t('knowledgeEditor.wikiBrowser.filterConcept') },
{ type: 'synthesis', label: t('knowledgeEditor.wikiBrowser.filterSynthesis') },
{ type: 'comparison', label: t('knowledgeEditor.wikiBrowser.filterComparison') },
]
let y = 10 // Find node coordinates
for (const { type, label } of types) { const node = graphNodes.find(n => n.slug === value)
const dot = document.createElementNS('http://www.w3.org/2000/svg', 'circle') if (node && graphPanZoomRef) {
dot.setAttribute('cx', '6'); dot.setAttribute('cy', String(y)) const container = graphRef.value
dot.setAttribute('r', '5'); dot.setAttribute('fill', nodeColorMap[type]) if (container) {
legendG.appendChild(dot) const width = container.clientWidth
const height = container.clientHeight
const txt = document.createElementNS('http://www.w3.org/2000/svg', 'text') // Center node while maintaining current scale, shifted left by 240px to account for the 480px drawer
txt.setAttribute('x', '18'); txt.setAttribute('y', String(y + 4)) const currentScale = graphPanZoomRef.getScale()
txt.setAttribute('font-size', '11') graphPanZoomRef.flyTo(
txt.setAttribute('fill', 'var(--td-text-color-secondary)') width / 2 - node.x * currentScale - 240,
txt.textContent = label height / 2 - node.y * currentScale
legendG.appendChild(txt) )
}
y += 22
} }
// Divider line // Trigger highlight
y += 4 graphSelectedSlug.value = value
const divider = document.createElementNS('http://www.w3.org/2000/svg', 'line') graphHighlightSlug.value = value
divider.setAttribute('x1', '-2'); divider.setAttribute('x2', '118') if (graphNodeElsRef.length > 0) {
divider.setAttribute('y1', String(y)); divider.setAttribute('y2', String(y)) applyHighlight(value, graphAdjacencyRef, graphNodeElsRef, graphEdgeElsRef)
divider.setAttribute('stroke', 'var(--td-component-stroke)') }
divider.setAttribute('stroke-width', '1')
legendG.appendChild(divider)
y += 12
// Arrow toggle row // Open drawer automatically when searching
const arrowRow = document.createElementNS('http://www.w3.org/2000/svg', 'g') openGraphDrawer(value)
arrowRow.style.cursor = 'pointer'
// Arrow icon (small →) // Clear search input after selection to be ready for next search
const arrowIcon = document.createElementNS('http://www.w3.org/2000/svg', 'text') setTimeout(() => { graphSearchValue.value = '' }, 300)
arrowIcon.setAttribute('x', '3'); arrowIcon.setAttribute('y', String(y + 3)) }
arrowIcon.setAttribute('font-size', '13')
arrowIcon.setAttribute('fill', showArrows.value ? 'var(--td-brand-color)' : 'var(--td-text-color-placeholder)')
arrowIcon.textContent = '→'
arrowRow.appendChild(arrowIcon)
const arrowLabel = document.createElementNS('http://www.w3.org/2000/svg', 'text') function handleGraphSearchEnter(context: { inputValue: string }) {
arrowLabel.setAttribute('x', '18'); arrowLabel.setAttribute('y', String(y + 4)) const value = context.inputValue?.trim()
arrowLabel.setAttribute('font-size', '11') if (!value) return
arrowLabel.setAttribute('fill', showArrows.value ? 'var(--td-brand-color)' : 'var(--td-text-color-secondary)')
arrowLabel.textContent = showArrows.value
? t('knowledgeEditor.wikiBrowser.hideArrows')
: t('knowledgeEditor.wikiBrowser.showArrows')
arrowRow.appendChild(arrowLabel)
arrowRow.addEventListener('click', () => { // Try to find exact or partial match
toggleArrows() const match = graphSearchOptions.value.find(opt =>
// Update label & color after toggle opt.label.toLowerCase().includes(value.toLowerCase()) ||
arrowLabel.textContent = showArrows.value opt.value.toLowerCase().includes(value.toLowerCase())
? t('knowledgeEditor.wikiBrowser.hideArrows') )
: t('knowledgeEditor.wikiBrowser.showArrows')
arrowLabel.setAttribute('fill', showArrows.value ? 'var(--td-brand-color)' : 'var(--td-text-color-secondary)')
arrowIcon.setAttribute('fill', showArrows.value ? 'var(--td-brand-color)' : 'var(--td-text-color-placeholder)')
})
legendG.appendChild(arrowRow) if (match) {
y += 16 handleGraphSearchSelect(match.value)
}
bg.setAttribute('height', String(y + 4))
svg.appendChild(legendG)
} }
// Load graph when switching to graph view // Load graph when switching to graph view
@@ -1523,9 +1683,114 @@ onMounted(() => {
height: 100%; height: 100%;
} }
.wiki-graph-search {
position: absolute;
top: 16px;
left: 16px;
width: 280px;
z-index: 10;
box-shadow: var(--td-shadow-1);
border-radius: 4px;
}
:deep(.wiki-graph-drawer) {
box-shadow: -4px 0 16px rgba(0, 0, 0, 0.08);
}
.graph-search-select {
background: var(--td-bg-color-container) !important;
opacity: 0.95;
}
.wiki-graph-canvas { .wiki-graph-canvas {
width: 100%; width: 100%;
height: 100%; height: 100%;
min-height: 500px; min-height: 500px;
} }
.wiki-graph-legend {
position: absolute;
top: 16px;
right: 16px;
background: var(--td-bg-color-container);
border: 1px solid var(--td-component-stroke);
border-radius: 6px;
padding: 10px 12px;
box-shadow: var(--td-shadow-1);
display: flex;
flex-direction: column;
gap: 12px;
z-index: 10;
opacity: 0.95;
}
.legend-items {
display: flex;
flex-direction: column;
gap: 8px;
}
.legend-item {
display: flex;
align-items: center;
gap: 8px;
font-size: 11px;
color: var(--td-text-color-secondary);
}
.legend-dot {
width: 10px;
height: 10px;
border-radius: 50%;
display: inline-block;
flex-shrink: 0;
}
.legend-divider {
height: 1px;
background: var(--td-component-stroke);
margin: 0 -12px;
}
.legend-action {
display: flex;
align-items: center;
gap: 6px;
font-size: 11px;
color: var(--td-text-color-secondary);
cursor: pointer;
user-select: none;
transition: all 0.15s;
&:hover {
color: var(--td-brand-color);
.legend-action-icon {
color: var(--td-brand-color);
}
}
&.active {
color: var(--td-brand-color);
.legend-action-icon {
color: var(--td-brand-color);
}
}
}
.legend-action-icon {
font-size: 13px;
font-family: monospace;
color: var(--td-text-color-placeholder);
transition: color 0.15s;
}
@keyframes node-active-pulse {
0% { transform: scale(1); opacity: 0.8; }
100% { transform: scale(1.6); opacity: 0; }
}
.node-active-ring {
transform-origin: 0 0;
animation: node-active-pulse 1.5s cubic-bezier(0.25, 0.46, 0.45, 0.94) infinite;
}
</style> </style>