-
Notifications
You must be signed in to change notification settings - Fork 52
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 4e229dc
Showing
13 changed files
with
397 additions
and
368 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 { Mermaid } from "./Mermaid.tsx"; | ||
import { useExcalidraw } from "./context/excalidraw.ts"; | ||
|
||
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,31 @@ | ||
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) => { | ||
const { svg } = await mermaid.render(`mermaid-diagram-${id}`, definition); | ||
startTransition(() => { | ||
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,107 @@ | ||
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 { Mermaid } from "./Mermaid"; | ||
import { useExcalidraw } from "./context/excalidraw.ts"; | ||
|
||
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; |
This file was deleted.
Oops, something went wrong.
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,28 @@ | ||
import type { ExcalidrawElementSkeleton } from "@excalidraw/excalidraw/types/data/transform"; | ||
import type { | ||
BinaryFiles, | ||
ExcalidrawImperativeAPI, | ||
} from "@excalidraw/excalidraw/types/types"; | ||
import React, { useContext } from "react"; | ||
import { parseMermaid } from "../../src/parseMermaid"; | ||
|
||
export const ExcalidrawContext = React.createContext<{ | ||
excalidrawAPI?: ExcalidrawImperativeAPI; | ||
addFiles: (files: BinaryFiles) => void; | ||
updateElements: (elements: ExcalidrawElementSkeleton[]) => void; | ||
translateMermaidToExcalidraw: (mermaidSyntax: string) => Promise<{ | ||
mermaid: Awaited<ReturnType<typeof parseMermaid>>; | ||
excalidraw: { elements: ExcalidrawElementSkeleton[]; files?: BinaryFiles }; | ||
}>; | ||
setApi: (api: ExcalidrawImperativeAPI) => void; | ||
} | null>(null); | ||
|
||
export const useExcalidraw = () => { | ||
const context = useContext(ExcalidrawContext); | ||
|
||
if (!context) { | ||
throw new Error("useExcalidraw must be used within a ExcalidrawProvider"); | ||
} | ||
|
||
return context; | ||
}; |
Oops, something went wrong.