Skip to content

Commit 01e0f4f

Browse files
committed
feat: Add pan and zoom to mermaid block
1 parent 396538b commit 01e0f4f

File tree

2 files changed

+38
-16
lines changed

2 files changed

+38
-16
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.mermaid {
2+
position: relative;
3+
width: 100%;
4+
overflow: hidden;
5+
border: 1px solid #ddd;
6+
margin: 20px 0px;
7+
cursor: grab;
8+
}

packages/docs-router/src/docs.rs

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -98,29 +98,43 @@ fn MermaidBlock(chart: &'static str) -> Element {
9898
rsx! {
9999
div {
100100
document::Link { rel: "stylesheet", href: asset!("assets/mermaid_block.css") }
101-
div { class: "diagram-container", style: "height: 600px;",
102-
div { class: "diagram-wrapper", id: "diagram-wrapper",
103-
pre { class: "mermaid", dangerous_inner_html: "{chart}" }
104-
script { r#type: "module",
105-
r#"
101+
pre {
102+
class: "mermaid",
103+
style: "background-color: #fff",
104+
dangerous_inner_html: "{chart}",
105+
}
106+
script { r#type: "module",
107+
r#"
106108
import mermaid from 'https://cdn.jsdelivr.net/npm/mermaid@11/+esm';
107-
mermaid.initialize({{ startOnLoad: false }});
109+
import Panzoom from 'https://cdn.jsdelivr.net/npm/@panzoom/[email protected]/+esm';
110+
111+
mermaid.initialize({{
112+
startOnLoad: false,
113+
}});
114+
108115
const mermaidElements = document.querySelectorAll('.mermaid');
116+
let elements = [];
109117
mermaidElements.forEach((element, index) => {{
118+
if (element.getAttribute('data-processed') === 'true') {{
119+
return;
120+
}}
110121
element.textContent = element.textContent.trim();
122+
elements.push(element);
111123
}});
112-
mermaid.run().catch(error => {{
113-
console.error('Mermaid rendering error:', error);
124+
125+
mermaid.run().then(() => {{
126+
elements.forEach((element, index) => {{
127+
let svg = element.firstElementChild;
128+
const panzoom = Panzoom(svg, {{
129+
step: 1,
130+
maxScale: 10,
131+
minScale: 0.5,
132+
}});
133+
element.addEventListener('wheel', panzoom.zoomWithWheel);
134+
}})
114135
}});
136+
115137
"#
116-
}
117-
}
118-
div { class: "zoom-controls",
119-
button { class: "zoom-in", "+" }
120-
button { class: "zoom-reset", "Reset" }
121-
button { class: "zoom-out", "-" }
122-
div { class: "zoom-level", "100%" }
123-
}
124138
}
125139
}
126140
}

0 commit comments

Comments
 (0)