2016-07-13 12:44:24 -08:00
|
|
|
import React from 'react';
|
2018-01-09 07:33:24 -09:00
|
|
|
import {Terminal} from 'xterm';
|
2019-10-06 10:51:56 -08:00
|
|
|
import {FitAddon} from 'xterm-addon-fit';
|
|
|
|
|
import {WebLinksAddon} from 'xterm-addon-web-links';
|
|
|
|
|
import {SearchAddon} from 'xterm-addon-search';
|
|
|
|
|
import {WebglAddon} from 'xterm-addon-webgl';
|
2019-10-09 12:23:55 -08:00
|
|
|
import {LigaturesAddon} from 'xterm-addon-ligatures';
|
2017-10-24 14:06:46 -08:00
|
|
|
import {clipboard} from 'electron';
|
2018-01-09 07:33:24 -09:00
|
|
|
import * as Color from 'color';
|
2017-02-23 02:10:05 -09:00
|
|
|
import terms from '../terms';
|
2017-10-22 13:16:52 -08:00
|
|
|
import processClipboard from '../utils/paste';
|
2019-09-23 09:37:22 -08:00
|
|
|
import SearchBox from './searchBox';
|
2017-06-11 02:42:39 -08:00
|
|
|
|
2019-10-06 10:51:56 -08:00
|
|
|
const isWindows = ['Windows', 'Win16', 'Win32', 'WinCE'].indexOf(navigator.platform) >= 0;
|
2018-01-15 08:36:02 -09:00
|
|
|
|
2017-06-11 02:42:39 -08:00
|
|
|
// map old hterm constants to xterm.js
|
|
|
|
|
const CURSOR_STYLES = {
|
|
|
|
|
BEAM: 'bar',
|
|
|
|
|
UNDERLINE: 'underline',
|
|
|
|
|
BLOCK: 'block'
|
2017-09-10 05:35:10 -08:00
|
|
|
};
|
2016-06-30 22:01:04 -08:00
|
|
|
|
2019-01-24 10:22:53 -09:00
|
|
|
const isWebgl2Supported = (() => {
|
|
|
|
|
let isSupported = window.WebGL2RenderingContext ? undefined : false;
|
|
|
|
|
return () => {
|
|
|
|
|
if (isSupported === undefined) {
|
|
|
|
|
const canvas = document.createElement('canvas');
|
|
|
|
|
const gl = canvas.getContext('webgl2', {depth: false, antialias: false});
|
|
|
|
|
isSupported = gl instanceof window.WebGL2RenderingContext;
|
|
|
|
|
}
|
|
|
|
|
return isSupported;
|
|
|
|
|
};
|
|
|
|
|
})();
|
|
|
|
|
|
2018-01-09 07:33:24 -09:00
|
|
|
const getTermOptions = props => {
|
|
|
|
|
// Set a background color only if it is opaque
|
2018-02-18 03:28:26 -09:00
|
|
|
const needTransparency = Color(props.backgroundColor).alpha() < 1;
|
|
|
|
|
const backgroundColor = needTransparency ? 'transparent' : props.backgroundColor;
|
2019-01-24 10:22:53 -09:00
|
|
|
|
2018-01-09 07:33:24 -09:00
|
|
|
return {
|
2018-01-21 00:19:27 -09:00
|
|
|
macOptionIsMeta: props.modifierKeys.altIsMeta,
|
2018-05-29 02:11:21 -08:00
|
|
|
scrollback: props.scrollback,
|
2018-01-09 07:33:24 -09:00
|
|
|
cursorStyle: CURSOR_STYLES[props.cursorShape],
|
|
|
|
|
cursorBlink: props.cursorBlink,
|
|
|
|
|
fontFamily: props.fontFamily,
|
|
|
|
|
fontSize: props.fontSize,
|
2018-02-14 04:09:02 -09:00
|
|
|
fontWeight: props.fontWeight,
|
|
|
|
|
fontWeightBold: props.fontWeightBold,
|
2018-04-20 14:22:34 -08:00
|
|
|
lineHeight: props.lineHeight,
|
2018-05-10 02:54:00 -08:00
|
|
|
letterSpacing: props.letterSpacing,
|
2018-02-18 03:28:26 -09:00
|
|
|
allowTransparency: needTransparency,
|
2019-01-11 04:31:11 -09:00
|
|
|
macOptionClickForcesSelection: props.macOptionSelectionMode === 'force',
|
2019-10-02 16:08:40 -08:00
|
|
|
bellStyle: props.bell === 'SOUND' ? 'sound' : 'none',
|
2019-10-06 10:51:56 -08:00
|
|
|
windowsMode: isWindows,
|
2018-01-09 07:33:24 -09:00
|
|
|
theme: {
|
|
|
|
|
foreground: props.foregroundColor,
|
|
|
|
|
background: backgroundColor,
|
|
|
|
|
cursor: props.cursorColor,
|
|
|
|
|
cursorAccent: props.cursorAccentColor,
|
2019-01-21 15:09:18 -09:00
|
|
|
selection: props.selectionColor,
|
2018-01-09 07:33:24 -09:00
|
|
|
black: props.colors.black,
|
|
|
|
|
red: props.colors.red,
|
|
|
|
|
green: props.colors.green,
|
|
|
|
|
yellow: props.colors.yellow,
|
|
|
|
|
blue: props.colors.blue,
|
|
|
|
|
magenta: props.colors.magenta,
|
|
|
|
|
cyan: props.colors.cyan,
|
|
|
|
|
white: props.colors.white,
|
|
|
|
|
brightBlack: props.colors.lightBlack,
|
|
|
|
|
brightRed: props.colors.lightRed,
|
|
|
|
|
brightGreen: props.colors.lightGreen,
|
|
|
|
|
brightYellow: props.colors.lightYellow,
|
|
|
|
|
brightBlue: props.colors.lightBlue,
|
|
|
|
|
brightMagenta: props.colors.lightMagenta,
|
|
|
|
|
brightCyan: props.colors.lightCyan,
|
|
|
|
|
brightWhite: props.colors.lightWhite
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
2018-03-17 04:51:36 -08:00
|
|
|
export default class Term extends React.PureComponent {
|
2016-09-21 06:27:11 -08:00
|
|
|
constructor(props) {
|
2016-07-13 12:44:24 -08:00
|
|
|
super(props);
|
2017-09-09 03:42:19 -08:00
|
|
|
props.ref_(props.uid, this);
|
2019-01-24 12:02:34 -09:00
|
|
|
this.termRef = null;
|
2017-09-10 05:35:10 -08:00
|
|
|
this.termWrapperRef = null;
|
|
|
|
|
this.termRect = null;
|
2018-01-09 07:33:24 -09:00
|
|
|
this.termOptions = {};
|
2018-12-06 14:56:29 -09:00
|
|
|
this.disposableListeners = [];
|
2019-10-02 16:08:40 -08:00
|
|
|
this.termDefaultBellSound = null;
|
2019-10-06 10:51:56 -08:00
|
|
|
this.fitAddon = new FitAddon();
|
|
|
|
|
this.searchAddon = new SearchAddon();
|
2016-07-13 12:44:24 -08:00
|
|
|
}
|
|
|
|
|
|
2019-01-24 14:46:39 -09:00
|
|
|
// The main process shows this in the About dialog
|
|
|
|
|
static reportRenderer(uid, type) {
|
|
|
|
|
const rendererTypes = Term.rendererTypes || {};
|
|
|
|
|
if (rendererTypes[uid] !== type) {
|
|
|
|
|
rendererTypes[uid] = type;
|
|
|
|
|
Term.rendererTypes = rendererTypes;
|
|
|
|
|
window.rpc.emit('info renderer', {uid, type});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-21 06:27:11 -08:00
|
|
|
componentDidMount() {
|
|
|
|
|
const {props} = this;
|
2016-06-30 22:01:04 -08:00
|
|
|
|
2018-01-09 07:33:24 -09:00
|
|
|
this.termOptions = getTermOptions(props);
|
|
|
|
|
this.term = props.term || new Terminal(this.termOptions);
|
2019-10-02 16:08:40 -08:00
|
|
|
this.termDefaultBellSound = this.term.getOption('bellSound');
|
2019-01-18 13:58:09 -09:00
|
|
|
|
|
|
|
|
// The parent element for the terminal is attached and removed manually so
|
|
|
|
|
// that we can preserve it across mounts and unmounts of the component
|
2019-01-24 12:02:34 -09:00
|
|
|
this.termRef = props.term ? props.term._core._parent : document.createElement('div');
|
|
|
|
|
this.termRef.className = 'term_fit term_term';
|
2019-01-18 13:58:09 -09:00
|
|
|
|
2019-01-24 12:02:34 -09:00
|
|
|
this.termWrapperRef.appendChild(this.termRef);
|
2019-01-18 13:58:09 -09:00
|
|
|
|
|
|
|
|
if (!props.term) {
|
2019-10-06 10:51:56 -08:00
|
|
|
let needTransparency = Color(props.backgroundColor).alpha() < 1;
|
|
|
|
|
let useWebGL = false;
|
|
|
|
|
if (props.webGLRenderer) {
|
|
|
|
|
if (needTransparency) {
|
|
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
|
console.warn(
|
|
|
|
|
'WebGL Renderer has been disabled since it does not support transparent backgrounds yet. ' +
|
|
|
|
|
'Falling back to canvas-based rendering.'
|
|
|
|
|
);
|
|
|
|
|
} else if (!isWebgl2Supported()) {
|
|
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
|
console.warn('WebGL2 is not supported on your machine. Falling back to canvas-based rendering.');
|
|
|
|
|
} else {
|
|
|
|
|
// Experimental WebGL renderer needs some more glue-code to make it work on Hyper.
|
2019-10-09 12:23:55 -08:00
|
|
|
// If you're working on enabling back WebGL, you will also need to look into `xterm-addon-ligatures` support for that renderer.
|
2019-10-06 10:51:56 -08:00
|
|
|
// useWebGL = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Term.reportRenderer(props.uid, useWebGL ? 'WebGL' : 'Canvas');
|
|
|
|
|
|
2019-01-18 13:58:09 -09:00
|
|
|
this.term.attachCustomKeyEventHandler(this.keyboardHandler);
|
2019-10-06 10:51:56 -08:00
|
|
|
this.term.loadAddon(this.fitAddon);
|
|
|
|
|
this.term.loadAddon(this.searchAddon);
|
|
|
|
|
this.term.loadAddon(new WebLinksAddon());
|
2019-01-24 12:02:34 -09:00
|
|
|
this.term.open(this.termRef);
|
2019-10-06 10:51:56 -08:00
|
|
|
if (useWebGL) {
|
|
|
|
|
this.term.loadAddon(new WebglAddon());
|
|
|
|
|
}
|
2019-10-09 12:23:55 -08:00
|
|
|
if (props.disableLigatures !== true) {
|
|
|
|
|
this.term.loadAddon(new LigaturesAddon());
|
|
|
|
|
}
|
2019-10-06 10:51:56 -08:00
|
|
|
} else {
|
|
|
|
|
// get the cached plugins
|
|
|
|
|
this.fitAddon = props.fitAddon;
|
|
|
|
|
this.searchAddon = props.searchAddon;
|
2017-08-02 11:05:47 -08:00
|
|
|
}
|
2019-01-18 13:58:09 -09:00
|
|
|
|
2019-10-10 11:20:26 -08:00
|
|
|
this.fitAddon.fit();
|
|
|
|
|
|
2018-02-03 07:00:30 -09:00
|
|
|
if (this.props.isTermActive) {
|
|
|
|
|
this.term.focus();
|
|
|
|
|
}
|
2016-10-19 14:55:06 -08:00
|
|
|
|
2017-06-11 02:42:39 -08:00
|
|
|
if (props.onTitle) {
|
2019-10-06 10:51:56 -08:00
|
|
|
this.disposableListeners.push(this.term.onTitleChange(props.onTitle));
|
2017-06-11 02:42:39 -08:00
|
|
|
}
|
2016-07-03 12:35:45 -08:00
|
|
|
|
2017-06-11 02:42:39 -08:00
|
|
|
if (props.onActive) {
|
2019-10-06 10:51:56 -08:00
|
|
|
this.term.textarea.addEventListener('focus', props.onActive);
|
|
|
|
|
this.disposableListeners.push({
|
|
|
|
|
dispose: () => this.term.textarea.removeEventListener('focus', this.props.onActive)
|
|
|
|
|
});
|
2016-08-05 14:30:40 -08:00
|
|
|
}
|
|
|
|
|
|
2017-06-11 02:42:39 -08:00
|
|
|
if (props.onData) {
|
2019-10-06 10:51:56 -08:00
|
|
|
this.disposableListeners.push(this.term.onData(props.onData));
|
2016-08-13 13:03:44 -08:00
|
|
|
}
|
|
|
|
|
|
2017-06-11 02:42:39 -08:00
|
|
|
if (props.onResize) {
|
2018-12-06 14:56:29 -09:00
|
|
|
this.disposableListeners.push(
|
2019-10-06 10:51:56 -08:00
|
|
|
this.term.onResize(({cols, rows}) => {
|
2018-12-06 14:56:29 -09:00
|
|
|
props.onResize(cols, rows);
|
|
|
|
|
})
|
|
|
|
|
);
|
2019-10-10 11:20:26 -08:00
|
|
|
|
|
|
|
|
// the row and col of init session is null, so reize the node-pty
|
|
|
|
|
props.onResize(this.term.cols, this.term.rows);
|
2016-09-21 06:27:11 -08:00
|
|
|
}
|
2016-07-13 12:44:24 -08:00
|
|
|
|
2018-01-22 08:12:03 -09:00
|
|
|
if (props.onCursorMove) {
|
2018-12-06 14:56:29 -09:00
|
|
|
this.disposableListeners.push(
|
2019-10-06 10:51:56 -08:00
|
|
|
this.term.onCursorMove(() => {
|
2018-12-06 14:56:29 -09:00
|
|
|
const cursorFrame = {
|
2018-12-15 12:30:58 -09:00
|
|
|
x: this.term._core.buffer.x * this.term._core.renderer.dimensions.actualCellWidth,
|
|
|
|
|
y: this.term._core.buffer.y * this.term._core.renderer.dimensions.actualCellHeight,
|
|
|
|
|
width: this.term._core.renderer.dimensions.actualCellWidth,
|
|
|
|
|
height: this.term._core.renderer.dimensions.actualCellHeight,
|
2019-03-27 00:41:28 -08:00
|
|
|
col: this.term._core.buffer.x,
|
|
|
|
|
row: this.term._core.buffer.y
|
2018-12-06 14:56:29 -09:00
|
|
|
};
|
|
|
|
|
props.onCursorMove(cursorFrame);
|
|
|
|
|
})
|
|
|
|
|
);
|
2018-01-22 08:12:03 -09:00
|
|
|
}
|
|
|
|
|
|
2017-10-22 13:16:52 -08:00
|
|
|
window.addEventListener('paste', this.onWindowPaste, {
|
|
|
|
|
capture: true
|
|
|
|
|
});
|
|
|
|
|
|
2017-02-23 02:10:05 -09:00
|
|
|
terms[this.props.uid] = this;
|
2016-07-13 12:44:24 -08:00
|
|
|
}
|
|
|
|
|
|
2017-09-10 05:35:10 -08:00
|
|
|
getTermDocument() {
|
2017-06-11 02:42:39 -08:00
|
|
|
// eslint-disable-next-line no-console
|
2017-09-25 04:46:53 -08:00
|
|
|
console.warn(
|
|
|
|
|
'The underlying terminal engine of Hyper no longer ' +
|
|
|
|
|
'uses iframes with individual `document` objects for each ' +
|
|
|
|
|
'terminal instance. This method call is retained for ' +
|
2017-11-01 13:31:44 -08:00
|
|
|
"backwards compatibility reasons. It's ok to attach directly" +
|
2017-09-25 04:46:53 -08:00
|
|
|
'to the `document` object of the main `window`.'
|
|
|
|
|
);
|
|
|
|
|
return document;
|
2016-12-21 06:19:41 -09:00
|
|
|
}
|
2016-11-16 09:44:04 -09:00
|
|
|
|
2017-10-22 13:16:52 -08:00
|
|
|
// intercepting paste event for any necessary processing of
|
|
|
|
|
// clipboard data, if result is falsy, paste event continues
|
2019-11-25 07:16:00 -09:00
|
|
|
onWindowPaste = e => {
|
2017-10-22 13:16:52 -08:00
|
|
|
if (!this.props.isTermActive) return;
|
|
|
|
|
|
|
|
|
|
const processed = processClipboard();
|
|
|
|
|
if (processed) {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
e.stopPropagation();
|
2019-01-06 05:55:16 -09:00
|
|
|
this.term._core.handler(processed);
|
2017-10-22 13:16:52 -08:00
|
|
|
}
|
2019-11-25 07:16:00 -09:00
|
|
|
};
|
2017-10-22 13:16:52 -08:00
|
|
|
|
2019-11-25 07:16:00 -09:00
|
|
|
onMouseUp = e => {
|
2017-11-03 14:01:21 -08:00
|
|
|
if (this.props.quickEdit && e.button === 2) {
|
|
|
|
|
if (this.term.hasSelection()) {
|
|
|
|
|
clipboard.writeText(this.term.getSelection());
|
|
|
|
|
this.term.clearSelection();
|
|
|
|
|
} else {
|
|
|
|
|
document.execCommand('paste');
|
|
|
|
|
}
|
|
|
|
|
} else if (this.props.copyOnSelect && this.term.hasSelection()) {
|
2017-10-24 14:06:46 -08:00
|
|
|
clipboard.writeText(this.term.getSelection());
|
|
|
|
|
}
|
2019-11-25 07:16:00 -09:00
|
|
|
};
|
2017-10-24 14:06:46 -08:00
|
|
|
|
2016-09-21 06:27:11 -08:00
|
|
|
write(data) {
|
2017-06-11 02:42:39 -08:00
|
|
|
this.term.write(data);
|
2016-07-13 12:44:24 -08:00
|
|
|
}
|
|
|
|
|
|
2016-09-21 06:27:11 -08:00
|
|
|
focus() {
|
2017-06-11 02:42:39 -08:00
|
|
|
this.term.focus();
|
2016-07-13 12:44:24 -08:00
|
|
|
}
|
|
|
|
|
|
2016-09-21 06:27:11 -08:00
|
|
|
clear() {
|
2017-06-11 02:42:39 -08:00
|
|
|
this.term.clear();
|
2017-08-02 11:05:47 -08:00
|
|
|
}
|
|
|
|
|
|
2017-09-10 05:35:10 -08:00
|
|
|
reset() {
|
2017-08-02 11:05:47 -08:00
|
|
|
this.term.reset();
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-25 07:16:00 -09:00
|
|
|
search = searchTerm => {
|
2019-10-06 10:51:56 -08:00
|
|
|
this.searchAddon.findNext(searchTerm);
|
2019-11-25 07:16:00 -09:00
|
|
|
};
|
2019-09-23 09:37:22 -08:00
|
|
|
|
2019-11-25 07:16:00 -09:00
|
|
|
searchNext = searchTerm => {
|
2019-10-06 10:51:56 -08:00
|
|
|
this.searchAddon.findNext(searchTerm);
|
2019-11-25 07:16:00 -09:00
|
|
|
};
|
2019-09-23 09:37:22 -08:00
|
|
|
|
2019-11-25 07:16:00 -09:00
|
|
|
searchPrevious = searchTerm => {
|
2019-10-06 10:51:56 -08:00
|
|
|
this.searchAddon.findPrevious(searchTerm);
|
2019-11-25 07:16:00 -09:00
|
|
|
};
|
2019-09-23 09:37:22 -08:00
|
|
|
|
2019-11-25 07:16:00 -09:00
|
|
|
closeSearchBox = () => {
|
2019-09-23 09:37:22 -08:00
|
|
|
this.props.toggleSearch();
|
2019-11-25 07:16:00 -09:00
|
|
|
};
|
2019-09-23 09:37:22 -08:00
|
|
|
|
2017-09-10 05:35:10 -08:00
|
|
|
resize(cols, rows) {
|
2017-08-02 11:05:47 -08:00
|
|
|
this.term.resize(cols, rows);
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-21 02:21:16 -09:00
|
|
|
selectAll() {
|
|
|
|
|
this.term.selectAll();
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-10 05:35:10 -08:00
|
|
|
fitResize() {
|
2017-11-06 03:26:56 -09:00
|
|
|
if (!this.termWrapperRef) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2019-10-06 10:51:56 -08:00
|
|
|
this.fitAddon.fit();
|
2016-07-03 12:35:45 -08:00
|
|
|
}
|
|
|
|
|
|
2017-09-03 11:15:33 -08:00
|
|
|
keyboardHandler(e) {
|
2017-11-02 18:51:18 -08:00
|
|
|
// Has Mousetrap flagged this event as a command?
|
|
|
|
|
return !e.catched;
|
2017-09-03 11:15:33 -08:00
|
|
|
}
|
|
|
|
|
|
2019-10-08 07:13:35 -08:00
|
|
|
componentDidUpdate(prevProps) {
|
|
|
|
|
if (!prevProps.cleared && this.props.cleared) {
|
|
|
|
|
this.clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const nextTermOptions = getTermOptions(this.props);
|
|
|
|
|
|
|
|
|
|
// Use bellSound in nextProps if it exists
|
|
|
|
|
// otherwise use the default sound found in xterm.
|
|
|
|
|
nextTermOptions.bellSound = this.props.bellSound || this.termDefaultBellSound;
|
|
|
|
|
|
|
|
|
|
if (!prevProps.search && this.props.search) {
|
|
|
|
|
this.search();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Update only options that have changed.
|
|
|
|
|
Object.keys(nextTermOptions)
|
|
|
|
|
.filter(option => option !== 'theme' && nextTermOptions[option] !== this.termOptions[option])
|
|
|
|
|
.forEach(option => {
|
|
|
|
|
try {
|
|
|
|
|
this.term.setOption(option, nextTermOptions[option]);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
if (/The webgl renderer only works with the webgl char atlas/i.test(e.message)) {
|
|
|
|
|
// Ignore this because the char atlas will also be changed
|
|
|
|
|
} else {
|
|
|
|
|
throw e;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Do we need to update theme?
|
|
|
|
|
const shouldUpdateTheme =
|
|
|
|
|
!this.termOptions.theme ||
|
|
|
|
|
nextTermOptions.rendererType !== this.termOptions.rendererType ||
|
|
|
|
|
Object.keys(nextTermOptions.theme).some(
|
|
|
|
|
option => nextTermOptions.theme[option] !== this.termOptions.theme[option]
|
|
|
|
|
);
|
|
|
|
|
if (shouldUpdateTheme) {
|
|
|
|
|
this.term.setOption('theme', nextTermOptions.theme);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.termOptions = nextTermOptions;
|
|
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
this.props.fontSize !== prevProps.fontSize ||
|
|
|
|
|
this.props.fontFamily !== prevProps.fontFamily ||
|
|
|
|
|
this.props.lineHeight !== prevProps.lineHeight ||
|
|
|
|
|
this.props.letterSpacing !== prevProps.letterSpacing
|
|
|
|
|
) {
|
|
|
|
|
// resize to fit the container
|
|
|
|
|
this.fitResize();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (prevProps.rows !== this.props.rows || prevProps.cols !== this.props.cols) {
|
|
|
|
|
this.resize(this.props.cols, this.props.rows);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//TODO: Remove usage of legacy and soon deprecated lifecycle methods
|
2019-10-06 10:51:56 -08:00
|
|
|
UNSAFE_componentWillReceiveProps(nextProps) {
|
2016-07-13 12:44:24 -08:00
|
|
|
if (!this.props.cleared && nextProps.cleared) {
|
|
|
|
|
this.clear();
|
2016-07-05 12:14:30 -08:00
|
|
|
}
|
2019-10-02 16:08:40 -08:00
|
|
|
|
2018-01-09 07:33:24 -09:00
|
|
|
const nextTermOptions = getTermOptions(nextProps);
|
|
|
|
|
|
2019-10-02 16:08:40 -08:00
|
|
|
// Use bellSound in nextProps if it exists
|
|
|
|
|
// otherwise use the default sound found in xterm.
|
|
|
|
|
nextTermOptions.bellSound = nextProps.bellSound || this.termDefaultBellSound;
|
|
|
|
|
|
2019-09-23 09:37:22 -08:00
|
|
|
if (!this.props.search && nextProps.search) {
|
|
|
|
|
this.search();
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-09 07:33:24 -09:00
|
|
|
// Update only options that have changed.
|
|
|
|
|
Object.keys(nextTermOptions)
|
|
|
|
|
.filter(option => option !== 'theme' && nextTermOptions[option] !== this.termOptions[option])
|
2018-12-28 13:42:05 -09:00
|
|
|
.forEach(option => {
|
|
|
|
|
try {
|
|
|
|
|
this.term.setOption(option, nextTermOptions[option]);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
if (/The webgl renderer only works with the webgl char atlas/i.test(e.message)) {
|
|
|
|
|
// Ignore this because the char atlas will also be changed
|
|
|
|
|
} else {
|
|
|
|
|
throw e;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
2018-01-09 07:33:24 -09:00
|
|
|
|
|
|
|
|
// Do we need to update theme?
|
|
|
|
|
const shouldUpdateTheme =
|
|
|
|
|
!this.termOptions.theme ||
|
2018-12-28 13:42:05 -09:00
|
|
|
nextTermOptions.rendererType !== this.termOptions.rendererType ||
|
2018-03-22 11:59:07 -08:00
|
|
|
Object.keys(nextTermOptions.theme).some(
|
|
|
|
|
option => nextTermOptions.theme[option] !== this.termOptions.theme[option]
|
|
|
|
|
);
|
2018-01-09 07:33:24 -09:00
|
|
|
if (shouldUpdateTheme) {
|
|
|
|
|
this.term.setOption('theme', nextTermOptions.theme);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.termOptions = nextTermOptions;
|
2017-08-02 11:05:47 -08:00
|
|
|
|
2018-04-20 14:22:34 -08:00
|
|
|
if (
|
|
|
|
|
this.props.fontSize !== nextProps.fontSize ||
|
|
|
|
|
this.props.fontFamily !== nextProps.fontFamily ||
|
2018-05-10 02:54:00 -08:00
|
|
|
this.props.lineHeight !== nextProps.lineHeight ||
|
|
|
|
|
this.props.letterSpacing !== nextProps.letterSpacing
|
2018-04-20 14:22:34 -08:00
|
|
|
) {
|
2017-08-02 11:05:47 -08:00
|
|
|
// resize to fit the container
|
2017-09-10 05:35:10 -08:00
|
|
|
this.fitResize();
|
2017-08-02 11:05:47 -08:00
|
|
|
}
|
|
|
|
|
|
2017-09-10 05:35:10 -08:00
|
|
|
if (nextProps.rows !== this.props.rows || nextProps.cols !== this.props.cols) {
|
|
|
|
|
this.resize(nextProps.cols, nextProps.rows);
|
2017-08-02 11:05:47 -08:00
|
|
|
}
|
2016-06-30 22:01:04 -08:00
|
|
|
}
|
|
|
|
|
|
2019-11-25 07:16:00 -09:00
|
|
|
onTermWrapperRef = component => {
|
2017-09-09 03:42:19 -08:00
|
|
|
this.termWrapperRef = component;
|
2019-01-21 10:34:23 -09:00
|
|
|
|
|
|
|
|
if (component) {
|
2019-10-12 06:06:19 -08:00
|
|
|
this.resizeObserver = new ResizeObserver(() => {
|
|
|
|
|
clearTimeout(this.resizeTimeout);
|
|
|
|
|
this.resizeTimeout = setTimeout(() => {
|
2019-01-21 10:34:23 -09:00
|
|
|
this.fitResize();
|
2019-10-10 11:20:26 -08:00
|
|
|
}, 500);
|
2019-01-21 10:34:23 -09:00
|
|
|
});
|
2019-10-12 06:06:19 -08:00
|
|
|
this.resizeObserver.observe(component);
|
2019-01-21 10:34:23 -09:00
|
|
|
} else {
|
2019-10-12 06:06:19 -08:00
|
|
|
this.resizeObserver.disconnect();
|
2019-01-21 10:34:23 -09:00
|
|
|
}
|
2019-11-25 07:16:00 -09:00
|
|
|
};
|
2017-09-09 03:42:19 -08:00
|
|
|
|
2016-09-21 06:27:11 -08:00
|
|
|
componentWillUnmount() {
|
2017-09-09 03:42:19 -08:00
|
|
|
terms[this.props.uid] = null;
|
2019-01-24 12:02:34 -09:00
|
|
|
this.termWrapperRef.removeChild(this.termRef);
|
2017-09-09 03:42:19 -08:00
|
|
|
this.props.ref_(this.props.uid, null);
|
2017-08-02 11:05:47 -08:00
|
|
|
|
|
|
|
|
// to clean up the terminal, we remove the listeners
|
2017-09-10 05:35:10 -08:00
|
|
|
// instead of invoking `destroy`, since it will make the
|
2017-08-02 11:05:47 -08:00
|
|
|
// term insta un-attachable in the future (which we need
|
|
|
|
|
// to do in case of splitting, see `componentDidMount`
|
2018-12-06 14:56:29 -09:00
|
|
|
this.disposableListeners.forEach(handler => handler.dispose());
|
|
|
|
|
this.disposableListeners = [];
|
2017-08-02 11:05:47 -08:00
|
|
|
|
2017-10-22 13:16:52 -08:00
|
|
|
window.removeEventListener('paste', this.onWindowPaste, {
|
|
|
|
|
capture: true
|
|
|
|
|
});
|
2016-06-30 22:01:04 -08:00
|
|
|
}
|
|
|
|
|
|
2018-03-17 04:51:36 -08:00
|
|
|
render() {
|
2017-09-10 05:35:10 -08:00
|
|
|
return (
|
2017-10-24 14:06:46 -08:00
|
|
|
<div
|
2018-03-17 04:51:36 -08:00
|
|
|
className={`term_fit ${this.props.isTermActive ? 'term_active' : ''}`}
|
2017-10-24 14:06:46 -08:00
|
|
|
style={{padding: this.props.padding}}
|
|
|
|
|
onMouseUp={this.onMouseUp}
|
|
|
|
|
>
|
2017-09-10 05:35:10 -08:00
|
|
|
{this.props.customChildrenBefore}
|
2019-01-18 13:58:09 -09:00
|
|
|
<div ref={this.onTermWrapperRef} className="term_fit term_wrapper" />
|
2017-09-10 05:35:10 -08:00
|
|
|
{this.props.customChildren}
|
2019-09-23 09:37:22 -08:00
|
|
|
{this.props.search ? (
|
|
|
|
|
<SearchBox
|
|
|
|
|
search={this.search}
|
|
|
|
|
next={this.searchNext}
|
|
|
|
|
prev={this.searchPrevious}
|
|
|
|
|
close={this.closeSearchBox}
|
|
|
|
|
/>
|
|
|
|
|
) : (
|
|
|
|
|
''
|
|
|
|
|
)}
|
2018-03-17 04:51:36 -08:00
|
|
|
|
2019-01-18 13:58:09 -09:00
|
|
|
<style jsx global>{`
|
2018-03-17 04:51:36 -08:00
|
|
|
.term_fit {
|
|
|
|
|
display: block;
|
|
|
|
|
width: 100%;
|
|
|
|
|
height: 100%;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.term_wrapper {
|
|
|
|
|
/* TODO: decide whether to keep this or not based on understanding what xterm-selection is for */
|
|
|
|
|
overflow: hidden;
|
|
|
|
|
}
|
|
|
|
|
`}</style>
|
2017-08-02 11:05:47 -08:00
|
|
|
</div>
|
2017-09-10 05:35:10 -08:00
|
|
|
);
|
2016-06-30 22:01:04 -08:00
|
|
|
}
|
|
|
|
|
}
|