Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/workbench/browser/parts/dialogs/dialogHandler.ts
3296 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { localize } from '../../../../nls.js';
7
import { IConfirmation, IConfirmationResult, IInputResult, ICheckbox, IInputElement, ICustomDialogOptions, IInput, AbstractDialogHandler, DialogType, IPrompt, IAsyncPromptResult } from '../../../../platform/dialogs/common/dialogs.js';
8
import { ILayoutService } from '../../../../platform/layout/browser/layoutService.js';
9
import { ILogService } from '../../../../platform/log/common/log.js';
10
import Severity from '../../../../base/common/severity.js';
11
import { Dialog, IDialogResult } from '../../../../base/browser/ui/dialog/dialog.js';
12
import { DisposableStore } from '../../../../base/common/lifecycle.js';
13
import { IKeybindingService } from '../../../../platform/keybinding/common/keybinding.js';
14
import { IClipboardService } from '../../../../platform/clipboard/common/clipboardService.js';
15
import { IInstantiationService } from '../../../../platform/instantiation/common/instantiation.js';
16
import { MarkdownRenderer, openLinkFromMarkdown } from '../../../../editor/browser/widget/markdownRenderer/browser/markdownRenderer.js';
17
import { IOpenerService } from '../../../../platform/opener/common/opener.js';
18
import { createWorkbenchDialogOptions } from '../../../../platform/dialogs/browser/dialog.js';
19
20
export class BrowserDialogHandler extends AbstractDialogHandler {
21
22
private static readonly ALLOWABLE_COMMANDS = [
23
'copy',
24
'cut',
25
'editor.action.selectAll',
26
'editor.action.clipboardCopyAction',
27
'editor.action.clipboardCutAction',
28
'editor.action.clipboardPasteAction'
29
];
30
31
private readonly markdownRenderer: MarkdownRenderer;
32
33
constructor(
34
@ILogService private readonly logService: ILogService,
35
@ILayoutService private readonly layoutService: ILayoutService,
36
@IKeybindingService private readonly keybindingService: IKeybindingService,
37
@IInstantiationService instantiationService: IInstantiationService,
38
@IClipboardService private readonly clipboardService: IClipboardService,
39
@IOpenerService private readonly openerService: IOpenerService
40
) {
41
super();
42
43
this.markdownRenderer = instantiationService.createInstance(MarkdownRenderer, {});
44
}
45
46
async prompt<T>(prompt: IPrompt<T>): Promise<IAsyncPromptResult<T>> {
47
this.logService.trace('DialogService#prompt', prompt.message);
48
49
const buttons = this.getPromptButtons(prompt);
50
51
const { button, checkboxChecked } = await this.doShow(prompt.type, prompt.message, buttons, prompt.detail, prompt.cancelButton ? buttons.length - 1 : -1 /* Disabled */, prompt.checkbox, undefined, typeof prompt?.custom === 'object' ? prompt.custom : undefined);
52
53
return this.getPromptResult(prompt, button, checkboxChecked);
54
}
55
56
async confirm(confirmation: IConfirmation): Promise<IConfirmationResult> {
57
this.logService.trace('DialogService#confirm', confirmation.message);
58
59
const buttons = this.getConfirmationButtons(confirmation);
60
61
const { button, checkboxChecked } = await this.doShow(confirmation.type ?? 'question', confirmation.message, buttons, confirmation.detail, buttons.length - 1, confirmation.checkbox, undefined, typeof confirmation?.custom === 'object' ? confirmation.custom : undefined);
62
63
return { confirmed: button === 0, checkboxChecked };
64
}
65
66
async input(input: IInput): Promise<IInputResult> {
67
this.logService.trace('DialogService#input', input.message);
68
69
const buttons = this.getInputButtons(input);
70
71
const { button, checkboxChecked, values } = await this.doShow(input.type ?? 'question', input.message, buttons, input.detail, buttons.length - 1, input?.checkbox, input.inputs, typeof input.custom === 'object' ? input.custom : undefined);
72
73
return { confirmed: button === 0, checkboxChecked, values };
74
}
75
76
async about(title: string, details: string, detailsToCopy: string): Promise<void> {
77
78
const { button } = await this.doShow(
79
Severity.Info,
80
title,
81
[
82
localize({ key: 'copy', comment: ['&& denotes a mnemonic'] }, "&&Copy"),
83
localize('ok', "OK")
84
],
85
details,
86
1
87
);
88
89
if (button === 0) {
90
this.clipboardService.writeText(detailsToCopy);
91
}
92
}
93
94
private async doShow(type: Severity | DialogType | undefined, message: string, buttons?: string[], detail?: string, cancelId?: number, checkbox?: ICheckbox, inputs?: IInputElement[], customOptions?: ICustomDialogOptions): Promise<IDialogResult> {
95
const dialogDisposables = new DisposableStore();
96
97
const renderBody = customOptions ? (parent: HTMLElement) => {
98
parent.classList.add(...(customOptions.classes || []));
99
customOptions.markdownDetails?.forEach(markdownDetail => {
100
const result = dialogDisposables.add(this.markdownRenderer.render(markdownDetail.markdown, {
101
actionHandler: markdownDetail.actionHandler || ((link, mdStr) => {
102
return openLinkFromMarkdown(this.openerService, link, mdStr.isTrusted, true /* skip URL validation to prevent another dialog from showing which is unsupported */);
103
}),
104
}));
105
parent.appendChild(result.element);
106
result.element.classList.add(...(markdownDetail.classes || []));
107
});
108
} : undefined;
109
110
const dialog = new Dialog(
111
this.layoutService.activeContainer,
112
message,
113
buttons,
114
createWorkbenchDialogOptions({
115
detail,
116
cancelId,
117
type: this.getDialogType(type),
118
renderBody,
119
icon: customOptions?.icon,
120
disableCloseAction: customOptions?.disableCloseAction,
121
buttonOptions: customOptions?.buttonDetails?.map(detail => ({ sublabel: detail })),
122
checkboxLabel: checkbox?.label,
123
checkboxChecked: checkbox?.checked,
124
inputs
125
}, this.keybindingService, this.layoutService, BrowserDialogHandler.ALLOWABLE_COMMANDS)
126
);
127
128
dialogDisposables.add(dialog);
129
130
const result = await dialog.show();
131
dialogDisposables.dispose();
132
133
return result;
134
}
135
}
136
137