From 0a1608a22b05f1ca541c24af100b798cb9bf38a8 Mon Sep 17 00:00:00 2001 From: xieyuanhu Date: Mon, 5 Aug 2019 20:29:03 +0800 Subject: [PATCH] Provide a safe operation for the dialog Signed-off-by: xieyuanhu --- packages/core/src/browser/dialogs.ts | 33 +++++++++++++++---- .../src/browser/file-dialog/file-dialog.ts | 3 ++ 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/packages/core/src/browser/dialogs.ts b/packages/core/src/browser/dialogs.ts index fe7c25f875881..790aa9d34b5c2 100644 --- a/packages/core/src/browser/dialogs.ts +++ b/packages/core/src/browser/dialogs.ts @@ -148,15 +148,30 @@ export abstract class AbstractDialog extends BaseWidget { this.addKeyListener(document.body, Key.ENTER, e => this.handleEnter(e)); } + protected canHandle(event: KeyboardEvent): boolean { + let result: boolean = false; + if (this.id && event.currentTarget) { + const dialogOverlay: HTMLElement = event.currentTarget as HTMLElement; + if (dialogOverlay.lastElementChild && this.id.localeCompare(dialogOverlay.lastElementChild.id) === 0) { + result = true; + } + } + return result; + } + protected handleEscape(event: KeyboardEvent): boolean | void { - this.close(); + if (this.canHandle(event)) { + this.close(); + } } protected handleEnter(event: KeyboardEvent): boolean | void { - if (event.target instanceof HTMLTextAreaElement) { - return false; + if (this.canHandle(event)) { + if (event.target instanceof HTMLTextAreaElement) { + return false; + } + this.accept(); } - this.accept(); } protected onActivateRequest(msg: Message): void { @@ -277,7 +292,7 @@ export class ConfirmDialog extends AbstractDialog { @inject(ConfirmDialogProps) protected readonly props: ConfirmDialogProps ) { super(props); - + this.id = 'theia-confirm-dialog'; this.contentNode.appendChild(this.createMessageNode(this.props.msg)); this.appendCloseButton(props.cancel); this.appendAcceptButton(props.ok); @@ -324,7 +339,7 @@ export class SingleTextInputDialog extends AbstractDialog { @inject(SingleTextInputDialogProps) protected readonly props: SingleTextInputDialogProps ) { super(props); - + this.id = 'theia-single-text-input-dialog'; this.inputField = document.createElement('input'); this.inputField.type = 'text'; this.inputField.setAttribute('style', 'flex: 0;'); @@ -363,4 +378,10 @@ export class SingleTextInputDialog extends AbstractDialog { this.inputField.focus(); } + protected handleEnter(event: KeyboardEvent): boolean | void { + if (event.target instanceof HTMLInputElement) { + return super.handleEnter(event); + } + } + } diff --git a/packages/filesystem/src/browser/file-dialog/file-dialog.ts b/packages/filesystem/src/browser/file-dialog/file-dialog.ts index b62b0fb1660db..fa380ad35a749 100644 --- a/packages/filesystem/src/browser/file-dialog/file-dialog.ts +++ b/packages/filesystem/src/browser/file-dialog/file-dialog.ts @@ -242,6 +242,7 @@ export class OpenFileDialog extends FileDialog> { @inject(FileDialogWidget) readonly widget: FileDialogWidget ) { super(props, widget); + this.id = 'theia-open-file-dialog'; if (props.canSelectFiles !== undefined) { this.widget.disableFileSelection = !props.canSelectFiles; } @@ -276,6 +277,7 @@ export class OpenFileDialog extends FileDialog> { } super.accept(); } + } @injectable() @@ -288,6 +290,7 @@ export class SaveFileDialog extends FileDialog { @inject(FileDialogWidget) readonly widget: FileDialogWidget ) { super(props, widget); + this.id = 'theia-save-file-dialog'; widget.addClass(SAVE_DIALOG_CLASS); }