Open source CapCut alternative.
TypeScript
feat(site): a mention pill wears the timeline item's face
David Le819679f87e4d
diff --git a/site/src/cut/components/Timeline.tsx b/site/src/cut/components/Timeline.tsxindex 90a6460c..06af1667 100644--- a/site/src/cut/components/Timeline.tsx+++ b/site/src/cut/components/Timeline.tsx@@ -1,8 +1,8 @@"use client";import { Fragment, useCallback, useEffect, useLayoutEffect, useMemo, useReducer, useRef, useState, useSyncExternalStore, type ReactNode } from "react";-import { ArrowDown, ArrowDownToLine, ArrowLeft, ArrowLeftToLine, ArrowRight, ArrowRightToLine, ArrowUp, ArrowUpToLine, AudioLines, Blend, Check, Circle, Clapperboard, Copy, Diamond, Download, Droplets, EllipsisVertical, Expand, Eye, EyeOff, FolderOpen, FolderPlus, FoldHorizontal, Fullscreen, Loader2, Moon, MoreHorizontal, Pause, Play, Scissors, SkipBack, Sparkles, Sticker, Sun, Target, Trash2, Type, UnfoldHorizontal, Volume2, VolumeX, type LucideIcon } from "lucide-react";-import { SHAPE_CHIP_ICONS } from "@/cut/components/entityIcons";+import { ArrowDownToLine, AudioLines, Check, Clapperboard, Copy, Diamond, Download, EllipsisVertical, Eye, EyeOff, FolderOpen, FolderPlus, Fullscreen, Loader2, MoreHorizontal, Pause, Play, Scissors, SkipBack, Sticker, Trash2, Type, Volume2, VolumeX } from "lucide-react";+import { EFFECT_CHIP_ICONS, effectIconKind, SHAPE_CHIP_ICONS, TRANSITION_ICONS } from "@/cut/components/entityIcons";import { Button } from "@/components/ui/button";import {DropdownMenu,@@ -51,7 +51,7 @@ import type { VideoTrackPlacement } from "@/cut/lib/store";import { playheadAt, setSkim, skimAt, subscribePlayhead, usePlayhead, useSkim } from "@/cut/lib/playhead";import { laneHidden, subtitleLaneCount } from "@/cut/lib/subtitles";import { formatTime, formatTimecode } from "@/cut/lib/time";-import { EFFECT_LABELS, isAudioEffect, type EffectId } from "@donkeycut/effects-kit";+import { EFFECT_LABELS, type EffectId } from "@donkeycut/effects-kit";import { emptySubtitles, IMAGE_CLIP_SECONDS, isAudioTransition, SHAPE_LABELS, TRANSITION_MAX, TRANSITION_STYLE_LABELS, transitionBarStart, transitionDefaultSeconds, type ShapeKind } from "@/cut/lib/types";import type { AudioClip, ClipSpan, ColorGrade, MediaAsset, Overlay, Selection, StickerOverlay, SubtitleCue, TimelineTransition, TransitionStyle, VideoClip } from "@/cut/lib/types";import { isLottieAsset } from "@/cut/lib/lottieAssets";@@ -101,26 +101,6 @@ function useDropPreview<T>(initial: T): [T, (next: T) => void] {return [value, set];}-const TRANSITION_ICONS: Record<TransitionStyle, LucideIcon> = {- crossfade: Blend,- audiocross: AudioLines,- crosszoom: Expand,- dipblack: Moon,- dipwhite: Sun,- blur: Droplets,- pushleft: ArrowLeft,- pushright: ArrowRight,- pushup: ArrowUp,- pushdown: ArrowDown,- wipeleft: ArrowLeftToLine,- wiperight: ArrowRightToLine,- wipeup: ArrowUpToLine,- wipedown: ArrowDownToLine,- circleopen: Circle,- circleclose: Target,- splitopen: UnfoldHorizontal,- splitclose: FoldHorizontal,-};/** Video row height: the picture band plus the sound band under it. */const VIDEO_H = 88;@@ -294,7 +274,7 @@ const overlayFamily = (o: Overlay): OverlayFamily =>/** The glyph on an effect's bar: a waveform for the treatments that work on* the sound, sparkles for the ones that work on the picture. */function EffectChipIcon({ effect }: { effect: EffectId }) {- const Icon = isAudioEffect(effect) ? AudioLines : Sparkles;+ const Icon = EFFECT_CHIP_ICONS[effectIconKind(effect)];return <Icon className="mr-1 size-2.5 shrink-0" />;}diff --git a/site/src/cut/components/entityIcons.ts b/site/src/cut/components/entityIcons.tsindex 47290206..f0f321cb 100644--- a/site/src/cut/components/entityIcons.ts+++ b/site/src/cut/components/entityIcons.ts@@ -1,23 +1,40 @@import { createElement, type ReactNode } from "react";import {+ ArrowDown,+ ArrowDownToLine,+ ArrowLeft,+ ArrowLeftToLine,+ ArrowRight,+ ArrowRightToLine,+ ArrowUp,+ ArrowUpToLine,+ AudioLines,+ Blend,Captions,Circle,Diamond,+ Droplets,+ Expand,+ FoldHorizontal,Heart,Hexagon,Minus,+ Moon,MoveRight,Sparkles,Square,Star,Sticker,+ Sun,+ Target,Triangle,Type,UnfoldHorizontal,type LucideIcon,} from "lucide-react";+import { isAudioEffect, type EffectId } from "@donkeycut/effects-kit";import type { AssetRef } from "@/cut/lib/assetRef";-import type { ShapeKind } from "@/cut/lib/types";+import type { ShapeKind, TransitionStyle } from "@/cut/lib/types";/** The glyph a shape wears on its timeline chip and mention pill. */export const SHAPE_CHIP_ICONS: Record<ShapeKind, LucideIcon> = {@@ -32,6 +49,37 @@ export const SHAPE_CHIP_ICONS: Record<ShapeKind, LucideIcon> = {arrow: MoveRight,};+/** The glyph a transition wears on its bar and on its mention pill. */+export const TRANSITION_ICONS: Record<TransitionStyle, LucideIcon> = {+ crossfade: Blend,+ audiocross: AudioLines,+ crosszoom: Expand,+ dipblack: Moon,+ dipwhite: Sun,+ blur: Droplets,+ pushleft: ArrowLeft,+ pushright: ArrowRight,+ pushup: ArrowUp,+ pushdown: ArrowDown,+ wipeleft: ArrowLeftToLine,+ wiperight: ArrowRightToLine,+ wipeup: ArrowUpToLine,+ wipedown: ArrowDownToLine,+ circleopen: Circle,+ circleclose: Target,+ splitopen: UnfoldHorizontal,+ splitclose: FoldHorizontal,+};++/** The glyph an effect wears on its bar and on its mention pill: a waveform+ * for the treatments that work on the sound, sparkles for the ones that work+ * on the picture. */+export const EFFECT_CHIP_ICONS = { sound: AudioLines, picture: Sparkles } as const;++/** Which of the two an effect takes. */+export const effectIconKind = (effect: EffectId | undefined): "sound" | "picture" =>+ effect && isAudioEffect(effect) ? "sound" : "picture";+/** The entity's icon as a rendered element, sized by `className`. Null for* media refs — those show the media itself. */export function entityGlyph(ref: AssetRef, className: string): ReactNode {@@ -50,9 +98,9 @@ export function entityIcon(ref: AssetRef): LucideIcon | null {case "sticker":return Sticker;case "effect":- return Sparkles;+ return EFFECT_CHIP_ICONS[effectIconKind(ref.effectId)];case "transition":- return UnfoldHorizontal;+ return TRANSITION_ICONS[ref.transitionStyle ?? "crossfade"];case "cue":return Captions;case "keyframe":diff --git a/site/src/cut/lib/assetRef.test.ts b/site/src/cut/lib/assetRef.test.tsindex f1a76753..6fa09689 100644--- a/site/src/cut/lib/assetRef.test.ts+++ b/site/src/cut/lib/assetRef.test.ts@@ -72,7 +72,7 @@ describe("entityRefs", () => {"Snap Test","VHS","Sticker",- "transition 1",+ "Cross fade","cue 1",]);// A sticker names by kind — its prompt-shaped asset name stays out of the@@ -89,6 +89,27 @@ describe("entityRefs", () => {const rect = refs.find((r) => r.name === "Rectangle")!;expect(rect.entityKind).toBe("shape");expect(rect.shapeKind).toBe("rect");+ // A bar reads the way its own label on the row reads, and carries the+ // style so its pill wears the icon the bar wears.+ const bar = refs.find((r) => r.name === "Cross fade")!;+ expect(bar.entityKind).toBe("transition");+ expect(bar.transitionStyle).toBe("crossfade");+ // An effect carries its treatment, which is what splits the waveform from+ // the sparkles.+ expect(refs.find((r) => r.name === "VHS")!.effectId).toBe("vhs");+ });++ test("two bars of one style number, one of a kind does not", () => {+ const refs = entityRefs(+ sources({+ transitions: [+ { id: "tr-1", start: 1, seconds: 0.5, style: "audiocross" },+ { id: "tr-3", start: 5, seconds: 0.5, style: "audiocross" },+ { id: "tr-2", start: 3, seconds: 0.5, style: "dipblack" },+ ],+ })+ );+ expect(refs.map((r) => r.name)).toEqual(["Cross dissolve 1", "Dip to black", "Cross dissolve 2"]);});test("names number in timeline order only on collision", () => {@@ -166,7 +187,7 @@ describe("selectionRefTokens", () => {{ kind: "cue", id: "cue-1" },],});- expect(token).toBe('@c1 @"Hello" @"transition 1" @"cue 1"');+ expect(token).toBe('@c1 @"Hello" @"Cross fade" @"cue 1"');});test("nothing resolvable yields null", () => {diff --git a/site/src/cut/lib/assetRef.ts b/site/src/cut/lib/assetRef.tsindex 925cd3a0..14c02763 100644--- a/site/src/cut/lib/assetRef.ts+++ b/site/src/cut/lib/assetRef.ts@@ -6,7 +6,7 @@ import { fetchLibrary, libraryRouteUrl, type LibraryAsset } from "./library";import { stockAspectDims, stockTitle, type StockImage, type StockMusic, type StockVideo } from "./stock";import { STOCK_IMAGES } from "./stockManifest";import { STOCK_VIDEOS } from "./stockVideoManifest";-import { EFFECT_LABELS } from "@donkeycut/effects-kit";+import { EFFECT_LABELS, type EffectId } from "@donkeycut/effects-kit";import { clipLen, getClipSpans, useEditor } from "./store";import {isEffectOverlay,@@ -14,7 +14,9 @@ import {isStickerOverlay,isTextOverlay,SHAPE_LABELS,+ TRANSITION_STYLE_LABELS,type ShapeKind,+ type TransitionStyle,} from "./types";import type {AudioClip,@@ -87,6 +89,11 @@ export interface AssetRef {entityKind?: "title" | "shape" | "sticker" | "effect" | "transition" | "cue" | "keyframe";/** The shape element's kind, for the pill's per-shape icon. */shapeKind?: ShapeKind;+ /** The transition bar's style, so its pill wears the icon its bar wears. */+ transitionStyle?: TransitionStyle;+ /** The effect element's treatment, same reason: the sound treatments carry+ * a waveform, the picture ones sparkles. */+ effectId?: EffectId;/** Which track a keyframe ref rides — mask keys color their pill amber to* match the timeline's diamonds. */keyTrack?: "pose" | "mask";@@ -564,8 +571,9 @@ function overlayBaseName(o: Overlay, fallback: string): string {* elements, transition bars, subtitle cues, and every keyframe on a track-0* clip's or element's pose and mask tracks. Elements name by their content* ("Snap Test", "Rectangle"), numbered in timeline order only when two share- * a name ("Rectangle 1", "Rectangle 2"); transitions, cues, and keyframes- * stay ordinal. Names re-derive from the current timeline, so a pasted+ * a name ("Rectangle 1", "Rectangle 2"); a transition bar names by its+ * style ("Cross dissolve"), numbered the same way; cues and keyframes stay+ * ordinal. Names re-derive from the current timeline, so a pasted* mention resolves to whatever holds that name now; the internal ids ride in* the ref, never in the prompt text. */export function entityRefs(src: EntitySources): AssetRef[] {@@ -612,6 +620,7 @@ export function entityRefs(src: EntitySources): AssetRef[] {{entityKind: label as AssetRef["entityKind"],shapeKind: isShapeOverlay(o) ? o.shape : undefined,+ effectId: isEffectOverlay(o) ? o.effect : undefined,preview: stickerAsset?.url,})@@ -627,15 +636,33 @@ export function entityRefs(src: EntitySources): AssetRef[] {out.push(...keyRefs("clip", sp.clip.id, parent, sp.start, sp.clip.mask?.kf, "mask"));});- [...src.transitions].sort((a, b) => a.start - b.start).forEach((t, i) => {- const name = `transition ${i + 1}`;+ // A bar names by its style, the way its own label on the row reads, and+ // takes a number only when the timeline holds more than one of that style.+ const bars = [...src.transitions].sort((a, b) => a.start - b.start);+ const barCount = new Map<string, number>();+ for (const t of bars) barCount.set(t.style, (barCount.get(t.style) ?? 0) + 1);+ const barNth = new Map<string, number>();+ bars.forEach((t) => {+ const base = TRANSITION_STYLE_LABELS[t.style];+ const n = (barNth.get(t.style) ?? 0) + 1;+ barNth.set(t.style, n);+ let name = (barCount.get(t.style) ?? 0) > 1 ? `${base} ${n}` : base;+ // An element already carrying that name — a title reading "Cross dissolve"+ // — keeps it; the bar counts on past it.+ let bump = n;+ while (taken.has(name.toLowerCase())) {+ bump += 1;+ barNth.set(t.style, bump);+ name = `${base} ${bump}`;+ }+ taken.add(name.toLowerCase());out.push(entityRef(`transition:${t.id}`,name,`${name} — a transition bar on the timeline. id ${t.id}, style ${t.style}, ` +`window ${sec(t.start)}–${sec(t.start + t.seconds)}.`,- { entityKind: "transition" }+ { entityKind: "transition", transitionStyle: t.style }));});