5
5
*/
6
6
import { Box , BoxProps } from "@chakra-ui/layout" ;
7
7
import { useToken } from "@chakra-ui/react" ;
8
- import React , { useEffect , useRef } from "react" ;
8
+ import React , { useEffect , useMemo , useRef } from "react" ;
9
9
import { Terminal } from "xterm" ;
10
10
import { FitAddon } from "xterm-addon-fit" ;
11
11
import "xterm/css/xterm.css" ;
12
12
import useActionFeedback from "../common/use-action-feedback" ;
13
13
import useIsUnmounted from "../common/use-is-unmounted" ;
14
- import { backgroundColorTerm , defaultCodeFontSizePt } from "../deployment/misc" ;
14
+ import { backgroundColorTerm } from "../deployment/misc" ;
15
15
import { EVENT_SERIAL_DATA , EVENT_SERIAL_RESET } from "../device/device" ;
16
16
import { parseTraceLine , useDevice } from "../device/device-hooks" ;
17
17
import { useSelection } from "../workbench/use-selection" ;
@@ -21,20 +21,16 @@ import "./xterm-custom.css";
21
21
import customKeyEventHandler from "./xterm-keyboard" ;
22
22
23
23
interface XTermProps extends BoxProps {
24
- fontSizePt ?: number ;
25
24
tabOutRef : HTMLElement ;
25
+ fontSizePt : number ;
26
26
}
27
27
28
28
/**
29
29
* xterm.js-based terminal.
30
30
*/
31
- const XTerm = ( {
32
- fontSizePt = defaultCodeFontSizePt ,
33
- tabOutRef,
34
- ...props
35
- } : XTermProps ) => {
31
+ const XTerm = ( { fontSizePt, tabOutRef, ...props } : XTermProps ) => {
36
32
const ref = useRef < HTMLDivElement > ( null ) ;
37
- useManagedTermimal ( ref , fontSizePt , tabOutRef ) ;
33
+ useManagedTermimal ( ref , tabOutRef , fontSizePt ) ;
38
34
return < Box { ...props } ref = { ref } backgroundColor = { backgroundColorTerm } /> ;
39
35
} ;
40
36
@@ -48,16 +44,18 @@ const ptToPixelRatio = 96 / 72;
48
44
*/
49
45
const useManagedTermimal = (
50
46
ref : React . RefObject < HTMLDivElement > ,
51
- fontSizePt : number ,
52
- tabOutRef : HTMLElement
47
+ tabOutRef : HTMLElement ,
48
+ fontSizePt : number
53
49
) : void => {
54
50
const parent = ref . current ;
55
51
const actionFeedback = useActionFeedback ( ) ;
56
52
const codeFontFamily = useToken ( "fonts" , "code" ) ;
57
53
const device = useDevice ( ) ;
58
54
const isUnmounted = useIsUnmounted ( ) ;
59
55
const [ , setSelection ] = useSelection ( ) ;
56
+ const fitAddon = useMemo ( ( ) => new FitAddon ( ) , [ ] ) ;
60
57
const currentTerminalRef = useCurrentTerminalRef ( ) ;
58
+ const initialFontSizeRef = useRef < number > ( fontSizePt ) ;
61
59
62
60
useEffect ( ( ) => {
63
61
if ( ! parent ) {
@@ -68,7 +66,7 @@ const useManagedTermimal = (
68
66
}
69
67
const terminal = new Terminal ( {
70
68
fontFamily : codeFontFamily ,
71
- fontSize : fontSizePt * ptToPixelRatio ,
69
+ fontSize : ptToPixelRatio * initialFontSizeRef . current ,
72
70
letterSpacing : 1.1 ,
73
71
screenReaderMode : true ,
74
72
theme : {
@@ -93,7 +91,6 @@ const useManagedTermimal = (
93
91
{ }
94
92
)
95
93
) ;
96
- const fitAddon = new FitAddon ( ) ;
97
94
terminal . loadAddon ( fitAddon ) ;
98
95
terminal . attachCustomKeyEventHandler ( ( e ) =>
99
96
customKeyEventHandler ( e , tabOutRef )
@@ -193,9 +190,22 @@ const useManagedTermimal = (
193
190
isUnmounted ,
194
191
parent ,
195
192
setSelection ,
196
- fontSizePt ,
193
+ fitAddon ,
194
+ initialFontSizeRef ,
197
195
tabOutRef ,
198
196
] ) ;
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 ] ) ;
199
209
} ;
200
210
201
211
export default XTerm ;
0 commit comments