Skip to content

Commit 81b7e45

Browse files
author
Jaipreet Singh
committed
Use a ReactContext to share the renderMime instance
Additionally, * Remove the conditional logic while adding the commands in the File list.
1 parent 4a36555 commit 81b7e45

File tree

7 files changed

+170
-195
lines changed

7 files changed

+170
-195
lines changed

src/components/FileList.tsx

Lines changed: 94 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -111,127 +111,112 @@ export class FileList extends React.Component<IFileListProps, IFileListState> {
111111
disableFiles: false
112112
};
113113

114-
/** Add right-click menu options for files in repo
115-
*
116-
*/
117-
118-
if (!commands.hasCommand(CommandIDs.gitFileOpen)) {
119-
commands.addCommand(CommandIDs.gitFileOpen, {
120-
label: 'Open',
121-
caption: 'Open selected file',
122-
execute: () => {
123-
try {
124-
this.openListedFile(
125-
this.state.contextMenuTypeX,
126-
this.state.contextMenuTypeY,
127-
this.state.contextMenuFile,
128-
this.props.app
129-
);
130-
} catch (err) {}
131-
}
132-
});
133-
}
114+
commands.addCommand(CommandIDs.gitFileOpen, {
115+
label: 'Open',
116+
caption: 'Open selected file',
117+
execute: async () => {
118+
try {
119+
await this.openListedFile(
120+
this.state.contextMenuTypeX,
121+
this.state.contextMenuTypeY,
122+
this.state.contextMenuFile,
123+
this.props.app
124+
);
125+
} catch (err) {}
126+
}
127+
});
128+
129+
commands.addCommand(CommandIDs.gitFileDiffWorking, {
130+
label: 'Diff',
131+
caption: 'Diff selected file',
132+
execute: () => {
133+
openDiffView(
134+
this.state.contextMenuFile,
135+
this.props.app,
136+
{
137+
currentRef: { specialRef: 'WORKING' },
138+
previousRef: { gitRef: 'HEAD' }
139+
},
140+
this.props.renderMime
141+
);
142+
}
143+
});
134144

135-
if (!commands.hasCommand(CommandIDs.gitFileDiffWorking)) {
136-
commands.addCommand(CommandIDs.gitFileDiffWorking, {
137-
label: 'Diff',
138-
caption: 'Diff selected file',
139-
execute: () => {
140-
openDiffView(
145+
commands.addCommand(CommandIDs.gitFileDiffIndex, {
146+
label: 'Diff',
147+
caption: 'Diff selected file',
148+
execute: () => {
149+
openDiffView(
150+
this.state.contextMenuFile,
151+
this.props.app,
152+
{
153+
currentRef: { specialRef: 'INDEX' },
154+
previousRef: { gitRef: 'HEAD' }
155+
},
156+
this.props.renderMime
157+
);
158+
}
159+
});
160+
161+
commands.addCommand(CommandIDs.gitFileStage, {
162+
label: 'Stage',
163+
caption: 'Stage the changes of selected file',
164+
execute: () => {
165+
try {
166+
this.addUnstagedFile(
141167
this.state.contextMenuFile,
142-
this.props.app,
143-
{
144-
currentRef: { specialRef: 'WORKING' },
145-
previousRef: { gitRef: 'HEAD' }
146-
},
147-
this.props.renderMime
168+
this.props.topRepoPath,
169+
this.props.refresh
148170
);
149-
}
150-
});
151-
}
171+
} catch (err) {}
172+
}
173+
});
152174

153-
if (!commands.hasCommand(CommandIDs.gitFileDiffIndex)) {
154-
commands.addCommand(CommandIDs.gitFileDiffIndex, {
155-
label: 'Diff',
156-
caption: 'Diff selected file',
157-
execute: () => {
158-
openDiffView(
175+
commands.addCommand(CommandIDs.gitFileTrack, {
176+
label: 'Track',
177+
caption: 'Start tracking selected file',
178+
execute: () => {
179+
try {
180+
this.addUntrackedFile(
159181
this.state.contextMenuFile,
160-
this.props.app,
161-
{
162-
currentRef: { specialRef: 'INDEX' },
163-
previousRef: { gitRef: 'HEAD' }
164-
},
165-
this.props.renderMime
182+
this.props.topRepoPath,
183+
this.props.refresh
166184
);
167-
}
168-
});
169-
}
185+
} catch (err) {}
186+
}
187+
});
170188

171-
if (!commands.hasCommand(CommandIDs.gitFileStage)) {
172-
commands.addCommand(CommandIDs.gitFileStage, {
173-
label: 'Stage',
174-
caption: 'Stage the changes of selected file',
175-
execute: () => {
176-
try {
177-
this.addUnstagedFile(
189+
commands.addCommand(CommandIDs.gitFileUnstage, {
190+
label: 'Unstage',
191+
caption: 'Unstage the changes of selected file',
192+
execute: () => {
193+
try {
194+
if (this.state.contextMenuTypeX !== 'D') {
195+
this.resetStagedFile(
178196
this.state.contextMenuFile,
179197
this.props.topRepoPath,
180198
this.props.refresh
181199
);
182-
} catch (err) {}
183-
}
184-
});
185-
}
200+
}
201+
} catch (err) {}
202+
}
203+
});
204+
205+
commands.addCommand(CommandIDs.gitFileDiscard, {
206+
label: 'Discard',
207+
caption: 'Discard recent changes of selected file',
208+
execute: () => {
209+
try {
210+
this.updateSelectedFile(
211+
this.state.contextMenuIndex,
212+
this.state.contextMenuStage
213+
);
214+
this.updateSelectedDiscardFile(this.state.contextMenuIndex);
215+
this.toggleDisableFiles();
216+
} catch (err) {}
217+
}
218+
});
186219

187-
if (!commands.hasCommand(CommandIDs.gitFileTrack)) {
188-
commands.addCommand(CommandIDs.gitFileTrack, {
189-
label: 'Track',
190-
caption: 'Start tracking selected file',
191-
execute: () => {
192-
try {
193-
this.addUntrackedFile(
194-
this.state.contextMenuFile,
195-
this.props.topRepoPath,
196-
this.props.refresh
197-
);
198-
} catch (err) {}
199-
}
200-
});
201-
}
202-
if (!commands.hasCommand(CommandIDs.gitFileUnstage)) {
203-
commands.addCommand(CommandIDs.gitFileUnstage, {
204-
label: 'Unstage',
205-
caption: 'Unstage the changes of selected file',
206-
execute: () => {
207-
try {
208-
if (this.state.contextMenuTypeX !== 'D') {
209-
this.resetStagedFile(
210-
this.state.contextMenuFile,
211-
this.props.topRepoPath,
212-
this.props.refresh
213-
);
214-
}
215-
} catch (err) {}
216-
}
217-
});
218-
}
219-
if (!commands.hasCommand(CommandIDs.gitFileDiscard)) {
220-
commands.addCommand(CommandIDs.gitFileDiscard, {
221-
label: 'Discard',
222-
caption: 'Discard recent changes of selected file',
223-
execute: () => {
224-
try {
225-
this.updateSelectedFile(
226-
this.state.contextMenuIndex,
227-
this.state.contextMenuStage
228-
);
229-
this.updateSelectedDiscardFile(this.state.contextMenuIndex);
230-
this.toggleDisableFiles();
231-
} catch (err) {}
232-
}
233-
});
234-
}
235220
this.state.contextMenuStaged.addItem({ command: CommandIDs.gitFileOpen });
236221
this.state.contextMenuStaged.addItem({
237222
command: CommandIDs.gitFileUnstage

src/components/diff/Diff.tsx

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ export function isDiffSupported(path: string): boolean {
2424
}
2525

2626
export interface IDiffProps {
27-
renderMime: IRenderMimeRegistry;
2827
path: string;
2928
diffContext: IDiffContext;
3029
}
@@ -33,22 +32,20 @@ export interface IDiffProps {
3332
* The parent diff component which maintains a registry of various diff providers.
3433
* Based on the extension of the file, it delegates to the relevant diff provider.
3534
*/
36-
export class Diff extends React.Component<IDiffProps, {}> {
37-
constructor(props) {
38-
super(props);
39-
}
40-
41-
render() {
42-
const fileExtension = PathExt.extname(this.props.path).toLocaleLowerCase();
35+
export function Diff(props: IDiffProps) {
36+
const fileExtension = PathExt.extname(props.path).toLocaleLowerCase();
4337

44-
if (fileExtension in DIFF_PROVIDER_REGISTRY) {
45-
const DiffProvider = DIFF_PROVIDER_REGISTRY[fileExtension];
46-
return <DiffProvider {...this.props} />;
47-
} else {
48-
// This will be removed and delegated to a "Plaintext" diff provider for
49-
// cases where the file extension does not have a dedicated diff provider.
50-
console.log(`Diff is not supported for ${fileExtension} files`);
51-
return null;
52-
}
38+
if (fileExtension in DIFF_PROVIDER_REGISTRY) {
39+
const DiffProvider = DIFF_PROVIDER_REGISTRY[fileExtension];
40+
return <DiffProvider {...props} />;
41+
} else {
42+
// This will be removed and delegated to a "Plaintext" diff provider for
43+
// cases where the file extension does not have a dedicated diff provider.
44+
console.log(`Diff is not supported for ${fileExtension} files`);
45+
return null;
5346
}
5447
}
48+
49+
const renderMimeContext = React.createContext<IRenderMimeRegistry | null>(null);
50+
export const RenderMimeProvider = renderMimeContext.Provider;
51+
export const RenderMimeConsumer = renderMimeContext.Consumer;

src/components/diff/DiffWidget.tsx

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import * as ReactDOM from 'react-dom';
44
import { IRenderMimeRegistry } from '@jupyterlab/rendermime';
55

66
import { getRefValue, IDiffContext } from './model';
7-
import { Diff, isDiffSupported } from './Diff';
7+
import { Diff, isDiffSupported, RenderMimeProvider } from './Diff';
88
import { JupyterLab } from '@jupyterlab/application';
99
import { showDialog } from '@jupyterlab/apputils';
1010
import { PathExt } from '@jupyterlab/coreutils';
@@ -13,7 +13,7 @@ import { style } from 'typestyle';
1313
export class DiffWidget extends Widget {
1414
private readonly _renderMime: IRenderMimeRegistry;
1515
private readonly _path: string;
16-
private readonly _gitContext: IDiffContext;
16+
private readonly _diffContext: IDiffContext;
1717

1818
constructor(
1919
renderMime: IRenderMimeRegistry,
@@ -23,7 +23,7 @@ export class DiffWidget extends Widget {
2323
super();
2424
this._renderMime = renderMime;
2525
this._path = path;
26-
this._gitContext = gitContext;
26+
this._diffContext = gitContext;
2727

2828
this.title.label = PathExt.basename(path);
2929
this.title.iconClass = style({
@@ -33,23 +33,19 @@ export class DiffWidget extends Widget {
3333
this.addClass('jp-git-diff-parent-diff-widget');
3434

3535
ReactDOM.render(
36-
<Diff
37-
renderMime={this._renderMime}
38-
path={this._path}
39-
diffContext={this._gitContext}
40-
/>,
36+
<RenderMimeProvider value={this._renderMime}>
37+
<Diff path={this._path} diffContext={this._diffContext} />
38+
</RenderMimeProvider>,
4139
this.node
4240
);
4341
}
4442

4543
onUpdateRequest(): void {
4644
ReactDOM.unmountComponentAtNode(this.node);
4745
ReactDOM.render(
48-
<Diff
49-
renderMime={this._renderMime}
50-
path={this._path}
51-
diffContext={this._gitContext}
52-
/>,
46+
<RenderMimeProvider value={this._renderMime}>
47+
<Diff path={this._path} diffContext={this._diffContext} />
48+
</RenderMimeProvider>,
5349
this.node
5450
);
5551
}

src/components/diff/NBDiffHeader.tsx

Lines changed: 29 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -11,46 +11,38 @@ export interface INBDiffHeaderProps {
1111
* being rendered. Shows the path to the file and the previous and current ref
1212
* being used for the diff.
1313
*/
14-
export class NBDiffHeader extends React.Component<INBDiffHeaderProps, {}> {
15-
constructor(props: INBDiffHeaderProps) {
16-
super(props);
17-
}
18-
19-
render() {
20-
return (
21-
<div>
22-
<div className="jp-git-diff-header-path">{this.props.path}</div>
23-
<div className="jp-Diff-addremchunk jp-git-diff-header">
24-
<div className="jp-Diff-addedchunk">
25-
Current:{' '}
26-
{this.getRefDisplayValue(this.props.diffContext.currentRef)}
27-
</div>
28-
<div className="jp-Diff-removedchunk">
29-
Previous:{' '}
30-
{this.getRefDisplayValue(this.props.diffContext.previousRef)}
31-
</div>
14+
export function NBDiffHeader(props: INBDiffHeaderProps) {
15+
return (
16+
<div>
17+
<div className="jp-git-diff-header-path">{props.path}</div>
18+
<div className="jp-Diff-addremchunk jp-git-diff-header">
19+
<div className="jp-Diff-addedchunk">
20+
Current: {getRefDisplayValue(props.diffContext.currentRef)}
21+
</div>
22+
<div className="jp-Diff-removedchunk">
23+
Previous: {getRefDisplayValue(props.diffContext.previousRef)}
3224
</div>
3325
</div>
34-
);
35-
}
36-
37-
/**
38-
* Utility method to get a user-friendly display text for a given ref.
39-
*/
40-
private getRefDisplayValue = (ref: ISpecialRef | IGitRef): string => {
41-
const SPECIAL_REFS = {
42-
WORKING: {
43-
displayName: 'Changed'
44-
},
45-
INDEX: {
46-
displayName: 'Staged'
47-
}
48-
};
26+
</div>
27+
);
28+
}
4929

50-
if ('specialRef' in ref) {
51-
return SPECIAL_REFS[ref.specialRef].displayName;
52-
} else {
53-
return ref.gitRef;
30+
/**
31+
* Utility method to get a user-friendly display text for a given ref.
32+
*/
33+
function getRefDisplayValue(ref: ISpecialRef | IGitRef): string {
34+
const SPECIAL_REFS = {
35+
WORKING: {
36+
displayName: 'Changed'
37+
},
38+
INDEX: {
39+
displayName: 'Staged'
5440
}
5541
};
42+
43+
if ('specialRef' in ref) {
44+
return SPECIAL_REFS[ref.specialRef].displayName;
45+
} else {
46+
return ref.gitRef;
47+
}
5648
}

0 commit comments

Comments
 (0)