Skip to content

Create professional demos and 3D mockups in seconds, directly in your browser

feat: add zoom fragment handling and clip resequencing in editor

CristianOlivera16f6bc64f41af
diff --git a/app/[locale]/(editor)/editor/page.tsx b/app/[locale]/(editor)/editor/page.tsx
index f67cb04..305ed1a 100644
--- a/app/[locale]/(editor)/editor/page.tsx
+++ b/app/[locale]/(editor)/editor/page.tsx
@@ -17,7 +17,7 @@ import { useVideoThumbnails, type VideoThumbnail } from "@/hooks/useVideoThumbna
import { useUndoRedo } from "@/hooks/useUndoRedo";
import { clearAllThumbnailCache } from "@/lib/thumbnail-cache";
import { addVideoToLibrary, addVideoToLibraryWithMetadata, getLibraryVideoCount, getLibraryVideo, findExistingVideo } from "@/lib/videos-library";
-import { calculateTotalDuration, clampClipToRealDuration, findNextClipPosition, getClipAtTime, probeMediaDuration, splitClipAtTime, type VideoTrackClip } from "@/types/video-track.types";
+import { calculateTotalDuration, clampClipToRealDuration, findNextClipPosition, getClipAtTime, probeMediaDuration, resequenceClips, splitClipAtTime, type VideoTrackClip } from "@/types/video-track.types";
import type { ExportQuality, BackgroundTab, VideoCanvasHandle, BackgroundColorConfig, AspectRatio, CropArea } from "@/types";
import type { TrimRange } from "@/types/timeline.types";
import type { MockupConfig, MenuPage } from "@/types/mockup.types";
@@ -1352,6 +1352,27 @@ export default function Editor() {
return videoUrl;
}, [activeClipAtPlayhead, videoUrl, videoUrlsMap, videoClips, currentTime]);
+ // URL of the clip that contains the selected zoom fragment's start time.
+ // Used by ZoomFragmentEditor so its preview shows the correct clip, not
+ // just the one at the playhead.
+ const zoomFragmentClipUrl = useMemo(() => {
+ if (!selectedZoomFragment) return videoUrl;
+ const clip = getClipAtTime(videoClips, selectedZoomFragment.startTime);
+ return clip ? (videoUrlsMap.get(clip.libraryVideoId) ?? videoUrl) : videoUrl;
+ }, [selectedZoomFragment, videoClips, videoUrlsMap, videoUrl]);
+
+ // Dimensions of the clip that contains the selected zoom fragment, so the
+ // preview aspect ratio matches the fragment's source video (not the
+ // playhead clip which may differ in multi-clip projects).
+ const zoomFragmentDimensions = useMemo(() => {
+ if (!selectedZoomFragment) return videoDimensions;
+ const clip = getClipAtTime(videoClips, selectedZoomFragment.startTime);
+ if (clip && clip.width && clip.height) {
+ return { width: clip.width, height: clip.height };
+ }
+ return videoDimensions;
+ }, [selectedZoomFragment, videoClips, videoDimensions]);
+
const handleUpdateVideoClip = useCallback((clipId: string, updates: Partial<VideoTrackClip>) => {
setVideoClips(prev => {
const newClips = prev.map(clip =>
@@ -1367,8 +1388,10 @@ export default function Editor() {
}, []);
const handleDeleteVideoClip = useCallback((clipId: string) => {
+ const deletedClip = videoClipsRef.current.find(c => c.id === clipId);
setVideoClips(prev => {
- const newClips = prev.filter(clip => clip.id !== clipId);
+ // Re-sequence remaining clips to be contiguous (no gaps after deletion).
+ const { clips: newClips } = resequenceClips(prev.filter(clip => clip.id !== clipId));
if (newClips.length > 0) {
const newDuration = calculateTotalDuration(newClips);
setVideoDuration(newDuration);
@@ -1402,10 +1425,33 @@ export default function Editor() {
}
return newClips;
});
+
+ // Remap zoom fragments: remove those within the deleted clip's range,
+ // shift those after it by the deleted clip's duration.
+ if (deletedClip) {
+ const deletedStart = deletedClip.startTime;
+ const deletedDuration = deletedClip.trimEnd - deletedClip.trimStart;
+ const deletedEnd = deletedStart + deletedDuration;
+ setZoomFragments(prevFrags =>
+ prevFrags
+ .filter(f => !(f.startTime >= deletedStart && f.endTime <= deletedEnd))
+ .map(f => {
+ if (f.startTime >= deletedEnd) {
+ return {
+ ...f,
+ startTime: f.startTime - deletedDuration,
+ endTime: f.endTime - deletedDuration,
+ };
+ }
+ return f;
+ })
+ );
+ }
+
if (selectedVideoClipId === clipId) {
setSelectedVideoClipId(null);
}
- }, [selectedVideoClipId]);
+ }, [selectedVideoClipId, setZoomFragments]);
const handleSplitVideoClip = useCallback(() => {
const clips = videoClipsRef.current;
@@ -1440,7 +1486,8 @@ export default function Editor() {
// Handler to remove video from track when deleted from library (cascade delete)
const handleDeleteVideoFromLibrary = useCallback((libraryVideoId: string) => {
setVideoClips(prev => {
- const newClips = prev.filter(clip => clip.libraryVideoId !== libraryVideoId);
+ const filtered = prev.filter(clip => clip.libraryVideoId !== libraryVideoId);
+ const newClips = filtered.length > 0 ? resequenceClips(filtered).clips : filtered;
if (newClips.length > 0) {
const newDuration = calculateTotalDuration(newClips);
setVideoDuration(newDuration);
@@ -2775,11 +2822,11 @@ export default function Editor() {
onAddZoomMovement={handleAddZoomMovement}
onDeleteZoomMovement={handleDeleteZoomMovement}
onUpdateZoomMovementPoint={(id, x, y) => handleUpdateZoomMovement(id, { focusX: x, focusY: y })}
- videoUrl={videoUrl}
+ videoUrl={zoomFragmentClipUrl}
videoThumbnail={zoomFragmentThumbnail}
currentTime={currentTime}
getThumbnailForTime={getThumbnailForTime}
- videoDimensions={videoDimensions}
+ videoDimensions={zoomFragmentDimensions}
mockupId={mockupId}
mockupConfig={mockupConfig}
onMockupChange={handleMockupChange}
@@ -3090,11 +3137,11 @@ export default function Editor() {
onAddZoomFragment={() => handleAddZoomFragment(currentTime)}
onUpdateZoomFragment={handleUpdateZoomFragment}
onDeleteZoomFragment={handleDeleteZoomFragment}
- videoUrl={videoUrl}
+ videoUrl={zoomFragmentClipUrl}
videoThumbnail={zoomFragmentThumbnail}
currentTime={currentTime}
getThumbnailForTime={getThumbnailForTime}
- videoDimensions={videoDimensions}
+ videoDimensions={zoomFragmentDimensions}
mockupId={mockupId}
mockupConfig={mockupConfig}
onMockupChange={handleMockupChange}
@@ -3138,7 +3185,7 @@ export default function Editor() {
<VideoCropperModal
isOpen={isCropperOpen}
onClose={handleCloseCropper}
- videoUrl={videoUrl}
+ videoUrl={activeClipUrl}
onCropApply={handleCropApply}
initialCrop={cropArea}
/>
diff --git a/app/components/ui/editor/ZoomFragmentEditor.tsx b/app/components/ui/editor/ZoomFragmentEditor.tsx
index bf31052..a936c38 100644
--- a/app/components/ui/editor/ZoomFragmentEditor.tsx
+++ b/app/components/ui/editor/ZoomFragmentEditor.tsx
@@ -44,9 +44,12 @@ export function ZoomFragmentEditor({
const dynamicThumbnail = useMemo(() => {
if (!getThumbnailForTime) return videoThumbnail || null;
- const thumb = getThumbnailForTime(currentTime);
+ // Use the fragment's start time (not the global playhead) so the preview
+ // always shows the frame this zoom fragment applies to, even when the
+ // playhead is in a different clip.
+ const thumb = getThumbnailForTime(fragment.startTime);
return thumb?.dataUrl || videoThumbnail || null;
- }, [getThumbnailForTime, currentTime, videoThumbnail]);
+ }, [getThumbnailForTime, fragment.startTime, videoThumbnail]);
const movementEnabled = fragment.movementEnabled ?? false;
const sortedMovements = useMemo(
diff --git a/types/video-track.types.ts b/types/video-track.types.ts
index 66a3d62..afe82fb 100644
--- a/types/video-track.types.ts
+++ b/types/video-track.types.ts
@@ -89,4 +89,22 @@ export function clampClipToRealDuration(clip: VideoTrackClip, realDuration: numb
return { ...clip, duration: Math.min(clip.duration, clamped), trimEnd: clamped };
}
return clip;
-}
\ No newline at end of file
+}
+
+export function resequenceClips(clips: VideoTrackClip[]): {
+ clips: VideoTrackClip[];
+ offsetMap: Map<string, number>;
+} {
+ const sorted = [...clips].sort((a, b) => a.startTime - b.startTime);
+ const offsetMap = new Map<string, number>();
+ let cursor = 0;
+ const resequenced = sorted.map(clip => {
+ const duration = clip.trimEnd - clip.trimStart;
+ const offset = cursor - clip.startTime;
+ if (offset !== 0) offsetMap.set(clip.id, offset);
+ const newClip = { ...clip, startTime: cursor };
+ cursor += duration;
+ return newClip;
+ });
+ return { clips: resequenced, offsetMap };
+}