@@ -7,7 +7,8 @@ import { MdTextDocumentContentProvider } from './mdTextDocument';
77import * as vscode from 'vscode' ;
88import * as markdownitContainer from 'markdown-it-container' ;
99import * as S from 'string' ;
10-
10+ import { store } from './store'
11+ import { initializeStorage } from './store/storage'
1112import * as Prism from 'prismjs' ;
1213
1314require ( 'prismjs/components/prism-wiki' ) ;
@@ -229,21 +230,32 @@ let highlight;
229230const API = new apiClient . default ( ) ;
230231axios . 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+
232244const checkLogin = async ( ) => {
233- return await API . isLogin ( ) ;
234- }
245+ return ( await API . getMe ( ) ) . status === 'ok' ;
246+ } ;
235247
236248const 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
256268export 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 ( ) ) ) ;
0 commit comments