fix: 帮助页夜间配色并让流程图适应窗口

This commit is contained in:
Nixevol
2026-10-21 20:29:17 +08:00
parent d0405a6d78
commit 58fbb5b1b3
2 changed files with 124 additions and 25 deletions
+59 -1
View File
@@ -40,11 +40,44 @@ export async function drawDiagrams(root: ParentNode, dark: boolean): Promise<voi
const nodes = [...root.querySelectorAll<HTMLElement>(".diagram")];
if (nodes.length === 0 || import.meta.env.VITEST) return;
const mermaid = (await import("mermaid")).default;
const ink = dark ? "#e6edf3" : "#1f2328";
const muted = dark ? "#8b949e" : "#59636e";
const panel = dark ? "#161b22" : "#f6f8fa";
const canvas = dark ? "#0d1117" : "#ffffff";
const line = dark ? "#30363d" : "#d0d7de";
mermaid.initialize({
startOnLoad: false,
securityLevel: "strict",
theme: dark ? "dark" : "neutral",
theme: "base",
fontFamily: '"Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif',
themeVariables: {
darkMode: dark,
background: canvas,
primaryColor: panel,
primaryTextColor: ink,
primaryBorderColor: line,
secondaryColor: panel,
secondaryTextColor: ink,
tertiaryColor: canvas,
tertiaryTextColor: ink,
lineColor: muted,
textColor: ink,
actorBkg: "#0969da",
actorBorder: dark ? "#4493f8" : "#0969da",
actorTextColor: "#ffffff",
actorLineColor: muted,
signalColor: ink,
signalTextColor: ink,
labelBoxBkgColor: panel,
labelBoxBorderColor: line,
labelTextColor: ink,
noteBkgColor: panel,
noteBorderColor: line,
noteTextColor: ink,
activationBkgColor: panel,
activationBorderColor: line,
},
sequence: { useMaxWidth: true, diagramMarginX: 12, diagramMarginY: 12 },
});
for (const node of nodes) {
const source = node.dataset.source ?? "";
@@ -52,4 +85,29 @@ export async function drawDiagrams(root: ParentNode, dark: boolean): Promise<voi
node.removeAttribute("data-processed");
}
await mermaid.run({ nodes });
fitDiagrams(root);
}
export function fitDiagrams(root: ParentNode): void {
for (const node of root.querySelectorAll<HTMLElement>(".diagram")) fitDiagram(node);
}
function fitDiagram(node: HTMLElement): void {
const svg = node.querySelector("svg");
if (!svg) return;
const box = svg.getBBox();
const pad = 16;
const width = box.width + pad * 2;
const height = box.height + pad * 2;
svg.setAttribute("viewBox", `${box.x - pad} ${box.y - pad} ${width} ${height}`);
svg.removeAttribute("height");
svg.removeAttribute("width");
const availW = Math.max(240, node.clientWidth - 8);
const availH = Math.max(320, window.innerHeight - 220);
const scale = Math.min(availW / width, availH / height);
svg.style.display = "block";
svg.style.width = `${Math.floor(width * scale)}px`;
svg.style.height = `${Math.floor(height * scale)}px`;
svg.style.maxWidth = "100%";
svg.style.margin = "0 auto";
}
+65 -24
View File
@@ -1,8 +1,8 @@
<script setup lang="ts">
import { nextTick, onMounted, ref, watch } from "vue";
import { nextTick, onBeforeUnmount, onMounted, ref, watch } from "vue";
import PageHeader from "@/components/PageHeader.vue";
import source from "@/help/help.md?raw";
import { drawDiagrams, renderHelp } from "@/help/render";
import { drawDiagrams, fitDiagrams, renderHelp } from "@/help/render";
import { useAppTheme } from "@/theme";
const html = renderHelp(source);
@@ -51,8 +51,18 @@ async function paint() {
for (const node of list) observer.observe(node);
}
function onResize() {
if (article.value) fitDiagrams(article.value);
}
onMounted(() => {
void paint();
window.addEventListener("resize", onResize);
});
onBeforeUnmount(() => {
window.removeEventListener("resize", onResize);
observer?.disconnect();
});
watch(isDark, () => {
@@ -61,7 +71,7 @@ watch(isDark, () => {
</script>
<template>
<div class="page">
<div class="page" :class="{ dark: isDark }">
<PageHeader title="帮助" />
<div class="body">
<nav class="toc" aria-label="帮助目录">
@@ -128,8 +138,16 @@ watch(isDark, () => {
overflow: auto;
padding: 20px 32px 56px;
}
.doc :deep(h2),
.doc :deep(h3),
.doc :deep(p),
.doc :deep(li),
.doc :deep(td),
.doc :deep(th) {
color: #1f2328;
}
.doc :deep(h2) {
max-width: 920px;
max-width: none;
margin: 32px 0 12px;
font-size: 20px;
font-weight: 650;
@@ -141,7 +159,7 @@ watch(isDark, () => {
margin-top: 0;
}
.doc :deep(h3) {
max-width: 72ch;
max-width: none;
margin: 26px 0 8px;
font-size: 15px;
font-weight: 650;
@@ -173,7 +191,7 @@ watch(isDark, () => {
}
.doc :deep(table) {
width: 100%;
max-width: 920px;
max-width: 100%;
margin-top: 12px;
border-collapse: collapse;
font-size: 13px;
@@ -189,16 +207,16 @@ watch(isDark, () => {
.doc :deep(th) {
background: #f6f8fa;
font-weight: 650;
white-space: nowrap;
}
.doc :deep(pre) {
max-width: 920px;
max-width: 100%;
margin: 12px 0 0;
padding: 12px 14px;
overflow: auto;
border: 1px solid #d0d7de;
border-radius: 6px;
background: #f6f8fa;
color: #1f2328;
font-family: ui-monospace, "Cascadia Code", Consolas, monospace;
font-size: 12.5px;
line-height: 1.6;
@@ -215,10 +233,11 @@ watch(isDark, () => {
background: rgba(9, 105, 218, 0.08);
}
.doc :deep(.diagram) {
max-width: 920px;
width: 100%;
max-width: 100%;
margin-top: 16px;
padding: 12px 8px;
overflow: auto;
padding: 8px;
overflow: hidden;
border: 1px solid #d0d7de;
border-radius: 6px;
background: #fff;
@@ -227,33 +246,52 @@ watch(isDark, () => {
display: block;
max-width: 100%;
height: auto;
margin: 0 auto;
}
:global(html.dark) .toc {
.doc :deep(.diagram svg text.actor) {
fill: #ffffff !important;
}
.page.dark .toc {
border-right-color: #30363d;
}
:global(html.dark) .toc a {
.page.dark .toc a {
color: #e6edf3;
}
:global(html.dark) .toc a.on {
.page.dark .toc a.on {
color: #4493f8;
background: rgba(68, 147, 248, 0.16);
}
:global(html.dark) .doc :deep(th),
:global(html.dark) .doc :deep(pre),
:global(html.dark) .doc :deep(.diagram) {
.page.dark .doc :deep(h2),
.page.dark .doc :deep(h3),
.page.dark .doc :deep(p),
.page.dark .doc :deep(li),
.page.dark .doc :deep(td),
.page.dark .doc :deep(th),
.page.dark .doc :deep(pre),
.page.dark .doc :deep(code) {
color: #e6edf3;
}
.page.dark .doc :deep(th),
.page.dark .doc :deep(pre) {
background: #161b22;
}
.page.dark .doc :deep(th),
.page.dark .doc :deep(td),
.page.dark .doc :deep(pre),
.page.dark .doc :deep(.diagram) {
border-color: #30363d;
}
:global(html.dark) .doc :deep(th),
:global(html.dark) .doc :deep(td),
:global(html.dark) .doc :deep(pre) {
border-color: #30363d;
.page.dark .doc :deep(.diagram) {
background: #0d1117;
}
:global(html.dark) .doc :deep(a) {
.page.dark .doc :deep(a) {
color: #4493f8;
}
@media (max-width: 760px) {
.page.dark .doc :deep(p code),
.page.dark .doc :deep(td code),
.page.dark .doc :deep(li code) {
background: rgba(68, 147, 248, 0.16);
}
@media (max-width: 1100px) {
.body {
flex-direction: column;
}
@@ -269,6 +307,9 @@ watch(isDark, () => {
.toc a {
white-space: nowrap;
}
.page.dark .toc {
border-bottom-color: #30363d;
}
.doc {
padding: 16px 16px 40px;
}