UNPKG

tldraw

Version:

A tiny little drawing editor.

113 lines (112 loc) 4.57 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 LinkEditor_exports = {}; __export(LinkEditor_exports, { LinkEditor: () => LinkEditor }); module.exports = __toCommonJS(LinkEditor_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 LinkEditor({ textEditor, value: initialValue, onClose }) { const editor = (0, import_editor.useEditor)(); const [value, setValue] = (0, import_react.useState)(initialValue); const msg = (0, import_useTranslation.useTranslation)(); const ref = (0, import_react.useRef)(null); const trackEvent = (0, import_events.useUiEvents)(); const source = "rich-text-menu"; const linkifiedValue = value.startsWith("http") ? value : `https://${value}`; const handleValueChange = (value2) => setValue(value2); const handleLinkComplete = (link) => { trackEvent("rich-text", { operation: "link-edit", source }); if (!link.startsWith("http://") && !link.startsWith("https://")) { link = `https://${link}`; } textEditor.chain().setLink({ href: link }).run(); if (editor.getInstanceState().isCoarsePointer) { textEditor.commands.blur(); } else { textEditor.commands.focus(); } onClose(); }; const handleVisitLink = () => { trackEvent("rich-text", { operation: "link-visit", source }); (0, import_editor.openWindow)(linkifiedValue, "_blank"); onClose(); }; const handleRemoveLink = () => { trackEvent("rich-text", { operation: "link-remove", source }); textEditor.chain().unsetLink().focus().run(); onClose(); }; const handleLinkCancel = () => onClose(); (0, import_react.useEffect)(() => { ref.current?.focus(); }, [value]); (0, import_react.useEffect)(() => { setValue(initialValue); }, [initialValue]); return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, { children: [ /* @__PURE__ */ (0, import_jsx_runtime.jsx)( import_TldrawUiInput.TldrawUiInput, { ref, "data-testid": "rich-text.link-input", className: "tlui-rich-text__toolbar-link-input", value, onValueChange: handleValueChange, onComplete: handleLinkComplete, onCancel: handleLinkCancel, placeholder: "example.com", "aria-label": "example.com" } ), /* @__PURE__ */ (0, import_jsx_runtime.jsx)( import_TldrawUiButton.TldrawUiButton, { className: "tlui-rich-text__toolbar-link-visit", title: msg("tool.rich-text-link-visit"), type: "icon", onPointerDown: import_editor.preventDefault, onClick: handleVisitLink, disabled: !value, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_TldrawUiButtonIcon.TldrawUiButtonIcon, { small: true, icon: "external-link" }) } ), /* @__PURE__ */ (0, import_jsx_runtime.jsx)( import_TldrawUiButton.TldrawUiButton, { className: "tlui-rich-text__toolbar-link-remove", title: msg("tool.rich-text-link-remove"), "data-testid": "rich-text.link-remove", type: "icon", onPointerDown: import_editor.preventDefault, onClick: handleRemoveLink, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_TldrawUiButtonIcon.TldrawUiButtonIcon, { small: true, icon: "trash" }) } ) ] }); } //# sourceMappingURL=LinkEditor.js.map