Skip to content

Commit 7053e6d

Browse files
committed
Change cursor to rename snippet on doubleClick; remove description div when it's empty; handle duplicate name on rename
1 parent eeb6a96 commit 7053e6d

File tree

1 file changed

+50
-63
lines changed

1 file changed

+50
-63
lines changed

src/CodeSnippetDisplay.tsx

Lines changed: 50 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ const CODE_SNIPPET_MORE_OTPIONS_INSERT = 'jp-codeSnippet-more-options-insert';
120120
const CODE_SNIPPET_MORE_OTPIONS_EDIT = 'jp-codeSnippet-more-options-edit';
121121
const CODE_SNIPPET_MORE_OTPIONS_DELETE = 'jp-codeSnippet-more-options-delete';
122122
const CODE_SNIPPET_CREATE_NEW_BTN = 'jp-createSnippetBtn';
123+
const CODE_SNIPPET_NAME = 'jp-codeSnippet-name';
123124

124125
/**
125126
* The threshold in pixels to start a drag event.
@@ -374,11 +375,18 @@ export class CodeSnippetDisplay extends React.Component<
374375
return <span>{elements}</span>;
375376
}
376377
}
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+
);
378387
};
379388

380389
// rename snippet on double click
381-
// TODO: duplicate name check!
382390
private async handleRenameSnippet(
383391
event: React.MouseEvent<HTMLSpanElement, MouseEvent>
384392
): Promise<void> {
@@ -399,30 +407,39 @@ export class CodeSnippetDisplay extends React.Component<
399407
new_element.onblur = async (): Promise<void> => {
400408
if (target.innerHTML !== new_element.value) {
401409
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);
411410

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' })],
419420
});
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+
}
420432
}
421433
new_element.replaceWith(target);
422434
};
423435
new_element.onkeydown = (event: KeyboardEvent): void => {
424436
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
426443
event.stopPropagation();
427444
event.preventDefault();
428445
new_element.blur();
@@ -604,9 +621,6 @@ export class CodeSnippetDisplay extends React.Component<
604621
'--preview-max-height',
605622
heightPreview
606623
);
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-
// }
610624
const final = distDown.toString(10) + 'px';
611625
document.documentElement.style.setProperty('--preview-distance', final);
612626
}
@@ -1184,7 +1198,7 @@ export class CodeSnippetDisplay extends React.Component<
11841198
}}
11851199
>
11861200
<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}>
11881202
{this.renderLanguageIcon(language)}
11891203
{this.boldNameOnSearch(id, language, name)}
11901204
</div>
@@ -1212,26 +1226,31 @@ export class CodeSnippetDisplay extends React.Component<
12121226
})}
12131227
</div>
12141228
</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)}
12181230
</div>
12191231
</div>
12201232
);
12211233
};
12221234

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+
12231247
static getDerivedStateFromProps(
12241248
nextProps: ICodeSnippetDisplayProps,
12251249
prevState: ICodeSnippetDisplayState
12261250
): ICodeSnippetDisplayState {
1227-
console.log('getDerivedStateFromProps');
1228-
console.log(prevState);
1229-
console.log(nextProps);
1230-
12311251
// this is why state doesn't change
12321252
if (prevState.searchValue === '' && prevState.filterTags.length === 0) {
12331253
return {
1234-
// codeSnippets: nextProps.codeSnippetManager.snippets,
12351254
codeSnippets: nextProps.codeSnippets,
12361255
matchIndices: [],
12371256
searchValue: '',
@@ -1240,17 +1259,6 @@ export class CodeSnippetDisplay extends React.Component<
12401259
}
12411260

12421261
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-
// });
12541262
return {
12551263
codeSnippets: prevState.codeSnippets,
12561264
matchIndices: prevState.matchIndices,
@@ -1380,28 +1388,7 @@ export class CodeSnippetDisplay extends React.Component<
13801388
console.log('Error in deleting the snippet');
13811389
return;
13821390
}
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));
13921391
});
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-
// }));
14051392
}
14061393
});
14071394
}

0 commit comments

Comments
 (0)