Skip to content

Open source CapCut alternative.

TypeScript

feat(site): dictation confirms on Enter, cancels on Escape

David Lee4253b0c12ff
diff --git a/site/src/cut/components/AiPanel.tsx b/site/src/cut/components/AiPanel.tsx
index a4b9a730..4eccbf53 100644
--- a/site/src/cut/components/AiPanel.tsx
+++ b/site/src/cut/components/AiPanel.tsx
@@ -344,11 +344,25 @@ function ChatSession({
onModelChange: (id: string) => void;
}) {
const [input, setInput] = useState("");
+ const composerRef = useRef<HTMLTextAreaElement>(null);
// Live dictation → drops the finished transcript into the composer, appended
// after whatever the user had already typed.
const mic = useMicTranscription((text) =>
setInput((prev) => (prev.trim() ? `${prev.trim()} ${text}` : text))
);
+ // When dictation ends the composer remounts; put the caret back at the end
+ // so Enter (confirm) → Enter (send) chains without a click.
+ const micWasActive = useRef(false);
+ useEffect(() => {
+ if (micWasActive.current && mic.state === "idle") {
+ const el = composerRef.current;
+ if (el) {
+ el.focus();
+ el.setSelectionRange(el.value.length, el.value.length);
+ }
+ }
+ micWasActive.current = mic.state !== "idle";
+ }, [mic.state]);
const [attachments, setAttachments] = useState<AssetRef[]>([]);
const candidates = useRefCandidates();
// Any OS file drag over the window hints the composer as a drop target;
@@ -611,6 +625,7 @@ function ChatSession({
candidates={candidates}
submitKey="enter"
menuSide="top"
+ inputRef={composerRef}
onSubmit={() => send(input)}
/>
<div className="flex items-center gap-1 px-1.5 pb-1.5">
diff --git a/site/src/cut/components/AssetRefs.tsx b/site/src/cut/components/AssetRefs.tsx
index 9b5d5360..03dbf66b 100644
--- a/site/src/cut/components/AssetRefs.tsx
+++ b/site/src/cut/components/AssetRefs.tsx
@@ -1,6 +1,6 @@
"use client";
-import { useEffect, useLayoutEffect, useMemo, useRef, useState } from "react";
+import { useEffect, useLayoutEffect, useMemo, useRef, useState, type RefObject } from "react";
import { Check, Copy, FileText, X } from "lucide-react";
import {
highlightMentions,
@@ -359,6 +359,7 @@ export function MentionTextarea({
className,
rows,
autoGrow = false,
+ inputRef,
}: {
value: string;
onChange: (v: string) => void;
@@ -374,6 +375,8 @@ export function MentionTextarea({
caller's `max-h-*`). Leave off when the caller wants a fixed or
manually resizable box. */
autoGrow?: boolean;
+ /** Caller's handle on the underlying textarea (e.g. to restore focus). */
+ inputRef?: RefObject<HTMLTextAreaElement | null>;
}) {
const taRef = useRef<HTMLTextAreaElement>(null);
const backdropRef = useRef<HTMLDivElement>(null);
@@ -482,7 +485,10 @@ export function MentionTextarea({
)}
</div>
<textarea
- ref={taRef}
+ ref={(el) => {
+ taRef.current = el;
+ if (inputRef) inputRef.current = el;
+ }}
className={cn(className, "relative block bg-transparent")}
rows={rows}
placeholder={placeholder}
diff --git a/site/src/cut/components/MicDictation.tsx b/site/src/cut/components/MicDictation.tsx
index 0055bdcd..bf66f2d9 100644
--- a/site/src/cut/components/MicDictation.tsx
+++ b/site/src/cut/components/MicDictation.tsx
@@ -1,5 +1,6 @@
"use client";
+import { useEffect } from "react";
import { Check, Mic, X } from "lucide-react";
import { cn } from "@/lib/utils";
import { useMicTranscription, type MicController } from "@/cut/lib/micTranscribe";
@@ -33,6 +34,25 @@ export function DictationBody({
}) {
const shown = [text.trim(), mic.partial].filter(Boolean).join(" ");
const finishing = mic.state === "finishing";
+ // Keyboard control while dictating: Enter confirms, Escape cancels. Capture
+ // on window so an input still focused under the overlay never sees the key.
+ useEffect(() => {
+ if (mic.state !== "recording") return;
+ const onKey = (e: KeyboardEvent) => {
+ if (e.isComposing) return;
+ if (e.key === "Enter" && !e.shiftKey && !e.metaKey && !e.ctrlKey && !e.altKey) {
+ e.preventDefault();
+ e.stopPropagation();
+ void mic.confirm();
+ } else if (e.key === "Escape") {
+ e.preventDefault();
+ e.stopPropagation();
+ mic.cancel();
+ }
+ };
+ window.addEventListener("keydown", onKey, true);
+ return () => window.removeEventListener("keydown", onKey, true);
+ }, [mic.state, mic.confirm, mic.cancel]);
return (
<div className={cn("flex min-h-[72px] flex-col", className)}>
<div className="min-h-0 flex-1 overflow-y-auto px-3 pt-2.5">