From 793e9873a58b777a7224e89b13d47579fd69e941 Mon Sep 17 00:00:00 2001 From: Mathias Wagner Date: Wed, 11 Sep 2024 20:07:58 +0200 Subject: [PATCH] Implement clipboard synchronization in GuacamoleRenderer.jsx --- .../renderer/GuacamoleRenderer.jsx | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/client/src/pages/Servers/components/ViewContainer/renderer/GuacamoleRenderer.jsx b/client/src/pages/Servers/components/ViewContainer/renderer/GuacamoleRenderer.jsx index bce7024..19686fb 100644 --- a/client/src/pages/Servers/components/ViewContainer/renderer/GuacamoleRenderer.jsx +++ b/client/src/pages/Servers/components/ViewContainer/renderer/GuacamoleRenderer.jsx @@ -27,6 +27,48 @@ const GuacamoleRenderer = ({ session, disconnectFromServer, pve }) => { } }; + const sendClipboardToServer = (text) => { + if (clientRef.current && text) { + const stream = clientRef.current.createClipboardStream("text/plain"); + const writer = new Guacamole.StringWriter(stream); + writer.sendText(text); + writer.sendEnd(); + } + }; + + const handleClipboardEvents = () => { + if (clientRef.current) { + clientRef.current.onclipboard = (stream, mimetype) => { + if (mimetype === "text/plain") { + const reader = new Guacamole.StringReader(stream); + let clipboardData = ""; + + reader.ontext = (text) => clipboardData += text; + reader.onend = async () => { + try { + await navigator.clipboard.writeText(clipboardData); + } catch (ignored) { + } + }; + } + }; + + let cachedClipboard = ""; + const intervalId = setInterval(async() => { + try { + const text = await navigator.clipboard.readText(); + if (text !== cachedClipboard) { + cachedClipboard = text; + sendClipboardToServer(text); + } + } catch (ignored) { + } + }, 500); + + return () => clearInterval(intervalId); + } + }; + const connect = () => { if (!sessionToken || clientRef.current) { return; @@ -68,6 +110,9 @@ const GuacamoleRenderer = ({ session, disconnectFromServer, pve }) => { disconnectFromServer(session.id); } }; + + handleClipboardEvents(); + return () => { client.disconnect(); clientRef.current = null;