/* eslint-disable react/no-danger */ 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'; import {HeaderContainer} from './header'; import TermsContainer from './terms'; import NotificationsContainer from './notifications'; import {HyperState} from '../hyper'; import {Dispatch} from 'redux'; const isMac = /Mac/.test(navigator.userAgent); class Hyper extends React.PureComponent { mousetrap!: MousetrapInstance; terms: any; constructor(props: any) { super(props); this.state = { lastConfigUpdate: 0 }; } //TODO: Remove usage of legacy and soon deprecated lifecycle methods UNSAFE_componentWillReceiveProps(next: any) { if (this.props.backgroundColor !== next.backgroundColor) { // this can be removed when `setBackgroundColor` in electron // starts working again document.body.style.backgroundColor = next.backgroundColor; } const {lastConfigUpdate} = next; if (lastConfigUpdate && lastConfigUpdate !== this.state.lastConfigUpdate) { this.setState({lastConfigUpdate}); this.attachKeyListeners(); } } handleFocusActive = (uid: string) => { const term = this.terms.getTermByUid(uid); if (term) { term.focus(); } }; handleSelectAll = () => { const term = this.terms.getActiveTerm(); if (term) { term.selectAll(); } }; attachKeyListeners() { if (!this.mousetrap) { this.mousetrap = new Mousetrap(); 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 = 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); } onTermsRef = (terms: any) => { this.terms = terms; window.focusActiveTerm = this.handleFocusActive; }; componentDidUpdate(prev: any) { if (prev.activeSession !== this.props.activeSession) { this.handleFocusActive(this.props.activeSession); } } componentWillUnmount() { document.body.style.backgroundColor = 'inherit'; this.mousetrap && this.mousetrap.reset(); } render() { const {isMac: isMac_, customCSS, uiFontFamily, borderColor, maximized, fullScreen} = this.props; const borderWidth = isMac_ ? '' : `${maximized ? '0' : '1'}px`; stylis.set({prefix: false}); return (
{this.props.customInnerChildren}
{this.props.customChildren} {/* Add custom CSS to Hyper. We add a scope to the customCSS so that it can get around the weighting applied by styled-jsx */}