ObsidianSimpleTimeTracker/src/main.ts

50 lines
1.4 KiB
TypeScript
Raw Normal View History

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();
2022-09-27 16:06:40 +02:00
2022-09-27 17:03:44 +02:00
let tracker = loadTracker(s);
2022-09-27 19:33:34 +02:00
let running = isRunning(tracker);
2022-09-27 16:06:40 +02:00
2022-09-27 19:33:34 +02:00
let btn = new ButtonComponent(e)
.setButtonText(running ? "End" : "Start")
2022-09-27 17:03:44 +02:00
.onClick(async () => {
2022-09-27 19:33:34 +02:00
if (running) {
2022-09-27 17:03:44 +02:00
endEntry(tracker);
} else {
startEntry(tracker, name.getValue());
}
await saveTracker(tracker, this.app, i.getSectionInfo(e));
2022-09-27 16:06:40 +02:00
});
2022-09-27 19:33:34 +02:00
btn.buttonEl.addClass("simple-time-tracker-btn");
let name = new TextComponent(e)
.setPlaceholder("Segment Name")
.setDisabled(running);
name.inputEl.addClass("simple-time-tracker-txt");
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);
}
}