Skip to content

Commit

Permalink
Naming homologizaiton
Browse files Browse the repository at this point in the history
  • Loading branch information
serrnovik committed Nov 4, 2024
1 parent 059b414 commit 9ee3128
Show file tree
Hide file tree
Showing 24 changed files with 2,103 additions and 89 deletions.
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
# Tag-Based Time Tracker
Multi-purpose time tracker for your notes!

![A screenshot of the plugin in action](reporting-screenshot.png)

This is a fork of [super-simple-plugin](https://github.com/Ellpeck/ObsidianSimpleTimeTracker) that has support for out-of-the-box tagging.

# TLDR

1. Execute the `Super Simple Time Tracker: Insert Time Tracker` command to start logging.
1. Execute the `Tag Based Time Tracker: Insert Time Tracker` command to start logging.
2. Set your tags in the settings in YAML format:

```yaml
Expand Down Expand Up @@ -80,17 +79,17 @@ clients:
```

## Tracker Data in Dataview
Super Simple Time Tracker has a public API that can be used with [Dataview](https://blacksmithgu.github.io/obsidian-dataview/), specifically [DataviewJS](https://blacksmithgu.github.io/obsidian-dataview/api/intro/), which can be accessed using the following code:
Tag Based Time Tracker has a public API that can be used with [Dataview](https://blacksmithgu.github.io/obsidian-dataview/), specifically [DataviewJS](https://blacksmithgu.github.io/obsidian-dataview/api/intro/), which can be accessed using the following code:

```js
dv.app.plugins.plugins["simple-time-tracker"].api;
dv.app.plugins.plugins["tag-based-time-tracker"].api;
```

The following is a short example that uses DataviewJS to load all trackers in the vault and print the total duration of each tracker:

```js
// Get the time tracker plugin API instance
let api = dv.app.plugins.plugins["simple-time-tracker"].api;
let api = dv.app.plugins.plugins["tag-based-time-tracker"].api;
for (let page of dv.pages()) {
// Load trackers in the file with the given path
Expand Down
2 changes: 1 addition & 1 deletion esbuild.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ esbuild.build({
copy({
assets: [{
from: ["./manifest.json", "./main.js", "./styles.css"],
to: ["./test-vault/.obsidian/plugins/simple-time-tracker/."]
to: ["./test-vault/.obsidian/plugins/tag-based-time-tracker/."]
}]
}),
],
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "simple-time-tracker",
"name": "tag-based-time-tracker",
"version": "1.0.3",
"description": "Multi-purpose time trackers for your notes!",
"main": "main.js",
Expand Down
14 changes: 7 additions & 7 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Editor, MarkdownRenderChild, moment, Plugin, TFile } from "obsidian";
import { defaultSettings, SimpleTimeTrackerSettings } from "./settings";
import { SimpleTimeTrackerSettingsTab } from "./settings-tab";
import { defaultSettings, TagBasedTimeTracker } from "./settings";
import { TagBasedTimeTrackerTab } from "./settings-tab";
import {
displayTracker,
Entry,
Expand All @@ -16,7 +16,7 @@ import {
} from "./tracker";
import { TimeTrackingSummary } from "./timeTrackingSummary";

export default class SimpleTimeTrackerPlugin extends Plugin {
export default class TagBasedTimeTrackerPlugin extends Plugin {
public api = {
// verbatim versions of the functions found in tracker.ts with the same parameters
loadTracker,
Expand All @@ -34,15 +34,15 @@ export default class SimpleTimeTrackerPlugin extends Plugin {
orderedEntries: (entries: Entry[]) =>
orderedEntries(entries, this.settings),
};
public settings: SimpleTimeTrackerSettings;
public settings: TagBasedTimeTracker;

async onload(): Promise<void> {
await this.loadSettings();

this.addSettingTab(new SimpleTimeTrackerSettingsTab(this.app, this));
this.addSettingTab(new TagBasedTimeTrackerTab(this.app, this));

this.registerMarkdownCodeBlockProcessor(
"simple-time-tracker",
"tag-based-time-tracker",
(s, e, i) => {
e.empty();
let component = new MarkdownRenderChild(e);
Expand Down Expand Up @@ -77,7 +77,7 @@ export default class SimpleTimeTrackerPlugin extends Plugin {
id: `insert`,
name: `Insert Time Tracker`,
editorCallback: (e, _) => {
e.replaceSelection("```simple-time-tracker\n```\n");
e.replaceSelection("```tag-based-time-tracker\n```\n");
},
});

Expand Down
14 changes: 7 additions & 7 deletions src/settings-tab.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import {App, PluginSettingTab, Setting} from "obsidian";
import SimpleTimeTrackerPlugin from "./main";
import TagBasedTimeTrackerPlugin from "./main";
import {defaultSettings} from "./settings";

export class SimpleTimeTrackerSettingsTab extends PluginSettingTab {
export class TagBasedTimeTrackerTab extends PluginSettingTab {

plugin: SimpleTimeTrackerPlugin;
plugin: TagBasedTimeTrackerPlugin;

constructor(app: App, plugin: SimpleTimeTrackerPlugin) {
constructor(app: App, plugin: TagBasedTimeTrackerPlugin) {
super(app, plugin);
this.plugin = plugin;
}

display(): void {
this.containerEl.empty();
this.containerEl.createEl("h2", {text: "Super Simple Time Tracker Settings"});
this.containerEl.createEl("h2", {text: "Tag Based Time Tracker Settings"});

new Setting(this.containerEl)
.setName("Timestamp Display Format")
Expand Down Expand Up @@ -94,12 +94,12 @@ export class SimpleTimeTrackerSettingsTab extends PluginSettingTab {
this.containerEl.createEl("p", { text: "Need help using the plugin? Feel free to join the Discord server!" });
this.containerEl.createEl("a", { href: "https://link.ellpeck.de/discordweb" }).createEl("img", {
attr: { src: "https://ellpeck.de/res/discord-wide.png" },
cls: "simple-time-tracker-settings-image"
cls: "tag-based-time-tracker-settings-image"
});
this.containerEl.createEl("p", { text: "If you like this plugin and want to support its development, you can do so through my website by clicking this fancy image!" });
this.containerEl.createEl("a", { href: "https://ellpeck.de/support" }).createEl("img", {
attr: { src: "https://ellpeck.de/res/generalsupport-wide.png" },
cls: "simple-time-tracker-settings-image"
cls: "tag-based-time-tracker-settings-image"
});
}
}
4 changes: 2 additions & 2 deletions src/settings.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const defaultSettings: SimpleTimeTrackerSettings = {
export const defaultSettings: TagBasedTimeTracker = {
timestampFormat: "YY-MM-DD HH:mm:ss",
editableTimestampFormat: "YYYY-MM-DD HH:mm:ss",
csvDelimiter: ",",
Expand Down Expand Up @@ -44,7 +44,7 @@ clients:
subTags: []`
};

export interface SimpleTimeTrackerSettings {
export interface TagBasedTimeTracker {
timestampFormat: string;
editableTimestampFormat: string;
csvDelimiter: string;
Expand Down
6 changes: 3 additions & 3 deletions src/timeTrackingSummary.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { SimpleTimeTrackerSettings } from "./settings";
import { TagBasedTimeTracker } from "./settings";
import { Configuration, Section, Item, SubTag } from "./interfaces";
import { parseYaml, moment } from "obsidian";


export class TimeTrackingSummary {
settings: SimpleTimeTrackerSettings;
settings: TagBasedTimeTracker;
api: any;
app: any; // Reference to the Obsidian app

constructor(app: any, settings: SimpleTimeTrackerSettings, api: any) {
constructor(app: any, settings: TagBasedTimeTracker, api: any) {
this.app = app;
this.settings = settings;
this.api = api;
Expand Down
56 changes: 28 additions & 28 deletions src/tracker.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { moment, MarkdownSectionInformation, ButtonComponent, TextComponent, TFile, MarkdownRenderer, Component, MarkdownRenderChild } from "obsidian";
import { SimpleTimeTrackerSettings } from "./settings";
import { TagBasedTimeTracker } from "./settings";
import { ConfirmModal } from "./confirm-modal";

export interface Tracker {
Expand Down Expand Up @@ -51,7 +51,7 @@ export async function loadAllTrackers(fileName: string): Promise<{ section: Mark
let curr: Partial<MarkdownSectionInformation> | undefined;
for (let i = 0; i < content.length; i++) {
let line = content[i];
if (line.trimEnd() == "```simple-time-tracker") {
if (line.trimEnd() == "```tag-based-time-tracker") {
curr = { lineStart: i + 1, text: "" };
} else if (curr) {
if (line.trimEnd() == "```") {
Expand All @@ -69,9 +69,9 @@ export async function loadAllTrackers(fileName: string): Promise<{ section: Mark

type GetFile = () => string;

export function displayTracker(tracker: Tracker, element: HTMLElement, getFile: GetFile, getSectionInfo: () => MarkdownSectionInformation, settings: SimpleTimeTrackerSettings, component: MarkdownRenderChild): void {
export function displayTracker(tracker: Tracker, element: HTMLElement, getFile: GetFile, getSectionInfo: () => MarkdownSectionInformation, settings: TagBasedTimeTracker, component: MarkdownRenderChild): void {

element.addClass("simple-time-tracker-container");
element.addClass("tag-based-time-tracker-container");
// add start/stop controls
let running = isRunning(tracker);
let btn = new ButtonComponent(element)
Expand All @@ -86,24 +86,24 @@ export function displayTracker(tracker: Tracker, element: HTMLElement, getFile:
}
await saveTracker(tracker, getFile(), getSectionInfo());
});
btn.buttonEl.addClass("simple-time-tracker-btn");
btn.buttonEl.addClass("tag-based-time-tracker-btn");
let newSegmentNameBox = new TextComponent(element)
.setPlaceholder("Segment name")
.setDisabled(running);
newSegmentNameBox.inputEl.addClass("simple-time-tracker-txt");
newSegmentNameBox.inputEl.addClass("tag-based-time-tracker-txt");

// add timers
let timer = element.createDiv({ cls: "simple-time-tracker-timers" });
let currentDiv = timer.createEl("div", { cls: "simple-time-tracker-timer" });
let current = currentDiv.createEl("span", { cls: "simple-time-tracker-timer-time" });
let timer = element.createDiv({ cls: "tag-based-time-tracker-timers" });
let currentDiv = timer.createEl("div", { cls: "tag-based-time-tracker-timer" });
let current = currentDiv.createEl("span", { cls: "tag-based-time-tracker-timer-time" });
currentDiv.createEl("span", { text: "Current" });
let totalDiv = timer.createEl("div", { cls: "simple-time-tracker-timer" });
let total = totalDiv.createEl("span", { cls: "simple-time-tracker-timer-time", text: "0s" });
let totalDiv = timer.createEl("div", { cls: "tag-based-time-tracker-timer" });
let total = totalDiv.createEl("span", { cls: "tag-based-time-tracker-timer-time", text: "0s" });
totalDiv.createEl("span", { text: "Total" });

if (tracker.entries.length > 0) {
// add table
let table = element.createEl("table", { cls: "simple-time-tracker-table" });
let table = element.createEl("table", { cls: "tag-based-time-tracker-table" });
table.createEl("tr").append(
createEl("th", { text: "Segment" }),
createEl("th", { text: "Start time" }),
Expand All @@ -115,7 +115,7 @@ export function displayTracker(tracker: Tracker, element: HTMLElement, getFile:
addEditableTableRow(tracker, entry, table, newSegmentNameBox, running, getFile, getSectionInfo, settings, 0, component);

// add copy buttons
let buttons = element.createEl("div", { cls: "simple-time-tracker-bottom" });
let buttons = element.createEl("div", { cls: "tag-based-time-tracker-bottom" });
new ButtonComponent(buttons)
.setButtonText("Copy as table")
.onClick(() => navigator.clipboard.writeText(createMarkdownTable(tracker, settings)));
Expand Down Expand Up @@ -172,7 +172,7 @@ export function getRunningEntry(entries: Entry[]): Entry {
return null;
}

export function createMarkdownTable(tracker: Tracker, settings: SimpleTimeTrackerSettings): string {
export function createMarkdownTable(tracker: Tracker, settings: TagBasedTimeTracker): string {
let table = [["Segment", "Start time", "End time", "Duration"]];
for (let entry of orderedEntries(tracker.entries, settings))
table.push(...createTableSection(entry, settings));
Expand All @@ -194,7 +194,7 @@ export function createMarkdownTable(tracker: Tracker, settings: SimpleTimeTracke
return ret;
}

export function createCsv(tracker: Tracker, settings: SimpleTimeTrackerSettings): string {
export function createCsv(tracker: Tracker, settings: TagBasedTimeTracker): string {
let ret = "";
for (let entry of orderedEntries(tracker.entries, settings)) {
for (let row of createTableSection(entry, settings))
Expand All @@ -203,15 +203,15 @@ export function createCsv(tracker: Tracker, settings: SimpleTimeTrackerSettings)
return ret;
}

export function orderedEntries(entries: Entry[], settings: SimpleTimeTrackerSettings): Entry[] {
export function orderedEntries(entries: Entry[], settings: TagBasedTimeTracker): Entry[] {
return settings.reverseSegmentOrder ? entries.slice().reverse() : entries;
}

export function formatTimestamp(timestamp: string, settings: SimpleTimeTrackerSettings): string {
export function formatTimestamp(timestamp: string, settings: TagBasedTimeTracker): string {
return moment(timestamp).format(settings.timestampFormat);
}

export function formatDuration(totalTime: number, settings: SimpleTimeTrackerSettings): string {
export function formatDuration(totalTime: number, settings: TagBasedTimeTracker): string {
let ret = "";
let duration = moment.duration(totalTime);
let hours = settings.fineGrainedDurations ? duration.hours() : Math.floor(duration.asHours());
Expand Down Expand Up @@ -289,7 +289,7 @@ function removeEntry(entries: Entry[], toRemove: Entry): boolean {
return false;
}

function setCountdownValues(tracker: Tracker, current: HTMLElement, total: HTMLElement, currentDiv: HTMLDivElement, settings: SimpleTimeTrackerSettings): void {
function setCountdownValues(tracker: Tracker, current: HTMLElement, total: HTMLElement, currentDiv: HTMLDivElement, settings: TagBasedTimeTracker): void {
let running = getRunningEntry(tracker.entries);
if (running && !running.endTime) {
current.setText(formatDuration(getDuration(running), settings));
Expand All @@ -300,11 +300,11 @@ function setCountdownValues(tracker: Tracker, current: HTMLElement, total: HTMLE
total.setText(formatDuration(getTotalDuration(tracker.entries), settings));
}

function formatEditableTimestamp(timestamp: string, settings: SimpleTimeTrackerSettings): string {
function formatEditableTimestamp(timestamp: string, settings: TagBasedTimeTracker): string {
return moment(timestamp).format(settings.editableTimestampFormat);
}

function unformatEditableTimestamp(formatted: string, settings: SimpleTimeTrackerSettings): string {
function unformatEditableTimestamp(formatted: string, settings: TagBasedTimeTracker): string {
return moment(formatted, settings.editableTimestampFormat).toISOString();
}

Expand All @@ -326,7 +326,7 @@ function updateLegacyInfo(entries: Entry[]): void {
}


function createTableSection(entry: Entry, settings: SimpleTimeTrackerSettings): string[][] {
function createTableSection(entry: Entry, settings: TagBasedTimeTracker): string[][] {
let ret = [[
entry.name,
entry.startTime ? formatTimestamp(entry.startTime, settings) : "",
Expand All @@ -339,7 +339,7 @@ function createTableSection(entry: Entry, settings: SimpleTimeTrackerSettings):
return ret;
}

function addEditableTableRow(tracker: Tracker, entry: Entry, table: HTMLTableElement, newSegmentNameBox: TextComponent, trackerRunning: boolean, getFile: GetFile, getSectionInfo: () => MarkdownSectionInformation, settings: SimpleTimeTrackerSettings, indent: number, component: MarkdownRenderChild): void {
function addEditableTableRow(tracker: Tracker, entry: Entry, table: HTMLTableElement, newSegmentNameBox: TextComponent, trackerRunning: boolean, getFile: GetFile, getSectionInfo: () => MarkdownSectionInformation, settings: TagBasedTimeTracker, indent: number, component: MarkdownRenderChild): void {
let entryRunning = getRunningEntry(tracker.entries) == entry;
let row = table.createEl("tr");

Expand All @@ -353,7 +353,7 @@ function addEditableTableRow(tracker: Tracker, entry: Entry, table: HTMLTableEle

let expandButton = new ButtonComponent(nameField.label)
.setClass("clickable-icon")
.setClass("simple-time-tracker-expand-button")
.setClass("tag-based-time-tracker-expand-button")
.setIcon(`chevron-${entry.collapsed ? "left" : "down"}`)
.onClick(async () => {
if (entry.collapsed) {
Expand All @@ -367,7 +367,7 @@ function addEditableTableRow(tracker: Tracker, entry: Entry, table: HTMLTableEle
expandButton.buttonEl.style.visibility = "hidden";

let entryButtons = row.createEl("td");
entryButtons.addClass("simple-time-tracker-table-buttons");
entryButtons.addClass("tag-based-time-tracker-table-buttons");
new ButtonComponent(entryButtons)
.setClass("clickable-icon")
.setIcon(`lucide-play`)
Expand Down Expand Up @@ -491,7 +491,7 @@ class EditableField {
this.label = this.cell.createEl("span", { text: value });
this.label.style.marginLeft = `${indent}em`;
this.box = new TextComponent(this.cell).setValue(value);
this.box.inputEl.addClass("simple-time-tracker-input");
this.box.inputEl.addClass("tag-based-time-tracker-input");
this.box.inputEl.hide();
this.box.inputEl.addEventListener('keydown', (e: KeyboardEvent) => {
// Save with Ctrl/Cmd + Enter
Expand Down Expand Up @@ -527,9 +527,9 @@ class EditableField {
}

class EditableTimestampField extends EditableField {
settings: SimpleTimeTrackerSettings;
settings: TagBasedTimeTracker;

constructor(row: HTMLTableRowElement, value: string, settings: SimpleTimeTrackerSettings) {
constructor(row: HTMLTableRowElement, value: string, settings: TagBasedTimeTracker) {
super(row, 0, value ? formatTimestamp(value, settings) : "");
this.settings = settings;
}
Expand Down
Loading

0 comments on commit 9ee3128

Please sign in to comment.