Skip to content

Commit d4ceafa

Browse files
committed
Add new command to insert a new task in the note (template line to link a new note)
1 parent a939111 commit d4ceafa

File tree

3 files changed

+37
-7
lines changed

3 files changed

+37
-7
lines changed

src/main.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Plugin, TFile, Notice } from "obsidian";
1+
import { Plugin, TFile, Notice, Editor } from "obsidian";
22
import { defaultSettings, TimeTreeSettings } from "./settings";
33
import { TimeTreeSettingsTab } from "./settings-tab";
44
import { FrontMatterManager } from "./front-matter-manager";
@@ -37,6 +37,14 @@ export default class TimeTreePlugin extends Plugin {
3737
await this.computeTimeTree();
3838
},
3939
});
40+
41+
this.addCommand({
42+
id: "new-task",
43+
name: "Insert new task",
44+
editorCallback: (editor, view) => {
45+
this.insertNewTask(editor);
46+
},
47+
});
4048
}
4149

4250
async loadSettings(): Promise<void> {
@@ -95,4 +103,20 @@ export default class TimeTreePlugin extends Plugin {
95103
);
96104
await this.calculator.updateNodeSizeFromFile(rootFile);
97105
}
106+
107+
async insertNewTask(editor: Editor): Promise<void> {
108+
let cursor = editor.getCursor();
109+
if (cursor.ch !== 0) {
110+
const currentLineText = editor.getLine(cursor.line);
111+
editor.replaceRange("\n", {
112+
line: cursor.line,
113+
ch: currentLineText.length,
114+
});
115+
cursor = { line: cursor.line + 1, ch: 0 };
116+
editor.setCursor(cursor);
117+
}
118+
const textToInsert = "# [[]]";
119+
editor.replaceRange(textToInsert, cursor);
120+
editor.setCursor({ line: cursor.line, ch: cursor.ch + 4 });
121+
}
98122
}

test-vault/.obsidian/workspace.json

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,14 @@
1111
"id": "98225c0495a73a21",
1212
"type": "leaf",
1313
"state": {
14-
"type": "graph",
15-
"state": {},
16-
"icon": "lucide-git-fork",
17-
"title": "Graph view"
14+
"type": "markdown",
15+
"state": {
16+
"file": "Tasks/level-2b-b.md",
17+
"mode": "source",
18+
"source": false
19+
},
20+
"icon": "lucide-file",
21+
"title": "level-2b-b"
1822
}
1923
},
2024
{
@@ -219,9 +223,9 @@
219223
},
220224
"active": "98225c0495a73a21",
221225
"lastOpenFiles": [
226+
"Tasks/level-1-b.md",
222227
"Tasks/level-2a-c.md",
223228
"Tasks/level-1-a.md",
224-
"Tasks/level-1-b.md",
225229
"Tasks/level-1-c.md",
226230
"Tasks/level-2c-b.md",
227231
"Tasks/level-2b-b.md",

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ elapsed: 8679
33
elapsed_child: 0
44
node_size: 14.93697369350021
55
---
6-
6+
# [[level-3-c]]
7+
# [[testing-new-task]]
8+
# [[newer-task]]
79
```simple-time-tracker
810
{"entries":[{"name":"Segment 1","startTime":"2025-03-12T04:47:55.128Z","endTime":"2025-03-12T04:47:57.829Z"},{"name":"Segment 2","startTime":"2025-03-12T04:47:59.594Z","endTime":"2025-03-12T04:48:02.845Z"},{"name":"Segment 3","startTime":"2025-03-12T17:04:37.683Z","endTime":"2025-03-12T17:04:38.847Z"},{"name":"Segment 4","startTime":"2025-03-12T17:05:23.165Z","endTime":"2025-03-12T17:05:24.213Z"},{"name":"Segment 5","startTime":"2025-03-12T17:05:53.499Z","endTime":"2025-03-12T17:05:54.014Z"}]}
911
```

0 commit comments

Comments
 (0)