-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: better hover and mobile support (#22)
* feat: make graph look a bit better on hover * refactor: remove `stripANSICode` regression reverted in Deno 1.40.1 77b90f408c4244e8ee2e4b3bd26c441d4a250671 * refactor: extract to layout * docs(README): use easier comparision * docs: nav * refactor: remove unneeded info * fix: add mobile viewport
- Loading branch information
Showing
8 changed files
with
121 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const layout = "base.ts" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
export default ({ content, title }: Lume.Data, {}: Lume.Helpers) => { | ||
// console.log("pages:", search.pages()) | ||
|
||
return /*html*/ ` | ||
<!DOCTYPE html> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<title>${title}</title> | ||
<meta name="viewport" content="width=device-width, initial-scale=1" /> | ||
<link rel="stylesheet" href="/assets/style.css" /> | ||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/highlightjs/[email protected]/build/styles/github.min.css" /> | ||
</head> | ||
<body> | ||
<header> | ||
<h1> | ||
<a href="https://github.com/daangn/stackgraph">StackGraph</a> | ||
</h1> | ||
<nav> | ||
<a href="/">Home</a> | ||
</nav> | ||
</header> | ||
<hr /> | ||
${content} | ||
</body> | ||
` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,7 @@ const imports = Object.fromEntries( | |
) => [k, v.map((x) => data.nodes.find((node) => node.id === x.target))]), | ||
) | ||
|
||
/** @type {import("https://esm.sh/[email protected]").NodeObject | undefined} */ | ||
let hoveredNode | ||
|
||
// console.log(imports) | ||
|
@@ -42,13 +43,13 @@ const graph = ForceGraph()(graphDom) | |
.linkDirectionalArrowLength(3) | ||
.linkWidth(0.5) | ||
.nodeCanvasObject((node, ctx, globalScale) => { | ||
ctx.globalAlpha = hoveredNode | ||
? ((hoveredNode === node || imports[hoveredNode.id]?.includes(node)) | ||
? 1 | ||
: 0.1) | ||
: 1 | ||
const state = hoveredNode | ||
? (hoveredNode === node || imports[hoveredNode.id]?.includes(node)) | ||
? "highlighted" | ||
: "hidden" | ||
: "normal" | ||
|
||
const label = /**@type{string}*/ (node.name) | ||
const label = /**@type {string}*/ (node.name) | ||
const fontSize = (node.type === "import" ? 20 : 16) / globalScale | ||
ctx.font = `${fontSize}px Sans-Serif` | ||
const textWidth = ctx.measureText(label).width | ||
|
@@ -58,10 +59,32 @@ const graph = ForceGraph()(graphDom) | |
|
||
const bgWidth = scaleBg(textWidth) | ||
const bgHeight = scaleBg(fontSize) | ||
// @ts-ignore: to re-use in nodePointerAreaPaint | ||
node.bgWidth = bgWidth | ||
// @ts-ignore: to re-use in nodePointerAreaPaint | ||
node.bgHeight = bgHeight | ||
|
||
// @ts-ignore: node do has color but force-graph lacks generics to know it | ||
ctx.fillStyle = node.color | ||
|
||
ctx.globalAlpha = 1 | ||
if (state === "hidden") { | ||
ctx.globalAlpha = 0.2 | ||
ctx.beginPath() | ||
ctx.arc( | ||
// @ts-ignore: node do has x and y but force-graph marks it optional | ||
node.x, | ||
// @ts-ignore: node do has x and y but force-graph marks it optional | ||
node.y, | ||
5 / globalScale, | ||
0, | ||
2 * Math.PI, | ||
) | ||
ctx.fill() | ||
ctx.closePath() | ||
return | ||
} | ||
|
||
if (node.type === "import") { | ||
ctx.fillRect( | ||
// @ts-ignore: node do has x and y but force-graph marks it optional | ||
|
@@ -92,17 +115,13 @@ const graph = ForceGraph()(graphDom) | |
// @ts-ignore: node do has x and y but force-graph marks it optional | ||
ctx.fillText(label, node.x, node.y) | ||
|
||
// @ts-ignore: to re-use in nodePointerAreaPaint | ||
node.bgWidth = bgWidth | ||
// @ts-ignore: to re-use in nodePointerAreaPaint | ||
node.bgHeight = bgHeight | ||
|
||
// if (hoveredNode === node) { | ||
{ | ||
if (state === "hidden") return | ||
ctx.save() | ||
ctx.globalAlpha = (hoveredNode === node) ? 1 : 0.25 | ||
ctx.lineWidth = (hoveredNode === node) ? 3 : 1 | ||
const arrowLength = (hoveredNode === node) ? 24 : 6 | ||
ctx.globalAlpha = (hoveredNode === node) ? 1 : 0.5 | ||
ctx.lineWidth = ((hoveredNode === node) ? 3 : 1) / globalScale | ||
const arrowLength = ((hoveredNode === node) ? 24 : 6) / globalScale | ||
const arrowRelPos = 0.5 | ||
const arrowColor = node.color // "rgba(241, 21, 21, 0.521)" | ||
const arrowHalfWidth = arrowLength / ARROW_WH_RATIO / 2 | ||
|
@@ -208,7 +227,11 @@ const graph = ForceGraph()(graphDom) | |
}) | ||
.autoPauseRedraw(false) | ||
|
||
graph.d3Force("link")?.distance(90) | ||
globalThis.addEventListener("resize", () => { | ||
graph.width(graphDom.clientWidth).height(graphDom.clientHeight) | ||
console.log(graphDom.clientWidth) | ||
}) | ||
|
||
// graph.d3Force("link")?.strength(link => { | ||
// console.log(link) | ||
// return link.type === "import" ? 1 : 0 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
body { | ||
max-width: 1200px; | ||
margin-inline: auto; | ||
} | ||
|
||
blockquote { | ||
background-color: #f2e8da; | ||
padding: 0.5em 1em; | ||
} | ||
|
||
pre { | ||
border: 1px solid #e1e4e8; | ||
} | ||
|
||
code { | ||
background-color: #dff4e6; | ||
} | ||
|
||
#graph { | ||
height: 60vh; | ||
background-color: #f7fafc; | ||
} | ||
|
||
nav a { | ||
font-size: 1.5em; | ||
} | ||
|
||
h2, | ||
h3, | ||
ul { | ||
margin: 0.2em 0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,13 @@ | ||
export const title = "StackGraph" | ||
|
||
const readme = () => | ||
Deno.readTextFile(import.meta.dirname + "/../README.md") | ||
.then((text) => text.split("\n").slice(3).join("\n")) | ||
|
||
const header = /*md*/ ` | ||
# [StackGraph](https://github.com/daangn/stackgraph) | ||
<div id="graph"></div> | ||
(StackGraph 저장소의 모든 변수 관계도, 유색 선은 의존 관계, 무색 선은 디렉터리/파일 트리) | ||
` | ||
|
||
export default async (_: Lume.Data, { md }: Lume.Helpers) => /*html*/ ` | ||
<!DOCTYPE html> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<title>StackGraph</title> | ||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/highlightjs/[email protected]/build/styles/github.min.css" /> | ||
<style> | ||
body { | ||
margin-inline: 20vw; | ||
} | ||
blockquote { | ||
background-color: #f2e8da; | ||
padding: 0.5em 1em; | ||
} | ||
pre { | ||
border: 1px solid #e1e4e8; | ||
} | ||
code { | ||
background-color: #dff4e6; | ||
} | ||
#graph { | ||
height: 60vh; | ||
background-color: #f7fafc; | ||
} | ||
h2, | ||
h3, | ||
ul { | ||
margin: 0.2em 0 | ||
} | ||
</style> | ||
</head> | ||
<div id="graph"></div> | ||
<p>(StackGraph 저장소의 모든 변수 관계도, 유색 선은 의존 관계, 무색 선은 디렉터리/파일 트리)</p> | ||
<body> | ||
${md(header + await readme())} | ||
<script type="module" src="./assets/demo.js"></script> | ||
</body> | ||
${md(await readme())} | ||
<script type="module" src="./assets/demo.js"></script> | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,21 @@ | ||
// 1.40.5: https://github.com/denoland/deno/issues/22496 | ||
import { stripAnsiCode } from "https://deno.land/[email protected]/fmt/colors.ts" | ||
import { Reducer, Stream } from "https://deno.land/x/[email protected]/stream/mod.ts" | ||
import { encodeVSCodeURI, prettyPrintURI } from "./vscode_uri.ts" | ||
|
||
import type { Declaration, DeclDeps } from "./decl_deps.ts" | ||
import type { TopDeclDeps } from "./top_decl_deps.ts" | ||
|
||
export const serializeNoColor = (x: unknown) => | ||
stripAnsiCode( | ||
Deno.inspect(x, { | ||
depth: Infinity, | ||
colors: false, | ||
sorted: true, | ||
trailingComma: true, | ||
compact: false, | ||
iterableLimit: Infinity, | ||
breakLength: Infinity, | ||
escapeSequences: false, | ||
strAbbreviateSize: Infinity, | ||
}), | ||
) | ||
Deno.inspect(x, { | ||
depth: Infinity, | ||
colors: false, | ||
sorted: true, | ||
trailingComma: true, | ||
compact: false, | ||
iterableLimit: Infinity, | ||
breakLength: Infinity, | ||
escapeSequences: false, | ||
strAbbreviateSize: Infinity, | ||
}) | ||
|
||
export const asRecord = | ||
<T extends string | number | symbol>(fn: (decl: Declaration) => T) => | ||
|