Skip to content

Open source CapCut alternative.

TypeScript

fix(site): custom guide lines undo, and the grab strokes sit under the elements

David Le66a58ca11dd1
diff --git a/site/src/cut/components/Editor.tsx b/site/src/cut/components/Editor.tsx
index 5aec852e..49d85548 100644
--- a/site/src/cut/components/Editor.tsx
+++ b/site/src/cut/components/Editor.tsx
@@ -1046,7 +1046,7 @@ export function Editor({
s.splitAtPlayhead(skimAt() ?? undefined);
} else if (mod && e.key === ";") {
e.preventDefault();
- s.setGuidesHidden(!s.guidesHidden);
+ s.toggleGuidesShown();
} else if (e.key.toLowerCase() === "t" && !e.metaKey && !e.ctrlKey && !e.altKey) {
s.addOverlay();
} else if ((e.key === "ArrowLeft" || e.key === "ArrowRight") && !controlFocused) {
diff --git a/site/src/cut/components/GuideOverlay.tsx b/site/src/cut/components/GuideOverlay.tsx
index 419b0f45..6ef335d7 100644
--- a/site/src/cut/components/GuideOverlay.tsx
+++ b/site/src/cut/components/GuideOverlay.tsx
@@ -3,20 +3,13 @@
import type React from "react";
import { startDrag } from "@/cut/lib/drag";
import { useEditor } from "@/cut/lib/store";
-import { guideGeometry } from "@/cut/lib/guides";
+import { guideGeometry, type GuideGeometry } from "@/cut/lib/guides";
/** A custom line released this close to a frame edge drops. */
const EDGE_DROP = 0.01;
-/**
- * The preview guides: thin lines and shaded keep-out regions drawn over the
- * stage in an SVG sized to it. The layer sits beside the stage in the chrome
- * host, outside the picture, so it never enters the frame loop, a captured
- * frame, or an export. It hides in a shared read-only view and under ⌘;.
- * Custom lines draw in blue and drag; a click on one, or a release at the
- * frame edge, removes it.
- */
-export function GuideOverlay({ stage }: { stage: { w: number; h: number } }) {
+/** The geometry of the guides showing now, or null when nothing draws. */
+function useGuideGeometry(): GuideGeometry | null {
const guides = useEditor((s) => s.guides);
const guideLines = useEditor((s) => s.guideLines);
const hidden = useEditor((s) => s.guidesHidden);
@@ -32,10 +25,24 @@ export function GuideOverlay({ stage }: { stage: { w: number; h: number } }) {
geo.custom.h.length === 0
)
return null;
+ return geo;
+}
+
+/**
+ * The custom lines' grab strokes. They mount inside the stage, under the
+ * elements, so a title beside a line is still the title when clicked; the
+ * picture beside it grabs the line. A drag moves the line; a click on one, or
+ * a release at the frame edge, removes it.
+ */
+export function GuideHandles({ stage }: { stage: { w: number; h: number } }) {
+ const geo = useGuideGeometry();
+ if (!geo) return null;
const { w, h } = stage;
- const label = Math.max(9, Math.min(12, w / 36));
const dragLine = (axis: "v" | "h", index: number, from: number) => (e: React.PointerEvent) => {
+ // Primary button only: startDrag ignores the rest, and the batch has to
+ // close on the same press that opened it.
+ if (e.button !== 0) return;
const size = axis === "v" ? w : h;
const st = useEditor.getState();
st.beginHistoryBatch();
@@ -54,6 +61,59 @@ export function GuideOverlay({ stage }: { stage: { w: number; h: number } }) {
});
};
+ return (
+ <svg
+ className="pointer-events-none absolute inset-0"
+ width={w}
+ height={h}
+ viewBox={`0 0 ${w} ${h}`}
+ aria-hidden
+ >
+ {geo.custom.v.map((f, i) => (
+ <line
+ key={`cv-${i}`}
+ x1={f * w}
+ y1={0}
+ x2={f * w}
+ y2={h}
+ stroke="transparent"
+ strokeWidth="12"
+ className="cursor-col-resize"
+ style={{ pointerEvents: "stroke" }}
+ onPointerDown={dragLine("v", i, f)}
+ />
+ ))}
+ {geo.custom.h.map((f, i) => (
+ <line
+ key={`ch-${i}`}
+ x1={0}
+ y1={f * h}
+ x2={w}
+ y2={f * h}
+ stroke="transparent"
+ strokeWidth="12"
+ className="cursor-row-resize"
+ style={{ pointerEvents: "stroke" }}
+ onPointerDown={dragLine("h", i, f)}
+ />
+ ))}
+ </svg>
+ );
+}
+
+/**
+ * The preview guides: thin lines and shaded keep-out regions drawn over the
+ * stage in an SVG sized to it. The layer sits beside the stage in the chrome
+ * host, outside the picture, so it never enters the frame loop, a captured
+ * frame, or an export. It hides in a shared read-only view and under ⌘;.
+ * Custom lines draw in blue; GuideHandles, inside the stage, moves them.
+ */
+export function GuideOverlay({ stage }: { stage: { w: number; h: number } }) {
+ const geo = useGuideGeometry();
+ if (!geo) return null;
+ const { w, h } = stage;
+ const label = Math.max(9, Math.min(12, w / 36));
+
return (
<svg
className="pointer-events-none absolute inset-0 overflow-visible"
@@ -123,32 +183,12 @@ export function GuideOverlay({ stage }: { stage: { w: number; h: number } }) {
<g key={`cv-${i}`}>
<line x1={f * w} y1={0} x2={f * w} y2={h} stroke="rgba(0,0,0,0.45)" strokeWidth="3" />
<line x1={f * w} y1={0} x2={f * w} y2={h} stroke="rgb(96,165,250)" strokeWidth="1" />
- <line
- x1={f * w}
- y1={0}
- x2={f * w}
- y2={h}
- stroke="transparent"
- strokeWidth="12"
- className="cursor-col-resize" style={{ pointerEvents: "stroke" }}
- onPointerDown={dragLine("v", i, f)}
- />
</g>
))}
{geo.custom.h.map((f, i) => (
<g key={`ch-${i}`}>
<line x1={0} y1={f * h} x2={w} y2={f * h} stroke="rgba(0,0,0,0.45)" strokeWidth="3" />
<line x1={0} y1={f * h} x2={w} y2={f * h} stroke="rgb(96,165,250)" strokeWidth="1" />
- <line
- x1={0}
- y1={f * h}
- x2={w}
- y2={f * h}
- stroke="transparent"
- strokeWidth="12"
- className="cursor-row-resize" style={{ pointerEvents: "stroke" }}
- onPointerDown={dragLine("h", i, f)}
- />
</g>
))}
</svg>
diff --git a/site/src/cut/components/GuidesMenu.tsx b/site/src/cut/components/GuidesMenu.tsx
index 63afa1ad..766ac908 100644
--- a/site/src/cut/components/GuidesMenu.tsx
+++ b/site/src/cut/components/GuidesMenu.tsx
@@ -25,11 +25,7 @@ export function GuidesMenu() {
const guides = useEditor((s) => s.guides);
const guidesHidden = useEditor((s) => s.guidesHidden);
const showing = guides.length > 0 && !guidesHidden;
- const toggle = () => {
- const s = useEditor.getState();
- if (s.guides.length === 0) s.toggleGuide("margins");
- else s.setGuidesHidden(!s.guidesHidden);
- };
+ const toggle = () => useEditor.getState().toggleGuidesShown();
return (
<div
className={cn(
diff --git a/site/src/cut/components/Preview.tsx b/site/src/cut/components/Preview.tsx
index 2bf3b409..9608fc8a 100644
--- a/site/src/cut/components/Preview.tsx
+++ b/site/src/cut/components/Preview.tsx
@@ -24,7 +24,7 @@ import { CLIP_MAX_ZOOM, clipCovers, clipKeyed, clipPoseAt, clipZoom, contentRect
import { hasMaskKeys, type MaskKey } from "@donkeycut/effects-kit";
import { cn } from "@/lib/utils";
import { MaskGizmoCore, OverlayChromeHost, OverlayLayer, StagePress } from "./OverlayLayer";
-import { GuideOverlay } from "./GuideOverlay";
+import { GuideHandles, GuideOverlay } from "./GuideOverlay";
import { RemovalBrush } from "./RemovalBrush";
import { CORNER_HANDLES, HANDLE_AXIS, TransformHandles, type ResizeHandle } from "./TransformHandles";
import {
@@ -608,6 +608,7 @@ export function Preview() {
</StagePictureFx>
<BufferingBadge />
<OriginalPeekBadge />
+ <GuideHandles stage={stage} />
<StagePress.Provider value={stagePress}>
{slices.map((slice) =>
slice.kind === "elements" ? (
diff --git a/site/src/cut/lib/store.ts b/site/src/cut/lib/store.ts
index 97417a9e..5cdaa24a 100644
--- a/site/src/cut/lib/store.ts
+++ b/site/src/cut/lib/store.ts
@@ -278,6 +278,9 @@ interface DocSnapshot {
audioClips: AudioClip[];
overlays: Overlay[];
subtitles: SubtitlesBlock;
+ /** The custom guide lines: placed, dragged and removed on the preview, so
+ * undo brings one back. */
+ guideLines: GuideLines;
/** Each asset's beat grid. Beats are edited on the timeline like anything
* else — dragged, added, removed, cleared — but they live on the asset
* rather than in the doc arrays, so the checkpoint carries them separately
@@ -426,6 +429,9 @@ export interface EditorState {
setGuides: (ids: GuideId[]) => void;
toggleGuide: (id: GuideId) => void;
setGuidesHidden: (hidden: boolean) => void;
+ /** The guides button and ⌘;: hide or show what is chosen; with nothing
+ * chosen, turn on the safe margins. */
+ toggleGuidesShown: () => void;
setGuideLines: (lines: GuideLines) => void;
/** Add a custom line at the frame center (stepping aside from one already
* there), turning the custom set on and the guides visible. */
@@ -1516,9 +1522,10 @@ export const useEditor = create<EditorState>((baseSet, get, api) => {
api.setState = set as typeof api.setState;
const snapshot = (): DocSnapshot => {
- const { clips, transitions, audioClips, overlays, subtitles, assets } = get();
+ const { clips, transitions, audioClips, overlays, subtitles, assets, guideLines } = get();
return {
beats: assets.map((a) => ({ id: a.id, grid: a.beats })),
+ guideLines: { v: [...guideLines.v], h: [...guideLines.h] },
// Render-owned clips are excluded — history captures the user's timeline,
// not the background run's placements (restoreDoc re-attaches the live ones).
clips: clips.filter((c) => !genClipIds.has(c.id)).map((c) => ({ ...c })),
@@ -1713,6 +1720,7 @@ export const useEditor = create<EditorState>((baseSet, get, api) => {
aspect: lastChosenAspect() ?? "9:16",
aspectTouched: lastChosenAspect() !== null,
guides: [],
+ guidesHidden: false,
guideLines: EMPTY_GUIDE_LINES,
fadeIn: 0,
fadeOut: 0,
@@ -2136,6 +2144,7 @@ export const useEditor = create<EditorState>((baseSet, get, api) => {
aspect: normalizeAspect(doc.aspect) ?? lastChosenAspect() ?? "9:16",
aspectTouched: doc.aspect !== undefined || lastChosenAspect() !== null,
guides: sanitizeGuides(doc.guides),
+ guidesHidden: false,
guideLines: sanitizeGuideLines(doc.guideLines),
fadeIn: doc.fadeIn ?? 0,
fadeOut: doc.fadeOut ?? 0,
@@ -2361,8 +2370,18 @@ export const useEditor = create<EditorState>((baseSet, get, api) => {
guidesHidden: s.guides.includes(id) ? s.guidesHidden : false,
})),
setGuidesHidden: (hidden) => set({ guidesHidden: hidden }),
- setGuideLines: (lines) => set({ guideLines: sanitizeGuideLines(lines) }),
- addGuideLine: (axis) =>
+ toggleGuidesShown: () =>
+ set((s) =>
+ s.guides.length === 0
+ ? { guides: ["margins"], guidesHidden: false }
+ : { guidesHidden: !s.guidesHidden }
+ ),
+ setGuideLines: (lines) => {
+ push();
+ set({ guideLines: sanitizeGuideLines(lines) });
+ },
+ addGuideLine: (axis) => {
+ push();
set((s) => {
const have = s.guideLines[axis];
let at = 0.5;
@@ -2372,18 +2391,23 @@ export const useEditor = create<EditorState>((baseSet, get, api) => {
guides: s.guides.includes("custom") ? s.guides : sanitizeGuides([...s.guides, "custom"]),
guidesHidden: false,
};
- }),
- moveGuideLine: (axis, index, at) =>
+ });
+ },
+ moveGuideLine: (axis, index, at) => {
+ push();
set((s) => {
if (index < 0 || index >= s.guideLines[axis].length) return {};
const next = [...s.guideLines[axis]];
next[index] = Math.min(1, Math.max(0, at));
return { guideLines: { ...s.guideLines, [axis]: next } };
- }),
- removeGuideLine: (axis, index) =>
+ });
+ },
+ removeGuideLine: (axis, index) => {
+ push();
set((s) => ({
guideLines: { ...s.guideLines, [axis]: s.guideLines[axis].filter((_, i) => i !== index) },
- })),
+ }));
+ },
setProjectFade: (patch) => {
const clamp = (v: number | undefined) =>
v === undefined ? undefined : Math.max(0, Math.min(TRANSITION_MAX, v));
@@ -5180,6 +5204,7 @@ useEditor.subscribe((s, prev) => {
s.audioClips !== prev.audioClips ||
s.overlays !== prev.overlays ||
s.subtitles !== prev.subtitles ||
+ s.guideLines !== prev.guideLines ||
(s.assets !== prev.assets && beatsEdited(prev.assets, s.assets))
)
docSeq++;