Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[release-4.7] Bug 1969791: fixes: WebTerminal widget should send resize events #9190

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -79,6 +79,11 @@ const CloudShellExec: React.FC<CloudShellExecProps> = ({
[tick],
);

const handleResize = React.useCallback((cols: number, rows: number) => {
const data = Base64.encode(JSON.stringify({ Height: rows, Width: cols }));
ws.current?.send(`4${data}`);
}, []);

React.useEffect(() => {
onActivate(true);
return () => {
Expand Down Expand Up @@ -239,7 +244,7 @@ const CloudShellExec: React.FC<CloudShellExecProps> = ({
if (wsOpen) {
return (
<div className="co-cloudshell-terminal__container">
<Terminal onData={onData} ref={terminal} />
<Terminal onData={onData} onResize={handleResize} ref={terminal} />
</div>
);
}
Expand Down
126 changes: 69 additions & 57 deletions frontend/packages/console-app/src/components/cloud-shell/Terminal.tsx
Expand Up @@ -14,6 +14,7 @@ const terminalOptions: ITerminalOptions = {

type TerminalProps = {
onData: (data: string) => void;
onResize: (cols: number, rows: number) => void;
};

export type ImperativeTerminalType = {
Expand All @@ -23,62 +24,73 @@ export type ImperativeTerminalType = {
onConnectionClosed: (msg: string) => void;
};

const Terminal = React.forwardRef<ImperativeTerminalType, TerminalProps>(({ onData }, ref) => {
const terminal = React.useRef<XTerminal>();
const terminalRef = React.useRef<HTMLDivElement>();

React.useEffect(() => {
const term: XTerminal = new XTerminal(terminalOptions);
term.open(terminalRef.current);
term.focus();

const resizeObserver: ResizeObserver = new ResizeObserver(() => {
window.requestAnimationFrame(() => fit(term));
});

resizeObserver.observe(terminalRef.current);

if (terminal.current !== term) {
terminal.current && terminal.current.destroy();
terminal.current = term;
}

return () => {
term.destroy();
resizeObserver.disconnect();
};
}, []);

React.useEffect(() => {
const term = terminal.current;
term.on('data', onData);

return () => {
term.off('data', onData);
};
}, [onData]);

React.useImperativeHandle(ref, () => ({
focus: () => {
terminal.current && terminal.current.focus();
},
reset: () => {
if (!terminal.current) return;
terminal.current.reset();
terminal.current.clear();
terminal.current.setOption('disableStdin', false);
},
onDataReceived: (data) => {
terminal.current && terminal.current.write(data);
},
onConnectionClosed: (msg) => {
if (!terminal.current) return;
terminal.current.write(`\x1b[31m${msg || 'disconnected'}\x1b[m\r\n`);
terminal.current.setOption('disableStdin', true);
},
}));

return <div className="co-terminal" ref={terminalRef} />;
});
const Terminal = React.forwardRef<ImperativeTerminalType, TerminalProps>(
({ onData, onResize }, ref) => {
const terminal = React.useRef<XTerminal>();
const terminalRef = React.useRef<HTMLDivElement>();

React.useEffect(() => {
const term: XTerminal = new XTerminal(terminalOptions);
term.open(terminalRef.current);
term.focus();

const resizeObserver: ResizeObserver = new ResizeObserver(() => {
window.requestAnimationFrame(() => fit(term));
});

resizeObserver.observe(terminalRef.current);

if (terminal.current !== term) {
terminal.current && terminal.current.destroy();
terminal.current = term;
}

return () => {
term.destroy();
resizeObserver.disconnect();
};
}, []);

const handleResize = React.useCallback(
({ cols, rows }: { cols: number; rows: number }) => {
onResize(cols, rows);
},
[onResize],
);

React.useEffect(() => {
const term = terminal.current;
term.on('data', onData);
term.on('resize', handleResize);

return () => {
term.off('data', onData);
term.off('resize', handleResize);
};
}, [onData, handleResize]);

React.useImperativeHandle(ref, () => ({
focus: () => {
terminal.current && terminal.current.focus();
},
reset: () => {
if (!terminal.current) return;
terminal.current.reset();
terminal.current.clear();
terminal.current.setOption('disableStdin', false);
},
onDataReceived: (data) => {
terminal.current && terminal.current.write(data);
},
onConnectionClosed: (msg) => {
if (!terminal.current) return;
terminal.current.write(`\x1b[31m${msg || 'disconnected'}\x1b[m\r\n`);
terminal.current.setOption('disableStdin', true);
},
}));

return <div className="co-terminal" ref={terminalRef} />;
},
);

export default Terminal;