Debian Codegraph @

docs/_static/mermaid-reset.js

raw
1 document.addEventListener("DOMContentLoaded", () => {
2 const isDarkTheme = () => {
3 if (
4 document.documentElement.classList.contains("dark") ||
5 document.documentElement.getAttribute("data-theme") === "dark" ||
6 document.body.classList.contains("dark") ||
7 document.body.getAttribute("data-theme") === "dark"
8 ) {
9 return true;
10 }
11 if (
12 document.documentElement.classList.contains("light") ||
13 document.documentElement.getAttribute("data-theme") === "light" ||
14 document.body.classList.contains("light") ||
15 document.body.getAttribute("data-theme") === "light"
16 ) {
17 return false;
18 }
19 if (
20 window.matchMedia &&
21 window.matchMedia("(prefers-color-scheme: dark)").matches
22 ) {
23 return true;
24 }
25 const bgColor = window.getComputedStyle(document.body).backgroundColor;
26 const match = bgColor.match(/rgb\((\d+),\s*(\d+),\s*(\d+)/);
27 if (match) {
28 const r = parseInt(match[1]);
29 const g = parseInt(match[2]);
30 const b = parseInt(match[3]);
31 const brightness = (r * 299 + g * 587 + b * 114) / 1000;
32 return brightness < 128;
33 }
34 return false;
35 };
36
37 const addResetButtons = () => {
38 document.querySelectorAll(".mermaid").forEach((diagram) => {
39 if (diagram.querySelector(".reset-zoom-btn")) return;
40 const svg = diagram.querySelector("svg");
41 if (!svg) return;
42 const resetBtn = document.createElement("button");
43 resetBtn.className = "reset-zoom-btn";
44 const darkMode = isDarkTheme();
45 if (darkMode) {
46 resetBtn.classList.add("dark-theme");
47 }
48 resetBtn.innerHTML = "↻";
49 resetBtn.title = "Reset zoom and pan";
50 const diagramStyle = window.getComputedStyle(diagram);
51 const marginTop = parseFloat(diagramStyle.marginTop) || 0;
52 const marginRight = parseFloat(diagramStyle.marginRight) || 0;
53 const paddingTop = parseFloat(diagramStyle.paddingTop) || 0;
54 const paddingRight = parseFloat(diagramStyle.paddingRight) || 0;
55 resetBtn.style.cssText = `
56 position: absolute;
57 top: ${marginTop + paddingTop}px;
58 right: ${marginRight + paddingRight + 10}px;
59 width: 28px;
60 height: 28px;
61 background: ${darkMode ? "rgba(50, 50, 50, 0.95)" : "rgba(255, 255, 255, 0.95)"};
62 border: 1px solid ${darkMode ? "rgba(255, 255, 255, 0.3)" : "rgba(0, 0, 0, 0.3)"};
63 border-radius: 4px;
64 cursor: pointer;
65 font-size: 14px;
66 line-height: 1;
67 padding: 0;
68 color: ${darkMode ? "#e0e0e0" : "#333"};
69 z-index: 1000;
70 display: flex;
71 align-items: center;
72 justify-content: center;
73 transition: all 0.2s;
74 box-shadow: 0 2px 6px ${darkMode ? "rgba(255, 255, 255, 0.2)" : "rgba(0, 0, 0, 0.2)"};
75 `;
76 resetBtn.onmouseover = () => {
77 resetBtn.style.opacity = "100%";
78 resetBtn.style.background = darkMode
79 ? "rgba(60, 60, 60, 1)"
80 : "rgba(255, 255, 255, 1)";
81 resetBtn.style.boxShadow = darkMode
82 ? "0 3px 10px rgba(255, 255, 255, 0.2)"
83 : "0 3px 10px rgba(0, 0, 0, 0.3)";
84 resetBtn.style.transform = "scale(1.1)";
85 };
86 resetBtn.onmouseout = () => {
87 resetBtn.style.opacity = "";
88 resetBtn.style.background = darkMode
89 ? "rgba(50, 50, 50, 0.95)"
90 : "rgba(255, 255, 255, 0.95)";
91 resetBtn.style.boxShadow = darkMode
92 ? "0 2px 6px rgba(255, 255, 255, 0.2)"
93 : "0 2px 6px rgba(0, 0, 0, 0.2)";
94 resetBtn.style.transform = "";
95 };
96 resetBtn.onclick = () => {
97 const gElement = svg.querySelector("g");
98 if (gElement) {
99 gElement.removeAttribute("transform");
100 const zoom = svg.__zoom;
101 if (zoom) {
102 zoom.k = 1;
103 zoom.x = 0;
104 zoom.y = 0;
105 }
106 }
107 };
108 diagram.style.position = "relative";
109 diagram.appendChild(resetBtn);
110 });
111 };
112 setTimeout(addResetButtons, 100);
113 const observer = new MutationObserver(() => setTimeout(addResetButtons, 100));
114 observer.observe(document.body, { childList: true, subtree: true });
115 });