Skip to content

Instantly share code, notes, and snippets.

@mdroidian
Last active February 28, 2024 01:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mdroidian/6006ff4715309f95f5feb9dbb424e9e7 to your computer and use it in GitHub Desktop.
Save mdroidian/6006ff4715309f95f5feb9dbb424e9e7 to your computer and use it in GitHub Desktop.
tldraw-v12-custom-arrow
import {
App as Editor,
TldrawEditor,
TldrawUi,
ContextMenu,
Canvas,
TLArrowUtil,
TLArrowTool, // overrode to export
TldrawEditorConfig,
defineShape,
TldrawUiOverrides,
TLTranslationKey,
toolbarItem,
TLArrowShape,
} from "@tldraw/tldraw";
import "@tldraw/tldraw/editor.css";
import "@tldraw/tldraw/ui.css";
class CustomArrowUtil extends TLArrowUtil {
override canBind = () => true;
override canEdit = () => false;
defaultProps() {
console.log("defaultProps");
return {
labelColor: "blue" as const,
color: "green" as const,
fill: "none" as const,
dash: "draw" as const,
size: "s" as const,
opacity: "1" as const,
arrowheadStart: "arrow" as const,
arrowheadEnd: "arrow" as const,
font: "draw" as const,
start: { type: "point" as const, x: 0, y: 0 },
end: { type: "point" as const, x: 0, y: 0 },
bend: 0,
text: "changeDefaultText",
};
}
override onBeforeCreate = (shape: TLArrowShape) => {
console.log("onBeforeCreate");
return {
...shape,
props: {
...shape.props,
labelColor: "blue" as const,
color: "green" as const,
},
};
};
render(shape: TLArrowShape) {
return <>{super.render(shape)}</>;
}
}
function App() {
const allRelationNames = ["supports"];
const customTldrawConfig = new TldrawEditorConfig({
tools: allRelationNames.map(
(name) =>
class extends TLArrowTool {
static id = name;
static initial = "idle";
shapeType = name;
}
),
shapes: [
...allRelationNames.map((id) =>
defineShape({
type: id,
getShapeUtil: () =>
class extends CustomArrowUtil {
constructor(app: Editor) {
super(app, id);
}
},
})
),
],
allowUnknownShapes: true,
});
const overrrides: TldrawUiOverrides = {
translations: {
en: {},
},
tools(app, tools) {
allRelationNames.forEach((name) => {
tools[name] = {
id: name,
icon: "tool-arrow",
label: name as TLTranslationKey,
kbd: "",
readonlyOk: true,
onSelect: () => {
app.setSelectedTool(name);
},
};
});
return tools;
},
toolbar(_, toolbar, { tools }) {
toolbar.push(...allRelationNames.map((name) => toolbarItem(tools[name])));
return toolbar;
},
};
return (
<div style={{ width: "100vw", height: "100vh" }}>
<TldrawEditor
baseUrl="https://samepage.network/assets/tldraw/"
config={customTldrawConfig}
>
<TldrawUi
assetBaseUrl="https://samepage.network/assets/tldraw/"
overrides={overrrides}
>
<ContextMenu>
<Canvas />
</ContextMenu>
</TldrawUi>
</TldrawEditor>
</div>
);
}
export default App;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment