Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SVG zoom for visualization graph #810

Merged
merged 2 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
"clean": "rimraf dist tsconfig.tsbuildinfo",
"prepack": "pnpm build"
},
"dependencies": {},
"devDependencies": {
"@fontsource/roboto": "^5.0.8",
"@nomicfoundation/ignition-core": "workspace:^",
Expand All @@ -32,6 +31,7 @@
"react-router-dom": "6.11.0",
"react-tooltip": "^5.21.4",
"styled-components": "5.3.10",
"svg-pan-zoom": "^3.6.1",
"vite": "^5.0.0",
"vite-plugin-singlefile": "^2.0.1"
}
Expand Down
17 changes: 17 additions & 0 deletions packages/ui/src/components/mermaid.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import React, { useEffect, useMemo } from "react";
import styled from "styled-components";
import svgPanZoom from "svg-pan-zoom";

import {
IgnitionModule,
IgnitionModuleResult,
} from "@nomicfoundation/ignition-core/ui-helpers";
import mermaid from "mermaid";

import { toMermaid } from "../utils/to-mermaid";

export const Mermaid: React.FC<{
Expand All @@ -23,6 +25,7 @@ export const Mermaid: React.FC<{

useEffect(() => {
mermaid.initialize({
maxTextSize: 500000,
zoeyTM marked this conversation as resolved.
Show resolved Hide resolved
flowchart: {
padding: 50,
},
Expand All @@ -31,6 +34,20 @@ export const Mermaid: React.FC<{
mermaid.contentLoaded();
});

// requestAnimationFrame is used to ensure that the svgPanZoom is called after the svg is rendered
useEffect(() => {
requestAnimationFrame(() => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you walk me through this one.

Is the problem that you want to hold on the svgPanZoom invocation until you are sure that the diagram svg is rendered?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct. If svgPanZoom is run before its rendered the function will error. This ensures that its run after rendering

setTimeout(() => {
svgPanZoom(".mermaid > svg", {
zoomEnabled: true,
controlIconsEnabled: true,
fit: true,
center: true,
});
}, 0);
});
});

return (
<Wrap>
<div className="mermaid">{diagram}</div>
Expand Down
13 changes: 13 additions & 0 deletions packages/ui/src/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,24 @@ body {

/* mermaid styles */

.mermaid {
background: #fbfbfb;
}

.mermaid * {
font-family: "Roboto", sans-serif;
font-size: 30px;
}

.mermaid svg {
height: 300px;
vertical-align: middle;
}

.mermaid span {
cursor: default;
}

.mermaid rect {
rx: 5;
ry: 5;
Expand Down
Loading
Loading