habitica-sync/main.ts

66 lines
1.9 KiB
TypeScript
Raw Normal View History

import { Notice, Plugin } from "obsidian";
import { HabiticaSyncSettingsTab } from "./settings";
import { HabiticaSyncView, VIEW_TYPE} from "./view"
2020-10-25 16:55:59 -04:00
interface HabiticaSyncSettings {
2021-10-10 21:08:13 -07:00
userID: string
apiToken: string
2020-12-22 11:24:00 -05:00
}
const DEFAULT_SETTINGS: Partial<HabiticaSyncSettings> = {
2021-10-10 21:08:13 -07:00
userID: "",
apiToken: ""
2020-12-22 11:24:00 -05:00
}
export default class HabiticaSync extends Plugin {
settings: HabiticaSyncSettings;
view: HabiticaSyncView;
2021-10-03 18:10:16 +05:30
displayNotice(message: string){
new Notice(message)
}
2021-10-03 18:10:16 +05:30
async onload() {
await this.loadSettings();
this.addSettingTab(new HabiticaSyncSettingsTab(this.app, this));
2021-10-03 18:10:16 +05:30
this.registerView(
VIEW_TYPE,
(leaf) => (this.view = new HabiticaSyncView(leaf, this))
2021-10-03 18:10:16 +05:30
);
2021-10-17 13:16:32 +05:30
this.addRibbonIcon("popup-open", "Open Habitica Pane", () => { //activate view
2021-10-03 18:10:16 +05:30
this.activateView();
});
this.addCommand({
id: "habitica-view-open",
name: "Habitica: Open Pane",
hotkeys: [{ modifiers: ["Mod", "Shift"], key: "h"}],
callback: () => {
this.activateView();
}
});
2021-10-03 18:10:16 +05:30
}
async loadSettings() {
this.settings = Object.assign(DEFAULT_SETTINGS, await this.loadData())
}
async saveSettings() {
await this.saveData(this.settings);
}
async onunload() {
await this.view.onClose();
this.app.workspace
.getLeavesOfType(VIEW_TYPE)
2021-10-03 18:10:16 +05:30
.forEach((leaf) => leaf.detach());
}
async activateView() {
this.app.workspace.detachLeavesOfType(VIEW_TYPE);
2021-10-03 18:10:16 +05:30
await this.app.workspace.getRightLeaf(false).setViewState({
type: VIEW_TYPE,
2021-10-03 18:10:16 +05:30
active: true,
});
this.app.workspace.revealLeaf(
this.app.workspace.getLeavesOfType(VIEW_TYPE)[0]
2021-10-03 18:10:16 +05:30
);
}
2020-10-25 16:55:59 -04:00
}