2021-07-22 23:04:56 +00:00
|
|
|
import { autoBindMethodsForReact } from 'class-autobind-decorator';
|
2021-05-12 06:35:00 +00:00
|
|
|
import { PureComponent, ReactNode } from 'react';
|
2017-09-17 14:04:56 +00:00
|
|
|
import ReactDOM from 'react-dom';
|
2021-07-22 23:04:56 +00:00
|
|
|
|
|
|
|
import { AUTOBIND_CFG, isMac } from '../../common/constants';
|
2017-09-17 14:04:56 +00:00
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
interface Props {
|
2021-09-08 18:46:03 +00:00
|
|
|
children?: ReactNode;
|
2021-05-18 20:32:18 +00:00
|
|
|
onKeydown?: (...args: any[]) => any;
|
|
|
|
onKeyup?: (...args: any[]) => any;
|
2021-05-12 06:35:00 +00:00
|
|
|
disabled?: boolean;
|
|
|
|
scoped?: boolean;
|
|
|
|
stopMetaPropagation?: boolean;
|
|
|
|
}
|
2017-09-17 14:04:56 +00:00
|
|
|
|
2021-02-02 22:23:42 +00:00
|
|
|
@autoBindMethodsForReact(AUTOBIND_CFG)
|
2021-09-27 13:47:22 +00:00
|
|
|
export class KeydownBinder extends PureComponent<Props> {
|
2018-06-25 17:42:50 +00:00
|
|
|
_handleKeydown(e: KeyboardEvent) {
|
|
|
|
const { stopMetaPropagation, onKeydown, disabled } = this.props;
|
2017-09-17 14:04:56 +00:00
|
|
|
|
|
|
|
if (disabled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const isMeta = isMac() ? e.metaKey : e.ctrlKey;
|
2021-05-12 06:35:00 +00:00
|
|
|
|
2017-09-17 14:04:56 +00:00
|
|
|
if (stopMetaPropagation && isMeta) {
|
|
|
|
e.stopPropagation();
|
|
|
|
}
|
|
|
|
|
2019-05-10 01:18:34 +00:00
|
|
|
if (onKeydown) {
|
|
|
|
onKeydown(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_handleKeyup(e: KeyboardEvent) {
|
|
|
|
const { stopMetaPropagation, onKeyup, disabled } = this.props;
|
|
|
|
|
|
|
|
if (disabled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const isMeta = isMac() ? e.metaKey : e.ctrlKey;
|
2021-05-12 06:35:00 +00:00
|
|
|
|
2019-05-10 01:18:34 +00:00
|
|
|
if (stopMetaPropagation && isMeta) {
|
|
|
|
e.stopPropagation();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (onKeyup) {
|
|
|
|
onKeyup(e);
|
|
|
|
}
|
2017-09-17 14:04:56 +00:00
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
componentDidMount() {
|
2017-09-17 14:04:56 +00:00
|
|
|
if (this.props.scoped) {
|
|
|
|
const el = ReactDOM.findDOMNode(this);
|
2021-07-30 03:32:08 +00:00
|
|
|
el?.addEventListener('keydown', this._handleKeydown);
|
|
|
|
el?.addEventListener('keyup', this._handleKeyup);
|
2017-09-17 14:04:56 +00:00
|
|
|
} else {
|
2018-10-17 16:42:33 +00:00
|
|
|
document.body && document.body.addEventListener('keydown', this._handleKeydown);
|
2019-05-10 01:18:34 +00:00
|
|
|
document.body && document.body.addEventListener('keyup', this._handleKeyup);
|
2017-09-17 14:04:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-18 17:44:08 +00:00
|
|
|
componentWillUnmount() {
|
2017-09-17 14:04:56 +00:00
|
|
|
if (this.props.scoped) {
|
|
|
|
const el = ReactDOM.findDOMNode(this);
|
2021-07-30 03:32:08 +00:00
|
|
|
el?.removeEventListener('keydown', this._handleKeydown);
|
|
|
|
el?.removeEventListener('keyup', this._handleKeyup);
|
2017-09-17 14:04:56 +00:00
|
|
|
} else {
|
2018-10-17 16:42:33 +00:00
|
|
|
document.body && document.body.removeEventListener('keydown', this._handleKeydown);
|
2019-05-10 01:18:34 +00:00
|
|
|
document.body && document.body.removeEventListener('keyup', this._handleKeyup);
|
2017-09-17 14:04:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
render() {
|
2021-09-08 18:46:03 +00:00
|
|
|
return this.props.children ?? null;
|
2017-09-17 14:04:56 +00:00
|
|
|
}
|
|
|
|
}
|