55 */
66import { Box , BoxProps } from "@chakra-ui/layout" ;
77import { useToken } from "@chakra-ui/react" ;
8- import React , { useEffect , useRef } from "react" ;
8+ import React , { useEffect , useMemo , useRef } from "react" ;
99import { Terminal } from "xterm" ;
1010import { FitAddon } from "xterm-addon-fit" ;
1111import "xterm/css/xterm.css" ;
1212import useActionFeedback from "../common/use-action-feedback" ;
1313import useIsUnmounted from "../common/use-is-unmounted" ;
14- import { backgroundColorTerm , defaultCodeFontSizePt } from "../deployment/misc" ;
14+ import { backgroundColorTerm } from "../deployment/misc" ;
1515import { EVENT_SERIAL_DATA , EVENT_SERIAL_RESET } from "../device/device" ;
1616import { parseTraceLine , useDevice } from "../device/device-hooks" ;
1717import { useSelection } from "../workbench/use-selection" ;
@@ -21,20 +21,16 @@ import "./xterm-custom.css";
2121import customKeyEventHandler from "./xterm-keyboard" ;
2222
2323interface XTermProps extends BoxProps {
24- fontSizePt ?: number ;
2524 tabOutRef : HTMLElement ;
25+ fontSizePt : number ;
2626}
2727
2828/**
2929 * xterm.js-based terminal.
3030 */
31- const XTerm = ( {
32- fontSizePt = defaultCodeFontSizePt ,
33- tabOutRef,
34- ...props
35- } : XTermProps ) => {
31+ const XTerm = ( { fontSizePt, tabOutRef, ...props } : XTermProps ) => {
3632 const ref = useRef < HTMLDivElement > ( null ) ;
37- useManagedTermimal ( ref , fontSizePt , tabOutRef ) ;
33+ useManagedTermimal ( ref , tabOutRef , fontSizePt ) ;
3834 return < Box { ...props } ref = { ref } backgroundColor = { backgroundColorTerm } /> ;
3935} ;
4036
@@ -48,16 +44,18 @@ const ptToPixelRatio = 96 / 72;
4844 */
4945const useManagedTermimal = (
5046 ref : React . RefObject < HTMLDivElement > ,
51- fontSizePt : number ,
52- tabOutRef : HTMLElement
47+ tabOutRef : HTMLElement ,
48+ fontSizePt : number
5349) : void => {
5450 const parent = ref . current ;
5551 const actionFeedback = useActionFeedback ( ) ;
5652 const codeFontFamily = useToken ( "fonts" , "code" ) ;
5753 const device = useDevice ( ) ;
5854 const isUnmounted = useIsUnmounted ( ) ;
5955 const [ , setSelection ] = useSelection ( ) ;
56+ const fitAddon = useMemo ( ( ) => new FitAddon ( ) , [ ] ) ;
6057 const currentTerminalRef = useCurrentTerminalRef ( ) ;
58+ const initialFontSizeRef = useRef < number > ( fontSizePt ) ;
6159
6260 useEffect ( ( ) => {
6361 if ( ! parent ) {
@@ -68,7 +66,7 @@ const useManagedTermimal = (
6866 }
6967 const terminal = new Terminal ( {
7068 fontFamily : codeFontFamily ,
71- fontSize : fontSizePt * ptToPixelRatio ,
69+ fontSize : ptToPixelRatio * initialFontSizeRef . current ,
7270 letterSpacing : 1.1 ,
7371 screenReaderMode : true ,
7472 theme : {
@@ -93,7 +91,6 @@ const useManagedTermimal = (
9391 { }
9492 )
9593 ) ;
96- const fitAddon = new FitAddon ( ) ;
9794 terminal . loadAddon ( fitAddon ) ;
9895 terminal . attachCustomKeyEventHandler ( ( e ) =>
9996 customKeyEventHandler ( e , tabOutRef )
@@ -193,9 +190,22 @@ const useManagedTermimal = (
193190 isUnmounted ,
194191 parent ,
195192 setSelection ,
196- fontSizePt ,
193+ fitAddon ,
194+ initialFontSizeRef ,
197195 tabOutRef ,
198196 ] ) ;
197+
198+ useEffect ( ( ) => {
199+ currentTerminalRef . current ?. setOption (
200+ "fontSize" ,
201+ fontSizePt * ptToPixelRatio
202+ ) ;
203+ try {
204+ fitAddon . fit ( ) ;
205+ } catch ( e ) {
206+ // It throws if you resize it when not visible but it does no harm.
207+ }
208+ } , [ currentTerminalRef , fitAddon , fontSizePt ] ) ;
199209} ;
200210
201211export default XTerm ;
0 commit comments