tldraw
Version:
A tiny little drawing editor.
203 lines (202 loc) • 5.46 kB
JavaScript
import { jsx } from "react/jsx-runtime";
import { GeoShapeGeoStyle, useMaybeEditor } from "@tldraw/editor";
import * as React from "react";
import { EmbedDialog } from "../components/EmbedDialog.mjs";
import { useA11y } from "../context/a11y.mjs";
import { useUiEvents } from "../context/events.mjs";
import { useDefaultHelpers } from "../overrides.mjs";
import { useTranslation } from "./useTranslation/useTranslation.mjs";
const ToolsContext = React.createContext(null);
function ToolsProvider({ overrides, children }) {
const editor = useMaybeEditor();
const trackEvent = useUiEvents();
const a11y = useA11y();
const msg = useTranslation();
const helpers = useDefaultHelpers();
const onToolSelect = React.useCallback(
(source, tool, id) => {
a11y.announce({ msg: msg(tool.label) });
trackEvent("select-tool", { source, id: id ?? tool.id });
},
[a11y, msg, trackEvent]
);
const tools = React.useMemo(() => {
if (!editor) return {};
const toolsArray = [
{
id: "select",
label: "tool.select",
icon: "tool-pointer",
kbd: "v",
readonlyOk: true,
onSelect(source) {
if (editor.isIn("select")) {
const currentNode = editor.root.getCurrent();
currentNode.exit({}, currentNode.id);
currentNode.enter({}, currentNode.id);
}
editor.setCurrentTool("select");
onToolSelect(source, this);
}
},
{
id: "hand",
label: "tool.hand",
icon: "tool-hand",
kbd: "h",
readonlyOk: true,
onSelect(source) {
editor.setCurrentTool("hand");
onToolSelect(source, this);
}
},
{
id: "eraser",
label: "tool.eraser",
icon: "tool-eraser",
kbd: "e",
onSelect(source) {
editor.setCurrentTool("eraser");
onToolSelect(source, this);
}
},
{
id: "draw",
label: "tool.draw",
icon: "tool-pencil",
kbd: "d,b,x",
onSelect(source) {
editor.setCurrentTool("draw");
onToolSelect(source, this);
}
},
...[...GeoShapeGeoStyle.values].map((id) => ({
id,
label: `tool.${id}`,
meta: {
geo: id
},
kbd: id === "rectangle" ? "r" : id === "ellipse" ? "o" : void 0,
icon: "geo-" + id,
onSelect(source) {
editor.run(() => {
editor.setStyleForNextShapes(GeoShapeGeoStyle, id);
editor.setCurrentTool("geo");
onToolSelect(source, this, `geo-${id}`);
});
}
})),
{
id: "arrow",
label: "tool.arrow",
icon: "tool-arrow",
kbd: "a",
onSelect(source) {
editor.setCurrentTool("arrow");
onToolSelect(source, this);
}
},
{
id: "line",
label: "tool.line",
icon: "tool-line",
kbd: "l",
onSelect(source) {
editor.setCurrentTool("line");
onToolSelect(source, this);
}
},
{
id: "frame",
label: "tool.frame",
icon: "tool-frame",
kbd: "f",
onSelect(source) {
editor.setCurrentTool("frame");
onToolSelect(source, this);
}
},
{
id: "text",
label: "tool.text",
icon: "tool-text",
kbd: "t",
onSelect(source) {
editor.setCurrentTool("text");
onToolSelect(source, this);
}
},
{
id: "asset",
label: "tool.media",
icon: "tool-media",
kbd: "cmd+u,ctrl+u",
onSelect(source) {
helpers.insertMedia();
onToolSelect(source, this, "media");
}
},
{
id: "note",
label: "tool.note",
icon: "tool-note",
kbd: "n",
onSelect(source) {
editor.setCurrentTool("note");
onToolSelect(source, this);
}
},
{
id: "laser",
label: "tool.laser",
readonlyOk: true,
icon: "tool-laser",
kbd: "k",
onSelect(source) {
editor.setCurrentTool("laser");
onToolSelect(source, this);
}
},
{
id: "embed",
label: "tool.embed",
icon: "dot",
onSelect(source) {
helpers.addDialog({ component: EmbedDialog });
onToolSelect(source, this);
}
},
{
id: "highlight",
label: "tool.highlight",
icon: "tool-highlight",
// TODO: pick a better shortcut
kbd: "shift+d",
onSelect(source) {
editor.setCurrentTool("highlight");
onToolSelect(source, this);
}
}
];
toolsArray.forEach((t) => t.onSelect = t.onSelect.bind(t));
const tools2 = Object.fromEntries(toolsArray.map((t) => [t.id, t]));
if (overrides) {
return overrides(editor, tools2, helpers);
}
return tools2;
}, [overrides, editor, helpers, onToolSelect]);
return /* @__PURE__ */ jsx(ToolsContext.Provider, { value: tools, children });
}
function useTools() {
const ctx = React.useContext(ToolsContext);
if (!ctx) {
throw new Error("useTools must be used within a ToolProvider");
}
return ctx;
}
export {
ToolsContext,
ToolsProvider,
useTools
};
//# sourceMappingURL=useTools.mjs.map