Skip to content

Commit 4841785

Browse files
committed
Fix APIClient initialization
1 parent ab5a914 commit 4841785

File tree

8 files changed

+38
-15
lines changed

8 files changed

+38
-15
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@
114114
},
115115
{
116116
"command": "HackMD.createActiveEditorContentToHackMD",
117-
"title": "HackMD: Create Active Editor Content To HackMD"
117+
"title": "HackMD: Create note from editor"
118118
}
119119
],
120120
"menus": {

src/api.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import * as apiClient from '@hackmd/api';
2+
export const apiExportType = apiClient.ExportType;
3+
export const API = new apiClient.default();
4+

src/commands/note.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import * as vscode from 'vscode';
2-
import * as apiClient from '@hackmd/api';
32
import { checkLogin } from './../utils';
4-
const API = new apiClient.default();
3+
import { API } from './../api';
54

65
export async function registerNoteCommands(context: vscode.ExtensionContext) {
76
context.subscriptions.push(vscode.commands.registerCommand('HackMD.createActiveEditorContentToHackMD', async () => {

src/commands/snippet.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import * as vscode from 'vscode';
2+
import { API } from './../api';
3+
4+
export function registerSnippetCommands(context: vscode.ExtensionContext) {
5+
context.subscriptions.push(vscode.commands.registerCommand('HackMD.createCodeSnippet', async () => {
6+
const editor = vscode.window.activeTextEditor;
7+
if (editor.selection.isEmpty) {
8+
vscode.window.showInformationMessage('The code block is empty. Please select a range of text first.');
9+
return;
10+
}
11+
12+
const textRange = new vscode.Range(editor.selection.start.line, editor.selection.start.character, editor.selection.end.line, editor.selection.end.character);
13+
const text = vscode.window.activeTextEditor.document.getText(textRange);
14+
const snippet = `> \`${vscode.workspace.asRelativePath(editor.document.uri.fsPath)}\`\n\n\`\`\`${editor.document.languageId}=${editor.selection.start.line + 1}\n${text}\n\`\`\``;
15+
16+
const noteUrl = await API.newNote(snippet);
17+
const clicked = await vscode.window.showInformationMessage('New Snippet Established!', ...['Copy URL to clip board', 'Open in browser']);
18+
if (clicked === 'Copy URL to clip board') {
19+
vscode.env.clipboard.writeText(noteUrl);
20+
} else if (clicked === 'Open in browser') {
21+
vscode.env.openExternal(vscode.Uri.parse(noteUrl));
22+
}
23+
}));
24+
}

src/commands/treeView.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import * as vscode from 'vscode';
22
import { Store } from '../store';
3-
import * as apiClient from '@hackmd/api';
43
import { HackMDTreeViewProvider } from './../tree/index';
54
import { NoteTreeNode } from './../tree/nodes';
65
import { MdTextDocumentContentProvider } from './../mdTextDocument';
76
import { refreshHistoryList } from './../utils';
8-
const API = new apiClient.default();
7+
import { API, apiExportType } from './../api';
98

109
export async function registerTreeViewCommands(context: vscode.ExtensionContext, store: Store) {
1110
const hackMDTreeViewProvider = new HackMDTreeViewProvider(store);
@@ -14,7 +13,7 @@ export async function registerTreeViewCommands(context: vscode.ExtensionContext,
1413

1514
context.subscriptions.push(vscode.commands.registerCommand('clickTreeItem', async (label, noteId) => {
1615
if (label && noteId) {
17-
const content = await API.exportString(noteId, apiClient.ExportType.MD);
16+
const content = await API.exportString(noteId, apiExportType.MD);
1817
if (content) {
1918
const uri = vscode.Uri.parse(`hackmd:${label}.md#${noteId}`);
2019
const doc = await vscode.workspace.openTextDocument(uri);
@@ -26,7 +25,7 @@ export async function registerTreeViewCommands(context: vscode.ExtensionContext,
2625
context.subscriptions.push(vscode.commands.registerCommand('note.showPreview', async (node: NoteTreeNode) => {
2726
const noteNode = node;
2827
if (noteNode.label && noteNode.noteId) {
29-
const content = await API.exportString(noteNode.noteId, apiClient.ExportType.MD);
28+
const content = await API.exportString(noteNode.noteId, apiExportType.MD);
3029
if (content) {
3130
const uri = vscode.Uri.parse(`hackmd:${noteNode.label}.md#${noteNode.noteId}`);
3231
vscode.commands.executeCommand('markdown.showPreview', uri);
@@ -37,7 +36,7 @@ export async function registerTreeViewCommands(context: vscode.ExtensionContext,
3736
context.subscriptions.push(vscode.commands.registerCommand('note.showPreviewAndEditor', async (node: NoteTreeNode) => {
3837
const noteNode = node;
3938
if (noteNode.label && noteNode.noteId) {
40-
const content = await API.exportString(noteNode.noteId, apiClient.ExportType.MD);
39+
const content = await API.exportString(noteNode.noteId,apiExportType.MD);
4140
if (content) {
4241
const uri = vscode.Uri.parse(`hackmd:${noteNode.label}.md#${noteNode.noteId}`);
4342
const doc = await vscode.workspace.openTextDocument(uri);

src/commands/user.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import * as vscode from 'vscode';
22
import { checkLogin, login, refreshHistoryList } from './../utils'
33
import { Store } from '../store';
4-
import * as apiClient from '@hackmd/api';
5-
const API = new apiClient.default();
4+
import { API } from './../api';
65

76
export async function registerUserCommands(context: vscode.ExtensionContext, store: Store) {
87
context.subscriptions.push(vscode.commands.registerCommand('HackMD.login', async () => {

src/mdTextDocument.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import * as vscode from 'vscode';
2-
import * as apiClient from '@hackmd/api';
3-
const API = new apiClient.default();
2+
import { API, apiExportType } from './api';
43

54
export class MdTextDocumentContentProvider implements vscode.TextDocumentContentProvider {
65
async provideTextDocumentContent(uri: vscode.Uri): Promise<string> {
76
const noteId = uri.fragment;
8-
const content = await API.exportString(noteId, apiClient.ExportType.MD);
7+
const content = await API.exportString(noteId, apiExportType.MD);
98
return content;
109
}
1110
}

src/utils.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import * as vscode from 'vscode';
22
import { store } from './store';
3-
import * as apiClient from '@hackmd/api';
3+
import { API } from './api';
44

5-
const API = new apiClient.default();
65
export const refreshHistoryList = async () => {
76
if (await checkLogin()) {
87
store.history = (await API.getHistory()).history.reverse();

0 commit comments

Comments
 (0)