From b88be2f7679f03b6d564e1083a6af8dd00ea44c8 Mon Sep 17 00:00:00 2001 From: Aaron Pham Date: Thu, 15 Aug 2024 04:16:42 -0400 Subject: [PATCH] fix(spa): only render graph once in global Signed-off-by: Aaron Pham --- quartz/components/Graph.tsx | 2 +- quartz/components/scripts/graph.inline.ts | 41 ++++++++++++++++------- 2 files changed, 29 insertions(+), 14 deletions(-) diff --git a/quartz/components/Graph.tsx b/quartz/components/Graph.tsx index 48765696e..4602f9e5c 100644 --- a/quartz/components/Graph.tsx +++ b/quartz/components/Graph.tsx @@ -64,7 +64,7 @@ export default ((opts?: GraphOptions) => {

{i18n(cfg.locale).components.graph.title}

- +
= new Map( Object.entries(await fetchData).map(([k, v]) => [ @@ -133,8 +137,6 @@ async function renderGraph(container: string, fullSlug: FullSlug) { if (showTags) tags.forEach((tag) => neighbourhood.add(tag)) } - // XXX: How does links got morphed into LinkNodes here? - // links => LinkData[], where as links.filter(l => neighbourhood.has(l.source) && neighbourhood.has(l.target)) => LinkNodes[] const graphData: { nodes: NodeData[]; links: LinkNodes[] } = { nodes: [...neighbourhood].map((url) => { const text = url.startsWith("tags/") ? "#" + url.substring(5) : (data.get(url)?.title ?? url) @@ -165,8 +167,8 @@ async function renderGraph(container: string, fullSlug: FullSlug) { d3.forceCollide((n) => nodeRadius(n)), ) - const width = canvas.offsetWidth - const height = Math.max(canvas.offsetHeight, 250) + const width = graph.offsetWidth + const height = Math.max(graph.offsetHeight, 250) const computedStyleMap = new Map() for (let i of [ "--secondary", @@ -178,7 +180,7 @@ async function renderGraph(container: string, fullSlug: FullSlug) { "--darkgray", "--bodyFont", ]) { - computedStyleMap.set(i, getComputedStyle(canvas).getPropertyValue(i)) + computedStyleMap.set(i, getComputedStyle(graph).getPropertyValue(i)) } // calculate color @@ -304,7 +306,7 @@ async function renderGraph(container: string, fullSlug: FullSlug) { await app.init({ width, height, - canvas: canvas, + canvas: global ? (graph as HTMLCanvasElement) : undefined, antialias: true, autoStart: false, autoDensity: true, @@ -314,6 +316,10 @@ async function renderGraph(container: string, fullSlug: FullSlug) { eventMode: "static", }) + if (!global) { + graph.appendChild(app.canvas) + } + const stage = app.stage stage.interactive = false @@ -454,7 +460,16 @@ async function renderGraph(container: string, fullSlug: FullSlug) { } }), ) + } else { + graphData.nodes.forEach((node) => { + if (!node.gfx) return + node.gfx.on("click", () => { + const targ = resolveRelative(fullSlug, node.id) + window.spaNavigate(new URL(targ, window.location.toString())) + }) + }) } + if (enableZoom) { d3.select(app.canvas).call( d3 @@ -520,7 +535,7 @@ document.addEventListener("nav", async (e: CustomEventMap["nav"]) => { sidebar.style.zIndex = "1" } - renderGraph("global-graph-container", slug) + renderGraph("global-graph-container", slug, true) registerEscapeHandler(container, hideGlobalGraph) }