some work

This commit is contained in:
Ell 2022-09-27 16:06:40 +02:00
parent 4d49d195a9
commit 93ecd63a73
4 changed files with 139 additions and 6 deletions

View file

@ -1,6 +1,7 @@
import { Plugin } from "obsidian";
import { ButtonComponent, MarkdownView, Plugin, TextComponent } from "obsidian";
import { defaultSettings, SimpleTimeTrackerSettings } from "./settings";
import { SimpleTimeTrackerSettingsTab } from "./settings-tab";
import { Tracker } from "./tracker";
export default class SimpleTimeTrackerPlugin extends Plugin {
@ -11,10 +12,22 @@ export default class SimpleTimeTrackerPlugin extends Plugin {
this.addSettingTab(new SimpleTimeTrackerSettingsTab(this.app, this));
this.registerMarkdownCodeBlockProcessor("simple-time-tracker", (s, e) => {
this.registerMarkdownCodeBlockProcessor("simple-time-tracker", (s, e, i) => {
e.empty();
e.addClass("simple-time-tracker");
let tracker = Tracker.load(s);
let name = new TextComponent(e)
.setPlaceholder("Name this segment");
new ButtonComponent(e)
.setButtonText("Start")
.onClick(() => {
tracker.start(name.getValue());
// TODO how do we save to the code block??
tracker.save();
});
});
}

65
src/tracker.ts Normal file
View file

@ -0,0 +1,65 @@
import { MarkdownSectionInformation } from "obsidian";
export class Tracker {
entries: Entry[] = [];
display(element: HTMLElement): void {
let list = element.createEl("ul");
for (let entry of this.entries)
list.createEl("li", { text: entry.toString() });
}
start(name: string): void {
// date constructor returns the current date
let entry = new Entry(name, new Date());
this.entries.push(entry);
}
end(): void {
}
save(): void {
// TODO save
JSON.stringify(this);
}
static load(json: string): Tracker {
if (json) {
try {
return JSON.parse(json);
} catch (e) {
console.log(`Failed to parse Tracker from ${json}`);
}
}
return new Tracker();
}
}
export class Entry {
private name: string;
private startTime: Date;
private endTime: Date;
constructor(name: string, startTime: Date) {
this.name = name;
this.startTime = startTime;
}
toString(): string {
let ret = "";
if (this.name)
ret += `${this.name}: `;
// if the days or months are different, we want to add the full date
if (this.startTime.getDay() != this.endTime.getDay() || this.startTime.getMonth() != this.endTime.getMonth()) {
ret += `${this.startTime.toLocaleString()} - ${this.endTime.toLocaleString()}`;
} else {
ret += `${this.startTime.toLocaleTimeString()} - ${this.endTime.toLocaleTimeString()}`;
}
return ret;
}
}

File diff suppressed because one or more lines are too long

View file

@ -3,3 +3,4 @@ This is a time tracker:
```simple-time-tracker
```