2016-09-29 11:10:17 -08:00
|
|
|
/* eslint-disable react/no-danger */
|
|
|
|
|
|
2016-07-13 12:44:24 -08:00
|
|
|
import React from 'react';
|
2017-11-02 18:51:18 -08:00
|
|
|
import Mousetrap from 'mousetrap';
|
2016-09-21 06:27:11 -08:00
|
|
|
|
|
|
|
|
import {connect} from '../utils/plugins';
|
|
|
|
|
import * as uiActions from '../actions/ui';
|
2017-11-02 18:51:18 -08:00
|
|
|
import {getRegisteredKeys, getCommandHandler, shouldPreventDefault} from '../command-registry';
|
2018-03-17 04:51:36 -08:00
|
|
|
import stylis from 'stylis';
|
2016-09-21 06:27:11 -08:00
|
|
|
|
2016-07-13 12:44:24 -08:00
|
|
|
import HeaderContainer from './header';
|
|
|
|
|
import TermsContainer from './terms';
|
|
|
|
|
import NotificationsContainer from './notifications';
|
|
|
|
|
|
|
|
|
|
const isMac = /Mac/.test(navigator.userAgent);
|
|
|
|
|
|
2018-03-17 04:51:36 -08:00
|
|
|
class Hyper extends React.PureComponent {
|
2016-09-21 06:27:11 -08:00
|
|
|
constructor(props) {
|
2016-07-13 12:44:24 -08:00
|
|
|
super(props);
|
2016-09-21 06:27:11 -08:00
|
|
|
this.handleFocusActive = this.handleFocusActive.bind(this);
|
2018-01-21 02:21:16 -09:00
|
|
|
this.handleSelectAll = this.handleSelectAll.bind(this);
|
2016-07-13 12:44:24 -08:00
|
|
|
this.onTermsRef = this.onTermsRef.bind(this);
|
2017-11-02 18:51:18 -08:00
|
|
|
this.mousetrap = null;
|
2017-11-14 14:55:21 -09:00
|
|
|
this.state = {
|
|
|
|
|
lastConfigUpdate: 0
|
|
|
|
|
};
|
2016-07-13 12:44:24 -08:00
|
|
|
}
|
2019-10-10 11:20:26 -08:00
|
|
|
//TODO: Remove usage of legacy and soon deprecated lifecycle methods
|
|
|
|
|
UNSAFE_componentWillReceiveProps(next) {
|
2016-07-13 12:44:24 -08:00
|
|
|
if (this.props.backgroundColor !== next.backgroundColor) {
|
2016-09-18 22:47:33 -08:00
|
|
|
// this can be removed when `setBackgroundColor` in electron
|
|
|
|
|
// starts working again
|
2016-07-13 12:44:24 -08:00
|
|
|
document.body.style.backgroundColor = next.backgroundColor;
|
|
|
|
|
}
|
2017-11-14 14:55:21 -09:00
|
|
|
const {lastConfigUpdate} = next;
|
|
|
|
|
if (lastConfigUpdate && lastConfigUpdate !== this.state.lastConfigUpdate) {
|
|
|
|
|
this.setState({lastConfigUpdate});
|
|
|
|
|
this.attachKeyListeners();
|
|
|
|
|
}
|
2016-07-13 12:44:24 -08:00
|
|
|
}
|
|
|
|
|
|
2019-10-10 11:20:26 -08:00
|
|
|
handleFocusActive(uid) {
|
|
|
|
|
const term = this.terms.getTermByUid(uid);
|
2016-09-21 06:27:11 -08:00
|
|
|
if (term) {
|
|
|
|
|
term.focus();
|
|
|
|
|
}
|
2016-07-13 12:44:24 -08:00
|
|
|
}
|
|
|
|
|
|
2018-01-21 02:21:16 -09:00
|
|
|
handleSelectAll() {
|
|
|
|
|
const term = this.terms.getActiveTerm();
|
|
|
|
|
if (term) {
|
|
|
|
|
term.selectAll();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-21 06:27:11 -08:00
|
|
|
attachKeyListeners() {
|
2017-11-02 18:51:18 -08:00
|
|
|
if (!this.mousetrap) {
|
|
|
|
|
this.mousetrap = new Mousetrap(window, true);
|
|
|
|
|
this.mousetrap.stopCallback = () => {
|
|
|
|
|
// All events should be intercepted even if focus is in an input/textarea
|
|
|
|
|
return false;
|
|
|
|
|
};
|
|
|
|
|
} else {
|
|
|
|
|
this.mousetrap.reset();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const keys = getRegisteredKeys();
|
|
|
|
|
Object.keys(keys).forEach(commandKeys => {
|
|
|
|
|
this.mousetrap.bind(
|
|
|
|
|
commandKeys,
|
|
|
|
|
e => {
|
|
|
|
|
const command = keys[commandKeys];
|
|
|
|
|
// We should tell to xterm that it should ignore this event.
|
|
|
|
|
e.catched = true;
|
|
|
|
|
this.props.execCommand(command, getCommandHandler(command), e);
|
|
|
|
|
shouldPreventDefault(command) && e.preventDefault();
|
|
|
|
|
},
|
|
|
|
|
'keydown'
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
|
this.attachKeyListeners();
|
2018-01-21 02:21:16 -09:00
|
|
|
window.rpc.on('term selectAll', this.handleSelectAll);
|
2016-07-13 12:44:24 -08:00
|
|
|
}
|
|
|
|
|
|
2016-09-21 06:27:11 -08:00
|
|
|
onTermsRef(terms) {
|
2016-07-13 12:44:24 -08:00
|
|
|
this.terms = terms;
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-21 06:27:11 -08:00
|
|
|
componentDidUpdate(prev) {
|
2016-07-13 12:44:24 -08:00
|
|
|
if (prev.activeSession !== this.props.activeSession) {
|
2019-10-10 11:20:26 -08:00
|
|
|
this.handleFocusActive(this.props.activeSession);
|
2016-07-13 12:44:24 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-21 06:27:11 -08:00
|
|
|
componentWillUnmount() {
|
2016-07-13 12:44:24 -08:00
|
|
|
document.body.style.backgroundColor = 'inherit';
|
2018-05-12 05:39:51 -08:00
|
|
|
this.mousetrap && this.mousetrap.reset();
|
2016-07-13 12:44:24 -08:00
|
|
|
}
|
|
|
|
|
|
2018-03-17 04:51:36 -08:00
|
|
|
render() {
|
2019-05-08 17:43:50 -08:00
|
|
|
const {isMac: isMac_, customCSS, uiFontFamily, borderColor, maximized, fullScreen} = this.props;
|
2017-09-10 05:35:10 -08:00
|
|
|
const borderWidth = isMac_ ? '' : `${maximized ? '0' : '1'}px`;
|
2017-01-10 20:45:49 -09:00
|
|
|
|
2017-08-31 05:20:39 -08:00
|
|
|
return (
|
2018-03-17 04:51:36 -08:00
|
|
|
<div id="hyper">
|
2017-08-31 05:20:39 -08:00
|
|
|
<div
|
|
|
|
|
style={{fontFamily: uiFontFamily, borderColor, borderWidth}}
|
2019-05-08 17:43:50 -08:00
|
|
|
className={`hyper_main ${isMac_ && 'hyper_mainRounded'} ${fullScreen ? 'fullScreen' : ''}`}
|
2016-09-21 06:27:11 -08:00
|
|
|
>
|
2017-09-10 05:35:10 -08:00
|
|
|
<HeaderContainer />
|
|
|
|
|
<TermsContainer ref_={this.onTermsRef} />
|
|
|
|
|
{this.props.customInnerChildren}
|
2017-08-31 05:20:39 -08:00
|
|
|
</div>
|
|
|
|
|
|
2017-09-10 05:35:10 -08:00
|
|
|
<NotificationsContainer />
|
2018-03-17 04:51:36 -08:00
|
|
|
|
2017-09-10 05:35:10 -08:00
|
|
|
{this.props.customChildren}
|
2018-03-17 04:51:36 -08:00
|
|
|
|
|
|
|
|
<style jsx>
|
|
|
|
|
{`
|
|
|
|
|
.hyper_main {
|
|
|
|
|
position: fixed;
|
|
|
|
|
top: 0;
|
|
|
|
|
left: 0;
|
|
|
|
|
right: 0;
|
|
|
|
|
bottom: 0;
|
|
|
|
|
border: 1px solid #333;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.hyper_mainRounded {
|
|
|
|
|
border-radius: 5px;
|
|
|
|
|
}
|
|
|
|
|
`}
|
|
|
|
|
</style>
|
|
|
|
|
|
|
|
|
|
{/*
|
|
|
|
|
Add custom CSS to Hyper.
|
|
|
|
|
We add a scope to the customCSS so that it can get around the weighting applied by styled-jsx
|
|
|
|
|
*/}
|
|
|
|
|
<style dangerouslySetInnerHTML={{__html: stylis('#hyper', customCSS, {prefix: false})}} />
|
2016-07-13 12:44:24 -08:00
|
|
|
</div>
|
2017-08-31 05:20:39 -08:00
|
|
|
);
|
2016-07-13 12:44:24 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-08 08:26:07 -08:00
|
|
|
const HyperContainer = connect(
|
2016-09-21 06:27:11 -08:00
|
|
|
state => {
|
2016-07-13 12:44:24 -08:00
|
|
|
return {
|
|
|
|
|
isMac,
|
|
|
|
|
customCSS: state.ui.css,
|
2017-02-17 18:15:55 -09:00
|
|
|
uiFontFamily: state.ui.uiFontFamily,
|
2016-07-13 12:44:24 -08:00
|
|
|
borderColor: state.ui.borderColor,
|
|
|
|
|
activeSession: state.sessions.activeUid,
|
2017-01-10 20:45:49 -09:00
|
|
|
backgroundColor: state.ui.backgroundColor,
|
2017-11-14 14:55:21 -09:00
|
|
|
maximized: state.ui.maximized,
|
2019-05-08 17:43:50 -08:00
|
|
|
fullScreen: state.ui.fullScreen,
|
2017-11-14 14:55:21 -09:00
|
|
|
lastConfigUpdate: state.ui._lastUpdate
|
2016-07-13 12:44:24 -08:00
|
|
|
};
|
|
|
|
|
},
|
2016-09-21 06:27:11 -08:00
|
|
|
dispatch => {
|
2016-07-13 12:44:24 -08:00
|
|
|
return {
|
2017-11-02 18:51:18 -08:00
|
|
|
execCommand: (command, fn, e) => {
|
|
|
|
|
dispatch(uiActions.execCommand(command, fn, e));
|
2016-07-13 12:44:24 -08:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
},
|
|
|
|
|
null,
|
2019-10-10 11:20:26 -08:00
|
|
|
{forwardRef: true}
|
2016-10-08 08:26:07 -08:00
|
|
|
)(Hyper, 'Hyper');
|
2016-07-13 12:44:24 -08:00
|
|
|
|
2016-10-08 08:26:07 -08:00
|
|
|
export default HyperContainer;
|