Skip to content

Open source CapCut alternative.

TypeScript

feat(site): paste a chat file's reference as a pill

David Le8bd775b8a24d
diff --git a/site/src/cut/components/AssetRefs.tsx b/site/src/cut/components/AssetRefs.tsx
index 42ac6aa4..e33ad41f 100644
--- a/site/src/cut/components/AssetRefs.tsx
+++ b/site/src/cut/components/AssetRefs.tsx
@@ -6,9 +6,11 @@ import {
highlightMentions,
liveRefUrl,
mentionToken,
+ parseMentions,
refToken,
sameRef,
splitMentions,
+ unlistedRefs,
type AssetRef,
} from "@/cut/lib/assetRef";
import { MEDIA_CORS } from "@/cut/lib/mediaCors";
@@ -1157,10 +1159,14 @@ export function MentionTextarea({
// own — no picker step. One shot per mention; deleting the token and
// re-typing it pins again at the playhead's new spot.
const autoPinned = useRef(new Set<string>());
+ // Tokens resolve against the attachments first, then the candidates: a
+ // chip attached from a paste or a drop pills its token even when the file
+ // is one the picker never lists.
+ const pool = useMemo(() => [...(attachedRefs ?? []), ...candidates], [attachedRefs, candidates]);
useEffect(() => {
if (!onUpsertRef) return;
const present = new Set<string>();
- for (const seg of highlightMentions(value, candidates)) {
+ for (const seg of highlightMentions(value, pool)) {
const ref = seg.ref;
if (!ref || (ref.kind !== "video" && ref.kind !== "audio")) continue;
const key = `${ref.scope}:${ref.id}`;
@@ -1173,20 +1179,20 @@ export function MentionTextarea({
if (at !== null) onUpsertRef({ ...(attached ?? ref), t: at });
}
for (const k of [...autoPinned.current]) if (!present.has(k)) autoPinned.current.delete(k);
- }, [value, candidates, attachedRefs, onUpsertRef]);
+ }, [value, pool, attachedRefs, onUpsertRef]);
// Mirror segments with their character offsets, so a pill can tell when
// the caret sits inside its token.
const mirrorSegs = useMemo(
() =>
- highlightMentions(value, candidates).reduce<
+ highlightMentions(value, pool).reduce<
{ text: string; ref: AssetRef | null; start: number }[]
>((acc, seg) => {
const prev = acc[acc.length - 1];
acc.push({ ...seg, start: prev ? prev.start + prev.text.length : 0 });
return acc;
}, []),
- [value, candidates]
+ [value, pool]
);
// A pill cover is one box, so its token must move between lines whole —
@@ -1426,11 +1432,19 @@ export function MentionTextarea({
return;
}
}
+ const text = e.clipboardData.getData("text/plain");
+ if (!text) return;
+ // A token copied off a tile the picker never lists — a file the
+ // chat made — attaches the file it names, and the chip pills it.
+ if (onUpsertRef) {
+ const hidden = unlistedRefs(useEditor.getState().assets);
+ for (const ref of parseMentions(text, [...pool, ...hidden]).refs) {
+ if (!pool.some((c) => sameRef(c, ref))) onUpsertRef(ref);
+ }
+ }
// A token pasted flush against a neighbor never resolves — the
// quote swallows the boundary. Restore the missing space on
// whichever side needs one.
- const text = e.clipboardData.getData("text/plain");
- if (!text) return;
const el = e.currentTarget;
const s = el.selectionStart ?? 0;
const sEnd = el.selectionEnd ?? s;
diff --git a/site/src/cut/components/ChatAssets.tsx b/site/src/cut/components/ChatAssets.tsx
index cfe64191..a8eda94d 100644
--- a/site/src/cut/components/ChatAssets.tsx
+++ b/site/src/cut/components/ChatAssets.tsx
@@ -22,6 +22,7 @@ import { usePreviewAudio } from "@/cut/lib/previewAudio";
import { useEditor } from "@/cut/lib/store";
import { formatTime } from "@/cut/lib/time";
import type { MediaAsset } from "@/cut/lib/types";
+import { FOCUS_RING, PICKED_RING, useAssetPick } from "@/cut/lib/assetPick";
import { cn } from "@/lib/utils";
import { AudioRow } from "./AudioPanel";
import { DocText, useDocText } from "./DocText";
@@ -256,11 +257,23 @@ function MediaCard({ item, asset }: ChatCardProps) {
const ratio =
asset?.width && asset?.height ? asset.width / asset.height : item.kind === "image" ? 1 : 16 / 10;
const width = Math.round(Math.min(248, Math.max(132, 210 * ratio)));
+ const { picked, pick } = useAssetPick(item.id);
return (
<div
ref={tileRef}
- className="ai-chat-asset group/card relative shrink-0 cursor-grab overflow-hidden rounded-xl border border-border bg-muted transition-colors hover:border-input"
+ className={cn(
+ "ai-chat-asset group/card relative shrink-0 cursor-grab overflow-hidden rounded-xl border border-border bg-muted transition-colors hover:border-input",
+ FOCUS_RING,
+ picked && PICKED_RING,
+ )}
style={{ width, aspectRatio: ratio }}
+ // A click picks the card, the ring every panel tile wears; focusable,
+ // so a card clicked a moment ago still answers ⌘C / Ctrl+C once the
+ // pointer has moved on.
+ tabIndex={0}
+ onClick={(e) => {
+ if (!(e.target as Element).closest("button")) pick();
+ }}
title={`${ref.name} — double-click to expand · drag to the timeline`}
{...dragProps(item, asset)}
onDoubleClick={() => expandRef(item, asset)}
@@ -310,8 +323,16 @@ function AudioCard({ item, asset }: ChatCardProps) {
const url = ref.url;
return () => usePreviewAudio.getState().stop(url);
}, [ref.url]);
+ const { picked, pick } = useAssetPick(item.id);
return (
- <div className="ai-chat-asset w-full" onDoubleClick={() => expandRef(item, asset)}>
+ <div
+ className={cn("ai-chat-asset w-full rounded-[7px]", FOCUS_RING, picked && PICKED_RING)}
+ tabIndex={0}
+ onClick={(e) => {
+ if (!(e.target as Element).closest("button")) pick();
+ }}
+ onDoubleClick={() => expandRef(item, asset)}
+ >
<AudioRow
name={ref.name}
duration={ref.duration ?? 0}
@@ -338,9 +359,18 @@ const DOC_PREVIEW_CHARS = 1200;
function DocCard({ item, asset }: ChatCardProps) {
const { text, failed } = useDocText(item.url);
const clipped = text !== null && text.length > DOC_PREVIEW_CHARS;
+ const { picked, pick } = useAssetPick(item.id);
return (
<div
- className="ai-chat-asset group/card relative w-full max-w-[280px] cursor-default overflow-hidden rounded-xl border border-border bg-background transition-colors hover:border-input"
+ className={cn(
+ "ai-chat-asset group/card relative w-full max-w-[280px] cursor-default overflow-hidden rounded-xl border border-border bg-background transition-colors hover:border-input",
+ FOCUS_RING,
+ picked && PICKED_RING,
+ )}
+ tabIndex={0}
+ onClick={(e) => {
+ if (!(e.target as Element).closest("button")) pick();
+ }}
title={`${item.name} — double-click to expand`}
{...dragProps(item, asset)}
onDoubleClick={() => expandRef(item, asset)}
diff --git a/site/src/cut/lib/assetPick.ts b/site/src/cut/lib/assetPick.ts
index 6333ae71..aff64565 100644
--- a/site/src/cut/lib/assetPick.ts
+++ b/site/src/cut/lib/assetPick.ts
@@ -35,6 +35,10 @@ export function useAssetPick(assetId: string): { picked: boolean; pick: () => vo
/** The ring a picked tile wears. One class string so the tabs cannot drift. */
export const PICKED_RING = "ring-2 ring-[#0a84ff] ring-offset-1 ring-offset-card";
+/** The same ring on keyboard focus, for a tile that is its own focus target.
+ * Spelled out so the classes are there for the build to see. */
+export const FOCUS_RING =
+ "outline-none focus-visible:ring-2 focus-visible:ring-[#0a84ff] focus-visible:ring-offset-1 focus-visible:ring-offset-card";
/** Arrow-key navigation across a grid of pickable tiles. Mark each tile's
* button with `data-pick-id` and put this handler on the grid container: while
diff --git a/site/src/cut/lib/assetRef.ts b/site/src/cut/lib/assetRef.ts
index 8ce4ed5e..cb3ba702 100644
--- a/site/src/cut/lib/assetRef.ts
+++ b/site/src/cut/lib/assetRef.ts
@@ -459,6 +459,13 @@ export const EMPTY_LIBRARY: LibraryData = { assets: [], folders: [], templates:
* typeface file in a prompt, so they stay out of the handle numbering. Baked
* mattes (origin "matte") are a cutout's internals the same way — numbering
* them would shift every later @v handle when a bake lands. */
+/** The project assets kept out of the candidates — chat-owned files and
+ * mattes — as refs, so a token copied off one of their tiles still resolves
+ * where it is pasted. */
+export function unlistedRefs(assets: MediaAsset[]): AssetRef[] {
+ return assets.filter((a) => a.origin === "chat" || a.origin === "matte").map(refFromAsset);
+}
+
export function projectRefs(assets: MediaAsset[]): AssetRef[] {
const counters = { v: 0, i: 0, a: 0 };
return assets
@@ -1033,7 +1040,9 @@ export function collectRefs(
chips: AssetRef[],
candidates: AssetRef[]
): { refs: AssetRef[]; text: string } {
- const parsed = parseMentions(text, candidates);
+ // An attached ref resolves its own token: a chat-owned file is never a
+ // candidate, and rides as a chip from the paste that named it.
+ const parsed = parseMentions(text, [...chips, ...candidates]);
// Re-resolve each ref's short handle from the live candidates — chips from
// drags predate handle assignment, and the model reads handles to talk
// about attachments ("v2") — and its URL from the live asset: a chip