From 1e15966bfce9b269fcfa842e40f5f9c1f67a35d8 Mon Sep 17 00:00:00 2001 From: bK3127B201 Date: Thu, 14 Mar 2024 18:20:58 +0200 Subject: [PATCH] feat: add tag display to Segment --- src/tracker.ts | 49 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/src/tracker.ts b/src/tracker.ts index 4d93614..8a49c76 100644 --- a/src/tracker.ts +++ b/src/tracker.ts @@ -370,11 +370,58 @@ class EditableTimestampField extends EditableField { } } +class EditableTagField extends EditableField { + constructor(row: HTMLTableRowElement, indent: number, value: string) { + super(row, indent, value); + + const tagValue = this.getTag(value); + this.label.innerHTML = tagValue; + } + + beginEdit(value: string): void { + super.beginEdit(value ? this.setTag(value) : ""); + } + + 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 = `${tagName}`; + return value.replace(regex, tagHtml); + } + return value; + } + + setTag(value: string): string { + const anchorRegex = /([^<]+)<\/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 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 endField = new EditableTimestampField(row, (entry.endTime), settings);