Skip to content

Commit 4a98f34

Browse files
authored
Merge pull request jupyter-ai-contrib#26 from nakul-py/fix22
Fixing Buttons Overlapping bug
2 parents d9dcd67 + 1e470d5 commit 4a98f34

File tree

3 files changed

+86
-7
lines changed

3 files changed

+86
-7
lines changed

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
@@ -36,11 +36,72 @@ export class UnifiedCellDiffManager extends BaseUnifiedDiffManager {
3636
this.activate();
3737
}
3838

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

46107
/**
@@ -81,6 +142,9 @@ export class UnifiedCellDiffManager extends BaseUnifiedDiffManager {
81142
}
82143

83144
this._cellFooterTracker.showFooter(cellId);
145+
146+
// Hide the main cell toolbar to avoid overlap
147+
this.hideCellToolbar();
84148
}
85149

86150
/**
@@ -104,9 +168,12 @@ export class UnifiedCellDiffManager extends BaseUnifiedDiffManager {
104168

105169
// Hide the footer if no other items remain
106170
this._cellFooterTracker.hideFooter(cellId);
171+
172+
// Show the main cell toolbar again
173+
this.showCellToolbar();
107174
}
108175

109-
private _cell: ICellModel;
176+
private _cell: Cell;
110177
private _cellFooterTracker?: ICellFooterTracker;
111178
}
112179

src/plugin.ts

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

275275
// Create a new manager
276276
const manager = await createUnifiedCellDiffView({
277-
cell,
277+
cell: cellWidget,
278278
editor: cellWidget.editor as CodeMirrorEditor,
279279
cellFooterTracker,
280280
originalSource,

0 commit comments

Comments
 (0)