noteshare.space/plugin/main.ts

91 lines
2.2 KiB
TypeScript
Raw Normal View History

2022-06-18 22:51:06 +03:00
import { App, MarkdownView, Plugin, PluginSettingTab, Setting } from "obsidian";
import { encryptMarkdown } from "src/encryption";
2022-06-19 23:49:37 +03:00
import { NoteSharingService } from "src/NoteSharingService";
2022-06-18 17:51:45 +03:00
// Remember to rename these classes and interfaces!
interface MyPluginSettings {
2022-06-19 23:49:37 +03:00
serverUrl: string;
2022-06-18 17:51:45 +03:00
}
const DEFAULT_SETTINGS: MyPluginSettings = {
2022-06-19 23:49:37 +03:00
serverUrl: "http://localhost:8080",
2022-06-18 22:51:06 +03:00
};
2022-06-18 17:51:45 +03:00
export default class MyPlugin extends Plugin {
settings: MyPluginSettings;
2022-06-19 23:49:37 +03:00
noteSharingService: NoteSharingService;
2022-06-18 17:51:45 +03:00
async onload() {
await this.loadSettings();
2022-06-19 23:49:37 +03:00
this.noteSharingService = new NoteSharingService(
this.settings.serverUrl
);
// This adds a settings tab so the user can configure various aspects of the plugin
this.addSettingTab(new SettingsTab(this.app, this));
2022-06-18 17:51:45 +03:00
// This adds a complex command that can check whether the current state of the app allows execution of the command
this.addCommand({
2022-06-19 23:49:37 +03:00
id: "obsidian-note-sharing-share-note",
name: "Create share link",
2022-06-18 17:51:45 +03:00
checkCallback: (checking: boolean) => {
// Conditions to check
2022-06-18 22:51:06 +03:00
const markdownView =
this.app.workspace.getActiveViewOfType(MarkdownView);
2022-06-18 17:51:45 +03:00
if (markdownView) {
if (!checking) {
2022-06-19 23:49:37 +03:00
this.noteSharingService.shareNote(markdownView);
2022-06-18 17:51:45 +03:00
}
return true;
}
2022-06-18 22:51:06 +03:00
},
2022-06-18 17:51:45 +03:00
});
}
2022-06-18 22:51:06 +03:00
onunload() {}
2022-06-18 17:51:45 +03:00
async loadSettings() {
2022-06-18 22:51:06 +03:00
this.settings = Object.assign(
{},
DEFAULT_SETTINGS,
await this.loadData()
);
2022-06-18 17:51:45 +03:00
}
async saveSettings() {
await this.saveData(this.settings);
2022-06-19 23:49:37 +03:00
this.noteSharingService.serverUrl = this.settings.serverUrl;
2022-06-18 17:51:45 +03:00
}
}
2022-06-19 23:49:37 +03:00
class SettingsTab extends PluginSettingTab {
2022-06-18 17:51:45 +03:00
plugin: MyPlugin;
constructor(app: App, plugin: MyPlugin) {
super(app, plugin);
this.plugin = plugin;
}
display(): void {
2022-06-18 22:51:06 +03:00
const { containerEl } = this;
2022-06-18 17:51:45 +03:00
containerEl.empty();
2022-06-19 23:49:37 +03:00
containerEl.createEl("h2", { text: "Obsidian Note Sharing" });
2022-06-18 17:51:45 +03:00
new Setting(containerEl)
2022-06-19 23:49:37 +03:00
.setName("Server URL")
.setDesc("Server URL hosting the encrypted notes.")
2022-06-18 22:51:06 +03:00
.addText((text) =>
text
2022-06-19 23:49:37 +03:00
.setPlaceholder("enter URL")
.setValue(this.plugin.settings.serverUrl)
2022-06-18 22:51:06 +03:00
.onChange(async (value) => {
2022-06-19 23:49:37 +03:00
this.plugin.settings.serverUrl = value;
2022-06-18 22:51:06 +03:00
await this.plugin.saveSettings();
})
);
2022-06-18 17:51:45 +03:00
}
}