Skip to content

Commit c71ea19

Browse files
committed
feat: add tree item icon
1 parent b8ba179 commit c71ea19

File tree

9 files changed

+86
-6
lines changed

9 files changed

+86
-6
lines changed

images/icon/dark/file-text.svg

Lines changed: 2 additions & 0 deletions
Loading

images/icon/dark/users.svg

Lines changed: 2 additions & 0 deletions
Loading

images/icon/light/file-text.svg

Lines changed: 2 additions & 0 deletions
Loading

images/icon/light/users.svg

Lines changed: 2 additions & 0 deletions
Loading

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
"hackmd-tree": [
7474
{
7575
"id": "hackmd.tree.my-notes",
76-
"name": "Browse My Notes"
76+
"name": "My Notes"
7777
},
7878
{
7979
"id": "hackmd.tree.recent-notes",

src/extension.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ import * as vscode from 'vscode';
55
import * as hljs from 'highlight.js/lib/highlight';
66
import * as markdownitContainer from 'markdown-it-container';
77
import * as Prism from 'prismjs';
8-
import React from 'react';
98
import ReactTreeView from 'react-vsc-treeview';
109
import * as S from 'string';
1110

1211
import { initializeAPIClient } from './api';
1312
import { registerCommands } from './commands';
13+
import { createWithContainer } from './treeReactApp/AppContainer';
1414
import { History, MyNotes, TeamNotes } from './treeReactApp/pages';
1515

1616
require('prismjs/components/prism-wiki');
@@ -211,9 +211,23 @@ export async function activate(context: vscode.ExtensionContext) {
211211

212212
registerCommands(context);
213213

214-
context.subscriptions.push(ReactTreeView.render(React.createElement(MyNotes), 'hackmd.tree.my-notes'));
215-
context.subscriptions.push(ReactTreeView.render(React.createElement(History), 'hackmd.tree.recent-notes'));
216-
context.subscriptions.push(ReactTreeView.render(React.createElement(TeamNotes), 'hackmd.tree.team-notes'));
214+
console.log(context.extensionPath, 'context.extensionPath');
215+
216+
context.subscriptions.push(
217+
ReactTreeView.render(createWithContainer(MyNotes, { extensionPath: context.extensionPath }), 'hackmd.tree.my-notes')
218+
);
219+
context.subscriptions.push(
220+
ReactTreeView.render(
221+
createWithContainer(History, { extensionPath: context.extensionPath }),
222+
'hackmd.tree.recent-notes'
223+
)
224+
);
225+
context.subscriptions.push(
226+
ReactTreeView.render(
227+
createWithContainer(TeamNotes, { extensionPath: context.extensionPath }),
228+
'hackmd.tree.team-notes'
229+
)
230+
);
217231

218232
return {
219233
extendMarkdownIt(md: any) {

src/treeReactApp/AppContainer.tsx

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { createContext, useContext } from 'react';
2+
3+
type AppContextType = {
4+
extensionPath: string;
5+
};
6+
7+
const AppContext = createContext<AppContextType>({
8+
extensionPath: '',
9+
});
10+
11+
export const useAppContext = () => {
12+
return useContext(AppContext);
13+
};
14+
15+
export const AppContainer = ({ extensionPath, children }: { children: React.ReactNode; extensionPath }) => {
16+
return <AppContext.Provider value={{ extensionPath }}>{children}</AppContext.Provider>;
17+
};
18+
19+
export function createWithContainer(Component: React.ComponentType, context: AppContextType) {
20+
return (
21+
<AppContainer {...context}>
22+
<Component />
23+
</AppContainer>
24+
);
25+
}

src/treeReactApp/components/NoteTreeItem.tsx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1+
import path from 'path';
2+
13
import { Note } from '@hackmd/api/dist/type';
24
import { useMemo } from 'react';
35
import { TreeItem } from 'react-vsc-treeview';
46

7+
import { useAppContext } from '../AppContainer';
8+
59
export const NoteTreeItem = ({ note }: { note: Note }) => {
610
const context = useMemo(
711
() => ({
@@ -11,15 +15,29 @@ export const NoteTreeItem = ({ note }: { note: Note }) => {
1115
[note]
1216
);
1317

18+
const { extensionPath } = useAppContext();
19+
const iconPath = useMemo(() => {
20+
if (extensionPath) {
21+
return {
22+
light: path.join(extensionPath, 'images/icon/light/file-text.svg'),
23+
dark: path.join(extensionPath, 'images/icon/dark/file-text.svg'),
24+
};
25+
} else {
26+
return undefined;
27+
}
28+
}, [extensionPath]);
29+
1430
return (
1531
<TreeItem
1632
label={note.title}
1733
id={note.id}
34+
iconPath={iconPath}
1835
command={{
1936
title: '',
2037
command: 'clickTreeItem',
2138
arguments: [note.title, note.id],
2239
}}
40+
description={note.tags?.join(', ') || ''}
2341
contextValue="file"
2442
context={context}
2543
/>

src/treeReactApp/pages/TeamNotes.tsx

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
import path from 'path';
2+
13
import { Team } from '@hackmd/api/dist/type';
24
import { useEffect, useMemo, useState } from 'react';
35
import { TreeItem } from 'react-vsc-treeview';
46
import useSWR from 'swr';
57

68
import { API } from '../../api';
9+
import { useAppContext } from '../AppContainer';
710
import { NoteTreeItem } from '../components/NoteTreeItem';
811
import { useTeamNotesStore } from '../store';
912

@@ -13,8 +16,20 @@ const TeamTreeItem = ({ team }: { team: Team }) => {
1316
() => API.getTeamNotes(team.path)
1417
);
1518

19+
const { extensionPath } = useAppContext();
20+
const iconPath = useMemo(() => {
21+
if (extensionPath) {
22+
return {
23+
light: path.join(extensionPath, 'images/icon/light/users.svg'),
24+
dark: path.join(extensionPath, 'images/icon/dark/users.svg'),
25+
};
26+
} else {
27+
return undefined;
28+
}
29+
}, [extensionPath]);
30+
1631
return (
17-
<TreeItem label={team.name} expanded>
32+
<TreeItem label={team.name} expanded iconPath={iconPath} description={team.path}>
1833
{notes.map((note) => {
1934
return <NoteTreeItem key={note.id} note={note} />;
2035
})}

0 commit comments

Comments
 (0)