Skip to content
This repository was archived by the owner on Sep 23, 2025. It is now read-only.

Commit 04762c0

Browse files
committed
Add click-to-enlarge functionality for Mermaid diagrams
- Create diagram-enlarger.js with modal overlay functionality - Add click handlers to all Mermaid SVG diagrams with zoom-in cursor - Implement full-screen modal with dark backdrop and centered diagram - Support multiple close methods: click anywhere, ESC key, or click outside - Add visual feedback with tooltips and close instructions - Prevent body scrolling while modal is open - Configure responsive sizing (90% viewport) with proper aspect ratio - Add diagram-enlarger.js to book.toml additional-js configuration Sequence diagrams are now easily readable when enlarged, significantly improving documentation UX for complex message flows and architecture diagrams.
1 parent b77dffd commit 04762c0

File tree

2 files changed

+98
-1
lines changed

2 files changed

+98
-1
lines changed

book.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ title = "Dialectic"
88
command = "mdbook-mermaid"
99

1010
[output.html]
11-
additional-js = ["mermaid.min.js", "mermaid-init.js"]
11+
additional-js = ["mermaid.min.js", "mermaid-init.js", "diagram-enlarger.js"]

diagram-enlarger.js

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
// Diagram Enlarger - Click to enlarge Mermaid diagrams in modal overlay
2+
(function() {
3+
'use strict';
4+
5+
// Wait for page to load and Mermaid to render
6+
document.addEventListener('DOMContentLoaded', function() {
7+
// Give Mermaid time to render
8+
setTimeout(initializeDiagramEnlarger, 500);
9+
});
10+
11+
function initializeDiagramEnlarger() {
12+
// Find all Mermaid diagrams
13+
const diagrams = document.querySelectorAll('.mermaid svg');
14+
15+
diagrams.forEach(function(svg) {
16+
// Make diagrams clickable
17+
svg.style.cursor = 'zoom-in';
18+
svg.title = 'Click to enlarge';
19+
20+
svg.addEventListener('click', function(e) {
21+
e.preventDefault();
22+
showEnlargedDiagram(svg);
23+
});
24+
});
25+
}
26+
27+
function showEnlargedDiagram(originalSvg) {
28+
// Create modal overlay
29+
const modal = document.createElement('div');
30+
modal.className = 'diagram-modal';
31+
modal.style.cssText = `
32+
position: fixed;
33+
top: 0;
34+
left: 0;
35+
width: 100%;
36+
height: 100%;
37+
background: rgba(0, 0, 0, 0.8);
38+
display: flex;
39+
justify-content: center;
40+
align-items: center;
41+
z-index: 10000;
42+
cursor: zoom-out;
43+
`;
44+
45+
// Clone the SVG for the modal
46+
const enlargedSvg = originalSvg.cloneNode(true);
47+
enlargedSvg.style.cssText = `
48+
max-width: 90vw;
49+
max-height: 90vh;
50+
background: white;
51+
border-radius: 8px;
52+
box-shadow: 0 4px 20px rgba(0, 0, 0, 0.3);
53+
cursor: zoom-out;
54+
`;
55+
56+
// Add close instruction
57+
const closeHint = document.createElement('div');
58+
closeHint.textContent = 'Click anywhere or press ESC to close';
59+
closeHint.style.cssText = `
60+
position: absolute;
61+
top: 20px;
62+
left: 50%;
63+
transform: translateX(-50%);
64+
color: white;
65+
font-size: 14px;
66+
background: rgba(0, 0, 0, 0.6);
67+
padding: 8px 16px;
68+
border-radius: 4px;
69+
pointer-events: none;
70+
`;
71+
72+
modal.appendChild(enlargedSvg);
73+
modal.appendChild(closeHint);
74+
document.body.appendChild(modal);
75+
76+
// Prevent body scrolling while modal is open
77+
document.body.style.overflow = 'hidden';
78+
79+
// Close modal on click
80+
modal.addEventListener('click', closeDiagramModal);
81+
82+
// Close modal on ESC key
83+
document.addEventListener('keydown', handleEscapeKey);
84+
85+
function closeDiagramModal() {
86+
document.body.removeChild(modal);
87+
document.body.style.overflow = '';
88+
document.removeEventListener('keydown', handleEscapeKey);
89+
}
90+
91+
function handleEscapeKey(e) {
92+
if (e.key === 'Escape') {
93+
closeDiagramModal();
94+
}
95+
}
96+
}
97+
})();

0 commit comments

Comments
 (0)