@@ -234,11 +234,38 @@ export class CodeSnippetEditor extends ReactWidget {
234
234
{ `"${ this . _codeSnippetEditorMetaData . name } " has unsaved changes, close without saving?` } { ' ' }
235
235
</ p >
236
236
) ,
237
- buttons : [ Dialog . cancelButton ( ) , Dialog . okButton ( ) ]
237
+ buttons : [
238
+ Dialog . cancelButton ( ) ,
239
+ Dialog . warnButton ( { label : 'Discard' } ) ,
240
+ Dialog . okButton ( { label : 'Save' } )
241
+ ]
238
242
} ) . then ( ( response : any ) : void => {
243
+ console . log ( response . button ) ;
239
244
if ( response . button . accept ) {
240
- this . dispose ( ) ;
241
- super . onCloseRequest ( msg ) ;
245
+ if ( response . button . label === 'Discard' ) {
246
+ this . dispose ( ) ;
247
+ super . onCloseRequest ( msg ) ;
248
+ } else if ( response . button . label === 'Save' ) {
249
+ const name = ( document . querySelector (
250
+ `.${ CODE_SNIPPET_EDITOR } -${ this . _codeSnippetEditorMetaData . id } .${ CODE_SNIPPET_EDITOR_NAME_INPUT } `
251
+ ) as HTMLInputElement ) . value ;
252
+ const description = ( document . querySelector (
253
+ `.${ CODE_SNIPPET_EDITOR } -${ this . _codeSnippetEditorMetaData . id } .${ CODE_SNIPPET_EDITOR_DESC_INPUT } `
254
+ ) as HTMLInputElement ) . value ;
255
+ const language = ( document . querySelector (
256
+ `.${ CODE_SNIPPET_EDITOR } -${ this . _codeSnippetEditorMetaData . id } .${ CODE_SNIPPET_EDITOR_LANG_INPUT } `
257
+ ) as HTMLSelectElement ) . value ;
258
+
259
+ const validity = this . validateInputs ( name , description , language ) ;
260
+ if ( validity ) {
261
+ this . updateSnippet ( ) . then ( value => {
262
+ if ( value ) {
263
+ this . dispose ( ) ;
264
+ super . onCloseRequest ( msg ) ;
265
+ }
266
+ } ) ;
267
+ }
268
+ }
242
269
}
243
270
} ) ;
244
271
} else {
@@ -348,7 +375,7 @@ export class CodeSnippetEditor extends ReactWidget {
348
375
return status ;
349
376
}
350
377
351
- async updateSnippet ( ) : Promise < void > {
378
+ async updateSnippet ( ) : Promise < boolean > {
352
379
const name = ( document . querySelector (
353
380
`.${ CODE_SNIPPET_EDITOR } -${ this . _codeSnippetEditorMetaData . id } .${ CODE_SNIPPET_EDITOR_NAME_INPUT } `
354
381
) as HTMLInputElement ) . value ;
@@ -363,8 +390,6 @@ export class CodeSnippetEditor extends ReactWidget {
363
390
this . _codeSnippetEditorMetaData . description = description ;
364
391
this . _codeSnippetEditorMetaData . language = language ;
365
392
366
- this . saved = true ;
367
-
368
393
const newPath =
369
394
'snippets/' + this . _codeSnippetEditorMetaData . name + '.json' ;
370
395
@@ -379,9 +404,9 @@ export class CodeSnippetEditor extends ReactWidget {
379
404
await showDialog ( {
380
405
title : 'Duplicate Name of Code Snippet' ,
381
406
body : < p > { `"${ newPath } " already exists.` } </ p > ,
382
- buttons : [ Dialog . cancelButton ( ) ]
407
+ buttons : [ Dialog . okButton ( { label : 'Dismiss' } ) ]
383
408
} ) ;
384
- return ;
409
+ return false ;
385
410
}
386
411
387
412
// set new name as an old name
@@ -396,18 +421,20 @@ export class CodeSnippetEditor extends ReactWidget {
396
421
await showDialog ( {
397
422
title : 'Duplicate Name of Code Snippet' ,
398
423
body : < p > { `"${ newPath } " already exists.` } </ p > ,
399
- buttons : [ Dialog . cancelButton ( ) ]
424
+ buttons : [ Dialog . okButton ( { label : 'Dismiss' } ) ]
400
425
} ) ;
401
426
}
402
427
} )
403
428
. catch ( ( ) => {
404
429
nameCheck = true ;
405
430
} ) ;
406
431
if ( ! nameCheck ) {
407
- return ;
432
+ return false ;
408
433
}
409
434
}
410
435
436
+ this . saved = true ;
437
+
411
438
await this . contentsService . save ( newPath , {
412
439
type : 'file' ,
413
440
format : 'text' ,
@@ -446,6 +473,7 @@ export class CodeSnippetEditor extends ReactWidget {
446
473
if ( this . _codeSnippetEditorMetaData . fromScratch ) {
447
474
this . dispose ( ) ;
448
475
}
476
+ return true ;
449
477
}
450
478
451
479
handleChangeOnTag ( selectedTags : string [ ] , allTags : string [ ] ) : void {
0 commit comments