forked from PowerShell/vscode-powershell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheckboxQuickPick.ts
86 lines (67 loc) · 2.57 KB
/
checkboxQuickPick.ts
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
import vscode = require("vscode");
const confirmItemLabel = "$(checklist) Confirm";
const checkedPrefix = "[ $(check) ]";
const uncheckedPrefix = "[ ]";
const defaultPlaceHolder = "Select 'Confirm' to confirm or press 'Esc' key to cancel";
export interface ICheckboxQuickPickItem {
label: string;
description?: string;
isSelected: boolean;
}
export interface ICheckboxQuickPickOptions {
confirmPlaceHolder: string;
}
const defaultOptions: ICheckboxQuickPickOptions = { confirmPlaceHolder: defaultPlaceHolder };
export async function showCheckboxQuickPick(
items: ICheckboxQuickPickItem[],
options: ICheckboxQuickPickOptions = defaultOptions): Promise<ICheckboxQuickPickItem[] | undefined> {
const selectedItem = await showInner(items, options);
return selectedItem !== undefined ? items : undefined;
}
function getQuickPickItems(items: ICheckboxQuickPickItem[]): vscode.QuickPickItem[] {
const quickPickItems: vscode.QuickPickItem[] = [];
quickPickItems.push({ label: confirmItemLabel, description: "" });
for (const item of items) {
quickPickItems.push({
label: convertToCheckBox(item),
description: item.description,
});
}
return quickPickItems;
}
async function showInner(
items: ICheckboxQuickPickItem[],
options: ICheckboxQuickPickOptions): Promise<vscode.QuickPickItem | undefined> {
const selection = await vscode.window.showQuickPick(
getQuickPickItems(items),
{
ignoreFocusOut: true,
matchOnDescription: true,
placeHolder: options.confirmPlaceHolder,
});
if (selection === undefined) {
return undefined;
}
if (selection.label === confirmItemLabel) {
return selection;
}
const index: number = getItemIndex(items, selection.label);
if (index >= 0) {
toggleSelection(items[index]);
} else {
console.log(`Couldn't find CheckboxQuickPickItem for label '${selection.label}'`);
}
return showInner(items, options);
}
function getItemIndex(items: ICheckboxQuickPickItem[], itemLabel: string): number {
const trimmedLabel = itemLabel.substring(itemLabel.indexOf("]") + 2);
return items.findIndex((item) => item.label === trimmedLabel);
}
function toggleSelection(item: ICheckboxQuickPickItem): void {
item.isSelected = !item.isSelected;
}
function convertToCheckBox(item: ICheckboxQuickPickItem): string {
return `${item.isSelected ? checkedPrefix : uncheckedPrefix} ${item.label}`;
}