Skip to content

Commit 6018b23

Browse files
authored
Merge branch 'main' into fix23
2 parents 098fdcf + 4a98f34 commit 6018b23

File tree

5 files changed

+102
-12
lines changed

5 files changed

+102
-12
lines changed

CONTRIBUTING.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
## Contributing
1+
# Contributing
22

3-
### Development install
3+
## Development install
44

55
Note: You will need NodeJS to build the extension package.
66

@@ -36,7 +36,7 @@ By default, the `jlpm build` command generates the source maps for this extensio
3636
jupyter lab build --minimize=False
3737
```
3838

39-
### Development uninstall
39+
## Development uninstall
4040

4141
```bash
4242
# Server extension must be manually disabled in develop mode

README.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ The extension provides commands to show diffs in multiple formats:
3838
- `jupyterlab-diff:unified-cell-diff` - Show cell diff using unified view
3939
- `jupyterlab-diff:unified-file-diff` - Show file diff using unified view for regular Python files and other text files
4040

41-
https://github.com/user-attachments/assets/0dacd7f0-5963-4ebe-81da-2958f0117071
41+
<https://github.com/user-attachments/assets/0dacd7f0-5963-4ebe-81da-2958f0117071>
4242

4343
### Programmatic Usage
4444

@@ -82,7 +82,7 @@ The commands can also be run from the browser console (for example during develo
8282

8383
First JupyterLab needs to be started with the `--expose-app-in-browser` flag to expose `window.jupyterapp`:
8484

85-
```
85+
```bash
8686
jupyter lab --expose-app-in-browser
8787
```
8888

@@ -138,6 +138,17 @@ The extension provides two diff viewing strategies:
138138

139139
- **Unified diff** (`unified-cell-diff`/`unified-file-diff`): Uses CodeMirror's `unifiedMergeView`. Displays changes in a single unified view with added/removed lines clearly marked. Can be used for both cell diffs and regular file diffs.
140140

141+
## Contributing
142+
143+
We welcome contributions from the community! To contribute:
144+
145+
- Fork the repository
146+
- Make a development install of jupyterlab-diff
147+
- Create a new branch
148+
- Make your changes
149+
- Submit a pull request
150+
For more details, check out our [CONTRIBUTING.md](https://github.com/jupyter-ai-contrib/jupyterlab-diff?tab=contributing-ov-file#contributing).
151+
141152
## Uninstall
142153

143154
To remove the extension, execute:

src/diff/base-unified-diff.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,20 +90,32 @@ export abstract class BaseUnifiedDiffManager {
9090
*/
9191
protected abstract removeToolbarButtons(): void;
9292

93+
/**
94+
* Hook to hide the cell toolbar — overridden in subclasses
95+
*/
96+
protected hideCellToolbar(): void {}
97+
98+
/**
99+
* Hook to show the cell toolbar — overridden in subclasses
100+
*/
101+
protected showCellToolbar(): void {}
102+
93103
/**
94104
* Activate the diff view
95105
*/
96106
protected activate(): void {
97107
this._applyDiff();
98108
this.addToolbarButtons();
109+
this.hideCellToolbar();
99110
}
100111

101112
/**
102113
* Deactivate the diff view
103114
*/
104-
private _deactivate(): void {
115+
protected _deactivate(): void {
105116
this.removeToolbarButtons();
106117
this._cleanupEditor();
118+
this.showCellToolbar();
107119
}
108120

109121
/**

src/diff/unified-cell.ts

Lines changed: 72 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ICellModel } from '@jupyterlab/cells';
1+
import { Cell } from '@jupyterlab/cells';
22
import { checkIcon, ToolbarButton, undoIcon } from '@jupyterlab/ui-components';
33
import { ICellFooterTracker } from 'jupyterlab-cell-input-footer';
44
import {
@@ -12,9 +12,9 @@ import type { ISharedText } from '@jupyter/ydoc';
1212
*/
1313
export interface IUnifiedCellDiffOptions extends IBaseUnifiedDiffOptions {
1414
/**
15-
* The cell to show the diff for
15+
* The cell widget to show the diff for
1616
*/
17-
cell: ICellModel;
17+
cell: Cell;
1818

1919
/**
2020
* The cell footer tracker
@@ -37,6 +37,8 @@ export class UnifiedCellDiffManager extends BaseUnifiedDiffManager {
3737
this.activate();
3838
}
3939

40+
private static _activeDiffCount = 0;
41+
4042
/**
4143
* Check if this cell still has pending changes
4244
*/
@@ -48,7 +50,66 @@ export class UnifiedCellDiffManager extends BaseUnifiedDiffManager {
4850
* Get the shared model for source manipulation
4951
*/
5052
protected getSharedModel(): ISharedText {
51-
return this._cell.sharedModel;
53+
return this._cell.model.sharedModel;
54+
}
55+
56+
/**
57+
* Activate the diff view without cell toolbar.
58+
*/
59+
protected activate(): void {
60+
super.activate();
61+
UnifiedCellDiffManager._activeDiffCount++;
62+
63+
const observer = new MutationObserver(() => {
64+
this.hideCellToolbar();
65+
});
66+
67+
observer.observe(this._cell.node, {
68+
childList: true,
69+
subtree: true
70+
});
71+
72+
(this as any)._toolbarObserver = observer;
73+
}
74+
75+
/**
76+
* Deactivate the diff view with cell toolbar.
77+
*/
78+
protected _deactivate(): void {
79+
super['_deactivate']();
80+
UnifiedCellDiffManager._activeDiffCount = Math.max(
81+
0,
82+
UnifiedCellDiffManager._activeDiffCount - 1
83+
);
84+
85+
const observer = (this as any)._toolbarObserver as MutationObserver;
86+
if (observer) {
87+
observer.disconnect();
88+
}
89+
}
90+
/**
91+
* Hide the cell's toolbar while the diff is active
92+
*/
93+
protected hideCellToolbar(): void {
94+
const toolbar = this._cell.node.querySelector('jp-toolbar') as HTMLElement;
95+
if (toolbar) {
96+
toolbar.style.display = 'none';
97+
}
98+
}
99+
100+
/**
101+
* Show the cell's toolbar when the diff is deactivated
102+
*/
103+
protected showCellToolbar(): void {
104+
if (UnifiedCellDiffManager._activeDiffCount > 0) {
105+
return;
106+
}
107+
const toolbar = this._cell.node.querySelector(
108+
'jp-toolbar'
109+
) as HTMLElement | null;
110+
if (toolbar) {
111+
toolbar.style.display = '';
112+
}
52113
}
53114

54115
/**
@@ -92,6 +153,9 @@ export class UnifiedCellDiffManager extends BaseUnifiedDiffManager {
92153
}
93154

94155
this._cellFooterTracker.showFooter(cellId);
156+
157+
// Hide the main cell toolbar to avoid overlap
158+
this.hideCellToolbar();
95159
}
96160

97161
/**
@@ -115,9 +179,12 @@ export class UnifiedCellDiffManager extends BaseUnifiedDiffManager {
115179

116180
// Hide the footer if no other items remain
117181
this._cellFooterTracker.hideFooter(cellId);
182+
183+
// Show the main cell toolbar again
184+
this.showCellToolbar();
118185
}
119186

120-
private _cell: ICellModel;
187+
private _cell: Cell;
121188
private _cellFooterTracker?: ICellFooterTracker;
122189
}
123190

src/plugin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ const unifiedCellDiffPlugin: JupyterFrontEndPlugin<void> = {
297297

298298
// Create a new manager
299299
const manager = await createUnifiedCellDiffView({
300-
cell,
300+
cell: cellWidget,
301301
editor: cellWidget.editor as CodeMirrorEditor,
302302
cellFooterTracker,
303303
originalSource,

0 commit comments

Comments
 (0)