generated from obsidianmd/obsidian-sample-plugin
-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
main.ts
149 lines (131 loc) · 4.26 KB
/
main.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import { DEFAULT_SETTINGS } from "lib/config/DefaultSettings";
import type { PluginSettings } from "lib/config/PluginSettings";
import { CODEBLOCK_LANG } from "lib/constants";
import reportBlockHandler from "lib/reports/reportBlockHandler";
import TogglService from "lib/toggl/TogglService";
import TogglSettingsTab from "lib/ui/TogglSettingsTab";
import TogglReportView, {
VIEW_TYPE_REPORT,
} from "lib/ui/views/TogglReportView";
import UserInputHelper from "lib/util/UserInputHelper";
import { settingsStore, versionLogDismissed } from "lib/util/stores";
import { Plugin, WorkspaceLeaf } from "obsidian";
export default class MyPlugin extends Plugin {
public settings: PluginSettings;
public toggl: TogglService;
public input: UserInputHelper;
public reportView: TogglReportView;
async onload() {
console.log(`Loading obsidian-toggl-integration ${this.manifest.version}`);
await this.loadSettings();
this.addSettingTab(new TogglSettingsTab(this.app, this));
// instantiate toggl class and set the API token if set in settings.
this.toggl = new TogglService(this);
if (this.settings.apiToken != null || this.settings.apiToken != "") {
this.toggl.refreshApiConnection(this.settings.apiToken);
this.input = new UserInputHelper(this);
}
// Register commands
// start timer command
this.addCommand({
checkCallback: (checking: boolean) => {
if (!checking) {
this.toggl.startTimer();
} else {
return true;
}
},
icon: "clock",
id: "start-timer",
name: "Start Toggl Timer",
});
// stop timer command
this.addCommand({
checkCallback: (checking: boolean) => {
if (!checking) {
this.toggl.stopTimer();
} else {
return this.toggl.currentTimeEntry != null;
}
},
icon: "clock",
id: "stop-timer",
name: "Stop Toggl Timer",
});
// Register the timer report view
this.registerView(
VIEW_TYPE_REPORT,
(leaf: WorkspaceLeaf) =>
(this.reportView = new TogglReportView(leaf, this)),
);
// Add the view to the right sidebar
if (this.app.workspace.layoutReady) {
this.initLeaf();
} else {
this.app.workspace.onLayoutReady(this.initLeaf.bind(this));
}
this.addCommand({
callback: async () => {
const existing = this.app.workspace.getLeavesOfType(VIEW_TYPE_REPORT);
if (existing.length) {
this.app.workspace.revealLeaf(existing[0]);
return;
}
await this.app.workspace.getRightLeaf(false).setViewState({
active: true,
type: VIEW_TYPE_REPORT,
});
this.app.workspace.revealLeaf(
this.app.workspace.getLeavesOfType(VIEW_TYPE_REPORT)[0],
);
},
id: "show-report-view",
name: "Open report view",
});
this.addCommand({
checkCallback: (checking: boolean) => {
if (!checking) {
this.toggl.refreshApiConnection(this.settings.apiToken);
} else {
return this.settings.apiToken != null || this.settings.apiToken != "";
}
},
id: "refresh-api",
name: "Refresh API Connection",
});
// Enable processing codeblocks for rendering in-note reports
this.registerCodeBlockProcessor();
}
initLeaf(): void {
if (this.app.workspace.getLeavesOfType(VIEW_TYPE_REPORT).length) {
return;
}
this.app.workspace.getRightLeaf(false).setViewState({
type: VIEW_TYPE_REPORT,
});
}
/**
* Registeres the MarkdownPostProcessor for rendering reports from
* codeblock queries.
*/
registerCodeBlockProcessor() {
this.registerMarkdownCodeBlockProcessor(CODEBLOCK_LANG, reportBlockHandler);
}
onunload() {}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
if (!this.settings.hasDismissedAlert) {
this.settings.hasDismissedAlert = false;
}
settingsStore.set(this.settings);
versionLogDismissed.set(this.settings.hasDismissedAlert);
versionLogDismissed.subscribe((bool) => {
this.settings.hasDismissedAlert = bool;
this.saveSettings();
});
}
async saveSettings() {
await this.saveData(this.settings);
settingsStore.set(this.settings);
}
}