Skip to content

Commit 97829b9

Browse files
committed
Probably fix 'hide unchanged cells' for merge notebooks, i hope?
1 parent f153938 commit 97829b9

File tree

2 files changed

+36
-12
lines changed

2 files changed

+36
-12
lines changed

src/components/diff/NotebookDiff.ts

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ import { Panel, Widget } from '@lumino/widgets';
1414
import { IDiffEntry } from 'nbdime/lib/diff/diffentries';
1515
import { IMergeDecision } from 'nbdime/lib/merge/decisions';
1616
import { NotebookMergeModel } from 'nbdime/lib/merge/model';
17-
import { NotebookMergeWidget } from 'nbdime/lib/merge/widget';
17+
import { CELLMERGE_CLASS, NotebookMergeWidget } from 'nbdime/lib/merge/widget';
18+
import { UNCHANGED_MERGE_CLASS } from 'nbdime/lib/merge/widget/common';
1819
import { NotebookDiffModel } from 'nbdime/lib/diff/model';
1920
import { CELLDIFF_CLASS, NotebookDiffWidget } from 'nbdime/lib/diff/widget';
2021
import {
@@ -144,12 +145,20 @@ export class NotebookDiff
144145
if (this._areUnchangedCellsHidden !== v) {
145146
Private.toggleShowUnchanged(
146147
this._scroller,
148+
this._isConflict,
147149
this._areUnchangedCellsHidden
148150
);
149151
this._areUnchangedCellsHidden = v;
150152
}
151153
}
152154

155+
/**
156+
* Helper to determine if a notebook merge should be shown.
157+
*/
158+
private get _isConflict(): boolean {
159+
return !!this._model.base;
160+
}
161+
153162
/**
154163
* Promise which fulfills when the widget is ready.
155164
*/
@@ -213,13 +222,13 @@ export class NotebookDiff
213222
try {
214223
await nbdWidget.init();
215224

216-
Private.markUnchangedRanges(this._scroller.node);
225+
Private.markUnchangedRanges(this._scroller.node, this._isConflict);
217226
} catch (reason) {
218227
// FIXME there is a bug in nbdime and init got reject due to recursion limit hit
219228
// console.error(`Failed to init notebook diff view: ${reason}`);
220229
// getReady.reject(reason);
221230
console.debug(`Failed to init notebook diff view: ${reason}`);
222-
Private.markUnchangedRanges(this._scroller.node);
231+
Private.markUnchangedRanges(this._scroller.node, this._isConflict);
223232
}
224233
} catch (reason) {
225234
this.showError(reason);
@@ -322,7 +331,11 @@ namespace Private {
322331
*
323332
* This simply marks with a class, real work is done by CSS.
324333
*/
325-
export function toggleShowUnchanged(root: Widget, show?: boolean): void {
334+
export function toggleShowUnchanged(
335+
root: Widget,
336+
isConflict: boolean,
337+
show?: boolean
338+
): void {
326339
const hiding = root.hasClass(HIDE_UNCHANGED_CLASS);
327340
if (show === undefined) {
328341
show = hiding;
@@ -333,7 +346,7 @@ namespace Private {
333346
if (show) {
334347
root.removeClass(HIDE_UNCHANGED_CLASS);
335348
} else {
336-
markUnchangedRanges(root.node);
349+
markUnchangedRanges(root.node, isConflict);
337350
root.addClass(HIDE_UNCHANGED_CLASS);
338351
}
339352
root.update();
@@ -360,12 +373,20 @@ namespace Private {
360373
/**
361374
* Marks certain cells with
362375
*/
363-
export function markUnchangedRanges(root: HTMLElement): void {
364-
const children = root.querySelectorAll(`.${CELLDIFF_CLASS}`);
376+
export function markUnchangedRanges(
377+
root: HTMLElement,
378+
isConflict: boolean
379+
): void {
380+
const CELL_CLASS = isConflict ? CELLMERGE_CLASS : CELLDIFF_CLASS;
381+
const UNCHANGED_CLASS = isConflict
382+
? UNCHANGED_MERGE_CLASS
383+
: UNCHANGED_DIFF_CLASS;
384+
385+
const children = root.querySelectorAll(`.${CELL_CLASS}`);
365386
let rangeStart = -1;
366387
for (let i = 0; i < children.length; ++i) {
367388
const child = children[i];
368-
if (!child.classList.contains(UNCHANGED_DIFF_CLASS)) {
389+
if (!child.classList.contains(UNCHANGED_CLASS)) {
369390
// Visible
370391
if (rangeStart !== -1) {
371392
// Previous was hidden
@@ -387,7 +408,10 @@ namespace Private {
387408
if (rangeStart === 0) {
388409
// All elements were hidden, nothing to mark
389410
// Add info on root instead
390-
const tag = root.querySelector('.jp-Notebook-diff') || root;
411+
const tag =
412+
root.querySelector('.jp-Notebook-diff') ??
413+
root.querySelector('.jp-Notebook-merge') ??
414+
root;
391415
tag.setAttribute('data-nbdime-AllCellsHidden', N.toString());
392416
return;
393417
}

src/components/diff/PlainTextDiff.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ export class PlainTextDiff
6464
/**
6565
* Helper to determine if three-way diff should be shown.
6666
*/
67-
private _isConflict(): boolean {
67+
private get _isConflict(): boolean {
6868
return this._base !== null;
6969
}
7070

@@ -99,7 +99,7 @@ export class PlainTextDiff
9999
this.createDiffView(
100100
this._challenger,
101101
this._reference,
102-
this._isConflict() ? this._base : undefined
102+
this._isConflict ? this._base : undefined
103103
);
104104
}
105105
})
@@ -141,7 +141,7 @@ export class PlainTextDiff
141141
this.createDiffView(
142142
this._challenger,
143143
this._reference,
144-
this._isConflict() ? this._base : undefined
144+
this._isConflict ? this._base : undefined
145145
);
146146

147147
this._challenger = null;

0 commit comments

Comments
 (0)