-
-
Notifications
You must be signed in to change notification settings - Fork 13
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
Yann Leflour
committed
Dec 15, 2023
1 parent
c766f3e
commit a6ed2e9
Showing
9 changed files
with
201 additions
and
33 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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,70 @@ | ||
import { useEditor, ShapeUtil, TLUnknownShape } from "@tldraw/editor"; | ||
import { useEffect, useState } from "react"; | ||
import { | ||
MermaidShape, | ||
MermaidShapeUtil, | ||
} from "../tools/mermaid/mermaid.shape-util"; | ||
|
||
const useSingleShape = < | ||
S extends TLUnknownShape, | ||
U extends typeof ShapeUtil<S> = typeof ShapeUtil<S>, | ||
>( | ||
Shape: U, | ||
): S | null => { | ||
const editor = useEditor(); | ||
const [mermaidShape, setMermaidShape] = useState<null | S>(null); | ||
|
||
useEffect(() => { | ||
const onEvent = () => { | ||
const selectedShapes = editor.getSelectedShapes(); | ||
if ( | ||
selectedShapes.length === 1 && | ||
selectedShapes[0].type === Shape.type | ||
) { | ||
setMermaidShape(selectedShapes[0] as S); | ||
} else { | ||
setMermaidShape(null); | ||
} | ||
}; | ||
|
||
editor.addListener("update", onEvent); | ||
|
||
return () => { | ||
editor.removeListener("event", onEvent); | ||
}; | ||
}, [editor, setMermaidShape, Shape.type]); | ||
|
||
return mermaidShape; | ||
}; | ||
|
||
export const MermaidEditor = () => { | ||
const editor = useEditor(); | ||
const mermaidShape = useSingleShape<MermaidShape>(MermaidShapeUtil); | ||
const [value, setValue] = useState<string>(""); | ||
|
||
useEffect(() => { | ||
setValue(mermaidShape ? mermaidShape.props.source : ""); | ||
}, [mermaidShape]); | ||
|
||
if (!mermaidShape) return null; | ||
|
||
const onChange = (e: React.ChangeEvent<HTMLTextAreaElement>) => { | ||
setValue(e.target.value); | ||
editor.updateShape({ | ||
id: mermaidShape.id, | ||
type: mermaidShape.type, | ||
props: { source: e.target.value }, | ||
}); | ||
}; | ||
|
||
return ( | ||
<div className="shadow-tl-2 m-w-1/2 pointer-events-auto rounded-lg p-3"> | ||
<textarea | ||
className="textarea rounded-none" | ||
rows={12} | ||
value={value} | ||
onChange={onChange} | ||
/> | ||
</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,7 +1,13 @@ | ||
import { MakeRealButton } from "./make-real-button"; | ||
import { MermaidEditor } from "./mermaid-editor"; | ||
|
||
export const ShareZone = () => ( | ||
<div className="z-300 flex gap-2 p-2"> | ||
<MakeRealButton /> | ||
</div> | ||
); | ||
export const ShareZone = () => { | ||
return ( | ||
<div className="z-300 flex flex-col gap-2 p-2"> | ||
<div className="flex justify-end gap-2"> | ||
<MakeRealButton /> | ||
</div> | ||
<MermaidEditor /> | ||
</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
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,13 @@ | ||
import { StyleProp } from "@tldraw/tldraw"; | ||
|
||
const validateString = (text: unknown): string => { | ||
if (typeof text !== "string") { | ||
throw new Error("Expected string"); | ||
} | ||
return text; | ||
}; | ||
|
||
export const SourceStyleProp = StyleProp.define("tldraw:TextStyle", { | ||
defaultValue: "", | ||
type: { validate: validateString }, | ||
}); |
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