|
2 | 2 | * Copyright (c) Red Hat, Inc. All rights reserved. |
3 | 3 | * Licensed under the MIT License. See LICENSE file in the project root for license information. |
4 | 4 | *-----------------------------------------------------------------------------------------------*/ |
5 | | -import { InputBox, QuickInputButton, QuickInputButtons, ThemeIcon, window } from 'vscode'; |
| 5 | +import { env, InputBox, QuickInputButton, QuickInputButtons, ThemeIcon, window } from 'vscode'; |
6 | 6 |
|
7 | 7 | export class quickBtn implements QuickInputButton { |
8 | 8 | constructor(public iconPath: ThemeIcon, public tooltip: string) { } |
@@ -31,12 +31,35 @@ export function inputValue(prompt: string, initialValue: string, password: boole |
31 | 31 | if (placeHolder) input.placeholder = placeHolder; |
32 | 32 | const enterBtn = new quickBtn(new ThemeIcon('check'), 'Enter'); |
33 | 33 | const cancelBtn = new quickBtn(new ThemeIcon('close'), 'Cancel'); |
34 | | - input.buttons = [QuickInputButtons.Back, enterBtn, cancelBtn]; |
| 34 | + const pasteBtn = new quickBtn(new ThemeIcon('output'), 'Paste from Clipboard'); |
| 35 | + const hideBtn = new quickBtn(new ThemeIcon('eye'), 'Hide value'); |
| 36 | + const showBtn = new quickBtn(new ThemeIcon('eye-closed'), 'Show value'); |
| 37 | + let isHidden = password; |
| 38 | + const updateButtons = (() => { |
| 39 | + const buttons = []; |
| 40 | + if (password) { |
| 41 | + buttons.push(isHidden ? showBtn : hideBtn); |
| 42 | + } |
| 43 | + buttons.push(pasteBtn, QuickInputButtons.Back, enterBtn, cancelBtn); |
| 44 | + input.buttons = buttons; |
| 45 | + }); |
| 46 | + updateButtons(); |
35 | 47 | const validationMessage: string = validate(input.value? input.value : ''); |
36 | 48 | const resolveAndClose = ((result) => { |
37 | 49 | input.dispose(); |
38 | 50 | resolve(result); |
39 | 51 | }); |
| 52 | + const pasteFromClipboard = (async () => { |
| 53 | + try { |
| 54 | + const clipboard = (await env.clipboard.readText()).trim(); |
| 55 | + if (clipboard.length > 0) { |
| 56 | + input.value = clipboard; |
| 57 | + input.validationMessage = validate(clipboard); |
| 58 | + } |
| 59 | + } catch (ignore) { |
| 60 | + // Do nothing |
| 61 | + } |
| 62 | + }); |
40 | 63 | input.ignoreFocusOut = true; |
41 | 64 | if (validationMessage) { |
42 | 65 | input.validationMessage = validationMessage; |
@@ -67,6 +90,13 @@ export function inputValue(prompt: string, initialValue: string, password: boole |
67 | 90 | }) |
68 | 91 | input.onDidTriggerButton(async (event) => { |
69 | 92 | if (event === QuickInputButtons.Back) resolveAndClose(undefined); |
| 93 | + else if (event === showBtn) { |
| 94 | + input.password = isHidden = !isHidden; |
| 95 | + updateButtons(); |
| 96 | + } else if (event === hideBtn) { |
| 97 | + input.password = isHidden = !isHidden; |
| 98 | + updateButtons(); |
| 99 | + } else if (event === pasteBtn) await pasteFromClipboard(); |
70 | 100 | else if (event === cancelBtn) resolveAndClose(null); |
71 | 101 | else if (event === enterBtn) await acceptInput(); |
72 | 102 | }); |
|
0 commit comments