Compare commits

...

3 Commits

Author SHA1 Message Date
Aaron Pham
c15e48d217
Merge 7037c0e3ab84d748776e89ba4f41d39898321c78 into fbc45548f7ee80715ec74d8c249c662a26f7feae 2025-02-01 22:00:39 +00:00
Aaron Pham
fbc45548f7
feat(graph): enable radial mode (#1738)
Some checks failed
Build and Test / publish-tag (push) Has been cancelled
Build and Test / build-and-test (macos-latest) (push) Has been cancelled
Build and Test / build-and-test (ubuntu-latest) (push) Has been cancelled
Build and Test / build-and-test (windows-latest) (push) Has been cancelled
Docker build & push image / build (push) Has been cancelled
2025-02-01 16:22:29 -05:00
Aaron Pham
7037c0e3ab
fix(popover): handle same page resolution
Signed-off-by: Aaron Pham <contact@aarnphm.xyz>
2025-01-18 17:09:20 -05:00
4 changed files with 47 additions and 4 deletions

View File

@ -36,6 +36,7 @@ Component.Graph({
opacityScale: 1, // how quickly do we fade out the labels when zooming out?
removeTags: [], // what tags to remove from the graph
showTags: true, // whether to show tags in the graph
enableRadial: false, // whether to constrain the graph, similar to Obsidian
},
globalGraph: {
drag: true,
@ -49,6 +50,7 @@ Component.Graph({
opacityScale: 1,
removeTags: [], // what tags to remove from the graph
showTags: true, // whether to show tags in the graph
enableRadial: true, // whether to constrain the graph, similar to Obsidian
},
})
```

View File

@ -18,6 +18,7 @@ export interface D3Config {
removeTags: string[]
showTags: boolean
focusOnHover?: boolean
enableRadial?: boolean
}
interface GraphOptions {
@ -39,6 +40,7 @@ const defaultOptions: GraphOptions = {
showTags: true,
removeTags: [],
focusOnHover: false,
enableRadial: false,
},
globalGraph: {
drag: true,
@ -53,10 +55,11 @@ const defaultOptions: GraphOptions = {
showTags: true,
removeTags: [],
focusOnHover: true,
enableRadial: true,
},
}
export default ((opts?: GraphOptions) => {
export default ((opts?: Partial<GraphOptions>) => {
const Graph: QuartzComponent = ({ displayClass, cfg }: QuartzComponentProps) => {
const localGraph = { ...defaultOptions.localGraph, ...opts?.localGraph }
const globalGraph = { ...defaultOptions.globalGraph, ...opts?.globalGraph }

View File

@ -8,6 +8,7 @@ import {
forceCenter,
forceLink,
forceCollide,
forceRadial,
zoomIdentity,
select,
drag,
@ -87,6 +88,7 @@ async function renderGraph(container: string, fullSlug: FullSlug) {
removeTags,
showTags,
focusOnHover,
enableRadial,
} = JSON.parse(graph.dataset["cfg"]!) as D3Config
const data: Map<SimpleSlug, ContentDetails> = new Map(
@ -161,15 +163,20 @@ async function renderGraph(container: string, fullSlug: FullSlug) {
})),
}
const width = graph.offsetWidth
const height = Math.max(graph.offsetHeight, 250)
// we virtualize the simulation and use pixi to actually render it
// Calculate the radius of the container circle
const radius = Math.min(width, height) / 2 - 40 // 40px padding
const simulation: Simulation<NodeData, LinkData> = forceSimulation<NodeData>(graphData.nodes)
.force("charge", forceManyBody().strength(-100 * repelForce))
.force("center", forceCenter().strength(centerForce))
.force("link", forceLink(graphData.links).distance(linkDistance))
.force("collide", forceCollide<NodeData>((n) => nodeRadius(n)).iterations(3))
const width = graph.offsetWidth
const height = Math.max(graph.offsetHeight, 250)
if (enableRadial)
simulation.force("radial", forceRadial(radius * 0.8, width / 2, height / 2).strength(0.3))
// precompute style prop strings as pixi doesn't support css variables
const cssVars = [

View File

@ -38,6 +38,11 @@ async function mouseEnterHandler(
targetUrl.hash = ""
targetUrl.search = ""
// If it's the same page, just return without fetching
if (thisUrl.toString() === targetUrl.toString()) {
return
}
const response = await fetchCanonical(targetUrl).catch((err) => {
console.error(err)
})
@ -100,10 +105,36 @@ async function mouseEnterHandler(
}
}
function handleSamePageClick(evt: MouseEvent) {
const link = evt.currentTarget as HTMLAnchorElement
const thisUrl = new URL(document.location.href)
thisUrl.hash = ""
thisUrl.search = ""
const targetUrl = new URL(link.href)
const hash = decodeURIComponent(targetUrl.hash)
targetUrl.hash = ""
targetUrl.search = ""
if (thisUrl.toString() === targetUrl.toString() && hash !== "") {
evt.preventDefault()
const mainContent = document.querySelector("article")
const heading = mainContent?.querySelector(hash) as HTMLElement | null
if (heading) {
heading.scrollIntoView({ behavior: "smooth" })
// Optionally update the URL without a page reload
history.pushState(null, "", hash)
}
}
}
document.addEventListener("nav", () => {
const links = [...document.getElementsByClassName("internal")] as HTMLAnchorElement[]
for (const link of links) {
link.addEventListener("mouseenter", mouseEnterHandler)
window.addCleanup(() => link.removeEventListener("mouseenter", mouseEnterHandler))
link.addEventListener("click", handleSamePageClick)
window.addCleanup(() => {
link.removeEventListener("mouseenter", mouseEnterHandler)
link.removeEventListener("click", handleSamePageClick)
})
}
})