Skip to content

Open source CapCut alternative.

TypeScript

fix(site): the in-use rule counts stickers, fonts and backdrops [rebuild]

David Le7fea5c6cd827
diff --git a/site/src/cut/lib/chatAssets.ts b/site/src/cut/lib/chatAssets.ts
index 3e73e049..e4549b96 100644
--- a/site/src/cut/lib/chatAssets.ts
+++ b/site/src/cut/lib/chatAssets.ts
@@ -2,7 +2,7 @@
import { useGenerate } from "./generate";
import { usePreviewAudio } from "./previewAudio";
-import { useEditor } from "./store";
+import { assetIdsInUse, useEditor } from "./store";
// Chat-created media belongs to its thread. The files live in the project's
// media folder like every other asset (drag-to-timeline and previews need
@@ -62,9 +62,7 @@ export function threadOwnsAssets(threadId: string): boolean {
* deletion. */
export function deleteChatAssets(threadId: string) {
const s = useEditor.getState();
- const inUse = new Set(
- [...s.clips, ...s.audioClips].map((c) => c.assetId)
- );
+ const inUse = assetIdsInUse(s);
const owned = s.assets.filter(
(a) => a.origin === "chat" && a.chatId === threadId && !inUse.has(a.id)
);
diff --git a/site/src/cut/lib/store.test.ts b/site/src/cut/lib/store.test.ts
index e2bed4ad..e569a22e 100644
--- a/site/src/cut/lib/store.test.ts
+++ b/site/src/cut/lib/store.test.ts
@@ -1,8 +1,8 @@
import { beforeEach, describe, expect, test } from "bun:test";
import { groupRemap } from "@donkeycut/effects-kit";
-import { adoptTransitionFields, clipLen, cutTranscribeSpec, deriveTransitionFields, docOverlays, getClipSpans, liftClipLooks, moveOverlayGroup, overlayLaneOrder, normalizeElementLanes, parkedTransitions, placeInRun, projectDuration, separateOverlaps, serializeDoc, startTrimRipple, useEditor } from "./store";
+import { adoptTransitionFields, assetIdsInUse, clipLen, cutTranscribeSpec, deriveTransitionFields, docOverlays, getClipSpans, liftClipLooks, moveOverlayGroup, overlayLaneOrder, normalizeElementLanes, parkedTransitions, placeInRun, projectDuration, separateOverlaps, serializeDoc, startTrimRipple, useEditor } from "./store";
import { playheadAt, setPlayhead, setSkim } from "./playhead";
-import { emptySubtitles } from "./types";
+import { emptySubtitles, uploadedFontId } from "./types";
import type { AudioClip, MediaAsset, SubtitleCue, TextOverlay, VideoClip } from "./types";
/**
@@ -1889,3 +1889,37 @@ describe("transcribe spec", () => {
expect(spec.audio.some((a) => a.file === still.fileName)).toBe(false);
});
});
+
+describe("assetIdsInUse", () => {
+ const clip = (id: string, extra: Partial<VideoClip> = {}) =>
+ ({ id, assetId: id, track: 0, start: 0, in: 0, out: 1, ...extra }) as VideoClip;
+
+ test("counts what a clip names and what its removal names", () => {
+ const used = assetIdsInUse({
+ clips: [clip("v1", { removal: { matte: { assetId: "m1" }, backdrop: { assetId: "b1" } } } as Partial<VideoClip>)],
+ audioClips: [{ id: "a1", assetId: "snd" } as AudioClip],
+ overlays: [],
+ });
+ expect([...used].sort()).toEqual(["b1", "m1", "snd", "v1"]);
+ });
+
+ test("counts a sticker's picture and an uploaded font, and neither built-in", () => {
+ // A chat-made image placed as a sticker, and a chat-made font a title is
+ // set in, are both still needed by a document no clip points at.
+ const used = assetIdsInUse({
+ clips: [],
+ audioClips: [],
+ overlays: [
+ { id: "o1", kind: "sticker", assetId: "pic" } as unknown as TextOverlay,
+ { id: "o2", kind: "text", font: uploadedFontId("fnt") } as unknown as TextOverlay,
+ { id: "o3", kind: "text", font: "inter" } as unknown as TextOverlay,
+ ],
+ subtitles: { font: uploadedFontId("capfnt") },
+ });
+ expect([...used].sort()).toEqual(["capfnt", "fnt", "pic"]);
+ });
+
+ test("says nothing is in use for an empty document", () => {
+ expect(assetIdsInUse({ clips: [], audioClips: [], overlays: [] }).size).toBe(0);
+ });
+});
diff --git a/site/src/cut/lib/store.ts b/site/src/cut/lib/store.ts
index 427371fd..c471d8fb 100644
--- a/site/src/cut/lib/store.ts
+++ b/site/src/cut/lib/store.ts
@@ -71,7 +71,7 @@ import { useGenNotify } from "./genNotify";
import { clampPlayhead, playheadAt, previewAt, setPlayhead, setSkim } from "./playhead";
import { engineTranscribeSamples, withEngineStt } from "./localStt";
import { laneCues, subtitleLaneCount, trackLocale } from "./subtitles";
-import { ANIM_STYLE_IDS, animStyleOfTransition, assetIsSilent, clipPoseAt, DEFAULT_BACKGROUND, emptySubtitles, frameOf, IMAGE_CLIP_SECONDS, isAudioTransition, isEffectOverlay, isStickerOverlay, MAX_SUBTITLE_LANES, mediaUrl, migrateBehindSubject, migrateLegacyTransitions, normalizeAspect, overlayAnimStyle, projectBackground, SPEED_FLOOR, SPEED_MIN, stampOverlayKinds, stripDefaultOverlayKinds, TRANSITION_MAX, TRANSITION_STYLE_IDS, transitionBarAt, transitionBarStart, transitionStyleOfAnim, type TransitionBoundaryKind } from "./types";
+import { ANIM_STYLE_IDS, animStyleOfTransition, assetIsSilent, clipPoseAt, DEFAULT_BACKGROUND, emptySubtitles, frameOf, IMAGE_CLIP_SECONDS, isAudioTransition, fontAssetId, isEffectOverlay, isStickerOverlay, isTextOverlay, MAX_SUBTITLE_LANES, mediaUrl, migrateBehindSubject, migrateLegacyTransitions, normalizeAspect, overlayAnimStyle, projectBackground, SPEED_FLOOR, SPEED_MIN, stampOverlayKinds, stripDefaultOverlayKinds, TRANSITION_MAX, TRANSITION_STYLE_IDS, transitionBarAt, transitionBarStart, transitionStyleOfAnim, type TransitionBoundaryKind } from "./types";
import { liftMoveTracks } from "./textMotion";
import { readTextStyle } from "./textStyle";
import { loadUiState, saveUiState, type ProjectUiState } from "./uiState";
@@ -6019,6 +6019,41 @@ let durAudio: AudioClip[] | null = null;
let durOverlays: Overlay[] | null = null;
let durValue = 0;
+/**
+ * Every asset the document points at.
+ *
+ * A clip names one, and so do the places that are easy to miss: a removal
+ * clip's baked matte and its backdrop, a sticker element's picture, and the
+ * uploaded font behind a title or a caption, which a text element names
+ * through the font registry instead of by asset. Anything reading this to
+ * decide what an asset is still needed for has to see all of them — dropping
+ * what a sticker draws from is as visible as dropping a clip's.
+ */
+export function assetIdsInUse(s: {
+ clips: VideoClip[];
+ audioClips: AudioClip[];
+ overlays: Overlay[];
+ subtitles?: { font?: string } | null;
+}): Set<string> {
+ const used = new Set<string>();
+ const font = (id: string | undefined) => {
+ const asset = id ? fontAssetId(id) : null;
+ if (asset) used.add(asset);
+ };
+ for (const c of s.clips) {
+ if (c.assetId) used.add(c.assetId);
+ if (c.removal?.matte?.assetId) used.add(c.removal.matte.assetId);
+ if (c.removal?.backdrop?.assetId) used.add(c.removal.backdrop.assetId);
+ }
+ for (const c of s.audioClips) if (c.assetId) used.add(c.assetId);
+ for (const o of s.overlays) {
+ if (isStickerOverlay(o) && o.assetId) used.add(o.assetId);
+ if (isTextOverlay(o)) font(o.font);
+ }
+ font(s.subtitles?.font);
+ return used;
+}
+
export function projectDuration(s: {
clips: VideoClip[];
audioClips: AudioClip[];
diff --git a/site/src/cut/lib/types.ts b/site/src/cut/lib/types.ts
index 7b3748b4..bf20bd7c 100644
--- a/site/src/cut/lib/types.ts
+++ b/site/src/cut/lib/types.ts
@@ -1114,6 +1114,12 @@ export function allFonts(): FontDef[] {
/** The registry id an uploaded font asset answers to. */
export const uploadedFontId = (assetId: string) => `asset:${assetId}`;
+/** The asset behind a registry font id, or null for one of the built-in
+ * families. The inverse of `uploadedFontId`, kept beside it so the shape of
+ * the id is written down once. */
+export const fontAssetId = (fontId: string): string | null =>
+ fontId.startsWith("asset:") ? fontId.slice("asset:".length) : null;
+
/** Subscribe to registry changes (UI font menus re-render on registration). */
export function onFontsChanged(cb: () => void): () => void {
fontListeners.add(cb);