forked from atom/github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecent-commits-controller.js
More file actions
120 lines (100 loc) · 3.87 KB
/
recent-commits-controller.js
File metadata and controls
120 lines (100 loc) · 3.87 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
114
115
116
117
118
119
120
import React from 'react';
import PropTypes from 'prop-types';
import {addEvent} from '../reporter-proxy';
import {CompositeDisposable} from 'event-kit';
import CommitDetailItem from '../items/commit-detail-item';
import URIPattern from '../atom/uri-pattern';
import RecentCommitsView from '../views/recent-commits-view';
import RefHolder from '../models/ref-holder';
export default class RecentCommitsController extends React.Component {
static propTypes = {
commits: PropTypes.arrayOf(PropTypes.object).isRequired,
isLoading: PropTypes.bool.isRequired,
undoLastCommit: PropTypes.func.isRequired,
workspace: PropTypes.object.isRequired,
repository: PropTypes.object.isRequired,
commands: PropTypes.object.isRequired,
}
static focus = RecentCommitsView.focus
constructor(props, context) {
super(props, context);
this.subscriptions = new CompositeDisposable(
this.props.workspace.onDidChangeActivePaneItem(this.updateSelectedCommit),
);
this.refView = new RefHolder();
this.state = {selectedCommitSha: ''};
}
updateSelectedCommit = () => {
const activeItem = this.props.workspace.getActivePaneItem();
const pattern = new URIPattern(decodeURIComponent(
CommitDetailItem.buildURI(
this.props.repository.getWorkingDirectoryPath(),
'{sha}'),
));
if (activeItem && activeItem.getURI) {
const match = pattern.matches(activeItem.getURI());
const {sha} = match.getParams();
if (match.ok() && sha && sha !== this.state.selectedCommitSha) {
return new Promise(resolve => this.setState({selectedCommitSha: sha}, resolve));
}
}
return Promise.resolve();
}
render() {
return (
<RecentCommitsView
ref={this.refView.setter}
commits={this.props.commits}
isLoading={this.props.isLoading}
undoLastCommit={this.props.undoLastCommit}
openCommit={this.openCommit}
selectNextCommit={this.selectNextCommit}
selectPreviousCommit={this.selectPreviousCommit}
selectedCommitSha={this.state.selectedCommitSha}
commands={this.props.commands}
/>
);
}
openCommit = async ({sha, preserveFocus}) => {
const workdir = this.props.repository.getWorkingDirectoryPath();
const uri = CommitDetailItem.buildURI(workdir, sha);
const item = await this.props.workspace.open(uri, {pending: true});
if (preserveFocus) {
item.preventFocus();
this.setFocus(this.constructor.focus.RECENT_COMMIT);
}
addEvent('open-commit-in-pane', {package: 'github', from: this.constructor.name});
}
// When no commit is selected, `getSelectedCommitIndex` returns -1 & the commit at index 0 (first commit) is selected
selectNextCommit = () => this.setSelectedCommitIndex(this.getSelectedCommitIndex() + 1);
selectPreviousCommit = () => this.setSelectedCommitIndex(Math.max(this.getSelectedCommitIndex() - 1, 0));
getSelectedCommitIndex() {
return this.props.commits.findIndex(commit => commit.getSha() === this.state.selectedCommitSha);
}
setSelectedCommitIndex(ind) {
const commit = this.props.commits[ind];
if (commit) {
return new Promise(resolve => this.setState({selectedCommitSha: commit.getSha()}, resolve));
} else {
return Promise.resolve();
}
}
getFocus(element) {
return this.refView.map(view => view.getFocus(element)).getOr(null);
}
setFocus(focus) {
return this.refView.map(view => {
const wasFocused = view.setFocus(focus);
if (wasFocused && this.getSelectedCommitIndex() === -1) {
this.setSelectedCommitIndex(0);
}
return wasFocused;
}).getOr(false);
}
advanceFocusFrom(focus) {
return this.refView.map(view => view.advanceFocusFrom(focus)).getOr(Promise.resolve(null));
}
retreatFocusFrom(focus) {
return this.refView.map(view => view.retreatFocusFrom(focus)).getOr(Promise.resolve(null));
}
}