hyper/lib/containers/hyper.tsx

168 lines
4.7 KiB
TypeScript
Raw Permalink Normal View History

import React, {forwardRef, useEffect, useRef} from 'react';
2023-07-25 09:30:19 -08:00
2023-06-26 01:29:50 -08:00
import Mousetrap from 'mousetrap';
2023-07-25 09:30:19 -08:00
import type {MousetrapInstance} from 'mousetrap';
import stylis from 'stylis';
2023-07-25 09:30:19 -08:00
import type {HyperState, HyperProps, HyperDispatch} from '../../typings/hyper';
import * as uiActions from '../actions/ui';
import {getRegisteredKeys, getCommandHandler, shouldPreventDefault} from '../command-registry';
2023-07-25 09:30:19 -08:00
import type Terms from '../components/terms';
import {connect} from '../utils/plugins';
2019-12-03 03:03:52 -09:00
import {HeaderContainer} from './header';
2016-07-13 12:44:24 -08:00
import NotificationsContainer from './notifications';
2023-07-25 09:30:19 -08:00
import TermsContainer from './terms';
2016-07-13 12:44:24 -08:00
const isMac = /Mac/.test(navigator.userAgent);
const Hyper = forwardRef<HTMLDivElement, HyperProps>((props, ref) => {
const mousetrap = useRef<MousetrapInstance | null>(null);
const terms = useRef<Terms | null>(null);
useEffect(() => {
void attachKeyListeners();
}, [props.lastConfigUpdate]);
useEffect(() => {
handleFocusActive(props.activeSession);
}, [props.activeSession]);
2016-07-13 12:44:24 -08:00
const handleFocusActive = (uid?: string | null) => {
const term = uid && terms.current?.getTermByUid(uid);
if (term) {
term.focus();
}
2019-11-25 07:16:00 -09:00
};
2016-07-13 12:44:24 -08:00
const handleSelectAll = () => {
const term = terms.current?.getActiveTerm();
if (term) {
term.selectAll();
}
2019-11-25 07:16:00 -09:00
};
const attachKeyListeners = async () => {
if (!mousetrap.current) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
mousetrap.current = new (Mousetrap as any)(window, true);
mousetrap.current!.stopCallback = () => {
// All events should be intercepted even if focus is in an input/textarea
return false;
};
} else {
mousetrap.current.reset();
}
2023-07-22 05:57:32 -08:00
const keys = await getRegisteredKeys();
2020-03-25 02:15:08 -08:00
Object.keys(keys).forEach((commandKeys) => {
mousetrap.current?.bind(
commandKeys,
(e) => {
const command = keys[commandKeys];
// We should tell xterm to ignore this event.
(e as any).catched = true;
props.execCommand(command, getCommandHandler(command), e);
shouldPreventDefault(command) && e.preventDefault();
},
'keydown'
);
});
};
useEffect(() => {
void attachKeyListeners();
window.rpc.on('term selectAll', handleSelectAll);
}, []);
2016-07-13 12:44:24 -08:00
2023-07-24 20:20:47 -08:00
const onTermsRef = (_terms: Terms | null) => {
terms.current = _terms;
2021-06-20 05:24:54 -08:00
window.focusActiveTerm = (uid?: string) => {
if (uid) {
handleFocusActive(uid);
2021-06-20 05:24:54 -08:00
} else {
terms.current?.getActiveTerm()?.focus();
2021-06-20 05:24:54 -08:00
}
};
2019-11-25 07:16:00 -09:00
};
2016-07-13 12:44:24 -08:00
useEffect(() => {
return () => {
mousetrap.current?.reset();
};
}, []);
const {isMac: isMac_, customCSS, uiFontFamily, borderColor, maximized, fullScreen} = props;
const borderWidth = isMac_ ? '' : `${maximized ? '0' : '1'}px`;
stylis.set({prefix: false});
return (
<div id="hyper" ref={ref}>
<div
style={{fontFamily: uiFontFamily, borderColor, borderWidth}}
className={`hyper_main ${isMac_ && 'hyper_mainRounded'} ${fullScreen ? 'fullScreen' : ''}`}
>
<HeaderContainer />
<TermsContainer ref_={onTermsRef} />
{props.customInnerChildren}
2016-07-13 12:44:24 -08:00
</div>
<NotificationsContainer />
{props.customChildren}
<style jsx>
{`
.hyper_main {
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
border: 1px solid #333;
}
.hyper_mainRounded {
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)}} />
</div>
);
});
Hyper.displayName = 'Hyper';
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>;