mirror of
https://github.com/Ellpeck/ObsidianSimpleTimeTracker.git
synced 2024-11-28 03:58:34 +01:00
Compare commits
2 commits
1e15966bfc
...
f11ce3e807
Author | SHA1 | Date | |
---|---|---|---|
|
f11ce3e807 | ||
|
a8070e4cf0 |
3 changed files with 44 additions and 55 deletions
1
.prettierignore
Normal file
1
.prettierignore
Normal file
|
@ -0,0 +1 @@
|
||||||
|
**
|
|
@ -1,4 +1,4 @@
|
||||||
import {moment, App, MarkdownSectionInformation, ButtonComponent, TextComponent, TFile} from "obsidian";
|
import {moment, App, MarkdownSectionInformation, ButtonComponent, TextComponent, TFile, MarkdownRenderer} from "obsidian";
|
||||||
import {SimpleTimeTrackerSettings} from "./settings";
|
import {SimpleTimeTrackerSettings} from "./settings";
|
||||||
|
|
||||||
export interface Tracker {
|
export interface Tracker {
|
||||||
|
@ -370,63 +370,28 @@ class EditableTimestampField extends EditableField {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class EditableTagField extends EditableField {
|
function addEditableTableRow(
|
||||||
constructor(row: HTMLTableRowElement, indent: number, value: string) {
|
tracker: Tracker,
|
||||||
super(row, indent, value);
|
entry: Entry,
|
||||||
|
table: HTMLTableElement,
|
||||||
const tagValue = this.getTag(value);
|
newSegmentNameBox: TextComponent,
|
||||||
this.label.innerHTML = tagValue;
|
trackerRunning: boolean,
|
||||||
}
|
file: string,
|
||||||
|
getSectionInfo: () => MarkdownSectionInformation,
|
||||||
beginEdit(value: string): void {
|
settings: SimpleTimeTrackerSettings,
|
||||||
super.beginEdit(value ? this.setTag(value) : "");
|
indent: number
|
||||||
}
|
): void {
|
||||||
|
|
||||||
endEdit(): string {
|
|
||||||
const value = this.box.getValue();
|
|
||||||
let displayValue = value;
|
|
||||||
if (value) {
|
|
||||||
displayValue = this.getTag(value);
|
|
||||||
}
|
|
||||||
this.label.innerHTML = displayValue;
|
|
||||||
this.box.inputEl.hide();
|
|
||||||
this.label.hidden = false;
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
|
|
||||||
getTag(value: string): string {
|
|
||||||
const regex = /#(\S+)(?=\s|$)/;
|
|
||||||
const match = value.match(regex);
|
|
||||||
if (match) {
|
|
||||||
const tagName = match[0];
|
|
||||||
const tagHtml = `<a href="${tagName}" class="tag" target="_blank" rel="noopener" data-tag-name="${tagName}">${tagName}</a>`;
|
|
||||||
return value.replace(regex, tagHtml);
|
|
||||||
}
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
|
|
||||||
setTag(value: string): string {
|
|
||||||
const anchorRegex = /<a href="([^"]+)" class="tag" target="_blank" rel="noopener" data-tag-name="([^"]+)">([^<]+)<\/a>/;
|
|
||||||
const match = value.match(anchorRegex);
|
|
||||||
if (match) {
|
|
||||||
const tagName = match[2];
|
|
||||||
return value.replace(anchorRegex, tagName);
|
|
||||||
}
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function addEditableTableRow(tracker: Tracker, entry: Entry, table: HTMLTableElement, newSegmentNameBox: TextComponent, trackerRunning: boolean, file: string, getSectionInfo: () => MarkdownSectionInformation, settings: SimpleTimeTrackerSettings, indent: number): void {
|
|
||||||
let entryRunning = getRunningEntry(tracker.entries) == entry;
|
let entryRunning = getRunningEntry(tracker.entries) == entry;
|
||||||
let row = table.createEl("tr");
|
let row = table.createEl("tr");
|
||||||
|
|
||||||
//let nameField = new EditableField(row, indent, entry.name);
|
let nameField = new EditableField(row, indent, entry.name);
|
||||||
let nameField = new EditableTagField(row, indent, entry.name);
|
let startField = new EditableTimestampField(row, entry.startTime, settings);
|
||||||
let startField = new EditableTimestampField(row, (entry.startTime), settings);
|
let endField = new EditableTimestampField(row, entry.endTime, settings);
|
||||||
let endField = new EditableTimestampField(row, (entry.endTime), settings);
|
|
||||||
|
|
||||||
row.createEl("td", { text: entry.endTime || entry.subEntries ? formatDuration(getDuration(entry), settings) : "" });
|
row.createEl("td", { text: entry.endTime || entry.subEntries ? formatDuration(getDuration(entry), settings) : "" });
|
||||||
|
|
||||||
|
renderSegments(row, file);
|
||||||
|
|
||||||
let entryButtons = row.createEl("td");
|
let entryButtons = row.createEl("td");
|
||||||
entryButtons.addClass("simple-time-tracker-table-buttons");
|
entryButtons.addClass("simple-time-tracker-table-buttons");
|
||||||
new ButtonComponent(entryButtons)
|
new ButtonComponent(entryButtons)
|
||||||
|
@ -452,6 +417,8 @@ function addEditableTableRow(tracker: Tracker, entry: Entry, table: HTMLTableEle
|
||||||
entry.endTime = endField.getTimestamp();
|
entry.endTime = endField.getTimestamp();
|
||||||
await saveTracker(tracker, this.app, file, getSectionInfo());
|
await saveTracker(tracker, this.app, file, getSectionInfo());
|
||||||
editButton.setIcon("lucide-pencil");
|
editButton.setIcon("lucide-pencil");
|
||||||
|
|
||||||
|
renderSegments(row, file);
|
||||||
} else {
|
} else {
|
||||||
nameField.beginEdit(entry.name);
|
nameField.beginEdit(entry.name);
|
||||||
// only allow editing start and end times if we don't have sub entries
|
// only allow editing start and end times if we don't have sub entries
|
||||||
|
@ -473,7 +440,24 @@ function addEditableTableRow(tracker: Tracker, entry: Entry, table: HTMLTableEle
|
||||||
});
|
});
|
||||||
|
|
||||||
if (entry.subEntries) {
|
if (entry.subEntries) {
|
||||||
for (let sub of orderedEntries(entry.subEntries, settings))
|
for (let sub of orderedEntries(entry.subEntries, settings)) addEditableTableRow(tracker, sub, table, newSegmentNameBox, trackerRunning, file, getSectionInfo, settings, indent + 1);
|
||||||
addEditableTableRow(tracker, sub, table, newSegmentNameBox, trackerRunning, file, getSectionInfo, settings, indent + 1);
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Render Segment as Markdown
|
||||||
|
* @param row - Html row in table
|
||||||
|
* @param path - Path to file with time tracker
|
||||||
|
*/
|
||||||
|
function renderSegments(row: any, path: string) {
|
||||||
|
// Get coluumn with Segment
|
||||||
|
const segment = row.querySelector("td:first-child span");
|
||||||
|
if (segment) {
|
||||||
|
const htmlData = segment.innerHTML;
|
||||||
|
// Render Markdown
|
||||||
|
// Result `<p>_rendered_html_</p>`
|
||||||
|
MarkdownRenderer.renderMarkdown(htmlData, segment as HTMLElement, path, this);
|
||||||
|
// Replace current segment by rendered version
|
||||||
|
segment.innerHTML = segment.querySelector("p").innerHTML;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
4
test-vault/test-markdown.md
Normal file
4
test-vault/test-markdown.md
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
Tested for #tag, *italic*, [link](test2), etc:
|
||||||
|
```simple-time-tracker
|
||||||
|
{"entries":[{"name":"`Segment 1`","startTime":"2022-09-27T19:51:18.000Z","endTime":"2022-09-27T19:51:24.000Z"},{"name":"Segment 2","startTime":"2022-09-27T19:51:25.000Z","endTime":"2022-09-27T19:51:26.000Z"},{"name":"#tag Seqment 3 *add* #tag1 text","startTime":null,"endTime":null,"subEntries":[{"name":"Part 1 #tagp1","startTime":"2024-03-17T11:16:00.382Z","endTime":"2024-03-17T11:16:15.966Z","subEntries":null},{"name":"Part 3","startTime":"2024-03-17T11:17:08.000Z","endTime":"2024-03-17T11:17:24.000Z","subEntries":null}]},{"name":"#tag3 Segment 4","startTime":null,"endTime":null,"subEntries":[{"name":"Part 1 #tag4","startTime":"2024-03-17T12:22:04.000Z","endTime":"2024-03-17T12:22:16.000Z","subEntries":null},{"name":"#tag5 Part 2 *italic*","startTime":"2024-03-17T12:22:20.000Z","endTime":"2024-03-17T12:22:24.000Z","subEntries":null}]},{"name":"*italic* Segment 5 #tag6 [test2](test2)","startTime":"2024-03-17T12:40:37.000Z","endTime":"2024-03-17T12:40:45.000Z","subEntries":null}]}
|
||||||
|
```
|
Loading…
Reference in a new issue