Skip to content

Commit 3cc58c9

Browse files
committed
Have 'close without saving' consistent with JupyterLab notebook
1 parent 9e4bdd6 commit 3cc58c9

File tree

1 file changed

+38
-10
lines changed

1 file changed

+38
-10
lines changed

src/CodeSnippetEditor.tsx

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -234,11 +234,38 @@ export class CodeSnippetEditor extends ReactWidget {
234234
{`"${this._codeSnippetEditorMetaData.name}" has unsaved changes, close without saving?`}{' '}
235235
</p>
236236
),
237-
buttons: [Dialog.cancelButton(), Dialog.okButton()]
237+
buttons: [
238+
Dialog.cancelButton(),
239+
Dialog.warnButton({ label: 'Discard' }),
240+
Dialog.okButton({ label: 'Save' })
241+
]
238242
}).then((response: any): void => {
243+
console.log(response.button);
239244
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+
}
242269
}
243270
});
244271
} else {
@@ -348,7 +375,7 @@ export class CodeSnippetEditor extends ReactWidget {
348375
return status;
349376
}
350377

351-
async updateSnippet(): Promise<void> {
378+
async updateSnippet(): Promise<boolean> {
352379
const name = (document.querySelector(
353380
`.${CODE_SNIPPET_EDITOR}-${this._codeSnippetEditorMetaData.id} .${CODE_SNIPPET_EDITOR_NAME_INPUT}`
354381
) as HTMLInputElement).value;
@@ -363,8 +390,6 @@ export class CodeSnippetEditor extends ReactWidget {
363390
this._codeSnippetEditorMetaData.description = description;
364391
this._codeSnippetEditorMetaData.language = language;
365392

366-
this.saved = true;
367-
368393
const newPath =
369394
'snippets/' + this._codeSnippetEditorMetaData.name + '.json';
370395

@@ -379,9 +404,9 @@ export class CodeSnippetEditor extends ReactWidget {
379404
await showDialog({
380405
title: 'Duplicate Name of Code Snippet',
381406
body: <p> {`"${newPath}" already exists.`} </p>,
382-
buttons: [Dialog.cancelButton()]
407+
buttons: [Dialog.okButton({ label: 'Dismiss' })]
383408
});
384-
return;
409+
return false;
385410
}
386411

387412
// set new name as an old name
@@ -396,18 +421,20 @@ export class CodeSnippetEditor extends ReactWidget {
396421
await showDialog({
397422
title: 'Duplicate Name of Code Snippet',
398423
body: <p> {`"${newPath}" already exists.`} </p>,
399-
buttons: [Dialog.cancelButton()]
424+
buttons: [Dialog.okButton({ label: 'Dismiss' })]
400425
});
401426
}
402427
})
403428
.catch(() => {
404429
nameCheck = true;
405430
});
406431
if (!nameCheck) {
407-
return;
432+
return false;
408433
}
409434
}
410435

436+
this.saved = true;
437+
411438
await this.contentsService.save(newPath, {
412439
type: 'file',
413440
format: 'text',
@@ -446,6 +473,7 @@ export class CodeSnippetEditor extends ReactWidget {
446473
if (this._codeSnippetEditorMetaData.fromScratch) {
447474
this.dispose();
448475
}
476+
return true;
449477
}
450478

451479
handleChangeOnTag(selectedTags: string[], allTags: string[]): void {

0 commit comments

Comments
 (0)