MCPcopy
hub / github.com/microsoft/playwright / XtermWrapper

Function XtermWrapper

packages/web/src/components/xtermWrapper.tsx:32–105  ·  view source on GitHub ↗
({
  source,
})

Source from the content-addressed store, hash-verified

30};
31
32export 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;

Callers

nothing calls this directly

Calls 9

useMeasureFunction · 0.90
currentDocumentThemeFunction · 0.90
addThemeListenerFunction · 0.90
removeThemeListenerFunction · 0.90
thenMethod · 0.80
writeMethod · 0.65
pushMethod · 0.65
clearMethod · 0.65
openMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…