|
| 1 | +(() => { |
| 2 | + function attachPanZoom(svg) { |
| 3 | + if (!svg || svg.dataset.panzoomAttached === "true") { |
| 4 | + return; |
| 5 | + } |
| 6 | + |
| 7 | + const container = svg.closest(".mermaid-container-fullscreen"); |
| 8 | + if (!container) { |
| 9 | + return; |
| 10 | + } |
| 11 | + |
| 12 | + svg.dataset.panzoomAttached = "true"; |
| 13 | + |
| 14 | + // Keep panning predictable inside fullscreen modal. |
| 15 | + container.style.overflow = "hidden"; |
| 16 | + container.style.touchAction = "none"; |
| 17 | + |
| 18 | + let scale = 1; |
| 19 | + let tx = 0; |
| 20 | + let ty = 0; |
| 21 | + let dragging = false; |
| 22 | + let dragOffsetX = 0; |
| 23 | + let dragOffsetY = 0; |
| 24 | + |
| 25 | + const minScale = 0.25; |
| 26 | + const maxScale = 6; |
| 27 | + |
| 28 | + const applyTransform = () => { |
| 29 | + svg.style.transformOrigin = "0 0"; |
| 30 | + svg.style.transform = `translate(${tx}px, ${ty}px) scale(${scale})`; |
| 31 | + }; |
| 32 | + |
| 33 | + const clamp = (value, min, max) => Math.min(max, Math.max(min, value)); |
| 34 | + |
| 35 | + applyTransform(); |
| 36 | + svg.style.cursor = "grab"; |
| 37 | + |
| 38 | + svg.addEventListener( |
| 39 | + "wheel", |
| 40 | + (event) => { |
| 41 | + event.preventDefault(); |
| 42 | + |
| 43 | + const rect = container.getBoundingClientRect(); |
| 44 | + const cx = event.clientX - rect.left; |
| 45 | + const cy = event.clientY - rect.top; |
| 46 | + |
| 47 | + const factor = event.deltaY < 0 ? 1.12 : 0.88; |
| 48 | + const nextScale = clamp(scale * factor, minScale, maxScale); |
| 49 | + |
| 50 | + // Zoom around cursor position. |
| 51 | + tx = cx - ((cx - tx) * nextScale) / scale; |
| 52 | + ty = cy - ((cy - ty) * nextScale) / scale; |
| 53 | + scale = nextScale; |
| 54 | + |
| 55 | + applyTransform(); |
| 56 | + }, |
| 57 | + { passive: false } |
| 58 | + ); |
| 59 | + |
| 60 | + svg.addEventListener("pointerdown", (event) => { |
| 61 | + if (event.button !== 0) { |
| 62 | + return; |
| 63 | + } |
| 64 | + dragging = true; |
| 65 | + dragOffsetX = event.clientX - tx; |
| 66 | + dragOffsetY = event.clientY - ty; |
| 67 | + svg.style.cursor = "grabbing"; |
| 68 | + if (svg.setPointerCapture) { |
| 69 | + svg.setPointerCapture(event.pointerId); |
| 70 | + } |
| 71 | + event.preventDefault(); |
| 72 | + }); |
| 73 | + |
| 74 | + svg.addEventListener("pointermove", (event) => { |
| 75 | + if (!dragging) { |
| 76 | + return; |
| 77 | + } |
| 78 | + tx = event.clientX - dragOffsetX; |
| 79 | + ty = event.clientY - dragOffsetY; |
| 80 | + applyTransform(); |
| 81 | + }); |
| 82 | + |
| 83 | + const stopDragging = (event) => { |
| 84 | + if (!dragging) { |
| 85 | + return; |
| 86 | + } |
| 87 | + dragging = false; |
| 88 | + svg.style.cursor = "grab"; |
| 89 | + if (event && svg.releasePointerCapture) { |
| 90 | + try { |
| 91 | + svg.releasePointerCapture(event.pointerId); |
| 92 | + } catch { |
| 93 | + // No-op: pointer may already be released. |
| 94 | + } |
| 95 | + } |
| 96 | + }; |
| 97 | + |
| 98 | + svg.addEventListener("pointerup", stopDragging); |
| 99 | + svg.addEventListener("pointercancel", stopDragging); |
| 100 | + svg.addEventListener("pointerleave", stopDragging); |
| 101 | + |
| 102 | + // Double-click to reset view. |
| 103 | + svg.addEventListener("dblclick", (event) => { |
| 104 | + event.preventDefault(); |
| 105 | + scale = 1; |
| 106 | + tx = 0; |
| 107 | + ty = 0; |
| 108 | + applyTransform(); |
| 109 | + }); |
| 110 | + } |
| 111 | + |
| 112 | + function initFullscreenPanZoom() { |
| 113 | + const installForActiveModal = () => { |
| 114 | + document |
| 115 | + .querySelectorAll(".mermaid-fullscreen-modal.active .mermaid svg") |
| 116 | + .forEach(attachPanZoom); |
| 117 | + }; |
| 118 | + |
| 119 | + const observer = new MutationObserver(() => { |
| 120 | + installForActiveModal(); |
| 121 | + }); |
| 122 | + |
| 123 | + observer.observe(document.body, { |
| 124 | + subtree: true, |
| 125 | + childList: true, |
| 126 | + attributes: true, |
| 127 | + attributeFilter: ["class"], |
| 128 | + }); |
| 129 | + |
| 130 | + document.addEventListener("click", (event) => { |
| 131 | + if (!event.target.closest(".mermaid-fullscreen-btn")) { |
| 132 | + return; |
| 133 | + } |
| 134 | + // Wait for extension script to clone and insert the SVG into modal. |
| 135 | + requestAnimationFrame(() => { |
| 136 | + requestAnimationFrame(installForActiveModal); |
| 137 | + }); |
| 138 | + }); |
| 139 | + |
| 140 | + installForActiveModal(); |
| 141 | + } |
| 142 | + |
| 143 | + if (document.readyState === "loading") { |
| 144 | + document.addEventListener("DOMContentLoaded", initFullscreenPanZoom); |
| 145 | + } else { |
| 146 | + initFullscreenPanZoom(); |
| 147 | + } |
| 148 | +})(); |
0 commit comments