Skip to content

Commit d30f781

Browse files
committed
Add input box validation
1 parent 2df6357 commit d30f781

File tree

6 files changed

+90
-70
lines changed

6 files changed

+90
-70
lines changed

package.json

+18-5
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,6 @@
6464
"command": "gitlabSnippets.viewSnippet",
6565
"title": "View"
6666
},
67-
{
68-
"command": "gitlabSnippets.toggleHost",
69-
"title": "Expand/Collapse"
70-
},
7167
{
7268
"command": "gitlabSnippets.exploreMore",
7369
"title": "More"
@@ -86,13 +82,30 @@
8682
"menus": {
8783
"commandPalette": [
8884
{
89-
"command": "gitlabSnippets.publish"
85+
"command": "gitlabSnippets.publish",
86+
"when": "editorFocus"
9087
},
9188
{
9289
"command": "gitlabSnippets.addHost"
9390
},
91+
{
92+
"command": "gitlabSnippets.star",
93+
"when": "false"
94+
},
9495
{
9596
"command": "gitlabSnippets.starById"
97+
},
98+
{
99+
"command": "gitlabSnippets.unstar",
100+
"when": "false"
101+
},
102+
{
103+
"command": "gitlabSnippets.download",
104+
"when": "false"
105+
},
106+
{
107+
"command": "gitlabSnippets.viewSnippet",
108+
"when": "false"
96109
}
97110
],
98111
"view/item/context": [

src/addHost.ts

+12-4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ export default async function addHost(state: Memento, defaultValue?: string) {
99
ignoreFocusOut: true,
1010
prompt: 'Enter your gitlab host',
1111
value: defaultValue || PROTOCOL,
12+
validateInput(value) {
13+
if (value && value.replace(PROTOCOL, '').trim()) {
14+
return null;
15+
}
16+
return 'Must enter a valid host';
17+
},
1218
});
1319
if (!host) {
1420
return;
@@ -19,6 +25,11 @@ export default async function addHost(state: Memento, defaultValue?: string) {
1925
const token = await window.showInputBox({
2026
ignoreFocusOut: true,
2127
prompt: 'Enter your gitlab token',
28+
validateInput(value) {
29+
if (!value || !value.trim()) {
30+
return 'Personal token is required!';
31+
}
32+
},
2233
});
2334
if (!token) {
2435
return;
@@ -28,13 +39,10 @@ export default async function addHost(state: Memento, defaultValue?: string) {
2839
placeHolder: 'Choose host API version',
2940
ignoreFocusOut: true,
3041
});
31-
if (!version) {
32-
return;
33-
}
3442
const registry = {
3543
host,
3644
token,
37-
version: parseInt(version, 10),
45+
version: (version && parseInt(version, 10)) || 4,
3846
};
3947
const api = new SnippetRegistry(registry);
4048
const snippets = await api.getSnippets();

src/commandName.ts

-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ export type Command =
77
| 'star'
88
| 'unstar'
99
| 'viewSnippet'
10-
| 'toggleHost'
1110
| 'exploreMore';
1211

1312
export default function commandName(shorthand: Command) {

src/extension.ts

+53-53
Original file line numberDiff line numberDiff line change
@@ -30,60 +30,60 @@ export function activate(context: ExtensionContext) {
3030
);
3131
views = [staredView, hostsView, myView];
3232

33-
subscriptions.concat(
33+
[
3434
[
35-
[
36-
'addHost',
37-
async () => {
38-
const { registry } = (await addHost(globalState)) || {};
39-
if (registry) {
40-
globalState.update(configKey('lastUseHost'), registry.host);
41-
hostSnippetsProvider.openLastest();
42-
mySnippetsProvider.openLastest();
43-
}
44-
},
45-
],
46-
[
47-
'publish',
48-
async () => {
49-
const res = await publish(globalState);
50-
mySnippetsProvider.reload(res?.registry?.host);
51-
},
52-
],
53-
['reloadMySnippets', (host: Host) => mySnippetsProvider.reload(host)],
54-
[
55-
'reloadExploreSnippets',
56-
(host: Host) => hostSnippetsProvider.reload(host),
57-
],
58-
[
59-
'star',
60-
(snippet: Snippet) => {
61-
starSnippet(globalState, snippet);
62-
staredProvider.reload();
63-
},
64-
],
65-
[
66-
'starById',
67-
async () => {
68-
await starById(globalState);
69-
staredProvider.reload();
70-
},
71-
],
72-
[
73-
'unstar',
74-
(snippet: StaredSnippet) => {
75-
unstarSnippet(globalState, snippet);
76-
staredProvider.reload();
77-
},
78-
],
79-
[
80-
'download',
81-
(snippet: StaredSnippet | Snippet) =>
82-
downloadSnippet(globalState, snippet),
83-
],
84-
['viewSnippet', viewSnippet],
85-
['exploreMore', () => hostSnippetsProvider.loadMore()],
86-
].map(([cmd, callback]) =>
35+
'addHost',
36+
async () => {
37+
const { registry } = (await addHost(globalState)) || {};
38+
if (registry) {
39+
globalState.update(configKey('lastUseHost'), registry.host);
40+
hostSnippetsProvider.openLastest();
41+
mySnippetsProvider.openLastest();
42+
}
43+
},
44+
],
45+
[
46+
'publish',
47+
async () => {
48+
const res = await publish(globalState);
49+
mySnippetsProvider.reload(res?.registry?.host);
50+
},
51+
],
52+
['reloadMySnippets', (host: Host) => mySnippetsProvider.reload(host)],
53+
[
54+
'reloadExploreSnippets',
55+
(host: Host) => hostSnippetsProvider.reload(host),
56+
],
57+
[
58+
'star',
59+
(snippet: Snippet) => {
60+
starSnippet(globalState, snippet);
61+
staredProvider.reload();
62+
},
63+
],
64+
[
65+
'starById',
66+
async () => {
67+
await starById(globalState);
68+
staredProvider.reload();
69+
},
70+
],
71+
[
72+
'unstar',
73+
(snippet: StaredSnippet) => {
74+
unstarSnippet(globalState, snippet);
75+
staredProvider.reload();
76+
},
77+
],
78+
[
79+
'download',
80+
(snippet: StaredSnippet | Snippet) =>
81+
downloadSnippet(globalState, snippet),
82+
],
83+
['viewSnippet', viewSnippet],
84+
['exploreMore', () => hostSnippetsProvider.loadMore()],
85+
].forEach(([cmd, callback]) =>
86+
subscriptions.push(
8787
commands.registerCommand(
8888
commandName(cmd as Command),
8989
callback as () => void

src/publishSnippet.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export default async function publish(state: Memento) {
3333
}
3434
const fileName = await window.showInputBox({
3535
ignoreFocusOut: true,
36-
prompt: 'Enter file name',
36+
prompt: 'Enter file name with extention. E.g., "example.js".',
3737
value: activeTextEditor.document.fileName,
3838
});
3939
if (!fileName) {
@@ -42,14 +42,19 @@ export default async function publish(state: Memento) {
4242
const title = await window.showInputBox({
4343
ignoreFocusOut: true,
4444
prompt: 'Enter a title',
45+
validateInput(value) {
46+
if (!value || !value.trim()) {
47+
return 'Title is required!';
48+
}
49+
},
4550
});
4651
if (!title) {
4752
return;
4853
}
4954
const description =
5055
(await window.showInputBox({
5156
ignoreFocusOut: true,
52-
prompt: 'Enter a brief description',
57+
prompt: '[Optional]Enter a brief description',
5358
})) || null;
5459
const visibility =
5560
(await window.showQuickPick(Object.values(VISIBILITY), {

src/views/getHostItem.ts

-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { Host } from '../types';
22
import { TreeItem, TreeItemCollapsibleState, ThemeIcon } from 'vscode';
3-
import commandName from '../commandName';
43

54
export default function getHostItem(
65
{ host }: Host,
@@ -14,9 +13,5 @@ export default function getHostItem(
1413
? TreeItemCollapsibleState.Expanded
1514
: TreeItemCollapsibleState.Collapsed,
1615
iconPath: ThemeIcon.Folder,
17-
command: {
18-
command: commandName('toggleHost'),
19-
title: 'Expand/collapse host list',
20-
},
2116
};
2217
}

0 commit comments

Comments
 (0)