import { Editor, useEditorState } from "@tiptap/react"; import { AlignCenter, AlignLeft, AlignRight, Bold, Heading1, Heading2, Heading3, Highlighter, Italic, List, ListOrdered, } from "lucide-react"; import { Toggle } from "../ui/toggle"; export default function Menubar({ editor }: { editor: Editor | null }) { if (!editor) { return null; } const editorState = useEditorState({ editor, selector: (ctx) => { return { // Text formatting isBold: ctx.editor.isActive("bold") ?? false, isItalic: ctx.editor.isActive("italic") ?? false, isStrike: ctx.editor.isActive("strike") ?? false, isHighlight: ctx.editor.isActive("highlight") ?? false, // Text alignment isAlignLeft: ctx.editor.isActive({ textAlign: "left" }) ?? false, isAlignCenter: ctx.editor.isActive({ textAlign: "center" }) ?? false, isAlignRight: ctx.editor.isActive({ textAlign: "right" }) ?? false, isAlignJustify: ctx.editor.isActive({ textAlign: "justify" }) ?? false, // Block types isParagraph: ctx.editor.isActive("paragraph") ?? false, isHeading1: ctx.editor.isActive("heading", { level: 1 }) ?? false, isHeading2: ctx.editor.isActive("heading", { level: 2 }) ?? false, isHeading3: ctx.editor.isActive("heading", { level: 3 }) ?? false, }; }, }); const options: { icon: React.ReactNode; onClick: () => void; pressed: boolean; }[] = [ { icon: , onClick: () => editor.chain().focus().toggleHeading({ level: 1 }).run(), pressed: editor.isActive("heading", { level: 1 }), }, { icon: , onClick: () => editor.chain().focus().toggleHeading({ level: 2 }).run(), pressed: editor.isActive("heading", { level: 2 }), }, { icon: , onClick: () => editor.chain().focus().toggleHeading({ level: 3 }).run(), pressed: editor.isActive("heading", { level: 3 }), }, { icon: , onClick: () => editor.chain().focus().toggleBold().run(), pressed: editor.isActive("bold"), }, { icon: , onClick: () => editor.chain().focus().toggleItalic().run(), pressed: editor.isActive("italic"), }, { icon: , onClick: () => editor.chain().focus().toggleTextAlign("left").run(), pressed: editor.isActive({ textAlign: "left" }), }, { icon: , onClick: () => editor.chain().focus().toggleTextAlign("center").run(), pressed: editor.isActive({ textAlign: "center" }), }, { icon: , onClick: () => editor.chain().focus().toggleTextAlign("right").run(), pressed: editor.isActive({ textAlign: "right" }), }, { icon: , onClick: () => editor.chain().focus().toggleBulletList().run(), pressed: editor.isActive("bulletList"), }, { icon: , onClick: () => editor.chain().focus().toggleOrderedList().run(), pressed: editor.isActive("orderList"), }, { icon: , onClick: () => editor.chain().focus().toggleHighlight().run(), pressed: editor.isActive("highlight"), }, ]; return (
{options.map((option, index) => ( {option.icon} ))}
); }