-
Notifications
You must be signed in to change notification settings - Fork 9.3k
/
index.tsx
314 lines (271 loc) · 8.76 KB
/
index.tsx
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
import * as React from 'react'
import { assertNever } from '../../lib/fatal-error'
import { encodePathAsUrl } from '../../lib/path'
import { Repository } from '../../models/repository'
import {
CommittedFileChange,
WorkingDirectoryFileChange,
AppFileStatusKind,
isManualConflict,
isConflictedFileStatus,
} from '../../models/status'
import {
DiffSelection,
DiffType,
IDiff,
IImageDiff,
ITextDiff,
ILargeTextDiff,
ImageDiffType,
ISubmoduleDiff,
} from '../../models/diff'
import { Button } from '../lib/button'
import {
NewImageDiff,
ModifiedImageDiff,
DeletedImageDiff,
} from './image-diffs'
import { BinaryFile } from './binary-file'
import { TextDiff } from './text-diff'
import { SideBySideDiff } from './side-by-side-diff'
import { enableExperimentalDiffViewer } from '../../lib/feature-flag'
import { IFileContents } from './syntax-highlighting'
import { SubmoduleDiff } from './submodule-diff'
// image used when no diff is displayed
const NoDiffImage = encodePathAsUrl(__dirname, 'static/ufo-alert.svg')
type ChangedFile = WorkingDirectoryFileChange | CommittedFileChange
/** The props for the Diff component. */
interface IDiffProps {
readonly repository: Repository
/**
* Whether the diff is readonly, e.g., displaying a historical diff, or the
* diff's lines can be selected, e.g., displaying a change in the working
* directory.
*/
readonly readOnly: boolean
/** The file whose diff should be displayed. */
readonly file: ChangedFile
/** Called when the includedness of lines or a range of lines has changed. */
readonly onIncludeChanged?: (diffSelection: DiffSelection) => void
/** The diff that should be rendered */
readonly diff: IDiff
/**
* Contents of the old and new files related to the current text diff.
*/
readonly fileContents: IFileContents | null
/** The type of image diff to display. */
readonly imageDiffType: ImageDiffType
/** Hiding whitespace in diff. */
readonly hideWhitespaceInDiff: boolean
/** Whether we should display side by side diffs. */
readonly showSideBySideDiff: boolean
/** Whether we should show a confirmation dialog when the user discards changes */
readonly askForConfirmationOnDiscardChanges?: boolean
/**
* Called when the user requests to open a binary file in an the
* system-assigned application for said file type.
*/
readonly onOpenBinaryFile: (fullPath: string) => void
/** Called when the user requests to open a submodule. */
readonly onOpenSubmodule?: (fullPath: string) => void
/**
* Called when the user is viewing an image diff and requests
* to change the diff presentation mode.
*/
readonly onChangeImageDiffType: (type: ImageDiffType) => void
/*
* Called when the user wants to discard a selection of the diff.
* Only applicable when readOnly is false.
*/
readonly onDiscardChanges?: (
diff: ITextDiff,
diffSelection: DiffSelection
) => void
/** Called when the user changes the hide whitespace in diffs setting. */
readonly onHideWhitespaceInDiffChanged: (checked: boolean) => void
}
interface IDiffState {
readonly forceShowLargeDiff: boolean
}
/** A component which renders a diff for a file. */
export class Diff extends React.Component<IDiffProps, IDiffState> {
public constructor(props: IDiffProps) {
super(props)
this.state = {
forceShowLargeDiff: false,
}
}
public render() {
const diff = this.props.diff
switch (diff.kind) {
case DiffType.Text:
return this.renderText(diff)
case DiffType.Binary:
return this.renderBinaryFile()
case DiffType.Submodule:
return this.renderSubmoduleDiff(diff)
case DiffType.Image:
return this.renderImage(diff)
case DiffType.LargeText: {
return this.state.forceShowLargeDiff
? this.renderLargeText(diff)
: this.renderLargeTextDiff()
}
case DiffType.Unrenderable:
return this.renderUnrenderableDiff()
default:
return assertNever(diff, `Unsupported diff type: ${diff}`)
}
}
private renderImage(imageDiff: IImageDiff) {
if (imageDiff.current && imageDiff.previous) {
return (
<ModifiedImageDiff
onChangeDiffType={this.props.onChangeImageDiffType}
diffType={this.props.imageDiffType}
current={imageDiff.current}
previous={imageDiff.previous}
/>
)
}
if (
imageDiff.current &&
(this.props.file.status.kind === AppFileStatusKind.New ||
this.props.file.status.kind === AppFileStatusKind.Untracked)
) {
return <NewImageDiff current={imageDiff.current} />
}
if (
imageDiff.previous &&
this.props.file.status.kind === AppFileStatusKind.Deleted
) {
return <DeletedImageDiff previous={imageDiff.previous} />
}
return null
}
private renderLargeTextDiff() {
return (
<div className="panel empty large-diff">
<img src={NoDiffImage} className="blankslate-image" alt="" />
<p>
The diff is too large to be displayed by default.
<br />
You can try to show it anyway, but performance may be negatively
impacted.
</p>
<Button onClick={this.showLargeDiff}>
{__DARWIN__ ? 'Show Diff' : 'Show diff'}
</Button>
</div>
)
}
private renderUnrenderableDiff() {
return (
<div className="panel empty large-diff">
<img src={NoDiffImage} alt="" />
<p>The diff is too large to be displayed.</p>
</div>
)
}
private renderLargeText(diff: ILargeTextDiff) {
// guaranteed to be set since this function won't be called if text or hunks are null
const textDiff: ITextDiff = {
text: diff.text,
hunks: diff.hunks,
kind: DiffType.Text,
lineEndingsChange: diff.lineEndingsChange,
maxLineNumber: diff.maxLineNumber,
hasHiddenBidiChars: diff.hasHiddenBidiChars,
}
return this.renderTextDiff(textDiff)
}
private renderText(diff: ITextDiff) {
if (diff.hunks.length === 0) {
if (
this.props.file.status.kind === AppFileStatusKind.New ||
this.props.file.status.kind === AppFileStatusKind.Untracked
) {
return <div className="panel empty">The file is empty</div>
}
if (this.props.file.status.kind === AppFileStatusKind.Renamed) {
return (
<div className="panel renamed">
The file was renamed but not changed
</div>
)
}
if (
isConflictedFileStatus(this.props.file.status) &&
isManualConflict(this.props.file.status)
) {
return (
<div className="panel empty">
The file is in conflict and must be resolved via the command line.
</div>
)
}
if (this.props.hideWhitespaceInDiff) {
return <div className="panel empty">Only whitespace changes found</div>
}
return <div className="panel empty">No content changes found</div>
}
return this.renderTextDiff(diff)
}
private renderSubmoduleDiff(diff: ISubmoduleDiff) {
return (
<SubmoduleDiff
onOpenSubmodule={this.props.onOpenSubmodule}
diff={diff}
readOnly={this.props.readOnly}
/>
)
}
private renderBinaryFile() {
return (
<BinaryFile
path={this.props.file.path}
repository={this.props.repository}
onOpenBinaryFile={this.props.onOpenBinaryFile}
/>
)
}
private renderTextDiff(diff: ITextDiff) {
if (enableExperimentalDiffViewer() || this.props.showSideBySideDiff) {
return (
<SideBySideDiff
file={this.props.file}
diff={diff}
fileContents={this.props.fileContents}
hideWhitespaceInDiff={this.props.hideWhitespaceInDiff}
showSideBySideDiff={this.props.showSideBySideDiff}
onIncludeChanged={this.props.onIncludeChanged}
onDiscardChanges={this.props.onDiscardChanges}
askForConfirmationOnDiscardChanges={
this.props.askForConfirmationOnDiscardChanges
}
onHideWhitespaceInDiffChanged={
this.props.onHideWhitespaceInDiffChanged
}
/>
)
}
return (
<TextDiff
file={this.props.file}
readOnly={this.props.readOnly}
hideWhitespaceInDiff={this.props.hideWhitespaceInDiff}
onIncludeChanged={this.props.onIncludeChanged}
onDiscardChanges={this.props.onDiscardChanges}
diff={diff}
fileContents={this.props.fileContents}
askForConfirmationOnDiscardChanges={
this.props.askForConfirmationOnDiscardChanges
}
onHideWhitespaceInDiffChanged={this.props.onHideWhitespaceInDiffChanged}
/>
)
}
private showLargeDiff = () => {
this.setState({ forceShowLargeDiff: true })
}
}