Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

feat(web): add trimTimelineClipEdge for per-clip edge trimming

Richie McIlroycc663a13c583
diff --git a/apps/web/lib/video-edits.ts b/apps/web/lib/video-edits.ts
index 8e25216a5..04a960753 100644
--- a/apps/web/lib/video-edits.ts
+++ b/apps/web/lib/video-edits.ts
@@ -871,6 +871,75 @@ export function dragSplitForShrink(
});
}
+/**
+ * Trim a single clip (display segment) from one of its edges, Loom-style.
+ *
+ * Outer edges (the first clip's left edge, the last clip's right edge) move the
+ * global in/out point (`trimStart`/`trimEnd`). Inner edges carve footage off the
+ * clip via {@link dragSplitForShrink}, leaving the neighbouring clips untouched.
+ * The carve only ever shrinks the clip it belongs to — it can never eat into an
+ * adjacent clip — so dragging clip A's right edge and clip B's left edge are
+ * independent operations even when A and B touch at a pure split.
+ */
+export function trimTimelineClipEdge(
+ state: VideoTimelineState,
+ clipId: string,
+ edge: "start" | "end",
+ sourceTime: number,
+): VideoTimelineState {
+ if (!isFiniteNumber(sourceTime)) return state;
+
+ const normalized = normalizeTimelineState(state);
+ const segments = getTimelineDisplaySegments(normalized);
+ const clip = segments.find((segment) => segment.id === clipId);
+ if (!clip) return state;
+
+ // An outer edge moves the global in/out point ONLY when the clip actually sits
+ // at that boundary. If footage was already carved off the front/back (a
+ // collapsed deletedRange before/after this clip), moving the trim would
+ // un-collapse it and balloon the timeline — so we carve instead, keeping the
+ // display tight like every other clip edge.
+ const movesTrimStart =
+ segments[0]?.id === clipId && clip.start <= normalized.trimStart + EPSILON;
+ const movesTrimEnd =
+ segments[segments.length - 1]?.id === clipId &&
+ clip.end >= normalized.trimEnd - EPSILON;
+
+ if (edge === "start") {
+ if (movesTrimStart) {
+ const target = clampEditTime(
+ sourceTime,
+ 0,
+ clip.end - MIN_RANGE_DURATION,
+ );
+ return setTimelineTrim(normalized, target, normalized.trimEnd);
+ }
+ const target = clampEditTime(
+ sourceTime,
+ clip.start,
+ clip.end - MIN_RANGE_DURATION,
+ );
+ if (target - clip.start < MIN_RANGE_DURATION) return normalized;
+ return dragSplitForShrink(normalized, clip.start, target);
+ }
+
+ if (movesTrimEnd) {
+ const target = clampEditTime(
+ sourceTime,
+ clip.start + MIN_RANGE_DURATION,
+ normalized.duration,
+ );
+ return setTimelineTrim(normalized, normalized.trimStart, target);
+ }
+ const target = clampEditTime(
+ sourceTime,
+ clip.start + MIN_RANGE_DURATION,
+ clip.end,
+ );
+ if (clip.end - target < MIN_RANGE_DURATION) return normalized;
+ return dragSplitForShrink(normalized, clip.end, target);
+}
+
export function getTimelineKeepRanges(
state: VideoTimelineState,
): VideoEditRange[] {