@@ -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