|
|
@@ -112,6 +112,63 @@ function PostEditor() {
|
|
|
}));
|
|
|
};
|
|
|
|
|
|
+ const handlePaste = async (event) => {
|
|
|
+ const items = event.clipboardData.items;
|
|
|
+ for (const item of items) {
|
|
|
+ if (item.type.indexOf("image") === 0) {
|
|
|
+ event.preventDefault();
|
|
|
+ const file = item.getAsFile();
|
|
|
+ if (!file) continue;
|
|
|
+
|
|
|
+ const tempSlug = getSlugForUpload() || "uploads";
|
|
|
+ const placeholder = `![Uploading ${file.name}...]()...`;
|
|
|
+
|
|
|
+ // Insert placeholder
|
|
|
+ // We use api.replaceSelection if we had access to 'api' from MDEditor,
|
|
|
+ // but here we are in a raw paste handler on the textarea.
|
|
|
+ // We'll append for now or try to use a more sophisticated insertion if possible,
|
|
|
+ // but sticking to insertTextAtCursor is safer for state consistency.
|
|
|
+ // BETTER: MDEditor's `paste` command? No, native onPaste is best.
|
|
|
+ // Native paste on textarea doesn't give us cursor position easily in React state flow
|
|
|
+ // without ref manipulation.
|
|
|
+ // Let's use the standard "append" or try to insert at end.
|
|
|
+ // Actually, let's use the replaceSelection approach if we can get a ref to the editor instance,
|
|
|
+ // but we don't have it easily.
|
|
|
+ // We will append to content with a newline for simplicity and reliability.
|
|
|
+
|
|
|
+ setFormData(prev => ({ ...prev, content: prev.content + "\n" + placeholder }));
|
|
|
+
|
|
|
+ try {
|
|
|
+ const formData = new FormData();
|
|
|
+ formData.append("file", file);
|
|
|
+
|
|
|
+ const response = await fetch(`${API_BASE}/upload?slug=${tempSlug}`, {
|
|
|
+ method: "POST",
|
|
|
+ body: formData,
|
|
|
+ credentials: "include",
|
|
|
+ });
|
|
|
+
|
|
|
+ if (!response.ok) throw new Error("Upload failed");
|
|
|
+
|
|
|
+ const data = await response.json();
|
|
|
+
|
|
|
+ // Replace placeholder with actual image markdown
|
|
|
+ setFormData(prev => ({
|
|
|
+ ...prev,
|
|
|
+ content: prev.content.replace(placeholder, ``)
|
|
|
+ }));
|
|
|
+
|
|
|
+ } catch (error) {
|
|
|
+ console.error("Paste upload error:", error);
|
|
|
+ setFormData(prev => ({
|
|
|
+ ...prev,
|
|
|
+ content: prev.content.replace(placeholder, `[Upload Failed: ${error.message}]`)
|
|
|
+ }));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
// Gallery selection handler
|
|
|
const handleImageSelect = (url, name) => {
|
|
|
// We append to end since we lose cursor position when modal opens/closes
|
|
|
@@ -392,6 +449,7 @@ function PostEditor() {
|
|
|
visibleDragBar={false}
|
|
|
textareaProps={{
|
|
|
placeholder: "Tell your story...",
|
|
|
+ onPaste: handlePaste
|
|
|
}}
|
|
|
className="shadow-sm" // Add slight shadow for separation
|
|
|
/>
|