3
3
Dialog ,
4
4
InputDialog ,
5
5
MainAreaWidget ,
6
+ ReactWidget ,
6
7
showDialog ,
7
8
showErrorMessage
8
9
} from '@jupyterlab/apputils' ;
@@ -14,8 +15,14 @@ import { ITerminal } from '@jupyterlab/terminal';
14
15
import { CommandRegistry } from '@lumino/commands' ;
15
16
import { Menu } from '@lumino/widgets' ;
16
17
import * as React from 'react' ;
17
- import { openDiffView } from './components/diff/DiffWidget' ;
18
+ import {
19
+ Diff ,
20
+ isDiffSupported ,
21
+ RenderMimeProvider
22
+ } from './components/diff/Diff' ;
23
+ import { getRefValue , IDiffContext } from './components/diff/model' ;
18
24
import { GitExtension } from './model' ;
25
+ import { diffIcon } from './style/icons' ;
19
26
import { Git } from './tokens' ;
20
27
import { GitCredentialsForm } from './widgets/CredentialsBox' ;
21
28
import { doGitClone } from './widgets/gitClone' ;
@@ -48,8 +55,7 @@ export namespace CommandIDs {
48
55
export const gitPush = 'git:push' ;
49
56
export const gitPull = 'git:pull' ;
50
57
// Context menu commands
51
- export const gitFileDiffIndex = 'git:context-diffIndex' ;
52
- export const gitFileDiffWorking = 'git:context-diffWorking' ;
58
+ export const gitFileDiff = 'git:context-diff' ;
53
59
export const gitFileDiscard = 'git:context-discard' ;
54
60
export const gitFileOpen = 'git:context-open' ;
55
61
export const gitFileUnstage = 'git:context-unstage' ;
@@ -278,39 +284,66 @@ export function addCommands(
278
284
}
279
285
} ) ;
280
286
281
- commands . addCommand ( CommandIDs . gitFileDiffWorking , {
287
+ commands . addCommand ( CommandIDs . gitFileDiff , {
282
288
label : 'Diff' ,
283
289
caption : 'Diff selected file' ,
284
- execute : async args => {
285
- const selectedFile : Git . IStatusFileResult = args as any ;
286
- await openDiffView (
287
- selectedFile . to ,
288
- model ,
289
- {
290
- currentRef : { specialRef : 'WORKING' } ,
290
+ execute : args => {
291
+ const { context, filePath, isText, status } = ( args as any ) as {
292
+ context ?: IDiffContext ;
293
+ filePath : string ;
294
+ isText : boolean ;
295
+ status ?: Git . Status ;
296
+ } ;
297
+
298
+ let diffContext = context ;
299
+ if ( ! diffContext ) {
300
+ const specialRef = status === 'staged' ? 'INDEX' : 'WORKING' ;
301
+ diffContext = {
302
+ currentRef : { specialRef } ,
291
303
previousRef : { gitRef : 'HEAD' }
292
- } ,
293
- renderMime ,
294
- ! selectedFile . is_binary
295
- ) ;
296
- }
297
- } ) ;
304
+ } ;
305
+ }
298
306
299
- commands . addCommand ( CommandIDs . gitFileDiffIndex , {
300
- label : 'Diff' ,
301
- caption : 'Diff selected file' ,
302
- execute : async args => {
303
- const selectedFile : Git . IStatusFileResult = args as any ;
304
- await openDiffView (
305
- selectedFile . to ,
306
- model ,
307
- {
308
- currentRef : { specialRef : 'INDEX' } ,
309
- previousRef : { gitRef : 'HEAD' }
310
- } ,
311
- renderMime ,
312
- ! selectedFile . is_binary
313
- ) ;
307
+ if ( isDiffSupported ( filePath ) || isText ) {
308
+ const id = `nbdiff-${ filePath } -${ getRefValue ( diffContext . currentRef ) } ` ;
309
+ const mainAreaItems = shell . widgets ( 'main' ) ;
310
+ let mainAreaItem = mainAreaItems . next ( ) ;
311
+ while ( mainAreaItem ) {
312
+ if ( mainAreaItem . id === id ) {
313
+ shell . activateById ( id ) ;
314
+ break ;
315
+ }
316
+ mainAreaItem = mainAreaItems . next ( ) ;
317
+ }
318
+
319
+ if ( ! mainAreaItem ) {
320
+ const serverRepoPath = model . getRelativeFilePath ( ) ;
321
+ const nbDiffWidget = ReactWidget . create (
322
+ < RenderMimeProvider value = { renderMime } >
323
+ < Diff
324
+ path = { filePath }
325
+ diffContext = { diffContext }
326
+ topRepoPath = { serverRepoPath }
327
+ />
328
+ </ RenderMimeProvider >
329
+ ) ;
330
+ nbDiffWidget . id = id ;
331
+ nbDiffWidget . title . label = PathExt . basename ( filePath ) ;
332
+ nbDiffWidget . title . icon = diffIcon ;
333
+ nbDiffWidget . title . closable = true ;
334
+ nbDiffWidget . addClass ( 'jp-git-diff-parent-diff-widget' ) ;
335
+
336
+ shell . add ( nbDiffWidget , 'main' ) ;
337
+ shell . activateById ( nbDiffWidget . id ) ;
338
+ }
339
+ } else {
340
+ showErrorMessage (
341
+ 'Diff Not Supported' ,
342
+ `Diff is not supported for ${ PathExt . extname (
343
+ filePath
344
+ ) . toLocaleLowerCase ( ) } files.`
345
+ ) ;
346
+ }
314
347
}
315
348
} ) ;
316
349
0 commit comments