| 1 | (function() { |
| 2 | var state = window.__antaMermaidZoom || {} |
| 3 | window.__antaMermaidZoom = state |
| 4 | |
| 5 | function getMermaidSource(sourceElement) { |
| 6 | if (sourceElement instanceof HTMLTextAreaElement) { |
| 7 | return sourceElement.value.trim() |
| 8 | } |
| 9 | |
| 10 | if (sourceElement instanceof HTMLTemplateElement) { |
| 11 | return sourceElement.content.textContent.trim() |
| 12 | } |
| 13 | |
| 14 | return sourceElement.textContent.trim() |
| 15 | } |
| 16 | |
| 17 | function findMermaidDiagram(sourceElement) { |
| 18 | var element = sourceElement.nextElementSibling |
| 19 | |
| 20 | while (element) { |
| 21 | if (element.classList.contains("mermaid")) { |
| 22 | return element |
| 23 | } |
| 24 | |
| 25 | var nestedDiagram = element.querySelector(".mermaid") |
| 26 | if (nestedDiagram) { |
| 27 | return nestedDiagram |
| 28 | } |
| 29 | |
| 30 | element = element.nextElementSibling |
| 31 | } |
| 32 | |
| 33 | return null |
| 34 | } |
| 35 | |
| 36 | function getMermaidClickTargets(source) { |
| 37 | var clickTargets = [] |
| 38 | var clickPattern = /^\s*click\s+([A-Za-z0-9_-]+)/gm |
| 39 | var match |
| 40 | |
| 41 | while ((match = clickPattern.exec(source)) !== null) { |
| 42 | clickTargets.push(match[1]) |
| 43 | } |
| 44 | |
| 45 | return clickTargets |
| 46 | } |
| 47 | |
| 48 | function isLinkClick(event) { |
| 49 | var path = typeof event.composedPath === "function" ? event.composedPath() : [] |
| 50 | |
| 51 | if (!path.length && event.target) { |
| 52 | path = [event.target] |
| 53 | } |
| 54 | |
| 55 | return path.some(function(target) { |
| 56 | if (!(target instanceof Element)) { |
| 57 | return false |
| 58 | } |
| 59 | |
| 60 | return Boolean(target.closest("a")) |
| 61 | }) |
| 62 | } |
| 63 | |
| 64 | function createZoomButton(source, title, sourceElement, diagram) { |
| 65 | var button = document.createElement("button") |
| 66 | button.className = "mermaid-zoom-open" |
| 67 | button.type = "button" |
| 68 | button.textContent = "Zoom" |
| 69 | button.setAttribute("aria-label", "Open " + title + " full screen") |
| 70 | button.title = "Open full screen" |
| 71 | button.addEventListener("click", function() { |
| 72 | openMermaidZoom(source, title, button) |
| 73 | }) |
| 74 | diagram.insertAdjacentElement("beforebegin", button) |
| 75 | sourceElement.zoomButton = button |
| 76 | } |
| 77 | |
| 78 | function initializeMermaidZoom() { |
| 79 | document.querySelectorAll(".mermaid-zoom-source").forEach(function(sourceElement) { |
| 80 | var diagram = findMermaidDiagram(sourceElement) |
| 81 | |
| 82 | if (!diagram) { |
| 83 | return |
| 84 | } |
| 85 | |
| 86 | if (sourceElement.dataset.zoomReady && sourceElement.zoomDiagram === diagram) { |
| 87 | return |
| 88 | } |
| 89 | |
| 90 | var source = getMermaidSource(sourceElement) |
| 91 | var title = sourceElement.dataset.title || "Mermaid diagram" |
| 92 | var clickTargets = getMermaidClickTargets(source) |
| 93 | var hasNavigationTargets = clickTargets.length > 0 |
| 94 | |
| 95 | if (sourceElement.zoomButton) { |
| 96 | sourceElement.zoomButton.remove() |
| 97 | } |
| 98 | |
| 99 | sourceElement.dataset.zoomReady = "true" |
| 100 | sourceElement.zoomDiagram = diagram |
| 101 | diagram.dataset.zoomReady = "true" |
| 102 | diagram.classList.add("mermaid-zoom-target") |
| 103 | createZoomButton(source, title, sourceElement, diagram) |
| 104 | |
| 105 | if (!hasNavigationTargets) { |
| 106 | diagram.classList.add("mermaid-zoom-target--clickable") |
| 107 | diagram.tabIndex = 0 |
| 108 | diagram.setAttribute("role", "button") |
| 109 | diagram.setAttribute("aria-label", "Open " + title + " full screen") |
| 110 | diagram.title = "Open full screen" |
| 111 | |
| 112 | diagram.addEventListener("click", function(event) { |
| 113 | if (isLinkClick(event)) { |
| 114 | return |
| 115 | } |
| 116 | |
| 117 | openMermaidZoom(source, title, diagram) |
| 118 | }) |
| 119 | |
| 120 | diagram.addEventListener("keydown", function(event) { |
| 121 | if (isLinkClick(event)) { |
| 122 | return |
| 123 | } |
| 124 | |
| 125 | if (event.key !== "Enter" && event.key !== " ") { |
| 126 | return |
| 127 | } |
| 128 | |
| 129 | event.preventDefault() |
| 130 | openMermaidZoom(source, title, diagram) |
| 131 | }) |
| 132 | } |
| 133 | }) |
| 134 | } |
| 135 | |
| 136 | function scheduleMermaidZoomInit() { |
| 137 | initializeMermaidZoom() |
| 138 | window.requestAnimationFrame(initializeMermaidZoom) |
| 139 | window.setTimeout(initializeMermaidZoom, 100) |
| 140 | } |
| 141 | |
| 142 | function waitForMermaid() { |
| 143 | return new Promise(function(resolve, reject) { |
| 144 | var attempts = 0 |
| 145 | |
| 146 | function check() { |
| 147 | if (window.mermaid && typeof window.mermaid.render === "function") { |
| 148 | resolve(window.mermaid) |
| 149 | return |
| 150 | } |
| 151 | attempts += 1 |
| 152 | if (attempts > 100) { |
| 153 | reject(new Error("Mermaid renderer is not available")) |
| 154 | return |
| 155 | } |
| 156 | window.setTimeout(check, 100) |
| 157 | } |
| 158 | |
| 159 | check() |
| 160 | }) |
| 161 | } |
| 162 | |
| 163 | function enableDragScroll(viewport) { |
| 164 | var active = false |
| 165 | var activePointerId = null |
| 166 | var startX = 0 |
| 167 | var startY = 0 |
| 168 | var scrollLeft = 0 |
| 169 | var scrollTop = 0 |
| 170 | |
| 171 | function isInteractiveTarget(event) { |
| 172 | return event.target instanceof Element && Boolean(event.target.closest("a, button, input, select, textarea, [role='button']")) |
| 173 | } |
| 174 | |
| 175 | viewport.addEventListener("pointerdown", function(event) { |
| 176 | if (event.button !== 0 || isInteractiveTarget(event)) { |
| 177 | return |
| 178 | } |
| 179 | |
| 180 | active = true |
| 181 | activePointerId = event.pointerId |
| 182 | startX = event.clientX |
| 183 | startY = event.clientY |
| 184 | scrollLeft = viewport.scrollLeft |
| 185 | scrollTop = viewport.scrollTop |
| 186 | viewport.setPointerCapture(event.pointerId) |
| 187 | }) |
| 188 | |
| 189 | viewport.addEventListener("pointermove", function(event) { |
| 190 | if (!active || event.pointerId !== activePointerId) { |
| 191 | return |
| 192 | } |
| 193 | viewport.scrollLeft = scrollLeft - event.clientX + startX |
| 194 | viewport.scrollTop = scrollTop - event.clientY + startY |
| 195 | }) |
| 196 | |
| 197 | viewport.addEventListener("pointerup", function(event) { |
| 198 | if (!active || event.pointerId !== activePointerId) { |
| 199 | return |
| 200 | } |
| 201 | |
| 202 | active = false |
| 203 | activePointerId = null |
| 204 | viewport.releasePointerCapture(event.pointerId) |
| 205 | }) |
| 206 | |
| 207 | viewport.addEventListener("pointercancel", function(event) { |
| 208 | if (!active || event.pointerId !== activePointerId) { |
| 209 | return |
| 210 | } |
| 211 | |
| 212 | active = false |
| 213 | activePointerId = null |
| 214 | }) |
| 215 | } |
| 216 | |
| 217 | function openMermaidZoom(source, title, invokingElement) { |
| 218 | if (state.pageLeaving) { |
| 219 | return |
| 220 | } |
| 221 | |
| 222 | if (typeof state.closeActiveMermaidZoom === "function") { |
| 223 | state.closeActiveMermaidZoom(false) |
| 224 | } |
| 225 | |
| 226 | waitForMermaid().then(function(mermaid) { |
| 227 | if (state.pageLeaving) { |
| 228 | return |
| 229 | } |
| 230 | |
| 231 | var overlay = document.createElement("div") |
| 232 | var panel = document.createElement("div") |
| 233 | var toolbar = document.createElement("div") |
| 234 | var titleElement = document.createElement("div") |
| 235 | var viewport = document.createElement("div") |
| 236 | var content = document.createElement("div") |
| 237 | var scale = 1 |
| 238 | var closed = false |
| 239 | var titleId = "__mermaid_zoom_title_" + Date.now() |
| 240 | |
| 241 | function makeButton(label, action) { |
| 242 | var button = document.createElement("button") |
| 243 | button.className = "mermaid-zoom-control" |
| 244 | button.type = "button" |
| 245 | button.textContent = label |
| 246 | button.addEventListener("click", action) |
| 247 | return button |
| 248 | } |
| 249 | |
| 250 | function applyScale() { |
| 251 | content.style.transform = "scale(" + scale + ")" |
| 252 | content.style.width = 100 / scale + "%" |
| 253 | } |
| 254 | |
| 255 | function getToolbarControls() { |
| 256 | return Array.prototype.slice.call(panel.querySelectorAll( |
| 257 | "a[href], button:not([disabled]), input:not([disabled]), select:not([disabled]), textarea:not([disabled]), [tabindex]:not([tabindex='-1'])" |
| 258 | )).filter(function(element) { |
| 259 | return element.offsetParent !== null |
| 260 | }) |
| 261 | } |
| 262 | |
| 263 | function closeZoom(restoreFocus) { |
| 264 | if (closed) { |
| 265 | return |
| 266 | } |
| 267 | |
| 268 | if (restoreFocus !== false) { |
| 269 | restoreFocus = true |
| 270 | } |
| 271 | |
| 272 | closed = true |
| 273 | state.closeActiveMermaidZoom = null |
| 274 | overlay.removeEventListener("click", closeOnBackdrop) |
| 275 | document.removeEventListener("keydown", handleOverlayKeydown) |
| 276 | overlay.remove() |
| 277 | |
| 278 | if (restoreFocus && invokingElement && invokingElement.isConnected && typeof invokingElement.focus === "function") { |
| 279 | invokingElement.focus() |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | function closeOnBackdrop(event) { |
| 284 | if (event.target === overlay) { |
| 285 | closeZoom() |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | function handleOverlayKeydown(event) { |
| 290 | if (event.key === "Escape") { |
| 291 | event.preventDefault() |
| 292 | closeZoom() |
| 293 | return |
| 294 | } |
| 295 | |
| 296 | if (event.key !== "Tab") { |
| 297 | return |
| 298 | } |
| 299 | |
| 300 | var toolbarControls = getToolbarControls() |
| 301 | |
| 302 | if (!toolbarControls.length) { |
| 303 | event.preventDefault() |
| 304 | panel.focus() |
| 305 | return |
| 306 | } |
| 307 | |
| 308 | var firstElement = toolbarControls[0] |
| 309 | var lastElement = toolbarControls[toolbarControls.length - 1] |
| 310 | |
| 311 | if (!panel.contains(document.activeElement)) { |
| 312 | event.preventDefault() |
| 313 | if (event.shiftKey) { |
| 314 | lastElement.focus() |
| 315 | } else { |
| 316 | firstElement.focus() |
| 317 | } |
| 318 | } else if (event.shiftKey && document.activeElement === firstElement) { |
| 319 | event.preventDefault() |
| 320 | lastElement.focus() |
| 321 | } else if (!event.shiftKey && document.activeElement === lastElement) { |
| 322 | event.preventDefault() |
| 323 | firstElement.focus() |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | overlay.className = "mermaid-zoom-overlay" |
| 328 | panel.className = "mermaid-zoom-panel" |
| 329 | toolbar.className = "mermaid-zoom-toolbar" |
| 330 | titleElement.className = "mermaid-zoom-title" |
| 331 | viewport.className = "mermaid-zoom-viewport" |
| 332 | content.className = "mermaid-zoom-content" |
| 333 | |
| 334 | panel.tabIndex = -1 |
| 335 | panel.setAttribute("role", "dialog") |
| 336 | panel.setAttribute("aria-modal", "true") |
| 337 | panel.setAttribute("aria-labelledby", titleId) |
| 338 | titleElement.id = titleId |
| 339 | titleElement.textContent = title || "Mermaid diagram" |
| 340 | toolbar.append( |
| 341 | titleElement, |
| 342 | makeButton("-", function() { |
| 343 | scale = Math.max(.5, scale - .25) |
| 344 | applyScale() |
| 345 | }), |
| 346 | makeButton("+", function() { |
| 347 | scale = Math.min(4, scale + .25) |
| 348 | applyScale() |
| 349 | }), |
| 350 | makeButton("100%", function() { |
| 351 | scale = 1 |
| 352 | applyScale() |
| 353 | }), |
| 354 | makeButton("Close", function() { |
| 355 | closeZoom() |
| 356 | }), |
| 357 | ) |
| 358 | viewport.append(content) |
| 359 | panel.append(toolbar, viewport) |
| 360 | overlay.append(panel) |
| 361 | document.body.append(overlay) |
| 362 | state.closeActiveMermaidZoom = closeZoom |
| 363 | |
| 364 | overlay.addEventListener("click", closeOnBackdrop) |
| 365 | document.addEventListener("keydown", handleOverlayKeydown) |
| 366 | |
| 367 | var initialFocusElement = toolbar.querySelector("button:not([disabled])") |
| 368 | if (initialFocusElement) { |
| 369 | initialFocusElement.focus() |
| 370 | } else { |
| 371 | panel.focus() |
| 372 | } |
| 373 | |
| 374 | mermaid.render("__mermaid_zoom_" + Date.now(), source).then(function(result) { |
| 375 | content.innerHTML = result.svg |
| 376 | if (typeof result.bindFunctions === "function") { |
| 377 | result.bindFunctions(content) |
| 378 | } else if (typeof result.fn === "function") { |
| 379 | result.fn(content) |
| 380 | } |
| 381 | applyScale() |
| 382 | enableDragScroll(viewport) |
| 383 | }).catch(function(error) { |
| 384 | content.textContent = "Unable to render diagram." |
| 385 | console.error("Mermaid zoom render failed:", error) |
| 386 | }) |
| 387 | }).catch(function(error) { |
| 388 | console.error("Mermaid zoom failed to initialize:", error) |
| 389 | }) |
| 390 | } |
| 391 | |
| 392 | if (!state.documentSubscriptionInstalled && window.document$) { |
| 393 | state.documentSubscriptionInstalled = true |
| 394 | window.document$.subscribe(function() { |
| 395 | if (typeof state.closeActiveMermaidZoom === "function") { |
| 396 | state.closeActiveMermaidZoom(false) |
| 397 | } |
| 398 | scheduleMermaidZoomInit() |
| 399 | }) |
| 400 | } |
| 401 | |
| 402 | if (!state.pageHideListenerInstalled) { |
| 403 | state.pageHideListenerInstalled = true |
| 404 | window.addEventListener("pageshow", function() { |
| 405 | state.pageLeaving = false |
| 406 | }) |
| 407 | window.addEventListener("pagehide", function() { |
| 408 | state.pageLeaving = true |
| 409 | if (typeof state.closeActiveMermaidZoom === "function") { |
| 410 | state.closeActiveMermaidZoom(false) |
| 411 | } |
| 412 | }) |
| 413 | } |
| 414 | |
| 415 | if (!state.domReadyListenerInstalled) { |
| 416 | state.domReadyListenerInstalled = true |
| 417 | if (document.readyState === "loading") { |
| 418 | document.addEventListener("DOMContentLoaded", scheduleMermaidZoomInit) |
| 419 | } else { |
| 420 | scheduleMermaidZoomInit() |
| 421 | } |
| 422 | } else { |
| 423 | scheduleMermaidZoomInit() |
| 424 | } |
| 425 | })() |