mirror of
https://github.com/Ellpeck/ObsidianSimpleTimeTracker.git
synced 2024-12-19 03:39:23 +01:00
some more work!
This commit is contained in:
parent
93ecd63a73
commit
e1c0a3cd17
4 changed files with 146 additions and 118 deletions
21
src/main.ts
21
src/main.ts
|
@ -1,7 +1,7 @@
|
||||||
import { ButtonComponent, MarkdownView, Plugin, TextComponent } from "obsidian";
|
import { ButtonComponent, Plugin, TextComponent } from "obsidian";
|
||||||
import { defaultSettings, SimpleTimeTrackerSettings } from "./settings";
|
import { defaultSettings, SimpleTimeTrackerSettings } from "./settings";
|
||||||
import { SimpleTimeTrackerSettingsTab } from "./settings-tab";
|
import { SimpleTimeTrackerSettingsTab } from "./settings-tab";
|
||||||
import { Tracker } from "./tracker";
|
import { displayTracker, endEntry, isRunning, loadTracker, saveTracker, startEntry, Tracker } from "./tracker";
|
||||||
|
|
||||||
export default class SimpleTimeTrackerPlugin extends Plugin {
|
export default class SimpleTimeTrackerPlugin extends Plugin {
|
||||||
|
|
||||||
|
@ -16,18 +16,23 @@ export default class SimpleTimeTrackerPlugin extends Plugin {
|
||||||
e.empty();
|
e.empty();
|
||||||
e.addClass("simple-time-tracker");
|
e.addClass("simple-time-tracker");
|
||||||
|
|
||||||
let tracker = Tracker.load(s);
|
let tracker = loadTracker(s);
|
||||||
|
|
||||||
let name = new TextComponent(e)
|
let name = new TextComponent(e)
|
||||||
.setPlaceholder("Name this segment");
|
.setPlaceholder("Name this segment");
|
||||||
new ButtonComponent(e)
|
new ButtonComponent(e)
|
||||||
.setButtonText("Start")
|
.setButtonText("Start")
|
||||||
.onClick(() => {
|
.onClick(async () => {
|
||||||
tracker.start(name.getValue());
|
if (isRunning(tracker)) {
|
||||||
|
endEntry(tracker);
|
||||||
// TODO how do we save to the code block??
|
} else {
|
||||||
tracker.save();
|
startEntry(tracker, name.getValue());
|
||||||
|
}
|
||||||
|
name.setValue("");
|
||||||
|
await saveTracker(tracker, this.app, i.getSectionInfo(e));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
displayTracker(tracker, e);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
130
src/tracker.ts
130
src/tracker.ts
|
@ -1,65 +1,75 @@
|
||||||
import { MarkdownSectionInformation } from "obsidian";
|
import { App, MarkdownSectionInformation } from "obsidian";
|
||||||
|
|
||||||
export class Tracker {
|
export class Tracker {
|
||||||
|
entries: Entry[];
|
||||||
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 {
|
export interface Entry {
|
||||||
|
name: string;
|
||||||
private name: string;
|
startTime: number;
|
||||||
private startTime: Date;
|
endTime: number;
|
||||||
private endTime: Date;
|
}
|
||||||
|
|
||||||
constructor(name: string, startTime: Date) {
|
export function startEntry(tracker: Tracker, name: string): void {
|
||||||
this.name = name;
|
// date constructor returns the current date
|
||||||
this.startTime = startTime;
|
let entry: Entry = { name: name, startTime: Date.now(), endTime: null };
|
||||||
}
|
tracker.entries.push(entry);
|
||||||
|
};
|
||||||
toString(): string {
|
|
||||||
let ret = "";
|
export function endEntry(tracker: Tracker): void {
|
||||||
if (this.name)
|
let last = tracker.entries.last();
|
||||||
ret += `${this.name}: `;
|
last.endTime = Date.now();
|
||||||
|
}
|
||||||
// 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()) {
|
export function isRunning(tracker: Tracker): boolean {
|
||||||
ret += `${this.startTime.toLocaleString()} - ${this.endTime.toLocaleString()}`;
|
let last = tracker.entries.last();
|
||||||
} else {
|
return last != null && !last.endTime;
|
||||||
ret += `${this.startTime.toLocaleTimeString()} - ${this.endTime.toLocaleTimeString()}`;
|
}
|
||||||
}
|
|
||||||
return ret;
|
export async function saveTracker(tracker: Tracker, app: App, section: MarkdownSectionInformation): Promise<void> {
|
||||||
}
|
let file = app.workspace.getActiveFile();
|
||||||
|
let content = await app.vault.cachedRead(file);
|
||||||
|
|
||||||
|
// figure out what part of the content we have to edit
|
||||||
|
let lines = content.split("\n");
|
||||||
|
let prev = lines.filter((_, i) => i <= section.lineStart).join("\n");
|
||||||
|
let next = lines.filter((_, i) => i >= section.lineEnd).join("\n");
|
||||||
|
// edit only the code block content, leave the rest untouched
|
||||||
|
content = `${prev}\n${JSON.stringify(tracker)}\n${next}`;
|
||||||
|
|
||||||
|
await app.vault.modify(file, content);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function loadTracker(json: string): Tracker {
|
||||||
|
if (json) {
|
||||||
|
try {
|
||||||
|
return JSON.parse(json);
|
||||||
|
} catch (e) {
|
||||||
|
console.log(`Failed to parse Tracker from ${json}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return { entries: [] };
|
||||||
|
}
|
||||||
|
|
||||||
|
export function displayTracker(tracker: Tracker, element: HTMLElement): void {
|
||||||
|
let list = element.createEl("ul");
|
||||||
|
for (let entry of tracker.entries)
|
||||||
|
list.createEl("li", { text: displayEntry(entry) });
|
||||||
|
};
|
||||||
|
|
||||||
|
export function displayEntry(entry: Entry): string {
|
||||||
|
// TODO add an option to display this as an interval rather than a from - to string
|
||||||
|
let ret = "";
|
||||||
|
if (entry.name)
|
||||||
|
ret += `${entry.name}: `;
|
||||||
|
|
||||||
|
let start = new Date(entry.startTime);
|
||||||
|
ret += `${start.toLocaleString()} - `;
|
||||||
|
|
||||||
|
if (entry.endTime) {
|
||||||
|
let end = new Date(entry.endTime);
|
||||||
|
ret += `${end.toLocaleString()}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
File diff suppressed because one or more lines are too long
|
@ -1,6 +1,7 @@
|
||||||
This is a time tracker:
|
This is a time tracker:
|
||||||
|
|
||||||
```simple-time-tracker
|
```simple-time-tracker
|
||||||
|
{"entries":[{"name":"","startTime":1664290639233,"endTime":1664290642003},{"name":"","startTime":1664290643788,"endTime":1664290647600},{"name":"","startTime":1664290650678,"endTime":1664290653168},{"name":"Another segment","startTime":1664290658822,"endTime":1664290662444},{"name":"","startTime":1664290690793,"endTime":1664290692366},{"name":"","startTime":1664290695119,"endTime":1664290696140}]}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue