diff --git a/src/terminal.tsx b/src/terminal.tsx index 4c3ea0f..eaaeaad 100644 --- a/src/terminal.tsx +++ b/src/terminal.tsx @@ -1,104 +1,83 @@ -// terminal.tsx // src/components/Terminal.tsx import React, { useEffect, useRef } from 'react'; import { Terminal } from 'xterm'; import { FitAddon } from 'xterm-addon-fit'; import 'xterm/css/xterm.css'; -const useTerminalResize = (terminalRef: React.RefObject, fitAddon: FitAddon, newWindow: Window | null) => { + +const useTerminalResize = (terminalRef: React.RefObject, fitAddon: FitAddon) => { useEffect(() => { - const handleResize = () => { - if (fitAddon && terminalRef.current) { - fitAddon.fit(); - } - }; - - if (newWindow) { - newWindow.addEventListener('resize', handleResize); - } else { - window.addEventListener('resize', handleResize); + const handleResize = () => { + if (fitAddon && terminalRef.current) { + fitAddon.fit(); } + }; + + window.addEventListener('resize', handleResize); + + // Fit the terminal initially + handleResize(); + + return () => { + window.removeEventListener('resize', handleResize); + }; + }, [terminalRef, fitAddon]); + }; - handleResize(); - return () => { - if (newWindow) { - newWindow.removeEventListener('resize', handleResize); - } else { - window.removeEventListener('resize', handleResize); - } - }; - }, [terminalRef, fitAddon, newWindow]); -}; - -function TerminalComponent(p: { websocket: string | null }) { - const { websocket } = p; - const terminalRef = useRef(null); +function TerminalComponent (p: {websocket: string|null}) { + const {websocket} = p + const terminalRef = useRef(null); const terminal = useRef(null); const fitAddon = useRef(null); - const newWindow = useRef(null); useEffect(() => { - newWindow.current = window.open('', '', 'width=800,height=600'); - - if (newWindow.current) { - newWindow.current.document.title = "Terminal"; - const terminalDiv = newWindow.current.document.createElement('div'); - terminalDiv.style.width = '100%'; - terminalDiv.style.height = '100%'; - newWindow.current.document.body.appendChild(terminalDiv); - - terminalRef.current = terminalDiv; - - if (terminalRef.current) { - terminal.current = new Terminal(); - fitAddon.current = new FitAddon(); - terminal.current.loadAddon(fitAddon.current); - terminal.current.open(terminalRef.current); - fitAddon.current.fit(); - - if (websocket == null) { - return () => { - terminal.current?.dispose(); - newWindow.current?.close(); - }; - } - - const socket = new WebSocket(websocket); - - socket.onerror = (ev) => { - console.log(ev); - }; - - socket.addEventListener('open', () => { - socket.send(JSON.stringify({ CommandType: 'resize', Arguments: `${terminal.current?.cols}x${terminal.current?.rows}` })); - }); - - terminal.current.onResize(({ cols, rows }) => { - socket.send(JSON.stringify({ CommandType: 'resize', Arguments: `${cols}x${rows}` })); - }); - - socket.addEventListener('message', (event) => { - terminal.current?.write(JSON.parse(event.data)); - }); - - terminal.current.onData((data) => { - socket.send(JSON.stringify({ CommandType: 'insert', Arguments: data })); - }); - - return () => { - console.log("closed websocket"); + if (terminalRef.current) { + terminal.current = new Terminal(); + fitAddon.current = new FitAddon(); + terminal.current.loadAddon(fitAddon.current); + terminal.current.open(terminalRef.current); + fitAddon.current.fit(); + if (websocket == null){ + + return ()=>{ terminal.current?.dispose(); - socket.close(); - newWindow.current?.close(); - }; + } } + + const socket = new WebSocket(websocket); + + socket.onerror = (ev)=>{ + console.log(ev) + } + + socket.addEventListener('open', () => { + socket.send(JSON.stringify({CommandType: 'resize', Arguments: `${terminal.current?.cols}x${terminal.current?.rows}`})); + }); + + terminal.current.onResize(({cols, rows})=>{ + socket.send(JSON.stringify({CommandType: 'resize', Arguments: `${cols}x${rows}`})); + }) + + socket.addEventListener('message', (event) => { + terminal.current?.write(JSON.parse(event.data)); + }); + + terminal.current.onData((data) => { + socket.send(JSON.stringify({CommandType: 'insert', Arguments: data})); + }); + + return () => { + console.log("closed websocket") + terminal.current?.dispose(); + socket.close(); + }; } }, []); - useTerminalResize(terminalRef, fitAddon.current!, newWindow.current); + useTerminalResize(terminalRef, fitAddon.current!) - return null; + return
; }; export default TerminalComponent;