Skip to content

OpenReel Video - Professional browser-based video editor. Open source CapCut alternative. 100% browser-based, no installation, no cloud uploads, no watermarks.

TypeScript

feat: playhead snap-to-edges and visible trim handles on selected clips

Augustus Otu1647d12384e9
diff --git a/apps/web/src/components/editor/Timeline.tsx b/apps/web/src/components/editor/Timeline.tsx
index 26d1db79..2b66f4e7 100644
--- a/apps/web/src/components/editor/Timeline.tsx
+++ b/apps/web/src/components/editor/Timeline.tsx
@@ -150,6 +150,17 @@ export const Timeline: React.FC = () => {
return Math.max(maxEnd, 60); // Minimum 60 seconds
}, [tracks]);
+ const playheadSnapPoints = useMemo(() => {
+ const points = new Set<number>();
+ for (const track of tracks) {
+ for (const clip of track.clips) {
+ points.add(clip.startTime);
+ points.add(clip.startTime + clip.duration);
+ }
+ }
+ return Array.from(points).sort((a, b) => a - b);
+ }, [tracks]);
+
const totalTracksHeight = useMemo(() => {
let height = 0;
for (const track of tracks) {
@@ -934,6 +945,7 @@ export const Timeline: React.FC = () => {
pixelsPerSecond={pixelsPerSecond}
scrollX={scrollX}
viewportWidth={viewportWidth}
+ snapPoints={playheadSnapPoints}
onSeek={(time) => {
const bridge = getPlaybackBridge();
bridge.scrubTo(time);
diff --git a/apps/web/src/components/editor/timeline/ClipComponent.tsx b/apps/web/src/components/editor/timeline/ClipComponent.tsx
index b3235a30..a6f0859b 100644
--- a/apps/web/src/components/editor/timeline/ClipComponent.tsx
+++ b/apps/web/src/components/editor/timeline/ClipComponent.tsx
@@ -501,27 +501,35 @@ export const ClipComponent: React.FC<ClipComponentProps> = ({
)}
{isSelected && (
- <div className="absolute inset-0 border-2 border-primary rounded-lg pointer-events-none shadow-[inset_0_0_10px_rgba(34,197,94,0.2)]" />
+ <div className="absolute inset-0 border-2 border-primary rounded-lg pointer-events-none" />
)}
{(isVideo || isImage || isAudio) && onTrimClip && (
<>
<div
onMouseDown={handleTrimMouseDown("left")}
- className={`absolute left-0 top-0 bottom-0 w-3 cursor-ew-resize z-20 opacity-0 group-hover:opacity-100 transition-opacity ${
- isAudio ? "hover:bg-blue-400/50" : isVideo ? "hover:bg-green-400/50" : "hover:bg-purple-400/50"
- }`}
+ className={`absolute left-0 top-0 bottom-0 w-2 cursor-ew-resize z-20 flex items-center justify-center transition-opacity ${
+ isSelected ? "opacity-100" : "opacity-0 group-hover:opacity-100"
+ } ${isSelected ? "bg-primary" : isAudio ? "hover:bg-blue-400/50" : isVideo ? "hover:bg-green-400/50" : "hover:bg-purple-400/50"}`}
+ style={{ borderRadius: "6px 0 0 6px" }}
onClick={(e) => e.stopPropagation()}
- title="Drag to adjust start"
- />
+ >
+ {isSelected && (
+ <div className="w-0.5 h-3 bg-primary-foreground/80 rounded-full" />
+ )}
+ </div>
<div
onMouseDown={handleTrimMouseDown("right")}
- className={`absolute right-0 top-0 bottom-0 w-3 cursor-ew-resize z-20 opacity-0 group-hover:opacity-100 transition-opacity ${
- isAudio ? "hover:bg-blue-400/50" : isVideo ? "hover:bg-green-400/50" : "hover:bg-purple-400/50"
- }`}
+ className={`absolute right-0 top-0 bottom-0 w-2 cursor-ew-resize z-20 flex items-center justify-center transition-opacity ${
+ isSelected ? "opacity-100" : "opacity-0 group-hover:opacity-100"
+ } ${isSelected ? "bg-primary" : isAudio ? "hover:bg-blue-400/50" : isVideo ? "hover:bg-green-400/50" : "hover:bg-purple-400/50"}`}
+ style={{ borderRadius: "0 6px 6px 0" }}
onClick={(e) => e.stopPropagation()}
- title="Drag to adjust end"
- />
+ >
+ {isSelected && (
+ <div className="w-0.5 h-3 bg-primary-foreground/80 rounded-full" />
+ )}
+ </div>
</>
)}
diff --git a/apps/web/src/components/editor/timeline/ShapeClipComponent.tsx b/apps/web/src/components/editor/timeline/ShapeClipComponent.tsx
index 86f9e690..4b723b87 100644
--- a/apps/web/src/components/editor/timeline/ShapeClipComponent.tsx
+++ b/apps/web/src/components/editor/timeline/ShapeClipComponent.tsx
@@ -198,15 +198,23 @@ export const ShapeClipComponent: React.FC<ShapeClipComponentProps> = ({
}}
>
<div
- className={`absolute left-0 top-0 bottom-0 w-2 cursor-ew-resize hover:bg-${colorClass}-400/50 z-20 opacity-0 group-hover:opacity-100 transition-opacity`}
+ className={`absolute left-0 top-0 bottom-0 w-2 cursor-ew-resize z-20 flex items-center justify-center transition-opacity ${
+ isSelected ? "opacity-100 bg-green-400" : `opacity-0 group-hover:opacity-100 hover:bg-${colorClass}-400/50`
+ }`}
+ style={{ borderRadius: "6px 0 0 6px" }}
onMouseDown={(e) => handleTrimStart(e, "left")}
- title="Drag to trim start"
- />
+ >
+ {isSelected && <div className="w-0.5 h-3 bg-green-900/60 rounded-full" />}
+ </div>
<div
- className={`absolute right-0 top-0 bottom-0 w-2 cursor-ew-resize hover:bg-${colorClass}-400/50 z-20 opacity-0 group-hover:opacity-100 transition-opacity`}
+ className={`absolute right-0 top-0 bottom-0 w-2 cursor-ew-resize z-20 flex items-center justify-center transition-opacity ${
+ isSelected ? "opacity-100 bg-green-400" : `opacity-0 group-hover:opacity-100 hover:bg-${colorClass}-400/50`
+ }`}
+ style={{ borderRadius: "0 6px 6px 0" }}
onMouseDown={(e) => handleTrimStart(e, "right")}
- title="Drag to trim end"
- />
+ >
+ {isSelected && <div className="w-0.5 h-3 bg-green-900/60 rounded-full" />}
+ </div>
<div className="w-full h-full flex items-center gap-1 px-3">
<IconComponent
size={12}
@@ -219,17 +227,7 @@ export const ShapeClipComponent: React.FC<ShapeClipComponentProps> = ({
</span>
</div>
{isSelected && (
- <>
- <div className="absolute inset-0 border-2 border-green-400 rounded-lg pointer-events-none" />
- <div
- className="absolute left-0 top-1/2 -translate-y-1/2 w-1.5 h-6 bg-green-400 rounded-r cursor-ew-resize"
- onMouseDown={(e) => handleTrimStart(e, "left")}
- />
- <div
- className="absolute right-0 top-1/2 -translate-y-1/2 w-1.5 h-6 bg-green-400 rounded-l cursor-ew-resize"
- onMouseDown={(e) => handleTrimStart(e, "right")}
- />
- </>
+ <div className="absolute inset-0 border-2 border-green-400 rounded-lg pointer-events-none" />
)}
</div>
</ContextMenuTrigger>
diff --git a/apps/web/src/components/editor/timeline/TextClipComponent.tsx b/apps/web/src/components/editor/timeline/TextClipComponent.tsx
index af1f5ec9..bced8d24 100644
--- a/apps/web/src/components/editor/timeline/TextClipComponent.tsx
+++ b/apps/web/src/components/editor/timeline/TextClipComponent.tsx
@@ -183,15 +183,23 @@ export const TextClipComponent: React.FC<TextClipComponentProps> = ({
}}
>
<div
- className="absolute left-0 top-0 bottom-0 w-2 cursor-ew-resize hover:bg-amber-400/50 z-20 opacity-0 group-hover:opacity-100 transition-opacity"
+ className={`absolute left-0 top-0 bottom-0 w-2 cursor-ew-resize z-20 flex items-center justify-center transition-opacity ${
+ isSelected ? "opacity-100 bg-amber-400" : "opacity-0 group-hover:opacity-100 hover:bg-amber-400/50"
+ }`}
+ style={{ borderRadius: "6px 0 0 6px" }}
onMouseDown={(e) => handleTrimStart(e, "left")}
- title="Drag to trim start"
- />
+ >
+ {isSelected && <div className="w-0.5 h-3 bg-amber-900/60 rounded-full" />}
+ </div>
<div
- className="absolute right-0 top-0 bottom-0 w-2 cursor-ew-resize hover:bg-amber-400/50 z-20 opacity-0 group-hover:opacity-100 transition-opacity"
+ className={`absolute right-0 top-0 bottom-0 w-2 cursor-ew-resize z-20 flex items-center justify-center transition-opacity ${
+ isSelected ? "opacity-100 bg-amber-400" : "opacity-0 group-hover:opacity-100 hover:bg-amber-400/50"
+ }`}
+ style={{ borderRadius: "0 6px 6px 0" }}
onMouseDown={(e) => handleTrimStart(e, "right")}
- title="Drag to trim end"
- />
+ >
+ {isSelected && <div className="w-0.5 h-3 bg-amber-900/60 rounded-full" />}
+ </div>
<div className="w-full h-full flex items-center gap-1 px-3">
<Type size={12} className="text-amber-400 flex-shrink-0" />
<span className="text-[10px] font-medium text-amber-200 truncate">
@@ -199,17 +207,7 @@ export const TextClipComponent: React.FC<TextClipComponentProps> = ({
</span>
</div>
{isSelected && (
- <>
- <div className="absolute inset-0 border-2 border-amber-400 rounded-lg pointer-events-none" />
- <div
- className="absolute left-0 top-1/2 -translate-y-1/2 w-1.5 h-6 bg-amber-400 rounded-r cursor-ew-resize"
- onMouseDown={(e) => handleTrimStart(e, "left")}
- />
- <div
- className="absolute right-0 top-1/2 -translate-y-1/2 w-1.5 h-6 bg-amber-400 rounded-l cursor-ew-resize"
- onMouseDown={(e) => handleTrimStart(e, "right")}
- />
- </>
+ <div className="absolute inset-0 border-2 border-amber-400 rounded-lg pointer-events-none" />
)}
</div>
</ContextMenuTrigger>
diff --git a/apps/web/src/components/editor/timeline/TimeRuler.tsx b/apps/web/src/components/editor/timeline/TimeRuler.tsx
index 23a89889..1fc5ade7 100644
--- a/apps/web/src/components/editor/timeline/TimeRuler.tsx
+++ b/apps/web/src/components/editor/timeline/TimeRuler.tsx
@@ -19,6 +19,7 @@ interface TimeRulerProps {
onSeek: (time: number) => void;
onScrubStart?: () => void;
onScrubEnd?: () => void;
+ snapPoints?: number[];
}
export const TimeRuler: React.FC<TimeRulerProps> = ({
@@ -28,6 +29,7 @@ export const TimeRuler: React.FC<TimeRulerProps> = ({
onSeek,
onScrubStart,
onScrubEnd,
+ snapPoints,
}) => {
const rulerRef = useRef<HTMLDivElement>(null);
const [isDragging, setIsDragging] = useState(false);
@@ -120,14 +122,56 @@ export const TimeRuler: React.FC<TimeRulerProps> = ({
[getTimeFromEvent, onSeek, onScrubStart],
);
+ const snapPointsRef = useRef(snapPoints);
+ snapPointsRef.current = snapPoints;
+
useEffect(() => {
if (!isDragging) return;
let rafId: number | null = null;
let latestTime: number | null = null;
+ let prevTime: number | null = null;
+ let prevTimestamp = 0;
+ let velocity = 0;
+
+ const SNAP_THRESHOLD_PX = 8;
+ const SLOW_VELOCITY_THRESHOLD = 150;
+
+ const applySnap = (rawTime: number): number => {
+ const points = snapPointsRef.current;
+ if (!points || points.length === 0) return rawTime;
+
+ if (velocity > SLOW_VELOCITY_THRESHOLD) return rawTime;
+
+ const thresholdSec = SNAP_THRESHOLD_PX / safePixelsPerSecond;
+ let bestDist = Infinity;
+ let snapped = rawTime;
+
+ for (const point of points) {
+ const dist = Math.abs(rawTime - point);
+ if (dist < thresholdSec && dist < bestDist) {
+ bestDist = dist;
+ snapped = point;
+ }
+ }
+
+ return snapped;
+ };
const handleMouseMove = (e: MouseEvent) => {
e.preventDefault();
- latestTime = getTimeFromEvent(e);
+ const rawTime = getTimeFromEvent(e);
+ const now = performance.now();
+
+ if (prevTime !== null && now - prevTimestamp > 0) {
+ const dt = (now - prevTimestamp) / 1000;
+ const pixelDelta = Math.abs(rawTime - prevTime) * safePixelsPerSecond;
+ velocity = pixelDelta / dt;
+ }
+ prevTime = rawTime;
+ prevTimestamp = now;
+
+ latestTime = applySnap(rawTime);
+
if (rafId === null) {
rafId = requestAnimationFrame(() => {
rafId = null;
@@ -154,7 +198,7 @@ export const TimeRuler: React.FC<TimeRulerProps> = ({
window.removeEventListener("mousemove", handleMouseMove);
window.removeEventListener("mouseup", handleMouseUp);
};
- }, [isDragging, getTimeFromEvent, onSeek, onScrubEnd]);
+ }, [isDragging, getTimeFromEvent, onSeek, onScrubEnd, safePixelsPerSecond]);
return (
<div