Skip to content

Open source CapCut alternative.

TypeScript
21.2 KB
Raw Blame
"use client";

import { useEffect, useMemo, useRef, useState } from "react";
import { useTranslations } from "next-intl";
import { PHASE_STEP } from "@/cut/components/AnimationTiles";
import { SwatchScene, useSwatchClock } from "@/cut/components/EffectsPanel";
import { SectionTitle } from "@/cut/components/SectionTitle";
import { Tile } from "@/cut/components/PanelTile";
import { clearElementDrag, setElementDragData, setObjectDragImage } from "@/cut/lib/assetDrag";
import { peekEdgeFrame, requestEdgeFrame } from "@/cut/lib/media";
import { PICKED_RING, pickGridNav, useAssetPick } from "@/cut/lib/assetPick";
import { getClipSpans, resolveTransitions, useEditor } from "@/cut/lib/store";
import { usePreviewTimeEvery } from "@/cut/lib/playhead";
import {
  ANIM_STYLE_IDS,
  TRANSITION_STYLE_GROUPS,
  overlayAnimStyle,
  previewUrl,
  transitionStyleOfAnim,
  type AnimStyle,
  type MediaAsset,
  type TimelineTransition,
  type TransitionStyle,
  type VideoClip,
} from "@/cut/lib/types";
import {
  CLOCK_SWEEP,
  shapeEase,
  takesBand,
  takesFeather,
  TRANSITION_BAND_MAX,
  TRANSITION_FEATHER_MAX,
} from "@/cut/lib/transitionShape";
import { Row, useSliderCheckpoint } from "@/cut/components/panelBits";
import { parseNumberInput } from "@/cut/components/ScrubValue";
import { ValueSlider } from "@/cut/components/ValueSlider";
import { cn } from "@/lib/utils";
import { ScrollArea } from "@/components/ui/scroll-area";
import { useLabels } from "@/i18n/labels";

/**
 * The Transitions tab: how one clip hands over to the next, and how a clip
 * enters and leaves on its own.
 *
 * Drag a tile onto the timeline and it takes the nearest place with a handover
 * to make: a cut between two clips, or a clip's own head or tail, where it
 * arrives from or leaves to nothing. On the timeline it is a bar of its own
 * above the tracks: drag it along the track to move it, drag its edge to
 * retime it. A click here only picks the tile, the same as every other panel.
 *
 * Select a clip carrying a transition — or a bar itself — and the tab follows
 * it: its style is the marked tile, and a click on another tile changes that
 * transition to it. The click never places anything: with nothing transitioned
 * it only picks.
 */
export function TransitionsPanel() {
  const t = useTranslations("panels.transitions");
  // The bar the tab is tuned to: a selected bar directly, or the one playing
  // the selected clip's cut.
  const live = useEditor((s) => {
    if (s.selection?.kind === "transition")
      return s.transitions.find((t) => t.id === s.selection!.id) ?? null;
    if (s.selection?.kind !== "clip") return null;
    const roles = resolveTransitions(s.clips, s.transitions);
    return (
      s.transitions.find((t) =>
        (roles.get(t.id) ?? []).some((r) => r.kind === "cut" && r.clipId === s.selection!.id)
      ) ?? null
    );
  });
  const { a, b } = useCutFrames();
  return (
    <>
      {/* No header row. The top padding clears the side panel's floating
          close button: the first group's title runs beside it on the left,
          and the first tile row starts below it. */}
      <ScrollArea className="min-h-0 flex-1" contentClassName="px-3.5 pt-5 pb-4">
        {/* One key handler over every group, so arrows walk the whole list
            across section boundaries. */}
        {live && takesFeather(live.style) && <SoftnessRow bar={live} />}
        {live && takesBand(live.style) && <BandRow bar={live} />}
        <div onKeyDown={pickGridNav}>
          {TRANSITION_STYLE_GROUPS.map((g) => (
            <section key={g.label} className="mb-3 flex flex-col gap-1.5">
              <SectionTitle>{t(`groups.${g.label.toLowerCase()}` as Parameters<typeof t>[0])}</SectionTitle>
              <div className="grid grid-cols-2 gap-2">
                {g.ids.map((id) => (
                  <TransitionTile key={id} style={id} live={live} a={a} b={b} />
                ))}
              </div>
            </section>
          ))}
        </div>
      </ScrollArea>
    </>
  );
}

/** How soft the selected bar's reveal edge is: 0 is the hard edge, the top
 * of the range a ramp half the frame wide. A drag is one undo step. */
function SoftnessRow({ bar }: { bar: TimelineTransition }) {
  const t = useTranslations("panels.transitions");
  const hard = t("hard");
  const ck = useSliderCheckpoint();
  const write = (v: number) => {
    ck.begin();
    useEditor.getState().updateTransitionTransient(bar.id, { feather: v > 0 ? v : undefined });
  };
  return (
    <div className="mb-3">
      <Row label={t("softness")}>
        <ValueSlider
          label={t("edgeSoftness")}
          sliderClassName="data-horizontal:w-24"
          valueClassName="w-9 text-muted-foreground"
          value={bar.feather ?? 0}
          min={0}
          max={TRANSITION_FEATHER_MAX}
          step={0.01}
          format={(v) => (v ? `${Math.round(v * 100)}%` : hard)}
          parse={(raw) => {
            if (raw.trim().toLowerCase() === hard.toLowerCase()) {
              return 0;
            }
            const n = parseNumberInput(raw.replace(/%$/, ""));
            return n === null ? null : n / 100;
          }}
          onDraft={write}
          onCommit={(v) => {
            write(v);
            ck.end();
          }}
        />
      </Row>
    </div>
  );
}

/** How wide the white panel running ahead of the selected wipe's edge is:
 * 0 is none, the top of the range half the frame. A drag is one undo step. */
function BandRow({ bar }: { bar: TimelineTransition }) {
  const t = useTranslations("panels.transitions");
  const none = t("none");
  const ck = useSliderCheckpoint();
  const write = (v: number) => {
    ck.begin();
    useEditor.getState().updateTransitionTransient(bar.id, { band: v > 0 ? v : undefined });
  };
  return (
    <div className="mb-3">
      <Row label={t("whiteBand")}>
        <ValueSlider
          label={t("whiteBandWidth")}
          sliderClassName="data-horizontal:w-24"
          valueClassName="w-9 text-muted-foreground"
          value={bar.band ?? 0}
          min={0}
          max={TRANSITION_BAND_MAX}
          step={0.01}
          format={(v) => (v ? `${Math.round(v * 100)}%` : none)}
          parse={(raw) => {
            if (raw.trim().toLowerCase() === none.toLowerCase()) {
              return 0;
            }
            const n = parseNumberInput(raw.replace(/%$/, ""));
            return n === null ? null : n / 100;
          }}
          onDraft={write}
          onCommit={(v) => {
            write(v);
            ck.end();
          }}
        />
      </Row>
    </div>
  );
}

/** The frame an asset shows at `srcT` seconds into its source: the nearest
 * filmstrip thumb for video, the picture itself for an image. */
function frameAt(asset: MediaAsset, srcT: number): string | null {
  if (asset.thumbs?.length) {
    const step = asset.thumbStep || 1;
    const i = Math.max(0, Math.min(asset.thumbs.length - 1, Math.round(srcT / step)));
    return asset.thumbs[i];
  }
  if (asset.type === "image") return asset.url;
  return null;
}

/** Tile-height capture for the swatch previews: covers a 2× retina portrait
 * tile whichever way the source is oriented. */
const TILE_FRAME_H = 640;

/** One side of the cut: the sharp capture at the exact source time once it
 * lands, the nearest filmstrip thumb (or the image itself) until then. */
function useCutFrame(
  slot: string,
  side: { asset: MediaAsset; srcT: number } | null
): string | null {
  const url = side?.asset.type === "video" ? previewUrl(side.asset) : null;
  const time = side?.srcT ?? 0;
  const id = url ? `${url}#${time.toFixed(2)}` : "";
  const cached = url ? peekEdgeFrame(url, time, TILE_FRAME_H) : null;
  const [frame, setFrame] = useState<{ id: string; src: string } | null>(null);
  useEffect(() => {
    if (!url || cached) return;
    let live = true;
    void requestEdgeFrame(slot, url, time, TILE_FRAME_H).then((src) => {
      if (live && src) setFrame({ id, src });
    });
    return () => {
      live = false;
    };
  }, [url, time, slot, id, cached]);
  if (!side) return null;
  const sharp = cached ?? (frame?.id === id ? frame.src : null);
  return sharp ?? frameAt(side.asset, time);
}

/** The two frames either side of the cut nearest the playhead — the real
 * footage a transition dropped there would blend. Null sides fall back to the
 * tiles' stand-in scenes. */
function useCutFrames(): { a: string | null; b: string | null } {
  const clips = useEditor((s) => s.clips);
  const assets = useEditor((s) => s.assets);
  // Re-pick the cut as the playhead crosses half-second marks.
  const tick = usePreviewTimeEvery(2);
  const cut = useMemo(() => {
    const spans = getClipSpans(clips, assets);
    let best: {
      a: { asset: MediaAsset; srcT: number };
      b: { asset: MediaAsset; srcT: number };
    } | null = null;
    let bestDist = Infinity;
    for (let i = 0; i + 1 < spans.length; i++) {
      const out = spans[i];
      const inc = spans[i + 1];
      const dist = Math.abs(tick / 2 - inc.start);
      if (dist >= bestDist) continue;
      bestDist = dist;
      best = {
        a: { asset: out.asset, srcT: out.clip.out },
        b: { asset: inc.asset, srcT: inc.clip.in },
      };
    }
    return best;
  }, [clips, assets, tick]);
  const a = useCutFrame("xtile-a", cut?.a ?? null);
  const b = useCutFrame("xtile-b", cut?.b ?? null);
  return { a, b };
}

/** How a transition tile's loop spends its time: a beat on the first shot,
 * the handover, a beat on the second. */
const X_LOOP = 2.4;
const X_HOLD = 0.5;
const X_RUN = 1.2;

/** Each tile's place across the whole list, group boundaries included. The
 * golden-ratio phase steps against it, so at any moment only a few tiles —
 * scattered through the grid — are mid-handover. */
const TILE_INDEX = new Map(
  TRANSITION_STYLE_GROUPS.flatMap((g) => g.ids).map((id, i) => [id, i] as const)
);

function TransitionTile({
  style,
  live,
  a,
  b,
}: {
  style: TransitionStyle;
  live: TimelineTransition | null;
  a: string | null;
  b: string | null;
}) {
  const transitionLabel = useLabels("transition");
  const { picked, pick } = useAssetPick(`transition:${style}`);
  const ref = useRef<HTMLButtonElement>(null);
  const { t, restart } = useHandoverClock(TILE_INDEX.get(style) ?? 0);
  // What the selection means for this tile: it wears the ring when it is the
  // live bar's style, and a click swaps that bar onto it.
  const isLive = !!live && live.style === style;
  const marked = live ? isLive : picked;
  useEffect(() => {
    // Selecting a transition brings its tile into view — the list is long
    // enough that its group is often scrolled away.
    if (isLive) ref.current?.scrollIntoView({ block: "nearest" });
  }, [isLive]);
  return (
    <button
      ref={ref}
      type="button"
      data-pick-id={`transition:${style}`}
      aria-pressed={marked}
      draggable
      onDragStart={(e) => {
        setElementDragData(e, { kind: "transition", style });
        setObjectDragImage(e);
      }}
      onDragEnd={clearElementDrag}
      onPointerEnter={restart}
      onFocus={restart}
      onClick={() => {
        if (!live) return pick();
        if (!isLive) useEditor.getState().updateTransition(live.id, { style });
        previewBar(live);
      }}
      // Scroll margin keeps the whole tile — selection ring included — clear
      // of the scroller's edges when scrollIntoView lands it there. The picked
      // ring is the focus indicator; the browser outline stays off.
      className="flex scroll-m-2 flex-col gap-1.5 text-[11px] font-medium text-muted-foreground outline-none transition-colors hover:text-foreground"
    >
      <TransitionSwatch
        style={style}
        a={a}
        b={b}
        t={t}
        className={cn("w-full rounded-lg border border-border", marked && PICKED_RING)}
      />
      <span className="leading-none">{transitionLabel(style)}</span>
    </button>
  );
}

/** A tile's place on the handover loop. A golden-ratio phase by place in the
 * list staggers the grid's loops; hovering a tile starts its handover over
 * from the top, so the pointer never lands mid-blend waiting for the loop to
 * come around. At rest during playback the clock holds its still frame; the
 * phase only shifts a running loop. */
function useHandoverClock(index: number): { t: number; restart: () => void } {
  const [runEpoch, setRunEpoch] = useState(0);
  const [phase, setPhase] = useState(() => ((index * PHASE_STEP) % 1) * X_LOOP);
  const restart = () => {
    setPhase(0);
    setRunEpoch((n) => n + 1);
  };
  const playing = useEditor((s) => s.playing);
  const clock = useSwatchClock(true, X_LOOP, runEpoch);
  return { t: playing ? clock : (clock + phase) % X_LOOP, restart };
}

/**
 * The tiles a video clip's own entrance or exit picks from — the Animation
 * view of the clip's inspector. Each style plays on the clip's edge frame
 * against black, the way an open edge really renders; the pick lands as the
 * bar on that edge, the same bar a tile dragged from this tab or the
 * assistant's set_animation writes. Upper-track clips composite through
 * alpha, so they offer the two ramps alpha can express.
 */
export function ClipAnimationTiles({
  clip,
  which,
  value,
  onPick,
}: {
  clip: VideoClip;
  which: "in" | "out";
  /** The style on that edge, or undefined for none. */
  value?: AnimStyle;
  onPick: (style: AnimStyle | null) => void;
}) {
  const t = useTranslations("panels.transitions");
  const asset = useEditor((s) => s.assets.find((a) => a.id === clip.assetId));
  const frame = useCutFrame(
    `anim-${which}`,
    asset ? { asset, srcT: which === "in" ? clip.in : clip.out } : null
  );
  const ids = clip.track > 0 ? ANIM_STYLE_IDS.filter((s) => overlayAnimStyle(s) === s) : ANIM_STYLE_IDS;
  return (
    <div className="grid grid-cols-2 gap-2" onKeyDown={pickGridNav}>
      <Tile selected={!value} pickId="anim:none" onClick={() => onPick(null)} label={t("none")} className="p-1">
        <span className="relative block aspect-square w-full overflow-hidden rounded-md bg-black">
          <Layer frame={frame} variant="day" />
        </span>
      </Tile>
      {ids.map((id, i) => (
        <ClipAnimTile
          key={id}
          style={id}
          which={which}
          frame={frame}
          index={i}
          selected={value === id}
          onPick={() => onPick(id)}
        />
      ))}
    </div>
  );
}

function ClipAnimTile({
  style,
  which,
  frame,
  index,
  selected,
  onPick,
}: {
  style: AnimStyle;
  which: "in" | "out";
  frame: string | null;
  index: number;
  selected: boolean;
  onPick: () => void;
}) {
  const animLabel = useLabels("anim");
  const { t, restart } = useHandoverClock(index);
  return (
    <Tile
      selected={selected}
      pickId={`anim:${style}`}
      onClick={onPick}
      onHover={(inside) => inside && restart()}
      label={animLabel(style)}
      className="p-1"
    >
      <TransitionSwatch
        style={transitionStyleOfAnim(style)}
        a={which === "in" ? null : frame}
        b={which === "in" ? frame : null}
        aVariant={which === "in" ? "void" : "day"}
        bVariant={which === "in" ? "day" : "void"}
        t={t}
        className="w-full"
      />
    </Tile>
  );
}

/** One full-tile layer of the handover — a shot, transformed or clipped as
 * the style's moment demands. */
function Layer({
  frame,
  variant,
  style,
}: {
  frame: string | null;
  variant: "day" | "dusk" | "void";
  style?: React.CSSProperties;
}) {
  return (
    <span className="absolute inset-0 overflow-hidden" style={style}>
      <SwatchScene frame={frame} variant={variant} />
    </span>
  );
}

/** The handover itself, played on the two shots: each style renders its own
 * moment at progress `p` — sliding, wiping, dipping or blending shot A into
 * shot B the way the real render does. */
function TransitionSwatch({
  style,
  a,
  b,
  aVariant = "day",
  bVariant = "dusk",
  t,
  className,
}: {
  style: TransitionStyle;
  a: string | null;
  b: string | null;
  /** The stand-in each side shows without a frame; `void` is black. */
  aVariant?: "day" | "dusk" | "void";
  bVariant?: "day" | "dusk" | "void";
  t: number;
  className?: string;
}) {
  const p0 = Math.min(1, Math.max(0, (t - X_HOLD) / X_RUN));
  const p = p0 * p0 * (3 - 2 * p0);
  const pc = (n: number) => `${(n * 100).toFixed(1)}%`;
  const A = (s?: React.CSSProperties) => <Layer frame={a} variant={aVariant} style={s} />;
  const B = (s?: React.CSSProperties) => <Layer frame={b} variant={bVariant} style={s} />;
  let layers: React.ReactNode;
  switch (style) {
    case "crossfade":
      layers = (
        <>
          {A()}
          {B({ opacity: p })}
        </>
      );
      break;
    case "crosszoom":
      layers = (
        <>
          {A({ transform: `scale(${(1 + 0.4 * p).toFixed(3)})` })}
          {B({ opacity: p, transform: `scale(${(1.4 - 0.4 * p).toFixed(3)})` })}
        </>
      );
      break;
    case "dipblack":
    case "dipwhite":
      layers = (
        <>
          {p < 0.5 ? A() : B()}
          <span
            className="absolute inset-0"
            style={{
              background: style === "dipblack" ? "#000" : "#fff",
              opacity: 1 - Math.abs(2 * p - 1),
            }}
          />
        </>
      );
      break;
    case "blur":
      layers = (
        <>
          {A({ filter: `blur(${(p * 3).toFixed(2)}px)` })}
          {B({ opacity: p, filter: `blur(${((1 - p) * 3).toFixed(2)}px)` })}
        </>
      );
      break;
    case "pushleft":
      layers = (
        <>
          {A({ transform: `translateX(${pc(-p)})` })}
          {B({ transform: `translateX(${pc(1 - p)})` })}
        </>
      );
      break;
    case "pushright":
      layers = (
        <>
          {A({ transform: `translateX(${pc(p)})` })}
          {B({ transform: `translateX(${pc(-(1 - p))})` })}
        </>
      );
      break;
    case "pushup":
      layers = (
        <>
          {A({ transform: `translateY(${pc(-p)})` })}
          {B({ transform: `translateY(${pc(1 - p)})` })}
        </>
      );
      break;
    case "pushdown":
      layers = (
        <>
          {A({ transform: `translateY(${pc(p)})` })}
          {B({ transform: `translateY(${pc(-(1 - p))})` })}
        </>
      );
      break;
    case "wipeleft":
      layers = (
        <>
          {A()}
          {B({ clipPath: `inset(0 0 0 ${pc(1 - p)})` })}
        </>
      );
      break;
    case "wiperight":
      layers = (
        <>
          {A()}
          {B({ clipPath: `inset(0 ${pc(1 - p)} 0 0)` })}
        </>
      );
      break;
    case "wipeup":
      layers = (
        <>
          {A()}
          {B({ clipPath: `inset(${pc(1 - p)} 0 0 0)` })}
        </>
      );
      break;
    case "wipedown":
      layers = (
        <>
          {A()}
          {B({ clipPath: `inset(0 0 ${pc(1 - p)} 0)` })}
        </>
      );
      break;
    case "circleopen":
      layers = (
        <>
          {A()}
          {B({ clipPath: `circle(${(p * 72).toFixed(1)}% at 50% 50%)` })}
        </>
      );
      break;
    case "circleclose":
      layers = (
        <>
          {B()}
          {A({ clipPath: `circle(${((1 - p) * 72).toFixed(1)}% at 50% 50%)` })}
        </>
      );
      break;
    case "splitopen":
      layers = (
        <>
          {A()}
          {B({ clipPath: `inset(0 ${pc((1 - p) / 2)})` })}
        </>
      );
      break;
    case "splitclose":
      layers = (
        <>
          {B()}
          {A({ clipPath: `inset(0 ${pc(p / 2)})` })}
        </>
      );
      break;
    case "clockwipe": {
      // A wedge pinned at the top-left corner sweeps from the top edge down
      // to the left one. Past the diagonal the wedge takes the far corner.
      const angle = shapeEase(style, p) * CLOCK_SWEEP;
      const wedge =
        angle <= Math.PI / 4
          ? `polygon(0 0, 100% 0, 100% ${pc(Math.tan(angle))})`
          : `polygon(0 0, 100% 0, 100% 100%, ${pc(1 / Math.tan(Math.max(angle, 1e-3)))} 100%)`;
      layers = (
        <>
          {A()}
          {B({ clipPath: wedge })}
        </>
      );
      break;
    }
    case "sliceleft":
    case "sliceup": {
      // Two halves slide opposite ways: the top half left and the bottom
      // right, or the left column up and the right one down.
      const e = shapeEase(style, p);
      const across = style === "sliceleft";
      const head = across ? "inset(0 0 50% 0)" : "inset(0 50% 0 0)";
      const tail = across ? "inset(50% 0 0 0)" : "inset(0 0 0 50%)";
      const move = (n: number) => (across ? `translateX(${pc(n)})` : `translateY(${pc(n)})`);
      layers = (
        <>
          {A({ clipPath: head, transform: move(-e) })}
          {B({ clipPath: head, transform: move(1 - e) })}
          {A({ clipPath: tail, transform: move(e) })}
          {B({ clipPath: tail, transform: move(e - 1) })}
        </>
      );
      break;
    }
  }
  return (
    <span
      data-drag-object
      className={cn("relative block aspect-square overflow-hidden rounded-md bg-black", className)}
    >
      {layers}
    </span>
  );
}

/** How far ahead of a handover the preview starts, and how long it runs past
 * it, so a change plays itself on the real footage. */
const PREVIEW_LEAD = 0.4;
const PREVIEW_TAIL = 0.35;

/** Play a bar's window on the real footage: a beat ahead of it, a beat past. */
export function previewBar(t: { start: number; seconds: number }) {
  const s = useEditor.getState();
  s.previewRange(Math.max(0, t.start - PREVIEW_LEAD), t.start + t.seconds + PREVIEW_TAIL);
}