Skip to content
Navigation Menu
{{ message }}
forked from atom/github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommit-view.js
More file actions
344 lines (301 loc) · 10.8 KB
/
Copy pathcommit-view.js
File metadata and controls
344 lines (301 loc) · 10.8 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/** @jsx etch.dom */
/* eslint react/no-unknown-property: "off" */
import {TextEditor} from 'atom';
import {CompositeDisposable} from 'event-kit';
import etch from 'etch';
import {autobind} from 'core-decorators';
import cx from 'classnames';
import {shortenSha} from '../helpers';
const LINE_ENDING_REGEX = /\r?\n/;
export default class CommitView {
static focus = {
EDITOR: Symbol('commit-editor'),
ABORT_MERGE_BUTTON: Symbol('commit-abort-merge-button'),
AMEND_BOX: Symbol('commit-amend-box'),
COMMIT_BUTTON: Symbol('commit-button'),
};
constructor(props) {
this.props = props;
// We don't want the user to see the UI flicker in the case
// the commit takes a very small time to complete. Instead we
// will only show the working message if we are working for longer
// than 1 second as per https://www.nngroup.com/articles/response-times-3-important-limits/
//
// The closure is created to restrict variable access
this.shouldShowWorking = (() => {
let showWorking = false;
let timeoutHandle = null;
return () => {
if (this.props.isCommitting) {
if (!showWorking && timeoutHandle === null) {
timeoutHandle = setTimeout(() => {
showWorking = true;
etch.update(this);
timeoutHandle = null;
}, 1000);
}
} else {
clearTimeout(timeoutHandle);
timeoutHandle = null;
showWorking = false;
}
return showWorking;
};
})();
etch.initialize(this);
this.editor = this.refs.editor;
// FIXME Use props-injected view registry instead of the Atom global
this.editorElement = atom.views.getView(this.editor);
this.editor.setText(this.props.message || '');
this.subscriptions = new CompositeDisposable(
this.editor.onDidChange(() => this.props.onChangeMessage && this.props.onChangeMessage(this.editor.getText())),
this.editor.onDidChangeCursorPosition(() => { etch.update(this); }),
props.commandRegistry.add('atom-workspace', {
'github:commit': this.commit,
'github:toggle-expanded-commit-message-editor': this.toggleExpandedCommitMessageEditor,
}),
props.config.onDidChange('github.automaticCommitMessageWrapping', () => etch.update(this)),
);
this.registerTooltips();
}
destroy() {
this.subscriptions.dispose();
etch.destroy(this);
}
update(props) {
const previousMessage = this.props.message;
this.props = {...this.props, ...props};
const newMessage = this.props.message;
if (this.editor && previousMessage !== newMessage && this.editor.getText() !== newMessage) {
this.editor.setText(newMessage);
}
return etch.update(this);
}
render() {
let remainingCharsClassName = '';
if (this.getRemainingCharacters() < 0) {
remainingCharsClassName = 'is-error';
} else if (this.getRemainingCharacters() < this.props.maximumCharacterLimit / 4) {
remainingCharsClassName = 'is-warning';
}
const showAbortMergeButton = this.props.isMerging || null;
const showAmendBox = (
!this.props.isMerging &&
this.props.lastCommit.isPresent() &&
!this.props.lastCommit.isUnbornRef()
) || null;
return (
<div className="github-CommitView" ref="CommitView">
<div className={cx('github-CommitView-editor', {'is-expanded': this.props.deactivateCommitBox})}>
<TextEditor
ref="editor"
softWrapped={true}
placeholderText="Commit message"
lineNumberGutterVisible={false}
showInvisibles={false}
autoHeight={false}
scrollPastEnd={false}
/>
{this.renderHardWrapIcons()}
<button ref="expandButton" className="github-CommitView-expandButton icon icon-screen-full"
onClick={this.toggleExpandedCommitMessageEditor}
/>
</div>
<footer className="github-CommitView-bar">
{showAbortMergeButton &&
<button ref="abortMergeButton" className="btn github-CommitView-button is-secondary"
onclick={this.abortMerge}>Abort Merge</button>
}
{showAmendBox &&
<label className="github-CommitView-label input-label">
<input
ref="amend"
className="input-checkbox"
type="checkbox"
onclick={this.handleAmendBoxClick}
checked={this.props.isAmending}
/> Amend
</label>
}
<button ref="commitButton" className="btn github-CommitView-button"
onclick={this.commit}
disabled={!this.isCommitButtonEnabled()}>{this.commitButtonText()}</button>
<div ref="remainingCharacters"
className={`github-CommitView-remaining-characters ${remainingCharsClassName}`}>
{this.getRemainingCharacters()}
</div>
</footer>
</div>
);
}
@autobind
renderHardWrapIcons() {
const singleLineMessage = this.editor && this.editor.getText().split(LINE_ENDING_REGEX).length === 1;
const hardWrap = this.props.config.get('github.automaticCommitMessageWrapping');
const notApplicable = this.props.deactivateCommitBox || singleLineMessage;
const svgPaths = {
hardWrapEnabled: {
path1: 'M7.058 10.2h-.975v2.4L2 9l4.083-3.6v2.4h.97l1.202 1.203L7.058 10.2zm2.525-4.865V4.2h2.334v1.14l-1.164 1.165-1.17-1.17z', // eslint-disable-line max-len
path2: 'M7.842 6.94l2.063 2.063-2.122 2.12.908.91 2.123-2.123 1.98 1.98.85-.848L11.58 8.98l2.12-2.123-.824-.825-2.122 2.12-2.062-2.06z', // eslint-disable-line max-len
},
hardWrapDisabled: {
path1: 'M11.917 8.4c0 .99-.788 1.8-1.75 1.8H6.083v2.4L2 9l4.083-3.6v2.4h3.5V4.2h2.334v4.2z',
},
};
return (
<div onclick={this.toggleHardWrap} ref="hardWrapButton" className="github-CommitView-hardwrap hard-wrap-icons">
<div className={cx('icon', 'no-hardwrap', 'icon-hardwrap-disabled', {hidden: notApplicable || hardWrap})}>
<svg width="16" height="16" viewBox="0 0 16 16" xmlns="http://www.w3.org/2000/svg">
<g fill-rule="evenodd">
<path d={svgPaths.hardWrapEnabled.path1} />
<path fill-rule="nonzero" d={svgPaths.hardWrapEnabled.path2} />
</g>
</svg>
</div>
<div className={cx('icon', 'hardwrap', 'icon-hardwrap-enabled', {hidden: notApplicable || !hardWrap})}>
<svg width="16" height="16" viewBox="0 0 16 16" xmlns="http://www.w3.org/2000/svg">
<path d={svgPaths.hardWrapDisabled.path1} fill-rule="evenodd" />
</svg>
</div>
</div>
);
}
writeAfterUpdate() {
if (this.props.deactivateCommitBox && this.tooltipsExist()) {
this.disposeTooltips();
} else if (!this.props.deactivateCommitBox && !this.tooltipsExist()) {
this.registerTooltips();
}
}
registerTooltips() {
this.expandTooltip = this.props.tooltips.add(this.refs.expandButton, {
title: 'Expand commit message editor',
class: 'github-CommitView-expandButton-tooltip',
});
this.hardWrapTooltip = this.props.tooltips.add(this.refs.hardWrapButton, {
title: 'Toggle hard wrap on commit',
class: 'github-CommitView-hardwrap-tooltip',
});
this.subscriptions.add(this.expandTooltip, this.hardWrapTooltip);
}
tooltipsExist() {
return this.expandTooltip || this.hardWrapTooltip;
}
disposeTooltips() {
this.expandTooltip && this.expandTooltip.dispose();
this.hardWrapTooltip && this.hardWrapTooltip.dispose();
this.expandTooltip = null;
this.hardWrapTooltip = null;
}
@autobind
toggleHardWrap() {
const currentSetting = this.props.config.get('github.automaticCommitMessageWrapping');
this.props.config.set('github.automaticCommitMessageWrapping', !currentSetting);
}
@autobind
abortMerge() {
this.props.abortMerge();
}
@autobind
handleAmendBoxClick() {
this.props.setAmending(this.refs.amend.checked);
}
@autobind
async commit() {
if (await this.props.prepareToCommit() && this.isCommitButtonEnabled()) {
try {
await this.props.commit(this.editor.getText());
} catch (e) {
// do nothing
}
} else {
this.setFocus(CommitView.focus.EDITOR);
}
}
getRemainingCharacters() {
if (this.editor != null) {
if (this.editor.getCursorBufferPosition().row === 0) {
return (this.props.maximumCharacterLimit - this.editor.lineTextForBufferRow(0).length).toString();
} else {
return '∞';
}
} else {
return this.props.maximumCharacterLimit || '';
}
}
isCommitButtonEnabled() {
return !this.props.isCommitting &&
this.props.stagedChangesExist &&
!this.props.mergeConflictsExist &&
this.props.lastCommit.isPresent() &&
(this.props.deactivateCommitBox || (this.editor && this.editor.getText().length !== 0));
}
commitButtonText() {
if (this.props.isAmending) {
return `Amend commit (${shortenSha(this.props.lastCommit.getSha())})`;
} else if (this.shouldShowWorking()) {
return 'Working...';
} else {
if (this.props.branchName) {
return `Commit to ${this.props.branchName}`;
} else {
return 'Commit';
}
}
}
@autobind
toggleExpandedCommitMessageEditor() {
return this.props.toggleExpandedCommitMessageEditor(this.editor && this.editor.getText());
}
rememberFocus(event) {
if (this.editorElement.contains(event.target)) {
return CommitView.focus.EDITOR;
}
if (this.refs.abortMergeButton && this.refs.abortMergeButton.contains(event.target)) {
return CommitView.focus.ABORT_MERGE_BUTTON;
}
if (this.refs.amend && this.refs.amend.contains(event.target)) {
return CommitView.focus.AMEND_BOX;
}
if (this.refs.commitButton && this.refs.commitButton.contains(event.target)) {
return CommitView.focus.COMMIT_BUTTON;
}
return null;
}
setFocus(focus) {
let fallback = false;
if (focus === CommitView.focus.EDITOR) {
this.editorElement.focus();
return true;
}
if (focus === CommitView.focus.ABORT_MERGE_BUTTON) {
if (this.refs.abortMergeButton) {
this.refs.abortMergeButton.focus();
return true;
} else {
fallback = true;
}
}
if (focus === CommitView.focus.AMEND_BOX) {
if (this.refs.amend) {
this.refs.amend.focus();
return true;
} else {
fallback = true;
}
}
if (focus === CommitView.focus.COMMIT_BUTTON) {
if (this.refs.commitButton) {
this.refs.commitButton.focus();
return true;
} else {
fallback = true;
}
}
if (fallback) {
this.editorElement.focus();
return true;
}
return false;
}
}
You can’t perform that action at this time.
