From 5fc4444f5a42972b00e8ebb054afff62958c471d Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Sat, 3 Oct 2020 19:21:11 -0700 Subject: [PATCH] Refit terminal when screen is resized; closes #2365 --- .../scripts/components/server/Console.tsx | 22 ++++++++++++------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/resources/scripts/components/server/Console.tsx b/resources/scripts/components/server/Console.tsx index 3840934b..0c04d95e 100644 --- a/resources/scripts/components/server/Console.tsx +++ b/resources/scripts/components/server/Console.tsx @@ -1,4 +1,4 @@ -import React, { useCallback, useEffect, useMemo, useState } from 'react'; +import React, { useEffect, useMemo, useRef } from 'react'; import { ITerminalOptions, Terminal } from 'xterm'; import * as TerminalFit from 'xterm/lib/addons/fit/fit'; import SpinnerOverlay from '@/components/elements/SpinnerOverlay'; @@ -7,6 +7,8 @@ import styled from 'styled-components/macro'; import { usePermissions } from '@/plugins/usePermissions'; import tw from 'twin.macro'; import 'xterm/dist/xterm.css'; +import useEventListener from '@/plugins/useEventListener'; +import { debounce } from 'debounce'; const theme = { background: 'transparent', @@ -51,9 +53,7 @@ const TerminalDiv = styled.div` export default () => { const TERMINAL_PRELUDE = '\u001b[1m\u001b[33mcontainer@pterodactyl~ \u001b[0m'; - const [ terminalElement, setTerminalElement ] = useState(null); - - const useRef = useCallback(node => setTerminalElement(node), []); + const ref = useRef(null); const terminal = useMemo(() => new Terminal({ ...terminalProps }), []); const { connected, instance } = ServerContext.useStoreState(state => state.socket); const [ canSendCommands ] = usePermissions([ 'control.console' ]); @@ -80,8 +80,8 @@ export default () => { }; useEffect(() => { - if (connected && terminalElement && !terminal.element) { - terminal.open(terminalElement); + if (connected && ref.current && !terminal.element) { + terminal.open(ref.current); // @see https://github.com/xtermjs/xterm.js/issues/2265 // @see https://github.com/xtermjs/xterm.js/issues/2230 @@ -98,7 +98,13 @@ export default () => { return true; }); } - }, [ terminal, connected, terminalElement ]); + }, [ terminal, connected ]); + + const fit = debounce(() => { + TerminalFit.fit(terminal); + }, 100); + + useEventListener('resize', () => fit()); useEffect(() => { if (connected && instance) { @@ -135,7 +141,7 @@ export default () => { maxHeight: '32rem', }} > - + {canSendCommands &&