Repository navigation
Expand file tree
/
Copy pathkeystroke.js
More file actions
66 lines (55 loc) · 1.56 KB
/
Copy pathkeystroke.js
File metadata and controls
66 lines (55 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import React from 'react';
import PropTypes from 'prop-types';
import {humanizeKeystroke} from 'underscore-plus';
import {Disposable} from 'event-kit';
import {autobind} from '../helpers';
import {RefHolderPropType} from '../prop-types';
export default class Keystroke extends React.Component {
static propTypes = {
keymaps: PropTypes.shape({
findKeyBindings: PropTypes.func.isRequired,
}).isRequired,
command: PropTypes.string.isRequired,
refTarget: RefHolderPropType,
}
constructor(props) {
super(props);
autobind(this, 'didChangeTarget');
this.sub = new Disposable();
this.state = {keybinding: null};
}
componentDidMount() {
this.observeTarget();
}
componentDidUpdate(prevProps, prevState) {
if (this.props.refTarget !== prevProps.refTarget) {
this.observeTarget();
} else if (this.props.command !== prevProps.command) {
this.didChangeTarget(this.props.refTarget.getOr(null));
}
}
componentWillUnmount() {
this.sub.dispose();
}
render() {
if (!this.state.keybinding) {
return null;
}
return <span className="keystroke">{humanizeKeystroke(this.state.keybinding.keystrokes)}</span>;
}
observeTarget() {
this.sub.dispose();
if (this.props.refTarget) {
this.sub = this.props.refTarget.observe(this.didChangeTarget);
} else {
this.didChangeTarget(null);
}
}
didChangeTarget(target) {
const [keybinding] = this.props.keymaps.findKeyBindings({
command: this.props.command,
target,
});
this.setState({keybinding});
}
}