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(editor): measure the card timecode gate instead of guessing per character

EtienneLescot89019289e2ec
diff --git a/src/components/ai-edition/v4/V4Timeline.geometry.test.tsx b/src/components/ai-edition/v4/V4Timeline.geometry.test.tsx
index 8f3320cc2..01269a5c5 100644
--- a/src/components/ai-edition/v4/V4Timeline.geometry.test.tsx
+++ b/src/components/ai-edition/v4/V4Timeline.geometry.test.tsx
@@ -17,6 +17,13 @@ vi.mock("sonner", () => ({ toast: { error: vi.fn(), info: vi.fn(), success: vi.f
// The audio lane's pill renders a ClipWaveform; no decode in this geometry suite.
vi.mock("@/hooks/useAudioPeaks", () => ({ useAudioPeaks: () => null }));
+// The duration gate reads its timecode's width off a canvas context. jsdom has
+// no canvas, so every test here runs against this stub: 6px a character, the
+// same figure the component's no-canvas fallback assumes, which keeps the width
+// arithmetic in the tests below the one the first cut reasoned in. The
+// measured-path test re-aims it at a wider face and expects the gate to follow.
+const measureText = vi.fn((text: string) => ({ width: text.length * 6 }));
+
import { ShortcutsProvider } from "@/contexts/ShortcutsContext";
import type { useTimeline } from "@/lib/ai-edition/store/useTimeline";
import { DEFAULT_SHORTCUTS, formatBinding } from "@/lib/shortcuts";
@@ -55,6 +62,13 @@ beforeAll(() => {
},
}),
});
+ vi.spyOn(HTMLCanvasElement.prototype, "getContext").mockImplementation(
+ () => ({ measureText }) as unknown as CanvasRenderingContext2D,
+ );
+});
+
+afterEach(() => {
+ measureText.mockImplementation((text: string) => ({ width: text.length * 6 }));
});
function clip(startSec: number, endSec: number) {
@@ -494,6 +508,18 @@ describe("V4Timeline clip row", () => {
expect(screen.getByText("56:05.0")).toBeInTheDocument();
});
+ it("measures the timecode where a canvas exists, rather than averaging its length", () => {
+ // The stubbed face costs 9px a character against the 6px the jsdom fallback
+ // assumes. A 700s clip of this 3965s span is a ~159px card — roomy enough
+ // by the count (50 + 47 + 7×6 = 139) and too tight once the face is read
+ // (50 + 47 + 7×9 = 160) — so only a measured gate withholds it.
+ measureText.mockImplementation((text: string) => ({ width: text.length * 9 }));
+ renderTimeline([clip(0, 700), clip(700, 3965)]);
+
+ expect(screen.queryByText("11:40.0")).not.toBeInTheDocument();
+ expect(screen.getByText("54:25.0")).toBeInTheDocument();
+ });
+
it("takes the card gutter out of each clip's own width", () => {
// The 6px is what separates two cards. Taken off the clip's width it stays
// local to that clip; inserted between them (a flex gap) it displaced every
diff --git a/src/components/ai-edition/v4/V4Timeline.tsx b/src/components/ai-edition/v4/V4Timeline.tsx
index bd97d7301..28aad552c 100644
--- a/src/components/ai-edition/v4/V4Timeline.tsx
+++ b/src/components/ai-edition/v4/V4Timeline.tsx
@@ -153,19 +153,33 @@ const PILL_SNAP_PX = 8;
const NARROW_CLIP_PX = 120;
// Whether a card can also carry its edited duration. The label pill is capped at
// `calc(100% - 50px)` so it clears the delete button, and everything inside it
-// but the name is incompressible: 15px of padding, the 16px pencil, two 8px
-// gaps, and the timecode. The timecode is the part that varies — `formatSec`
-// never prints an hour field, so a clip past ten minutes reads `16:40.0` and one
-// past a hundred `100:00.0` — so the room is measured against THIS card's own
-// text rather than a single number that only ever fitted the short form.
-// Measured in the running window: 6.0px per character at 10px in the mono face,
-// and a 6-character code overlapping the delete button at a 121px card, clear at
-// 131px.
+// but the name is incompressible: 15px of pill padding (`3px 9px 3px 6px`), the
+// pencil and two 8px gaps — 47px. The timecode is the part that varies —
+// `formatSec` never prints an hour field, so a clip past ten minutes reads
+// `16:40.0` and one past a hundred `100:00.0` — so its width is measured with
+// canvas `measureText` in the face `.tlClipDuration` actually renders, rather
+// than guessed from a per-character average. Only where canvas is unavailable
+// (jsdom) does the gate fall back to the first cut's estimate: 6px per
+// character at 10px in the mono face.
const CLIP_LABEL_RESERVE_PX = 50;
const CLIP_LABEL_FIXED_PX = 47;
-const CLIP_LABEL_CHAR_PX = 6;
+const CLIP_LABEL_FALLBACK_CHAR_PX = 6;
+// `.tlClipDuration` renders `500 10px/1.2 var(--font-mono)`; canvas wants the
+// same face without the line height, so the family comes from the token itself.
+let durationMeasureCtx: CanvasRenderingContext2D | null | undefined;
+function durationTextPx(text: string): number | undefined {
+ if (durationMeasureCtx === undefined) {
+ durationMeasureCtx = document.createElement("canvas").getContext("2d");
+ }
+ const ctx = durationMeasureCtx;
+ if (ctx === null) return undefined;
+ const family = getComputedStyle(document.documentElement).getPropertyValue("--font-mono").trim();
+ ctx.font = `500 10px ${family || "monospace"}`;
+ return ctx.measureText(text).width;
+}
function cardFitsDuration(cardPx: number, text: string): boolean {
- return cardPx >= CLIP_LABEL_RESERVE_PX + CLIP_LABEL_FIXED_PX + text.length * CLIP_LABEL_CHAR_PX;
+ const textPx = durationTextPx(text) ?? text.length * CLIP_LABEL_FALLBACK_CHAR_PX;
+ return cardPx >= CLIP_LABEL_RESERVE_PX + CLIP_LABEL_FIXED_PX + textPx;
}
const CLIP_GUTTER_PX = 6;