Merge pull request #6 from Sompalkar/fix/timeline-clip-deletion

fix: delete only selected clip from timeline
This commit is contained in:
iza
2025-06-23 13:24:18 +03:00
committed by GitHub
4 changed files with 731 additions and 181 deletions

1
.gitignore vendored
View File

@ -18,3 +18,4 @@
# typescript # typescript
/apps/web/next-env.d.ts /apps/web/next-env.d.ts
/apps/web/yarn.lock

View File

@ -10,21 +10,28 @@ import { useDragDrop } from "@/hooks/use-drag-drop";
import { useRef, useState } from "react"; import { useRef, useState } from "react";
import { toast } from "sonner"; import { toast } from "sonner";
// MediaPanel lets users add, view, and drag media (images, videos, audio) into the project.
// You can upload files or drag them from your computer. Dragging from here to the timeline adds them to your video project.
export function MediaPanel() { export function MediaPanel() {
const { mediaItems, addMediaItem, removeMediaItem } = useMediaStore(); const { mediaItems, addMediaItem, removeMediaItem } = useMediaStore();
const fileInputRef = useRef<HTMLInputElement>(null); const fileInputRef = useRef<HTMLInputElement>(null);
const [isProcessing, setIsProcessing] = useState(false); const [isProcessing, setIsProcessing] = useState(false);
const processFiles = async (files: FileList | File[]) => { const processFiles = async (files: FileList | File[]) => {
// If no files, do nothing
if (!files?.length) return; if (!files?.length) return;
setIsProcessing(true); setIsProcessing(true);
try { try {
// Process files (extract metadata, generate thumbnails, etc.)
const items = await processMediaFiles(files); const items = await processMediaFiles(files);
items.forEach(item => { // Add each processed media item to the store
items.forEach((item) => {
addMediaItem(item); addMediaItem(item);
}); });
} catch (error) { } catch (error) {
// Show error if processing fails
console.error("File processing failed:", error); console.error("File processing failed:", error);
toast.error("Failed to process files"); toast.error("Failed to process files");
} finally { } finally {
@ -33,37 +40,47 @@ export function MediaPanel() {
}; };
const { isDragOver, dragProps } = useDragDrop({ const { isDragOver, dragProps } = useDragDrop({
// When files are dropped, process them
onDrop: processFiles, onDrop: processFiles,
}); });
const handleFileSelect = () => fileInputRef.current?.click(); const handleFileSelect = () => fileInputRef.current?.click(); // Open file picker
const handleFileChange = (e: React.ChangeEvent<HTMLInputElement>) => { const handleFileChange = (e: React.ChangeEvent<HTMLInputElement>) => {
// When files are selected via file picker, process them
if (e.target.files) processFiles(e.target.files); if (e.target.files) processFiles(e.target.files);
e.target.value = ""; e.target.value = ""; // Reset input
}; };
const handleRemove = (e: React.MouseEvent, id: string) => { const handleRemove = (e: React.MouseEvent, id: string) => {
// Remove a media item from the store
e.stopPropagation(); e.stopPropagation();
removeMediaItem(id); removeMediaItem(id);
}; };
const formatDuration = (duration: number) => { const formatDuration = (duration: number) => {
// Format seconds as mm:ss
const min = Math.floor(duration / 60); const min = Math.floor(duration / 60);
const sec = Math.floor(duration % 60); const sec = Math.floor(duration % 60);
return `${min}:${sec.toString().padStart(2, "0")}`; return `${min}:${sec.toString().padStart(2, "0")}`;
}; };
const startDrag = (e: React.DragEvent, item: any) => { const startDrag = (e: React.DragEvent, item: any) => {
e.dataTransfer.setData("application/x-media-item", JSON.stringify({ // When dragging a media item, set drag data for timeline to read
e.dataTransfer.setData(
"application/x-media-item",
JSON.stringify({
id: item.id, id: item.id,
type: item.type, type: item.type,
name: item.name, name: item.name,
})); })
);
e.dataTransfer.effectAllowed = "copy"; e.dataTransfer.effectAllowed = "copy";
}; };
const renderPreview = (item: any) => { const renderPreview = (item: any) => {
// Render a preview for each media type (image, video, audio, unknown)
// Each preview is draggable to the timeline
const baseDragProps = { const baseDragProps = {
draggable: true, draggable: true,
onDragStart: (e: React.DragEvent) => startDrag(e, item), onDragStart: (e: React.DragEvent) => startDrag(e, item),
@ -84,7 +101,10 @@ export function MediaPanel() {
if (item.type === "video") { if (item.type === "video") {
if (item.thumbnailUrl) { if (item.thumbnailUrl) {
return ( return (
<div className="relative w-full h-full cursor-grab active:cursor-grabbing" {...baseDragProps}> <div
className="relative w-full h-full cursor-grab active:cursor-grabbing"
{...baseDragProps}
>
<img <img
src={item.thumbnailUrl} src={item.thumbnailUrl}
alt={item.name} alt={item.name}
@ -103,11 +123,16 @@ export function MediaPanel() {
); );
} }
return ( return (
<div className="w-full h-full bg-muted/30 flex flex-col items-center justify-center text-muted-foreground rounded cursor-grab active:cursor-grabbing" {...baseDragProps}> <div
className="w-full h-full bg-muted/30 flex flex-col items-center justify-center text-muted-foreground rounded cursor-grab active:cursor-grabbing"
{...baseDragProps}
>
<Video className="h-6 w-6 mb-1" /> <Video className="h-6 w-6 mb-1" />
<span className="text-xs">Video</span> <span className="text-xs">Video</span>
{item.duration && ( {item.duration && (
<span className="text-xs opacity-70">{formatDuration(item.duration)}</span> <span className="text-xs opacity-70">
{formatDuration(item.duration)}
</span>
)} )}
</div> </div>
); );
@ -115,18 +140,26 @@ export function MediaPanel() {
if (item.type === "audio") { if (item.type === "audio") {
return ( return (
<div className="w-full h-full bg-gradient-to-br from-green-500/20 to-emerald-500/20 flex flex-col items-center justify-center text-muted-foreground rounded border border-green-500/20 cursor-grab active:cursor-grabbing" {...baseDragProps}> <div
className="w-full h-full bg-gradient-to-br from-green-500/20 to-emerald-500/20 flex flex-col items-center justify-center text-muted-foreground rounded border border-green-500/20 cursor-grab active:cursor-grabbing"
{...baseDragProps}
>
<Music className="h-6 w-6 mb-1" /> <Music className="h-6 w-6 mb-1" />
<span className="text-xs">Audio</span> <span className="text-xs">Audio</span>
{item.duration && ( {item.duration && (
<span className="text-xs opacity-70">{formatDuration(item.duration)}</span> <span className="text-xs opacity-70">
{formatDuration(item.duration)}
</span>
)} )}
</div> </div>
); );
} }
return ( return (
<div className="w-full h-full bg-muted/30 flex flex-col items-center justify-center text-muted-foreground rounded cursor-grab active:cursor-grabbing" {...baseDragProps}> <div
className="w-full h-full bg-muted/30 flex flex-col items-center justify-center text-muted-foreground rounded cursor-grab active:cursor-grabbing"
{...baseDragProps}
>
<Image className="h-6 w-6" /> <Image className="h-6 w-6" />
<span className="text-xs mt-1">Unknown</span> <span className="text-xs mt-1">Unknown</span>
</div> </div>
@ -135,6 +168,7 @@ export function MediaPanel() {
return ( return (
<> <>
{/* Hidden file input for uploading media */}
<input <input
ref={fileInputRef} ref={fileInputRef}
type="file" type="file"
@ -145,13 +179,14 @@ export function MediaPanel() {
/> />
<div <div
className={`h-full flex flex-col transition-colors relative ${isDragOver ? "bg-accent/30" : "" className={`h-full flex flex-col transition-colors relative ${isDragOver ? "bg-accent/30" : ""}`}
}`}
{...dragProps} {...dragProps}
> >
{/* Show overlay when dragging files over the panel */}
<DragOverlay isVisible={isDragOver} /> <DragOverlay isVisible={isDragOver} />
<div className="p-2 border-b"> <div className="p-2 border-b">
{/* Button to add/upload media */}
<Button <Button
variant="outline" variant="outline"
size="sm" size="sm"
@ -174,18 +209,22 @@ export function MediaPanel() {
</div> </div>
<div className="flex-1 overflow-y-auto p-2"> <div className="flex-1 overflow-y-auto p-2">
{/* Show message if no media, otherwise show media grid */}
{mediaItems.length === 0 ? ( {mediaItems.length === 0 ? (
<div className="flex flex-col items-center justify-center py-8 text-center h-full"> <div className="flex flex-col items-center justify-center py-8 text-center h-full">
<div className="w-16 h-16 rounded-full bg-muted/30 flex items-center justify-center mb-4"> <div className="w-16 h-16 rounded-full bg-muted/30 flex items-center justify-center mb-4">
<Image className="h-8 w-8 text-muted-foreground" /> <Image className="h-8 w-8 text-muted-foreground" />
</div> </div>
<p className="text-sm text-muted-foreground">No media in project</p> <p className="text-sm text-muted-foreground">
No media in project
</p>
<p className="text-xs text-muted-foreground/70 mt-1"> <p className="text-xs text-muted-foreground/70 mt-1">
Drag files here or use the button above Drag files here or use the button above
</p> </p>
</div> </div>
) : ( ) : (
<div className="grid grid-cols-2 gap-2"> <div className="grid grid-cols-2 gap-2">
{/* Render each media item as a draggable button */}
{mediaItems.map((item) => ( {mediaItems.map((item) => (
<div key={item.id} className="relative group"> <div key={item.id} className="relative group">
<Button <Button
@ -195,11 +234,10 @@ export function MediaPanel() {
<AspectRatio ratio={item.aspectRatio}> <AspectRatio ratio={item.aspectRatio}>
{renderPreview(item)} {renderPreview(item)}
</AspectRatio> </AspectRatio>
<span className="text-xs truncate px-1"> <span className="text-xs truncate px-1">{item.name}</span>
{item.name}
</span>
</Button> </Button>
{/* Show remove button on hover */}
<div className="absolute -top-2 -right-2 opacity-0 group-hover:opacity-100 transition-opacity"> <div className="absolute -top-2 -right-2 opacity-0 group-hover:opacity-100 transition-opacity">
<Button <Button
variant="destructive" variant="destructive"

File diff suppressed because it is too large Load Diff

View File

@ -15,6 +15,7 @@ export interface TimelineTrack {
name: string; name: string;
type: "video" | "audio" | "effects"; type: "video" | "audio" | "effects";
clips: TimelineClip[]; clips: TimelineClip[];
muted?: boolean;
} }
interface TimelineStore { interface TimelineStore {
@ -41,6 +42,7 @@ interface TimelineStore {
clipId: string, clipId: string,
startTime: number startTime: number
) => void; ) => void;
toggleTrackMute: (trackId: string) => void;
// Computed values // Computed values
getTotalDuration: () => number; getTotalDuration: () => number;
@ -55,6 +57,7 @@ export const useTimelineStore = create<TimelineStore>((set, get) => ({
name: `${type.charAt(0).toUpperCase() + type.slice(1)} Track`, name: `${type.charAt(0).toUpperCase() + type.slice(1)} Track`,
type, type,
clips: [], clips: [],
muted: false,
}; };
set((state) => ({ set((state) => ({
tracks: [...state.tracks, newTrack], tracks: [...state.tracks, newTrack],
@ -125,8 +128,6 @@ export const useTimelineStore = create<TimelineStore>((set, get) => ({
}); });
}, },
updateClipTrim: (trackId, clipId, trimStart, trimEnd) => { updateClipTrim: (trackId, clipId, trimStart, trimEnd) => {
set((state) => ({ set((state) => ({
tracks: state.tracks.map((track) => tracks: state.tracks.map((track) =>
@ -134,9 +135,7 @@ export const useTimelineStore = create<TimelineStore>((set, get) => ({
? { ? {
...track, ...track,
clips: track.clips.map((clip) => clips: track.clips.map((clip) =>
clip.id === clipId clip.id === clipId ? { ...clip, trimStart, trimEnd } : clip
? { ...clip, trimStart, trimEnd }
: clip
), ),
} }
: track : track
@ -151,9 +150,7 @@ export const useTimelineStore = create<TimelineStore>((set, get) => ({
? { ? {
...track, ...track,
clips: track.clips.map((clip) => clips: track.clips.map((clip) =>
clip.id === clipId clip.id === clipId ? { ...clip, startTime } : clip
? { ...clip, startTime }
: clip
), ),
} }
: track : track
@ -161,13 +158,22 @@ export const useTimelineStore = create<TimelineStore>((set, get) => ({
})); }));
}, },
toggleTrackMute: (trackId) => {
set((state) => ({
tracks: state.tracks.map((track) =>
track.id === trackId ? { ...track, muted: !track.muted } : track
),
}));
},
getTotalDuration: () => { getTotalDuration: () => {
const { tracks } = get(); const { tracks } = get();
if (tracks.length === 0) return 0; if (tracks.length === 0) return 0;
const trackEndTimes = tracks.map((track) => const trackEndTimes = tracks.map((track) =>
track.clips.reduce((maxEnd, clip) => { track.clips.reduce((maxEnd, clip) => {
const clipEnd = clip.startTime + clip.duration - clip.trimStart - clip.trimEnd; const clipEnd =
clip.startTime + clip.duration - clip.trimStart - clip.trimEnd;
return Math.max(maxEnd, clipEnd); return Math.max(maxEnd, clipEnd);
}, 0) }, 0)
); );