Open source CapCut alternative.
TypeScript
fix(site): @here becomes the time as it is typed
David Le1b7b038bd3fc
diff --git a/site/src/cut/components/AiPanel.tsx b/site/src/cut/components/AiPanel.tsxindex 05eb4237..cb0909ac 100644--- a/site/src/cut/components/AiPanel.tsx+++ b/site/src/cut/components/AiPanel.tsx@@ -97,7 +97,8 @@ import { useCreditsRecheck, useOutOfCredits } from "@/cut/lib/hosted";import { cutChatLive, dropPiSession, foldIntoCutChat, hydratePiSession, judgeEngineSkill, readPiSession, streamCutChat, triageQueuedMessages } from "@/cut/lib/pi/cutAgent";import { dueForRetriage, toolProgress } from "@/cut/lib/queueTriage";import { registerQueueSink } from "@/cut/lib/chatQueue";-import { syncTimes, type TimeMark } from "@/cut/lib/timeMark";+import { previewAt } from "@/cut/lib/playhead";+import { resolvePlayheadTokens, syncTimes, type TimeMark } from "@/cut/lib/timeMark";import { productionDeps } from "@/cut/lib/pi/prodDeps";import { withChatProject } from "@/cut/lib/projectChatTools";import { runAiTool } from "@/cut/lib/aiTools";@@ -1675,10 +1676,15 @@ function ChatSession({// Moments are re-read here too: the cut can have moved since the token was// written, and a token typed by hand picks up its mark now.const doc = useEditor.getState();- const synced = syncTimes(text.trim(), times, {- clips: doc.clips,- audioClips: doc.audioClips,- });+ const cut = { clips: doc.clips, audioClips: doc.audioClips };+ // A `@here` still standing in the text — typed and sent in one breath,+ // never completed by a following character — takes the moment now.+ const written = resolvePlayheadTokens(text.trim(), previewAt(), cut);+ const synced = syncTimes(+ written?.text ?? text.trim(),+ [...times, ...(written?.marks ?? [])],+ cut,+ );const body = synced.text;const { refs: all } = collectRefs(body, attachments, candidates);if (!body && all.length === 0) return;diff --git a/site/src/cut/components/AssetRefs.tsx b/site/src/cut/components/AssetRefs.tsxindex eb1f4780..63e181f4 100644--- a/site/src/cut/components/AssetRefs.tsx+++ b/site/src/cut/components/AssetRefs.tsx@@ -24,6 +24,7 @@ import {formatMark,markAt,parseMark,+ resolvePlayheadTokens,PLAYHEAD_HANDLE,PLAYHEAD_NAME,splitTimes,@@ -1545,9 +1546,27 @@ export function MentionTextarea({value={value}onChange={(e) => {setDismissed(null);- onChange(e.target.value);- setCaret(e.target.selectionStart ?? 0);- setSelEnd(e.target.selectionEnd ?? e.target.selectionStart ?? 0);+ const v = e.target.value;+ const c = e.target.selectionStart ?? v.length;+ // `@here` typed straight through becomes the moment the word names —+ // the menu is one way to take it, the word itself is the other.+ if (onInsertTime) {+ const { clips, audioClips } = useEditor.getState();+ const written = resolvePlayheadTokens(v, previewAt(), { clips, audioClips }, c);+ if (written) {+ for (const mark of written.marks) onInsertTime(mark);+ onChange(written.text);+ setCaret(written.caret);+ setSelEnd(written.caret);+ requestAnimationFrame(() =>+ taRef.current?.setSelectionRange(written.caret, written.caret)+ );+ return;+ }+ }+ onChange(v);+ setCaret(c);+ setSelEnd(e.target.selectionEnd ?? c);}}onFocus={() => setFocused(true)}onBlur={() => setFocused(false)}diff --git a/site/src/cut/lib/timeMark.test.ts b/site/src/cut/lib/timeMark.test.tsindex 5e1ed123..eb507922 100644--- a/site/src/cut/lib/timeMark.test.ts+++ b/site/src/cut/lib/timeMark.test.ts@@ -1,6 +1,14 @@import { describe, expect, test } from "bun:test";import { retimeOf } from "@donkeycut/effects-kit";-import { formatMark, markAt, markTime, parseMark, splitTimes, syncTimes } from "./timeMark";+import {+ formatMark,+ markAt,+ markTime,+ parseMark,+ resolvePlayheadTokens,+ splitTimes,+ syncTimes,+} from "./timeMark";import { splitTimelineItems } from "./timelineItems";import type { AudioClip, VideoClip } from "./types";@@ -198,6 +206,49 @@ describe("a project with no picture", () => {});});+describe("the word becomes the time", () => {+ test("once a character follows it", () => {+ const s = cut();+ const typed = "@here hi";+ const done = resolvePlayheadTokens(typed, 12, s, typed.length);+ expect(done?.text).toBe("@0:12 hi");+ expect(done?.marks[0].clipId).toBe("b");+ // The caret sat at the end, and moves with the text under it.+ expect(done?.caret).toBe("@0:12 hi".length);+ });++ test("and the caret keeps its place when the time is wider than the word", () => {+ const typed = "@here hi";+ const done = resolvePlayheadTokens(typed, 3723, { clips: [] }, typed.length);+ expect(done?.text).toBe("@1:02:03 hi");+ expect(done?.caret).toBe("@1:02:03 hi".length);+ });++ test("but not while it is still being typed", () => {+ expect(resolvePlayheadTokens("@here", 12, cut(), 5)).toBe(null);+ expect(resolvePlayheadTokens("@her", 12, cut(), 4)).toBe(null);+ // Sent as it stands, with no caret in play, it resolves.+ expect(resolvePlayheadTokens("@here", 12, cut())?.text).toBe("@0:12");+ });++ test("by either name, anywhere in the line, and never mid-word", () => {+ expect(resolvePlayheadTokens("cut @playhead now", 12, cut(), 0)?.text).toBe("cut @0:12 now");+ expect(resolvePlayheadTokens("zoom @Here, then out", 12, cut(), 0)?.text).toBe(+ "zoom @0:12, then out",+ );+ expect(resolvePlayheadTokens("@heron.mp4 is loud", 12, cut(), 0)).toBe(null);+ expect(resolvePlayheadTokens("email [email protected]", 12, cut(), 0)).toBe(null);+ });++ test("twice in one line, with the caret landing past both", () => {+ const typed = "@here and @here x";+ const done = resolvePlayheadTokens(typed, 12, cut(), typed.length);+ expect(done?.text).toBe("@0:12 and @0:12 x");+ expect(done?.marks).toHaveLength(2);+ expect(done?.caret).toBe("@0:12 and @0:12 x".length);+ });+});+describe("syncTimes", () => {test("rewrites every token to where its moment sits now", () => {const s = cut();diff --git a/site/src/cut/lib/timeMark.ts b/site/src/cut/lib/timeMark.tsindex a587589c..7c7fe627 100644--- a/site/src/cut/lib/timeMark.ts+++ b/site/src/cut/lib/timeMark.ts@@ -36,6 +36,42 @@ export const TIME_TOKEN_RE = /@(\d{1,3}:[0-5]\d(?::[0-5]\d)?(?:\.\d{1,2})?)(?![\export const PLAYHEAD_HANDLE = "here";export const PLAYHEAD_NAME = "playhead";+/** A written-out `@here` / `@playhead`, once something follows it. */+const PLAYHEAD_TOKEN_RE = new RegExp(+ `(^|[\\s([])@(${PLAYHEAD_HANDLE}|${PLAYHEAD_NAME})(?![\\w-])`,+ "gi"+);++/**+ * Swap every finished `@here` for the moment it names, so the word becomes the+ * time whether the user took it off the menu or typed straight through it.+ * `caret` is where they are: a token ending right there is still being typed+ * and is left alone until the next character lands. Null when nothing changed.+ */+export function resolvePlayheadTokens(+ text: string,+ at: number,+ s: TimeMarkState,+ caret?: number+): { text: string; marks: TimeMark[]; caret: number } | null {+ const marks: TimeMark[] = [];+ let out = "";+ let last = 0;+ let shift = 0;+ for (const m of text.matchAll(PLAYHEAD_TOKEN_RE)) {+ const start = m.index + (m[1] ?? "").length;+ const end = start + 1 + m[2].length;+ if (end === caret) continue;+ const mark = markAt(at, s);+ marks.push(mark);+ out += text.slice(last, start) + mark.token;+ if (caret !== undefined && end <= caret) shift += mark.token.length - (end - start);+ last = end;+ }+ if (marks.length === 0) return null;+ return { text: out + text.slice(last), marks, caret: (caret ?? 0) + shift };+}+/** How far outside a clip's trim a source second may sit and still count as* inside it — a rounding tolerance, not a reach. */const TRIM_EPS = 1e-6;