File size: 9,394 Bytes
96dd062 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 | (() => {
// 单例模式:检查是否已经初始化过
if (window.mermaidInitialized) {
return;
}
window.mermaidInitialized = true;
// 记录当前主题状态,避免不必要的重新渲染
let currentTheme = null;
let isRendering = false; // 防止并发渲染
let retryCount = 0;
const MAX_RETRIES = 3;
const RETRY_DELAY = 1000; // 1秒
// 检查主题是否真的发生了变化
function hasThemeChanged() {
const isDark = document.documentElement.classList.contains("dark");
const newTheme = isDark ? "dark" : "default";
if (currentTheme !== newTheme) {
currentTheme = newTheme;
return true;
}
return false;
}
// 等待 Mermaid 库加载完成
function waitForMermaid(timeout = 10000) {
return new Promise((resolve, reject) => {
const startTime = Date.now();
function check() {
if (window.mermaid && typeof window.mermaid.initialize === "function") {
resolve(window.mermaid);
} else if (Date.now() - startTime > timeout) {
reject(new Error("Mermaid library failed to load within timeout"));
} else {
setTimeout(check, 100);
}
}
check();
});
}
// 设置 MutationObserver 监听 html 元素的 class 属性变化
function setupMutationObserver() {
const observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
if (
mutation.type === "attributes" &&
mutation.attributeName === "class"
) {
// 检查是否是 dark 类的变化
const target = mutation.target;
const wasDark = mutation.oldValue
? mutation.oldValue.includes("dark")
: false;
const isDark = target.classList.contains("dark");
if (wasDark !== isDark) {
if (hasThemeChanged()) {
// 延迟渲染,避免主题切换时的闪烁
setTimeout(() => renderMermaidDiagrams(), 150);
}
}
}
});
});
// 开始观察 html 元素的 class 属性变化
observer.observe(document.documentElement, {
attributes: true,
attributeFilter: ["class"],
attributeOldValue: true,
});
}
// 设置其他事件监听器
function setupEventListeners() {
// 监听页面切换
document.addEventListener("astro:page-load", () => {
// 重新初始化主题状态
currentTheme = null;
retryCount = 0; // 重置重试计数
if (hasThemeChanged()) {
setTimeout(() => renderMermaidDiagrams(), 100);
}
});
// 监听页面可见性变化,页面重新可见时重新渲染
document.addEventListener("visibilitychange", () => {
if (!document.hidden) {
setTimeout(() => renderMermaidDiagrams(), 200);
}
});
}
async function initializeMermaid() {
try {
await waitForMermaid();
// 初始化 Mermaid 配置
window.mermaid.initialize({
startOnLoad: false,
theme: "default",
themeVariables: {
fontFamily: "inherit",
fontSize: "16px",
},
securityLevel: "loose",
// 添加错误处理配置
errorLevel: "warn",
logLevel: "error",
});
// 渲染所有 Mermaid 图表
await renderMermaidDiagrams();
} catch (error) {
console.error("Failed to initialize Mermaid:", error);
// 如果初始化失败,尝试重新加载
if (retryCount < MAX_RETRIES) {
retryCount++;
setTimeout(() => initializeMermaid(), RETRY_DELAY * retryCount);
}
}
}
async function renderMermaidDiagrams() {
// 防止并发渲染
if (isRendering) {
return;
}
// 检查 Mermaid 是否可用
if (!window.mermaid || typeof window.mermaid.render !== "function") {
console.warn("Mermaid not available, skipping render");
return;
}
isRendering = true;
try {
const mermaidElements = document.querySelectorAll(
".mermaid[data-mermaid-code]",
);
if (mermaidElements.length === 0) {
isRendering = false;
return;
}
// 延迟检测主题,确保 DOM 已经更新
await new Promise((resolve) => setTimeout(resolve, 100));
const htmlElement = document.documentElement;
const isDark = htmlElement.classList.contains("dark");
const theme = isDark ? "dark" : "default";
// 更新 Mermaid 主题(只需要更新一次)
window.mermaid.initialize({
startOnLoad: false,
theme: theme,
themeVariables: {
fontFamily: "inherit",
fontSize: "16px",
// 强制应用主题变量
primaryColor: isDark ? "#ffffff" : "#000000",
primaryTextColor: isDark ? "#ffffff" : "#000000",
primaryBorderColor: isDark ? "#ffffff" : "#000000",
lineColor: isDark ? "#ffffff" : "#000000",
secondaryColor: isDark ? "#333333" : "#f0f0f0",
tertiaryColor: isDark ? "#555555" : "#e0e0e0",
},
securityLevel: "loose",
errorLevel: "warn",
logLevel: "error",
});
// 批量渲染所有图表,添加重试机制
const renderPromises = Array.from(mermaidElements).map(
async (element, index) => {
let attempts = 0;
const maxAttempts = 3;
while (attempts < maxAttempts) {
try {
const code = element.getAttribute("data-mermaid-code");
if (!code) {
break;
}
// 显示加载状态
element.innerHTML =
'<div class="mermaid-loading">Rendering diagram...</div>';
// 渲染图表
const { svg } = await window.mermaid.render(
`mermaid-${Date.now()}-${index}-${attempts}`,
code,
);
element.innerHTML = svg;
// 添加响应式支持
const svgElement = element.querySelector("svg");
if (svgElement) {
svgElement.setAttribute("width", "100%");
svgElement.removeAttribute("height");
svgElement.style.maxWidth = "100%";
svgElement.style.height = "auto";
// 强制应用样式
if (isDark) {
svgElement.style.filter = "brightness(0.9) contrast(1.1)";
} else {
svgElement.style.filter = "none";
}
}
// 渲染成功,跳出重试循环
break;
} catch (error) {
attempts++;
console.warn(
`Mermaid rendering attempt ${attempts} failed for element ${index}:`,
error,
);
if (attempts >= maxAttempts) {
console.error(
`Failed to render Mermaid diagram after ${maxAttempts} attempts:`,
error,
);
element.innerHTML = `
<div class="mermaid-error">
<p>Failed to render diagram after ${maxAttempts} attempts.</p>
<button onclick="location.reload()" style="margin-top: 8px; padding: 4px 8px; background: var(--primary); color: white; border: none; border-radius: 4px; cursor: pointer;">
Retry Page
</button>
</div>
`;
} else {
// 等待一段时间后重试
await new Promise((resolve) =>
setTimeout(resolve, 500 * attempts),
);
}
}
}
},
);
// 等待所有渲染完成
await Promise.all(renderPromises);
retryCount = 0; // 重置重试计数
} catch (error) {
console.error("Error in renderMermaidDiagrams:", error);
// 如果渲染失败,尝试重新渲染
if (retryCount < MAX_RETRIES) {
retryCount++;
setTimeout(() => renderMermaidDiagrams(), RETRY_DELAY * retryCount);
}
} finally {
isRendering = false;
}
}
// 初始化主题状态
function initializeThemeState() {
const isDark = document.documentElement.classList.contains("dark");
currentTheme = isDark ? "dark" : "default";
}
// 加载 Mermaid 库
async function loadMermaid() {
if (typeof window.mermaid !== "undefined") {
return Promise.resolve();
}
return new Promise((resolve, reject) => {
const script = document.createElement("script");
script.src =
"https://cdn.jsdelivr.net/npm/mermaid@11/dist/mermaid.min.js";
script.onload = () => {
console.log("Mermaid library loaded successfully");
resolve();
};
script.onerror = (error) => {
console.error("Failed to load Mermaid library:", error);
// 尝试备用 CDN
const fallbackScript = document.createElement("script");
fallbackScript.src = "https://unpkg.com/mermaid@11/dist/mermaid.min.js";
fallbackScript.onload = () => {
console.log("Mermaid library loaded from fallback CDN");
resolve();
};
fallbackScript.onerror = () => {
reject(
new Error(
"Failed to load Mermaid from both primary and fallback CDNs",
),
);
};
document.head.appendChild(fallbackScript);
};
document.head.appendChild(script);
});
}
// 主初始化函数
async function initialize() {
try {
// 设置监听器
setupMutationObserver();
setupEventListeners();
// 初始化主题状态
initializeThemeState();
// 加载并初始化 Mermaid
await loadMermaid();
await initializeMermaid();
} catch (error) {
console.error("Failed to initialize Mermaid system:", error);
}
}
// 启动初始化
if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", initialize);
} else {
initialize();
}
})();
|