Skip to content

AI media harness for planning, generating, and editing locally

TypeScript

Separate audio and video drops, and mark lanes that refuse

Bitti5b0496c700ed
diff --git a/src/components/ProjectWorkspace.test.ts b/src/components/ProjectWorkspace.test.ts
index 89782e8..1cbf985 100644
--- a/src/components/ProjectWorkspace.test.ts
+++ b/src/components/ProjectWorkspace.test.ts
@@ -1,10 +1,10 @@
// @vitest-environment jsdom
-import { cleanup, fireEvent, render, screen, waitFor } from "@testing-library/react";
+import { cleanup, createEvent, fireEvent, render, screen, waitFor } from "@testing-library/react";
import { createElement } from "react";
import { afterEach, describe, expect, it, vi } from "vitest";
import completeFixture from "../../fixtures/project-v1-complete.json";
-import { compileMiniMaxH3Prompt, createProjectConfig, parseProjectConfig, STORY_TRACK_ID, type ProjectConfig } from "../lib/project";
+import { compileMiniMaxH3Prompt, createProjectConfig, parseProjectConfig, STORY_TRACK_ID, type ProjectAsset, type ProjectConfig } from "../lib/project";
import { formatDurationTimecode } from "./ProjectWorkspace";
import { ProjectWorkspace } from "./ProjectWorkspace";
import { GeneratorView } from "./workspace/GeneratorView";
@@ -95,15 +95,24 @@ describe("project workspace timecode", () => {
};
}
- /** A project carrying one importable clip, so the media panel has a card. */
+ /** A project carrying one importable clip of each kind, so the media panel
+ * has a card that belongs on a video track and one that belongs on audio. */
function projectWithMedia(): ProjectConfig {
const fresh = createProjectConfig({ name: "Ceramic lamp", prompt: "A quiet product film", aspectRatio: "16:9", resolution: "1080p", targetDurationSeconds: 30 });
return parseProjectConfig({ ...fresh, assets: [{
id: "asset-macro", kind: "video", name: "Macro footage", relativePath: "media/imported/macro.mp4",
mimeType: "video/mp4", durationMs: null, width: null, height: null, createdAt: fresh.createdAt,
+ }, {
+ id: "asset-room", kind: "audio", name: "Room tone", relativePath: "media/imported/room.wav",
+ mimeType: "audio/wav", durationMs: null, width: null, height: null, createdAt: fresh.createdAt,
}] });
}
+ /** The lane for the first track of this kind, and the card for that asset. */
+ const laneFor = (container: HTMLElement, config: ProjectConfig, kind: "video" | "audio") =>
+ container.querySelectorAll(".track-lane")[config.timeline.tracks.findIndex((track) => track.kind === kind)];
+ const cardFor = (asset: ProjectAsset) => screen.getByTitle(new RegExp(`^${asset.name} —`));
+
function mediaPanel(config: ProjectConfig, onChange = vi.fn()) {
const view = render(createElement(TimelineView, { config, folderPath: "C:\Ceramic Lamp", onChange, onOpenGenerator: () => undefined }));
fireEvent.click(screen.getByRole("button", { name: "Media" }));
@@ -149,6 +158,74 @@ describe("project workspace timecode", () => {
expect(parseProjectConfig(next)).toBeTruthy();
});
+ it("drops a sound onto an audio track, and refuses every video track", () => {
+ const config = projectWithMedia();
+ const sound = config.assets[1];
+ const { container, onChange } = mediaPanel(config);
+
+ const dataTransfer = fakeDataTransfer();
+ fireEvent.dragStart(cardFor(sound), { dataTransfer });
+ // Video and audio are separated in BOTH directions. The video test beside
+ // this one only proved the video half; a sound landing on a video track
+ // makes a clip with a picture track and no picture.
+ fireEvent.dragOver(laneFor(container, config, "video"), { dataTransfer });
+ fireEvent.drop(laneFor(container, config, "video"), { dataTransfer });
+ expect(onChange).not.toHaveBeenCalled();
+
+ fireEvent.drop(laneFor(container, config, "audio"), { dataTransfer });
+ const next = onChange.mock.calls[0][0] as ProjectConfig;
+ const audioTrack = next.timeline.tracks.find((track) => track.kind === "audio")!;
+ expect(audioTrack.clips.map((clip) => clip.assetId)).toEqual([sound.id]);
+ expect(next.timeline.tracks.filter((track) => track.kind === "video").every((track) => track.clips.length === 0)).toBe(true);
+ expect(parseProjectConfig(next)).toBeTruthy();
+ });
+
+ it("lands the dropped clip under the pointer and selects it", () => {
+ const config = projectWithMedia();
+ const video = config.assets[0];
+ const { container, onChange, rerender } = mediaPanel(config);
+ const lane = laneFor(container, config, "video") as HTMLElement;
+ // jsdom lays nothing out, so the lane has to be told how wide it is — the
+ // drop reads exactly this to turn a pointer position into a start time.
+ lane.getBoundingClientRect = () => ({ left: 100, width: 1000, top: 0, right: 1100, bottom: 72, height: 72, x: 100, y: 0, toJSON: () => ({}) });
+
+ const dataTransfer = fakeDataTransfer();
+ fireEvent.dragStart(cardFor(video), { dataTransfer });
+ // jsdom has no DragEvent, so fireEvent builds a plain Event and drops any
+ // pointer coordinates on the floor. The drop reads clientX, so put it back.
+ const drop = createEvent.drop(lane, { dataTransfer });
+ Object.defineProperty(drop, "clientX", { value: 350 });
+ fireEvent(lane, drop);
+
+ const next = onChange.mock.calls[0][0] as ProjectConfig;
+ const dropped = next.timeline.tracks.flatMap((track) => track.clips)[0];
+ // A quarter of the way along a 30-second canvas, not pinned to zero.
+ expect(dropped.startMs).toBe(Math.round(0.25 * 30_000));
+ expect(parseProjectConfig(next)).toBeTruthy();
+
+ // Dropping something and then having to find it again is a fair definition
+ // of a broken drop, so the new clip arrives selected.
+ rerender(createElement(TimelineView, { config: next, folderPath: "C:\Ceramic Lamp", onChange, onOpenGenerator: () => undefined }));
+ const clip = container.querySelector(".timeline-clip.selected");
+ expect(clip).not.toBeNull();
+ expect(clip!.textContent).toContain(video.name);
+ });
+
+ it("marks the lanes that cannot take what is being dragged", () => {
+ const config = projectWithMedia();
+ const { container } = mediaPanel(config);
+ const rejected = () => Array.from(container.querySelectorAll(".track-lane--reject"));
+
+ expect(rejected()).toHaveLength(0);
+ const dataTransfer = fakeDataTransfer();
+ fireEvent.dragStart(cardFor(config.assets[1]), { dataTransfer });
+ // Every video track, for the whole drag — not only the one under the
+ // pointer. A dark lane on its own says nothing the user can act on.
+ expect(rejected()).toHaveLength(config.timeline.tracks.filter((track) => track.kind === "video").length);
+ fireEvent.dragEnd(cardFor(config.assets[1]));
+ expect(rejected()).toHaveLength(0);
+ });
+
it("does not delete a selected clip from a locked track", () => {
const config = parseProjectConfig(completeFixture);
const firstTrackWithClip = config.timeline.tracks.find((track) => track.clips.length > 0)!;
diff --git a/src/components/workspace/TimelineView.tsx b/src/components/workspace/TimelineView.tsx
index b2082c7..2dcfae3 100644
--- a/src/components/workspace/TimelineView.tsx
+++ b/src/components/workspace/TimelineView.tsx
@@ -199,14 +199,22 @@ export function TimelineView({ config, folderPath, onChange, onOpenGenerator }:
const track = tracks.find((item) => item.id === trackId);
const asset = assetById(assetId);
if (!track || !acceptsAsset(track, asset) || !asset) return;
- // Clamp so a drop near the right edge still lands a whole clip on the ruler.
- const startMs = Math.round(Math.max(0, Math.min(duration - DROPPED_CLIP_MS, ratio * duration)));
+ const durationMs = asset.durationMs && asset.durationMs > 0 ? asset.durationMs : DROPPED_CLIP_MS;
+ /* Clamp so a drop near the right edge still lands a whole clip on the
+ ruler, against THIS clip's length rather than the 5s default — a long
+ import dropped at the end used to run off the end of the canvas.
+ Math.max last, because a clip longer than the whole canvas makes
+ `duration - durationMs` negative and 0 is the only honest start.
+ A non-finite ratio (a zero-width lane, a lane that has not been laid out
+ yet) would carry NaN into startMs, which zod then refuses to save. */
+ const position = Number.isFinite(ratio) ? ratio * duration : 0;
+ const startMs = Math.round(Math.max(0, Math.min(duration - durationMs, position)));
const clip: TimelineClip = {
id: `clip-${crypto.randomUUID()}`,
assetId: asset.id,
trackId: track.id,
startMs,
- durationMs: asset.durationMs && asset.durationMs > 0 ? asset.durationMs : DROPPED_CLIP_MS,
+ durationMs,
sourceStartMs: 0,
label: asset.name,
color: null,
@@ -392,6 +400,11 @@ export function TimelineView({ config, folderPath, onChange, onOpenGenerator }:
duration={duration}
selectedId={selectedId}
dropActive={dropTrackId === track.id}
+ /* A lane that stays dark is indistinguishable from a lane the
+ pointer simply is not over, so a sound held above a video track
+ looked like it had not been picked up rather than like it was
+ being refused. Marked for the whole drag, not just on hover. */
+ dropBlocked={draggedAsset !== undefined && !acceptsAsset(track, draggedAsset)}
onSelect={setSelectedId}
onToggle={toggleTrack}
onRename={renameTrack}
@@ -421,11 +434,12 @@ export function TimelineView({ config, folderPath, onChange, onOpenGenerator }:
);
}
-function TrackRow({ track, duration, selectedId, dropActive, onSelect, onToggle, onRename, onDragOverLane, onDragLeaveLane, onDropLane }: {
+function TrackRow({ track, duration, selectedId, dropActive, dropBlocked, onSelect, onToggle, onRename, onDragOverLane, onDragLeaveLane, onDropLane }: {
track: ProjectConfig["timeline"]["tracks"][number];
duration: number;
selectedId: string;
dropActive: boolean;
+ dropBlocked: boolean;
onSelect: (id: string) => void;
onToggle: (id: string, key: "muted" | "locked") => void;
onRename: (id: string, name: string) => void;
@@ -451,7 +465,8 @@ function TrackRow({ track, duration, selectedId, dropActive, onSelect, onToggle,
<button className={track.locked ? "active" : ""} onClick={() => onToggle(track.id, "locked")} aria-label={`${track.locked ? "Unlock" : "Lock"} ${track.name}`} title={`${track.locked ? "Unlock" : "Lock"} ${track.name}`}>{track.locked ? <Lock size={16} /> : <LockOpen size={16} />}</button>
</div>
<div
- className={`track-lane ${track.muted ? "muted" : ""} ${dropActive ? "track-lane--drop" : ""}`}
+ className={`track-lane ${track.muted ? "muted" : ""} ${dropActive ? "track-lane--drop" : ""} ${dropBlocked ? "track-lane--reject" : ""}`}
+ title={dropBlocked ? (track.locked ? `${track.name} is locked` : `${track.name} only takes ${track.kind === "audio" ? "sound" : "video and pictures"}`) : undefined}
onDragOver={onDragOverLane}
onDragLeave={onDragLeaveLane}
onDrop={onDropLane}
diff --git a/src/styles/timeline.css b/src/styles/timeline.css
index 9e10ef8..a648403 100644
--- a/src/styles/timeline.css
+++ b/src/styles/timeline.css
@@ -1030,6 +1030,20 @@
box-shadow: inset 0 0 0 1px var(--accent-bright);
}
+/* And the other half of the answer: a lane that cannot take what is being
+ dragged is marked for the whole drag, so "no" is visible before the pointer
+ ever reaches it. A dark lane alone was indistinguishable from a lane the
+ pointer merely was not over. */
+.track-lane--reject {
+ cursor: not-allowed;
+ background-image: repeating-linear-gradient(
+ 135deg,
+ rgba(255, 255, 255, 0.05) 0 6px,
+ transparent 6px 12px
+ );
+ opacity: 0.55;
+}
+
/* --- Clips ----------------------------------------------------------------- */
.timeline-clip {