diff --git a/src/components/video-editor/timeline/core/spans.test.ts b/src/components/video-editor/timeline/core/spans.test.ts index 9bc0ebf0..29b02545 100644 --- a/src/components/video-editor/timeline/core/spans.test.ts +++ b/src/components/video-editor/timeline/core/spans.test.ts @@ -20,7 +20,7 @@ describe("timeline core/spans", () => { it("clamps start when requested end exceeds total", () => { expect( normalizeRegionSpan({ startMs: 980, endMs: 1200, totalMs: 1000, minDurationMs: 100 }), - ).toEqual({ start: 900, end: 1080 }); + ).toEqual({ start: 900, end: 1000 }); }); it("keeps already valid spans unchanged", () => { @@ -28,4 +28,10 @@ describe("timeline core/spans", () => { normalizeRegionSpan({ startMs: 100, endMs: 300, totalMs: 1000, minDurationMs: 50 }), ).toEqual({ start: 100, end: 300 }); }); + + it("never exceeds total when min duration is larger than total", () => { + expect( + normalizeRegionSpan({ startMs: 100, endMs: 300, totalMs: 80, minDurationMs: 100 }), + ).toEqual({ start: 0, end: 80 }); + }); }); diff --git a/src/components/video-editor/timeline/core/spans.ts b/src/components/video-editor/timeline/core/spans.ts index 8f49973e..6f8ac203 100644 --- a/src/components/video-editor/timeline/core/spans.ts +++ b/src/components/video-editor/timeline/core/spans.ts @@ -11,11 +11,14 @@ export function normalizeRegionSpan(params: { minDurationMs: number; }) { const { startMs, endMs, totalMs, minDurationMs } = params; - const clampedStart = Math.max(0, Math.min(startMs, totalMs)); - const minEnd = clampedStart + minDurationMs; - const clampedEnd = Math.min(totalMs, Math.max(minEnd, endMs)); - const normalizedStart = Math.max(0, Math.min(clampedStart, totalMs - minDurationMs)); - const normalizedEnd = Math.max(minEnd, Math.min(clampedEnd, totalMs)); + const safeTotalMs = Math.max(0, totalMs); + const safeMinDurationMs = Math.max(0, Math.min(minDurationMs, safeTotalMs)); + const clampedStart = Math.max(0, Math.min(startMs, safeTotalMs)); + const normalizedStart = Math.max(0, Math.min(clampedStart, safeTotalMs - safeMinDurationMs)); + const normalizedEnd = Math.min( + safeTotalMs, + Math.max(endMs, normalizedStart + safeMinDurationMs), + ); return { start: normalizedStart, end: normalizedEnd }; }