Skip to content

Commit

Permalink
feat: Implement collapsing nested subentries (#50)
Browse files Browse the repository at this point in the history
* feat: Implement collapsing nested subentries

* fix: Change tooltip based on collapse state

* fix: Avoid hardcoded CSS constants

* fix: Retain old formatting when editing row

* fix: Change collapsed field from number to boolean

* fix: Remove unneeded wrapper code
  • Loading branch information
jamesbtan authored Aug 9, 2024
1 parent 9b718d2 commit 1ddefa5
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/tracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export interface Entry {
startTime: string;
endTime: string;
subEntries: Entry[];
collapsed?: boolean;
}

export async function saveTracker(tracker: Tracker, fileName: string, section: MarkdownSectionInformation): Promise<void> {
Expand Down Expand Up @@ -345,6 +346,22 @@ function addEditableTableRow(tracker: Tracker, entry: Entry, table: HTMLTableEle

renderNameAsMarkdown(nameField.label, getFile, component);

let expandButton = new ButtonComponent(nameField.label)
.setClass("clickable-icon")
.setClass("simple-time-tracker-expand-button")
.setIcon(`chevron-${entry.collapsed ? 'right' : 'down'}`)
.setTooltip(entry.collapsed ? "Expand" : "Collapse")
.onClick(async () => {
if (entry.collapsed) {
delete entry.collapsed;
} else {
entry.collapsed = true;
}
await saveTracker(tracker, this.app, getFile(), getSectionInfo());
});
if (!entry.subEntries?.length) expandButton.buttonEl.style.visibility = 'hidden';
nameField.cell.insertBefore(expandButton.buttonEl, nameField.label);

let entryButtons = row.createEl("td");
entryButtons.addClass("simple-time-tracker-table-buttons");
new ButtonComponent(entryButtons)
Expand All @@ -363,6 +380,7 @@ function addEditableTableRow(tracker: Tracker, entry: Entry, table: HTMLTableEle
.onClick(async () => {
if (nameField.editing()) {
entry.name = nameField.endEdit();
expandButton.buttonEl.style.display = null;
startField.endEdit();
entry.startTime = startField.getTimestamp();
if (!entryRunning) {
Expand All @@ -375,6 +393,7 @@ function addEditableTableRow(tracker: Tracker, entry: Entry, table: HTMLTableEle
renderNameAsMarkdown(nameField.label, getFile, component);
} else {
nameField.beginEdit(entry.name);
expandButton.buttonEl.style.display = 'none';
// only allow editing start and end times if we don't have sub entries
if (!entry.subEntries) {
startField.beginEdit(entry.startTime);
Expand All @@ -401,7 +420,7 @@ function addEditableTableRow(tracker: Tracker, entry: Entry, table: HTMLTableEle
await saveTracker(tracker, getFile(), getSectionInfo());
});

if (entry.subEntries) {
if (entry.subEntries && !entry.collapsed) {
for (let sub of orderedEntries(entry.subEntries, settings))
addEditableTableRow(tracker, sub, table, newSegmentNameBox, trackerRunning, getFile, getSectionInfo, settings, indent + 1, component);
}
Expand Down

0 comments on commit 1ddefa5

Please sign in to comment.