|
13 | 13 | function openMermaidFullscreen(btnEl) { |
14 | 14 | "use strict"; |
15 | 15 |
|
16 | | - // Find SVG: try sibling .mermaid first, fall back to searching the whole page |
| 16 | + // Find SVG: search backwards through siblings, then fall back to page-wide search |
17 | 17 | var svg = null; |
18 | | - var mermaidEl = btnEl.previousElementSibling; |
19 | | - while (mermaidEl) { |
20 | | - if (mermaidEl.classList && mermaidEl.classList.contains("mermaid")) { |
21 | | - svg = mermaidEl.querySelector("svg"); |
| 18 | + |
| 19 | + // Strategy 1: previous sibling with class "mermaid" |
| 20 | + var el = btnEl.previousElementSibling; |
| 21 | + while (el) { |
| 22 | + if (el.classList && el.classList.contains("mermaid")) { |
| 23 | + svg = el.querySelector("svg"); |
22 | 24 | break; |
23 | 25 | } |
24 | | - mermaidEl = mermaidEl.previousElementSibling; |
| 26 | + el = el.previousElementSibling; |
25 | 27 | } |
26 | | - // Fallback: find any .mermaid SVG on the page |
| 28 | + |
| 29 | + // Strategy 2: previous sibling that IS an SVG or contains one |
27 | 30 | if (!svg) { |
28 | | - var allMermaid = document.querySelectorAll(".mermaid svg"); |
29 | | - if (allMermaid.length > 0) svg = allMermaid[0]; |
| 31 | + el = btnEl.previousElementSibling; |
| 32 | + while (el) { |
| 33 | + if (el.tagName === "SVG") { svg = el; break; } |
| 34 | + var nested = el.querySelector("svg"); |
| 35 | + if (nested) { svg = nested; break; } |
| 36 | + el = el.previousElementSibling; |
| 37 | + } |
30 | 38 | } |
| 39 | + |
| 40 | + // Strategy 3: any SVG in .md-content |
| 41 | + if (!svg) { |
| 42 | + var content = document.querySelector(".md-content"); |
| 43 | + if (content) svg = content.querySelector("svg"); |
| 44 | + } |
| 45 | + |
31 | 46 | if (!svg) { |
32 | | - btnEl.textContent = "\u26A0 Diagram not ready \u2014 try again"; |
33 | | - setTimeout(function () { btnEl.textContent = "\u2922 Fullscreen"; }, 2000); |
| 47 | + // Debug: show what the previous sibling actually is |
| 48 | + var prev = btnEl.previousElementSibling; |
| 49 | + var info = prev ? (prev.tagName + "." + prev.className) : "none"; |
| 50 | + var svgCount = document.querySelectorAll("svg").length; |
| 51 | + btnEl.textContent = "\u26A0 SVG not found (prev: " + info + ", svgs: " + svgCount + ")"; |
| 52 | + setTimeout(function () { btnEl.textContent = "\u2922 Fullscreen"; }, 4000); |
34 | 53 | return; |
35 | 54 | } |
36 | 55 |
|
|
0 commit comments