-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Expand file tree
/
Copy pathmultiStepInput.ts
More file actions
307 lines (276 loc) · 9.27 KB
/
multiStepInput.ts
File metadata and controls
307 lines (276 loc) · 9.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { QuickPickItem, window, Disposable, CancellationToken, QuickInputButton, QuickInput, ExtensionContext, QuickInputButtons, Uri } from 'vscode';
/**
* A multi-step input using window.createQuickPick() and window.createInputBox().
*
* This first part uses the helper class `MultiStepInput` that wraps the API for the multi-step case.
*/
export async function multiStepInput(context: ExtensionContext) {
class MyButton implements QuickInputButton {
constructor(public iconPath: { light: Uri; dark: Uri; }, public tooltip: string) { }
}
const createResourceGroupButton = new MyButton({
dark: Uri.file(context.asAbsolutePath('resources/dark/add.svg')),
light: Uri.file(context.asAbsolutePath('resources/light/add.svg')),
}, 'Create Resource Group');
const resourceGroups: QuickPickItem[] = ['vscode-data-function', 'vscode-appservice-microservices', 'vscode-appservice-monitor', 'vscode-appservice-preview', 'vscode-appservice-prod']
.map(label => ({ label }));
interface State {
title: string;
step: number;
totalSteps: number;
resourceGroup: QuickPickItem | string;
name: string;
runtime: QuickPickItem;
}
async function collectInputs() {
const state = {} as Partial<State>;
await MultiStepInput.run(input => pickResourceGroup(input, state));
return state as State;
}
const title = 'Create Application Service';
async function pickResourceGroup(input: MultiStepInput, state: Partial<State>) {
const pick = await input.showQuickPick({
title,
step: 1,
totalSteps: 3,
placeholder: 'Pick a resource group',
items: resourceGroups,
activeItem: typeof state.resourceGroup !== 'string' ? state.resourceGroup : undefined,
buttons: [createResourceGroupButton],
shouldResume: shouldResume
});
if (pick instanceof MyButton) {
return (input: MultiStepInput) => inputResourceGroupName(input, state);
}
state.resourceGroup = pick;
return (input: MultiStepInput) => inputName(input, state);
}
async function inputResourceGroupName(input: MultiStepInput, state: Partial<State>) {
state.resourceGroup = await input.showInputBox({
title,
step: 2,
totalSteps: 4,
value: typeof state.resourceGroup === 'string' ? state.resourceGroup : '',
prompt: 'Choose a unique name for the resource group',
validate: validateNameIsUnique,
shouldResume: shouldResume
});
return (input: MultiStepInput) => inputName(input, state);
}
async function inputName(input: MultiStepInput, state: Partial<State>) {
const additionalSteps = typeof state.resourceGroup === 'string' ? 1 : 0;
// TODO: Remember current value when navigating back.
state.name = await input.showInputBox({
title,
step: 2 + additionalSteps,
totalSteps: 3 + additionalSteps,
value: state.name || '',
prompt: 'Choose a unique name for the Application Service',
validate: validateNameIsUnique,
shouldResume: shouldResume
});
return (input: MultiStepInput) => pickRuntime(input, state);
}
async function pickRuntime(input: MultiStepInput, state: Partial<State>) {
const additionalSteps = typeof state.resourceGroup === 'string' ? 1 : 0;
const runtimes = await getAvailableRuntimes(state.resourceGroup!, undefined /* TODO: token */);
// TODO: Remember currently active item when navigating back.
state.runtime = await input.showQuickPick({
title,
step: 3 + additionalSteps,
totalSteps: 3 + additionalSteps,
placeholder: 'Pick a runtime',
items: runtimes,
activeItem: state.runtime,
shouldResume: shouldResume
});
}
function shouldResume() {
// Could show a notification with the option to resume.
return new Promise<boolean>((resolve, reject) => {
});
}
async function validateNameIsUnique(name: string) {
// ...validate...
await new Promise(resolve => setTimeout(resolve, 1000));
return name === 'vscode' ? 'Name not unique' : undefined;
}
async function getAvailableRuntimes(resourceGroup: QuickPickItem | string, token?: CancellationToken): Promise<QuickPickItem[]> {
// ...retrieve...
await new Promise(resolve => setTimeout(resolve, 1000));
return ['Node 8.9', 'Node 6.11', 'Node 4.5']
.map(label => ({ label }));
}
const state = await collectInputs();
window.showInformationMessage(`Creating Application Service '${state.name}'`);
}
// -------------------------------------------------------
// Helper code that wraps the API for the multi-step case.
// -------------------------------------------------------
class InputFlowAction {
private constructor() { }
static back = new InputFlowAction();
static cancel = new InputFlowAction();
static resume = new InputFlowAction();
}
type InputStep = (input: MultiStepInput) => Thenable<InputStep | void>;
interface QuickPickParameters<T extends QuickPickItem> {
title: string;
step: number;
totalSteps: number;
items: T[];
activeItem?: T;
placeholder: string;
buttons?: QuickInputButton[];
shouldResume: () => Thenable<boolean>;
}
interface InputBoxParameters {
title: string;
step: number;
totalSteps: number;
value: string;
prompt: string;
validate: (value: string) => Promise<string | undefined>;
buttons?: QuickInputButton[];
shouldResume: () => Thenable<boolean>;
}
class MultiStepInput {
static async run<T>(start: InputStep) {
const input = new MultiStepInput();
return input.stepThrough(start);
}
private current?: QuickInput;
private steps: InputStep[] = [];
private async stepThrough<T>(start: InputStep) {
let step: InputStep | void = start;
while (step) {
this.steps.push(step);
if (this.current) {
this.current.enabled = false;
this.current.busy = true;
}
try {
step = await step(this);
} catch (err) {
if (err === InputFlowAction.back) {
this.steps.pop();
step = this.steps.pop();
} else if (err === InputFlowAction.resume) {
step = this.steps.pop();
} else if (err === InputFlowAction.cancel) {
step = undefined;
} else {
throw err;
}
}
}
if (this.current) {
this.current.dispose();
}
}
async showQuickPick<T extends QuickPickItem, P extends QuickPickParameters<T>>({ title, step, totalSteps, items, activeItem, placeholder, buttons, shouldResume }: P) {
const disposables: Disposable[] = [];
try {
return await new Promise<T | (P extends { buttons: (infer I)[] } ? I : never)>((resolve, reject) => {
const input = window.createQuickPick<T>();
input.title = title;
input.step = step;
input.totalSteps = totalSteps;
input.placeholder = placeholder;
input.items = items;
if (activeItem) {
input.activeItems = [activeItem];
}
input.buttons = [
...(this.steps.length > 1 ? [QuickInputButtons.Back] : []),
...(buttons || [])
];
disposables.push(
input.onDidTriggerButton(item => {
if (item === QuickInputButtons.Back) {
reject(InputFlowAction.back);
} else {
resolve(<any>item);
}
}),
input.onDidChangeSelection(items => resolve(items[0])),
input.onDidHide(() => {
(async () => {
reject(shouldResume && await shouldResume() ? InputFlowAction.resume : InputFlowAction.cancel);
})()
.catch(reject);
})
);
if (this.current) {
this.current.dispose();
}
this.current = input;
setTimeout(() => input.show(), 5);
});
} finally {
disposables.forEach(d => d.dispose());
}
}
async showInputBox<P extends InputBoxParameters>({ title, step, totalSteps, value, prompt, validate, buttons, shouldResume }: P) {
const disposables: Disposable[] = [];
try {
return await new Promise<string | (P extends { buttons: (infer I)[] } ? I : never)>((resolve, reject) => {
const input = window.createInputBox();
input.title = title;
input.step = step;
input.totalSteps = totalSteps;
input.value = value || '';
input.prompt = prompt;
input.buttons = [
...(this.steps.length > 1 ? [QuickInputButtons.Back] : []),
...(buttons || [])
];
let validating = validate('');
disposables.push(
input.onDidTriggerButton(item => {
if (item === QuickInputButtons.Back) {
reject(InputFlowAction.back);
} else {
resolve(<any>item);
}
}),
input.onDidAccept(async () => {
const value = input.value;
input.enabled = false;
input.busy = true;
if (!(await validate(value))) {
resolve(value);
}
input.enabled = true;
input.busy = false;
}),
input.onDidChangeValue(async text => {
const current = validate(text);
validating = current;
const validationMessage = await current;
if (current === validating) {
input.validationMessage = validationMessage;
}
}),
input.onDidHide(() => {
(async () => {
reject(shouldResume && await shouldResume() ? InputFlowAction.resume : InputFlowAction.cancel);
})()
.catch(reject);
})
);
if (this.current) {
this.current.dispose();
}
this.current = input;
setTimeout(() => input.show(), 5);
});
} finally {
disposables.forEach(d => d.dispose());
}
}
}