6
6
/* eslint-disable no-inner-declarations */
7
7
8
8
import { Toolbar } from '@jupyterlab/apputils' ;
9
+ import { Contents } from '@jupyterlab/services' ;
9
10
import { INotebookContent } from '@jupyterlab/nbformat' ;
10
11
import { IRenderMimeRegistry } from '@jupyterlab/rendermime' ;
11
12
import { PromiseDelegate } from '@lumino/coreutils' ;
@@ -96,6 +97,11 @@ export const createNotebookDiff = async (
96
97
'Hide unchanged cells' ;
97
98
toolbar . addItem ( 'hideUnchanged' , new Widget ( { node : label } ) ) ;
98
99
100
+ if ( model . hasConflict ) {
101
+ // FIXME: Merge view breaks when moving checkboxes to the toolbar
102
+ // toolbar.addItem('clear-outputs', diffWidget.nbdWidget.widgets[0])
103
+ }
104
+
99
105
// Connect toolbar checkbox and notebook diff widget
100
106
diffWidget . areUnchangedCellsHidden = checkbox . checked ;
101
107
checkbox . onchange = ( ) => {
@@ -159,20 +165,39 @@ export class NotebookDiff
159
165
return this . _model . hasConflict ;
160
166
}
161
167
168
+ get nbdWidget ( ) : NotebookDiffWidget | NotebookMergeWidget {
169
+ return this . _nbdWidget ;
170
+ }
171
+
162
172
/**
163
173
* Promise which fulfills when the widget is ready.
164
174
*/
165
175
get ready ( ) : Promise < void > {
166
176
return this . _isReady ;
167
177
}
168
178
179
+ /**
180
+ * Checks if the conflicted file has been resolved.
181
+ */
182
+ get isFileResolved ( ) : boolean {
183
+ const widget = this . nbdWidget as NotebookMergeWidget ;
184
+ this . _lastSerializeModel = widget . model . serialize ( ) ;
185
+ const validated = widget . validateMerged ( this . _lastSerializeModel ) ;
186
+ return JSON . stringify ( this . _lastSerializeModel ) === JSON . stringify ( validated )
187
+ }
188
+
169
189
/**
170
190
* Gets the file contents of a resolved merge conflict,
171
191
* and rejects if unable to retrieve.
192
+ *
193
+ * @see https://github.com/jupyter/nbdime/blob/a74b538386d05e3e9c26753ad21faf9ff4d269d7/packages/webapp/src/app/save.ts#L20
172
194
*/
173
- async getResolvedFile ( ) : Promise < string > {
174
- // TODO: Implement
175
- return Promise . reject ( 'TODO' ) ;
195
+ async getResolvedFile ( ) : Promise < Partial < Contents . IModel > > {
196
+ return Promise . resolve ( {
197
+ format : 'json' ,
198
+ type : 'notebook' ,
199
+ content : this . _lastSerializeModel ?? ( this . nbdWidget as NotebookMergeWidget ) . model . serialize ( )
200
+ } ) ;
176
201
}
177
202
178
203
/**
@@ -209,7 +234,7 @@ export class NotebookDiff
209
234
? this . createMergeView . bind ( this )
210
235
: this . createDiffView . bind ( this ) ;
211
236
212
- const nbdWidget = await createView (
237
+ this . _nbdWidget = await createView (
213
238
challengerContent ,
214
239
referenceContent ,
215
240
baseContent
@@ -218,9 +243,9 @@ export class NotebookDiff
218
243
while ( this . _scroller . widgets . length > 0 ) {
219
244
this . _scroller . widgets [ 0 ] . dispose ( ) ;
220
245
}
221
- this . _scroller . addWidget ( nbdWidget ) ;
246
+ this . _scroller . addWidget ( this . _nbdWidget ) ;
222
247
try {
223
- await nbdWidget . init ( ) ;
248
+ await this . _nbdWidget . init ( ) ;
224
249
225
250
Private . markUnchangedRanges ( this . _scroller . node , this . _hasConflict ) ;
226
251
} catch ( reason ) {
@@ -306,6 +331,8 @@ export class NotebookDiff
306
331
protected _model : Git . Diff . IModel < string > ;
307
332
protected _renderMime : IRenderMimeRegistry ;
308
333
protected _scroller : Panel ;
334
+ protected _nbdWidget : NotebookMergeWidget | NotebookDiffWidget ;
335
+ protected _lastSerializeModel : INotebookContent | null = null ;
309
336
}
310
337
311
338
namespace Private {
@@ -389,6 +416,9 @@ namespace Private {
389
416
const UNCHANGED_CLASS = hasConflict
390
417
? UNCHANGED_MERGE_CLASS
391
418
: UNCHANGED_DIFF_CLASS ;
419
+ const NOTEBOOK_CLASS = hasConflict
420
+ ? '.jp-Notebook-merge'
421
+ : '.jp-Notebook-diff' ;
392
422
393
423
const children = root . querySelectorAll ( `.${ CELL_CLASS } ` ) ;
394
424
let rangeStart = - 1 ;
@@ -416,10 +446,7 @@ namespace Private {
416
446
if ( rangeStart === 0 ) {
417
447
// All elements were hidden, nothing to mark
418
448
// Add info on root instead
419
- const tag =
420
- root . querySelector ( '.jp-Notebook-diff' ) ??
421
- root . querySelector ( '.jp-Notebook-merge' ) ??
422
- root ;
449
+ const tag = root . querySelector ( NOTEBOOK_CLASS ) ?? root ;
423
450
tag . setAttribute ( 'data-nbdime-AllCellsHidden' , N . toString ( ) ) ;
424
451
return ;
425
452
}
0 commit comments