Skip to content

Commit 07b537b

Browse files
authored
Merge pull request #689 from KaplanOpenSource/issue641
Fix Mermaid expand button detection
2 parents 5ca4f50 + 606f881 commit 07b537b

1 file changed

Lines changed: 42 additions & 19 deletions

File tree

docs/overrides/javascripts/mermaid-zoom.js

Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,91 @@
11
// Mermaid diagram expand button
2-
// Polls for rendered SVGs inside .mermaid elements, then adds an Expand button.
2+
// Polls DOM for SVGs rendered by Mermaid and adds an Expand button.
33

44
(function () {
55
function addButtons() {
6-
document.querySelectorAll("pre.mermaid svg, .mermaid svg").forEach(function (svg) {
6+
// Find all SVGs on the page that look like Mermaid diagrams
7+
document.querySelectorAll("svg").forEach(function (svg) {
8+
// Skip if already processed
9+
if (svg.getAttribute("data-zoom-done")) return;
10+
// Only target Mermaid diagrams (they have aria-roledescription or class containing mermaid-related ids)
11+
var isMermaid = svg.getAttribute("aria-roledescription") === "classDiagram" ||
12+
svg.getAttribute("aria-roledescription") === "flowchart-v2" ||
13+
svg.getAttribute("aria-roledescription") === "flowchart" ||
14+
svg.getAttribute("aria-roledescription") === "sequence" ||
15+
svg.getAttribute("aria-roledescription") === "stateDiagram" ||
16+
svg.getAttribute("aria-roledescription") ||
17+
(svg.id && svg.id.startsWith("mermaid-")) ||
18+
(svg.parentElement && svg.parentElement.classList.contains("mermaid"));
19+
if (!isMermaid) return;
20+
21+
svg.setAttribute("data-zoom-done", "true");
22+
723
var container = svg.parentElement;
8-
if (!container || container.getAttribute("data-zoom-done")) return;
9-
container.setAttribute("data-zoom-done", "true");
24+
if (!container) return;
1025
container.style.position = "relative";
1126

1227
// Expand button
1328
var btn = document.createElement("button");
1429
btn.textContent = "Expand";
1530
btn.title = "View diagram fullscreen";
16-
btn.style.cssText = "position:absolute;top:4px;right:4px;z-index:10;" +
17-
"background:#4051b5;color:#fff;border:none;border-radius:4px;" +
18-
"padding:4px 10px;font-size:12px;cursor:pointer;opacity:0.8;";
31+
Object.assign(btn.style, {
32+
position: "absolute", top: "4px", right: "4px", zIndex: "10",
33+
background: "#4051b5", color: "#fff", border: "none", borderRadius: "4px",
34+
padding: "4px 10px", fontSize: "12px", cursor: "pointer", opacity: "0.8"
35+
});
1936
btn.onmouseenter = function () { btn.style.opacity = "1"; };
2037
btn.onmouseleave = function () { btn.style.opacity = "0.8"; };
2138
container.appendChild(btn);
2239

2340
// Fullscreen overlay
2441
var overlay = document.createElement("div");
25-
overlay.style.cssText = "display:none;position:fixed;top:0;left:0;width:100vw;" +
26-
"height:100vh;z-index:9999;background:rgba(255,255,255,0.97);" +
27-
"align-items:center;justify-content:center;padding:2rem;" +
28-
"box-sizing:border-box;overflow:auto;cursor:zoom-out;flex-direction:column;";
42+
Object.assign(overlay.style, {
43+
display: "none", position: "fixed", top: "0", left: "0",
44+
width: "100vw", height: "100vh", zIndex: "9999",
45+
background: "rgba(255,255,255,0.97)", alignItems: "center",
46+
justifyContent: "center", padding: "2rem", boxSizing: "border-box",
47+
overflow: "auto", cursor: "zoom-out", flexDirection: "column"
48+
});
2949
var hint = document.createElement("div");
3050
hint.textContent = "Click anywhere or press Escape to close";
31-
hint.style.cssText = "position:fixed;bottom:1rem;left:50%;transform:translateX(-50%);" +
32-
"font-size:14px;color:#666;";
51+
Object.assign(hint.style, {
52+
position: "fixed", bottom: "1rem", left: "50%",
53+
transform: "translateX(-50%)", fontSize: "14px", color: "#666"
54+
});
3355
overlay.appendChild(hint);
3456
document.body.appendChild(overlay);
3557

36-
// Open
3758
btn.addEventListener("click", function (e) {
3859
e.preventDefault();
3960
e.stopPropagation();
4061
overlay.querySelectorAll("svg").forEach(function (s) { s.remove(); });
4162
var clone = svg.cloneNode(true);
42-
clone.style.cssText = "max-width:95vw;max-height:85vh;width:auto;height:auto;";
4363
clone.removeAttribute("width");
4464
clone.removeAttribute("height");
65+
clone.removeAttribute("style");
66+
Object.assign(clone.style, {
67+
maxWidth: "95vw", maxHeight: "85vh", width: "auto", height: "auto"
68+
});
4569
overlay.insertBefore(clone, hint);
4670
overlay.style.display = "flex";
4771
document.body.style.overflow = "hidden";
4872
});
4973

50-
// Close
5174
overlay.addEventListener("click", function () {
5275
overlay.style.display = "none";
5376
document.body.style.overflow = "";
5477
});
5578
});
5679
}
5780

58-
// Poll for Mermaid SVGs (rendered asynchronously)
81+
// Poll every 500ms for up to 20 seconds (Mermaid renders async)
5982
var tries = 0;
6083
var timer = setInterval(function () {
6184
addButtons();
62-
if (++tries > 30) clearInterval(timer);
85+
if (++tries > 40) clearInterval(timer);
6386
}, 500);
6487

65-
// Escape to close all overlays
88+
// Escape to close
6689
document.addEventListener("keydown", function (e) {
6790
if (e.key === "Escape") {
6891
document.querySelectorAll("div").forEach(function (el) {

0 commit comments

Comments
 (0)