generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
63 lines (53 loc) · 1.63 KB
/
main.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
import { App, Plugin, PluginSettingTab, Setting } from "obsidian";
interface SwitchVSCodeSettings {
vaultPath: string;
}
export default class SwitchVSCode extends Plugin {
settings: SwitchVSCodeSettings;
async onload() {
this.addSettingTab(new SwitchVSCodeSettingsTab(this.app, this));
await this.loadSettings();
this.addCommand({
id: "Switch-vscode",
name: "Switch current file with Visual Studio Code",
callback: this.SwitchCurrentFile.bind(this),
});
}
// open file in vscode
async SwitchCurrentFile() {
const vaultPath = this.settings.vaultPath;
const currentFilePath = this.app.workspace.getActiveFile().path;
if (vaultPath)
console.error("please config vault path in plugin setting");
currentFilePath
? window.open(`vscode://file/${vaultPath}/${currentFilePath}`)
: console.error("no editor available yet");
}
async loadSettings() {
this.settings = Object.assign({}, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
}
class SwitchVSCodeSettingsTab extends PluginSettingTab {
plugin: SwitchVSCode;
constructor(app: App, plugin: SwitchVSCode) {
super(app, plugin);
this.plugin = plugin;
}
display(): void {
const { containerEl } = this;
containerEl.empty();
containerEl.createEl("h2", { text: "Settings" });
new Setting(containerEl).setName("vault path").addText((text) =>
text
.setValue(this.plugin.settings.vaultPath)
.onChange(async (value) => {
value = value.trim();
this.plugin.settings.vaultPath = value;
await this.plugin.saveSettings();
})
);
}
}