{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "rich-text-editor-find-and-replace",
  "title": "Find and Replace",
  "description": "Search, navigate and replace text from a toolbar popover.",
  "dependencies": [
    "@tiptap/extension-find-and-replace@3.32.2",
    "@tiptap/react@3.32.2",
    "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/find-and-replace.tsx",
      "content": "import type { FindAndReplaceOptions } from '@tiptap/extension-find-and-replace'\nimport type { Editor } from '@tiptap/react'\nimport type { ExtensionComponent } from '@/lib/blocove/rich-text-editor-extension'\nimport { FindAndReplace } from '@tiptap/extension-find-and-replace'\nimport { useCurrentEditor, useEditorState } from '@tiptap/react'\nimport { SearchIcon } from 'lucide-react'\nimport { useRef, 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/**\n * Registers the find and replace extension. Every search and replace action is\n * a command, so the toolbar below is only one way to drive it.\n */\nexport const EditorFindAndReplace: ExtensionComponent<Partial<FindAndReplaceOptions>> = defineExtensionComponent(\n  'find-and-replace',\n  props => [\n    FindAndReplace.configure(props),\n  ],\n)\n\nexport interface EditorFindAndReplaceToolbarProps {\n  className?: string\n}\n\n/**\n * Toolbar control for searching and replacing inside the document.\n */\nexport function EditorFindAndReplaceToolbar({\n  className,\n}: EditorFindAndReplaceToolbarProps) {\n  const { editor } = useCurrentEditor()\n\n  if (!editor) {\n    return null\n  }\n\n  return <EditorFindAndReplaceToolbarContent editor={editor} className={className} />\n}\n\ninterface FindAndReplaceStorage {\n  results: unknown[]\n  currentIndex: number | null\n  caseSensitive: boolean\n  useRegex: boolean\n  wholeWord: boolean\n}\n\nfunction EditorFindAndReplaceToolbarContent({\n  editor,\n  className,\n}: {\n  editor: Editor\n  className?: string\n}) {\n  const [open, setOpen] = useState(false)\n  const [search, setSearch] = useState('')\n  const [replacement, setReplacement] = useState('')\n  // An input method composes text before it commits it, so searching on every\n  // intermediate value would move the highlights while the user is still\n  // choosing a character.\n  const composingRef = useRef(false)\n  const state = useEditorState({\n    editor,\n    selector: ({ editor: instance }) => {\n      const storage = instance.storage.findAndReplace as FindAndReplaceStorage\n\n      return {\n        matches: storage.results.length,\n        current: storage.currentIndex === null\n          ? 0\n          : storage.currentIndex + 1,\n      }\n    },\n  })\n\n  function searchFor(value: string) {\n    if (composingRef.current) {\n      return\n    }\n\n    editor.commands.setSearchTerm(value)\n  }\n\n  return (\n    <Popover open={open} onOpenChange={setOpen}>\n      {/* `EditorToolbarButton` does not forward the trigger props Base UI hands\n          out, so the popup is anchored to a presentational wrapper. */}\n      <PopoverTrigger\n        nativeButton={false}\n        render={<span className=\"inline-flex\" role=\"presentation\" tabIndex={-1} />}\n      >\n        <EditorToolbarButton label=\"Find and replace\" className={className}>\n          <SearchIcon />\n        </EditorToolbarButton>\n      </PopoverTrigger>\n      <PopoverContent aria-label=\"Find and replace\" align=\"start\" className=\"w-80\">\n        <div className=\"flex flex-col gap-2\">\n          <Input\n            aria-label=\"Find\"\n            placeholder=\"Find\"\n            value={search}\n            onChange={(event) => {\n              setSearch(event.target.value)\n              searchFor(event.target.value)\n            }}\n            onCompositionStart={() => {\n              composingRef.current = true\n            }}\n            onCompositionEnd={(event) => {\n              composingRef.current = false\n              searchFor(event.currentTarget.value)\n            }}\n          />\n          <Input\n            aria-label=\"Replace with\"\n            placeholder=\"Replace with\"\n            value={replacement}\n            onChange={(event) => {\n              setReplacement(event.target.value)\n              editor.commands.setReplaceTerm(event.target.value)\n            }}\n          />\n          <p aria-live=\"polite\" className=\"text-xs text-muted-foreground\">\n            {state.matches === 0\n              ? 'No results'\n              : `${state.current} of ${state.matches}`}\n          </p>\n          <div className=\"flex flex-wrap items-center justify-end gap-1\">\n            <Button\n              type=\"button\"\n              variant=\"ghost\"\n              size=\"sm\"\n              onClick={() => editor.commands.goToPreviousResult()}\n            >\n              Previous\n            </Button>\n            <Button\n              type=\"button\"\n              variant=\"ghost\"\n              size=\"sm\"\n              onClick={() => editor.commands.goToNextResult()}\n            >\n              Next\n            </Button>\n            <Button\n              type=\"button\"\n              size=\"sm\"\n              onClick={() => editor.commands.replace()}\n            >\n              Replace\n            </Button>\n            <Button\n              type=\"button\"\n              variant=\"secondary\"\n              size=\"sm\"\n              onClick={() => editor.commands.replaceAll()}\n            >\n              Replace all\n            </Button>\n          </div>\n          <div className=\"flex items-center justify-between\">\n            <span className=\"flex items-center gap-2 text-xs text-muted-foreground\">\n              <Button type=\"button\" variant=\"ghost\" size=\"sm\" aria-label=\"Match case\" onClick={() => editor.commands.setCaseSensitive(!(editor.storage.findAndReplace as FindAndReplaceStorage).caseSensitive)}>\n                Aa\n              </Button>\n              <Button type=\"button\" variant=\"ghost\" size=\"sm\" aria-label=\"Use regular expression\" onClick={() => editor.commands.setUseRegex(!(editor.storage.findAndReplace as FindAndReplaceStorage).useRegex)}>\n                .*\n              </Button>\n              <Button type=\"button\" variant=\"ghost\" size=\"sm\" aria-label=\"Match whole word\" onClick={() => editor.commands.setWholeWord(!(editor.storage.findAndReplace as FindAndReplaceStorage).wholeWord)}>\n                Word\n              </Button>\n            </span>\n            <Button\n              type=\"button\"\n              variant=\"ghost\"\n              size=\"sm\"\n              onClick={() => {\n                setSearch('')\n                editor.commands.clearSearch()\n              }}\n            >\n              Clear\n            </Button>\n          </div>\n        </div>\n      </PopoverContent>\n    </Popover>\n  )\n}\n",
      "type": "registry:ui",
      "target": "components/blocove/rich-text-editor/find-and-replace.tsx"
    }
  ],
  "categories": [
    "rich-text-editor",
    "extension"
  ],
  "type": "registry:ui"
}