From d30f781edcf5b94850d7715852494ad1a4fedc68 Mon Sep 17 00:00:00 2001 From: Jennie Ji Date: Sun, 19 Apr 2020 15:56:43 +0800 Subject: [PATCH] Add input box validation --- package.json | 23 +++++++-- src/addHost.ts | 16 ++++-- src/commandName.ts | 1 - src/extension.ts | 106 +++++++++++++++++++-------------------- src/publishSnippet.ts | 9 +++- src/views/getHostItem.ts | 5 -- 6 files changed, 90 insertions(+), 70 deletions(-) diff --git a/package.json b/package.json index 162d064..30c4b9e 100644 --- a/package.json +++ b/package.json @@ -64,10 +64,6 @@ "command": "gitlabSnippets.viewSnippet", "title": "View" }, - { - "command": "gitlabSnippets.toggleHost", - "title": "Expand/Collapse" - }, { "command": "gitlabSnippets.exploreMore", "title": "More" @@ -86,13 +82,30 @@ "menus": { "commandPalette": [ { - "command": "gitlabSnippets.publish" + "command": "gitlabSnippets.publish", + "when": "editorFocus" }, { "command": "gitlabSnippets.addHost" }, + { + "command": "gitlabSnippets.star", + "when": "false" + }, { "command": "gitlabSnippets.starById" + }, + { + "command": "gitlabSnippets.unstar", + "when": "false" + }, + { + "command": "gitlabSnippets.download", + "when": "false" + }, + { + "command": "gitlabSnippets.viewSnippet", + "when": "false" } ], "view/item/context": [ diff --git a/src/addHost.ts b/src/addHost.ts index f240b92..1615ee8 100644 --- a/src/addHost.ts +++ b/src/addHost.ts @@ -9,6 +9,12 @@ export default async function addHost(state: Memento, defaultValue?: string) { ignoreFocusOut: true, prompt: 'Enter your gitlab host', value: defaultValue || PROTOCOL, + validateInput(value) { + if (value && value.replace(PROTOCOL, '').trim()) { + return null; + } + return 'Must enter a valid host'; + }, }); if (!host) { return; @@ -19,6 +25,11 @@ export default async function addHost(state: Memento, defaultValue?: string) { const token = await window.showInputBox({ ignoreFocusOut: true, prompt: 'Enter your gitlab token', + validateInput(value) { + if (!value || !value.trim()) { + return 'Personal token is required!'; + } + }, }); if (!token) { return; @@ -28,13 +39,10 @@ export default async function addHost(state: Memento, defaultValue?: string) { placeHolder: 'Choose host API version', ignoreFocusOut: true, }); - if (!version) { - return; - } const registry = { host, token, - version: parseInt(version, 10), + version: (version && parseInt(version, 10)) || 4, }; const api = new SnippetRegistry(registry); const snippets = await api.getSnippets(); diff --git a/src/commandName.ts b/src/commandName.ts index 1e13e08..0ba11b2 100644 --- a/src/commandName.ts +++ b/src/commandName.ts @@ -7,7 +7,6 @@ export type Command = | 'star' | 'unstar' | 'viewSnippet' - | 'toggleHost' | 'exploreMore'; export default function commandName(shorthand: Command) { diff --git a/src/extension.ts b/src/extension.ts index 229091c..1e02d19 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -30,60 +30,60 @@ export function activate(context: ExtensionContext) { ); views = [staredView, hostsView, myView]; - subscriptions.concat( + [ [ - [ - 'addHost', - async () => { - const { registry } = (await addHost(globalState)) || {}; - if (registry) { - globalState.update(configKey('lastUseHost'), registry.host); - hostSnippetsProvider.openLastest(); - mySnippetsProvider.openLastest(); - } - }, - ], - [ - 'publish', - async () => { - const res = await publish(globalState); - mySnippetsProvider.reload(res?.registry?.host); - }, - ], - ['reloadMySnippets', (host: Host) => mySnippetsProvider.reload(host)], - [ - 'reloadExploreSnippets', - (host: Host) => hostSnippetsProvider.reload(host), - ], - [ - 'star', - (snippet: Snippet) => { - starSnippet(globalState, snippet); - staredProvider.reload(); - }, - ], - [ - 'starById', - async () => { - await starById(globalState); - staredProvider.reload(); - }, - ], - [ - 'unstar', - (snippet: StaredSnippet) => { - unstarSnippet(globalState, snippet); - staredProvider.reload(); - }, - ], - [ - 'download', - (snippet: StaredSnippet | Snippet) => - downloadSnippet(globalState, snippet), - ], - ['viewSnippet', viewSnippet], - ['exploreMore', () => hostSnippetsProvider.loadMore()], - ].map(([cmd, callback]) => + 'addHost', + async () => { + const { registry } = (await addHost(globalState)) || {}; + if (registry) { + globalState.update(configKey('lastUseHost'), registry.host); + hostSnippetsProvider.openLastest(); + mySnippetsProvider.openLastest(); + } + }, + ], + [ + 'publish', + async () => { + const res = await publish(globalState); + mySnippetsProvider.reload(res?.registry?.host); + }, + ], + ['reloadMySnippets', (host: Host) => mySnippetsProvider.reload(host)], + [ + 'reloadExploreSnippets', + (host: Host) => hostSnippetsProvider.reload(host), + ], + [ + 'star', + (snippet: Snippet) => { + starSnippet(globalState, snippet); + staredProvider.reload(); + }, + ], + [ + 'starById', + async () => { + await starById(globalState); + staredProvider.reload(); + }, + ], + [ + 'unstar', + (snippet: StaredSnippet) => { + unstarSnippet(globalState, snippet); + staredProvider.reload(); + }, + ], + [ + 'download', + (snippet: StaredSnippet | Snippet) => + downloadSnippet(globalState, snippet), + ], + ['viewSnippet', viewSnippet], + ['exploreMore', () => hostSnippetsProvider.loadMore()], + ].forEach(([cmd, callback]) => + subscriptions.push( commands.registerCommand( commandName(cmd as Command), callback as () => void diff --git a/src/publishSnippet.ts b/src/publishSnippet.ts index 497f2fb..7a2785e 100644 --- a/src/publishSnippet.ts +++ b/src/publishSnippet.ts @@ -33,7 +33,7 @@ export default async function publish(state: Memento) { } const fileName = await window.showInputBox({ ignoreFocusOut: true, - prompt: 'Enter file name', + prompt: 'Enter file name with extention. E.g., "example.js".', value: activeTextEditor.document.fileName, }); if (!fileName) { @@ -42,6 +42,11 @@ export default async function publish(state: Memento) { const title = await window.showInputBox({ ignoreFocusOut: true, prompt: 'Enter a title', + validateInput(value) { + if (!value || !value.trim()) { + return 'Title is required!'; + } + }, }); if (!title) { return; @@ -49,7 +54,7 @@ export default async function publish(state: Memento) { const description = (await window.showInputBox({ ignoreFocusOut: true, - prompt: 'Enter a brief description', + prompt: '[Optional]Enter a brief description', })) || null; const visibility = (await window.showQuickPick(Object.values(VISIBILITY), { diff --git a/src/views/getHostItem.ts b/src/views/getHostItem.ts index f94de8d..f8d35e5 100644 --- a/src/views/getHostItem.ts +++ b/src/views/getHostItem.ts @@ -1,6 +1,5 @@ import { Host } from '../types'; import { TreeItem, TreeItemCollapsibleState, ThemeIcon } from 'vscode'; -import commandName from '../commandName'; export default function getHostItem( { host }: Host, @@ -14,9 +13,5 @@ export default function getHostItem( ? TreeItemCollapsibleState.Expanded : TreeItemCollapsibleState.Collapsed, iconPath: ThemeIcon.Folder, - command: { - command: commandName('toggleHost'), - title: 'Expand/collapse host list', - }, }; }