Skip to content

Commit df55734

Browse files
committed
feat: react vscode tree element
1 parent 4f2fea8 commit df55734

9 files changed

Lines changed: 174 additions & 51 deletions

File tree

package-lock.json

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

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
"mdTree": [
7474
{
7575
"id": "mdTreeItems",
76-
"name": "History"
76+
"name": "Browser Notes"
7777
}
7878
]
7979
},
@@ -246,6 +246,8 @@
246246
"mathjax-full": "^3.0.1",
247247
"mobx": "^5.15.4",
248248
"node-fetch": "^2.6.7",
249+
"react": "^17.0.2",
250+
"react-vsc-treeview": "^0.2.3",
249251
"string": "git+ssh://git@github.com/hackmdio/string.js.git#a68176b3d"
250252
}
251253
}

src/api.ts

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,40 @@
11
import * as vscode from 'vscode';
22

3-
import APIClient, { ExportType } from '@hackmd/api';
4-
const config = {
5-
serverUrl: vscode.workspace.getConfiguration('Hackmd').get('serverURL') as string,
6-
enterprise: vscode.workspace.getConfiguration('Hackmd').get('enterprise') as boolean,
7-
};
8-
const API = new APIClient(config);
3+
import ApiClient from '@hackmd/api';
4+
5+
import { ACCESS_TOKEN_KEY } from './constants';
6+
7+
let API: ApiClient;
8+
9+
export async function initializeAPIClient(context: vscode.ExtensionContext) {
10+
let accessToken = await context.secrets.get(ACCESS_TOKEN_KEY);
11+
const apiEndPoint = vscode.workspace.getConfiguration('Hackmd').get('apiEndPoint') as string;
12+
13+
if (!accessToken) {
14+
const input = await vscode.window.showInputBox({
15+
prompt: 'Please input your HackMD access token',
16+
password: true,
17+
ignoreFocusOut: true,
18+
placeHolder: 'Access Token',
19+
title: 'HackMD Access Token',
20+
});
21+
22+
if (!input) {
23+
return;
24+
}
25+
26+
await context.secrets.store(ACCESS_TOKEN_KEY, input);
27+
accessToken = input;
28+
}
29+
30+
API = new ApiClient(accessToken, apiEndPoint);
31+
}
32+
933
vscode.workspace.onDidChangeConfiguration(async (e) => {
1034
if (e.affectsConfiguration('Hackmd')) {
11-
const clicked = await vscode.window.showInformationMessage(
12-
'Setting updated. Restart to apply this change.',
13-
...['Restart']
14-
);
15-
if (clicked === 'Restart') {
16-
vscode.commands.executeCommand('workbench.action.reloadWindow');
17-
}
35+
const extension = vscode.extensions.getExtension<{ context: vscode.ExtensionContext }>('hackmd.hackmd-vscode');
36+
await initializeAPIClient(extension.exports.context);
1837
}
1938
});
2039

21-
export { API, ExportType };
40+
export { API };

src/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const ACCESS_TOKEN_KEY = 'hackmd_access_token';

src/extension.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@
22
// Import the module and reference it with the alias vscode in your code below
33
import * as vscode from 'vscode';
44

5-
import axios from 'axios';
65
import * as hljs from 'highlight.js/lib/highlight';
76
import * as markdownitContainer from 'markdown-it-container';
87
import * as Prism from 'prismjs';
8+
import React from 'react';
9+
import ReactTreeView from 'react-vsc-treeview';
910
import * as S from 'string';
1011

11-
import { registerCommands } from './commands/index';
12-
import { initializeStorage } from './store/storage';
12+
// import { registerCommands } from './commands/index';
13+
// import { initializeStorage } from './store/storage';
14+
import { initializeAPIClient } from './api';
15+
import { TreeApp } from './tree/TreeApp';
1316

1417
require('prismjs/components/prism-wiki');
1518
require('prismjs/components/prism-haskell');
@@ -203,13 +206,14 @@ function highlightRender(code, lang) {
203206
}
204207

205208
let highlight;
206-
// this method is called when your extension is activated
207-
// your extension is activated the very first time the command is executed
208-
axios.defaults.withCredentials = true;
209209

210210
export async function activate(context: vscode.ExtensionContext) {
211-
initializeStorage();
212-
registerCommands(context);
211+
await initializeAPIClient(context);
212+
213+
// initializeStorage();
214+
// registerCommands(context);
215+
216+
ReactTreeView.render(React.createElement(TreeApp), 'mdTreeItems');
213217

214218
return {
215219
extendMarkdownIt(md: any) {
@@ -263,6 +267,7 @@ export async function activate(context: vscode.ExtensionContext) {
263267

264268
return md;
265269
},
270+
context,
266271
};
267272
}
268273

src/store/storage.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ import { API } from './../api';
55
import { store } from '.';
66

77
export async function initializeStorage() {
8-
store.history = store.history = (await API.getHistory()).history.reverse();
9-
store.isLogin = await checkLogin();
8+
// store.history = store.history = (await API.getHistory()).history.reverse();
9+
// store.isLogin = await checkLogin();
1010
}

src/tree/TreeApp.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { TreeItem } from 'react-vsc-treeview';
2+
3+
export const TreeApp = () => {
4+
return <TreeItem label="Hello World"></TreeItem>;
5+
};

tsconfig.json

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
11
{
2-
"compilerOptions": {
3-
"module": "commonjs",
4-
"target": "es6",
5-
"outDir": "out",
6-
"lib": ["es6", "dom"],
7-
"sourceMap": true,
8-
"rootDir": "src",
9-
"strict": false /* enable all strict type-checking options */
10-
/* Additional Checks */
11-
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
12-
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
13-
// "noUnusedParameters": true, /* Report errors on unused parameters. */
14-
},
15-
"exclude": ["node_modules", ".vscode-test"]
16-
}
2+
"compilerOptions": {
3+
"module": "commonjs",
4+
"target": "es6",
5+
"outDir": "out",
6+
"lib": [
7+
"es6",
8+
"dom"
9+
],
10+
"sourceMap": true,
11+
"rootDir": "src",
12+
"jsx": "preserve",
13+
"strict": false /* enable all strict type-checking options */
14+
/* Additional Checks */
15+
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
16+
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
17+
// "noUnusedParameters": true, /* Report errors on unused parameters. */
18+
},
19+
"exclude": [
20+
"node_modules",
21+
".vscode-test"
22+
]
23+
}

webpack.config.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ const extensionConfig = {
2323
vscode: 'commonjs vscode',
2424
},
2525
resolve: {
26-
extensions: ['.ts', '.js'],
26+
extensions: ['.ts', '.js', '.tsx'],
2727
},
2828
module: {
2929
rules: [
3030
{
31-
test: /\.ts$/,
31+
test: /\.tsx?$/,
3232
exclude: /node_modules/,
3333
use: [
3434
{
@@ -38,6 +38,11 @@ const extensionConfig = {
3838
},
3939
],
4040
},
41+
plugins: [
42+
new webpack.ProvidePlugin({
43+
React: 'react',
44+
}),
45+
],
4146
};
4247

4348
/**@type {import('webpack').Configuration}*/

0 commit comments

Comments
 (0)