Repository navigation
Expand file tree
/
Copy pathgutter.js
More file actions
113 lines (95 loc) · 2.66 KB
/
Copy pathgutter.js
File metadata and controls
113 lines (95 loc) · 2.66 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import React from 'react';
import PropTypes from 'prop-types';
import {Disposable} from 'event-kit';
import {autobind, extractProps} from '../helpers';
import {RefHolderPropType} from '../prop-types';
import {TextEditorContext} from './atom-text-editor';
import RefHolder from '../models/ref-holder';
const gutterProps = {
name: PropTypes.string.isRequired,
priority: PropTypes.number.isRequired,
visible: PropTypes.bool,
type: PropTypes.oneOf(['line-number', 'decorated']),
labelFn: PropTypes.func,
onMouseDown: PropTypes.func,
onMouseMove: PropTypes.func,
};
class BareGutter extends React.Component {
static propTypes = {
editorHolder: RefHolderPropType.isRequired,
className: PropTypes.string,
...gutterProps,
}
static defaultProps = {
visible: true,
type: 'decorated',
labelFn: () => {},
}
constructor(props) {
super(props);
autobind(this, 'observeEditor', 'forceUpdate');
this.state = {
gutter: null,
};
this.sub = new Disposable();
}
componentDidMount() {
this.sub = this.props.editorHolder.observe(this.observeEditor);
}
componentDidUpdate(prevProps) {
if (this.props.editorHolder !== prevProps.editorHolder) {
this.sub.dispose();
this.sub = this.props.editorHolder.observe(this.observeEditor);
}
}
componentWillUnmount() {
if (this.state.gutter !== null) {
try {
this.state.gutter.destroy();
} catch (e) {
// Gutter already destroyed. Disregard.
}
}
this.sub.dispose();
}
render() {
return null;
}
observeEditor(editor) {
this.setState((prevState, props) => {
if (prevState.gutter !== null) {
prevState.gutter.destroy();
}
const options = extractProps(props, gutterProps);
options.class = props.className;
return {gutter: editor.addGutter(options)};
});
}
}
export default class Gutter extends React.Component {
static propTypes = {
editor: PropTypes.object,
}
constructor(props) {
super(props);
this.state = {
editorHolder: RefHolder.on(this.props.editor),
};
}
static getDerivedStateFromProps(props, state) {
const editorChanged = state.editorHolder.map(editor => editor !== props.editor).getOr(props.editor !== undefined);
return editorChanged ? RefHolder.on(props.editor) : null;
}
render() {
if (!this.state.editorHolder.isEmpty()) {
return <BareGutter {...this.props} editorHolder={this.state.editorHolder} />;
}
return (
<TextEditorContext.Consumer>
{editorHolder => (
<BareGutter {...this.props} editorHolder={editorHolder} />
)}
</TextEditorContext.Consumer>
);
}
}