|
| 1 | +"use client"; |
| 2 | +import React, { useEffect } from "react"; |
| 3 | +import { BubbleMenu, EditorContent, FloatingMenu, useEditor } from "@tiptap/react"; |
| 4 | +import { HardBreak } from "@tiptap/extension-hard-break"; |
| 5 | +import StarterKit from "@tiptap/starter-kit"; |
| 6 | +import EditorMenubar from "./EditorMenuBar"; |
| 7 | +import { Codepen } from "react-feather"; |
| 8 | +import { |
| 9 | + BoldIcon, |
| 10 | + Italic, |
| 11 | + Code, |
| 12 | + Heading1, |
| 13 | + Heading2, |
| 14 | + List, |
| 15 | + ListOrdered, |
| 16 | + StrikethroughIcon, |
| 17 | +} from "lucide-react"; |
| 18 | + |
| 19 | +interface EditorProps { |
| 20 | + editorState: string; |
| 21 | + setEditorState: (params: string) => void; |
| 22 | +} |
| 23 | +const Markdown = ({ editorState, setEditorState }: EditorProps) => { |
| 24 | + const CustomHardBreak = HardBreak.extend({ |
| 25 | + addKeyboardShortcuts() { |
| 26 | + return { |
| 27 | + "Mod-Enter": () => true, |
| 28 | + }; |
| 29 | + }, |
| 30 | + }); |
| 31 | + const editor = useEditor({ |
| 32 | + extensions: [StarterKit, CustomHardBreak], |
| 33 | + content: editorState, |
| 34 | + onUpdate: ({ editor }) => { |
| 35 | + setEditorState(editor.getHTML()); |
| 36 | + }, |
| 37 | + }); |
| 38 | + useEffect(() => { |
| 39 | + if (editorState === null || editorState === "") { |
| 40 | + editor?.commands.setContent(""); |
| 41 | + } |
| 42 | + }, [editorState]); |
| 43 | + |
| 44 | + const BubbleMenuFunctions = [ |
| 45 | + { |
| 46 | + title: "bold", |
| 47 | + onFunction: () => editor?.chain().focus().toggleBold().run(), |
| 48 | + icon: <BoldIcon size={16} className="font-bold " />, |
| 49 | + }, |
| 50 | + { |
| 51 | + title: "italic", |
| 52 | + onFunction: () => editor?.chain().focus().toggleItalic().run(), |
| 53 | + icon: <Italic size={16} className="font-bold " />, |
| 54 | + }, |
| 55 | + { |
| 56 | + title: "strike", |
| 57 | + onFunction: () => editor?.chain().focus().toggleStrike().run(), |
| 58 | + icon: <StrikethroughIcon size={18} className="font-bold " />, |
| 59 | + }, |
| 60 | + { |
| 61 | + title: "code", |
| 62 | + onFunction: () => editor?.chain().focus().toggleCode().run(), |
| 63 | + icon: <Code size={18} className="font-bold " />, |
| 64 | + }, |
| 65 | + { |
| 66 | + title: "codeBlck", |
| 67 | + onFunction: () => editor?.chain().focus().toggleCodeBlock().run(), |
| 68 | + icon: <Codepen size={16} className="font-bold " />, |
| 69 | + }, |
| 70 | + ]; |
| 71 | + |
| 72 | + const FloatingMenuFunctions = [ |
| 73 | + { |
| 74 | + title: `heading`, |
| 75 | + attribute: { level: 1 }, |
| 76 | + onFunction: () => editor?.chain().focus().toggleHeading({ level: 1 }).run(), |
| 77 | + icon: <Heading1 className="w-4 h-4" />, |
| 78 | + }, |
| 79 | + { |
| 80 | + title: `heading`, |
| 81 | + attribute: { level: 2 }, |
| 82 | + onFunction: () => editor?.chain().focus().toggleHeading({ level: 2 }).run(), |
| 83 | + icon: <Heading2 className="w-4 h-4" />, |
| 84 | + }, |
| 85 | + { |
| 86 | + title: `codeBlock`, |
| 87 | + attribute: { level: 1 }, |
| 88 | + onFunction: () => editor?.chain().focus().toggleCodeBlock().run(), |
| 89 | + icon: <Codepen className="w-4 h-4" />, |
| 90 | + }, |
| 91 | + { |
| 92 | + title: `bulletList`, |
| 93 | + onFunction: () => editor?.chain().focus().toggleBulletList().run(), |
| 94 | + icon: <List className="w-4 h-4" />, |
| 95 | + }, |
| 96 | + { |
| 97 | + title: `orderedList`, |
| 98 | + onFunction: () => editor?.chain().focus().toggleOrderedList().run(), |
| 99 | + icon: <ListOrdered className="w-4 h-4" />, |
| 100 | + }, |
| 101 | + ]; |
| 102 | + |
| 103 | + return ( |
| 104 | + <> |
| 105 | + <main className="dark:bg-secondary-light outline-none focus:ring rounded-lg p-3 text-white dark:text-white placholder:text-gray-400 text-lg w-full mb-2 max-w-full "> |
| 106 | + {editor && ( |
| 107 | + <BubbleMenu className="bubble-menu" tippyOptions={{ duration: 100 }} editor={editor}> |
| 108 | + <div className="flex gap-1 bg-white px-4 py-1 text-black rounded-sm"> |
| 109 | + {BubbleMenuFunctions.length > 0 && |
| 110 | + BubbleMenuFunctions.map((func) => ( |
| 111 | + <button |
| 112 | + key={func.title} |
| 113 | + role="button" |
| 114 | + onClick={(e) => { |
| 115 | + e.preventDefault(); |
| 116 | + func.onFunction(); |
| 117 | + }} |
| 118 | + className={ |
| 119 | + editor.isActive(func.title) |
| 120 | + ? "is-active rounded-sm p-0.5" |
| 121 | + : " rounded-sm p-0.5" |
| 122 | + } |
| 123 | + > |
| 124 | + {func.icon} |
| 125 | + </button> |
| 126 | + ))} |
| 127 | + </div> |
| 128 | + </BubbleMenu> |
| 129 | + )} |
| 130 | + |
| 131 | + {editor && ( |
| 132 | + <FloatingMenu className="floating-menu" tippyOptions={{ duration: 100 }} editor={editor}> |
| 133 | + <div className="flex gap-1 bg-white px-4 py-1 text-black rounded-sm"> |
| 134 | + {FloatingMenuFunctions.length > 0 && |
| 135 | + FloatingMenuFunctions.map((func) => ( |
| 136 | + <button |
| 137 | + role="button" |
| 138 | + onClick={(e) => { |
| 139 | + e.preventDefault(); |
| 140 | + func.onFunction(); |
| 141 | + }} |
| 142 | + className={ |
| 143 | + editor.isActive(func.title, func.attribute) |
| 144 | + ? "is-active rounded-sm p-0.5" |
| 145 | + : "rounded-sm p-0.5" |
| 146 | + } |
| 147 | + > |
| 148 | + {func.icon} |
| 149 | + </button> |
| 150 | + ))} |
| 151 | + </div> |
| 152 | + </FloatingMenu> |
| 153 | + )} |
| 154 | + |
| 155 | + {editor && <EditorMenubar editor={editor} />} |
| 156 | + |
| 157 | + <EditorContent className="prose dark:prose-invert mt-4" editor={editor} /> |
| 158 | + </main> |
| 159 | + </> |
| 160 | + ); |
| 161 | +}; |
| 162 | +export default Markdown; |
0 commit comments