mirror of
https://github.com/binaricat/Netcatty.git
synced 2026-11-20 23:20:46 +00:00
* fix(#3398): automated AI fix * fix: address Codex review on PR #3400 * fix: address Codex review on PR #3400 * fix: address Codex review on PR #3400 * fix: address Codex review on PR #3400 * fix: address Codex review on PR #3400 * fix: address Codex review on PR #3400 * fix: address Codex review on PR #3400 * fix: address Codex review on PR #3400 * fix: address Codex review on PR #3400 * fix: address Codex review on PR #3400 * fix(terminal): protect popup and sanitized multiline pastes * fix(terminal): preserve input state across paste confirmation * fix(terminal): retain serial character-mode paste echo * test(terminal): cover paced input write options * fix(terminal): retain telnet echo for paced pastes * test(terminal): scope serial buffer assertion to serial sessions * fix(terminal): align runtime write options with bridge contract * fix(terminal): preserve bookkeeping for all paced pastes * fix(terminal): record confirmed paste lines in order * fix: record paced paste commands after transport writes * fix: omit paste receipts for filtered input * fix: cancel superseded paste batches across input paths * fix: classify each acknowledged paste line before recording * fix: stop paced paste when editing buffered serial input * fix: preserve recording pause boundaries and serial edit cancellation * fix: treat paced terminal pastes as user input * fix: preserve paste drafts until write and snippet broadcast origin * fix: retain filtered paste drafts and invalidate urgent input ownership * fix: broadcast paced user paste only on guarded line receipts --------- Co-authored-by: netcatty-bot <308658023+netcatty-bot@users.noreply.github.com> Co-authored-by: bincxz <16399091+binaricat@users.noreply.github.com>
142 lines
5.4 KiB
TypeScript
142 lines
5.4 KiB
TypeScript
import type { Terminal as XTerm } from "@xterm/xterm";
|
|
import type React from "react";
|
|
import { useEffect } from "react";
|
|
|
|
import { requestMultilinePasteConfirm } from "../../../application/state/multilinePasteConfirmStore";
|
|
import { netcattyBridge } from "../../../infrastructure/services/netcattyBridge";
|
|
import { logger } from "../../../lib/logger";
|
|
import type { TerminalSession } from "../../../types";
|
|
import type { RemoteClipboardImageUploadResult } from "../clipboardImagePaste";
|
|
import type { MultilinePasteConfirmGate } from "../terminalClipboardPaste";
|
|
import { handleTerminalClipboardPaste } from "../terminalClipboardPaste";
|
|
|
|
interface UseTerminalFilePasteOptions {
|
|
isLocalConnection: boolean;
|
|
status: TerminalSession["status"];
|
|
termRef: React.MutableRefObject<XTerm | null>;
|
|
sessionRef: React.MutableRefObject<string | null>;
|
|
terminalBackend: {
|
|
writeToSession: (sessionId: string, data: string, options?: { automated?: boolean; sensitive?: boolean }) => void;
|
|
};
|
|
isSensitiveInput?: () => boolean;
|
|
scrollOnPasteRef?: React.RefObject<boolean>;
|
|
onPasteData?: (data: string, options?: { lineDelayMs?: number }) => boolean | void;
|
|
scrollToBottomAfterProgrammaticInput: (data: string) => void;
|
|
containerRef: React.RefObject<HTMLDivElement | null>;
|
|
/** Remote sessions only: auto-upload a clipboard image on paste. */
|
|
autoUploadClipboardImage?: boolean;
|
|
/** Multi-line paste confirmation gate (#3398); undefined keeps confirm off. */
|
|
multilinePasteConfirmRef?: React.RefObject<Omit<MultilinePasteConfirmGate, "requestConfirm"> | undefined>;
|
|
getRemoteCwd?: () => Promise<string | null | undefined>;
|
|
onClipboardImageUploadResult?: (result: RemoteClipboardImageUploadResult) => void;
|
|
}
|
|
|
|
export function useTerminalFilePaste({
|
|
isLocalConnection,
|
|
status,
|
|
termRef,
|
|
sessionRef,
|
|
terminalBackend,
|
|
isSensitiveInput,
|
|
scrollOnPasteRef,
|
|
onPasteData,
|
|
scrollToBottomAfterProgrammaticInput,
|
|
containerRef,
|
|
autoUploadClipboardImage = false,
|
|
multilinePasteConfirmRef,
|
|
getRemoteCwd,
|
|
onClipboardImageUploadResult,
|
|
}: UseTerminalFilePasteOptions) {
|
|
useEffect(() => {
|
|
const container = containerRef.current;
|
|
if (!container) return;
|
|
|
|
const handlePaste = (event: ClipboardEvent) => {
|
|
if (status !== "connected") return;
|
|
|
|
const bridge = netcattyBridge.get();
|
|
|
|
const wantsImageUpload =
|
|
autoUploadClipboardImage && !isLocalConnection && !!bridge?.readClipboardImage;
|
|
const canHandleLocalPaste =
|
|
isLocalConnection && !!(bridge?.readClipboardFiles || bridge?.hasClipboardImage);
|
|
// The multi-line paste confirmation (#3398) must also intercept plain
|
|
// remote keyboard pastes; otherwise xterm's default handler would send
|
|
// the lines without the safety dialog when neither image upload nor
|
|
// local file handling applies.
|
|
const shouldInterceptPaste =
|
|
wantsImageUpload
|
|
|| canHandleLocalPaste
|
|
|| !!multilinePasteConfirmRef?.current?.enabled;
|
|
if (!shouldInterceptPaste) return;
|
|
|
|
// ⚡ Must call preventDefault SYNCHRONOUSLY — the event lifecycle
|
|
// is synchronous; calling it after an await is too late and the
|
|
// browser will have already performed the default paste action.
|
|
event.preventDefault();
|
|
event.stopPropagation();
|
|
|
|
// Capture the pasted text synchronously: navigator.clipboard.readText()
|
|
// can be rejected (permissions / platform quirks) even though the event
|
|
// already carries the text, so prefer it and only fall back to the
|
|
// async clipboard API when the event has no text data.
|
|
const eventText = event.clipboardData?.getData("text/plain") ?? "";
|
|
|
|
void (async () => {
|
|
try {
|
|
const term = termRef.current;
|
|
if (!term) return;
|
|
await handleTerminalClipboardPaste({
|
|
bridge,
|
|
autoUploadClipboardImage: wantsImageUpload,
|
|
clipboardImageBridge: bridge ?? undefined,
|
|
confirmMultilinePaste: multilinePasteConfirmRef?.current
|
|
? { ...multilinePasteConfirmRef.current, requestConfirm: requestMultilinePasteConfirm }
|
|
: undefined,
|
|
// The confirm dialog can outlive the captured session (disconnect
|
|
// / auto-reconnect), so revalidate against the live session ref
|
|
// like the context-menu and shortcut callers do.
|
|
getCurrentSessionId: () => sessionRef.current,
|
|
getRemoteCwd,
|
|
isLocalConnection,
|
|
isSensitiveInput,
|
|
onClipboardImageUploadResult,
|
|
readClipboardText: async () => {
|
|
if (eventText) return eventText;
|
|
return navigator.clipboard.readText();
|
|
},
|
|
scrollOnPaste: scrollOnPasteRef?.current ?? false,
|
|
onPasteData,
|
|
sessionId: sessionRef.current,
|
|
terminalBackend,
|
|
term,
|
|
scrollToBottomAfterProgrammaticInput,
|
|
});
|
|
} catch (error) {
|
|
logger.error("Failed to handle file paste", error);
|
|
}
|
|
})();
|
|
};
|
|
|
|
container.addEventListener("paste", handlePaste, true);
|
|
return () => {
|
|
container.removeEventListener("paste", handlePaste, true);
|
|
};
|
|
}, [
|
|
autoUploadClipboardImage,
|
|
containerRef,
|
|
multilinePasteConfirmRef,
|
|
getRemoteCwd,
|
|
isLocalConnection,
|
|
isSensitiveInput,
|
|
onClipboardImageUploadResult,
|
|
onPasteData,
|
|
scrollOnPasteRef,
|
|
scrollToBottomAfterProgrammaticInput,
|
|
sessionRef,
|
|
status,
|
|
terminalBackend,
|
|
termRef,
|
|
]);
|
|
}
|