2021-10-15 10:30:25 +05:30
|
|
|
import { Notice, Plugin } from "obsidian";
|
2021-10-17 17:40:37 +05:30
|
|
|
import { HabiticaSyncSettingsTab } from "./settings";
|
|
|
|
|
import { HabiticaSyncView, VIEW_TYPE} from "./view"
|
2020-10-25 16:55:59 -04:00
|
|
|
|
2021-10-17 17:40:37 +05:30
|
|
|
interface HabiticaSyncSettings {
|
2021-10-10 21:08:13 -07:00
|
|
|
userID: string
|
|
|
|
|
apiToken: string
|
2020-12-22 11:24:00 -05:00
|
|
|
}
|
2021-10-17 17:40:37 +05:30
|
|
|
const DEFAULT_SETTINGS: Partial<HabiticaSyncSettings> = {
|
2021-10-10 21:08:13 -07:00
|
|
|
userID: "",
|
|
|
|
|
apiToken: ""
|
2020-12-22 11:24:00 -05:00
|
|
|
}
|
2021-10-17 17:40:37 +05:30
|
|
|
export default class HabiticaSync extends Plugin {
|
|
|
|
|
settings: HabiticaSyncSettings;
|
|
|
|
|
view: HabiticaSyncView;
|
2021-10-03 18:10:16 +05:30
|
|
|
|
2021-10-15 10:30:25 +05:30
|
|
|
displayNotice(message: string){
|
|
|
|
|
new Notice(message)
|
|
|
|
|
}
|
2021-10-03 18:10:16 +05:30
|
|
|
async onload() {
|
|
|
|
|
await this.loadSettings();
|
2021-10-17 17:40:37 +05:30
|
|
|
this.addSettingTab(new HabiticaSyncSettingsTab(this.app, this));
|
2021-10-03 18:10:16 +05:30
|
|
|
this.registerView(
|
2021-10-17 17:40:37 +05:30
|
|
|
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();
|
|
|
|
|
});
|
2021-10-17 17:40:37 +05:30
|
|
|
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
|
2021-10-17 17:40:37 +05:30
|
|
|
.getLeavesOfType(VIEW_TYPE)
|
2021-10-03 18:10:16 +05:30
|
|
|
.forEach((leaf) => leaf.detach());
|
|
|
|
|
}
|
|
|
|
|
async activateView() {
|
2021-10-17 17:40:37 +05:30
|
|
|
this.app.workspace.detachLeavesOfType(VIEW_TYPE);
|
2021-10-03 18:10:16 +05:30
|
|
|
|
|
|
|
|
await this.app.workspace.getRightLeaf(false).setViewState({
|
2021-10-17 17:40:37 +05:30
|
|
|
type: VIEW_TYPE,
|
2021-10-03 18:10:16 +05:30
|
|
|
active: true,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
this.app.workspace.revealLeaf(
|
2021-10-17 17:40:37 +05:30
|
|
|
this.app.workspace.getLeavesOfType(VIEW_TYPE)[0]
|
2021-10-03 18:10:16 +05:30
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-25 16:55:59 -04:00
|
|
|
}
|