|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import React, { useEffect, useRef, useState } from "react"; |
| 4 | +import ReactMarkdown from "react-markdown"; |
| 5 | +import remarkGfm from "remark-gfm"; |
| 6 | + |
| 7 | +import { uploadPostContentImage } from "../_services/action"; |
| 8 | + |
| 9 | +export default function MarkdownEditor({ |
| 10 | + initialValue = "", |
| 11 | + onChange, |
| 12 | + onImagesUploaded, |
| 13 | +}: { |
| 14 | + initialValue?: string; |
| 15 | + onChange?: (value: string) => void; |
| 16 | + onImagesUploaded?: (ids: string[]) => void; |
| 17 | +}) { |
| 18 | + const [tab, setTab] = useState<"write" | "preview">("write"); |
| 19 | + const [value, setValue] = useState(initialValue); |
| 20 | + const [uploadedIds, setUploadedIds] = useState<string[]>([]); |
| 21 | + const textareaRef = useRef<HTMLTextAreaElement | null>(null); |
| 22 | + |
| 23 | + useEffect(() => { |
| 24 | + if (onChange) onChange(value); |
| 25 | + }, [value, onChange]); |
| 26 | + |
| 27 | + useEffect(() => { |
| 28 | + // Auto-resize textarea for nicer UX |
| 29 | + const ta = textareaRef.current; |
| 30 | + if (!ta) return; |
| 31 | + ta.style.height = "0px"; |
| 32 | + ta.style.height = Math.min(ta.scrollHeight, 800) + "px"; // limit max-height |
| 33 | + }, [value, tab]); |
| 34 | + |
| 35 | + // notify parent whenever uploadedIds changes |
| 36 | + useEffect(() => { |
| 37 | + if (onImagesUploaded) onImagesUploaded(uploadedIds); |
| 38 | + }, [uploadedIds, onImagesUploaded]); |
| 39 | + |
| 40 | + const handlePaste = async (e: React.ClipboardEvent<HTMLTextAreaElement>) => { |
| 41 | + if (!e.clipboardData || e.clipboardData.files.length === 0) return; |
| 42 | + |
| 43 | + const file = e.clipboardData.files[0]; |
| 44 | + if (!file || !file.type.startsWith("image/")) return; |
| 45 | + |
| 46 | + e.preventDefault(); |
| 47 | + |
| 48 | + const placeholderMarkdown = `![Uploading image...]()`; |
| 49 | + const textarea = textareaRef.current; |
| 50 | + if (!textarea) return; |
| 51 | + |
| 52 | + const start = textarea.selectionStart; |
| 53 | + const end = textarea.selectionEnd; |
| 54 | + const newValue = |
| 55 | + value.substring(0, start) + placeholderMarkdown + value.substring(end); |
| 56 | + |
| 57 | + setValue(newValue); |
| 58 | + |
| 59 | + try { |
| 60 | + const uploaded = await uploadPostContentImage(file); |
| 61 | + |
| 62 | + const finalMarkdown = ``; |
| 63 | + |
| 64 | + setValue((current) => |
| 65 | + current.replace(placeholderMarkdown, finalMarkdown), |
| 66 | + ); |
| 67 | + |
| 68 | + // store the uploaded ID |
| 69 | + setUploadedIds((prev) => [...prev, uploaded.id]); |
| 70 | + } catch (err) { |
| 71 | + console.error("Image upload failed:", err); |
| 72 | + setValue((current) => |
| 73 | + current.replace(placeholderMarkdown, ""), |
| 74 | + ); |
| 75 | + } |
| 76 | + }; |
| 77 | + |
| 78 | + return ( |
| 79 | + <div className="mx-auto w-full max-w-4xl rounded-2xl bg-white p-4 shadow dark:bg-gray-900"> |
| 80 | + <div className="mb-3 flex items-center justify-between"> |
| 81 | + <div className="flex gap-1 rounded-md bg-gray-100 p-1 dark:bg-gray-800"> |
| 82 | + <button |
| 83 | + type="button" |
| 84 | + onClick={() => setTab("write")} |
| 85 | + className={`rounded-md px-4 py-1 text-sm font-medium ${ |
| 86 | + tab === "write" |
| 87 | + ? "bg-white shadow dark:bg-gray-700" |
| 88 | + : "text-gray-600 dark:text-gray-300" |
| 89 | + }`} |
| 90 | + > |
| 91 | + Write |
| 92 | + </button> |
| 93 | + <button |
| 94 | + type="button" |
| 95 | + onClick={() => setTab("preview")} |
| 96 | + className={`rounded-md px-4 py-1 text-sm font-medium ${ |
| 97 | + tab === "preview" |
| 98 | + ? "bg-white shadow dark:bg-gray-700" |
| 99 | + : "text-gray-600 dark:text-gray-300" |
| 100 | + }`} |
| 101 | + > |
| 102 | + Preview |
| 103 | + </button> |
| 104 | + </div> |
| 105 | + <div className="text-xs text-gray-500 dark:text-gray-400"> |
| 106 | + Markdown Editor |
| 107 | + </div> |
| 108 | + </div> |
| 109 | + |
| 110 | + <div className="overflow-hidden rounded-lg border border-gray-200 dark:border-gray-700"> |
| 111 | + {tab === "write" ? ( |
| 112 | + <div className="p-4"> |
| 113 | + <textarea |
| 114 | + ref={textareaRef} |
| 115 | + value={value} |
| 116 | + onChange={(e) => setValue(e.target.value)} |
| 117 | + onPaste={handlePaste} |
| 118 | + placeholder="# Start typing markdown..." |
| 119 | + className="max-h-[600px] min-h-[160px] w-full resize-none bg-transparent text-sm leading-relaxed text-gray-900 outline-none dark:text-gray-100" |
| 120 | + /> |
| 121 | + </div> |
| 122 | + ) : ( |
| 123 | + <div className="prose prose-sm dark:prose-invert max-h-[600px] min-h-[160px] max-w-none overflow-y-auto p-4 text-sm"> |
| 124 | + {value.trim() ? ( |
| 125 | + <ReactMarkdown remarkPlugins={[remarkGfm]}>{value}</ReactMarkdown> |
| 126 | + ) : ( |
| 127 | + <div className="text-gray-500"> |
| 128 | + Nothing to preview — start writing markdown in the{" "} |
| 129 | + <strong>Write</strong> tab. |
| 130 | + </div> |
| 131 | + )} |
| 132 | + </div> |
| 133 | + )} |
| 134 | + </div> |
| 135 | + |
| 136 | + <div className="mt-3 flex items-center justify-between text-xs text-gray-500 dark:text-gray-400"> |
| 137 | + <div>Characters: {value.length}</div> |
| 138 | + <div className="flex gap-2"> |
| 139 | + <button |
| 140 | + type="button" |
| 141 | + onClick={() => { |
| 142 | + setValue(""); |
| 143 | + setTab("write"); |
| 144 | + }} |
| 145 | + className="rounded-md bg-red-50 px-3 py-1 text-red-600 dark:bg-red-900/30 dark:text-red-300" |
| 146 | + > |
| 147 | + Clear |
| 148 | + </button> |
| 149 | + <button |
| 150 | + type="button" |
| 151 | + onClick={() => setTab(tab === "write" ? "preview" : "write")} |
| 152 | + className="rounded-md bg-gray-50 px-3 py-1 text-gray-700 dark:bg-gray-800 dark:text-gray-200" |
| 153 | + > |
| 154 | + {tab === "write" ? "Show Preview" : "Edit"} |
| 155 | + </button> |
| 156 | + </div> |
| 157 | + </div> |
| 158 | + </div> |
| 159 | + ); |
| 160 | +} |
0 commit comments