UNPKG

tldraw

Version:

A tiny little drawing editor.

103 lines (102 loc) 4.1 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 AltTextEditor_exports = {}; __export(AltTextEditor_exports, { AltTextEditor: () => AltTextEditor }); module.exports = __toCommonJS(AltTextEditor_exports); var import_jsx_runtime = require("react/jsx-runtime"); var import_editor = require("@tldraw/editor"); var import_react = require("react"); var import_events = require("../../context/events"); var import_useTranslation = require("../../hooks/useTranslation/useTranslation"); var import_TldrawUiButton = require("../primitives/Button/TldrawUiButton"); var import_TldrawUiButtonIcon = require("../primitives/Button/TldrawUiButtonIcon"); var import_TldrawUiInput = require("../primitives/TldrawUiInput"); function AltTextEditor({ shapeId, onClose, source }) { const editor = (0, import_editor.useEditor)(); const [altText, setAltText] = (0, import_react.useState)(() => { const shape = editor.getShape(shapeId); if (!shape) return ""; if (!("altText" in shape.props)) throw Error("Shape does not have altText property"); return shape.props.altText || ""; }); const msg = (0, import_useTranslation.useTranslation)(); const ref = (0, import_react.useRef)(null); const trackEvent = (0, import_events.useUiEvents)(); const isReadonly = editor.getIsReadonly(); const handleValueChange = (value) => setAltText(value); const handleComplete = () => { trackEvent("set-alt-text", { source }); const shape = editor.getShape(shapeId); if (!shape) return; editor.updateShapes([ { id: shape.id, type: shape.type, props: { altText } } ]); onClose(); }; const handleConfirm = () => handleComplete(); const handleAltTextCancel = (0, import_react.useCallback)(() => onClose(), [onClose]); (0, import_react.useEffect)(() => { ref.current?.select(); function handleKeyDown(event) { if (event.key === "Escape") { event.stopPropagation(); handleAltTextCancel(); } } document.addEventListener("keydown", handleKeyDown, { capture: true }); return () => { document.removeEventListener("keydown", handleKeyDown, { capture: true }); }; }, [handleAltTextCancel]); return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, { children: [ /* @__PURE__ */ (0, import_jsx_runtime.jsx)( import_TldrawUiInput.TldrawUiInput, { ref, className: "tlui-media__toolbar-alt-text-input", "data-testid": "media-toolbar.alt-text-input", value: altText, placeholder: msg("tool.media-alt-text-desc"), "aria-label": msg("tool.media-alt-text-desc"), onValueChange: handleValueChange, onComplete: handleComplete, onCancel: handleAltTextCancel, disabled: isReadonly } ), !isReadonly && /* @__PURE__ */ (0, import_jsx_runtime.jsx)( import_TldrawUiButton.TldrawUiButton, { title: msg("tool.media-alt-text-confirm"), "data-testid": "tool.media-alt-text-confirm", type: "icon", onPointerDown: import_editor.preventDefault, onClick: handleConfirm, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_TldrawUiButtonIcon.TldrawUiButtonIcon, { small: true, icon: "check" }) } ) ] }); } //# sourceMappingURL=AltTextEditor.js.map