Skip to content

Commit 367be61

Browse files
committed
Refactor, add tree folder
1 parent 38ce868 commit 367be61

4 files changed

Lines changed: 118 additions & 114 deletions

File tree

src/extension.ts

Lines changed: 6 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22
// Import the module and reference it with the alias vscode in your code below
33
import axios from 'axios';
44
import * as apiClient from '@hackmd/api';
5-
import { MdTreeItemProvider } from './mdTreeView';
65
import { MdTextDocumentContentProvider } from './mdTextDocument';
76
import * as vscode from 'vscode';
87
import * as markdownitContainer from 'markdown-it-container';
98
import * as S from 'string';
109
import { store } from './store'
1110
import { initializeStorage } from './store/storage'
1211
import * as Prism from 'prismjs';
12+
import { checkLogin, login, refreshHistoryList } from './tree/index';
13+
import { HackMDTreeViewProvider } from './tree/index'
1314

1415
require('prismjs/components/prism-wiki');
1516
require('prismjs/components/prism-haskell');
@@ -230,43 +231,6 @@ let highlight;
230231
const API = new apiClient.default();
231232
axios.defaults.withCredentials = true;
232233

233-
234-
const refreshHistoryList = async (context) => {
235-
if (await checkLogin()) {
236-
store.history = (await API.getHistory()).history;
237-
context.globalState.update('history', store.history);
238-
} else {
239-
store.history = [{}];
240-
context.globalState.update('history', [{}]);
241-
}
242-
};
243-
244-
const checkLogin = async () => {
245-
return (await API.getMe()).status === 'ok';
246-
};
247-
248-
const login = async (context: vscode.ExtensionContext) => {
249-
const { email, password } = getLoginCredential(context);
250-
if (!email || !password) {
251-
vscode.window.showInformationMessage('Please enter your email and password to use HackMD extension!')
252-
return;
253-
}
254-
await API.login(email, password);
255-
if (await checkLogin()) {
256-
store.isLogin = true;
257-
context.globalState.update('isLogin', true);
258-
vscode.window.showInformationMessage('Successfully login!');
259-
} else {
260-
vscode.window.showInformationMessage('Wrong email or password, please enter again');
261-
}
262-
};
263-
264-
const getLoginCredential = (context: vscode.ExtensionContext) => {
265-
const email: string = context.globalState.get('email');
266-
const password: string = context.globalState.get('password');
267-
return { email, password };
268-
};
269-
270234
export async function activate(context: vscode.ExtensionContext) {
271235
initializeStorage(context);
272236
context.subscriptions.push(vscode.commands.registerCommand('HackMD.login', async () => {
@@ -304,7 +268,7 @@ export async function activate(context: vscode.ExtensionContext) {
304268

305269
context.globalState.update('email', email);
306270
context.globalState.update('password', password);
307-
271+
308272
await login(context);
309273
await refreshHistoryList(context);
310274
}));
@@ -321,12 +285,12 @@ export async function activate(context: vscode.ExtensionContext) {
321285
await refreshHistoryList(context);
322286
}));
323287

324-
const treeViewProvider = new MdTreeItemProvider(store);
288+
const hackMDTreeViewProvider = new HackMDTreeViewProvider(store);
325289
context.subscriptions.push(
326-
vscode.window.registerTreeDataProvider('mdTreeItems', treeViewProvider)
290+
vscode.window.registerTreeDataProvider('mdTreeItems', hackMDTreeViewProvider)
327291
);
328292
context.subscriptions.push(
329-
vscode.commands.registerCommand('treeView.refreshList', () => treeViewProvider.refresh())
293+
vscode.commands.registerCommand('treeView.refreshList', () => hackMDTreeViewProvider.refresh())
330294
);
331295

332296
context.subscriptions.push(vscode.workspace.registerTextDocumentContentProvider('hackmd', new MdTextDocumentContentProvider()));

src/mdTreeView.ts

Lines changed: 0 additions & 72 deletions
This file was deleted.

src/tree/index.ts

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import * as vscode from 'vscode';
2+
import { Store, store } from '../store';
3+
import { reaction } from 'mobx';
4+
import { TreeNode, NoteTreeNode } from './nodes';
5+
import * as apiClient from '@hackmd/api';
6+
const API = new apiClient.default();
7+
8+
export class HackMDTreeViewProvider implements vscode.TreeDataProvider<TreeNode> {
9+
private _onDidChangeTreeData = new vscode.EventEmitter<TreeNode>();
10+
public readonly onDidChangeTreeData: vscode.Event<TreeNode> = this._onDidChangeTreeData.event;
11+
constructor(private store: Store) {
12+
reaction(
13+
() => [
14+
store.history
15+
],
16+
() => {
17+
this.refresh();
18+
}
19+
);
20+
}
21+
22+
getTreeItem(element: TreeNode): vscode.TreeItem {
23+
return element;
24+
}
25+
26+
getChildren(element?: TreeNode): vscode.ProviderResult<TreeNode[]> {
27+
if (store.isLogin) {
28+
if (element === undefined) {
29+
return [new TreeNode("history", vscode.TreeItemCollapsibleState.Collapsed)]
30+
} else {
31+
return this.store.history.map(item =>
32+
new NoteTreeNode(item.id, item.text, vscode.TreeItemCollapsibleState.None)
33+
);
34+
}
35+
}
36+
}
37+
38+
refresh() {
39+
this._onDidChangeTreeData.fire();
40+
}
41+
}
42+
43+
export const refreshHistoryList = async (context) => {
44+
if (await checkLogin()) {
45+
store.history = (await API.getHistory()).history;
46+
context.globalState.update('history', store.history);
47+
} else {
48+
store.history = [{}];
49+
context.globalState.update('history', [{}]);
50+
}
51+
};
52+
53+
export const checkLogin = async () => {
54+
return (await API.getMe()).status === 'ok';
55+
};
56+
57+
export const login = async (context: vscode.ExtensionContext) => {
58+
const { email, password } = getLoginCredential(context);
59+
if (!email || !password) {
60+
vscode.window.showInformationMessage('Please enter your email and password to use HackMD extension!')
61+
return;
62+
}
63+
await API.login(email, password);
64+
if (await checkLogin()) {
65+
store.isLogin = true;
66+
context.globalState.update('isLogin', true);
67+
vscode.window.showInformationMessage('Successfully login!');
68+
} else {
69+
vscode.window.showInformationMessage('Wrong email or password, please enter again');
70+
}
71+
};
72+
73+
export const getLoginCredential = (context: vscode.ExtensionContext) => {
74+
const email: string = context.globalState.get('email');
75+
const password: string = context.globalState.get('password');
76+
return { email, password };
77+
};

src/tree/nodes.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import * as vscode from 'vscode';
2+
3+
export class TreeNode extends vscode.TreeItem {
4+
constructor(
5+
public readonly label: string,
6+
public readonly collapsibaleState: vscode.TreeItemCollapsibleState,
7+
) {
8+
super(label, collapsibaleState);
9+
this.command = {
10+
title: '',
11+
command: 'clickTreeItem',
12+
arguments: [
13+
this.label,
14+
]
15+
};
16+
}
17+
}
18+
19+
export class NoteTreeNode extends TreeNode {
20+
constructor(
21+
public readonly noteId: string,
22+
public readonly label: string,
23+
public readonly collapsibaleState: vscode.TreeItemCollapsibleState,
24+
) {
25+
super(label, collapsibaleState);
26+
this.command = {
27+
title: '',
28+
command: 'clickTreeItem',
29+
arguments: [
30+
this.label,
31+
this.noteId
32+
]
33+
};
34+
}
35+
}

0 commit comments

Comments
 (0)