Skip to content

Commit 9a4aa7e

Browse files
committed
Remove old diffs when a new diff is created
1 parent 27c30c7 commit 9a4aa7e

File tree

7 files changed

+59
-72
lines changed

7 files changed

+59
-72
lines changed

CONTRIBUTING.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
## Contributing
32

43
### Development install

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
"@jupyterlab/services": "^7.0.0",
6464
"@jupyterlab/ui-components": "^4.0.0",
6565
"@lumino/widgets": "^2.0.0",
66-
"jupyterlab-cell-input-footer": "^0.2.0",
66+
"jupyterlab-cell-input-footer": "^0.3.0",
6767
"nbdime": "^7.0.1"
6868
},
6969
"devDependencies": {

src/command.ts

Lines changed: 41 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,94 +1,91 @@
1-
import {
2-
ICellFooterTracker,
3-
} from 'jupyterlab-cell-input-footer';
1+
import { ICellFooterTracker } from 'jupyterlab-cell-input-footer';
42
import { IDiffEntry } from 'nbdime/lib/diff/diffentries';
53
import { createPatchStringDiffModel } from 'nbdime/lib/diff/model';
64
import { MergeView } from 'nbdime/lib/common/mergeview';
7-
import {
8-
ToolbarButton,
9-
} from '@jupyterlab/ui-components';
10-
import {
11-
requestAPI
12-
} from './handler';
5+
import { ToolbarButton } from '@jupyterlab/ui-components';
6+
import { requestAPI } from './handler';
137

148
export namespace ShowDiff {
15-
169
export interface ICommandArgs {
17-
cell_id?: string,
18-
original_source: string,
19-
diff: IDiffEntry[]
10+
cell_id?: string;
11+
original_source: string;
12+
diff: IDiffEntry[];
2013
}
2114

2215
export interface IFetchDiff {
23-
original_source: string,
24-
new_source: string
16+
original_source: string;
17+
new_source: string;
2518
}
2619
}
2720

28-
2921
/**
3022
* Adds a Diff UX underneath a JupyterLab cell.
31-
*
32-
* @param data
33-
* @param cellFooterTracker
23+
*
24+
* @param data
25+
* @param cellFooterTracker
3426
*/
35-
export function showCellDiff(data: ShowDiff.ICommandArgs, cellFooterTracker: ICellFooterTracker) {
36-
let diff = createPatchStringDiffModel(
37-
data["original_source"],
38-
data["diff"]
39-
)
40-
27+
export function showCellDiff(
28+
data: ShowDiff.ICommandArgs,
29+
cellFooterTracker: ICellFooterTracker
30+
) {
31+
let diff = createPatchStringDiffModel(data['original_source'], data['diff']);
32+
4133
let mergeView: MergeView;
4234
mergeView = new MergeView({ remote: diff });
43-
mergeView.addClass("nbdime-root");
44-
mergeView.addClass("jp-Notebook-diff");
35+
//
36+
mergeView.addClass('jp-cell-diff');
37+
// Add the classes below to pick up the styling from nbdime.
38+
mergeView.addClass('nbdime-root');
39+
mergeView.addClass('jp-Notebook-diff');
4540
mergeView.hide();
4641

4742
let footer = cellFooterTracker.getFooter(data.cell_id);
43+
// Try removing any old widget that exists.
44+
try {
45+
footer?.removeWidget('jp-cell-diff');
46+
} catch {}
47+
4848
footer?.addWidget(mergeView);
4949

5050
if (footer?.isHidden) {
5151
footer.show();
5252
footer.update();
5353
}
54-
footer?.addItemOnLeft(
54+
footer?.addToolbarItemOnLeft(
5555
'compare',
5656
new ToolbarButton({
5757
// icon: wandIcon,
58-
label: "Compare changes",
58+
label: 'Compare changes',
5959
enabled: true,
60-
onClick: () => {
60+
onClick: () => {
6161
if (mergeView.isHidden) {
62-
mergeView.show()
62+
mergeView.show();
6363
return;
6464
}
65-
mergeView.hide()
65+
mergeView.hide();
6666
}
6767
})
6868
);
6969
}
7070

71-
72-
export async function fetchDiff(data: ShowDiff.IFetchDiff): Promise<ShowDiff.ICommandArgs> {
71+
export async function fetchDiff(
72+
data: ShowDiff.IFetchDiff
73+
): Promise<ShowDiff.ICommandArgs> {
7374
return await requestAPI('api/celldiff');
7475
}
7576

76-
7777
/**
7878
* Adds a diff to the Cell Footer
79-
*
79+
*
8080
*/
8181
export function showCellDiffCommand(cellFooterTracker: ICellFooterTracker) {
8282
return (args: any) => {
83-
let data: ShowDiff.ICommandArgs = (args as any);
84-
let cellId = data["cell_id"];
83+
let data: ShowDiff.ICommandArgs = args as any;
84+
let cellId = data['cell_id'];
8585
if (cellId) {
86-
87-
if (data && data["original_source"] && data["diff"]) {
88-
89-
showCellDiff(data, cellFooterTracker)
86+
if (data && data['original_source'] && data['diff']) {
87+
showCellDiff(data, cellFooterTracker);
9088
}
9189
}
92-
}
90+
};
9391
}
94-

src/handler.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,7 @@ export async function requestAPI<T>(
1515
): Promise<T> {
1616
// Make request to Jupyter API
1717
const settings = ServerConnection.makeSettings();
18-
const requestUrl = URLExt.join(
19-
settings.baseUrl,
20-
endPoint
21-
);
18+
const requestUrl = URLExt.join(settings.baseUrl, endPoint);
2219

2320
let response: Response;
2421
try {
@@ -42,4 +39,4 @@ export async function requestAPI<T>(
4239
}
4340

4441
return data;
45-
}
42+
}

src/index.ts

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,9 @@ import {
33
JupyterFrontEndPlugin
44
} from '@jupyterlab/application';
55

6-
import {
7-
showCellDiffCommand
8-
} from './command';
6+
import { showCellDiffCommand } from './command';
97

10-
import {
11-
ICellFooterTracker
12-
} from 'jupyterlab-cell-input-footer';
8+
import { ICellFooterTracker } from 'jupyterlab-cell-input-footer';
139

1410
/**
1511
* A JupyterLab plugin providing a command for displaying a diff below a cell.
@@ -19,18 +15,15 @@ const showDiff: JupyterFrontEndPlugin<void> = {
1915
requires: [ICellFooterTracker],
2016
autoStart: true,
2117
activate: async (
22-
app: JupyterFrontEnd,
23-
cellFooterTracker: ICellFooterTracker,
18+
app: JupyterFrontEnd,
19+
cellFooterTracker: ICellFooterTracker
2420
) => {
25-
console.log("Jupyterlab extension - show cell diff.")
21+
console.log('Jupyterlab extension - show cell diff.');
2622
await app.serviceManager.ready;
2723

28-
app.commands.addCommand(
29-
'show-diff',
30-
{
31-
execute: showCellDiffCommand(cellFooterTracker)
32-
}
33-
)
24+
app.commands.addCommand('show-diff', {
25+
execute: showCellDiffCommand(cellFooterTracker)
26+
});
3427
}
3528
};
3629

style/base.css

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66

77
.jp-cellfooter .cm-merge-2pane {
88
display: grid;
9-
padding: 0px;
9+
padding: 0;
10+
1011
/* editor | gap | editor */
1112
grid-template-columns: 49% 2% 49%;
1213
grid-auto-rows: minmax(18px, auto);

yarn.lock

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3727,7 +3727,7 @@ __metadata:
37273727
eslint: ^8.36.0
37283728
eslint-config-prettier: ^8.8.0
37293729
eslint-plugin-prettier: ^5.0.0
3730-
jupyterlab-cell-input-footer: ^0.2.0
3730+
jupyterlab-cell-input-footer: ^0.3.0
37313731
mkdirp: ^1.0.3
37323732
nbdime: ^7.0.1
37333733
npm-run-all2: ^7.0.1
@@ -3745,13 +3745,13 @@ __metadata:
37453745
languageName: unknown
37463746
linkType: soft
37473747

3748-
"jupyterlab-cell-input-footer@npm:^0.2.0":
3749-
version: 0.2.0
3750-
resolution: "jupyterlab-cell-input-footer@npm:0.2.0"
3748+
"jupyterlab-cell-input-footer@npm:^0.3.0":
3749+
version: 0.3.0
3750+
resolution: "jupyterlab-cell-input-footer@npm:0.3.0"
37513751
dependencies:
37523752
"@jupyterlab/services": ^7.0.0
37533753
"@lumino/coreutils": ^2.1.2
3754-
checksum: b298f6ba525668ce18c25c1e1bd5c1f515d2c76f9611092b88e1ae2769e2966784455c4d7e80f10061a8482d8c938264af15436faf0234bada101fa0a42d62ef
3754+
checksum: 88e8a420022a9228a09271f7b7e5ddc15f2bab9f9cf4e11541364a4060d8907ebdee2174efe0ecbbbdfa56d649fda25380e7a0f9d6dbe4d3486b3c0ab2e55a4c
37553755
languageName: node
37563756
linkType: hard
37573757

0 commit comments

Comments
 (0)