feat:enabled-redo-functionality

This commit is contained in:
aashishparuvada
2025-06-24 09:31:53 +05:30
parent 9570847edd
commit 073f0d89bd
2 changed files with 37 additions and 5 deletions

View File

@ -40,7 +40,7 @@ export function Timeline() {
// Timeline shows all tracks (video, audio, effects) and their clips. // Timeline shows all tracks (video, audio, effects) and their clips.
// You can drag media here to add it to your project. // You can drag media here to add it to your project.
// Clips can be trimmed, deleted, and moved. // Clips can be trimmed, deleted, and moved.
const { tracks, addTrack, addClipToTrack, removeTrack, toggleTrackMute, removeClipFromTrack, moveClipToTrack, getTotalDuration, selectedClips, selectClip, deselectClip, clearSelectedClips, setSelectedClips, updateClipTrim, undo } = const { tracks, addTrack, addClipToTrack, removeTrack, toggleTrackMute, removeClipFromTrack, moveClipToTrack, getTotalDuration, selectedClips, selectClip, deselectClip, clearSelectedClips, setSelectedClips, updateClipTrim, undo, redo } =
useTimelineStore(); useTimelineStore();
const { mediaItems, addMediaItem } = useMediaStore(); const { mediaItems, addMediaItem } = useMediaStore();
const { currentTime, duration, seek, setDuration, isPlaying, play, pause, toggle, setSpeed, speed } = usePlaybackStore(); const { currentTime, duration, seek, setDuration, isPlaying, play, pause, toggle, setSpeed, speed } = usePlaybackStore();
@ -110,6 +110,21 @@ export function Timeline() {
return () => window.removeEventListener("keydown", handleKeyDown); return () => window.removeEventListener("keydown", handleKeyDown);
}, [undo]); }, [undo]);
// Keyboard event for redo (Cmd+Shift+Z or Cmd+Y)
useEffect(() => {
const handleKeyDown = (e: KeyboardEvent) => {
if ((e.metaKey || e.ctrlKey) && e.key === "z" && e.shiftKey) {
e.preventDefault();
redo();
} else if ((e.metaKey || e.ctrlKey) && e.key === "y") {
e.preventDefault();
redo();
}
};
window.addEventListener("keydown", handleKeyDown);
return () => window.removeEventListener("keydown", handleKeyDown);
}, [redo]);
// Mouse down on timeline background to start marquee // Mouse down on timeline background to start marquee
const handleTimelineMouseDown = (e: React.MouseEvent) => { const handleTimelineMouseDown = (e: React.MouseEvent) => {
if (e.target === e.currentTarget && e.button === 0) { if (e.target === e.currentTarget && e.button === 0) {

View File

@ -21,6 +21,7 @@ export interface TimelineTrack {
interface TimelineStore { interface TimelineStore {
tracks: TimelineTrack[]; tracks: TimelineTrack[];
history: TimelineTrack[][]; history: TimelineTrack[][];
redoStack: TimelineTrack[][];
// Multi-selection // Multi-selection
selectedClips: { trackId: string; clipId: string }[]; selectedClips: { trackId: string; clipId: string }[];
@ -57,25 +58,34 @@ interface TimelineStore {
// New actions // New actions
undo: () => void; undo: () => void;
redo: () => void;
pushHistory: () => void; pushHistory: () => void;
} }
export const useTimelineStore = create<TimelineStore>((set, get) => ({ export const useTimelineStore = create<TimelineStore>((set, get) => ({
tracks: [], tracks: [],
history: [], history: [],
redoStack: [],
selectedClips: [], selectedClips: [],
pushHistory: () => { pushHistory: () => {
const { tracks, history } = get(); const { tracks, history, redoStack } = get();
// Deep copy tracks // Deep copy tracks
set({ history: [...history, JSON.parse(JSON.stringify(tracks))] }); set({
history: [...history, JSON.parse(JSON.stringify(tracks))],
redoStack: [] // Clear redo stack when new action is performed
});
}, },
undo: () => { undo: () => {
const { history } = get(); const { history, redoStack, tracks } = get();
if (history.length === 0) return; if (history.length === 0) return;
const prev = history[history.length - 1]; const prev = history[history.length - 1];
set({ tracks: prev, history: history.slice(0, -1) }); set({
tracks: prev,
history: history.slice(0, -1),
redoStack: [...redoStack, JSON.parse(JSON.stringify(tracks))] // Add current state to redo stack
});
}, },
selectClip: (trackId, clipId, multi = false) => { selectClip: (trackId, clipId, multi = false) => {
@ -244,4 +254,11 @@ export const useTimelineStore = create<TimelineStore>((set, get) => ({
return Math.max(...trackEndTimes, 0); return Math.max(...trackEndTimes, 0);
}, },
redo: () => {
const { redoStack } = get();
if (redoStack.length === 0) return;
const next = redoStack[redoStack.length - 1];
set({ tracks: next, redoStack: redoStack.slice(0, -1) });
},
})); }));