This repository was archived by the owner on Dec 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 402
/
Copy pathchanged-file-container.js
115 lines (96 loc) · 3.53 KB
/
changed-file-container.js
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
import React from 'react';
import PropTypes from 'prop-types';
import yubikiri from 'yubikiri';
import {CompositeDisposable, Emitter} from 'event-kit';
import {autobind} from '../helpers';
import ObserveModel from '../views/observe-model';
import LoadingView from '../views/loading-view';
import ChangedFileController from '../controllers/changed-file-controller';
import PatchBuffer from '../models/patch/patch-buffer';
export default class ChangedFileContainer extends React.Component {
static propTypes = {
repository: PropTypes.object.isRequired,
stagingStatus: PropTypes.oneOf(['staged', 'unstaged']),
relPath: PropTypes.string.isRequired,
largeDiffThreshold: PropTypes.number,
workspace: PropTypes.object.isRequired,
commands: PropTypes.object.isRequired,
keymaps: PropTypes.object.isRequired,
tooltips: PropTypes.object.isRequired,
config: PropTypes.object.isRequired,
destroy: PropTypes.func.isRequired,
undoLastDiscard: PropTypes.func.isRequired,
surfaceFileAtPath: PropTypes.func.isRequired,
}
constructor(props) {
super(props);
autobind(this, 'fetchData', 'renderWithData');
this.emitter = new Emitter();
this.patchBuffer = new PatchBuffer();
this.lastMultiFilePatch = null;
this.sub = new CompositeDisposable();
this.state = {renderStatusOverride: null};
}
fetchData(repository) {
const staged = this.props.stagingStatus === 'staged';
const builderOpts = {};
if (this.state.renderStatusOverride !== null) {
builderOpts.renderStatusOverrides = {[this.props.relPath]: this.state.renderStatusOverride};
}
if (this.props.largeDiffThreshold !== undefined) {
builderOpts.largeDiffThreshold = this.props.largeDiffThreshold;
}
const before = () => this.emitter.emit('will-update-patch');
const after = patch => this.emitter.emit('did-update-patch', patch);
return yubikiri({
multiFilePatch: repository.getFilePatchForPath(this.props.relPath, {
staged,
patchBuffer: this.patchBuffer,
builder: builderOpts,
before,
after,
}),
isPartiallyStaged: repository.isPartiallyStaged(this.props.relPath),
hasUndoHistory: repository.hasDiscardHistory(this.props.relPath),
});
}
render() {
return (
<ObserveModel model={this.props.repository} fetchData={this.fetchData}>
{this.renderWithData}
</ObserveModel>
);
}
renderWithData(data) {
const currentMultiFilePatch = data && data.multiFilePatch;
if (currentMultiFilePatch !== this.lastMultiFilePatch) {
this.sub.dispose();
/* istanbul ignore else */
if (currentMultiFilePatch) {
// Keep this component's renderStatusOverride synchronized with the FilePatch we're rendering
this.sub = new CompositeDisposable(
...currentMultiFilePatch.getFilePatches().map(fp => fp.onDidChangeRenderStatus(() => {
this.setState({renderStatusOverride: fp.getRenderStatus()});
})),
);
}
this.lastMultiFilePatch = currentMultiFilePatch;
}
if (this.props.repository.isLoading() || data === null) {
return <LoadingView />;
}
return (
<ChangedFileController
onWillUpdatePatch={this.onWillUpdatePatch}
onDidUpdatePatch={this.onDidUpdatePatch}
{...data}
{...this.props}
/>
);
}
componentWillUnmount() {
this.sub.dispose();
}
onWillUpdatePatch = cb => this.emitter.on('will-update-patch', cb);
onDidUpdatePatch = cb => this.emitter.on('did-update-patch', cb);
}