@@ -120,6 +120,7 @@ const CODE_SNIPPET_MORE_OTPIONS_INSERT = 'jp-codeSnippet-more-options-insert';
120
120
const CODE_SNIPPET_MORE_OTPIONS_EDIT = 'jp-codeSnippet-more-options-edit' ;
121
121
const CODE_SNIPPET_MORE_OTPIONS_DELETE = 'jp-codeSnippet-more-options-delete' ;
122
122
const CODE_SNIPPET_CREATE_NEW_BTN = 'jp-createSnippetBtn' ;
123
+ const CODE_SNIPPET_NAME = 'jp-codeSnippet-name' ;
123
124
124
125
/**
125
126
* The threshold in pixels to start a drag event.
@@ -374,11 +375,18 @@ export class CodeSnippetDisplay extends React.Component<
374
375
return < span > { elements } </ span > ;
375
376
}
376
377
}
377
- return < span onDoubleClick = { this . handleRenameSnippet } > { name } </ span > ;
378
+ return (
379
+ < span
380
+ title = { 'Double click to rename' }
381
+ className = { CODE_SNIPPET_NAME }
382
+ onDoubleClick = { this . handleRenameSnippet }
383
+ >
384
+ { name }
385
+ </ span >
386
+ ) ;
378
387
} ;
379
388
380
389
// rename snippet on double click
381
- // TODO: duplicate name check!
382
390
private async handleRenameSnippet (
383
391
event : React . MouseEvent < HTMLSpanElement , MouseEvent >
384
392
) : Promise < void > {
@@ -399,30 +407,39 @@ export class CodeSnippetDisplay extends React.Component<
399
407
new_element . onblur = async ( ) : Promise < void > => {
400
408
if ( target . innerHTML !== new_element . value ) {
401
409
const newName = new_element . value ;
402
- this . props . codeSnippetManager
403
- . renameSnippet ( oldName , newName )
404
- . then ( async ( res : boolean ) => {
405
- if ( res ) {
406
- console . log ( target ) ;
407
- console . log ( new_element ) ;
408
- target . innerHTML = new_element . value ;
409
- } else {
410
- new_element . replaceWith ( target ) ;
411
410
412
- await showDialog ( {
413
- title : 'Duplicate Name of Code Snippet' ,
414
- body : < p > { `"${ newName } " already exists.` } </ p > ,
415
- buttons : [ Dialog . okButton ( { label : 'Dismiss' } ) ] ,
416
- } ) ;
417
- return ;
418
- }
411
+ const isDuplicateName = this . props . codeSnippetManager . duplicateNameExists (
412
+ newName
413
+ ) ;
414
+
415
+ if ( isDuplicateName ) {
416
+ await showDialog ( {
417
+ title : 'Duplicate Name of Code Snippet' ,
418
+ body : < p > { `"${ newName } " already exists.` } </ p > ,
419
+ buttons : [ Dialog . okButton ( { label : 'Dismiss' } ) ] ,
419
420
} ) ;
421
+ } else {
422
+ this . props . codeSnippetManager
423
+ . renameSnippet ( oldName , newName )
424
+ . then ( async ( res : boolean ) => {
425
+ if ( res ) {
426
+ target . innerHTML = new_element . value ;
427
+ } else {
428
+ console . log ( 'Error in renaming snippet!' ) ;
429
+ }
430
+ } ) ;
431
+ }
420
432
}
421
433
new_element . replaceWith ( target ) ;
422
434
} ;
423
435
new_element . onkeydown = ( event : KeyboardEvent ) : void => {
424
436
switch ( event . code ) {
425
- case 'Enter' || 'NumpadEnter' : // Enter
437
+ case 'Enter' : // Enter
438
+ event . stopPropagation ( ) ;
439
+ event . preventDefault ( ) ;
440
+ new_element . blur ( ) ;
441
+ break ;
442
+ case 'NumpadEnter' : // Enter
426
443
event . stopPropagation ( ) ;
427
444
event . preventDefault ( ) ;
428
445
new_element . blur ( ) ;
@@ -604,9 +621,6 @@ export class CodeSnippetDisplay extends React.Component<
604
621
'--preview-max-height' ,
605
622
heightPreview
606
623
) ;
607
- // if (realTarget.getBoundingClientRect().top > window.screen.height / 2) {
608
- // distDown = distDown - 66; //this is bumping it up further if it's close to the end of the screen
609
- // }
610
624
const final = distDown . toString ( 10 ) + 'px' ;
611
625
document . documentElement . style . setProperty ( '--preview-distance' , final ) ;
612
626
}
@@ -1184,7 +1198,7 @@ export class CodeSnippetDisplay extends React.Component<
1184
1198
} }
1185
1199
>
1186
1200
< div key = { displayName } className = { TITLE_CLASS } id = { id . toString ( ) } >
1187
- < div id = { id . toString ( ) } title = { name } className = { DISPLAY_NAME_CLASS } >
1201
+ < div id = { id . toString ( ) } className = { DISPLAY_NAME_CLASS } >
1188
1202
{ this . renderLanguageIcon ( language ) }
1189
1203
{ this . boldNameOnSearch ( id , language , name ) }
1190
1204
</ div >
@@ -1212,26 +1226,31 @@ export class CodeSnippetDisplay extends React.Component<
1212
1226
} ) }
1213
1227
</ div >
1214
1228
</ div >
1215
- < div className = { CODE_SNIPPET_DESC } id = { id . toString ( ) } >
1216
- < p id = { id . toString ( ) } > { `${ codeSnippet . description } ` } </ p >
1217
- </ div >
1229
+ { this . renderDescription ( codeSnippet , id ) }
1218
1230
</ div >
1219
1231
</ div >
1220
1232
) ;
1221
1233
} ;
1222
1234
1235
+ renderDescription ( codeSnippet : ICodeSnippet , id : number ) : JSX . Element {
1236
+ if ( codeSnippet . description . length !== 0 ) {
1237
+ return (
1238
+ < div className = { CODE_SNIPPET_DESC } id = { id . toString ( ) } >
1239
+ < p id = { id . toString ( ) } > { `${ codeSnippet . description } ` } </ p >
1240
+ </ div >
1241
+ ) ;
1242
+ } else {
1243
+ return null ;
1244
+ }
1245
+ }
1246
+
1223
1247
static getDerivedStateFromProps (
1224
1248
nextProps : ICodeSnippetDisplayProps ,
1225
1249
prevState : ICodeSnippetDisplayState
1226
1250
) : ICodeSnippetDisplayState {
1227
- console . log ( 'getDerivedStateFromProps' ) ;
1228
- console . log ( prevState ) ;
1229
- console . log ( nextProps ) ;
1230
-
1231
1251
// this is why state doesn't change
1232
1252
if ( prevState . searchValue === '' && prevState . filterTags . length === 0 ) {
1233
1253
return {
1234
- // codeSnippets: nextProps.codeSnippetManager.snippets,
1235
1254
codeSnippets : nextProps . codeSnippets ,
1236
1255
matchIndices : [ ] ,
1237
1256
searchValue : '' ,
@@ -1240,17 +1259,6 @@ export class CodeSnippetDisplay extends React.Component<
1240
1259
}
1241
1260
1242
1261
if ( prevState . searchValue !== '' || prevState . filterTags . length !== 0 ) {
1243
- // const newSnippets = props.codeSnippets.filter(codeSnippet => {
1244
- // return (
1245
- // state.matchIndices[codeSnippet.id] !== null ||
1246
- // // (state.searchValue !== '' &&
1247
- // // codeSnippet.name.toLowerCase().includes(state.searchValue)) ||
1248
- // // (state.searchValue !== '' &&
1249
- // // codeSnippet.language.toLowerCase().includes(state.searchValue)) ||
1250
- // (codeSnippet.tags &&
1251
- // codeSnippet.tags.some(tag => state.filterTags.includes(tag)))
1252
- // );
1253
- // });
1254
1262
return {
1255
1263
codeSnippets : prevState . codeSnippets ,
1256
1264
matchIndices : prevState . matchIndices ,
@@ -1380,28 +1388,7 @@ export class CodeSnippetDisplay extends React.Component<
1380
1388
console . log ( 'Error in deleting the snippet' ) ;
1381
1389
return ;
1382
1390
}
1383
-
1384
- // active tags after delete
1385
- // const activeTags = this.getActiveTags();
1386
-
1387
- // filterTags: only the tags that are still being used
1388
- // this.setState(state => ({
1389
- // codeSnippets: snippets,
1390
- // filterTags: state.filterTags.filter(tag => activeTags.includes(tag))
1391
- // }), () => console.log(this.state));
1392
1391
} ) ;
1393
- // this.props._codeSnippetWidgetModel.deleteSnippet(codeSnippet.id);
1394
- // this.props._codeSnippetWidgetModel.reorderSnippet();
1395
- // this.props._codeSnippetWidgetModel.updateSnippetContents();
1396
-
1397
- // active tags after delete
1398
- // const activeTags = this.getActiveTags();
1399
-
1400
- // // filterTags: only the tags that are still being used
1401
- // this.setState(state => ({
1402
- // codeSnippets: this.props.codeSnippetManager.snippets,
1403
- // filterTags: state.filterTags.filter(tag => activeTags.includes(tag))
1404
- // }));
1405
1392
}
1406
1393
} ) ;
1407
1394
}
0 commit comments