Skip to content

Commit

Permalink
feat[DRAG&DROP]: Added drag and drop support to upload images and vids
Browse files Browse the repository at this point in the history
Now images can be copied, pasted and dragged into the post editor to make user's loif a lot easier :)
  • Loading branch information
parazeeknova committed Oct 16, 2024
1 parent 206b822 commit f858c28
Showing 1 changed file with 26 additions and 5 deletions.
31 changes: 26 additions & 5 deletions apps/web/src/components/Posts/editor/PostEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ import "./styles.css";
import { Button } from "@/components/ui/button";
import { cn } from "@/lib/utils";
import { useSubmitPostMutation } from "@/posts/editor/mutations";
import { useDropzone } from "@uploadthing/react";
import { ImageIcon, Loader2, X } from "lucide-react";
import Image from "next/image";
import { useRef } from "react";
import { type ClipboardEvent, useRef } from "react";
import useMediaUpload, { type Attachment } from "./useMediaUpload";

export default function PostEditor() {
Expand All @@ -31,6 +32,12 @@ export default function PostEditor() {
reset: resetMediaUploads
} = useMediaUpload();

const { getRootProps, getInputProps, isDragActive } = useDropzone({
onDrop: startUpload
});

const { onClick, ...rootProps } = getRootProps();

const editor = useEditor({
extensions: [
StarterKit.configure({
Expand Down Expand Up @@ -63,14 +70,28 @@ export default function PostEditor() {
);
}

function onPaste(e: ClipboardEvent<HTMLInputElement>) {
const files = Array.from(e.clipboardData.items)
.filter((item) => item.kind === "file")
.map((item) => item.getAsFile()) as File[];
startUpload(files);
}

return (
<div className="flex flex-col gap-5 rounded-2xl border border-border bg-card p-5 shadow-sm">
<div className="flex gap-5">
<UserAvatar avatarUrl={user.avatarUrl} className="hidden sm:inline" />
<EditorContent
editor={editor}
className="editor-content max-h-[20rem] w-full flex-grow overflow-y-auto rounded-2xl bg-[hsl(var(--background-alt))] px-5 py-5 text-foreground"
/>
<div {...rootProps} className="w-full">
<EditorContent
editor={editor}
className={cn(
"max-h-[20rem] w-full overflow-y-auto rounded-2xl bg-[hsl(var(--background-alt))] px-5 py-3 text-foreground",
isDragActive && "outline-dashed"
)}
onPaste={onPaste}
/>
<input {...getInputProps()} />
</div>
</div>
{!!attachments.length && (
<AttachmentPreviews
Expand Down

0 comments on commit f858c28

Please sign in to comment.