Skip to content

Commit 4be0142

Browse files
committed
Fix showing preview with command palette
1 parent 3cc66cf commit 4be0142

File tree

2 files changed

+82
-24
lines changed

2 files changed

+82
-24
lines changed

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,15 +99,15 @@
9999
}
100100
},
101101
{
102-
"command": "note.showPreview",
102+
"command": "HackMD.showPreview",
103103
"title": "HackMD: Show Preview",
104104
"icon": {
105105
"light": "src/icon/light/view-dark.svg",
106106
"dark": "src/icon/dark/view-light.svg"
107107
}
108108
},
109109
{
110-
"command": "note.showPreviewAndEditor",
110+
"command": "HackMD.showPreviewAndEditor",
111111
"title": "HackMD: Show Preview To Side",
112112
"icon": {
113113
"light": "src/icon/light/column-dark.svg",
@@ -133,12 +133,12 @@
133133
],
134134
"view/item/context": [
135135
{
136-
"command": "note.showPreview",
136+
"command": "HackMD.showPreview",
137137
"when": "view == mdTreeItems && viewItem == file",
138138
"group": "inline"
139139
},
140140
{
141-
"command": "note.showPreviewAndEditor",
141+
"command": "HackMD.showPreviewAndEditor",
142142
"when": "view == mdTreeItems && viewItem == file",
143143
"group": "inline"
144144
}

src/commands/treeView.ts

Lines changed: 78 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,35 +17,93 @@ export async function registerTreeViewCommands(context: vscode.ExtensionContext)
1717
context.subscriptions.push(vscode.commands.registerCommand('clickTreeItem', async (label, noteId) => {
1818
if (label && noteId) {
1919
const content = await API.exportString(noteId, ExportType.MD);
20-
if (content) {
21-
const uri = vscode.Uri.parse(`hackmd:${label}.md#${noteId}`);
22-
const doc = await vscode.workspace.openTextDocument(uri);
23-
await vscode.window.showTextDocument(doc, { preview: false });
24-
}
20+
if (!checkNoteExist(content)) { return; }
21+
22+
const uri = vscode.Uri.parse(`hackmd:${label}.md#${noteId}`);
23+
const doc = await vscode.workspace.openTextDocument(uri);
24+
await vscode.window.showTextDocument(doc, { preview: false });
25+
2526
}
2627
}));
2728

28-
context.subscriptions.push(vscode.commands.registerCommand('note.showPreview', async (noteNode: NoteTreeNode) => {
29-
if (noteNode.label && noteNode.noteId) {
29+
context.subscriptions.push(vscode.commands.registerCommand('HackMD.showPreview', async (noteNode: NoteTreeNode) => {
30+
if (noteNode) {
3031
const content = await API.exportString(noteNode.noteId, ExportType.MD);
31-
if (content) {
32-
const uri = vscode.Uri.parse(`hackmd:${noteNode.label}.md#${noteNode.noteId}`);
33-
vscode.commands.executeCommand('markdown.showPreview', uri);
34-
}
32+
if (!checkNoteExist(content)) { return; }
33+
34+
const uri = vscode.Uri.parse(`hackmd:${noteNode.label}.md#${noteNode.noteId}`);
35+
vscode.commands.executeCommand('markdown.showPreview', uri);
36+
} else {
37+
const editor = vscode.window.activeTextEditor;
38+
if (!checkEditorExist(editor)) { return; }
39+
40+
const noteId = editor.document.uri.fragment;
41+
if (!checkNoteIdExist(noteId)) { return; }
42+
43+
const content = await API.exportString(noteId, ExportType.MD);
44+
if (!checkNoteExist(content)) { return; }
45+
46+
const fileName = editor.document.fileName.split('.')[0];
47+
const uri = vscode.Uri.parse(`hackmd:${fileName}.md#${noteId}`);
48+
vscode.commands.executeCommand('markdown.showPreview', uri);
3549
}
3650
}));
3751

38-
context.subscriptions.push(vscode.commands.registerCommand('note.showPreviewAndEditor', async (noteNode: NoteTreeNode) => {
39-
if (noteNode.label && noteNode.noteId) {
52+
context.subscriptions.push(vscode.commands.registerCommand('HackMD.showPreviewAndEditor', async (noteNode: NoteTreeNode) => {
53+
if (noteNode) {
4054
const content = await API.exportString(noteNode.noteId, ExportType.MD);
41-
if (content) {
42-
const uri = vscode.Uri.parse(`hackmd:${noteNode.label}.md#${noteNode.noteId}`);
43-
const doc = await vscode.workspace.openTextDocument(uri);
44-
await vscode.window.showTextDocument(doc, { preview: false });
45-
vscode.commands.executeCommand('markdown.showPreviewToSide', uri);
46-
}
55+
if (!checkNoteExist(content)) { return; }
56+
57+
const uri = vscode.Uri.parse(`hackmd:${noteNode.label}.md#${noteNode.noteId}`);
58+
const doc = await vscode.workspace.openTextDocument(uri);
59+
await vscode.window.showTextDocument(doc, { preview: false });
60+
vscode.commands.executeCommand('markdown.showPreviewToSide', uri);
61+
62+
} else {
63+
const editor = vscode.window.activeTextEditor;
64+
if (!checkEditorExist(editor)) { return; }
65+
66+
const noteId = editor.document.uri.fragment;
67+
if (!checkNoteIdExist(noteId)) { return; }
68+
69+
const content = await API.exportString(noteId, ExportType.MD);
70+
if (!checkNoteExist(content)) { return; }
71+
72+
const fileName = editor.document.fileName.split('.')[0];
73+
const uri = vscode.Uri.parse(`hackmd:${fileName}.md#${noteId}`);
74+
const doc = await vscode.workspace.openTextDocument(uri);
75+
await vscode.window.showTextDocument(doc, { preview: false });
76+
vscode.commands.executeCommand('markdown.showPreviewToSide', uri);
4777
}
4878
}));
4979

5080
context.subscriptions.push(vscode.workspace.registerTextDocumentContentProvider('hackmd', new MdTextDocumentContentProvider()));
51-
}
81+
}
82+
83+
const checkEditorExist = (editor) => {
84+
if (editor) {
85+
return true;
86+
} else {
87+
vscode.window.showInformationMessage('Current window is not a text editor. Please open one first.');
88+
return false;
89+
}
90+
};
91+
92+
const checkNoteIdExist = (noteId) => {
93+
if (noteId) {
94+
return true;
95+
} else {
96+
vscode.window.showInformationMessage("Please open a note first");
97+
return false;
98+
}
99+
};
100+
101+
const checkNoteExist = (content) => {
102+
if (content) {
103+
return true;
104+
} else {
105+
vscode.window.showInformationMessage("Can't find the note from HackMD. Make sure it's still exist.");
106+
return false;
107+
}
108+
};
109+

0 commit comments

Comments
 (0)