Skip to content

Commit c9c1b32

Browse files
committed
Refactor, add commands folder
1 parent 367be61 commit c9c1b32

5 files changed

Lines changed: 98 additions & 77 deletions

File tree

src/commands/index.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import * as vscode from 'vscode';
2+
import { registerUserCommands } from './user'
3+
import { registerTreeViewCommands } from './treeView'
4+
import { Store } from '../store';
5+
6+
export function registerCommand(context: vscode.ExtensionContext, store: Store) {
7+
registerUserCommands(context, store);
8+
registerTreeViewCommands(context, store)
9+
}

src/commands/treeView.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import * as vscode from 'vscode'
2+
import { Store } from '../store';
3+
import * as apiClient from '@hackmd/api';
4+
import { HackMDTreeViewProvider } from './../tree/index'
5+
import { MdTextDocumentContentProvider } from './../mdTextDocument';
6+
const API = new apiClient.default();
7+
8+
export async function registerTreeViewCommands(context: vscode.ExtensionContext, store: Store) {
9+
const hackMDTreeViewProvider = new HackMDTreeViewProvider(store);
10+
context.subscriptions.push(vscode.window.registerTreeDataProvider('mdTreeItems', hackMDTreeViewProvider));
11+
context.subscriptions.push(vscode.commands.registerCommand('treeView.refreshList', () => hackMDTreeViewProvider.refresh()));
12+
13+
context.subscriptions.push(vscode.commands.registerCommand('clickTreeItem', async (label, noteId) => {
14+
if (label && noteId) {
15+
const content = await API.exportString(noteId, apiClient.ExportType.MD);
16+
if (content) {
17+
const uri = vscode.Uri.parse(`hackmd:${label}.md#${noteId}`);
18+
const doc = await vscode.workspace.openTextDocument(uri);
19+
await vscode.window.showTextDocument(doc, { preview: false });
20+
}
21+
}
22+
}));
23+
context.subscriptions.push(vscode.workspace.registerTextDocumentContentProvider('hackmd', new MdTextDocumentContentProvider()));
24+
}

src/commands/user.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import * as vscode from 'vscode'
2+
import { checkLogin, login, refreshHistoryList } from './../tree/index'
3+
import { Store } from '../store';
4+
import * as apiClient from '@hackmd/api';
5+
const API = new apiClient.default();
6+
7+
export async function registerUserCommands(context: vscode.ExtensionContext, store: Store) {
8+
context.subscriptions.push(vscode.commands.registerCommand('HackMD.login', async () => {
9+
if (await checkLogin()) {
10+
vscode.window.showInformationMessage('Already logged in, please log out first.');
11+
return;
12+
}
13+
14+
const email = await vscode.window.showInputBox({
15+
ignoreFocusOut: true,
16+
password: false,
17+
placeHolder: 'email',
18+
prompt: 'Please enter your email account',
19+
validateInput: (text) => {
20+
if (text && text !== "") {
21+
return undefined;
22+
} else {
23+
return 'Email cannot be empty';
24+
}
25+
}
26+
});
27+
const password = await vscode.window.showInputBox({
28+
ignoreFocusOut: true,
29+
password: true,
30+
placeHolder: 'password',
31+
prompt: 'Please enter your password',
32+
validateInput: (text) => {
33+
if (text && text !== "") {
34+
return undefined;
35+
} else {
36+
return 'password cannot be empty';
37+
}
38+
}
39+
});
40+
41+
context.globalState.update('email', email);
42+
context.globalState.update('password', password);
43+
44+
await login(context);
45+
await refreshHistoryList(context);
46+
}));
47+
48+
context.subscriptions.push(vscode.commands.registerCommand('HackMD.logout', async () => {
49+
if (!(await checkLogin())) {
50+
vscode.window.showInformationMessage('Currently not logged in.');
51+
return;
52+
}
53+
await API.logout();
54+
store.isLogin = false;
55+
context.globalState.update('isLogin', false);
56+
vscode.window.showInformationMessage('Successfully logged out.');
57+
await refreshHistoryList(context);
58+
}));
59+
}

src/extension.ts

Lines changed: 2 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
// The module 'vscode' contains the VS Code extensibility API
22
// Import the module and reference it with the alias vscode in your code below
33
import axios from 'axios';
4-
import * as apiClient from '@hackmd/api';
5-
import { MdTextDocumentContentProvider } from './mdTextDocument';
64
import * as vscode from 'vscode';
75
import * as markdownitContainer from 'markdown-it-container';
86
import * as S from 'string';
97
import { store } from './store'
108
import { initializeStorage } from './store/storage'
119
import * as Prism from 'prismjs';
12-
import { checkLogin, login, refreshHistoryList } from './tree/index';
13-
import { HackMDTreeViewProvider } from './tree/index'
10+
import { registerCommand } from './commands'
1411

1512
require('prismjs/components/prism-wiki');
1613
require('prismjs/components/prism-haskell');
@@ -228,83 +225,11 @@ function highlightRender(code, lang) {
228225
let highlight;
229226
// this method is called when your extension is activated
230227
// your extension is activated the very first time the command is executed
231-
const API = new apiClient.default();
232228
axios.defaults.withCredentials = true;
233229

234230
export async function activate(context: vscode.ExtensionContext) {
235231
initializeStorage(context);
236-
context.subscriptions.push(vscode.commands.registerCommand('HackMD.login', async () => {
237-
if (await checkLogin()) {
238-
vscode.window.showInformationMessage('Already logged in, please log out first.');
239-
return;
240-
}
241-
242-
const email = await vscode.window.showInputBox({
243-
ignoreFocusOut: true,
244-
password: false,
245-
placeHolder: 'email',
246-
prompt: 'Please enter your email account',
247-
validateInput: (text) => {
248-
if (text && text !== "") {
249-
return undefined;
250-
} else {
251-
return 'Email cannot be empty';
252-
}
253-
}
254-
});
255-
const password = await vscode.window.showInputBox({
256-
ignoreFocusOut: true,
257-
password: true,
258-
placeHolder: 'password',
259-
prompt: 'Please enter your password',
260-
validateInput: (text) => {
261-
if (text && text !== "") {
262-
return undefined;
263-
} else {
264-
return 'password cannot be empty';
265-
}
266-
}
267-
});
268-
269-
context.globalState.update('email', email);
270-
context.globalState.update('password', password);
271-
272-
await login(context);
273-
await refreshHistoryList(context);
274-
}));
275-
276-
context.subscriptions.push(vscode.commands.registerCommand('HackMD.logout', async () => {
277-
if (!(await checkLogin())) {
278-
vscode.window.showInformationMessage('Currently not logged in.');
279-
return;
280-
}
281-
await API.logout();
282-
store.isLogin = false;
283-
context.globalState.update('isLogin', false);
284-
vscode.window.showInformationMessage('Successfully logged out.');
285-
await refreshHistoryList(context);
286-
}));
287-
288-
const hackMDTreeViewProvider = new HackMDTreeViewProvider(store);
289-
context.subscriptions.push(
290-
vscode.window.registerTreeDataProvider('mdTreeItems', hackMDTreeViewProvider)
291-
);
292-
context.subscriptions.push(
293-
vscode.commands.registerCommand('treeView.refreshList', () => hackMDTreeViewProvider.refresh())
294-
);
295-
296-
context.subscriptions.push(vscode.workspace.registerTextDocumentContentProvider('hackmd', new MdTextDocumentContentProvider()));
297-
298-
context.subscriptions.push(vscode.commands.registerCommand('clickTreeItem', async (label, noteId) => {
299-
if (label && noteId) {
300-
const content = await API.exportString(noteId, apiClient.ExportType.MD);
301-
if (content) {
302-
const uri = vscode.Uri.parse(`hackmd:${label}.md#${noteId}`);
303-
const doc = await vscode.workspace.openTextDocument(uri);
304-
await vscode.window.showTextDocument(doc, { preview: false });
305-
}
306-
}
307-
}));
232+
registerCommand(context, store);
308233

309234
return {
310235
extendMarkdownIt(md: any) {

src/store/storage.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@ import * as vscode from 'vscode';
44

55
export async function initializeStorage(context: vscode.ExtensionContext) {
66
store.history = context.globalState.get('history');
7+
store.isLogin = context.globalState.get('isLogin');
78
reaction(
89
() => [store.history],
910
() => {
1011
context.globalState.update('history', store.history);
1112
vscode.commands.executeCommand("setContext", 'history', store.history);
13+
14+
context.globalState.update('isLogin', store.isLogin);
15+
vscode.commands.executeCommand("setContext", 'isLogin', store.isLogin);
1216
}
1317
);
1418
}

0 commit comments

Comments
 (0)