Skip to content

Commit 44ce896

Browse files
committed
0.1.2
1 parent 1f1a24c commit 44ce896

21 files changed

+169
-141
lines changed

manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"id": "time-tree",
33
"name": "Time Tree",
4-
"version": "0.1.1",
4+
"version": "0.1.2",
55
"minAppVersion": "0.15.0",
66
"description": "Track accumulated time spent on unlimited hierarchical tasks",
77
"author": "Lucas Lopes",

src/front-matter-manager.ts

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { TFile, Notice, App } from "obsidian";
1+
import { TFile, Notice, App, Editor } from "obsidian";
22
import * as YAML from "yaml";
33

44
export class FrontMatterManager {
@@ -56,4 +56,79 @@ export class FrontMatterManager {
5656
}
5757
await this.app.vault.modify(file, newContent);
5858
}
59+
60+
// Helper function to find the end of YAML front matter (line L is the last YAML line).
61+
getYamlEnd = (lines: string[]): number => {
62+
let yamlEnd = 0;
63+
if (lines[0].trim() === "---") {
64+
for (let i = 1; i < lines.length; i++) {
65+
if (lines[i].trim() === "---") {
66+
yamlEnd = i + 1;
67+
break;
68+
}
69+
}
70+
}
71+
return yamlEnd;
72+
};
73+
74+
// Helper function to collect tracker block boundaries after the YAML front matter.
75+
getTrackerBlocks = (
76+
lines: string[],
77+
start: number
78+
): { start: number; end: number }[] => {
79+
const blocks: { start: number; end: number }[] = [];
80+
for (let i = start; i < lines.length; i++) {
81+
if (lines[i].trimEnd() === "```simple-time-tracker") {
82+
const blockStart = i;
83+
for (let j = i + 1; j < lines.length; j++) {
84+
if (lines[j].trimEnd() === "```") {
85+
blocks.push({ start: blockStart, end: j });
86+
i = j; // Skip the rest of this block
87+
break;
88+
}
89+
}
90+
}
91+
}
92+
return blocks;
93+
};
94+
95+
// Helper function to check if a given line index is inside any tracker block.
96+
isLineInTracker = (
97+
line: number,
98+
blocks: { start: number; end: number }[]
99+
): boolean => {
100+
return blocks.some((block) => line >= block.start && line <= block.end);
101+
};
102+
103+
// Helper function to find the first line after YAML that is not within any tracker block.
104+
findTargetLine = (
105+
lines: string[],
106+
yamlEnd: number,
107+
blocks: { start: number; end: number }[]
108+
): number => {
109+
let target = yamlEnd;
110+
while (target < lines.length && this.isLineInTracker(target, blocks)) {
111+
target++;
112+
}
113+
if (target >= lines.length) {
114+
target = lines.length - 1;
115+
}
116+
return target;
117+
};
118+
119+
async adjustCursorOutsideTracker(editor: Editor): Promise<void> {
120+
const content = editor.getValue();
121+
const lines = content.split("\n");
122+
123+
const yamlEnd = this.getYamlEnd(lines);
124+
const trackerBlocks = this.getTrackerBlocks(lines, yamlEnd);
125+
const targetLine = this.findTargetLine(lines, yamlEnd, trackerBlocks);
126+
127+
const cursor = editor.getCursor();
128+
if (!this.isLineInTracker(cursor.line, trackerBlocks)) {
129+
return;
130+
}
131+
132+
editor.setCursor({ line: targetLine, ch: 0 });
133+
}
59134
}

src/main.ts

Lines changed: 3 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -106,73 +106,12 @@ export default class TimeTreePlugin extends Plugin {
106106
this.scheduleComputeTimeTree();
107107
}
108108

109-
async adjustCursorOutsideTracker(editor: Editor): Promise<void> {
110-
// Get the full content and split it into lines.
111-
const content = editor.getValue();
112-
const lines = content.split("\n");
113-
114-
// Determine the end of YAML front matter if present. Line L is the last line of YAML metadata.
115-
let yamlEnd = 0;
116-
if (lines[0].trim() === "---") {
117-
for (let i = 1; i < lines.length; i++) {
118-
if (lines[i].trim() === "---") {
119-
yamlEnd = i + 1; // YAML metadata ends at line L
120-
break;
121-
}
122-
}
123-
}
124-
125-
// Collect tracker block boundaries, but only after the YAML metadata.
126-
const trackerBlocks: { start: number; end: number }[] = [];
127-
for (let i = yamlEnd; i < lines.length; i++) {
128-
if (lines[i].trimEnd() === "```simple-time-tracker") {
129-
const blockStart = i;
130-
// Look for the closing marker.
131-
for (let j = i + 1; j < lines.length; j++) {
132-
if (lines[j].trimEnd() === "```") {
133-
trackerBlocks.push({ start: blockStart, end: j });
134-
i = j; // Skip the rest of this block
135-
break;
136-
}
137-
}
138-
}
139-
}
140-
141-
// Define a helper to check if a given line index is inside any tracker block.
142-
const isLineInTracker = (line: number): boolean => {
143-
return trackerBlocks.some(
144-
(block) => line >= block.start && line <= block.end
145-
);
146-
};
147-
148-
// Find the first line after YAML metadata that is not inside any tracker block.
149-
let targetLine = yamlEnd;
150-
while (targetLine < lines.length && isLineInTracker(targetLine)) {
151-
targetLine++;
152-
}
153-
if (targetLine >= lines.length) {
154-
targetLine = lines.length - 1;
155-
}
156-
157-
// Get the current cursor position.
158-
const cursor = editor.getCursor();
159-
160-
// If the cursor is not inside any tracker block, do nothing.
161-
const cursorInTracker = trackerBlocks.some(
162-
(block) => cursor.line >= block.start && cursor.line <= block.end
163-
);
164-
if (!cursorInTracker) {
165-
return;
166-
}
167-
168-
// Move the cursor to the first line after YAML metadata that is not part of any tracker block.
169-
editor.setCursor({ line: targetLine, ch: 0 });
170-
}
171-
172109
async startStopTracker(): Promise<void> {
173110
const activeView = this.app.workspace.getActiveViewOfType(MarkdownView);
174111
if (activeView) {
175-
await this.adjustCursorOutsideTracker(activeView.editor);
112+
await this.frontMatterManager.adjustCursorOutsideTracker(
113+
activeView.editor
114+
);
176115
} else {
177116
new Notice("No active Markdown editor found.");
178117
}

test-vault/.obsidian/graph.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,16 @@
2222
}
2323
}
2424
],
25-
"collapse-display": true,
26-
"showArrow": false,
27-
"textFadeMultiplier": 0,
28-
"nodeSizeMultiplier": 1,
29-
"lineSizeMultiplier": 1,
25+
"collapse-display": false,
26+
"showArrow": true,
27+
"textFadeMultiplier": -1,
28+
"nodeSizeMultiplier": 2.51789957682292,
29+
"lineSizeMultiplier": 5,
3030
"collapse-forces": false,
3131
"centerStrength": 0.382568359375,
3232
"repelStrength": 20,
3333
"linkStrength": 1,
3434
"linkDistance": 184,
35-
"scale": 0.38564307862722413,
35+
"scale": 0.389572078704501,
3636
"close": true
3737
}

test-vault/.obsidian/plugins/time-tree/main

Lines changed: 61 additions & 48 deletions
Large diffs are not rendered by default.

test-vault/.obsidian/plugins/time-tree/manifest

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"id": "time-tree",
33
"name": "Time Tree",
4-
"version": "0.1.1",
4+
"version": "0.1.2",
55
"minAppVersion": "0.15.0",
66
"description": "Track accumulated time spent on unlimited hierarchical tasks",
77
"author": "Lucas Lopes",

test-vault/.obsidian/plugins/time-tree/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"id": "time-tree",
33
"name": "Time Tree",
4-
"version": "0.1.1",
4+
"version": "0.1.2",
55
"minAppVersion": "0.15.0",
66
"description": "Track accumulated time spent on unlimited hierarchical tasks",
77
"author": "Lucas Lopes",

test-vault/.obsidian/workspace.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@
8484
}
8585
}
8686
],
87-
"currentTab": 4
87+
"currentTab": 2
8888
}
8989
],
9090
"direction": "vertical"
@@ -228,7 +228,7 @@
228228
"command-palette:Open command palette": false
229229
}
230230
},
231-
"active": "1e02cc558bcf0724",
231+
"active": "e0dedde8b73de522",
232232
"lastOpenFiles": [
233233
"Tasks/sub/test-note.md",
234234
"Untitled.md",

test-vault/Tasks/level-1-a.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
2-
descendants: 1165488
2+
descendants: 1174106
33
elapsed: 0
4-
node_size: 92.54875998801583
4+
node_size: 92.5977491517517
55
---
66

77
# [[level-2a-a]]

test-vault/Tasks/level-1-b.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
descendants: 129725
33
elapsed: 0
4-
node_size: 30.76434555245755
4+
node_size: 30.67065752708492
55
---
66

77
# [[level-2b-a]]

0 commit comments

Comments
 (0)