-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
20a6a25
commit 57a65b3
Showing
11 changed files
with
391 additions
and
364 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import React from "react"; | ||
|
||
import { useExcalidraw } from "./ExcalidrawWrapper.tsx"; | ||
import { Mermaid } from "./Mermaid.tsx"; | ||
|
||
function CustomTest() { | ||
const excalidraw = useExcalidraw(); | ||
const [parsedMermaid, setParsedMermaid] = React.useState<{ | ||
data: string | null; | ||
error: string | null; | ||
definition: string | null; | ||
}>({ | ||
data: null, | ||
error: null, | ||
definition: null, | ||
}); | ||
|
||
return ( | ||
<> | ||
<form | ||
onSubmit={async (event) => { | ||
event.preventDefault(); | ||
|
||
const data = new FormData(event.target as HTMLFormElement); | ||
const mermaidSyntax = data.get("mermaid-input") as string; | ||
|
||
if (!mermaidSyntax) { | ||
return; | ||
} | ||
|
||
try { | ||
setParsedMermaid({ | ||
data: null, | ||
definition: null, | ||
error: null, | ||
}); | ||
|
||
const { mermaid } = await excalidraw.translateMermaidToExcalidraw( | ||
mermaidSyntax | ||
); | ||
|
||
setParsedMermaid({ | ||
data: JSON.stringify(mermaid, null, 2), | ||
definition: mermaidSyntax, | ||
error: null, | ||
}); | ||
} catch (error) { | ||
setParsedMermaid({ | ||
data: null, | ||
definition: null, | ||
error: String(error), | ||
}); | ||
} | ||
}} | ||
> | ||
<textarea | ||
id="mermaid-input" | ||
rows={10} | ||
cols={50} | ||
name="mermaid-input" | ||
style={{ marginTop: "1rem" }} | ||
placeholder="Input Mermaid Syntax" | ||
/> | ||
<br /> | ||
<button type="submit" id="render-excalidraw-btn"> | ||
{"Render to Excalidraw"} | ||
</button> | ||
</form> | ||
|
||
{parsedMermaid.definition && ( | ||
<Mermaid definition={parsedMermaid.definition} id="custom-diagram" /> | ||
)} | ||
|
||
{parsedMermaid.data && ( | ||
<details id="parsed-data-details"> | ||
<summary>{"Parsed data from parseMermaid"}</summary> | ||
<pre id="custom-parsed-data">{parsedMermaid.data}</pre> | ||
</details> | ||
)} | ||
|
||
{parsedMermaid.error && <div id="error">{parsedMermaid.error}</div>} | ||
</> | ||
); | ||
} | ||
|
||
export default CustomTest; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import mermaid from "mermaid"; | ||
import React from "react"; | ||
|
||
interface MermaidProps { | ||
id: string; | ||
definition: string; | ||
} | ||
|
||
export function Mermaid({ definition, id }: MermaidProps) { | ||
const [svg, setSvg] = React.useState(""); | ||
const [, startTransition] = React.useTransition(); | ||
|
||
React.useEffect(() => { | ||
const render = async (id: string, definition: string) => { | ||
startTransition(() => { | ||
mermaid.render(`mermaid-diagram-${id}`, definition).then(({ svg }) => { | ||
setSvg(svg); | ||
}); | ||
}); | ||
}; | ||
|
||
render(id, definition); | ||
}, [definition, id]); | ||
|
||
return ( | ||
<div | ||
style={{ width: "50%" }} | ||
className="mermaid" | ||
dangerouslySetInnerHTML={{ __html: svg }} | ||
/> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
import React from "react"; | ||
|
||
import { FLOWCHART_DIAGRAM_TESTCASES } from "./testcases/flowchart"; | ||
import { SEQUENCE_DIAGRAM_TESTCASES } from "./testcases/sequence.ts"; | ||
import { CLASS_DIAGRAM_TESTCASES } from "./testcases/class.ts"; | ||
import { UNSUPPORTED_DIAGRAM_TESTCASES } from "./testcases/unsupported.ts"; | ||
import { useExcalidraw } from "./ExcalidrawWrapper.tsx"; | ||
import { Mermaid } from "./Mermaid"; | ||
|
||
interface TestCaseProps { | ||
name: string; | ||
baseId: string; | ||
testcases: { name: string; definition: string }[]; | ||
} | ||
|
||
function Testcase({ name, baseId, testcases }: TestCaseProps) { | ||
const excalidraw = useExcalidraw(); | ||
const activeTestcase = React.useRef<number>(); | ||
|
||
React.useEffect(() => { | ||
const testcase = activeTestcase.current; | ||
|
||
if (testcase !== undefined) { | ||
const { definition } = testcases[testcase]; | ||
|
||
excalidraw.translateMermaidToExcalidraw(definition); | ||
} | ||
}, [excalidraw.translateMermaidToExcalidraw, testcases]); | ||
|
||
return ( | ||
<> | ||
<h2>{`${name} Diagrams`}</h2> | ||
<details> | ||
<summary>{`${name} Examples`}</summary> | ||
<div id={`${baseId}-container`}> | ||
{testcases.map(({ name, definition }, index) => { | ||
const id = `${baseId}-${index}`; | ||
|
||
return ( | ||
<div key={id}> | ||
<h2 style={{ marginTop: "50px", color: "#f06595" }}>{name}</h2> | ||
|
||
<pre | ||
style={{ | ||
fontSize: "16px", | ||
fontWeight: "600", | ||
fontStyle: "italic", | ||
background: "#eeeef1", | ||
whiteSpace: "pre-wrap", | ||
width: "45vw", | ||
padding: "5px", | ||
}} | ||
> | ||
{definition} | ||
</pre> | ||
|
||
<button | ||
onClick={() => { | ||
excalidraw.translateMermaidToExcalidraw(definition); | ||
activeTestcase.current = index; | ||
}} | ||
> | ||
{"Render to Excalidraw"} | ||
</button> | ||
|
||
<Mermaid definition={definition} id={id} /> | ||
</div> | ||
); | ||
})} | ||
</div> | ||
</details> | ||
</> | ||
); | ||
} | ||
|
||
function Testcases() { | ||
return ( | ||
<> | ||
<Testcase | ||
name="Flowchart" | ||
baseId="flowchart" | ||
testcases={FLOWCHART_DIAGRAM_TESTCASES} | ||
/> | ||
|
||
<Testcase | ||
name="Sequence" | ||
baseId="sequence" | ||
testcases={SEQUENCE_DIAGRAM_TESTCASES} | ||
/> | ||
|
||
<Testcase | ||
name="Class" | ||
baseId="class" | ||
testcases={CLASS_DIAGRAM_TESTCASES} | ||
/> | ||
|
||
<Testcase | ||
name="Unsupported" | ||
baseId="unsupported" | ||
testcases={UNSUPPORTED_DIAGRAM_TESTCASES} | ||
/> | ||
</> | ||
); | ||
} | ||
|
||
export default Testcases; |
Oops, something went wrong.