Skip to content

Commit d4f774a

Browse files
committed
Working on switching popup for duplicate names.
1 parent 7aa884f commit d4f774a

File tree

2 files changed

+50
-12
lines changed

2 files changed

+50
-12
lines changed

src/CodeSnippetEditor.tsx

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,14 @@ import { Message } from '@lumino/messaging';
2929

3030
import React from 'react';
3131

32-
import { CodeSnippetContentsService } from './CodeSnippetContentsService';
32+
import {
33+
CodeSnippetContentsService,
34+
ICodeSnippet
35+
} from './CodeSnippetContentsService';
3336
import { CodeSnippetWidget } from './CodeSnippetWidget';
3437
import { SUPPORTED_LANGUAGES } from './CodeSnippetLanguages';
3538
import { CodeSnippetEditorTags } from './CodeSnippetEditorTags';
39+
import { saveOverWriteFile } from './CodeSnippetInputDialog';
3640

3741
/**
3842
* CSS style classes
@@ -366,7 +370,7 @@ export class CodeSnippetEditor extends ReactWidget {
366370
message += 'Name must be filled out\n';
367371
status = false;
368372
}
369-
if (name.match(/[^a-z0-9_]+/)) {
373+
if (name.match(/[^a-zA-Z0-9_]+/)) {
370374
message += 'Wrong format of the name\n';
371375
status = false;
372376
}
@@ -410,6 +414,8 @@ export class CodeSnippetEditor extends ReactWidget {
410414
const newPath =
411415
'snippets/' + this._codeSnippetEditorMetaData.name + '.json';
412416

417+
//const oldPath = 'snippets/' + this.oldCodeSnippetName + '.json';
418+
413419
if (!this._codeSnippetEditorMetaData.fromScratch) {
414420
const oldPath = 'snippets/' + this.oldCodeSnippetName + '.json';
415421

@@ -418,14 +424,15 @@ export class CodeSnippetEditor extends ReactWidget {
418424
try {
419425
await this.contentsService.rename(oldPath, newPath);
420426
} catch (error) {
427+
console.log('tffff');
421428
await showDialog({
422429
title: 'Duplicate Name of Code Snippet',
423430
body: <p> {`"${newPath}" already exists.`} </p>,
424431
buttons: [Dialog.okButton({ label: 'Dismiss' })]
425432
});
426433
return false;
427434
}
428-
435+
console.log('huhhh');
429436
// set new name as an old name
430437
this.oldCodeSnippetName = this._codeSnippetEditorMetaData.name;
431438
}
@@ -435,17 +442,48 @@ export class CodeSnippetEditor extends ReactWidget {
435442
.getData(newPath, 'file')
436443
.then(async (value: Contents.IModel) => {
437444
if (value.name) {
438-
await showDialog({
439-
title: 'Duplicate Name of Code Snippet',
440-
body: <p> {`"${newPath}" already exists.`} </p>,
441-
buttons: [Dialog.okButton({ label: 'Dismiss' })]
442-
});
445+
console.log(
446+
this.codeSnippetWidget.codeSnippetWidgetModel.snippets[37]
447+
);
448+
const oldSnippet: ICodeSnippet = JSON.parse(value.content);
449+
const newSnippet: ICodeSnippet = {
450+
name: this._codeSnippetEditorMetaData.name,
451+
description: this._codeSnippetEditorMetaData.description,
452+
language: this._codeSnippetEditorMetaData.language,
453+
code: this._codeSnippetEditorMetaData.code,
454+
id: this._codeSnippetEditorMetaData.id,
455+
tags: this._codeSnippetEditorMetaData.selectedTags
456+
};
457+
console.log(oldSnippet.id);
458+
// await showDialog({
459+
// title: 'Duplicate Name of Code Snippet',
460+
// body: <p> {`"${newPath}" already exists.`} </p>,
461+
// buttons: [Dialog.okButton({ label: 'Dismiss' })]
462+
// });
463+
const result = saveOverWriteFile(
464+
this.codeSnippetWidget.codeSnippetWidgetModel,
465+
oldSnippet,
466+
newSnippet
467+
);
468+
result
469+
.then(newSnippets => {
470+
this.codeSnippetWidget.renderCodeSnippetsSignal.emit(
471+
newSnippets
472+
);
473+
})
474+
.catch(_ => {
475+
console.log('cancelling overwrite!');
476+
return false;
477+
});
443478
}
479+
return true;
444480
})
445481
.catch(() => {
446482
nameCheck = true;
483+
console.log('hi');
447484
});
448485
if (!nameCheck) {
486+
console.log('gdi');
449487
return false;
450488
}
451489
}
@@ -585,7 +623,7 @@ export class CodeSnippetEditor extends ReactWidget {
585623
></input>
586624
<p className={CODE_SNIPPET_EDITOR_INPUTNAME_VALIDITY}>
587625
{
588-
'Name of the code snippet MUST be lowercased, alphanumeric or composed of underscore(_)'
626+
'Name of the code snippet MUST be alphanumeric or composed of underscore(_)'
589627
}
590628
</p>
591629
<label className={CODE_SNIPPET_EDITOR_LABEL}>

src/CodeSnippetInputDialog.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ function createNewSnippet(
173173
/**
174174
* Rename a file, asking for confirmation if it is overwriting another.
175175
*/
176-
async function saveOverWriteFile(
176+
export async function saveOverWriteFile(
177177
codeSnippetWidgetModel: CodeSnippetWidgetModel,
178178
oldSnippet: ICodeSnippet,
179179
newSnippet: ICodeSnippet
@@ -196,7 +196,7 @@ async function saveOverWriteFile(
196196
/**
197197
* Ask the user whether to overwrite a file.
198198
*/
199-
async function shouldOverwrite(path: string): Promise<boolean> {
199+
export async function shouldOverwrite(path: string): Promise<boolean> {
200200
const options = {
201201
title: 'Overwrite code snippet?',
202202
body: `"${path}" already exists, overwrite?`,
@@ -314,7 +314,7 @@ class Private {
314314
const body = document.createElement('form');
315315
const nameValidity = document.createElement('p');
316316
nameValidity.textContent =
317-
'Name of the code snippet MUST be lowercased, alphanumeric, or composed of underscore(_)';
317+
'Name of the code snippet MUST be alphanumeric, or composed of underscore(_)';
318318
nameValidity.className = CODE_SNIPPET_INPUTNAME_VALIDITY;
319319

320320
const descriptionValidity = document.createElement('p');

0 commit comments

Comments
 (0)