hyper/lib/containers/hyper.tsx

176 lines
5 KiB
TypeScript
Raw Normal View History

2016-07-13 12:44:24 -08:00
import React from 'react';
2021-05-06 06:01:57 -08:00
import Mousetrap, {MousetrapInstance} 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';
2020-06-19 04:51:34 -08:00
import Terms from '../components/terms';
2016-07-13 12:44:24 -08:00
const isMac = /Mac/.test(navigator.userAgent);
class Hyper extends React.PureComponent<HyperProps> {
mousetrap!: MousetrapInstance;
2020-06-19 04:51:34 -08:00
terms!: Terms;
2020-03-07 05:38:46 -09:00
constructor(props: HyperProps) {
2016-07-13 12:44:24 -08:00
super(props);
}
componentDidUpdate(prev: HyperProps) {
if (this.props.backgroundColor !== prev.backgroundColor) {
// this can be removed when `setBackgroundColor` in electron
// starts working again
document.body.style.backgroundColor = this.props.backgroundColor;
2016-07-13 12:44:24 -08:00
}
const {lastConfigUpdate} = this.props;
if (lastConfigUpdate && lastConfigUpdate !== prev.lastConfigUpdate) {
this.attachKeyListeners();
}
if (prev.activeSession !== this.props.activeSession) {
this.handleFocusActive(this.props.activeSession!);
}
2016-07-13 12:44:24 -08:00
}
2020-06-19 04:51:34 -08:00
handleFocusActive = (uid?: string) => {
const term = uid && 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) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
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();
}
2020-03-07 05:38:46 -09:00
const keys = getRegisteredKeys();
2020-03-25 02:15:08 -08:00
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 as any).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
}
2020-06-19 04:51:34 -08:00
onTermsRef = (terms: Terms) => {
2016-07-13 12:44:24 -08:00
this.terms = terms;
2021-06-20 05:24:54 -08:00
window.focusActiveTerm = (uid?: string) => {
if (uid) {
this.handleFocusActive(uid);
} else {
this.terms.getActiveTerm().focus();
}
};
2019-11-25 07:16:00 -09:00
};
2016-07-13 12:44:24 -08:00
componentWillUnmount() {
2016-07-13 12:44:24 -08:00
document.body.style.backgroundColor = 'inherit';
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 {
2023-01-28 01:00:37 -09:00
border-radius: 10.5px;
overflow: hidden;
}
`}
</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 {
2020-06-19 04:51:34 -08:00
execCommand: (command: string, fn: (e: any, dispatch: HyperDispatch) => void, 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>;