-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #284 from RyoJerryYu/feat/add-obsidian-rich-feature
feat: beautify excalidraw components
- Loading branch information
Showing
15 changed files
with
642 additions
and
361 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
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
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,8 @@ | ||
import dynamic from "next/dynamic"; | ||
|
||
export const ExcalidrawScene = dynamic( | ||
async () => (await import("./ExcalidrawSceneImpl")).ExcalidrawSceneImpl, | ||
{ | ||
ssr: false, | ||
} | ||
); |
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,6 @@ | ||
import dynamic from "next/dynamic"; | ||
|
||
export const MermaidCodeBlock = dynamic( | ||
async () => (await import("./MermaidCodeBlockImpl")).MermaidCodeBlockImpl, | ||
{ ssr: false } | ||
); |
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,54 @@ | ||
"use client"; | ||
import { convertToExcalidrawElements } from "@excalidraw/excalidraw"; | ||
import { ExcalidrawElement } from "@excalidraw/excalidraw/types/element/types"; | ||
import { parseMermaidToExcalidraw } from "@excalidraw/mermaid-to-excalidraw"; | ||
import { useEffect, useState } from "react"; | ||
import { ExcalidrawSceneImpl } from "./ExcalidrawSceneImpl"; | ||
|
||
export type MermaidCodeBlockProps = { | ||
name?: string; | ||
children: string; | ||
className?: string; | ||
}; | ||
export function MermaidCodeBlockImpl({ | ||
name = "mermaid", | ||
children, | ||
className, | ||
}: MermaidCodeBlockProps) { | ||
console.log(children); | ||
const [elements, setElements] = useState<ExcalidrawElement[]>([]); | ||
const [parseDone, setParseDone] = useState(false); | ||
useEffect(() => { | ||
const parseMermaid = async () => { | ||
if (parseDone) return; | ||
try { | ||
const { elements: elementSkeletons } = await parseMermaidToExcalidraw( | ||
children, | ||
{} | ||
); | ||
const excalidrawElements = | ||
convertToExcalidrawElements(elementSkeletons); | ||
setElements(excalidrawElements); | ||
setParseDone(true); | ||
console.log("elements", excalidrawElements); | ||
} catch (e) { | ||
console.error("error", e); | ||
} | ||
}; | ||
|
||
parseMermaid(); | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [children, parseDone]); | ||
|
||
return ( | ||
<div className="relative h-[600px] py-4"> | ||
{parseDone ? ( | ||
<ExcalidrawSceneImpl elements={elements} /> | ||
) : ( | ||
<div className="absolute inset-0 flex items-center justify-center"> | ||
Loading... | ||
</div> | ||
)} | ||
</div> | ||
); | ||
} |
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 |
---|---|---|
@@ -1,34 +1,32 @@ | ||
"use client"; | ||
export {}; | ||
|
||
import mermaid from "mermaid"; | ||
import React, { useEffect, useLayoutEffect, useMemo } from "react"; | ||
// // mermaid.init({ | ||
// // startOnLoad: false, | ||
// // }); | ||
|
||
mermaid.init({ | ||
startOnLoad: false, | ||
}); | ||
// type MermaidProps = { | ||
// name?: string; | ||
// children: string; | ||
// className?: string; | ||
// }; | ||
|
||
type MermaidProps = { | ||
name?: string; | ||
children: string; | ||
className?: string; | ||
}; | ||
// const Mermaid = ({ name = "mermaid", children, className }: MermaidProps) => { | ||
// const [svg, setSvg] = React.useState(""); | ||
// useEffect(() => { | ||
// const res = mermaid.render(name, children); | ||
// setSvg(res); | ||
// }, [name, children]); | ||
|
||
const Mermaid = ({ name = "mermaid", children, className }: MermaidProps) => { | ||
const [svg, setSvg] = React.useState(""); | ||
useEffect(() => { | ||
const res = mermaid.render(name, children); | ||
setSvg(res); | ||
}, [name, children]); | ||
// // TODO maybe caculate simple size and set default size | ||
// return svg === "" ? ( | ||
// <code>{children}</code> | ||
// ) : ( | ||
// <div | ||
// className={className ? `mermaid ${className}` : `mermaid`} | ||
// dangerouslySetInnerHTML={{ __html: svg }} | ||
// /> | ||
// ); | ||
// }; | ||
|
||
// TODO maybe caculate simple size and set default size | ||
return svg === "" ? ( | ||
<code>{children}</code> | ||
) : ( | ||
<div | ||
className={className ? `mermaid ${className}` : `mermaid`} | ||
dangerouslySetInnerHTML={{ __html: svg }} | ||
/> | ||
); | ||
}; | ||
|
||
export default Mermaid; | ||
// export default Mermaid; |
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
Oops, something went wrong.