({
source,
})
| 30 | }; |
| 31 | |
| 32 | export const XtermWrapper: React.FC<{ source: XtermDataSource }> = ({ |
| 33 | source, |
| 34 | }) => { |
| 35 | const [measure, xtermElement] = useMeasure<HTMLDivElement>(); |
| 36 | const [theme, setTheme] = React.useState(currentDocumentTheme()); |
| 37 | const [modulePromise] = React.useState<Promise<XtermModule>>(import('./xtermModule').then(m => m.default)); |
| 38 | const terminal = React.useRef<{ terminal: Terminal, fitAddon: FitAddon } | null>(null); |
| 39 | |
| 40 | React.useEffect(() => { |
| 41 | addThemeListener(setTheme); |
| 42 | return () => removeThemeListener(setTheme); |
| 43 | }, []); |
| 44 | |
| 45 | React.useEffect(() => { |
| 46 | const oldSourceWrite = source.write; |
| 47 | const oldSourceClear = source.clear; |
| 48 | |
| 49 | (async () => { |
| 50 | // Always load the module first. |
| 51 | const { Terminal, FitAddon } = await modulePromise; |
| 52 | const element = xtermElement.current; |
| 53 | if (!element) |
| 54 | return; |
| 55 | |
| 56 | const terminalTheme = theme === 'dark-mode' ? darkTheme : lightTheme; |
| 57 | if (terminal.current && terminal.current.terminal.options.theme === terminalTheme) |
| 58 | return; |
| 59 | |
| 60 | if (terminal.current) |
| 61 | element.textContent = ''; |
| 62 | |
| 63 | const newTerminal = new Terminal({ |
| 64 | convertEol: true, |
| 65 | fontSize: 13, |
| 66 | scrollback: 10000, |
| 67 | fontFamily: 'monospace', |
| 68 | theme: terminalTheme, |
| 69 | }); |
| 70 | |
| 71 | const fitAddon = new FitAddon(); |
| 72 | newTerminal.loadAddon(fitAddon); |
| 73 | for (const p of source.pending) |
| 74 | newTerminal.write(p); |
| 75 | source.write = (data => { |
| 76 | source.pending.push(data); |
| 77 | newTerminal.write(data); |
| 78 | }); |
| 79 | source.clear = () => { |
| 80 | source.pending = []; |
| 81 | newTerminal.clear(); |
| 82 | }; |
| 83 | newTerminal.open(element); |
| 84 | fitAddon.fit(); |
| 85 | terminal.current = { terminal: newTerminal, fitAddon }; |
| 86 | })(); |
| 87 | return () => { |
| 88 | source.clear = oldSourceClear; |
| 89 | source.write = oldSourceWrite; |
nothing calls this directly
no test coverage detected
searching dependent graphs…