{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "rich-text-editor",
  "title": "Rich Text Editor",
  "description": "A composable rich text editor built on Tiptap, with a toolbar, bubble menu, character count and typeset content.",
  "dependencies": [
    "@tiptap/core@3.32.2",
    "@tiptap/extension-bubble-menu@3.32.2",
    "@tiptap/extension-document@3.32.2",
    "@tiptap/extension-floating-menu@3.32.2",
    "@tiptap/extension-text-align@3.32.2",
    "@tiptap/extensions@3.32.2",
    "@tiptap/pm@3.32.2",
    "@tiptap/react@3.32.2",
    "cn",
    "lucide-react"
  ],
  "registryDependencies": [
    "litingyes/blocove/rich-text-editor-extension",
    "litingyes/blocove/rich-text-editor-text",
    "litingyes/blocove/rich-text-editor-paragraph",
    "litingyes/blocove/rich-text-editor-hard-break",
    "button",
    "kbd",
    "separator",
    "toggle",
    "tooltip"
  ],
  "files": [
    {
      "path": "src/registry/blocove/ui/rich-text-editor/editor.tsx",
      "content": "import type { EditorOptions, Extensions, Editor as TiptapEditor } from '@tiptap/react'\nimport type { ReactNode } from 'react'\nimport { Document } from '@tiptap/extension-document'\nimport { TextAlign } from '@tiptap/extension-text-align'\nimport { CharacterCount, Placeholder, UndoRedo } from '@tiptap/extensions'\nimport {\n  EditorContext,\n  EditorContent as TiptapEditorContent,\n  useCurrentEditor,\n  useEditor,\n  useEditorState,\n} from '@tiptap/react'\nimport { BubbleMenu, FloatingMenu } from '@tiptap/react/menus'\nimport { cn } from 'cn'\nimport {\n  AlignCenterIcon,\n  AlignLeftIcon,\n  AlignRightIcon,\n  EraserIcon,\n  Redo2Icon,\n  Undo2Icon,\n} from 'lucide-react'\nimport { useEffect, useRef } from 'react'\nimport { Button } from '@/components/ui/button'\nimport { Kbd } from '@/components/ui/kbd'\nimport { Separator } from '@/components/ui/separator'\nimport { Toggle } from '@/components/ui/toggle'\nimport {\n  Tooltip,\n  TooltipContent,\n  TooltipProvider,\n  TooltipTrigger,\n} from '@/components/ui/tooltip'\nimport {\n  collectEditorChildren,\n  dedupeExtensions,\n  extensionSignature,\n  resolveExtensions,\n} from '@/lib/blocove/rich-text-editor-extension'\n\nexport interface EditorProps {\n  /** Controlled HTML content. */\n  value?: string\n  /** Initial HTML content when uncontrolled. */\n  defaultValue?: string\n  /** Called with the new HTML whenever the document changes. */\n  onChange?: (html: string) => void\n  /** Placeholder shown while the document is empty. */\n  placeholder?: string\n  /** Whether the document can be edited. */\n  editable?: boolean\n  /** Focus the editor once it is ready. */\n  autoFocus?: boolean\n  /** Maximum character count, enforced by the CharacterCount extension. */\n  characterLimit?: number\n  /** Additional Tiptap extensions, appended after the declared ones. */\n  extensions?: Extensions\n  /**\n   * Whether the editor registers its own undo/redo history.\n   *\n   * Defaults to `false` when a collaboration extension is present, because\n   * collaboration carries its own undo manager, and to `true` otherwise.\n   */\n  history?: boolean\n  /** Called once the editor instance is created. */\n  onReady?: (editor: TiptapEditor) => void\n  className?: string\n  /** ProseMirror editor props, merged with the defaults. */\n  editorProps?: EditorOptions['editorProps']\n  /** Editor UI and extension declarations. */\n  children?: ReactNode\n}\n\n/**\n * Rich text editor root. Registers the extensions declared by its children,\n * provides the Tiptap editor context and renders the editor UI.\n */\nexport function Editor({\n  value,\n  defaultValue,\n  onChange,\n  placeholder = 'Start writing…',\n  editable = true,\n  autoFocus = false,\n  characterLimit,\n  extensions,\n  history,\n  onReady,\n  className,\n  editorProps,\n  children,\n}: EditorProps) {\n  const { extensions: extensionConfigs } = collectEditorChildren(children)\n  const configSignature = extensionSignature(extensionConfigs)\n  const rawExtensionSignature = (extensions ?? [])\n    .map(extension => extension.name)\n    .join(',')\n  const onChangeRef = useRef(onChange)\n  const onReadyRef = useRef(onReady)\n  const lastEmittedRef = useRef<string | null>(null)\n  const declaredExtensions = [\n    ...resolveExtensions(extensionConfigs),\n    ...(extensions ?? []),\n  ]\n  // A collaborative document belongs to Yjs. Seeding it from React props, or\n  // pushing a controlled `value` back into it, would either overwrite a peer's\n  // edits or fight the sync plugin over the same transaction.\n  const hasCollaboration = declaredExtensions.some(\n    extension => extension.name === 'collaboration',\n  )\n  // Collaboration ships its own undo manager, so registering the editor's\n  // history next to it would fight over the same transaction metadata.\n  const historyEnabled = history ?? !hasCollaboration\n\n  onChangeRef.current = onChange\n  onReadyRef.current = onReady\n\n  const editor = useEditor(\n    {\n      immediatelyRender: false,\n      autofocus: autoFocus ? 'end' : false,\n      editable,\n      content: hasCollaboration\n        ? undefined\n        : value ?? defaultValue ?? '',\n      extensions: dedupeExtensions([\n        Document,\n        ...declaredExtensions,\n        Placeholder.configure({ placeholder }),\n        CharacterCount.configure({ limit: characterLimit }),\n        TextAlign.configure({\n          types: ['heading', 'paragraph'],\n        }),\n        ...(historyEnabled\n          ? [UndoRedo]\n          : []),\n      ]),\n      editorProps: {\n        attributes: {\n          'aria-label': 'Rich text editor',\n          'aria-multiline': 'true',\n          'class': 'focus:outline-none',\n        },\n        ...editorProps,\n      },\n      onCreate: ({ editor: instance }) => {\n        onReadyRef.current?.(instance)\n      },\n      onUpdate: ({ editor: instance }) => {\n        const html = instance.getHTML()\n\n        if (html === lastEmittedRef.current) {\n          return\n        }\n\n        lastEmittedRef.current = html\n        onChangeRef.current?.(html)\n      },\n    },\n    [configSignature, rawExtensionSignature, historyEnabled],\n  )\n\n  useEffect(() => {\n    if (!editor) {\n      return\n    }\n\n    // Tiptap emits an `update` event by default when only the editable flag\n    // changes, which would call `onChange` with unchanged HTML on mount and on\n    // every `editable` toggle. Suppress it: `onChange` reports document edits.\n    editor.setEditable(editable, false)\n  }, [editor, editable])\n\n  useEffect(() => {\n    if (!editor || value === undefined || hasCollaboration) {\n      return\n    }\n\n    if (value === editor.getHTML()) {\n      return\n    }\n\n    lastEmittedRef.current = value\n    editor.commands.setContent(value, { emitUpdate: false })\n  }, [editor, value, hasCollaboration])\n\n  return (\n    <EditorContext value={{ editor }}>\n      <div\n        data-slot=\"editor\"\n        className={cn(\n          'flex w-full flex-col overflow-hidden rounded-lg border border-input bg-transparent text-foreground transition-colors focus-within:border-ring focus-within:ring-3 focus-within:ring-ring/50 dark:bg-input/30',\n          className,\n        )}\n      >\n        {children}\n      </div>\n    </EditorContext>\n  )\n}\n\nexport interface EditorContentProps extends React.ComponentProps<'div'> {\n  /** Apply the shadcn/typeset styles to the rendered document. */\n  typeset?: boolean\n}\n\nconst typesetClassName = [\n  'typeset typeset-docs',\n  'min-h-0',\n  '[&_.ProseMirror]:min-h-24',\n  '[&_.ProseMirror]:break-words',\n  '[&_.ProseMirror]:outline-none',\n  '[&_.ProseMirror-focused]:outline-none',\n  '[&_.is-editor-empty:first-child::before]:pointer-events-none',\n  '[&_.is-editor-empty:first-child::before]:float-left',\n  '[&_.is-editor-empty:first-child::before]:h-0',\n  '[&_.is-editor-empty:first-child::before]:text-muted-foreground',\n  '[&_.is-editor-empty:first-child::before]:content-[attr(data-placeholder)]',\n].join(' ')\n\n/**\n * The editable surface. Wraps the ProseMirror content in a typeset container;\n * the container must not carry `data-slot` so typeset styles reach the content.\n */\nexport function EditorContent({\n  className,\n  typeset = true,\n  ...props\n}: EditorContentProps) {\n  const { editor } = useCurrentEditor()\n\n  return (\n    <div\n      data-slot=\"editor-content\"\n      className={cn('min-h-0 flex-1 px-3 py-2.5', className)}\n      {...props}\n    >\n      <div className={cn(typeset ? typesetClassName : 'min-h-0')}>\n        {editor\n          ? <TiptapEditorContent editor={editor} />\n          : <div className=\"min-h-24\" />}\n      </div>\n    </div>\n  )\n}\n\nexport interface EditorToolbarProps extends React.ComponentProps<'div'> {\n  /** Extension controls rendered between the history and alignment groups. */\n  children?: ReactNode\n}\n\n/**\n * Editor toolbar. Renders the history and alignment controls and slots the\n * extension-provided controls in between.\n */\nexport function EditorToolbar({\n  className,\n  children,\n  ...props\n}: EditorToolbarProps) {\n  const { editor } = useCurrentEditor()\n\n  return (\n    <TooltipProvider>\n      <div\n        data-slot=\"editor-toolbar\"\n        role=\"toolbar\"\n        aria-label=\"Formatting\"\n        className={cn(\n          'flex flex-wrap items-center gap-1 border-b p-1.5',\n          className,\n        )}\n        {...props}\n      >\n        {editor\n          ? <EditorToolbarContent editor={editor}>{children}</EditorToolbarContent>\n          : <div className=\"h-7\" aria-hidden />}\n      </div>\n    </TooltipProvider>\n  )\n}\n\nfunction EditorToolbarContent({\n  editor,\n  children,\n}: {\n  editor: TiptapEditor\n  children?: ReactNode\n}) {\n  const state = useEditorState({\n    editor,\n    selector: ({ editor: instance }) => ({\n      canUndo: instance.can().undo(),\n      canRedo: instance.can().redo(),\n      alignLeft: instance.isActive({ textAlign: 'left' }),\n      alignCenter: instance.isActive({ textAlign: 'center' }),\n      alignRight: instance.isActive({ textAlign: 'right' }),\n    }),\n  })\n\n  return (\n    <>\n      <EditorToolbarButton\n        label=\"Undo\"\n        shortcut=\"Mod Z\"\n        disabled={!state.canUndo}\n        onClick={() => editor.chain().focus().undo().run()}\n      >\n        <Undo2Icon />\n      </EditorToolbarButton>\n      <EditorToolbarButton\n        label=\"Redo\"\n        shortcut=\"Shift Mod Z\"\n        disabled={!state.canRedo}\n        onClick={() => editor.chain().focus().redo().run()}\n      >\n        <Redo2Icon />\n      </EditorToolbarButton>\n\n      {children\n        ? (\n            <>\n              <Separator\n                orientation=\"vertical\"\n                className=\"mx-1 h-5 self-center\"\n              />\n              {children}\n            </>\n          )\n        : null}\n\n      <Separator orientation=\"vertical\" className=\"mx-1 h-5 self-center\" />\n      <EditorToolbarButton\n        label=\"Align left\"\n        pressed={state.alignLeft}\n        onPressedChange={() =>\n          editor.chain().focus().setTextAlign('left').run()}\n      >\n        <AlignLeftIcon />\n      </EditorToolbarButton>\n      <EditorToolbarButton\n        label=\"Align center\"\n        pressed={state.alignCenter}\n        onPressedChange={() =>\n          editor.chain().focus().setTextAlign('center').run()}\n      >\n        <AlignCenterIcon />\n      </EditorToolbarButton>\n      <EditorToolbarButton\n        label=\"Align right\"\n        pressed={state.alignRight}\n        onPressedChange={() =>\n          editor.chain().focus().setTextAlign('right').run()}\n      >\n        <AlignRightIcon />\n      </EditorToolbarButton>\n\n      <Separator orientation=\"vertical\" className=\"mx-1 h-5 self-center\" />\n      <EditorToolbarButton\n        label=\"Clear formatting\"\n        onClick={() =>\n          editor.chain().focus().unsetAllMarks().clearNodes().run()}\n      >\n        <EraserIcon />\n      </EditorToolbarButton>\n    </>\n  )\n}\n\nexport interface EditorToolbarButtonProps {\n  label: string\n  shortcut?: string\n  /** Omit to render a plain action button instead of a toggle. */\n  pressed?: boolean\n  onPressedChange?: (pressed: boolean) => void\n  onClick?: () => void\n  disabled?: boolean\n  className?: string\n  children?: ReactNode\n}\n\n/**\n * A single toolbar control with a tooltip and an optional keyboard shortcut.\n * Used by the built-in controls and by every extension item.\n */\nexport function EditorToolbarButton({\n  label,\n  shortcut,\n  pressed,\n  onPressedChange,\n  onClick,\n  disabled,\n  className,\n  children,\n}: EditorToolbarButtonProps) {\n  const control = pressed === undefined\n    ? (\n        <Button\n          type=\"button\"\n          variant=\"ghost\"\n          size=\"icon-sm\"\n          aria-label={label}\n          disabled={disabled}\n          onClick={onClick}\n          className={className}\n        >\n          {children}\n        </Button>\n      )\n    : (\n        <Toggle\n          type=\"button\"\n          aria-label={label}\n          size=\"sm\"\n          pressed={pressed}\n          onPressedChange={onPressedChange}\n          disabled={disabled}\n          className={cn('h-7 min-w-7 px-0', className)}\n        >\n          {children}\n        </Toggle>\n      )\n\n  return (\n    <Tooltip>\n      <TooltipTrigger render={control} />\n      <TooltipContent side=\"bottom\" className=\"flex items-center gap-1.5\">\n        <span>{label}</span>\n        {shortcut\n          ? <Kbd>{shortcut}</Kbd>\n          : null}\n      </TooltipContent>\n    </Tooltip>\n  )\n}\n\nexport interface EditorBubbleMenuProps {\n  className?: string\n  /** Mark controls rendered inside the floating menu. */\n  children?: ReactNode\n}\n\n/**\n * Floating formatting menu shown above a text selection.\n */\nexport function EditorBubbleMenu({\n  className,\n  children,\n}: EditorBubbleMenuProps) {\n  const { editor } = useCurrentEditor()\n\n  if (!editor || !children) {\n    return null\n  }\n\n  return (\n    <BubbleMenu\n      editor={editor}\n      data-slot=\"editor-bubble-menu\"\n      options={{ placement: 'top' }}\n      shouldShow={({ editor: instance, state }) =>\n        !state.selection.empty\n        && instance.isEditable\n        && !instance.isActive('codeBlock')}\n      className={cn(\n        'flex items-center gap-0.5 rounded-lg border bg-popover p-1 text-popover-foreground shadow-md ring-1 ring-foreground/10',\n        className,\n      )}\n    >\n      <TooltipProvider>{children}</TooltipProvider>\n    </BubbleMenu>\n  )\n}\n\nexport interface EditorFloatingMenuProps {\n  className?: string\n  /** Controls rendered inside the floating menu. */\n  children?: ReactNode\n}\n\n/**\n * Floating menu anchored to the block the caret sits in. It only shows on an\n * empty block, which is where an insertion affordance such as a slash menu or a\n * quick insert control belongs.\n */\nexport function EditorFloatingMenu({\n  className,\n  children,\n}: EditorFloatingMenuProps) {\n  const { editor } = useCurrentEditor()\n\n  if (!editor || !children) {\n    return null\n  }\n\n  return (\n    <FloatingMenu\n      editor={editor}\n      data-slot=\"editor-floating-menu\"\n      options={{ placement: 'bottom-start' }}\n      shouldShow={({ editor: instance, state }) =>\n        instance.isEditable\n        && state.selection.empty\n        && !instance.isActive('codeBlock')\n        && state.selection.$from.parent.content.size === 0}\n      className={cn(\n        'flex items-center gap-0.5 rounded-lg border bg-popover p-1 text-popover-foreground shadow-md ring-1 ring-foreground/10',\n        className,\n      )}\n    >\n      <TooltipProvider>{children}</TooltipProvider>\n    </FloatingMenu>\n  )\n}\n\nexport interface EditorCharacterCountProps {\n  className?: string\n  format?: 'characters' | 'words' | 'both'\n}\n\n/**\n * Live character/word count for the current document.\n */\nexport function EditorCharacterCount({\n  className,\n  format = 'characters',\n}: EditorCharacterCountProps) {\n  const { editor } = useCurrentEditor()\n\n  return (\n    <div\n      data-slot=\"editor-character-count\"\n      className={cn(\n        'flex items-center justify-end gap-3 border-t px-3 py-1.5 text-xs text-muted-foreground',\n        className,\n      )}\n    >\n      {editor\n        ? <EditorCharacterCountContent editor={editor} format={format} />\n        : <span>0 characters</span>}\n    </div>\n  )\n}\n\nfunction EditorCharacterCountContent({\n  editor,\n  format,\n}: {\n  editor: TiptapEditor\n  format: NonNullable<EditorCharacterCountProps['format']>\n}) {\n  const counts = useEditorState({\n    editor,\n    selector: ({ editor: instance }) => {\n      const storage = instance.storage.characterCount as {\n        characters: () => number\n        words: () => number\n      }\n      const limit = instance.extensionManager.extensions.find(\n        extension => extension.name === 'characterCount',\n      )?.options.limit as number | null | undefined\n\n      return {\n        characters: storage.characters(),\n        words: storage.words(),\n        limit: limit ?? null,\n      }\n    },\n  })\n\n  const parts = format === 'words'\n    ? [`${counts.words} words`]\n    : format === 'both'\n      ? [`${counts.characters} characters`, `${counts.words} words`]\n      : [\n          counts.limit === null\n            ? `${counts.characters} characters`\n            : `${counts.characters} / ${counts.limit} characters`,\n        ]\n\n  return (\n    <>\n      {parts.map(part => (\n        <span key={part}>{part}</span>\n      ))}\n    </>\n  )\n}\n",
      "type": "registry:ui",
      "target": "components/blocove/rich-text-editor/editor.tsx"
    }
  ],
  "categories": [
    "rich-text-editor"
  ],
  "type": "registry:ui"
}