Skip to content

Open source CapCut alternative.

TypeScript

fix(site): an unselected element hands its drag to the picture

David Le4a380a8b53f1
diff --git a/docs/guides/cut/README.md b/docs/guides/cut/README.md
index a0ddd14e..bbadf95e 100644
--- a/docs/guides/cut/README.md
+++ b/docs/guides/cut/README.md
@@ -58,7 +58,7 @@ Two things follow from that. A cut is a read from a different ring rather than a
What is on screen at a given instant is still a pure function of the document and a time, and it is still the same function an export asks. The preview reaches an instant by playing toward it and an export by jumping straight there; because both draw through one compositor from one plan, the file cannot quietly disagree with what was watched.
-The stage sits under a camera. The picture opens fitted to its pane, and that fit is 100%: from there a pinch or ⌘-scroll zooms about the cursor, a plain scroll pans, and a drag on the empty room around the picture — or a middle-button drag anywhere — moves the view. The camera holds its place on screen when a panel opens or the selection changes shape; the pan absorbs the layout's move, so the picture stays where the eye left it. A round control in the pane's top-right corner appears while the camera moves and lingers a moment after, opening into the zoom level, a step in each direction, and a Reset that refits.
+The stage sits under a camera. The picture opens fitted to its pane, and that fit is 100%: from there a pinch or ⌘-scroll zooms about the cursor, a plain scroll pans, and a drag on the empty room around the picture — or a middle-button drag anywhere — moves the view. A drag on the picture belongs to the picture, whatever is drawn over it: a title, a shape, a sticker, a caption all hand the gesture down to whatever the footage itself would do with it — move the view, or slide the selected clip's crop. Selection is what arms something to be moved; a press that never travels selects what it landed on, and the drag after that moves it. The camera holds its place on screen when a panel opens or the selection changes shape; the pan absorbs the layout's move, so the picture stays where the eye left it. A round control in the pane's top-right corner appears while the camera moves and lingers a moment after, opening into the zoom level, a step in each direction, and a Reset that refits.
The playhead lives apart from the document. It moves sixty times a second and the document does not, so a moved playhead wakes the surfaces that draw it and nothing else — and a paused editor with nothing changing schedules no frames at all.
diff --git a/site/src/cut/components/OverlayLayer.tsx b/site/src/cut/components/OverlayLayer.tsx
index a66a3120..9b2c9e71 100644
--- a/site/src/cut/components/OverlayLayer.tsx
+++ b/site/src/cut/components/OverlayLayer.tsx
@@ -61,6 +61,15 @@ const PLATE_RADIUS_EM = `${PLATE_RADIUS}em`;
* and grabbable, the way the video clip gizmo already does. */
export const OverlayChromeHost = createContext<HTMLElement | null>(null);
+/** How the stage takes a press that started on something the user has not
+ * selected. Selection comes first: an unselected title, shape, sticker or
+ * caption is part of the picture, so pressing one pans the picture the way
+ * pressing bare footage does, and a press that never travels selects it.
+ * Grabbing to move is what a selected element does. */
+export const StagePress = createContext<
+ ((e: React.PointerEvent, onTap: () => void) => void) | null
+>(null);
+
/**
* The rotate handle's cursor. CSS has no rotate cursor, so the affordance is
* drawn here — and because the glyph is an arc with a head at each end rather
@@ -296,6 +305,9 @@ export function OverlayLayer({
// handles or drag. The timeline bar is the thing you grab.
if (o.kind === "effect") return null;
const selected = sel?.id === o.id;
+ // A group drags as a unit, so any member is grabbable once the
+ // selection is anywhere inside it.
+ const inGroup = !!sel?.groupId && sel.groupId === o.groupId;
const inRange = t >= o.start && t <= o.end;
// While hover-scrubbing (paused, skimmer active) the preview must show the
// exact frame under the skimmer — a selected but out-of-frame title can't
@@ -309,6 +321,7 @@ export function OverlayLayer({
// The skimmer paints the bare frame: the item still renders, its
// selection chrome (outline, resize handle) does not.
selected={selected && !scrubbing}
+ armed={(selected || inGroup) && !scrubbing}
ghost={!inRange && !selected}
t={t}
stageWidth={stageWidth}
@@ -425,11 +438,16 @@ function SubtitleCaption({
}) {
const subtitles = useEditor((s) => s.subtitles);
const frame = frameOf(useEditor((s) => s.aspect));
+ const selection = useEditor((s) => s.selection);
+ const stagePress = useContext(StagePress);
const t = usePreviewTime();
const cues = laneCues(subtitles, lane);
const cue = cueAt(cues, t);
if (!cue || !cue.text.trim()) return null;
+ // The caption moves its whole track, so it takes the same rule the elements
+ // take: the cue on screen has to be the selection before a drag moves it.
+ const armed = selection?.kind === "cue" && selection.id === cue.id;
// Captions ride the same style/opener/anchor logic as the export burn-in,
// so the preview and the rendered file match exactly.
@@ -448,9 +466,16 @@ function SubtitleCaption({
return (
<div
ref={(el) => registerBox(subtitleBoxId(lane), el)}
- className="sub-caption pointer-events-auto absolute -translate-x-1/2 -translate-y-1/2 cursor-grab text-center whitespace-pre-wrap active:cursor-grabbing"
+ className={cn(
+ "sub-caption pointer-events-auto absolute -translate-x-1/2 -translate-y-1/2 cursor-grab text-center whitespace-pre-wrap active:cursor-grabbing",
+ armed && "outline-[1.5px] outline-offset-[3px] outline-[#0a84ff]"
+ )}
onPointerDown={(e) => {
const s = useEditor.getState();
+ if (!armed && stagePress) {
+ stagePress(e, () => s.select({ kind: "cue", id: cue.id }));
+ return;
+ }
s.pushHistory();
const { x: x0, y: y0 } = ov;
startDrag(e, {
@@ -491,6 +516,7 @@ function SubtitleCaption({
function OverlayItem({
overlay: o,
selected,
+ armed,
ghost,
t,
stageWidth,
@@ -502,6 +528,9 @@ function OverlayItem({
}: {
overlay: Overlay;
selected: boolean;
+ /** Whether a press on this element moves it. Only the selection (or the
+ * group holding it) drags; everything else hands the press to the stage. */
+ armed: boolean;
ghost: boolean;
/** The previewed timeline moment (playhead or paused skimmer). */
t: number;
@@ -624,6 +653,7 @@ function OverlayItem({
// The twin copies the box's placement and transform chain; its laid-out
// size is read off the real box, which keeps auto-sized text honest.
const chromeHost = useContext(OverlayChromeHost);
+ const stagePress = useContext(StagePress);
const liftChrome = !!chromeHost && selected && !editing;
const [chromeSize, setChromeSize] = useState<{ w: number; h: number } | null>(null);
useLayoutEffect(() => {
@@ -960,6 +990,13 @@ function OverlayItem({
const beginMove = (e: React.PointerEvent) => {
if (editing) return;
const s = useEditor.getState();
+ // Nothing moves until it is the selection: an unselected element is part
+ // of the picture, so the stage pans under it and a stationary press picks
+ // it up for the next drag.
+ if (!armed && stagePress) {
+ stagePress(e, () => s.select({ kind: "overlay", id: o.id }));
+ return;
+ }
s.select({ kind: "overlay", id: o.id });
s.pushHistory();
// A grouped element drags its whole group: the grabbed one snaps, the
diff --git a/site/src/cut/components/Preview.tsx b/site/src/cut/components/Preview.tsx
index c5fc3bd6..74e90bdf 100644
--- a/site/src/cut/components/Preview.tsx
+++ b/site/src/cut/components/Preview.tsx
@@ -22,7 +22,7 @@ import { resizePreviewSurface, setPreviewCanvas } from "@/cut/lib/previewCanvas"
import { CLIP_MAX_ZOOM, clipCovers, clipKeyed, clipPoseAt, clipZoom, contentRect, frameOf, isFullRect, rectOf, REGION_MAX_SCALE, type Aspect, type ClipSpan, type FrameRect, type MediaAsset, type VideoClip } from "@/cut/lib/types";
import { hasMaskKeys, type MaskKey } from "@donkeycut/effects-kit";
import { cn } from "@/lib/utils";
-import { MaskGizmoCore, OverlayChromeHost, OverlayLayer } from "./OverlayLayer";
+import { MaskGizmoCore, OverlayChromeHost, OverlayLayer, StagePress } from "./OverlayLayer";
import { CORNER_HANDLES, HANDLE_AXIS, TransformHandles, type ResizeHandle } from "./TransformHandles";
import {
StageEffectPaint,
@@ -489,7 +489,7 @@ export function Preview() {
// Drag a selected fill-mode clip inside the frame to choose the visible crop.
// The clip has to be the selection: an unselected picture is something to
// grab and move around the pane, and that press goes to the camera.
- const panDrag = (e: React.PointerEvent) => {
+ const panDrag = (e: React.PointerEvent, onTap: () => void = stageClick) => {
const s = useEditor.getState();
const span = pannableSpan(s, previewAt());
if (!span || s.selection?.kind !== "clip" || s.selection.id !== span.clip.id) return false;
@@ -530,12 +530,20 @@ export function Preview() {
// first, play or pause once nothing is selected.
onUp: (_dx, _dy, moved) => {
setCropCentered((c) => (c.x || c.y ? { x: false, y: false } : c));
- if (!moved) stageClick();
+ if (!moved) onTap();
},
});
return true;
};
+ /** A press that landed on something drawn over the picture the user has not
+ * selected — a title, a caption. It belongs to the picture: the drag pans,
+ * exactly as it would on bare footage, and a press that never travels runs
+ * `onTap`, which is where the thing under the pointer gets selected. */
+ const stagePress = (e: React.PointerEvent, onTap: () => void) => {
+ if (!panDrag(e, onTap)) cameraDrag(e, onTap);
+ };
+
// The topmost regioned clip under a stage point at the playhead — clicking
// its picture selects it, in the preview and the timeline alike. Full-frame
// clips stay out: a click on the backdrop clears the selection.
@@ -651,21 +659,23 @@ export function Preview() {
className="block size-full"
/>
</StagePictureFx>
- {slices.map((slice) =>
- slice.kind === "elements" ? (
- <OverlayChromeHost.Provider key={slice.key} value={chromeHost}>
- <OverlayLayer
- stageWidth={stage.w}
- gradeAbove={slice.gradeAbove}
- from={slice.from}
- to={slice.to}
- captions={slice.captions}
- />
- </OverlayChromeHost.Provider>
- ) : (
- <StageEffectPaint key={slice.key} lane={slice.lane} />
- )
- )}
+ <StagePress.Provider value={stagePress}>
+ {slices.map((slice) =>
+ slice.kind === "elements" ? (
+ <OverlayChromeHost.Provider key={slice.key} value={chromeHost}>
+ <OverlayLayer
+ stageWidth={stage.w}
+ gradeAbove={slice.gradeAbove}
+ from={slice.from}
+ to={slice.to}
+ captions={slice.captions}
+ />
+ </OverlayChromeHost.Provider>
+ ) : (
+ <StageEffectPaint key={slice.key} lane={slice.lane} />
+ )
+ )}
+ </StagePress.Provider>
</div>
{/* Chrome layers sit beside the stage, outside its clipping: the
element chrome host, the clip's mask gizmo, and the clip gizmo.