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(timeline): create pills at a readable size, and paste what was copied

Etienne Lescot069b6c927a3c
diff --git a/src/components/ai-edition/NewEditorShell.tsx b/src/components/ai-edition/NewEditorShell.tsx
index 356e250d7..da30d4661 100644
--- a/src/components/ai-edition/NewEditorShell.tsx
+++ b/src/components/ai-edition/NewEditorShell.tsx
@@ -707,61 +707,80 @@ export function NewEditorShell() {
const doc = useProjectStore.getState().document;
if (!doc) return;
const { pasteClipboard } = await import("@/lib/ai-edition/store/regionClipboard");
- const clip = pasteClipboard();
- if (!clip) return;
+ const snapshot = pasteClipboard();
+ if (!snapshot) return;
+ const { anchorRegionsWithDerivedMs } = await import("@/lib/ai-edition/timeline/timelineMap");
+ const { createId } = await import("@/lib/ai-edition/document/ids");
+
+ // Land it at the playhead, keeping the copied length.
const timeMs = Math.round(useProjectStore.getState().currentTimeSec * 1000);
- const region = { ...clip.region, id: crypto.randomUUID() };
- if (clip.kind === "zoom") {
- const zoom = region as unknown as (typeof doc.zoomRanges)[number];
- const duration = zoom.endMs - zoom.startMs;
- zoom.startMs = timeMs;
- zoom.endMs = timeMs + duration;
- await saveDocument({ ...doc, zoomRanges: [...doc.zoomRanges, zoom] });
- } else if (clip.kind === "annotation") {
- const annotation = region as unknown as (typeof doc.annotations)[number];
- const duration = annotation.endMs - annotation.startMs;
- annotation.startMs = timeMs;
- annotation.endMs = timeMs + duration;
- await saveDocument({ ...doc, annotations: [...doc.annotations, annotation] });
- } else if (clip.kind === "speed") {
- const speedRegions =
- (doc.legacyEditor as { speedRegions?: Array<Record<string, unknown>> })?.speedRegions ?? [];
- const speed = region as unknown as (typeof speedRegions)[number];
- const duration = Number(speed.endMs) - Number(speed.startMs);
- speed.startMs = timeMs;
- speed.endMs = timeMs + duration;
+ const src = snapshot.region as { startMs: number; endMs: number };
+ const prefix = snapshot.kind === "annotation" ? "ann" : snapshot.kind;
+ const pasted = {
+ ...snapshot.region,
+ id: createId(prefix),
+ startMs: timeMs,
+ endMs: timeMs + (Number(src.endMs) - Number(src.startMs)),
+ };
+ // Anchor to the clip(s) it covers, exactly like every add* does. Pasting
+ // used to store a bare startMs/endMs, so the region survived until the
+ // first clip reorder or trim and then drifted off its content — see
+ // technical-documentation/architecture/timeline-model.md.
+ const anchored = anchorRegionsWithDerivedMs(
+ [pasted as unknown as { id: string; startMs: number; endMs: number }],
+ doc.timeline.clips,
+ () => createId(prefix),
+ );
+
+ if (snapshot.kind === "zoom") {
+ await saveDocument({
+ ...doc,
+ zoomRanges: [...doc.zoomRanges, ...anchored] as typeof doc.zoomRanges,
+ });
+ } else if (snapshot.kind === "annotation") {
await saveDocument({
...doc,
- legacyEditor: {
- ...doc.legacyEditor,
- speedRegions: [...speedRegions, speed],
- },
+ annotations: [...doc.annotations, ...anchored] as typeof doc.annotations,
+ });
+ } else {
+ // speed and cameraFullscreen are both plain spans on legacyEditor.
+ const key = snapshot.kind === "speed" ? "speedRegions" : "cameraFullscreenRegions";
+ const legacy = (doc.legacyEditor as Record<string, unknown>) ?? {};
+ const prev = (legacy[key] as unknown[]) ?? [];
+ await saveDocument({
+ ...doc,
+ legacyEditor: { ...legacy, [key]: [...prev, ...anchored] },
});
}
toast.success("Region pasted");
}, [saveDocument]);
+ // Copy the SELECTED pill. Reads the same arrays the lanes render, so what gets
+ // copied is what the user is looking at — the old version dug into the raw
+ // document with a ternary chain that mapped a trim to "zoom" and sent
+ // cameraFullscreen down the speed branch, where neither could ever be found.
+ //
+ // Trims stay out: they are stored in SOURCE time against a clip anchor, so
+ // pasting one at the playhead is a ventilation problem, not a copy. Cut
+ // already excludes them for the same reason.
const handleCopyRegion = useCallback(async () => {
- const doc = useProjectStore.getState().document;
- if (!doc || !tl.selection) return;
+ const sel = tl.selection;
+ if (!sel || sel.kind === "trim") return;
+ const source =
+ sel.kind === "zoom"
+ ? tl.zoomRegions
+ : sel.kind === "annotation"
+ ? tl.annotationRegions
+ : sel.kind === "speed"
+ ? tl.speedRegions
+ : tl.cameraFullscreenRegions;
+ const region = (source as Array<{ id: string }>).find((r) => r.id === sel.id);
+ if (!region) return;
const { copyRegion } = await import("@/lib/ai-edition/store/regionClipboard");
- const region =
- tl.selection.kind === "zoom"
- ? doc.zoomRanges.find((z) => z.id === tl.selection?.id)
- : tl.selection.kind === "annotation"
- ? (doc.annotations as unknown[]).find(
- (a) => (a as { id: string }).id === tl.selection?.id,
- )
- : ((doc.legacyEditor as { speedRegions?: unknown[] } | null)?.speedRegions ?? []).find(
- (s) => (s as { id: string }).id === tl.selection?.id,
- );
- if (region) {
- copyRegion({
- kind: tl.selection.kind === "trim" ? "zoom" : tl.selection.kind,
- region: region as Record<string, unknown>,
- });
- toast.success("Region copied");
- }
+ copyRegion({ kind: sel.kind, region: region as unknown as Record<string, unknown> });
+ // One clipboard wins at a time: a copied pill retires the copied clip.
+ setCopiedClipId(null);
+ toast.success("Region copied");
}, [tl]);
useEffect(() => {
@@ -836,14 +855,21 @@ export function NewEditorShell() {
// of hardcoded keys, so rebinding in the shortcuts dialog actually
// changes runtime behavior.
if (matchesShortcut(e, shortcuts.copySelected, isMac)) {
- if (tl.clipSelection) {
+ // A pill and a clip can no longer both be selected (see selectRegion /
+ // selectClip), so this reads the one the user actually picked instead
+ // of preferring clips whatever was clicked last.
+ if (tl.selection) {
e.preventDefault();
- setCopiedClipId(tl.clipSelection);
+ void handleCopyRegion();
return;
}
- if (tl.selection) {
+ if (tl.clipSelection) {
e.preventDefault();
- void handleCopyRegion();
+ setCopiedClipId(tl.clipSelection);
+ // A copied clip retires the copied pill — see clearRegionClipboard.
+ void import("@/lib/ai-edition/store/regionClipboard").then((m) =>
+ m.clearRegionClipboard(),
+ );
return;
}
}
@@ -858,12 +884,13 @@ export function NewEditorShell() {
}
if (matchesShortcut(e, shortcuts.paste, isMac)) {
e.preventDefault();
- // A selected/copied clip takes priority — pasting with a clip in
- // hand is unambiguously "duplicate this clip", even if a region
- // was copied earlier in the session.
- const clipToDuplicate = copiedClipId ?? tl.clipSelection;
- if (clipToDuplicate) {
- void tl.duplicateClip(clipToDuplicate);
+ // Paste what was COPIED. It used to fall back to `tl.clipSelection`,
+ // so a clip merely being selected hijacked the paste — and since
+ // `copiedClipId` was never cleared, one Ctrl+C on a clip turned every
+ // later Ctrl+V into a clip duplication for the rest of the session,
+ // whatever the user copied afterwards.
+ if (copiedClipId) {
+ void tl.duplicateClip(copiedClipId);
return;
}
void pasteRegion();
diff --git a/src/components/ai-edition/v4/V4Timeline.geometry.test.tsx b/src/components/ai-edition/v4/V4Timeline.geometry.test.tsx
index 5497244bc..fa07316d2 100644
--- a/src/components/ai-edition/v4/V4Timeline.geometry.test.tsx
+++ b/src/components/ai-edition/v4/V4Timeline.geometry.test.tsx
@@ -84,6 +84,9 @@ function renderTimeline(
updateAnnotationSpan: vi.fn(async () => {
/* the drag only awaits it */
}),
+ addZoom: vi.fn(async () => {
+ /* the toolbar only awaits it */
+ }),
};
render(
<V4Timeline
@@ -181,6 +184,38 @@ describe("V4Timeline lane pills", () => {
});
});
+describe("V4Timeline create-from-toolbar", () => {
+ // The button asks for a DURATION worth a fixed number of pixels at the current
+ // zoom, so the pill you get is always the same size on screen — which is what
+ // the flat 2 s could not do: on this 30-minute fixture zoomed out it is one
+ // pixel. (It used to look fine only because the removed 1.5% minimum width
+ // inflated it in the rendering.)
+ const durationOf = (tl: { addZoom: ReturnType<typeof vi.fn> }) =>
+ tl.addZoom.mock.calls.at(-1)?.[0] as number;
+
+ it("scales the new region's duration with the zoom", () => {
+ const { tl } = renderTimeline();
+ fireEvent.click(screen.getByTitle("buttons.addZoom"));
+ // 900px viewport / 1800 s = 0.5 px per second, so 40px of pill is 80 s.
+ expect(durationOf(tl)).toBeCloseTo(80, 3);
+
+ // Zoomed to the 50x ceiling the same 40px is worth 1.6 s: same pill on
+ // screen, a region 50x shorter.
+ zoomIn(40);
+ fireEvent.click(screen.getByTitle("buttons.addZoom"));
+ expect(durationOf(tl)).toBeCloseTo(1.6, 3);
+ });
+
+ it("never asks for a slice too short to be worth creating", () => {
+ // Past ~30x on a short timeline the pixels are worth hundredths of a
+ // second; the region would be born unusable, so the duration floors.
+ const { tl } = renderTimeline([clip(0, 3)]);
+ zoomIn(40);
+ fireEvent.click(screen.getByTitle("buttons.addZoom"));
+ expect(durationOf(tl)).toBeCloseTo(0.25, 3);
+ });
+});
+
describe("V4Timeline clip row", () => {
// Three clips = two junctions. As a flex row with `gap: 6px`, each junction
// added 6px while every clip shrank proportionally to pay for it, so a clip's
diff --git a/src/components/ai-edition/v4/V4Timeline.tsx b/src/components/ai-edition/v4/V4Timeline.tsx
index 195028588..ee15d7df2 100644
--- a/src/components/ai-edition/v4/V4Timeline.tsx
+++ b/src/components/ai-edition/v4/V4Timeline.tsx
@@ -125,6 +125,25 @@ const PILL_HANDLE_OUT_PX = PILL_HANDLE_PX + PILL_MOVE_GAP_PX;
const PILL_CONTENT_MIN_PX = 34;
/** Edge-snap radius while dragging a pill, in screen px. */
const PILL_SNAP_PX = 8;
+/**
+ * Width a pill created from the toolbar aims for on screen. Its DURATION is
+ * whatever that width is worth at the current zoom: dezoomed you get a long
+ * region, zoomed in a short one, and either way a pill you can see, read and
+ * grab the moment it appears.
+ *
+ * A fixed 2 s did the opposite — on a 65-minute timeline zoomed out it is half a
+ * pixel. It only ever looked usable because the old 1.5%-of-the-timeline minimum
+ * width inflated it in the RENDERING, which is the lie this branch removed. Same
+ * intent, honest implementation: the width now comes from the duration actually
+ * stored, so what you see is what the effect covers.
+ *
+ * Sized to clear PILL_CONTENT_MIN_PX, so a new pill shows its icon and label
+ * immediately with room left to grab either handle.
+ */
+const PILL_CREATE_PX = 40;
+/** Floor on that duration. Only bites past ~30x zoom, where 40px is worth a few
+ * hundredths of a second and the region would be born unusable. */
+const PILL_CREATE_MIN_SEC = 0.25;
/** Visual separation between two clip cards. Taken off each clip's own width
* (see .tlClip) rather than inserted between them, so it cannot displace the
* clips that follow — which is what a flex `gap` did, once per junction. */
@@ -451,6 +470,13 @@ export function V4Timeline({
// `total`, which is a duration in disguise and so scales with the recording.
const navSpan = Math.max(0.02, nav.end - nav.start);
const pxPerSec = viewportWidthPx / navSpan / total;
+ // Duration handed to the toolbar's create buttons, so the pill they produce is
+ // PILL_CREATE_PX wide whatever the zoom. Only THIS path scales: the keyboard
+ // shortcuts and the agent keep useTimeline's flat default, since neither knows
+ // the zoom (`nav` is local state here). Before the panel is measured there is
+ // no zoom to read, so the default stands.
+ const createDurationSec =
+ pxPerSec > 0 ? Math.max(PILL_CREATE_MIN_SEC, PILL_CREATE_PX / pxPerSec) : undefined;
// ── region lanes ────────────────────────────────────────────────
// zoom/speed/annotation: one pill per row, never coalesced — each carries
@@ -1313,9 +1339,9 @@ export function V4Timeline({
title={tool.label}
aria-label={tool.label}
onClick={() => {
- if (tool.id === "speed") void tl.addSpeed();
- if (tool.id === "comment") void tl.addAnnotation();
- if (tool.id === "cut") void tl.addTrim();
+ if (tool.id === "speed") void tl.addSpeed(createDurationSec);
+ if (tool.id === "comment") void tl.addAnnotation(createDurationSec);
+ if (tool.id === "cut") void tl.addTrim(createDurationSec);
}}
>
{tool.icon}
@@ -1326,7 +1352,7 @@ export function V4Timeline({
className={styles.tlToolBtn}
title={t("buttons.addZoom")}
aria-label={t("buttons.addZoom")}
- onClick={() => void tl.addZoom()}
+ onClick={() => void tl.addZoom(createDurationSec)}
>
<ZoomIn size={15} />
</button>
@@ -1349,7 +1375,7 @@ export function V4Timeline({
className={styles.tlToolBtn}
title={t("buttons.addCameraFullscreen")}
aria-label={t("buttons.addCameraFullscreen")}
- onClick={() => void tl.addCameraFullscreen()}
+ onClick={() => void tl.addCameraFullscreen(createDurationSec)}
>
<Maximize2 size={15} />
</button>