UNPKG

tldraw

Version:

A tiny little drawing editor.

121 lines (120 loc) 5.16 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; 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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var DefaultImageToolbar_exports = {}; __export(DefaultImageToolbar_exports, { DefaultImageToolbar: () => DefaultImageToolbar }); module.exports = __toCommonJS(DefaultImageToolbar_exports); var import_jsx_runtime = require("react/jsx-runtime"); var import_editor = require("@tldraw/editor"); var import_react = require("react"); var import_useTranslation = require("../../hooks/useTranslation/useTranslation"); var import_TldrawUiContextualToolbar = require("../primitives/TldrawUiContextualToolbar"); var import_AltTextEditor = require("./AltTextEditor"); var import_DefaultImageToolbarContent = require("./DefaultImageToolbarContent"); function DefaultImageToolbar({ children }) { const editor = (0, import_editor.useEditor)(); const imageShapeId = (0, import_editor.useValue)( "imageShape", () => { const onlySelectedShape = editor.getOnlySelectedShape(); if (!onlySelectedShape || onlySelectedShape.type !== "image") return null; return onlySelectedShape.id; }, [editor] ); const showToolbar = (0, import_editor.useValue)( "showToolbar", () => editor.isInAny("select.idle", "select.pointing_shape", "select.crop"), [editor] ); const isLocked = (0, import_editor.useValue)( "locked", () => imageShapeId ? editor.getShape(imageShapeId)?.isLocked : false, [editor, imageShapeId] ); if (!imageShapeId || !showToolbar || isLocked) return null; return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(ContextualToolbarInner, { imageShapeId, children }, imageShapeId); } function ContextualToolbarInner({ children, imageShapeId }) { const editor = (0, import_editor.useEditor)(); const msg = (0, import_useTranslation.useTranslation)(); const isChangingCrop = (0, import_editor.useValue)( "editor path", () => editor.isInAny( "select.crop.cropping", "select.crop.pointing_crop_handle", "select.crop.translating_crop" ), [editor] ); const camera = (0, import_editor.useValue)("camera", () => editor.getCamera(), [editor]); const isInCropTool = (0, import_editor.useValue)("editor path", () => editor.isIn("select.crop."), [editor]); const previousSelectionBounds = (0, import_react.useRef)(void 0); const handleManipulatingEnd = (0, import_react.useCallback)(() => { editor.setCroppingShape(null); editor.setCurrentTool("select.idle"); }, [editor]); const [isEditingAltText, setIsEditingAltText] = (0, import_react.useState)(false); const handleEditAltTextStart = (0, import_react.useCallback)(() => setIsEditingAltText(true), []); const handleManipulatingStart = (0, import_react.useCallback)( () => editor.setCurrentTool("select.crop.idle"), [editor] ); const onEditAltTextClose = (0, import_react.useCallback)(() => setIsEditingAltText(false), []); (0, import_react.useEffect)(() => { previousSelectionBounds.current = void 0; }, [camera]); const getSelectionBounds = (0, import_react.useCallback)(() => { if (isInCropTool && previousSelectionBounds.current) { return previousSelectionBounds.current; } const fullBounds = editor.getSelectionScreenBounds(); if (!fullBounds) return void 0; const bounds = new import_editor.Box(fullBounds.x, fullBounds.y, fullBounds.width, 0); previousSelectionBounds.current = bounds; return bounds; }, [editor, isInCropTool]); if (isChangingCrop) { previousSelectionBounds.current = void 0; return null; } return /* @__PURE__ */ (0, import_jsx_runtime.jsx)( import_TldrawUiContextualToolbar.TldrawUiContextualToolbar, { className: "tlui-media__toolbar tlui-image__toolbar", getSelectionBounds, label: msg("tool.image-toolbar-title"), children: children ? children : isEditingAltText ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_AltTextEditor.AltTextEditor, { shapeId: imageShapeId, onClose: onEditAltTextClose, source: "image-toolbar" }) : /* @__PURE__ */ (0, import_jsx_runtime.jsx)( import_DefaultImageToolbarContent.DefaultImageToolbarContent, { imageShapeId, isManipulating: isInCropTool, onEditAltTextStart: handleEditAltTextStart, onManipulatingStart: handleManipulatingStart, onManipulatingEnd: handleManipulatingEnd } ) } ); } //# sourceMappingURL=DefaultImageToolbar.js.map