Skip to content

Commit 7b30354

Browse files
authored
Merge pull request #190 from kpinnipa/fixMenu
Fixed bug in inputDialog with language duplicate tags
2 parents c940ea9 + b6e5c9f commit 7b30354

File tree

1 file changed

+27
-9
lines changed

1 file changed

+27
-9
lines changed

src/CodeSnippetInputDialog.ts

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ export function CodeSnippetInputDialog(
5757
idx: number
5858
): Promise<Contents.IModel | null> {
5959
const tags: string[] = [];
60+
const langTags: string[] = [];
6061
const codeSnippetManager = CodeSnippetService.getCodeSnippetService();
6162

6263
const snippets = codeSnippetManager.snippets;
@@ -70,9 +71,12 @@ export function CodeSnippetInputDialog(
7071
}
7172
}
7273
}
74+
if (!langTags.includes(snippet.language)) {
75+
langTags.push(snippet.language);
76+
}
7377
}
7478

75-
const body: InputHandler = new InputHandler(tags, language);
79+
const body: InputHandler = new InputHandler(tags, language, langTags);
7680

7781
return showInputDialog(
7882
codeSnippetWidget,
@@ -262,8 +266,8 @@ class InputHandler extends Widget {
262266
* Construct a new "code snippet" dialog.
263267
* readonly inputNode: HTMLInputElement; <--- in Widget class
264268
*/
265-
constructor(tags: string[], language: string) {
266-
super({ node: Private.createInputNode(tags, language) });
269+
constructor(snippetTags: string[], language: string, langTags: string[]) {
270+
super({ node: Private.createInputNode(snippetTags, language, langTags) });
267271
this.addClass(FILE_DIALOG_CLASS);
268272
}
269273

@@ -295,7 +299,8 @@ class MessageHandler extends Widget {
295299
*/
296300
class Private {
297301
static selectedTags: string[] = [];
298-
static allTags: string[];
302+
static allSnippetTags: string[];
303+
static allLangTags: string[];
299304

300305
static handleOnBlur(event: Event): void {
301306
const target = event.target as HTMLElement;
@@ -307,8 +312,13 @@ class Private {
307312
/**
308313
* Create the node for a code snippet form handler. This is what's creating all of the elements to be displayed.
309314
*/
310-
static createInputNode(tags: string[], language: string): HTMLElement {
311-
Private.allTags = tags;
315+
static createInputNode(
316+
snippetTags: string[],
317+
language: string,
318+
langTags: string[]
319+
): HTMLElement {
320+
Private.allSnippetTags = snippetTags;
321+
Private.allLangTags = langTags;
312322
const body = document.createElement('form');
313323

314324
const nameTitle = document.createElement('label');
@@ -347,7 +357,7 @@ class Private {
347357

348358
const tagList = document.createElement('li');
349359
tagList.classList.add(CODE_SNIPPET_INPUTTAG_LIST);
350-
for (const tag of tags) {
360+
for (const tag of snippetTags) {
351361
const tagElem = document.createElement('ul');
352362
tagElem.className = `${CODE_SNIPPET_INPUT_TAG} tag unapplied-tag`;
353363
const tagBtn = document.createElement('button');
@@ -412,10 +422,18 @@ class Private {
412422

413423
if (inputElement.value !== '' && event.keyCode === 13) {
414424
// duplicate tag
415-
if (Private.allTags.includes(inputElement.value)) {
425+
if (Private.allSnippetTags.includes(inputElement.value)) {
416426
alert('Duplicate Tag Name!');
417427
return;
418428
}
429+
430+
if (Private.allLangTags.includes(inputElement.value)) {
431+
alert(
432+
'This tag already exists in language tags!\nIf you want to create this tag, lowercase the first letter.'
433+
);
434+
return;
435+
}
436+
419437
event.preventDefault();
420438

421439
// create new tag
@@ -446,7 +464,7 @@ class Private {
446464

447465
// add it to the selected tags
448466
Private.selectedTags.push(tagBtn.innerText);
449-
Private.allTags.push(tagBtn.innerText);
467+
Private.allSnippetTags.push(tagBtn.innerText);
450468

451469
// reset InputElement
452470
inputElement.blur();

0 commit comments

Comments
 (0)