forked from jasonwilliams/anki
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommands.ts
104 lines (94 loc) · 3.02 KB
/
commands.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import { IContext } from "./extension";
import { Uri, commands, ProgressLocation, window, workspace } from "vscode";
import { Transformer } from "./markdown/transformer";
import { CONSTANTS } from "./constants";
import { getLogger } from "./logger";
import { initialSetup } from "./initialSetup";
import { MarkdownFile } from './models/MarkdownFile';
export const registerCommands = (ctx: IContext) => {
// Handle Syncing the Anki Instance
let disposableSync = commands.registerCommand("anki.sync", async () => {
// The code you place here will be executed every time your command is executed
window.withProgress(
{
location: ProgressLocation.Notification,
title: "Syncing your Anki Instance...",
cancellable: false,
},
async () => {
try {
await ctx.ankiService.syncGui();
} catch (e) {
window.showErrorMessage(CONSTANTS.failedToConnectMessage);
}
}
);
});
// Handle Syncing the Anki Instance
let disposableSendToDeck = commands.registerCommand(
"anki.sendToDeck",
async () => {
// The code you place here will be executed every time your command is executed
window.withProgress(
{
location: ProgressLocation.Notification,
title: `Sending to Deck: ${ctx.config.defaultDeck}...`,
cancellable: false,
},
async () => {
try {
getLogger().info("active Editor..");
await new Transformer(MarkdownFile.fromActiveTextEditor(), ctx.ankiService, true).transform();
} catch (e) {
window.showErrorMessage(e.toString());
}
}
);
}
);
// Handle Syncing the Anki Instance
let disposableSendToStandalone = commands.registerCommand(
"anki.sendToStandalone",
async () => {
// The code you place here will be executed every time your command is executed
window.withProgress(
{
location: ProgressLocation.Notification,
title: `Sending to own deck...`,
cancellable: false,
},
async () => {
try {
await new Transformer(MarkdownFile.fromActiveTextEditor(), ctx.ankiService, false).transform();
} catch (e) {
getLogger().error(e);
getLogger().error(
"This is usually because there is no H1 or something is before the title heading"
);
window.showErrorMessage(`Deck not sent: ${e.message}`);
}
}
);
}
);
let disposableTreeItemOpen = commands.registerCommand(
"anki.treeItem",
async (uri) => {
const doc = await workspace.openTextDocument(Uri.parse(uri));
window.showTextDocument(doc);
}
);
let disposableForceInstall = commands.registerCommand(
"anki.forceInstall",
async () => {
await initialSetup(ctx);
}
);
ctx.context.subscriptions.push(
disposableSync,
disposableSendToDeck,
disposableSendToStandalone,
disposableTreeItemOpen,
disposableForceInstall
);
};