Optimize pasting (faster, works better with vim)

This commit is contained in:
Karmaa
2025-03-21 16:02:01 -05:00
parent d1db9b7972
commit 652b8d2da2

View File

@@ -115,8 +115,6 @@ export const NewTerminal = forwardRef(({ hostConfig, isVisible, setIsNoAuthHidde
terminalInstance.current.write(decoder.decode(new Uint8Array(data))); terminalInstance.current.write(decoder.decode(new Uint8Array(data)));
}); });
let isPasting = false;
let buffer = ""; let buffer = "";
let bufferTimeout = null; let bufferTimeout = null;
@@ -131,38 +129,25 @@ export const NewTerminal = forwardRef(({ hostConfig, isVisible, setIsNoAuthHidde
} }
}); });
terminalInstance.current.attachCustomKeyEventHandler((event) => { terminalInstance.current.attachCustomKeyEventHandler(async (event) => {
if ((event.ctrlKey || event.metaKey) && event.key === "v") { if ((event.ctrlKey || event.metaKey) && event.key === "v") {
if (isPasting) return false;
isPasting = true;
event.preventDefault(); event.preventDefault();
navigator.clipboard.readText().then((text) => { if (!socketRef.current) return false;
text = text.replace(/\r\n/g, "\n").replace(/\r/g, "\n");
const lines = text.split("\n");
if (socketRef.current) { try {
let index = 0; const text = await navigator.clipboard.readText();
const lines = text.replace(/\r\n/g, "\n").replace(/\r/g, "\n").split("\n");
const sendLine = () => { await Promise.all(lines.map(line => {
if (index < lines.length) { return new Promise(resolve => {
socketRef.current.emit("data", lines[index] + "\r"); socketRef.current.emit("data", line + "\r");
index++; resolve();
setTimeout(sendLine, 10); });
} else { }));
isPasting = false; } catch (err) {
}
};
sendLine();
} else {
isPasting = false;
}
}).catch((err) => {
console.error("Failed to read clipboard contents:", err); console.error("Failed to read clipboard contents:", err);
isPasting = false; }
});
return false; return false;
} }