forked from atom/github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopen-commit-dialog.js
More file actions
113 lines (96 loc) · 3.15 KB
/
open-commit-dialog.js
File metadata and controls
113 lines (96 loc) · 3.15 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 {CompositeDisposable} from 'event-kit';
import Commands, {Command} from '../atom/commands';
export default class OpenCommitDialog extends React.Component {
static propTypes = {
commandRegistry: PropTypes.object.isRequired,
didAccept: PropTypes.func.isRequired,
didCancel: PropTypes.func.isRequired,
isValidEntry: PropTypes.func.isRequired,
}
constructor(props, context) {
super(props, context);
this.state = {
error: null,
};
this.subs = new CompositeDisposable();
}
componentDidMount() {
setTimeout(() => this.commitRefElement.focus());
}
componentWillUnmount() {
this.subs.dispose();
}
render() {
return this.renderDialog();
}
renderDialog() {
return (
<div className="github-Dialog github-OpenCommit modal">
<Commands registry={this.props.commandRegistry} target=".github-OpenCommit">
<Command command="core:cancel" callback={this.cancel} />
<Command command="core:confirm" callback={this.accept} />
</Commands>
<main className="github-DialogInputs">
<label className="github-DialogLabel github-CommitRef">
Commit sha or Git ref:
<atom-text-editor mini={true} ref={this.editorRefs('commitRef')} tabIndex="1" />
</label>
{this.state.error && <span className="error">{this.state.error}</span>}
</main>
<div className="github-DialogButtons">
<button className="btn github-CancelButton" onClick={this.cancel} tabIndex="3">
Cancel
</button>
<button
className="btn btn-primary icon icon-commit"
onClick={this.accept}
disabled={!!this.state.error || this.getCommitRef().length === 0}
tabIndex="2">
Open Commit
</button>
</div>
</div>
);
}
accept = async () => {
const ref = this.getCommitRef();
const valid = await this.props.isValidEntry(ref);
if (valid === true) {
this.props.didAccept({ref});
} else {
this.setState({error: `There is no commit associated with "${ref}" in this repository`});
}
}
cancel = () => this.props.didCancel()
editorRefs = baseName => {
const elementName = `${baseName}Element`;
const modelName = `${baseName}Editor`;
const subName = `${baseName}Subs`;
const changeMethodName = `didChange${baseName[0].toUpperCase()}${baseName.substring(1)}`;
return element => {
if (!element) {
return;
}
this[elementName] = element;
const editor = element.getModel();
if (this[modelName] !== editor) {
this[modelName] = editor;
/* istanbul ignore if */
if (this[subName]) {
this[subName].dispose();
this.subs.remove(this[subName]);
}
this[subName] = editor.onDidChange(this[changeMethodName]);
this.subs.add(this[subName]);
}
};
}
didChangeCommitRef = () => new Promise(resolve => {
this.setState({error: null}, resolve);
})
getCommitRef() {
return this.commitRefEditor ? this.commitRefEditor.getText() : '';
}
}