Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"@typescript-eslint/prefer-namespace-keyword": "error",
"@typescript-eslint/unified-signatures": "error",
"@typescript-eslint/no-var-requires": "off",
"@typescript-eslint/no-explicit-any": "off",
"arrow-body-style": "error",
"camelcase": [
"error",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ const pageData = {
"section": "extensions",
"subsection": "",
"source": "react",
"tabName": null,
"slug": "/extensions/react-console/react",
"sourceLink": "https://github.com/patternfly/react-console",
"relPath": "packages/module/patternfly-docs/content/extensions/react-console/examples/ReactConsole.md",
"propComponents": [
{
"name": "AccessConsoles",
Expand Down
1 change: 1 addition & 0 deletions packages/module/patternfly-docs/generated/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module.exports = {
section: "extensions",
subsection: "",
source: "react",
tabName: null,
Component: () => import(/* webpackChunkName: "extensions/react-console/react/index" */ './extensions/react-console/react')
}
};
71 changes: 36 additions & 35 deletions packages/module/src/components/SerialConsole/XTerm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ export const XTerm: React.FunctionComponent<XTermProps> = ({
onData,
innerRef
}) => {
let terminal: Terminal;
const terminalRef = React.useRef<Terminal>();
const ref = React.useRef<HTMLDivElement>();

useImperativeHandle(innerRef, () => ({
focusTerminal() {
if (terminal) {
terminal.focus();
if (terminalRef.current) {
terminalRef.current.focus();
}
},
/**
Expand All @@ -44,8 +44,8 @@ export const XTerm: React.FunctionComponent<XTermProps> = ({
* @param {string} data String content to be writen into the terminal
*/
onDataReceived: (data: string) => {
if (terminal) {
terminal.write(data);
if (terminalRef.current) {
terminalRef.current.write(data);
}
},
/**
Expand All @@ -54,16 +54,35 @@ export const XTerm: React.FunctionComponent<XTermProps> = ({
* @param {string} reason String error to be written into the terminal
*/
onConnectionClosed: (reason: string) => {
if (terminal) {
terminal.write(`\x1b[31m${reason || 'disconnected'}\x1b[m\r\n`);
terminal.refresh(terminal.rows, terminal.rows); // start to end row
if (terminalRef.current) {
terminalRef.current.write(`\x1b[31m${reason || 'disconnected'}\x1b[m\r\n`);
terminalRef.current.refresh(terminalRef.current.rows, terminalRef.current.rows); // start to end row
}
}
}));

const onBeforeUnload = React.useCallback((event: any) => {
// Firefox requires this when the page is in an iframe
event.preventDefault();

// see "an almost cross-browser solution" at
// https://developer.mozilla.org/en-US/docs/Web/Events/beforeunload
event.returnValue = '';
return '';
}, []);


const onFocusIn = () => {
window.addEventListener('beforeunload', onBeforeUnload);
};

const onFocusOut = React.useCallback(() => {
window.removeEventListener('beforeunload', onBeforeUnload);
}, [onBeforeUnload]);

React.useEffect(() => {
const fitAddon = new FitAddon();
terminal = new Terminal({
terminalRef.current = new Terminal({
cols,
rows,
cursorBlink: true,
Expand All @@ -75,21 +94,21 @@ export const XTerm: React.FunctionComponent<XTermProps> = ({
const onWindowResize = () => {
const geometry = fitAddon.proposeDimensions();
if (geometry) {
terminal.resize(geometry.rows, geometry.cols);
terminalRef.current.resize(geometry.rows, geometry.cols);
}
};

if (onData) {
terminal.onData(onData);
terminalRef.current.onData(onData);
}

if (onTitleChanged) {
terminal.onTitleChange(onTitleChanged);
terminalRef.current.onTitleChange(onTitleChanged);
}

terminal.loadAddon(fitAddon);
terminalRef.current.loadAddon(fitAddon);

terminal.open(ref.current);
terminalRef.current.open(ref.current);

const resizeListener = debounce(onWindowResize, 100);

Expand All @@ -99,34 +118,16 @@ export const XTerm: React.FunctionComponent<XTermProps> = ({
}
onWindowResize();
}
terminal.focus();
terminalRef.current.focus();

return () => {
terminal.dispose();
terminalRef.current.dispose();
if (canUseDOM) {
window.removeEventListener('resize', resizeListener);
}
onFocusOut();
};
}, []);

const onBeforeUnload = (event: any) => {
// Firefox requires this when the page is in an iframe
event.preventDefault();

// see "an almost cross-browser solution" at
// https://developer.mozilla.org/en-US/docs/Web/Events/beforeunload
event.returnValue = '';
return '';
};

const onFocusIn = () => {
window.addEventListener('beforeunload', onBeforeUnload);
};

const onFocusOut = () => {
window.removeEventListener('beforeunload', onBeforeUnload);
};
}, [cols, fontFamily, fontSize, onData, onFocusOut, onTitleChanged, rows]);

// ensure react never reuses this div by keying it with the terminal widget
// Workaround for xtermjs/xterm.js#3172
Expand Down
34 changes: 17 additions & 17 deletions packages/module/src/components/SpiceConsole/SpiceConsole.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,28 @@ export const SpiceConsole: React.FunctionComponent<SpiceConsoleProps> = ({
textCtrlAltDel
}) => {
let spiceStaticComponent: React.ReactNode;
let sc: any;
const scRef = React.useRef<any>();
const [status, setStatus] = React.useState(CONNECTING);

const disconnect = React.useCallback(() => {
if (scRef.current) {
scRef.current.stop();
scRef.current = undefined;
}
}, [scRef]);

const onSpiceError = React.useCallback((e: any) => {
disconnect();
setStatus(DISCONNECTED);
onDisconnected(e);
}, [disconnect, setStatus, onDisconnected]);

React.useEffect(() => {
try {
const protocol = encrypt ? 'wss' : 'ws';
const uri = `${protocol}://${host}:${port}/${path}`;

sc = new SpiceMainConn({
scRef.current = new SpiceMainConn({
uri,
/* eslint-disable camelcase */
screen_id: 'spice-screen',
Expand All @@ -71,27 +84,14 @@ export const SpiceConsole: React.FunctionComponent<SpiceConsoleProps> = ({
disconnect();
}
return () => disconnect();
}, []);

const disconnect = () => {
if (sc) {
sc.stop();
sc = undefined;
}
};
}, [disconnect, encrypt, host, onInitFailed, onSpiceError, password, path, port]);

const onCtrlAltDel = () => {
if (sc) {
if (scRef.current) {
sendCtrlAltDel();
}
};

const onSpiceError = (e: any) => {
disconnect();
setStatus(DISCONNECTED);
onDisconnected(e);
};

if (!spiceStaticComponent) {
// create just once
spiceStaticComponent = <div id="spice-screen" />;
Expand Down