tldraw
Version:
A tiny little drawing editor.
101 lines (100 loc) • 3.7 kB
JavaScript
import { jsx } from "react/jsx-runtime";
import { Box, useEditor, useValue } from "@tldraw/editor";
import { useCallback, useEffect, useRef, useState } from "react";
import { useTranslation } from "../../hooks/useTranslation/useTranslation.mjs";
import { TldrawUiContextualToolbar } from "../primitives/TldrawUiContextualToolbar.mjs";
import { AltTextEditor } from "./AltTextEditor.mjs";
import { DefaultImageToolbarContent } from "./DefaultImageToolbarContent.mjs";
function DefaultImageToolbar({ children }) {
const editor = useEditor();
const imageShapeId = useValue(
"imageShape",
() => {
const onlySelectedShape = editor.getOnlySelectedShape();
if (!onlySelectedShape || onlySelectedShape.type !== "image") return null;
return onlySelectedShape.id;
},
[editor]
);
const showToolbar = useValue(
"showToolbar",
() => editor.isInAny("select.idle", "select.pointing_shape", "select.crop"),
[editor]
);
const isLocked = useValue(
"locked",
() => imageShapeId ? editor.getShape(imageShapeId)?.isLocked : false,
[editor, imageShapeId]
);
if (!imageShapeId || !showToolbar || isLocked) return null;
return /* @__PURE__ */ jsx(ContextualToolbarInner, { imageShapeId, children }, imageShapeId);
}
function ContextualToolbarInner({
children,
imageShapeId
}) {
const editor = useEditor();
const msg = useTranslation();
const isChangingCrop = useValue(
"editor path",
() => editor.isInAny(
"select.crop.cropping",
"select.crop.pointing_crop_handle",
"select.crop.translating_crop"
),
[editor]
);
const camera = useValue("camera", () => editor.getCamera(), [editor]);
const isInCropTool = useValue("editor path", () => editor.isIn("select.crop."), [editor]);
const previousSelectionBounds = useRef(void 0);
const handleManipulatingEnd = useCallback(() => {
editor.setCroppingShape(null);
editor.setCurrentTool("select.idle");
}, [editor]);
const [isEditingAltText, setIsEditingAltText] = useState(false);
const handleEditAltTextStart = useCallback(() => setIsEditingAltText(true), []);
const handleManipulatingStart = useCallback(
() => editor.setCurrentTool("select.crop.idle"),
[editor]
);
const onEditAltTextClose = useCallback(() => setIsEditingAltText(false), []);
useEffect(() => {
previousSelectionBounds.current = void 0;
}, [camera]);
const getSelectionBounds = useCallback(() => {
if (isInCropTool && previousSelectionBounds.current) {
return previousSelectionBounds.current;
}
const fullBounds = editor.getSelectionScreenBounds();
if (!fullBounds) return void 0;
const bounds = new 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__ */ jsx(
TldrawUiContextualToolbar,
{
className: "tlui-media__toolbar tlui-image__toolbar",
getSelectionBounds,
label: msg("tool.image-toolbar-title"),
children: children ? children : isEditingAltText ? /* @__PURE__ */ jsx(AltTextEditor, { shapeId: imageShapeId, onClose: onEditAltTextClose, source: "image-toolbar" }) : /* @__PURE__ */ jsx(
DefaultImageToolbarContent,
{
imageShapeId,
isManipulating: isInCropTool,
onEditAltTextStart: handleEditAltTextStart,
onManipulatingStart: handleManipulatingStart,
onManipulatingEnd: handleManipulatingEnd
}
)
}
);
}
export {
DefaultImageToolbar
};
//# sourceMappingURL=DefaultImageToolbar.mjs.map