@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 56 lines 2.59 KB
Raw
1 import { pasteTextToFocusComposer, shouldRedirectInputToComposer } from "./composerFocusRedirect";
2 import { readPastedComposerContext } from "../composerInlineTokenPaste";
3 import { isPasteAsTextShortcut } from "@t3tools/client-runtime/text-paste";
4 import { useEffect, useRef } from "react";
5 import { isMacPlatform } from "../../lib/utils";
6 import { isCommandPaletteOpen } from "../../commandPaletteBus";
7 import { getTerminalFocusOwner } from "../../lib/terminalFocus";
8 import { isInSideChatPane } from "../../lib/sideChatPane";
9 import type { ThreadId } from "@t3tools/contracts";
10 import type { ComposerHandleRef } from "../../composerHandleContext";
11 /**
12 * Paste-to-focus: the resting composer blurs on a click into the timeline, so a paste that
13 * follows has no editable target and would be dropped. This sends it to the composer instead.
14 */
15 export function usePasteToFocusComposer(
16 activeThreadId: ThreadId | null,
17 composerRef: ComposerHandleRef,
18 ) {
19 const pasteAsTextShortcutUntilRef = useRef(0);
20 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- attaches window keydown and paste capture listeners that route stray pastes into the composer
21 useEffect(() => {
22 if (!activeThreadId) return;
23 const keyHandler = (event: KeyboardEvent) => {
24 if (
25 shouldRedirectInputToComposer(event) &&
26 isPasteAsTextShortcut(event, isMacPlatform(navigator.platform))
27 ) {
28 pasteAsTextShortcutUntilRef.current = Date.now() + 1_000;
29 }
30 };
31 const handler = (event: ClipboardEvent) => {
32 if (isCommandPaletteOpen() || isInSideChatPane(event.target)) return;
33 if (getTerminalFocusOwner() !== null) return;
34 if (composerRef.current?.isModelPickerOpen()) return;
35 const text = pasteTextToFocusComposer(event);
36 const clipboardData = event.clipboardData;
37 if (text === null || clipboardData === null) return;
38 const bypassAutoAttachment = Date.now() <= pasteAsTextShortcutUntilRef.current;
39 pasteAsTextShortcutUntilRef.current = 0;
40 if (
41 ((readPastedComposerContext(clipboardData)?.records.length ?? 0) === 0 &&
42 composerRef.current?.pasteTextAtEnd(text, { bypassAutoAttachment })) ||
43 composerRef.current?.insertTextAtEnd(text, { clipboardData })
44 ) {
45 event.preventDefault();
46 event.stopPropagation();
47 }
48 };
49 window.addEventListener("keydown", keyHandler, true);
50 window.addEventListener("paste", handler, true);
51 return () => {
52 window.removeEventListener("keydown", keyHandler, true);
53 window.removeEventListener("paste", handler, true);
54 };
55 }, [activeThreadId, composerRef]);
56 }