orrganized code a bit better

This commit is contained in:
Ell 2022-09-27 21:44:28 +02:00
parent 49406dd869
commit d001d7887a
4 changed files with 41 additions and 43 deletions

View file

@ -1,7 +1,7 @@
import { ButtonComponent, Plugin, TextComponent } from "obsidian"; import { Plugin } from "obsidian";
import { defaultSettings, SimpleTimeTrackerSettings } from "./settings"; import { defaultSettings, SimpleTimeTrackerSettings } from "./settings";
import { SimpleTimeTrackerSettingsTab } from "./settings-tab"; import { SimpleTimeTrackerSettingsTab } from "./settings-tab";
import { displayTracker, endEntry, isRunning, loadTracker, saveTracker, startEntry, Tracker } from "./tracker"; import { displayTracker, loadTracker } from "./tracker";
export default class SimpleTimeTrackerPlugin extends Plugin { export default class SimpleTimeTrackerPlugin extends Plugin {
@ -13,29 +13,9 @@ export default class SimpleTimeTrackerPlugin extends Plugin {
this.addSettingTab(new SimpleTimeTrackerSettingsTab(this.app, this)); this.addSettingTab(new SimpleTimeTrackerSettingsTab(this.app, this));
this.registerMarkdownCodeBlockProcessor("simple-time-tracker", (s, e, i) => { this.registerMarkdownCodeBlockProcessor("simple-time-tracker", (s, e, i) => {
e.empty();
let tracker = loadTracker(s); let tracker = loadTracker(s);
let running = isRunning(tracker); e.empty();
displayTracker(tracker, e, () => i.getSectionInfo(e));
let btn = new ButtonComponent(e)
.setButtonText(running ? "End" : "Start")
.onClick(async () => {
if (running) {
endEntry(tracker);
} else {
startEntry(tracker, name.getValue());
}
await saveTracker(tracker, this.app, i.getSectionInfo(e));
});
btn.buttonEl.addClass("simple-time-tracker-btn");
let name = new TextComponent(e)
.setPlaceholder("Segment name")
.setDisabled(running);
name.inputEl.addClass("simple-time-tracker-txt");
displayTracker(tracker, e);
}); });
} }

View file

@ -1,4 +1,4 @@
import { moment, App, MarkdownSectionInformation } from "obsidian"; import { moment, App, MarkdownSectionInformation, ButtonComponent, TextComponent } from "obsidian";
export class Tracker { export class Tracker {
entries: Entry[]; entries: Entry[];
@ -52,7 +52,25 @@ export function loadTracker(json: string): Tracker {
return { entries: [] }; return { entries: [] };
} }
export function displayTracker(tracker: Tracker, element: HTMLElement): void { export function displayTracker(tracker: Tracker, element: HTMLElement, getSectionInfo: () => MarkdownSectionInformation): void {
// add start/stop controls
let running = isRunning(tracker);
let btn = new ButtonComponent(element)
.setButtonText(running ? "End" : "Start")
.onClick(async () => {
if (running) {
endEntry(tracker);
} else {
startEntry(tracker, name.getValue());
}
await saveTracker(tracker, this.app, getSectionInfo());
});
btn.buttonEl.addClass("simple-time-tracker-btn");
let name = new TextComponent(element)
.setPlaceholder("Segment name")
.setDisabled(running);
name.inputEl.addClass("simple-time-tracker-txt");
// add timers // add timers
let timer = element.createDiv({ cls: "simple-time-tracker-timers" }); let timer = element.createDiv({ cls: "simple-time-tracker-timers" });
let currentDiv = timer.createEl("div", { cls: "simple-time-tracker-timer" }); let currentDiv = timer.createEl("div", { cls: "simple-time-tracker-timer" });

File diff suppressed because one or more lines are too long

View file

@ -1,7 +1,7 @@
This is a time tracker: This is a time tracker:
```simple-time-tracker ```simple-time-tracker
{"entries":[{"name":"Segment 1","startTime":1664306406,"endTime":1664306408},{"name":"Segment 2","startTime":1664306409,"endTime":1664306410},{"name":"Segment 3","startTime":1664306411,"endTime":1664306412},{"name":"Segment 4","startTime":1664306413,"endTime":1664306422},{"name":"Segment 5","startTime":1664306455,"endTime":1664306458},{"name":"Segment 6","startTime":1664306543,"endTime":1664306545},{"name":"Segment 7","startTime":1664306581,"endTime":1664306599},{"name":"Segment 8","startTime":1664306956,"endTime":1664306959},{"name":"Segment 9","startTime":1664306962,"endTime":1664306965},{"name":"Segment 10","startTime":1664307015,"endTime":1664307018},{"name":"Segment 11","startTime":1664307036,"endTime":1664307039},{"name":"Segment 12","startTime":1664307055,"endTime":1664307149},{"name":"Segment 13","startTime":1664307152,"endTime":1664307159},{"name":"Segment 14","startTime":1664307169,"endTime":1664307198},{"name":"Segment 15","startTime":1664307254,"endTime":1664307270},{"name":"Segment 16","startTime":1664307272,"endTime":1664307279},{"name":"Working on stuff","startTime":1664307284,"endTime":1664307290},{"name":"Segment 18","startTime":1664307593,"endTime":1664307611}]} {"entries":[{"name":"Segment 1","startTime":1664306406,"endTime":1664306408},{"name":"Segment 2","startTime":1664306409,"endTime":1664306410},{"name":"Segment 3","startTime":1664306411,"endTime":1664306412},{"name":"Segment 4","startTime":1664306413,"endTime":1664306422},{"name":"Segment 5","startTime":1664306455,"endTime":1664306458},{"name":"Segment 6","startTime":1664306543,"endTime":1664306545},{"name":"Segment 7","startTime":1664306581,"endTime":1664306599},{"name":"Segment 8","startTime":1664306956,"endTime":1664306959},{"name":"Segment 9","startTime":1664306962,"endTime":1664306965},{"name":"Segment 10","startTime":1664307015,"endTime":1664307018},{"name":"Segment 11","startTime":1664307036,"endTime":1664307039},{"name":"Segment 12","startTime":1664307055,"endTime":1664307149},{"name":"Segment 13","startTime":1664307152,"endTime":1664307159},{"name":"Segment 14","startTime":1664307169,"endTime":1664307198},{"name":"Segment 15","startTime":1664307254,"endTime":1664307270},{"name":"Segment 16","startTime":1664307272,"endTime":1664307279},{"name":"Working on stuff","startTime":1664307284,"endTime":1664307290},{"name":"Segment 18","startTime":1664307593,"endTime":1664307611},{"name":"Segment 19","startTime":1664307842,"endTime":1664307851}]}
``` ```