Skip to content

Commit 174dd20

Browse files
committed
Add editor and preview mode buttons beside viewItems
1 parent c9c1b32 commit 174dd20

9 files changed

Lines changed: 157 additions & 11 deletions

File tree

package.json

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,16 @@
7777
"command": "treeView.refreshList",
7878
"title": "Refresh",
7979
"icon": "src/icon/refresh.svg"
80+
},
81+
{
82+
"command": "note.showPreview",
83+
"title": "Show preview",
84+
"icon": "src/icon/eye.svg"
85+
},
86+
{
87+
"command": "note.showPreviewAndEditor",
88+
"title": "Show preview to side",
89+
"icon": "src/icon/education.svg"
8090
}
8191
],
8292
"menus": {
@@ -86,14 +96,26 @@
8696
"when": "view == mdTreeItems",
8797
"group": "navigation"
8898
}
99+
],
100+
"view/item/context": [
101+
{
102+
"command": "note.showPreview",
103+
"when": "view == mdTreeItems && viewItem == file",
104+
"group": "inline"
105+
},
106+
{
107+
"command": "note.showPreviewAndEditor",
108+
"when": "view == mdTreeItems && viewItem == file",
109+
"group": "inline"
110+
}
89111
]
90112
},
91113
"viewsWelcome": [
92114
{
93-
"view": "mdTreeItems",
94-
"contents": "No notes found. Login first.\n[Login HackMD](command:HackMD.login)"
115+
"view": "mdTreeItems",
116+
"contents": "No notes found. Login first.\n[Login HackMD](command:HackMD.login)"
95117
}
96-
]
118+
]
97119
},
98120
"scripts": {
99121
"vscode:prepublish": "webpack --mode production",
@@ -156,4 +178,4 @@
156178
"node-fetch": "^2.6.0",
157179
"string": "git+ssh://git@github.com/hackmdio/string.js.git#a68176b3d"
158180
}
159-
}
181+
}

src/commands/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ import { Store } from '../store';
55

66
export function registerCommand(context: vscode.ExtensionContext, store: Store) {
77
registerUserCommands(context, store);
8-
registerTreeViewCommands(context, store)
8+
registerTreeViewCommands(context, store);
99
}

src/commands/treeView.ts

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import * as vscode from 'vscode'
1+
import * as vscode from 'vscode';
22
import { Store } from '../store';
33
import * as apiClient from '@hackmd/api';
4-
import { HackMDTreeViewProvider } from './../tree/index'
4+
import { HackMDTreeViewProvider } from './../tree/index';
5+
import { NoteTreeNode } from './../tree/nodes';
56
import { MdTextDocumentContentProvider } from './../mdTextDocument';
67
const API = new apiClient.default();
78

@@ -20,5 +21,30 @@ export async function registerTreeViewCommands(context: vscode.ExtensionContext,
2021
}
2122
}
2223
}));
24+
25+
context.subscriptions.push(vscode.commands.registerCommand('note.showPreview', async (node: NoteTreeNode) => {
26+
const noteNode = node;
27+
if (noteNode.label && noteNode.noteId) {
28+
const content = await API.exportString(noteNode.noteId, apiClient.ExportType.MD);
29+
if (content) {
30+
const uri = vscode.Uri.parse(`hackmd:${noteNode.label}.md#${noteNode.noteId}`);
31+
vscode.commands.executeCommand('markdown.showPreview', uri);
32+
}
33+
}
34+
}));
35+
36+
context.subscriptions.push(vscode.commands.registerCommand('note.showPreviewAndEditor', async (node: NoteTreeNode) => {
37+
const noteNode = node;
38+
if (noteNode.label && noteNode.noteId) {
39+
const content = await API.exportString(noteNode.noteId, apiClient.ExportType.MD);
40+
if (content) {
41+
const uri = vscode.Uri.parse(`hackmd:${noteNode.label}.md#${noteNode.noteId}`);
42+
const doc = await vscode.workspace.openTextDocument(uri);
43+
await vscode.window.showTextDocument(doc, { preview: false });
44+
vscode.commands.executeCommand('markdown.showPreviewToSide', uri);
45+
}
46+
}
47+
}));
48+
2349
context.subscriptions.push(vscode.workspace.registerTextDocumentContentProvider('hackmd', new MdTextDocumentContentProvider()));
2450
}

src/commands/user.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ export async function registerUserCommands(context: vscode.ExtensionContext, sto
5252
}
5353
await API.logout();
5454
store.isLogin = false;
55-
context.globalState.update('isLogin', false);
5655
vscode.window.showInformationMessage('Successfully logged out.');
5756
await refreshHistoryList(context);
5857
}));

src/icon/education.svg

Lines changed: 49 additions & 0 deletions
Loading

src/icon/eye.svg

Lines changed: 48 additions & 0 deletions
Loading

src/mdTextDocument.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ export class MdTextDocumentContentProvider implements vscode.TextDocumentContent
66
async provideTextDocumentContent(uri: vscode.Uri): Promise<string> {
77
const noteId = uri.fragment;
88
const content = await API.exportString(noteId, apiClient.ExportType.MD);
9+
// vscode.commands.executeCommand('markdown.showSource', content);
10+
// vscode.commands.executeCommand('markdown.showPreviewToSide', content);
11+
12+
// vscode.commands.executeCommand('markdown.showSource', content);
913
return content;
1014
}
1115
}

src/tree/index.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,8 @@ export class HackMDTreeViewProvider implements vscode.TreeDataProvider<TreeNode>
4343
export const refreshHistoryList = async (context) => {
4444
if (await checkLogin()) {
4545
store.history = (await API.getHistory()).history;
46-
context.globalState.update('history', store.history);
4746
} else {
4847
store.history = [{}];
49-
context.globalState.update('history', [{}]);
5048
}
5149
};
5250

@@ -63,7 +61,6 @@ export const login = async (context: vscode.ExtensionContext) => {
6361
await API.login(email, password);
6462
if (await checkLogin()) {
6563
store.isLogin = true;
66-
context.globalState.update('isLogin', true);
6764
vscode.window.showInformationMessage('Successfully login!');
6865
} else {
6966
vscode.window.showInformationMessage('Wrong email or password, please enter again');

src/tree/nodes.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,6 @@ export class NoteTreeNode extends TreeNode {
3131
this.noteId
3232
]
3333
};
34+
this.contextValue = 'file';
3435
}
3536
}

0 commit comments

Comments
 (0)