{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "rich-text-editor-link",
  "title": "Link",
  "description": "The link mark with a URL popover in the toolbar.",
  "dependencies": [
    "@tiptap/extension-link@3.32.2",
    "@tiptap/react@3.32.2",
    "cn",
    "lucide-react"
  ],
  "registryDependencies": [
    "litingyes/blocove/rich-text-editor",
    "litingyes/blocove/rich-text-editor-extension",
    "button",
    "input",
    "popover"
  ],
  "files": [
    {
      "path": "src/registry/blocove/ui/rich-text-editor/link.tsx",
      "content": "import type { LinkOptions } from '@tiptap/extension-link'\nimport type { Editor } from '@tiptap/react'\nimport type { ExtensionComponent } from '@/lib/blocove/rich-text-editor-extension'\nimport { Link } from '@tiptap/extension-link'\nimport { useCurrentEditor, useEditorState } from '@tiptap/react'\nimport { LinkIcon } from 'lucide-react'\nimport { useState } from 'react'\nimport { EditorToolbarButton } from '@/components/blocove/rich-text-editor/editor'\nimport { Button } from '@/components/ui/button'\nimport { Input } from '@/components/ui/input'\nimport { Popover, PopoverContent, PopoverTrigger } from '@/components/ui/popover'\nimport { defineExtensionComponent } from '@/lib/blocove/rich-text-editor-extension'\n\n// Matches an explicit URL protocol: `https:`, `mailto:`, `tel:`, …\nconst protocolPattern = /^[a-z][\\w+.-]*:/i\n\n/**\n * Registers the Tiptap Link mark. Links do not open while editing and always\n * carry a safe `rel` attribute.\n */\nexport const EditorLink: ExtensionComponent<Partial<LinkOptions>> = defineExtensionComponent(\n  'link',\n  (props: Partial<LinkOptions>) => [\n    Link.configure({\n      openOnClick: false,\n      autolink: true,\n      HTMLAttributes: { rel: 'noopener noreferrer nofollow' },\n      ...props,\n    }),\n  ],\n)\n\n/**\n * Adds an `https://` protocol to bare URLs. Explicit protocols such as\n * `mailto:` and `tel:` are kept as they are.\n */\nfunction normalizeHref(href: string) {\n  const trimmed = href.trim()\n\n  if (trimmed === '' || protocolPattern.test(trimmed)) {\n    return trimmed\n  }\n\n  return `https://${trimmed}`\n}\n\nexport interface EditorLinkToolbarProps {\n  className?: string\n}\n\nexport function EditorLinkToolbar({ className }: EditorLinkToolbarProps) {\n  const { editor } = useCurrentEditor()\n\n  if (!editor) {\n    return null\n  }\n\n  return <EditorLinkToolbarContent editor={editor} className={className} />\n}\n\nfunction EditorLinkToolbarContent({\n  editor,\n  className,\n}: {\n  editor: Editor\n  className?: string\n}) {\n  const active = useEditorState({\n    editor,\n    selector: ({ editor: instance }) => instance.isActive('link'),\n  })\n  const [open, setOpen] = useState(false)\n  const [href, setHref] = useState('')\n\n  function closePopover() {\n    setOpen(false)\n    editor.chain().focus().run()\n  }\n\n  function handleOpenChange(nextOpen: boolean) {\n    if (!nextOpen) {\n      closePopover()\n      return\n    }\n\n    const attributes = editor.getAttributes('link') as { href?: string }\n\n    setHref(attributes.href ?? '')\n    setOpen(true)\n  }\n\n  function applyLink() {\n    const nextHref = normalizeHref(href)\n\n    if (nextHref === '') {\n      editor.chain().focus().extendMarkRange('link').unsetLink().run()\n    }\n    else {\n      editor.chain().focus().extendMarkRange('link').setLink({ href: nextHref }).run()\n    }\n\n    closePopover()\n  }\n\n  function removeLink() {\n    editor.chain().focus().extendMarkRange('link').unsetLink().run()\n    closePopover()\n  }\n\n  return (\n    <Popover open={open} onOpenChange={handleOpenChange}>\n      {/* `EditorToolbarButton` does not forward the props Base UI hands to a\n          trigger, so the popup is anchored to a presentational wrapper. The\n          wrapper is not focusable: the toolbar button stays the only control. */}\n      <PopoverTrigger\n        nativeButton={false}\n        render={<span className=\"inline-flex\" role=\"presentation\" tabIndex={-1} />}\n      >\n        <EditorToolbarButton label=\"Link\" pressed={active} className={className}>\n          <LinkIcon />\n        </EditorToolbarButton>\n      </PopoverTrigger>\n      <PopoverContent\n        aria-label=\"Link\"\n        align=\"start\"\n        // Focus is handed back to the editor by the close handler instead.\n        finalFocus={false}\n      >\n        <Input\n          aria-label=\"Link URL\"\n          placeholder=\"https://example.com\"\n          value={href}\n          onChange={event => setHref(event.target.value)}\n          onKeyDown={(event) => {\n            if (event.key === 'Enter') {\n              event.preventDefault()\n              applyLink()\n            }\n          }}\n        />\n        <div className=\"flex items-center justify-end gap-1.5\">\n          <Button\n            type=\"button\"\n            variant=\"ghost\"\n            size=\"sm\"\n            onClick={removeLink}\n          >\n            Remove\n          </Button>\n          <Button type=\"button\" size=\"sm\" onClick={applyLink}>\n            Apply\n          </Button>\n        </div>\n      </PopoverContent>\n    </Popover>\n  )\n}\n",
      "type": "registry:ui",
      "target": "components/blocove/rich-text-editor/link.tsx"
    }
  ],
  "categories": [
    "rich-text-editor",
    "mark"
  ],
  "type": "registry:ui"
}