noteshare.space/plugin/main.ts

101 lines
2.5 KiB
TypeScript
Raw Normal View History

2022-06-29 23:24:59 +03:00
import {
MarkdownView,
Menu,
Notice,
Plugin,
TAbstractFile,
TFile,
} from "obsidian";
2022-06-19 23:49:37 +03:00
import { NoteSharingService } from "src/NoteSharingService";
2022-06-29 12:18:32 +03:00
import { DEFAULT_SETTINGS } from "src/obsidian/PluginSettings";
2022-06-26 23:46:39 +03:00
import SettingsTab from "src/obsidian/SettingsTab";
2022-06-29 12:08:06 +03:00
import { SharedNoteSuccessModal } from "src/ui/SharedNoteSuccessModal";
2022-06-29 12:18:32 +03:00
import type { EventRef } from "obsidian";
import type { PluginSettings } from "src/obsidian/PluginSettings";
2022-06-18 17:51:45 +03:00
// Remember to rename these classes and interfaces!
2022-06-26 23:46:39 +03:00
export default class NoteSharingPlugin extends Plugin {
private settings: PluginSettings;
private noteSharingService: NoteSharingService;
private eventRef: EventRef;
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
);
2022-06-26 23:46:39 +03:00
// Init settings tab
2022-06-19 23:49:37 +03:00
this.addSettingTab(new SettingsTab(this.app, this));
2022-06-18 17:51:45 +03:00
2022-06-26 23:46:39 +03:00
// Add note sharing command
2022-06-29 12:08:06 +03:00
this.addCommands();
2022-06-26 23:46:39 +03:00
this.eventRef = this.app.workspace.on(
"file-menu",
(menu, file, source) => this.onMenuOpenCallback(menu, file, source)
);
this.registerEvent(this.eventRef);
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-29 12:08:06 +03:00
addCommands() {
this.addCommand({
id: "obsidian-note-sharing-share-note",
name: "Create share link",
checkCallback: (checking: boolean) => {
// Only works on Markdown views
const activeView =
this.app.workspace.getActiveViewOfType(MarkdownView);
if (!activeView) return false;
if (checking) return true;
this.shareNote(activeView.getViewData());
},
});
}
2022-06-26 23:46:39 +03:00
// https://github.dev/platers/obsidian-linter/blob/c30ceb17dcf2c003ca97862d94cbb0fd47b83d52/src/main.ts#L139-L149
onMenuOpenCallback(menu: Menu, file: TAbstractFile, source: string) {
if (file instanceof TFile && file.extension === "md") {
menu.addItem((item) => {
item.setIcon("paper-plane-glyph");
item.setTitle("Share note");
item.onClick(async (evt) => {
2022-06-29 12:08:06 +03:00
this.shareNote(await this.app.vault.read(file));
2022-06-26 23:46:39 +03:00
});
});
}
2022-06-18 17:51:45 +03:00
}
2022-06-29 12:08:06 +03:00
async shareNote(mdText: string) {
2022-06-29 23:24:59 +03:00
this.noteSharingService
.shareNote(mdText)
.then((res) => {
new SharedNoteSuccessModal(
this,
res.view_url,
res.expire_time
).open();
})
.catch((err: Error) => {
console.error(err);
new Notice(err.message, 7500);
});
2022-06-29 12:08:06 +03:00
}
2022-06-18 17:51:45 +03:00
}