Skip to content

Commit 4f3b883

Browse files
committed
Refresh treeView when login and logout
1 parent 9c3d29e commit 4f3b883

7 files changed

Lines changed: 165 additions & 22 deletions

File tree

package-lock.json

Lines changed: 8 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
},
1515
"activationEvents": [
1616
"onView:mdTreeItems",
17-
"onCommand:extension.login"
17+
"onCommand:HackMD.login",
18+
"onCommand::HackMD.logout"
1819
],
1920
"license": "MIT",
2021
"homepage": "https://github.com/hackmdio/vscode-hackmd/blob/master/README.md",
@@ -50,7 +51,7 @@
5051
"activitybar": [
5152
{
5253
"id": "mdTree",
53-
"title": "HackMD TreeView",
54+
"title": "HackMD Notes",
5455
"icon": "src/icon/notes.svg"
5556
}
5657
]
@@ -59,16 +60,34 @@
5960
"mdTree": [
6061
{
6162
"id": "mdTreeItems",
62-
"name": "HackMD Notes"
63+
"name": ""
6364
}
6465
]
6566
},
6667
"commands": [
6768
{
68-
"command": "extension.login",
69+
"command": "HackMD.login",
6970
"title": "Login"
71+
},
72+
{
73+
"command": "HackMD.logout",
74+
"title": "Logout"
75+
},
76+
{
77+
"command": "treeView.refreshList",
78+
"title": "Refresh!",
79+
"icon": "src/icon/refresh.svg"
7080
}
71-
]
81+
],
82+
"menus": {
83+
"view/title": [
84+
{
85+
"command": "treeView.refreshList",
86+
"when": "view == mdTreeItems",
87+
"group": "navigation"
88+
}
89+
]
90+
}
7291
},
7392
"scripts": {
7493
"vscode:prepublish": "webpack --mode production",
@@ -127,6 +146,7 @@
127146
"markdown-it-table-of-contents": "^0.4.4",
128147
"mathjax": "^3.0.1",
129148
"mathjax-full": "^3.0.1",
149+
"mobx": "^5.15.4",
130150
"node-fetch": "^2.6.0",
131151
"string": "git+ssh://git@github.com/hackmdio/string.js.git#a68176b3d"
132152
}

src/extension.ts

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import { MdTextDocumentContentProvider } from './mdTextDocument';
77
import * as vscode from 'vscode';
88
import * as markdownitContainer from 'markdown-it-container';
99
import * as S from 'string';
10-
10+
import { store } from './store'
11+
import { initializeStorage } from './store/storage'
1112
import * as Prism from 'prismjs';
1213

1314
require('prismjs/components/prism-wiki');
@@ -229,21 +230,32 @@ let highlight;
229230
const API = new apiClient.default();
230231
axios.defaults.withCredentials = true;
231232

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+
232244
const checkLogin = async () => {
233-
return await API.isLogin();
234-
}
245+
return (await API.getMe()).status === 'ok';
246+
};
235247

236248
const login = async (context: vscode.ExtensionContext) => {
237249
const { email, password } = getLoginCredential(context);
238250
if (!email || !password) {
239-
vscode.window.showInformationMessage('Please enter email and password to use HackMD extension!')
251+
vscode.window.showInformationMessage('Please enter your email and password to use HackMD extension!')
240252
return;
241253
}
242254
await API.login(email, password);
243-
if (checkLogin) {
244-
vscode.window.showInformationMessage('Successfully login!')
255+
if (await checkLogin()) {
256+
vscode.window.showInformationMessage('Successfully login!');
245257
} else {
246-
vscode.window.showInformationMessage('Wrong email or password, please enter again')
258+
vscode.window.showInformationMessage('Wrong email or password, please enter again');
247259
}
248260
};
249261

@@ -254,7 +266,13 @@ const getLoginCredential = (context: vscode.ExtensionContext) => {
254266
};
255267

256268
export async function activate(context: vscode.ExtensionContext) {
257-
context.subscriptions.push(vscode.commands.registerCommand('extension.login', async () => {
269+
initializeStorage(context);
270+
context.subscriptions.push(vscode.commands.registerCommand('HackMD.login', async () => {
271+
if (await checkLogin()) {
272+
vscode.window.showInformationMessage('Already logged in, please log out first.');
273+
return;
274+
}
275+
258276
const email = await vscode.window.showInputBox({
259277
ignoreFocusOut: true,
260278
password: false,
@@ -284,13 +302,27 @@ export async function activate(context: vscode.ExtensionContext) {
284302

285303
context.globalState.update('email', email);
286304
context.globalState.update('password', password);
287-
login(context);
305+
306+
await login(context);
307+
await refreshHistoryList(context);
288308
}));
289309

310+
context.subscriptions.push(vscode.commands.registerCommand('HackMD.logout', async () => {
311+
if (!(await checkLogin())) {
312+
vscode.window.showInformationMessage('Currently not logged in.');
313+
return;
314+
}
315+
await API.logout();
316+
vscode.window.showInformationMessage('Successfully logged out.');
317+
await refreshHistoryList(context);
318+
}));
290319

291-
const history = (await API.getHistory()).history;
320+
const treeViewProvider = new MdTreeItemProvider(store);
321+
context.subscriptions.push(
322+
vscode.window.registerTreeDataProvider('mdTreeItems', treeViewProvider)
323+
);
292324
context.subscriptions.push(
293-
vscode.window.registerTreeDataProvider('mdTreeItems', new MdTreeItemProvider(history))
325+
vscode.commands.registerCommand('treeView.refreshList', () => treeViewProvider.refresh())
294326
);
295327

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

src/icon/refresh.svg

Lines changed: 46 additions & 0 deletions
Loading

src/mdTreeView.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,20 @@
11
import * as vscode from 'vscode';
2-
2+
import { Store } from './store';
3+
import { reaction } from 'mobx';
4+
// const history = (await API.getHistory()).history;
35
export class MdTreeItemProvider implements vscode.TreeDataProvider<TreeNode> {
4-
constructor(public history: Array<any>) { }
6+
private _onDidChangeTreeData = new vscode.EventEmitter<TreeNode>();
7+
public readonly onDidChangeTreeData: vscode.Event<TreeNode> = this._onDidChangeTreeData.event;
8+
constructor(private store: Store) {
9+
reaction(
10+
() => [
11+
store.history
12+
],
13+
() => {
14+
this.refresh();
15+
}
16+
);
17+
}
518

619
getTreeItem(element: TreeNode): vscode.TreeItem {
720
return element;
@@ -11,11 +24,15 @@ export class MdTreeItemProvider implements vscode.TreeDataProvider<TreeNode> {
1124
if (element === undefined) {
1225
return [new TreeNode("history", vscode.TreeItemCollapsibleState.Collapsed)]
1326
} else {
14-
return this.history.map(item =>
27+
return this.store.history.map(item =>
1528
new NoteTreeNode(item.id, item.text, vscode.TreeItemCollapsibleState.None)
1629
);
1730
}
1831
}
32+
33+
refresh() {
34+
this._onDidChangeTreeData.fire();
35+
}
1936
}
2037

2138
export class TreeNode extends vscode.TreeItem {

src/store/index.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { observable } from "mobx";
2+
3+
export interface Store {
4+
history: Array<any>
5+
}
6+
7+
export const store: Store = observable({
8+
history: [{}]
9+
});

src/store/storage.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { reaction } from "mobx";
2+
import { store } from '.';
3+
import * as vscode from 'vscode';
4+
5+
export async function initializeStorage(context: vscode.ExtensionContext) {
6+
store.history = context.globalState.get('history');
7+
reaction(
8+
() => [store.history],
9+
() => {
10+
context.globalState.update('history', store.history);
11+
vscode.commands.executeCommand("setContext", 'history', store.history);
12+
}
13+
);
14+
}

0 commit comments

Comments
 (0)