2022-06-26 23:46:39 +03:00
|
|
|
import {
|
|
|
|
EventRef,
|
|
|
|
MarkdownView,
|
|
|
|
Menu,
|
|
|
|
Plugin,
|
|
|
|
TAbstractFile,
|
|
|
|
TFile,
|
|
|
|
} from "obsidian";
|
2022-06-19 23:49:37 +03:00
|
|
|
import { NoteSharingService } from "src/NoteSharingService";
|
2022-06-26 23:46:39 +03:00
|
|
|
import { DEFAULT_SETTINGS, PluginSettings } from "src/obsidian/PluginSettings";
|
|
|
|
import SettingsTab from "src/obsidian/SettingsTab";
|
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-18 17:51:45 +03:00
|
|
|
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) => {
|
2022-06-26 23:46:39 +03:00
|
|
|
// Only works on Markdown views
|
|
|
|
const activeView =
|
2022-06-18 22:51:06 +03:00
|
|
|
this.app.workspace.getActiveViewOfType(MarkdownView);
|
2022-06-26 23:46:39 +03:00
|
|
|
if (!activeView) return false;
|
|
|
|
if (checking) return true;
|
|
|
|
this.noteSharingService.shareNote(activeView.getViewData());
|
2022-06-18 22:51:06 +03:00
|
|
|
},
|
2022-06-18 17:51:45 +03:00
|
|
|
});
|
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-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) => {
|
|
|
|
this.noteSharingService.shareNote(
|
|
|
|
await this.app.vault.read(file)
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2022-06-18 17:51:45 +03:00
|
|
|
}
|
|
|
|
}
|