Merge pull request #134 from aidenybai/add-prefetching-within-graph
This commit is contained in:
commit
232bd2f016
@ -1,12 +1,5 @@
|
|||||||
async function drawGraph(
|
async function drawGraph(baseUrl, pathColors, depth, enableDrag, enableLegend, enableZoom) {
|
||||||
baseUrl,
|
const container = document.getElementById("graph-container")
|
||||||
pathColors,
|
|
||||||
depth,
|
|
||||||
enableDrag,
|
|
||||||
enableLegend,
|
|
||||||
enableZoom
|
|
||||||
) {
|
|
||||||
const container = document.getElementById('graph-container')
|
|
||||||
const { index, links, content } = await fetchData
|
const { index, links, content } = await fetchData
|
||||||
|
|
||||||
// Use .pathname to remove hashes / searchParams / text fragments
|
// Use .pathname to remove hashes / searchParams / text fragments
|
||||||
@ -23,22 +16,19 @@ async function drawGraph(
|
|||||||
const copyLinks = JSON.parse(JSON.stringify(links))
|
const copyLinks = JSON.parse(JSON.stringify(links))
|
||||||
|
|
||||||
const neighbours = new Set()
|
const neighbours = new Set()
|
||||||
const wl = [curPage || '/', '__SENTINEL']
|
const wl = [curPage || "/", "__SENTINEL"]
|
||||||
if (depth >= 0) {
|
if (depth >= 0) {
|
||||||
while (depth >= 0 && wl.length > 0) {
|
while (depth >= 0 && wl.length > 0) {
|
||||||
// compute neighbours
|
// compute neighbours
|
||||||
const cur = wl.shift()
|
const cur = wl.shift()
|
||||||
if (cur === '__SENTINEL') {
|
if (cur === "__SENTINEL") {
|
||||||
depth--
|
depth--
|
||||||
wl.push('__SENTINEL')
|
wl.push("__SENTINEL")
|
||||||
} else {
|
} else {
|
||||||
neighbours.add(cur)
|
neighbours.add(cur)
|
||||||
const outgoing = index.links[cur] || []
|
const outgoing = index.links[cur] || []
|
||||||
const incoming = index.backlinks[cur] || []
|
const incoming = index.backlinks[cur] || []
|
||||||
wl.push(
|
wl.push(...outgoing.map((l) => l.target), ...incoming.map((l) => l.source))
|
||||||
...outgoing.map((l) => l.target),
|
|
||||||
...incoming.map((l) => l.source)
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -47,14 +37,12 @@ async function drawGraph(
|
|||||||
|
|
||||||
const data = {
|
const data = {
|
||||||
nodes: [...neighbours].map((id) => ({ id })),
|
nodes: [...neighbours].map((id) => ({ id })),
|
||||||
links: copyLinks.filter(
|
links: copyLinks.filter((l) => neighbours.has(l.source) && neighbours.has(l.target)),
|
||||||
(l) => neighbours.has(l.source) && neighbours.has(l.target)
|
|
||||||
),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const color = (d) => {
|
const color = (d) => {
|
||||||
if (d.id === curPage || (d.id === '/' && curPage === '')) {
|
if (d.id === curPage || (d.id === "/" && curPage === "")) {
|
||||||
return 'var(--g-node-active)'
|
return "var(--g-node-active)"
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const pathColor of pathColors) {
|
for (const pathColor of pathColors) {
|
||||||
@ -65,7 +53,7 @@ async function drawGraph(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return 'var(--g-node)'
|
return "var(--g-node)"
|
||||||
}
|
}
|
||||||
|
|
||||||
const drag = (simulation) => {
|
const drag = (simulation) => {
|
||||||
@ -89,9 +77,9 @@ async function drawGraph(
|
|||||||
const noop = () => {}
|
const noop = () => {}
|
||||||
return d3
|
return d3
|
||||||
.drag()
|
.drag()
|
||||||
.on('start', enableDrag ? dragstarted : noop)
|
.on("start", enableDrag ? dragstarted : noop)
|
||||||
.on('drag', enableDrag ? dragged : noop)
|
.on("drag", enableDrag ? dragged : noop)
|
||||||
.on('end', enableDrag ? dragended : noop)
|
.on("end", enableDrag ? dragended : noop)
|
||||||
}
|
}
|
||||||
|
|
||||||
const height = Math.max(container.offsetHeight, 250)
|
const height = Math.max(container.offsetHeight, 250)
|
||||||
@ -99,67 +87,58 @@ async function drawGraph(
|
|||||||
|
|
||||||
const simulation = d3
|
const simulation = d3
|
||||||
.forceSimulation(data.nodes)
|
.forceSimulation(data.nodes)
|
||||||
.force('charge', d3.forceManyBody().strength(-30))
|
.force("charge", d3.forceManyBody().strength(-30))
|
||||||
.force(
|
.force(
|
||||||
'link',
|
"link",
|
||||||
d3
|
d3
|
||||||
.forceLink(data.links)
|
.forceLink(data.links)
|
||||||
.id((d) => d.id)
|
.id((d) => d.id)
|
||||||
.distance(40)
|
.distance(40),
|
||||||
)
|
)
|
||||||
.force('center', d3.forceCenter())
|
.force("center", d3.forceCenter())
|
||||||
|
|
||||||
const svg = d3
|
const svg = d3
|
||||||
.select('#graph-container')
|
.select("#graph-container")
|
||||||
.append('svg')
|
.append("svg")
|
||||||
.attr('width', width)
|
.attr("width", width)
|
||||||
.attr('height', height)
|
.attr("height", height)
|
||||||
.attr('viewBox', [-width / 2, -height / 2, width, height])
|
.attr("viewBox", [-width / 2, -height / 2, width, height])
|
||||||
|
|
||||||
if (enableLegend) {
|
if (enableLegend) {
|
||||||
const legend = [
|
const legend = [{ Current: "var(--g-node-active)" }, { Note: "var(--g-node)" }, ...pathColors]
|
||||||
{ Current: 'var(--g-node-active)' },
|
|
||||||
{ Note: 'var(--g-node)' },
|
|
||||||
...pathColors,
|
|
||||||
]
|
|
||||||
legend.forEach((legendEntry, i) => {
|
legend.forEach((legendEntry, i) => {
|
||||||
const key = Object.keys(legendEntry)[0]
|
const key = Object.keys(legendEntry)[0]
|
||||||
const colour = legendEntry[key]
|
const colour = legendEntry[key]
|
||||||
svg
|
svg
|
||||||
.append('circle')
|
.append("circle")
|
||||||
.attr('cx', -width / 2 + 20)
|
.attr("cx", -width / 2 + 20)
|
||||||
.attr('cy', height / 2 - 30 * (i + 1))
|
.attr("cy", height / 2 - 30 * (i + 1))
|
||||||
.attr('r', 6)
|
.attr("r", 6)
|
||||||
.style('fill', colour)
|
.style("fill", colour)
|
||||||
svg
|
svg
|
||||||
.append('text')
|
.append("text")
|
||||||
.attr('x', -width / 2 + 40)
|
.attr("x", -width / 2 + 40)
|
||||||
.attr('y', height / 2 - 30 * (i + 1))
|
.attr("y", height / 2 - 30 * (i + 1))
|
||||||
.text(key)
|
.text(key)
|
||||||
.style('font-size', '15px')
|
.style("font-size", "15px")
|
||||||
.attr('alignment-baseline', 'middle')
|
.attr("alignment-baseline", "middle")
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// draw links between nodes
|
// draw links between nodes
|
||||||
const link = svg
|
const link = svg
|
||||||
.append('g')
|
.append("g")
|
||||||
.selectAll('line')
|
.selectAll("line")
|
||||||
.data(data.links)
|
.data(data.links)
|
||||||
.join('line')
|
.join("line")
|
||||||
.attr('class', 'link')
|
.attr("class", "link")
|
||||||
.attr('stroke', 'var(--g-link)')
|
.attr("stroke", "var(--g-link)")
|
||||||
.attr('stroke-width', 2)
|
.attr("stroke-width", 2)
|
||||||
.attr('data-source', (d) => d.source.id)
|
.attr("data-source", (d) => d.source.id)
|
||||||
.attr('data-target', (d) => d.target.id)
|
.attr("data-target", (d) => d.target.id)
|
||||||
|
|
||||||
// svg groups
|
// svg groups
|
||||||
const graphNode = svg
|
const graphNode = svg.append("g").selectAll("g").data(data.nodes).enter().append("g")
|
||||||
.append('g')
|
|
||||||
.selectAll('g')
|
|
||||||
.data(data.nodes)
|
|
||||||
.enter()
|
|
||||||
.append('g')
|
|
||||||
|
|
||||||
// calculate radius
|
// calculate radius
|
||||||
const nodeRadius = (d) => {
|
const nodeRadius = (d) => {
|
||||||
@ -170,82 +149,68 @@ async function drawGraph(
|
|||||||
|
|
||||||
// draw individual nodes
|
// draw individual nodes
|
||||||
const node = graphNode
|
const node = graphNode
|
||||||
.append('circle')
|
.append("circle")
|
||||||
.attr('class', 'node')
|
.attr("class", "node")
|
||||||
.attr('id', (d) => d.id)
|
.attr("id", (d) => d.id)
|
||||||
.attr('r', nodeRadius)
|
.attr("r", nodeRadius)
|
||||||
.attr('fill', color)
|
.attr("fill", color)
|
||||||
.style('cursor', 'pointer')
|
.style("cursor", "pointer")
|
||||||
.on('click', (_, d) => {
|
.on("click", (_, d) => {
|
||||||
// SPA navigation
|
// SPA navigation
|
||||||
window.navigate(
|
window.Million.navigate(new URL(`${baseUrl}${decodeURI(d.id).replace(/\s+/g, "-")}/`), ".singlePage")
|
||||||
new URL(`${baseUrl}${decodeURI(d.id).replace(/\s+/g, '-')}/`),
|
|
||||||
'.singlePage'
|
|
||||||
)
|
|
||||||
})
|
})
|
||||||
.on('mouseover', function(_, d) {
|
.on("mouseover", function (_, d) {
|
||||||
d3.selectAll('.node')
|
d3.selectAll(".node").transition().duration(100).attr("fill", "var(--g-node-inactive)")
|
||||||
.transition()
|
|
||||||
.duration(100)
|
|
||||||
.attr('fill', 'var(--g-node-inactive)')
|
|
||||||
|
|
||||||
const neighbours = parseIdsFromLinks([
|
const neighbours = parseIdsFromLinks([
|
||||||
...(index.links[d.id] || []),
|
...(index.links[d.id] || []),
|
||||||
...(index.backlinks[d.id] || []),
|
...(index.backlinks[d.id] || []),
|
||||||
])
|
])
|
||||||
const neighbourNodes = d3
|
const neighbourNodes = d3.selectAll(".node").filter((d) => neighbours.includes(d.id))
|
||||||
.selectAll('.node')
|
|
||||||
.filter((d) => neighbours.includes(d.id))
|
|
||||||
const currentId = d.id
|
const currentId = d.id
|
||||||
|
window.Million.prefetch(new URL(`${baseUrl}${decodeURI(d.id).replace(/\s+/g, "-")}/`))
|
||||||
const linkNodes = d3
|
const linkNodes = d3
|
||||||
.selectAll('.link')
|
.selectAll(".link")
|
||||||
.filter((d) => d.source.id === currentId || d.target.id === currentId)
|
.filter((d) => d.source.id === currentId || d.target.id === currentId)
|
||||||
|
|
||||||
// highlight neighbour nodes
|
// highlight neighbour nodes
|
||||||
neighbourNodes.transition().duration(200).attr('fill', color)
|
neighbourNodes.transition().duration(200).attr("fill", color)
|
||||||
|
|
||||||
// highlight links
|
// highlight links
|
||||||
linkNodes
|
linkNodes.transition().duration(200).attr("stroke", "var(--g-link-active)")
|
||||||
.transition()
|
|
||||||
.duration(200)
|
|
||||||
.attr('stroke', 'var(--g-link-active)')
|
|
||||||
|
|
||||||
// show text for self
|
// show text for self
|
||||||
d3.select(this.parentNode)
|
d3.select(this.parentNode)
|
||||||
.raise()
|
.raise()
|
||||||
.select('text')
|
.select("text")
|
||||||
.transition()
|
.transition()
|
||||||
.duration(200)
|
.duration(200)
|
||||||
.style('opacity', 1)
|
.style("opacity", 1)
|
||||||
})
|
})
|
||||||
.on('mouseleave', function(_, d) {
|
.on("mouseleave", function (_, d) {
|
||||||
d3.selectAll('.node').transition().duration(200).attr('fill', color)
|
d3.selectAll(".node").transition().duration(200).attr("fill", color)
|
||||||
|
|
||||||
const currentId = d.id
|
const currentId = d.id
|
||||||
const linkNodes = d3
|
const linkNodes = d3
|
||||||
.selectAll('.link')
|
.selectAll(".link")
|
||||||
.filter((d) => d.source.id === currentId || d.target.id === currentId)
|
.filter((d) => d.source.id === currentId || d.target.id === currentId)
|
||||||
|
|
||||||
linkNodes.transition().duration(200).attr('stroke', 'var(--g-link)')
|
linkNodes.transition().duration(200).attr("stroke", "var(--g-link)")
|
||||||
|
|
||||||
d3.select(this.parentNode)
|
d3.select(this.parentNode).select("text").transition().duration(200).style("opacity", 0)
|
||||||
.select('text')
|
|
||||||
.transition()
|
|
||||||
.duration(200)
|
|
||||||
.style('opacity', 0)
|
|
||||||
})
|
})
|
||||||
.call(drag(simulation))
|
.call(drag(simulation))
|
||||||
|
|
||||||
// draw labels
|
// draw labels
|
||||||
const labels = graphNode
|
const labels = graphNode
|
||||||
.append('text')
|
.append("text")
|
||||||
.attr('dx', 0)
|
.attr("dx", 0)
|
||||||
.attr('dy', (d) => nodeRadius(d) + 8 + 'px')
|
.attr("dy", (d) => nodeRadius(d) + 8 + "px")
|
||||||
.attr('text-anchor', 'middle')
|
.attr("text-anchor", "middle")
|
||||||
.text((d) => content[d.id]?.title || d.id.replace('-', ' '))
|
.text((d) => content[d.id]?.title || d.id.replace("-", " "))
|
||||||
.style('opacity', 0)
|
.style("opacity", 0)
|
||||||
.style('pointer-events', 'none')
|
.style("pointer-events", "none")
|
||||||
.style('font-size', '0.4em')
|
.style("font-size", "0.4em")
|
||||||
.raise()
|
.raise()
|
||||||
.call(drag(simulation))
|
.call(drag(simulation))
|
||||||
|
|
||||||
@ -260,24 +225,24 @@ async function drawGraph(
|
|||||||
[width, height],
|
[width, height],
|
||||||
])
|
])
|
||||||
.scaleExtent([0.25, 4])
|
.scaleExtent([0.25, 4])
|
||||||
.on('zoom', ({ transform }) => {
|
.on("zoom", ({ transform }) => {
|
||||||
link.attr('transform', transform)
|
link.attr("transform", transform)
|
||||||
node.attr('transform', transform)
|
node.attr("transform", transform)
|
||||||
const scale = transform.k
|
const scale = transform.k
|
||||||
const scaledOpacity = Math.max((scale - 1) / 3.75, 0)
|
const scaledOpacity = Math.max((scale - 1) / 3.75, 0)
|
||||||
labels.attr('transform', transform).style('opacity', scaledOpacity)
|
labels.attr("transform", transform).style("opacity", scaledOpacity)
|
||||||
})
|
}),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
// progress the simulation
|
// progress the simulation
|
||||||
simulation.on('tick', () => {
|
simulation.on("tick", () => {
|
||||||
link
|
link
|
||||||
.attr('x1', (d) => d.source.x)
|
.attr("x1", (d) => d.source.x)
|
||||||
.attr('y1', (d) => d.source.y)
|
.attr("y1", (d) => d.source.y)
|
||||||
.attr('x2', (d) => d.target.x)
|
.attr("x2", (d) => d.target.x)
|
||||||
.attr('y2', (d) => d.target.y)
|
.attr("y2", (d) => d.target.y)
|
||||||
node.attr('cx', (d) => d.x).attr('cy', (d) => d.y)
|
node.attr("cx", (d) => d.x).attr("cy", (d) => d.y)
|
||||||
labels.attr('x', (d) => d.x).attr('y', (d) => d.y)
|
labels.attr("x", (d) => d.x).attr("y", (d) => d.y)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -1,12 +1,16 @@
|
|||||||
import { router, navigate } from "https://unpkg.com/million@1.8.9-0/dist/router.mjs"
|
import { router, navigate, reload, prefetch } from "https://unpkg.com/million@1.9.4/dist/router.mjs"
|
||||||
|
|
||||||
export const attachSPARouting = (draw) => {
|
export const attachSPARouting = (draw) => {
|
||||||
// SPA navigation for access later
|
// Attach SPA functions to the global Million namespace
|
||||||
window.navigate = navigate
|
window.Million = {
|
||||||
// We only mutate document.title and content within .singlePage element
|
router,
|
||||||
|
navigate,
|
||||||
|
reload,
|
||||||
|
prefetch,
|
||||||
|
};
|
||||||
router(".singlePage")
|
router(".singlePage")
|
||||||
// We need on initial load, then subsequent redirs
|
// We need on initial load, then subsequent redirs
|
||||||
// requestAnimationFrame() delays graph draw until SPA routing is finished
|
// requestAnimationFrame() delays graph draw until SPA routing is finished
|
||||||
window.addEventListener("million:navigate", () => requestAnimationFrame(draw))
|
reload(draw)
|
||||||
window.addEventListener("DOMContentLoaded", () => requestAnimationFrame(draw))
|
window.addEventListener("DOMContentLoaded", () => requestAnimationFrame(draw))
|
||||||
}
|
}
|
||||||
|
@ -153,7 +153,7 @@ const highlight = (content, term) => {
|
|||||||
|
|
||||||
const redir = (id, term) => {
|
const redir = (id, term) => {
|
||||||
// SPA navigation
|
// SPA navigation
|
||||||
window.navigate(
|
window.Million.navigate(
|
||||||
new URL(
|
new URL(
|
||||||
`${BASE_URL.replace(/\/$/g, "")}${id}#:~:text=${encodeURIComponent(term)}/`
|
`${BASE_URL.replace(/\/$/g, "")}${id}#:~:text=${encodeURIComponent(term)}/`
|
||||||
),
|
),
|
||||||
|
@ -107,7 +107,7 @@
|
|||||||
</script>
|
</script>
|
||||||
{{else}}
|
{{else}}
|
||||||
<script>
|
<script>
|
||||||
window.navigate = (url) => window.location.href = url;
|
window.Million.navigate = (url) => window.location.href = url;
|
||||||
draw();
|
draw();
|
||||||
</script>
|
</script>
|
||||||
{{end}}
|
{{end}}
|
||||||
|
Loading…
Reference in New Issue
Block a user