import { Editor } from "@tiptap/core"; import { Check, Trash } from "lucide-react"; import { Dispatch, FC, SetStateAction, useCallback, useEffect, useRef } from "react"; import { cn } from "../utils"; import isValidHttpUrl from "./utils/link-validator"; interface LinkSelectorProps { editor: Editor; isOpen: boolean; setIsOpen: Dispatch>; } export const LinkSelector: FC = ({ editor, isOpen, setIsOpen }) => { const inputRef = useRef(null); const onLinkSubmit = useCallback(() => { const input = inputRef.current; const url = input?.value; if (url && isValidHttpUrl(url)) { editor.chain().focus().setLink({ href: url }).run(); setIsOpen(false); } }, [editor, inputRef, setIsOpen]); useEffect(() => { inputRef.current && inputRef.current?.focus(); }); return (
{isOpen && (
{ if (e.key === "Enter") { e.preventDefault(); onLinkSubmit(); } }} > {editor.getAttributes("link").href ? ( ) : ( )}
)}
); };