hyper/lib/containers/hyper.tsx

174 lines
4.9 KiB
TypeScript
Raw Normal View History

2016-07-13 12:44:24 -08:00
import React from 'react';
import Mousetrap from 'mousetrap';
import {connect} from '../utils/plugins';
import * as uiActions from '../actions/ui';
import {getRegisteredKeys, getCommandHandler, shouldPreventDefault} from '../command-registry';
import stylis from 'stylis';
2019-12-03 03:03:52 -09:00
import {HeaderContainer} from './header';
2016-07-13 12:44:24 -08:00
import TermsContainer from './terms';
import NotificationsContainer from './notifications';
import {HyperState, HyperProps, HyperDispatch} from '../hyper';
2016-07-13 12:44:24 -08:00
const isMac = /Mac/.test(navigator.userAgent);
class Hyper extends React.PureComponent<HyperProps, {lastConfigUpdate: number}> {
mousetrap!: MousetrapInstance;
terms: any;
constructor(props: any) {
2016-07-13 12:44:24 -08:00
super(props);
this.state = {
lastConfigUpdate: 0
};
2016-07-13 12:44:24 -08:00
}
Update Electron to v6 (#3785) * 3.0.0 * 3.0.2 * Save * Save * Upgrade yarn lock packages * update node-gyp and node-pty * update travis and appveyor to node 12 * appveyor is outdated as always * update travis to xenial * update node-pty@0.9.0-beta26 * update yarn.lock * update electron to 6.0.8 * move node-pty to the correct package.json * Fix linting failure * Update yarn lockfile to try to fix appveyor build * Remove unnecessary changes from package.json * Try to fix appveyor by using a newer image * Fix linting after my last change * update electron to 6.0.9 * install windows-build-tools on appveyor * fix syntax * switch back to 2017 image * remove old resolutions field * revert accidental version change * update electron to 6.0.11 and electron-rebuild to 1.8.6 * downgrade yarn to 1.18 until this issue is resolved https://github.com/yarnpkg/yarn/issues/7584 * update node-gyp to 6.0.0 and generate a fresh yarn lockfile * update react and a few other dependencies * fix lint * this should actually be electron-builder, I think! * update a few dependencies * change to electron-store electron-config was renamed to electron-store a while ago * update xterm to v4.1.0 and ora to 4.0.2 * move pify to app/package.json * TODO: Revert maybe. Throw a fit on every change to maybe fix the resizing issues * a * fix react ref problem * fix split view focus problem * remove the unnecessary fit * remove the init col and row * fix the problem that cannot show about hyper * update electron to 6.0.12 * fix lint * add more todos for componentWillReceiveProps deprecation * update babel and plugins Co-authored-by: Juan Campa <juancampa@gmail.com> Co-authored-by: Benjamin Staneck <staneck@gmail.com> Co-authored-by: ivan <ivanwonder@outlook.com>
2019-10-10 11:20:26 -08:00
//TODO: Remove usage of legacy and soon deprecated lifecycle methods
UNSAFE_componentWillReceiveProps(next: HyperProps) {
2016-07-13 12:44:24 -08:00
if (this.props.backgroundColor !== next.backgroundColor) {
// 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;
}
const {lastConfigUpdate} = next;
if (lastConfigUpdate && lastConfigUpdate !== this.state.lastConfigUpdate) {
this.setState({lastConfigUpdate});
this.attachKeyListeners();
}
2016-07-13 12:44:24 -08:00
}
2019-11-25 07:16:00 -09:00
handleFocusActive = (uid: string) => {
Update Electron to v6 (#3785) * 3.0.0 * 3.0.2 * Save * Save * Upgrade yarn lock packages * update node-gyp and node-pty * update travis and appveyor to node 12 * appveyor is outdated as always * update travis to xenial * update node-pty@0.9.0-beta26 * update yarn.lock * update electron to 6.0.8 * move node-pty to the correct package.json * Fix linting failure * Update yarn lockfile to try to fix appveyor build * Remove unnecessary changes from package.json * Try to fix appveyor by using a newer image * Fix linting after my last change * update electron to 6.0.9 * install windows-build-tools on appveyor * fix syntax * switch back to 2017 image * remove old resolutions field * revert accidental version change * update electron to 6.0.11 and electron-rebuild to 1.8.6 * downgrade yarn to 1.18 until this issue is resolved https://github.com/yarnpkg/yarn/issues/7584 * update node-gyp to 6.0.0 and generate a fresh yarn lockfile * update react and a few other dependencies * fix lint * this should actually be electron-builder, I think! * update a few dependencies * change to electron-store electron-config was renamed to electron-store a while ago * update xterm to v4.1.0 and ora to 4.0.2 * move pify to app/package.json * TODO: Revert maybe. Throw a fit on every change to maybe fix the resizing issues * a * fix react ref problem * fix split view focus problem * remove the unnecessary fit * remove the init col and row * fix the problem that cannot show about hyper * update electron to 6.0.12 * fix lint * add more todos for componentWillReceiveProps deprecation * update babel and plugins Co-authored-by: Juan Campa <juancampa@gmail.com> Co-authored-by: Benjamin Staneck <staneck@gmail.com> Co-authored-by: ivan <ivanwonder@outlook.com>
2019-10-10 11:20:26 -08:00
const term = this.terms.getTermByUid(uid);
if (term) {
term.focus();
}
2019-11-25 07:16:00 -09:00
};
2016-07-13 12:44:24 -08:00
2019-11-25 07:16:00 -09:00
handleSelectAll = () => {
const term = this.terms.getActiveTerm();
if (term) {
term.selectAll();
}
2019-11-25 07:16:00 -09:00
};
attachKeyListeners() {
if (!this.mousetrap) {
this.mousetrap = new (Mousetrap as any)(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: Record<string, any> = getRegisteredKeys();
Object.keys(keys).forEach(commandKeys => {
this.mousetrap.bind(
commandKeys,
(e: any) => {
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();
window.rpc.on('term selectAll', this.handleSelectAll);
2016-07-13 12:44:24 -08:00
}
2019-11-25 07:16:00 -09:00
onTermsRef = (terms: any) => {
2016-07-13 12:44:24 -08:00
this.terms = terms;
window.focusActiveTerm = this.handleFocusActive;
2019-11-25 07:16:00 -09:00
};
2016-07-13 12:44:24 -08:00
componentDidUpdate(prev: HyperProps) {
2016-07-13 12:44:24 -08:00
if (prev.activeSession !== this.props.activeSession) {
this.handleFocusActive(this.props.activeSession!);
2016-07-13 12:44:24 -08:00
}
}
componentWillUnmount() {
2016-07-13 12:44:24 -08:00
document.body.style.backgroundColor = 'inherit';
this.mousetrap && this.mousetrap.reset();
2016-07-13 12:44:24 -08:00
}
render() {
const {isMac: isMac_, customCSS, uiFontFamily, borderColor, maximized, fullScreen} = this.props;
const borderWidth = isMac_ ? '' : `${maximized ? '0' : '1'}px`;
stylis.set({prefix: false});
return (
<div id="hyper">
<div
style={{fontFamily: uiFontFamily, borderColor, borderWidth}}
className={`hyper_main ${isMac_ && 'hyper_mainRounded'} ${fullScreen ? 'fullScreen' : ''}`}
>
<HeaderContainer />
<TermsContainer ref_={this.onTermsRef} />
{this.props.customInnerChildren}
</div>
<NotificationsContainer />
{this.props.customChildren}
<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)}} />
2016-07-13 12:44:24 -08:00
</div>
);
2016-07-13 12:44:24 -08:00
}
}
const mapStateToProps = (state: HyperState) => {
return {
isMac,
customCSS: state.ui.css,
uiFontFamily: state.ui.uiFontFamily,
borderColor: state.ui.borderColor,
activeSession: state.sessions.activeUid,
backgroundColor: state.ui.backgroundColor,
maximized: state.ui.maximized,
fullScreen: state.ui.fullScreen,
lastConfigUpdate: state.ui._lastUpdate
};
};
const mapDispatchToProps = (dispatch: HyperDispatch) => {
return {
execCommand: (command: any, fn: any, e: any) => {
dispatch(uiActions.execCommand(command, fn, e));
}
};
};
const HyperContainer = connect(mapStateToProps, mapDispatchToProps, null, {forwardRef: true})(Hyper, 'Hyper');
2016-07-13 12:44:24 -08:00
export default HyperContainer;
export type HyperConnectedProps = ReturnType<typeof mapStateToProps> & ReturnType<typeof mapDispatchToProps>;