forked from atom/github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathactionable-review-view.js
More file actions
175 lines (153 loc) · 5.05 KB
/
Copy pathactionable-review-view.js
File metadata and controls
175 lines (153 loc) · 5.05 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import React from 'react';
import PropTypes from 'prop-types';
import cx from 'classnames';
import {remote, shell} from 'electron';
import {TextBuffer} from 'atom';
import AtomTextEditor from '../atom/atom-text-editor';
import RefHolder from '../models/ref-holder';
import {addEvent} from '../reporter-proxy';
import Commands, {Command} from '../atom/commands';
const {Menu, MenuItem} = remote;
export default class ActionableReviewView extends React.Component {
static propTypes = {
// Model
originalContent: PropTypes.object.isRequired,
isPosting: PropTypes.bool,
// Atom environment
commands: PropTypes.object.isRequired,
confirm: PropTypes.func.isRequired,
// Action methods
contentUpdater: PropTypes.func.isRequired,
createMenu: PropTypes.func,
createMenuItem: PropTypes.func,
// Render prop
render: PropTypes.func.isRequired,
}
static defaultProps = {
createMenu: /* istanbul ignore next */ () => new Menu(),
createMenuItem: /* istanbul ignore next */ (...args) => new MenuItem(...args),
}
constructor(props) {
super(props);
this.refEditor = new RefHolder();
this.refRoot = new RefHolder();
this.buffer = new TextBuffer();
this.state = {editing: false};
}
componentDidUpdate(prevProps, prevState) {
if (this.state.editing && !prevState.editing) {
this.buffer.setText(this.props.originalContent.body);
this.refEditor.map(e => e.getElement().focus());
}
}
render() {
return this.state.editing ? this.renderEditor() : this.props.render(this.showActionsMenu);
}
renderEditor() {
const className = cx('github-Review-editable', {'github-Review-editable--disabled': this.props.isPosting});
return (
<div className={className} ref={this.refRoot.setter}>
{this.renderCommands()}
<AtomTextEditor
buffer={this.buffer}
lineNumberGutterVisible={false}
softWrapped={true}
autoHeight={true}
readOnly={this.props.isPosting}
refModel={this.refEditor}
/>
<footer className="github-Review-editable-footer">
<button
className="github-Review-editableCancelButton btn btn-sm"
title="Cancel editing comment"
disabled={this.props.isPosting}
onClick={this.onCancel}>
Cancel
</button>
<button
className="github-Review-updateCommentButton btn btn-sm btn-primary"
title="Update comment"
disabled={this.props.isPosting}
onClick={this.onSubmitUpdate}>
Update comment
</button>
</footer>
</div>
);
}
renderCommands() {
return (
<Commands registry={this.props.commands} target={this.refRoot}>
<Command command="github:submit-comment" callback={this.onSubmitUpdate} />
<Command command="core:cancel" callback={this.onCancel} />
</Commands>
);
}
onCancel = () => {
if (this.buffer.getText() === this.props.originalContent.body) {
this.setState({editing: false});
} else {
const choice = this.props.confirm({
message: 'Are you sure you want to discard your unsaved changes?',
buttons: ['OK', 'Cancel'],
});
if (choice === 0) {
this.setState({editing: false});
}
}
}
onSubmitUpdate = async () => {
const text = this.buffer.getText();
if (text === this.props.originalContent.body || text === '') {
this.setState({editing: false});
return;
}
try {
await this.props.contentUpdater(this.props.originalContent.id, text);
this.setState({editing: false});
} catch (e) {
this.buffer.setText(text);
}
}
reportAbuse = (commentUrl, author) => {
return new Promise((resolve, reject) => {
const url = 'https://github.com/contact/report-content?report=' +
`${encodeURIComponent(author)}&content_url=${encodeURIComponent(commentUrl)}`;
shell.openExternal(url, {}, err => {
if (err) { reject(err); } else {
resolve();
addEvent('report-abuse', {package: 'github', component: this.constructor.name});
}
});
});
}
openOnGitHub = url => {
return new Promise((resolve, reject) => {
shell.openExternal(url, {}, err => {
if (err) { reject(err); } else {
resolve();
addEvent('open-comment-in-browser', {package: 'github', component: this.constructor.name});
}
});
});
}
showActionsMenu = (event, content, author) => {
event.preventDefault();
const menu = this.props.createMenu();
if (content.viewerCanUpdate) {
menu.append(this.props.createMenuItem({
label: 'Edit',
click: () => this.setState({editing: true}),
}));
}
menu.append(this.props.createMenuItem({
label: 'Open on GitHub',
click: () => this.openOnGitHub(content.url),
}));
menu.append(this.props.createMenuItem({
label: 'Report abuse',
click: () => this.reportAbuse(content.url, author.login),
}));
menu.popup(remote.getCurrentWindow());
}
}