Skip to content

Create polished demo videos without editing skills. Mac/Windows/Linux

feat(captions): add timeline add-by-hover and refine the caption editor

Joe Hachem9250a676d646
diff --git a/src/components/video-editor/CaptionListPanel.tsx b/src/components/video-editor/CaptionListPanel.tsx
index 7ae2503c..91898c50 100644
--- a/src/components/video-editor/CaptionListPanel.tsx
+++ b/src/components/video-editor/CaptionListPanel.tsx
@@ -1,5 +1,6 @@
import { ArrowsMerge, Scissors, Trash } from "@phosphor-icons/react";
import { useCallback, useEffect, useState } from "react";
+import { Button } from "@/components/ui/button";
import type { CaptionRetimeSpan } from "./captionOps";
import type { CaptionCue } from "./types";
@@ -101,7 +102,10 @@ function CaptionEditor({
</span>
<textarea
value={draftText}
- rows={2}
+ rows={3}
+ // Freshly-added captions start empty — focus the field so the
+ // user can type immediately after dropping one on the timeline.
+ autoFocus={cue.text === ""}
onFocus={() => onBeginEdit(cue.id)}
onChange={(event) => setDraftText(event.target.value)}
onBlur={commitText}
@@ -115,7 +119,7 @@ function CaptionEditor({
event.currentTarget.blur();
}
}}
- className="w-full resize-none rounded-md border border-foreground/10 bg-background/60 px-2 py-1.5 text-sm text-foreground outline-none focus-visible:border-[#2563EB] focus-visible:ring-1 focus-visible:ring-[#2563EB]"
+ className="min-h-[4.5rem] w-full resize-none rounded-md border border-foreground/10 bg-background/60 px-2 py-1.5 text-sm text-foreground outline-none focus-visible:border-[#2563EB] focus-visible:ring-1 focus-visible:ring-[#2563EB]"
/>
</label>
@@ -174,14 +178,16 @@ function CaptionEditor({
<ArrowsMerge className="h-4 w-4" />
Merge
</button>
- <button
+ <Button
type="button"
+ variant="destructive"
+ size="sm"
onClick={() => onDelete(cue.id)}
- className="flex h-9 items-center justify-center gap-1.5 rounded-lg border border-destructive/30 bg-destructive/10 text-xs font-medium text-destructive transition-colors hover:bg-destructive/20"
+ className="h-9 gap-1.5 rounded-lg border border-red-500/20 bg-red-500/10 text-xs text-red-400 transition-all hover:border-red-500/30 hover:bg-red-500/20"
>
- <Trash className="h-4 w-4" />
+ <Trash className="h-3 w-3" />
Delete
- </button>
+ </Button>
</div>
</div>
);
@@ -208,6 +214,7 @@ export default function CaptionListPanel({
return (
<CaptionEditor
+ key={cue.id}
cue={cue}
canMerge={canMerge}
currentTimeMs={currentTimeMs}
diff --git a/src/components/video-editor/VideoEditor.tsx b/src/components/video-editor/VideoEditor.tsx
index 36093c7d..fc66c7dd 100644
--- a/src/components/video-editor/VideoEditor.tsx
+++ b/src/components/video-editor/VideoEditor.tsx
@@ -121,10 +121,19 @@ import { resolveAutoCaptionSourcePath } from "./autoCaptionSource";
import { CropControl } from "./CropControl";
import {
type CaptionEditTarget,
+ normalizeCaptionEditText,
normalizeCaptionWords,
updateCaptionCuesForEditedTarget,
} from "./captionEditing";
-import { type CaptionRetimeSpan, deleteCue, mergeCues, retimeCue, splitCue } from "./captionOps";
+import {
+ addCue,
+ type CaptionRetimeSpan,
+ createCaptionCue,
+ deleteCue,
+ mergeCues,
+ retimeCue,
+ splitCue,
+} from "./captionOps";
import { ExportSettingsMenu } from "./ExportSettingsMenu";
import ExtensionManager from "./ExtensionManager";
import {
@@ -3677,6 +3686,16 @@ export default function VideoEditor() {
return captions;
}
const words = normalizeCaptionWords(cue);
+ if (words.length === 0) {
+ // Text-only cue (e.g. a caption added manually from the timeline,
+ // which starts empty with no words). updateCaptionCuesForEditedTarget
+ // can't seed a cue that has no words, so set the text directly — the
+ // renderer derives placeholder word timings from the cue text.
+ const normalized = normalizeCaptionEditText(text);
+ return captions.map((value) =>
+ value.id === id ? { ...value, text: normalized } : value,
+ );
+ }
const target: CaptionEditTarget = {
id: cue.id,
startMs: cue.startMs,
@@ -3716,6 +3735,27 @@ export default function VideoEditor() {
setAutoCaptions((captions) => deleteCue(captions, id));
}, []);
+ const handleCaptionAdded = useCallback(
+ (span: Span) => {
+ videoPlaybackRef.current?.cancelCaptionEdit();
+ // span is in timeline-ms; caption cues are stored in source-ms.
+ const newCue = createCaptionCue({
+ startMs: mapTimelineTimeToSourceTime(span.start),
+ endMs: mapTimelineTimeToSourceTime(span.end),
+ });
+ setAutoCaptions((captions) => addCue(captions, newCue));
+ // Select the new caption and open its side-panel editor.
+ setSelectedCaptionId(newCue.id);
+ setActiveEffectSection("caption");
+ setSelectedZoomId(null);
+ setSelectedClipId(null);
+ setSelectedAnnotationId(null);
+ setSelectedAudioId(null);
+ handleSeek(span.start / 1000, { pause: true });
+ },
+ [handleSeek, mapTimelineTimeToSourceTime],
+ );
+
const handlePreviewSkipBack = useCallback(() => {
const currentMs = timelinePlayheadTime * 1000;
const keyframes = timelineRef.current?.keyframes ?? [];
@@ -3738,6 +3778,7 @@ export default function VideoEditor() {
setActiveEffectSection("zoom");
setSelectedAnnotationId(null);
setSelectedAudioId(null);
+ setSelectedCaptionId(null);
} else {
setActiveEffectSection((s) => (s === "zoom" ? "scene" : s));
}
@@ -3748,6 +3789,7 @@ export default function VideoEditor() {
if (id) {
setSelectedZoomId(null);
setSelectedAudioId(null);
+ setSelectedCaptionId(null);
}
}, []);
@@ -3770,6 +3812,7 @@ export default function VideoEditor() {
setZoomRegions((prev) => [...prev, newRegion]);
setSelectedZoomId(id);
setSelectedAnnotationId(null);
+ setSelectedCaptionId(null);
extensionHost.emitEvent({
type: "timeline:region-added",
data: { id, startMs: newRegion.startMs, endMs: newRegion.endMs },
@@ -3973,6 +4016,7 @@ export default function VideoEditor() {
setSelectedZoomId(null);
setSelectedAnnotationId(null);
setSelectedAudioId(null);
+ setSelectedCaptionId(null);
} else {
setActiveEffectSection((s) => (s === "clip" ? "scene" : s));
}
@@ -4163,6 +4207,7 @@ export default function VideoEditor() {
if (id) {
setSelectedZoomId(null);
setSelectedAnnotationId(null);
+ setSelectedCaptionId(null);
setActiveEffectSection("audio");
}
}, []);
@@ -4182,6 +4227,7 @@ export default function VideoEditor() {
setSelectedAudioId(id);
setSelectedZoomId(null);
setSelectedAnnotationId(null);
+ setSelectedCaptionId(null);
setActiveEffectSection("audio");
}, []);
@@ -6799,6 +6845,9 @@ export default function VideoEditor() {
}
selectedCaptionId={selectedCaptionId}
onSelectCaption={handleSelectCaption}
+ onCaptionDelete={handleCaptionDelete}
+ onCaptionAdded={handleCaptionAdded}
+ captionsEnabled={autoCaptionSettings.enabled}
annotationRegions={annotationRegions}
onAnnotationAdded={handleAnnotationAdded}
onAnnotationSpanChange={handleAnnotationSpanChange}
diff --git a/src/components/video-editor/captionLayout.ts b/src/components/video-editor/captionLayout.ts
index 727e4c44..8b2faad2 100644
--- a/src/components/video-editor/captionLayout.ts
+++ b/src/components/video-editor/captionLayout.ts
@@ -59,7 +59,9 @@ type CaptionSourceWord = {
};
const CAPTION_ENTER_MS = 180;
-const CAPTION_EXIT_MS = 140;
+// Keep the exit symmetric with the entrance so captions fade out with the
+// same opacity/translateY/scale curve and duration they faded in with.
+const CAPTION_EXIT_MS = CAPTION_ENTER_MS;
const CAPTION_BLOCK_GAP_BREAK_MS = 500;
function clamp(value: number, min: number, max: number) {
@@ -518,13 +520,34 @@ export function buildActiveCaptionLayout(options: {
getActiveCaptionCue(options.cues, options.timeMs) ??
options.cues.find((candidate) => candidate.id === pageCueId) ??
options.cues[0];
+ // `animationEndMs` is stretched to the next page's start (see buildCaptionPages)
+ // so consecutive pages stay on screen with no gap. That's right for continuous
+ // speech, but pages are built from every cue flattened together — so when a
+ // caption component (cue) is the last before a pause, its stitched end points
+ // across the silence into the next cue. exitProgress would then stay pinned at
+ // 1 and the caption would vanish with no fade. Anchor the exit to the real end
+ // of the visible words instead, and only when nothing follows within the
+ // gap-break window (otherwise keep the stitched end so continuous page swaps
+ // don't flicker).
+ const visibleContentEndMs = visibleWords.reduce(
+ (latest, word) => Math.max(latest, word.endMs),
+ animationStartMs,
+ );
+ const willDisappear =
+ options.settings.animationStyle !== "none" &&
+ !isWithinCaptionCoverage(options.cues, visibleContentEndMs + 1);
+ const exitAnchorMs = willDisappear ? visibleContentEndMs : animationEndMs;
const enterProgress = clamp01((options.timeMs - animationStartMs) / CAPTION_ENTER_MS);
- const exitProgress = clamp01((animationEndMs - options.timeMs) / CAPTION_EXIT_MS);
+ const exitProgress = clamp01((exitAnchorMs - options.timeMs) / CAPTION_EXIT_MS);
const animation = getCaptionAnimationState(
options.settings.animationStyle,
enterProgress,
exitProgress,
);
+ // On a real disappearance the per-style opacity only eases to its readability
+ // floor (~0.3) before the element unmounts, which reads as a snap. Fade fully
+ // to 0 across the exit window so the caption leaves smoothly.
+ const opacity = willDisappear ? animation.opacity * exitProgress : animation.opacity;
return {
cue: activeCue,
@@ -550,7 +573,7 @@ export function buildActiveCaptionLayout(options: {
})),
},
visiblePageIndex,
- opacity: animation.opacity,
+ opacity,
translateY: animation.translateY,
scale: animation.scale,
} satisfies ActiveCaptionLayout;
diff --git a/src/components/video-editor/captionOps.ts b/src/components/video-editor/captionOps.ts
index 5d2324f3..95f15802 100644
--- a/src/components/video-editor/captionOps.ts
+++ b/src/components/video-editor/captionOps.ts
@@ -20,6 +20,29 @@ function createCaptionCueId(): string {
return `caption-${globalThis.crypto.randomUUID()}`;
}
+/**
+ * Build a new caption cue with a generated id. Word timings are intentionally
+ * omitted — the renderer derives placeholder per-word timings from the text
+ * until the user adds real ones.
+ */
+export function createCaptionCue(params: {
+ startMs: number;
+ endMs: number;
+ text?: string;
+}): CaptionCue {
+ return {
+ id: createCaptionCueId(),
+ startMs: Math.round(params.startMs),
+ endMs: Math.round(params.endMs),
+ text: params.text ?? "",
+ };
+}
+
+/** Insert a cue and keep the list ordered, matching the other cue operations. */
+export function addCue(cues: CaptionCue[], cue: CaptionCue): CaptionCue[] {
+ return sortCaptionCues([...cues, cue]);
+}
+
function rescaleWordsIntoSpan(
words: CaptionCueWord[],
oldStartMs: number,
diff --git a/src/components/video-editor/timeline/TimelineEditor.tsx b/src/components/video-editor/timeline/TimelineEditor.tsx
index 2b353b29..0607ea5e 100644
--- a/src/components/video-editor/timeline/TimelineEditor.tsx
+++ b/src/components/video-editor/timeline/TimelineEditor.tsx
@@ -71,6 +71,9 @@ export interface TimelineEditorProps {
onSelectAudio?: (id: string | null) => void;
captionRegions?: CaptionCue[];
onCaptionSpanChange?: (id: string, span: Span) => void;
+ onCaptionDelete?: (id: string) => void;
+ onCaptionAdded?: (span: Span) => void;
+ captionsEnabled?: boolean;
selectedCaptionId?: string | null;
onSelectCaption?: (id: string | null) => void;
videoPath?: string | null;
@@ -149,6 +152,9 @@ const TimelineEditor = forwardRef<TimelineEditorHandle, TimelineEditorProps>(
onSelectAudio,
captionRegions = [],
onCaptionSpanChange,
+ onCaptionDelete,
+ onCaptionAdded,
+ captionsEnabled = false,
selectedCaptionId,
onSelectCaption,
videoPath,
@@ -188,6 +194,7 @@ const TimelineEditor = forwardRef<TimelineEditorHandle, TimelineEditorProps>(
});
const [liveSpanPreviewById, setLiveSpanPreviewById] = useState<Record<string, Span>>({});
+ const [isDragging, setIsDragging] = useState(false);
const liveZoomPreview = useMemo(() => {
const previewSpans: Record<string, Span> = { ...liveSpanPreviewById };
const hiddenZoomIds = new Set<string>();
@@ -331,6 +338,8 @@ const TimelineEditor = forwardRef<TimelineEditorHandle, TimelineEditorProps>(
handleItemSpanChange,
canPlaceZoomAtMs,
addZoomAtMs,
+ canPlaceCaptionAtMs,
+ addCaptionAtMs,
} = useTimelineEditorRuntime({
ref,
videoDuration,
@@ -372,6 +381,10 @@ const TimelineEditor = forwardRef<TimelineEditorHandle, TimelineEditorProps>(
onSelectAudio,
captionCues: captionRegions,
onCaptionSpanChange,
+ onCaptionDelete,
+ onCaptionAdded,
+ selectedCaptionId,
+ onSelectCaption,
isMac,
keyShortcuts,
isTimelineFocusedRef,
@@ -425,6 +438,7 @@ const TimelineEditor = forwardRef<TimelineEditorHandle, TimelineEditorProps>(
onItemSpanChange={handleItemSpanChange}
resolveTargetRowId={getResolvedDropRowId}
allRegionSpans={allRegionSpans}
+ onDraggingChange={setIsDragging}
onLiveSpanPreviewChange={(id, span) => {
setLiveSpanPreviewById((prev) => {
if (!span) {
@@ -460,6 +474,9 @@ const TimelineEditor = forwardRef<TimelineEditorHandle, TimelineEditorProps>(
onSeek={onSeek}
onAddZoomAtMs={addZoomAtMs}
canPlaceZoomAtMs={canPlaceZoomAtMs}
+ onAddCaptionAtMs={addCaptionAtMs}
+ canPlaceCaptionAtMs={canPlaceCaptionAtMs}
+ captionsEnabled={captionsEnabled}
onSelectZoom={handleSelectZoom}
onSelectClip={handleSelectClip}
onSelectAnnotation={handleSelectAnnotation}
@@ -478,6 +495,7 @@ const TimelineEditor = forwardRef<TimelineEditorHandle, TimelineEditorProps>(
showSourceAudioTrack={showSourceAudioTrack}
liveSpanPreviewById={liveZoomPreview.previewSpans}
liveHiddenItemIds={Array.from(liveZoomPreview.hiddenZoomIds)}
+ isDragging={isDragging}
isLoading={isLoading}
/>
</TimelineWrapper>
diff --git a/src/components/video-editor/timeline/components/viewport/TimelineCanvas.tsx b/src/components/video-editor/timeline/components/viewport/TimelineCanvas.tsx
index 4e0e61bd..928f7536 100644
--- a/src/components/video-editor/timeline/components/viewport/TimelineCanvas.tsx
+++ b/src/components/video-editor/timeline/components/viewport/TimelineCanvas.tsx
@@ -30,6 +30,7 @@ import {
isAudioTrackRowId,
} from "../../core/rows";
import type { TimelineRenderItem } from "../../core/timelineTypes";
+import { DEFAULT_CAPTION_DURATION_MS } from "../../hooks/actions/useTimelineCaptionActions";
import { useTimelineAudioPeaks } from "../../hooks/useTimelineAudioPeaks";
import Item from "../../Item";
import glassStyles from "../../ItemGlass.module.css";
@@ -60,6 +61,9 @@ interface TimelineCanvasProps {
onSelectAudio?: (id: string | null) => void;
onSelectCaption?: (id: string | null) => void;
onAddZoomAtMs?: (startMs: number) => void;
+ onAddCaptionAtMs?: (startMs: number) => void;
+ canPlaceCaptionAtMs?: (startMs: number) => boolean;
+ captionsEnabled?: boolean;
selectedZoomId: string | null;
selectedClipId?: string | null;
selectedAnnotationId?: string | null;
@@ -73,9 +77,133 @@ interface TimelineCanvasProps {
showSourceAudioTrack?: boolean;
liveSpanPreviewById?: Record<string, { start: number; end: number }>;
liveHiddenItemIds?: string[];
+ isDragging?: boolean;
isLoading?: boolean;
}
+interface LaneHoverParams {
+ direction: string;
+ rangeStart: number;
+ visibleDurationMs: number;
+ videoDurationMs: number;
+ valueToPixels: (value: number) => number;
+ // Ghost block length to preview under the cursor.
+ ghostDurationMs: number;
+ // Whether the lane currently accepts adds (e.g. captions only when shown).
+ enabled: boolean;
+ // Suppress the ghost while a drag/resize is in progress.
+ isDragging: boolean;
+ onAddAtMs?: (startMs: number) => void;
+ canPlaceAtMs?: (startMs: number) => boolean;
+}
+
+/**
+ * Hover + click-to-add behaviour for a single timeline lane (zoom, captions, …).
+ * Tracks the pointer position over the row and derives the translucent "add"
+ * ghost geometry. Lanes differ only by their ghost duration, enabled flag and
+ * add/can-place callbacks.
+ */
+function useTimelineLaneHover({
+ direction,
+ rangeStart,
+ visibleDurationMs,
+ videoDurationMs,
+ valueToPixels,
+ ghostDurationMs,
+ enabled,
+ isDragging,
+ onAddAtMs,
+ canPlaceAtMs,
+}: LaneHoverParams) {
+ const [isHovered, setIsHovered] = useState(false);
+ const [hoverMs, setHoverMs] = useState<number | null>(null);
+
+ const updateHoverTime = useCallback(
+ (clientX: number, rect: DOMRect) => {
+ if (rect.width <= 0) return;
+ const position =
+ direction === "rtl"
+ ? Math.max(0, Math.min(rect.right - clientX, rect.width))
+ : Math.max(0, Math.min(clientX - rect.left, rect.width));
+ const ratio = position / rect.width;
+ const nextMs = rangeStart + ratio * visibleDurationMs;
+ setHoverMs(Math.max(0, Math.min(nextMs, videoDurationMs)));
+ },
+ [direction, rangeStart, videoDurationMs, visibleDurationMs],
+ );
+
+ const onMouseEnter = useCallback(
+ (event: MouseEvent<HTMLDivElement>) => {
+ setIsHovered(true);
+ updateHoverTime(event.clientX, event.currentTarget.getBoundingClientRect());
+ },
+ [updateHoverTime],
+ );
+
+ const onMouseMove = useCallback(
+ (event: MouseEvent<HTMLDivElement>) => {
+ setIsHovered(true);
+ updateHoverTime(event.clientX, event.currentTarget.getBoundingClientRect());
+ },
+ [updateHoverTime],
+ );
+
+ const onMouseLeave = useCallback(() => {
+ setIsHovered(false);
+ setHoverMs(null);
+ }, []);
+
+ const onMouseDown = useCallback((event: MouseEvent<HTMLDivElement>) => {
+ event.stopPropagation();
+ }, []);
+
+ const onClick = useCallback(
+ (event: MouseEvent<HTMLDivElement>) => {
+ event.stopPropagation();
+ if (!onAddAtMs || hoverMs === null) return;
+ const startMs = Math.max(0, Math.min(hoverMs, videoDurationMs));
+ if (canPlaceAtMs && !canPlaceAtMs(startMs)) return;
+ onAddAtMs(startMs);
+ },
+ [canPlaceAtMs, onAddAtMs, videoDurationMs, hoverMs],
+ );
+
+ const reset = useCallback(() => {
+ setIsHovered(false);
+ setHoverMs(null);
+ }, []);
+
+ const ghostStartMs = hoverMs === null ? null : Math.max(0, Math.min(hoverMs, videoDurationMs));
+ const ghostEndMs =
+ ghostStartMs === null
+ ? null
+ : Math.max(ghostStartMs, Math.min(videoDurationMs, ghostStartMs + ghostDurationMs));
+ const ghostStartOffsetPx =
+ ghostStartMs === null ? 0 : valueToPixels(Math.max(0, ghostStartMs - rangeStart));
+ const ghostEndOffsetPx =
+ ghostEndMs === null ? 0 : valueToPixels(Math.max(0, ghostEndMs - rangeStart));
+ const ghostWidthPx = Math.max(18, ghostEndOffsetPx - ghostStartOffsetPx);
+ const canShowGhost =
+ !isDragging &&
+ enabled &&
+ isHovered &&
+ ghostStartMs !== null &&
+ (onAddAtMs ? (canPlaceAtMs?.(ghostStartMs) ?? true) : false);
+
+ return {
+ reset,
+ ghostStartMs,
+ ghostStartOffsetPx,
+ ghostWidthPx,
+ canShowGhost,
+ onMouseEnter,
+ onMouseMove,
+ onMouseLeave,
+ onMouseDown,
+ onClick,
+ };
+}
+
interface TimelineHoverParams {
direction: string;
sidebarWidth: number;
@@ -84,6 +212,10 @@ interface TimelineHoverParams {
videoDurationMs: number;
onAddZoomAtMs?: (startMs: number) => void;
canPlaceZoomAtMs?: (startMs: number) => boolean;
+ onAddCaptionAtMs?: (startMs: number) => void;
+ canPlaceCaptionAtMs?: (startMs: number) => boolean;
+ captionsEnabled?: boolean;
+ isDragging: boolean;
valueToPixels: (value: number) => number;
}
@@ -95,12 +227,14 @@ function useTimelineHover({
videoDurationMs,
onAddZoomAtMs,
canPlaceZoomAtMs,
+ onAddCaptionAtMs,
+ canPlaceCaptionAtMs,
+ captionsEnabled,
+ isDragging,
valueToPixels,
}: TimelineHoverParams) {
const [isTimelineHovered, setIsTimelineHovered] = useState(false);
const [timelineHoverMs, setTimelineHoverMs] = useState<number | null>(null);
- const [isZoomRowHovered, setIsZoomRowHovered] = useState(false);
- const [zoomRowHoverMs, setZoomRowHoverMs] = useState<number | null>(null);
const visibleDurationMs = Math.max(1, rangeEnd - rangeStart);
@@ -135,82 +269,42 @@ function useTimelineHover({
[isTimelineHovered, updateTimelineHoverTime],
);
+ const zoom = useTimelineLaneHover({
+ direction,
+ rangeStart,
+ visibleDurationMs,
+ videoDurationMs,
+ valueToPixels,
+ ghostDurationMs: Math.min(1000, videoDurationMs),
+ enabled: true,
+ isDragging,
+ onAddAtMs: onAddZoomAtMs,
+ canPlaceAtMs: canPlaceZoomAtMs,
+ });
+
+ const caption = useTimelineLaneHover({
+ direction,
+ rangeStart,
+ visibleDurationMs,
+ videoDurationMs,
+ valueToPixels,
+ ghostDurationMs: Math.min(DEFAULT_CAPTION_DURATION_MS, videoDurationMs),
+ enabled: Boolean(captionsEnabled),
+ isDragging,
+ onAddAtMs: onAddCaptionAtMs,
+ canPlaceAtMs: canPlaceCaptionAtMs,
+ });
+
const handleTimelineMouseLeave = useCallback(() => {
setIsTimelineHovered(false);
setTimelineHoverMs(null);
- setIsZoomRowHovered(false);
- setZoomRowHoverMs(null);
- }, []);
-
- const updateZoomRowHoverTime = useCallback(
- (clientX: number, rect: DOMRect) => {
- if (rect.width <= 0) return;
- const position =
- direction === "rtl"
- ? Math.max(0, Math.min(rect.right - clientX, rect.width))
- : Math.max(0, Math.min(clientX - rect.left, rect.width));
- const ratio = position / rect.width;
- const nextMs = rangeStart + ratio * visibleDurationMs;
- setZoomRowHoverMs(Math.max(0, Math.min(nextMs, videoDurationMs)));
- },
- [direction, rangeStart, videoDurationMs, visibleDurationMs],
- );
-
- const handleZoomRowMouseEnter = useCallback(
- (event: MouseEvent<HTMLDivElement>) => {
- setIsZoomRowHovered(true);
- updateZoomRowHoverTime(event.clientX, event.currentTarget.getBoundingClientRect());
- },
- [updateZoomRowHoverTime],
- );
-
- const handleZoomRowMouseMove = useCallback(
- (event: MouseEvent<HTMLDivElement>) => {
- if (!isZoomRowHovered) setIsZoomRowHovered(true);
- updateZoomRowHoverTime(event.clientX, event.currentTarget.getBoundingClientRect());
- },
- [isZoomRowHovered, updateZoomRowHoverTime],
- );
-
- const handleZoomRowMouseLeave = useCallback(() => {
- setIsZoomRowHovered(false);
- setZoomRowHoverMs(null);
- }, []);
-
- const handleZoomRowMouseDown = useCallback((event: MouseEvent<HTMLDivElement>) => {
- event.stopPropagation();
- }, []);
-
- const handleZoomRowClick = useCallback(
- (event: MouseEvent<HTMLDivElement>) => {
- event.stopPropagation();
- if (!onAddZoomAtMs || zoomRowHoverMs === null) return;
- const startMs = Math.max(0, Math.min(zoomRowHoverMs, videoDurationMs));
- if (canPlaceZoomAtMs && !canPlaceZoomAtMs(startMs)) return;
- onAddZoomAtMs(startMs);
- },
- [canPlaceZoomAtMs, onAddZoomAtMs, videoDurationMs, zoomRowHoverMs],
- );
+ zoom.reset();
+ caption.reset();
+ }, [zoom.reset, caption.reset]);
- const ghostStartMs =
- zoomRowHoverMs === null ? null : Math.max(0, Math.min(zoomRowHoverMs, videoDurationMs));
- const ghostDurationMs = Math.min(1000, videoDurationMs);
- const ghostEndMs =
- ghostStartMs === null
- ? null
- : Math.max(ghostStartMs, Math.min(videoDurationMs, ghostStartMs + ghostDurationMs));
- const ghostStartOffsetPx =
- ghostStartMs === null ? 0 : valueToPixels(Math.max(0, ghostStartMs - rangeStart));
- const ghostEndOffsetPx =
- ghostEndMs === null ? 0 : valueToPixels(Math.max(0, ghostEndMs - rangeStart));
- const ghostWidthPx = Math.max(18, ghostEndOffsetPx - ghostStartOffsetPx);
const timelineGhostOffsetPx =
timelineHoverMs === null ? 0 : valueToPixels(Math.max(0, timelineHoverMs - rangeStart));
const canShowGhostPlayhead = isTimelineHovered && timelineHoverMs !== null;
- const canShowGhostZoom =
- isZoomRowHovered &&
- ghostStartMs !== null &&
- (onAddZoomAtMs ? (canPlaceZoomAtMs?.(ghostStartMs) ?? true) : false);
return {
canShowGhostPlayhead,
@@ -218,15 +312,24 @@ function useTimelineHover({
handleTimelineMouseEnter,
handleTimelineMouseMove,
handleTimelineMouseLeave,
- canShowGhostZoom,
- ghostStartMs,
- ghostStartOffsetPx,
- ghostWidthPx,
- handleZoomRowMouseEnter,
- handleZoomRowMouseMove,
- handleZoomRowMouseLeave,
- handleZoomRowMouseDown,
- handleZoomRowClick,
+ canShowGhostZoom: zoom.canShowGhost,
+ ghostStartMs: zoom.ghostStartMs,
+ ghostStartOffsetPx: zoom.ghostStartOffsetPx,
+ ghostWidthPx: zoom.ghostWidthPx,
+ handleZoomRowMouseEnter: zoom.onMouseEnter,
+ handleZoomRowMouseMove: zoom.onMouseMove,
+ handleZoomRowMouseLeave: zoom.onMouseLeave,
+ handleZoomRowMouseDown: zoom.onMouseDown,
+ handleZoomRowClick: zoom.onClick,
+ canShowGhostCaption: caption.canShowGhost,
+ captionGhostStartMs: caption.ghostStartMs,
+ captionGhostStartOffsetPx: caption.ghostStartOffsetPx,
+ captionGhostWidthPx: caption.ghostWidthPx,
+ handleCaptionRowMouseEnter: caption.onMouseEnter,
+ handleCaptionRowMouseMove: caption.onMouseMove,
+ handleCaptionRowMouseLeave: caption.onMouseLeave,
+ handleCaptionRowMouseDown: caption.onMouseDown,
+ handleCaptionRowClick: caption.onClick,
};
}
@@ -259,6 +362,16 @@ interface TimelineCanvasRowsProps {
onZoomRowMouseLeave: MouseEventHandler<HTMLDivElement>;
onZoomRowMouseDown: MouseEventHandler<HTMLDivElement>;
onZoomRowClick: MouseEventHandler<HTMLDivElement>;
+ captionsEnabled?: boolean;
+ canShowGhostCaption: boolean;
+ captionGhostStartMs: number | null;
+ captionGhostStartOffsetPx: number;
+ captionGhostWidthPx: number;
+ onCaptionRowMouseEnter: MouseEventHandler<HTMLDivElement>;
+ onCaptionRowMouseMove: MouseEventHandler<HTMLDivElement>;
+ onCaptionRowMouseLeave: MouseEventHandler<HTMLDivElement>;
+ onCaptionRowMouseDown: MouseEventHandler<HTMLDivElement>;
+ onCaptionRowClick: MouseEventHandler<HTMLDivElement>;
}
interface AudioItemWithWaveformProps {
@@ -328,6 +441,16 @@ const TimelineCanvasRows = memo(function TimelineCanvasRows({
onZoomRowMouseLeave,
onZoomRowMouseDown,
onZoomRowClick,
+ captionsEnabled = false,
+ canShowGhostCaption,
+ captionGhostStartMs,
+ captionGhostStartOffsetPx,
+ captionGhostWidthPx,
+ onCaptionRowMouseEnter,
+ onCaptionRowMouseMove,
+ onCaptionRowMouseLeave,
+ onCaptionRowMouseDown,
+ onCaptionRowClick,
}: TimelineCanvasRowsProps) {
const hiddenIds = useMemo(() => new Set(liveHiddenItemIds ?? []), [liveHiddenItemIds]);
const { clipItems, zoomItems, captionItems, annotationRows, audioRows } = useMemo(() => {
@@ -497,8 +620,45 @@ const TimelineCanvasRows = memo(function TimelineCanvasRows({
))}
</Row>
- {captionItems.length > 0 && (
- <Row id={CAPTION_ROW_ID} isEmpty={false}>
+ {(captionsEnabled || captionItems.length > 0) && (
+ <Row
+ id={CAPTION_ROW_ID}
+ isEmpty={captionItems.length === 0}
+ onMouseEnter={onCaptionRowMouseEnter}
+ onMouseMove={onCaptionRowMouseMove}
+ onMouseLeave={onCaptionRowMouseLeave}
+ onMouseDown={onCaptionRowMouseDown}
+ onClick={onCaptionRowClick}
+ >
+ {canShowGhostCaption && captionGhostStartMs !== null && (
+ <div className="absolute inset-0 z-[3] pointer-events-none">
+ <div
+ className="absolute top-1/2 -translate-y-1/2 h-[85%] min-h-[22px]"
+ style={
+ direction === "rtl"
+ ? {
+ right: `${captionGhostStartOffsetPx}px`,
+ width: `${captionGhostWidthPx}px`,
+ }
+ : {
+ left: `${captionGhostStartOffsetPx}px`,
+ width: `${captionGhostWidthPx}px`,
+ }
+ }
+ >
+ <div
+ className={cn(
+ glassStyles.glassCaption,
+ "w-full h-full overflow-hidden flex items-center justify-center cursor-default relative opacity-80",
+ )}
+ >
+ <div className="relative z-10 inline-flex h-4 w-4 items-center justify-center rounded-full border border-white/45 bg-white/15 text-white">
+ <Plus className="h-2.5 w-2.5" />
+ </div>
+ </div>
+ </div>
+ </div>
+ )}
{captionItems.map((item) => (
<Item
id={item.id}
@@ -568,6 +728,9 @@ export default function TimelineCanvas({
onSeek,
onAddZoomAtMs,
canPlaceZoomAtMs,
+ onAddCaptionAtMs,
+ canPlaceCaptionAtMs,
+ captionsEnabled,
onSelectZoom,
onSelectClip,
onSelectAnnotation,
@@ -586,6 +749,7 @@ export default function TimelineCanvas({
showSourceAudioTrack = false,
liveSpanPreviewById,
liveHiddenItemIds,
+ isDragging = false,
isLoading = false,
}: TimelineCanvasProps) {
const { setTimelineRef, style, sidebarWidth, direction, range, valueToPixels, pixelsToValue } =
@@ -770,6 +934,15 @@ export default function TimelineCanvas({
handleZoomRowMouseLeave,
handleZoomRowMouseDown,
handleZoomRowClick,
+ canShowGhostCaption,
+ captionGhostStartMs,
+ captionGhostStartOffsetPx,
+ captionGhostWidthPx,
+ handleCaptionRowMouseEnter,
+ handleCaptionRowMouseMove,
+ handleCaptionRowMouseLeave,
+ handleCaptionRowMouseDown,
+ handleCaptionRowClick,
} = useTimelineHover({
direction,
sidebarWidth,
@@ -778,6 +951,10 @@ export default function TimelineCanvas({
videoDurationMs,
onAddZoomAtMs,
canPlaceZoomAtMs,
+ onAddCaptionAtMs,
+ canPlaceCaptionAtMs,
+ captionsEnabled,
+ isDragging,
valueToPixels,
});
@@ -852,6 +1029,16 @@ export default function TimelineCanvas({
onZoomRowMouseLeave={handleZoomRowMouseLeave}
onZoomRowMouseDown={handleZoomRowMouseDown}
onZoomRowClick={handleZoomRowClick}
+ captionsEnabled={captionsEnabled}
+ canShowGhostCaption={canShowGhostCaption}
+ captionGhostStartMs={captionGhostStartMs}
+ captionGhostStartOffsetPx={captionGhostStartOffsetPx}
+ captionGhostWidthPx={captionGhostWidthPx}
+ onCaptionRowMouseEnter={handleCaptionRowMouseEnter}
+ onCaptionRowMouseMove={handleCaptionRowMouseMove}
+ onCaptionRowMouseLeave={handleCaptionRowMouseLeave}
+ onCaptionRowMouseDown={handleCaptionRowMouseDown}
+ onCaptionRowClick={handleCaptionRowClick}
/>
</div>
</div>
diff --git a/src/components/video-editor/timeline/components/wrapper/TimelineWrapper.tsx b/src/components/video-editor/timeline/components/wrapper/TimelineWrapper.tsx
index 4e09f74e..fa3cc923 100644
--- a/src/components/video-editor/timeline/components/wrapper/TimelineWrapper.tsx
+++ b/src/components/video-editor/timeline/components/wrapper/TimelineWrapper.tsx
@@ -26,6 +26,7 @@ interface TimelineWrapperProps {
resolveTargetRowId?: (id: string, proposedRowId: string) => string;
allRegionSpans?: TimelineRegionSpan[];
onLiveSpanPreviewChange?: (id: string, span: Span | null) => void;
+ onDraggingChange?: (dragging: boolean) => void;
}
export default function TimelineWrapper({
@@ -41,6 +42,7 @@ export default function TimelineWrapper({
resolveTargetRowId,
allRegionSpans = [],
onLiveSpanPreviewChange,
+ onDraggingChange,
}: TimelineWrapperProps) {
const totalMs = Math.max(0, Math.round(videoDuration * 1000));
@@ -152,10 +154,11 @@ export default function TimelineWrapper({
const onDragStart = useCallback(
(event: DragStartEvent) => {
+ onDraggingChange?.(true);
const span = event.active.data.current.getSpanFromDragEvent?.(event);
if (span) showTooltip(span);
},
- [showTooltip],
+ [onDraggingChange, showTooltip],
);
const onDragMove = useCallback(
@@ -181,6 +184,7 @@ export default function TimelineWrapper({
const onResizeMove = useCallback(
(event: ResizeMoveEvent) => {
+ onDraggingChange?.(true);
const span = event.active.data.current.getSpanFromResizeEvent?.(event);
const screenX =
event.activatorEvent && "clientX" in event.activatorEvent
@@ -190,7 +194,7 @@ export default function TimelineWrapper({
// dnd-timeline mutates the active item's DOM during resize; React preview
// renders here can reset that inline width/edge position and make trims stutter.
},
- [showTooltip],
+ [onDraggingChange, showTooltip],
);
const hideTooltip = useCallback(() => showTooltip(null), [showTooltip]);
@@ -200,8 +204,9 @@ export default function TimelineWrapper({
hideTooltip();
onResizeEnd(event);
onLiveSpanPreviewChange?.(event.active.id as string, null);
+ onDraggingChange?.(false);
},
- [hideTooltip, onLiveSpanPreviewChange, onResizeEnd],
+ [hideTooltip, onDraggingChange, onLiveSpanPreviewChange, onResizeEnd],
);
const onDragEndWithTooltip = useCallback(
@@ -209,8 +214,20 @@ export default function TimelineWrapper({
hideTooltip();
onDragEnd(event);
onLiveSpanPreviewChange?.(event.active.id as string, null);
+ onDraggingChange?.(false);
},
- [hideTooltip, onDragEnd, onLiveSpanPreviewChange],
+ [hideTooltip, onDraggingChange, onDragEnd, onLiveSpanPreviewChange],
+ );
+
+ // dnd-kit fires onDragCancel (not onDragEnd) when a drag is aborted, e.g. via
+ // Escape. Without this the drag-active state and live preview would stick.
+ const onDragCancel = useCallback(
+ (event: { active: { id: string | number } }) => {
+ hideTooltip();
+ onLiveSpanPreviewChange?.(String(event.active.id), null);
+ onDraggingChange?.(false);
+ },
+ [hideTooltip, onDraggingChange, onLiveSpanPreviewChange],
);
const handleRangeChange = useCallback(
@@ -239,6 +256,7 @@ export default function TimelineWrapper({
onDragStart={onDragStart}
onDragMove={onDragMove}
onDragEnd={onDragEndWithTooltip}
+ onDragCancel={onDragCancel}
autoScroll={{
enabled: true,
threshold: { x: 0.08, y: 0.08 },
diff --git a/src/components/video-editor/timeline/hooks/actions/useTimelineCaptionActions.ts b/src/components/video-editor/timeline/hooks/actions/useTimelineCaptionActions.ts
new file mode 100644
index 00000000..09190bb0
--- /dev/null
+++ b/src/components/video-editor/timeline/hooks/actions/useTimelineCaptionActions.ts
@@ -0,0 +1,68 @@
+import type { Span } from "dnd-timeline";
+import { useCallback } from "react";
+import type { CaptionCue } from "../../../types";
+import { timelineNotifications } from "../utils/timelineNotifications";
+
+// Default length of a caption added manually from the timeline. Shared with the
+// hover ghost so the preview width matches what actually gets created.
+export const DEFAULT_CAPTION_DURATION_MS = 1500;
+
+interface UseTimelineCaptionActionsParams {
+ totalMs: number;
+ // Caption regions in timeline-ms (matches the hover position passed in).
+ captionRegions: CaptionCue[];
+ onCaptionAdded?: (span: Span) => void;
+}
+
+export function useTimelineCaptionActions({
+ totalMs,
+ captionRegions,
+ onCaptionAdded,
+}: UseTimelineCaptionActionsParams) {
+ const canPlaceCaptionAtMs = useCallback(
+ (startMs: number) => {
+ if (totalMs === 0) {
+ return false;
+ }
+ const startPos = Math.max(0, Math.min(startMs, totalMs));
+ // Only block when the cursor sits inside an existing caption — tight
+ // gaps are fine because the new caption is clamped to fit (see below).
+ return !captionRegions.some((cue) => startPos >= cue.startMs && startPos < cue.endMs);
+ },
+ [totalMs, captionRegions],
+ );
+
+ const addCaptionAtMs = useCallback(
+ (startMs: number) => {
+ if (!onCaptionAdded || totalMs === 0) {
+ return;
+ }
+ const startPos = Math.max(0, Math.min(startMs, totalMs));
+ if (!canPlaceCaptionAtMs(startPos)) {
+ timelineNotifications.error(
+ "Cannot place caption here",
+ "A caption already exists at this position.",
+ );
+ return;
+ }
+ const nextCaptionStartMs = captionRegions
+ .filter((cue) => cue.startMs > startPos)
+ .reduce((min, cue) => Math.min(min, cue.startMs), totalMs);
+ const endPos = Math.min(
+ startPos + DEFAULT_CAPTION_DURATION_MS,
+ totalMs,
+ nextCaptionStartMs,
+ );
+ if (endPos <= startPos) {
+ return;
+ }
+ onCaptionAdded({ start: startPos, end: endPos });
+ },
+ [onCaptionAdded, totalMs, canPlaceCaptionAtMs, captionRegions],
+ );
+
+ return {
+ canPlaceCaptionAtMs,
+ addCaptionAtMs,
+ };
+}
diff --git a/src/components/video-editor/timeline/hooks/useTimelineEditorRuntime.ts b/src/components/video-editor/timeline/hooks/useTimelineEditorRuntime.ts
index 865f01a1..25d830c3 100644
--- a/src/components/video-editor/timeline/hooks/useTimelineEditorRuntime.ts
+++ b/src/components/video-editor/timeline/hooks/useTimelineEditorRuntime.ts
@@ -15,6 +15,7 @@ import type {
import type { TimelineShortcutBindings } from "../core/timelineTypes";
import type { TimelineEditorHandle } from "../TimelineEditor";
import { useTimelineAudioActions } from "./actions/useTimelineAudioActions";
+import { useTimelineCaptionActions } from "./actions/useTimelineCaptionActions";
import { useTimelineZoomActions } from "./actions/useTimelineZoomActions";
import { useTimelineDndBindings } from "./useTimelineDndBindings";
import { useTimelineKeyboardShortcuts } from "./useTimelineKeyboardShortcuts";
@@ -62,6 +63,10 @@ interface UseTimelineEditorRuntimeParams {
onSelectAudio?: (id: string | null) => void;
captionCues: CaptionCue[];
onCaptionSpanChange?: (id: string, span: Span) => void;
+ onCaptionDelete?: (id: string) => void;
+ onCaptionAdded?: (span: Span) => void;
+ selectedCaptionId?: string | null;
+ onSelectCaption?: (id: string | null) => void;
isMac: boolean;
keyShortcuts: TimelineShortcutBindings;
isTimelineFocusedRef: RefObject<boolean>;
@@ -108,6 +113,10 @@ export function useTimelineEditorRuntime({
onSelectAudio,
captionCues,
onCaptionSpanChange,
+ onCaptionDelete,
+ onCaptionAdded,
+ selectedCaptionId,
+ onSelectCaption,
isMac,
keyShortcuts,
isTimelineFocusedRef,
@@ -127,6 +136,7 @@ export function useTimelineEditorRuntime({
deleteSelectedClip,
deleteSelectedAnnotation,
deleteSelectedAudio,
+ deleteSelectedCaption,
clearSelectedBlocks,
handleSelectZoom,
handleSelectClip,
@@ -144,14 +154,17 @@ export function useTimelineEditorRuntime({
selectedClipId,
selectedAnnotationId,
selectedAudioId,
+ selectedCaptionId,
onZoomDelete,
onClipDelete,
onAnnotationDelete,
onAudioDelete,
+ onCaptionDelete,
onSelectZoom,
onSelectClip,
onSelectAnnotation,
onSelectAudio,
+ onSelectCaption,
});
useTimelineNormalization({
@@ -207,6 +220,12 @@ export function useTimelineEditorRuntime({
onZoomSuggested,
});
+ const { canPlaceCaptionAtMs, addCaptionAtMs } = useTimelineCaptionActions({
+ totalMs,
+ captionRegions: captionCues,
+ onCaptionAdded,
+ });
+
const handleSplitClip = useCallback(() => {
if (!videoDuration || videoDuration === 0 || totalMs === 0 || !onClipSplit) {
return;
@@ -251,6 +270,7 @@ export function useTimelineEditorRuntime({
selectedClipId,
selectedAnnotationId,
selectedAudioId,
+ selectedCaptionId,
selectAllBlocksActive,
addKeyframe,
handleAddZoom,
@@ -261,6 +281,7 @@ export function useTimelineEditorRuntime({
deleteSelectedClip,
deleteSelectedAnnotation,
deleteSelectedAudio,
+ deleteSelectedCaption,
cycleAnnotationsAtCurrentTime,
});
@@ -303,6 +324,8 @@ export function useTimelineEditorRuntime({
handleItemSpanChange,
canPlaceZoomAtMs,
addZoomAtMs,
+ canPlaceCaptionAtMs,
+ addCaptionAtMs,
handleAddZoom,
handleSuggestZooms,
handleSplitClip,
diff --git a/src/components/video-editor/timeline/hooks/useTimelineKeyboardShortcuts.ts b/src/components/video-editor/timeline/hooks/useTimelineKeyboardShortcuts.ts
index ba1f5673..94cb3973 100644
--- a/src/components/video-editor/timeline/hooks/useTimelineKeyboardShortcuts.ts
+++ b/src/components/video-editor/timeline/hooks/useTimelineKeyboardShortcuts.ts
@@ -15,6 +15,7 @@ interface UseTimelineKeyboardShortcutsParams {
selectedClipId?: string | null;
selectedAnnotationId?: string | null;
selectedAudioId?: string | null;
+ selectedCaptionId?: string | null;
selectAllBlocksActive: boolean;
addKeyframe: () => void;
handleAddZoom: () => void;
@@ -25,6 +26,7 @@ interface UseTimelineKeyboardShortcutsParams {
deleteSelectedClip: () => void;
deleteSelectedAnnotation: () => void;
deleteSelectedAudio: () => void;
+ deleteSelectedCaption: () => void;
cycleAnnotationsAtCurrentTime: (backward?: boolean) => boolean;
}
@@ -40,6 +42,7 @@ export function useTimelineKeyboardShortcuts({
selectedClipId,
selectedAnnotationId,
selectedAudioId,
+ selectedCaptionId,
selectAllBlocksActive,
addKeyframe,
handleAddZoom,
@@ -50,6 +53,7 @@ export function useTimelineKeyboardShortcuts({
deleteSelectedClip,
deleteSelectedAnnotation,
deleteSelectedAudio,
+ deleteSelectedCaption,
cycleAnnotationsAtCurrentTime,
}: UseTimelineKeyboardShortcutsParams) {
useEffect(() => {
@@ -102,6 +106,7 @@ export function useTimelineKeyboardShortcuts({
selectedClipId,
selectedAnnotationId,
selectedAudioId,
+ selectedCaptionId,
});
if (target !== "none") {
e.preventDefault();
@@ -116,6 +121,8 @@ export function useTimelineKeyboardShortcuts({
deleteSelectedAnnotation();
} else if (target === "audio") {
deleteSelectedAudio();
+ } else if (target === "caption") {
+ deleteSelectedCaption();
}
}
};
@@ -129,6 +136,7 @@ export function useTimelineKeyboardShortcuts({
cycleAnnotationsAtCurrentTime,
deleteSelectedAnnotation,
deleteSelectedAudio,
+ deleteSelectedCaption,
deleteSelectedClip,
deleteSelectedKeyframe,
deleteSelectedZoom,
@@ -142,6 +150,7 @@ export function useTimelineKeyboardShortcuts({
selectAllBlocksActive,
selectedAnnotationId,
selectedAudioId,
+ selectedCaptionId,
selectedClipId,
selectedKeyframeId,
selectedZoomId,
diff --git a/src/components/video-editor/timeline/hooks/useTimelineSelection.ts b/src/components/video-editor/timeline/hooks/useTimelineSelection.ts
index c7452a69..a0811f96 100644
--- a/src/components/video-editor/timeline/hooks/useTimelineSelection.ts
+++ b/src/components/video-editor/timeline/hooks/useTimelineSelection.ts
@@ -13,14 +13,17 @@ interface UseTimelineSelectionParams {
selectedClipId?: string | null;
selectedAnnotationId?: string | null;
selectedAudioId?: string | null;
+ selectedCaptionId?: string | null;
onZoomDelete: (id: string) => void;
onClipDelete?: (id: string) => void;
onAnnotationDelete?: (id: string) => void;
onAudioDelete?: (id: string) => void;
+ onCaptionDelete?: (id: string) => void;
onSelectZoom: (id: string | null) => void;
onSelectClip?: (id: string | null) => void;
onSelectAnnotation?: (id: string | null) => void;
onSelectAudio?: (id: string | null) => void;
+ onSelectCaption?: (id: string | null) => void;
}
export function useTimelineSelection({
@@ -32,14 +35,17 @@ export function useTimelineSelection({
selectedClipId,
selectedAnnotationId,
selectedAudioId,
+ selectedCaptionId,
onZoomDelete,
onClipDelete,
onAnnotationDelete,
onAudioDelete,
+ onCaptionDelete,
onSelectZoom,
onSelectClip,
onSelectAnnotation,
onSelectAudio,
+ onSelectCaption,
}: UseTimelineSelectionParams) {
const [keyframes, setKeyframes] = useState<{ id: string; time: number }[]>([]);
const [selectedKeyframeId, setSelectedKeyframeId] = useState<string | null>(null);
@@ -113,6 +119,12 @@ export function useTimelineSelection({
onSelectAudio(null);
}, [selectedAudioId, onAudioDelete, onSelectAudio]);
+ const deleteSelectedCaption = useCallback(() => {
+ if (!selectedCaptionId || !onCaptionDelete) return;
+ onCaptionDelete(selectedCaptionId);
+ onSelectCaption?.(null);
+ }, [selectedCaptionId, onCaptionDelete, onSelectCaption]);
+
const clearSelectedBlocks = useCallback(() => {
onSelectZoom(null);
onSelectClip?.(null);
@@ -201,6 +213,7 @@ export function useTimelineSelection({
deleteSelectedClip,
deleteSelectedAnnotation,
deleteSelectedAudio,
+ deleteSelectedCaption,
clearSelectedBlocks,
handleSelectZoom,
handleSelectClip,
diff --git a/src/components/video-editor/timeline/hooks/utils/timelineSelectionUtils.ts b/src/components/video-editor/timeline/hooks/utils/timelineSelectionUtils.ts
index af4cb77d..9e825635 100644
--- a/src/components/video-editor/timeline/hooks/utils/timelineSelectionUtils.ts
+++ b/src/components/video-editor/timeline/hooks/utils/timelineSelectionUtils.ts
@@ -1,4 +1,11 @@
-export type DeleteSelectionTarget = "keyframe" | "zoom" | "clip" | "annotation" | "audio" | "none";
+export type DeleteSelectionTarget =
+ | "keyframe"
+ | "zoom"
+ | "clip"
+ | "annotation"
+ | "audio"
+ | "caption"
+ | "none";
interface ResolveDeleteSelectionTargetParams {
selectAllBlocksActive: boolean;
@@ -7,6 +14,7 @@ interface ResolveDeleteSelectionTargetParams {
selectedClipId?: string | null;
selectedAnnotationId?: string | null;
selectedAudioId?: string | null;
+ selectedCaptionId?: string | null;
}
export function resolveDeleteSelectionTarget({
@@ -16,6 +24,7 @@ export function resolveDeleteSelectionTarget({
selectedClipId,
selectedAnnotationId,
selectedAudioId,
+ selectedCaptionId,
}: ResolveDeleteSelectionTargetParams): DeleteSelectionTarget {
if (selectAllBlocksActive) return "zoom";
if (selectedKeyframeId) return "keyframe";
@@ -23,5 +32,6 @@ export function resolveDeleteSelectionTarget({
if (selectedClipId) return "clip";
if (selectedAnnotationId) return "annotation";
if (selectedAudioId) return "audio";
+ if (selectedCaptionId) return "caption";
return "none";
}