2022-09-27 17:03:44 +02:00
|
|
|
import { ButtonComponent, Plugin, TextComponent } from "obsidian";
|
2022-09-27 15:36:00 +02:00
|
|
|
import { defaultSettings, SimpleTimeTrackerSettings } from "./settings";
|
|
|
|
import { SimpleTimeTrackerSettingsTab } from "./settings-tab";
|
2022-09-27 17:03:44 +02:00
|
|
|
import { displayTracker, endEntry, isRunning, loadTracker, saveTracker, startEntry, Tracker } from "./tracker";
|
2022-09-27 15:36:00 +02:00
|
|
|
|
|
|
|
export default class SimpleTimeTrackerPlugin extends Plugin {
|
|
|
|
|
|
|
|
settings: SimpleTimeTrackerSettings;
|
|
|
|
|
|
|
|
async onload(): Promise<void> {
|
|
|
|
await this.loadSettings();
|
|
|
|
|
|
|
|
this.addSettingTab(new SimpleTimeTrackerSettingsTab(this.app, this));
|
|
|
|
|
2022-09-27 16:06:40 +02:00
|
|
|
this.registerMarkdownCodeBlockProcessor("simple-time-tracker", (s, e, i) => {
|
2022-09-27 15:36:00 +02:00
|
|
|
e.empty();
|
|
|
|
e.addClass("simple-time-tracker");
|
2022-09-27 16:06:40 +02:00
|
|
|
|
2022-09-27 17:03:44 +02:00
|
|
|
let tracker = loadTracker(s);
|
2022-09-27 16:06:40 +02:00
|
|
|
|
|
|
|
let name = new TextComponent(e)
|
|
|
|
.setPlaceholder("Name this segment");
|
|
|
|
new ButtonComponent(e)
|
|
|
|
.setButtonText("Start")
|
2022-09-27 17:03:44 +02:00
|
|
|
.onClick(async () => {
|
|
|
|
if (isRunning(tracker)) {
|
|
|
|
endEntry(tracker);
|
|
|
|
} else {
|
|
|
|
startEntry(tracker, name.getValue());
|
|
|
|
}
|
|
|
|
name.setValue("");
|
|
|
|
await saveTracker(tracker, this.app, i.getSectionInfo(e));
|
2022-09-27 16:06:40 +02:00
|
|
|
});
|
2022-09-27 17:03:44 +02:00
|
|
|
|
|
|
|
displayTracker(tracker, e);
|
2022-09-27 15:36:00 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async loadSettings() {
|
|
|
|
this.settings = Object.assign({}, defaultSettings, await this.loadData());
|
|
|
|
}
|
|
|
|
|
|
|
|
async saveSettings() {
|
|
|
|
await this.saveData(this.settings);
|
|
|
|
}
|
|
|
|
}
|