UNPKG

tldraw

Version:

A tiny little drawing editor.

317 lines (316 loc) • 10.7 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var useTools_exports = {}; __export(useTools_exports, { ToolsContext: () => ToolsContext, ToolsProvider: () => ToolsProvider, onDragFromToolbarToCreateShape: () => onDragFromToolbarToCreateShape, useTools: () => useTools }); module.exports = __toCommonJS(useTools_exports); var import_jsx_runtime = require("react/jsx-runtime"); var import_editor = require("@tldraw/editor"); var React = __toESM(require("react"), 1); var import_selectHelpers = require("../../tools/SelectTool/selectHelpers"); var import_EmbedDialog = require("../components/EmbedDialog"); var import_a11y = require("../context/a11y"); var import_events = require("../context/events"); var import_overrides = require("../overrides"); var import_useTranslation = require("./useTranslation/useTranslation"); const ToolsContext = React.createContext(null); function ToolsProvider({ overrides, children }) { const editor = (0, import_editor.useMaybeEditor)(); const trackEvent = (0, import_events.useUiEvents)(); const a11y = (0, import_a11y.useA11y)(); const msg = (0, import_useTranslation.useTranslation)(); const helpers = (0, import_overrides.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); } }, ...[...import_editor.GeoShapeGeoStyle.values].map((geo) => ({ id: geo, label: `tool.${geo}`, meta: { geo }, kbd: geo === "rectangle" ? "r" : geo === "ellipse" ? "o" : void 0, icon: "geo-" + geo, onSelect(source) { editor.run(() => { editor.setStyleForNextShapes(import_editor.GeoShapeGeoStyle, geo); editor.setCurrentTool("geo"); onToolSelect(source, this, `geo-${geo}`); }); }, onDragStart(source, info) { onDragFromToolbarToCreateShape(editor, info, { createShape: (id) => editor.createShape({ id, type: "geo", props: { w: 200, h: 200, geo } }) }); trackEvent("drag-tool", { source, id: "geo" }); } })), { id: "arrow", label: "tool.arrow", icon: "tool-arrow", kbd: "a", onSelect(source) { editor.setCurrentTool("arrow"); onToolSelect(source, this); }, onDragStart(source, info) { onDragFromToolbarToCreateShape(editor, info, { createShape: (id) => editor.createShape({ id, type: "arrow", props: { start: { x: 0, y: 200 }, end: { x: 200, y: 0 } } }) }); trackEvent("drag-tool", { source, id: "arrow" }); } }, { id: "line", label: "tool.line", icon: "tool-line", kbd: "l", onSelect(source) { editor.setCurrentTool("line"); onToolSelect(source, this); }, onDragStart(source, info) { onDragFromToolbarToCreateShape(editor, info, { createShape: (id) => { const [start, end] = (0, import_editor.getIndicesBetween)(null, null, 2); editor.createShape({ id, type: "line", props: { points: { [start]: { id: start, index: start, x: 0, y: 200 }, [end]: { id: end, index: end, x: 200, y: 0 } } } }); } }); trackEvent("drag-tool", { source, id: "line" }); } }, { id: "frame", label: "tool.frame", icon: "tool-frame", kbd: "f", onSelect(source) { editor.setCurrentTool("frame"); onToolSelect(source, this); }, onDragStart(source, info) { onDragFromToolbarToCreateShape(editor, info, { createShape: (id) => editor.createShape({ id, type: "frame" }) }); trackEvent("drag-tool", { source, id: "frame" }); } }, { id: "text", label: "tool.text", icon: "tool-text", kbd: "t", onSelect(source) { editor.setCurrentTool("text"); onToolSelect(source, this); }, onDragStart(source, info) { onDragFromToolbarToCreateShape(editor, info, { createShape: (id) => editor.createShape({ id, type: "text", props: { richText: (0, import_editor.toRichText)("Text") } }), onDragEnd: (id) => { (0, import_selectHelpers.startEditingShapeWithRichText)(editor, id, { selectAll: true }); } }); trackEvent("drag-tool", { source, id: "text" }); } }, { 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); }, onDragStart(source, info) { onDragFromToolbarToCreateShape(editor, info, { createShape: (id) => editor.createShape({ id, type: "note" }), onDragEnd: (id) => { (0, import_selectHelpers.startEditingShapeWithRichText)(editor, id, { selectAll: true }); } }); trackEvent("drag-tool", { source, id: "note" }); } }, { 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: import_EmbedDialog.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, trackEvent]); return /* @__PURE__ */ (0, import_jsx_runtime.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; } function onDragFromToolbarToCreateShape(editor, info, opts) { const { x, y } = editor.inputs.getCurrentPagePoint(); const stoppingPoint = editor.markHistoryStoppingPoint("drag shape tool"); editor.setCurrentTool("select.translating"); const id = (0, import_editor.createShapeId)(); opts.createShape(id); const shape = (0, import_editor.assertExists)(editor.getShape(id), "Shape not found"); const { w, h } = editor.getShapePageBounds(id); editor.updateShape({ id, type: shape.type, x: x - w / 2, y: y - h / 2 }); editor.select(id); editor.setCurrentTool("select.translating", { ...info, target: "shape", shape: editor.getShape(id), isCreating: true, creatingMarkId: stoppingPoint, onCreate() { editor.setCurrentTool("select.idle"); editor.select(id); opts.onDragEnd?.(id); } }); editor.getCurrentTool().setCurrentToolIdMask(shape.type); } //# sourceMappingURL=useTools.js.map