From 652b8d2da2e9ff631c6179b41ab2d1ce862fa821 Mon Sep 17 00:00:00 2001 From: Karmaa Date: Fri, 21 Mar 2025 16:02:01 -0500 Subject: [PATCH] Optimize pasting (faster, works better with vim) --- src/apps/ssh/Terminal.jsx | 41 +++++++++++++-------------------------- 1 file changed, 13 insertions(+), 28 deletions(-) diff --git a/src/apps/ssh/Terminal.jsx b/src/apps/ssh/Terminal.jsx index 4fbf0600..f2498b0a 100644 --- a/src/apps/ssh/Terminal.jsx +++ b/src/apps/ssh/Terminal.jsx @@ -115,8 +115,6 @@ export const NewTerminal = forwardRef(({ hostConfig, isVisible, setIsNoAuthHidde terminalInstance.current.write(decoder.decode(new Uint8Array(data))); }); - let isPasting = false; - let buffer = ""; 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 (isPasting) return false; - isPasting = true; - event.preventDefault(); - navigator.clipboard.readText().then((text) => { - text = text.replace(/\r\n/g, "\n").replace(/\r/g, "\n"); - const lines = text.split("\n"); + if (!socketRef.current) return false; - if (socketRef.current) { - let index = 0; + try { + const text = await navigator.clipboard.readText(); + const lines = text.replace(/\r\n/g, "\n").replace(/\r/g, "\n").split("\n"); - const sendLine = () => { - if (index < lines.length) { - socketRef.current.emit("data", lines[index] + "\r"); - index++; - setTimeout(sendLine, 10); - } else { - isPasting = false; - } - }; - - sendLine(); - } else { - isPasting = false; - } - }).catch((err) => { + await Promise.all(lines.map(line => { + return new Promise(resolve => { + socketRef.current.emit("data", line + "\r"); + resolve(); + }); + })); + } catch (err) { console.error("Failed to read clipboard contents:", err); - isPasting = false; - }); + } return false; }