Skip to content

Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.

fix(edit-clip): seek the preview to the playhead once metadata loads

EtienneLescota997ce76f06e
diff --git a/src/components/ai-edition/EditClipModal.test.tsx b/src/components/ai-edition/EditClipModal.test.tsx
index b59463c52..9d5c5ad07 100644
--- a/src/components/ai-edition/EditClipModal.test.tsx
+++ b/src/components/ai-edition/EditClipModal.test.tsx
@@ -239,6 +239,10 @@ describe("EditClipModal crop from the keyboard", () => {
describe("EditClipModal playhead for framing the crop", () => {
// jsdom has no media pipeline: stand in a clock the modal can seek.
let videoTime = 0;
+ let readyState = 1;
+ afterEach(() => {
+ readyState = 1;
+ });
beforeAll(() => {
Object.defineProperty(HTMLMediaElement.prototype, "currentTime", {
configurable: true,
@@ -249,7 +253,7 @@ describe("EditClipModal playhead for framing the crop", () => {
});
Object.defineProperty(HTMLMediaElement.prototype, "readyState", {
configurable: true,
- get: () => 1,
+ get: () => readyState,
});
HTMLMediaElement.prototype.pause = vi.fn();
});
@@ -326,6 +330,22 @@ describe("EditClipModal playhead for framing the crop", () => {
expect(videoTime).toBe(60);
});
+ it("keeps a scrub made before the video's metadata loads", () => {
+ readyState = 0;
+ videoTime = 0;
+ renderWithVideo();
+ drag(screen.getByTestId("edit-clip-trim-track"), 500, 500);
+ expect(videoTime).toBe(0);
+
+ readyState = 1;
+ const video = document.querySelector("video");
+ if (!video) throw new Error("no preview video rendered");
+ act(() => {
+ video.dispatchEvent(new Event("loadedmetadata"));
+ });
+ expect(videoTime).toBe(50);
+ });
+
it("is not an edit: Apply stays off and the backdrop still closes", () => {
const { onClose } = renderWithVideo();
drag(screen.getByTestId("edit-clip-trim-track"), 500, 800);
diff --git a/src/components/ai-edition/Modals.tsx b/src/components/ai-edition/Modals.tsx
index a765b1432..f2b7bb7a8 100644
--- a/src/components/ai-edition/Modals.tsx
+++ b/src/components/ai-edition/Modals.tsx
@@ -713,12 +713,17 @@ export function EditClipModal({
setCropTouched(false);
}, [open, clip]);
- // The picture follows the playhead.
+ // The picture follows the playhead: every seek of the preview is this one, including the
+ // first, and a scrub before the metadata lands is not lost.
useEffect(() => {
const v = cropVideoRef.current;
- // Before metadata, the crop preview effect below does the first seek.
- if (!open || !v || v.readyState < 1) return;
- v.currentTime = playheadSec;
+ if (!open || !v) return;
+ const seek = () => {
+ v.currentTime = playheadSec;
+ };
+ if (v.readyState >= 1) seek();
+ else v.addEventListener("loadedmetadata", seek, { once: true });
+ return () => v.removeEventListener("loadedmetadata", seek);
}, [open, playheadSec]);
// Re-detect the active ratio preset whenever the stored region or the
@@ -734,9 +739,8 @@ export function EditClipModal({
}, [open, clip, videoAspectRatio, cropTouched]);
// Crop preview: a paused still frame is enough to judge a crop (mirrors
- // the standalone CropModal this replaced) — seek once per open to the
- // clip's original in-point, where the playhead starts; the playhead effect
- // above takes over from there.
+ // the standalone CropModal this replaced). The playhead effect above seeks
+ // it; this one reads the frame's aspect ratio.
useEffect(() => {
if (!open || !clip) return;
// A clip switch must not leave the previous clip's dimensions live: an
@@ -748,16 +752,15 @@ export function EditClipModal({
setVideoAspectRatio(16 / 9);
const v = cropVideoRef.current;
if (!v) return;
- const seek = () => {
+ const onMetadata = () => {
v.pause();
- if (Number.isFinite(clip.sourceStartSec)) v.currentTime = clip.sourceStartSec;
if (v.videoWidth > 0 && v.videoHeight > 0) {
setVideoAspectRatio(v.videoWidth / v.videoHeight);
}
};
- if (v.readyState >= 1) seek();
- else v.addEventListener("loadedmetadata", seek, { once: true });
- return () => v.removeEventListener("loadedmetadata", seek);
+ if (v.readyState >= 1) onMetadata();
+ else v.addEventListener("loadedmetadata", onMetadata, { once: true });
+ return () => v.removeEventListener("loadedmetadata", onMetadata);
}, [open, clip]);
if (!clip) return null;