-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.ts
More file actions
75 lines (61 loc) · 1.72 KB
/
main.ts
File metadata and controls
75 lines (61 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import { Notice, Plugin, MarkdownView } from 'obsidian';
export default class DoNextTaskPlugin extends Plugin {
onload() {
// Add ribbon icon for marking next task
this.addRibbonIcon('check-circle', 'Mark next task', () => {
this.markNextTask();
});
// Add command for marking next task
this.addCommand({
id: 'mark-next-task',
name: 'Mark next task',
callback: () => {
this.markNextTask();
}
});
}
markNextTask() {
const activeView = this.app.workspace.getActiveViewOfType(MarkdownView);
if (!activeView) {
new Notice('No active note');
return;
}
const editor = activeView.editor;
const lineCount = editor.lineCount();
// Find first unchecked task
let taskLineIndex = -1;
const taskRegex = /^(\s*[-*]\s+\[)\s(\].*)/;
for (let i = 0; i < lineCount; i++) {
const line = editor.getLine(i);
if (taskRegex.test(line)) {
taskLineIndex = i;
break;
}
}
if (taskLineIndex === -1) {
new Notice('No uncompleted tasks found');
return;
}
// Get current time in HH:MM format
const now = new Date();
const hours = String(now.getHours()).padStart(2, '0');
const minutes = String(now.getMinutes()).padStart(2, '0');
const timeString = `${hours}:${minutes}`;
// Replace the task line using replaceRange to preserve cursor position
const oldLine = editor.getLine(taskLineIndex);
const match = oldLine.match(taskRegex);
if (match) {
const newLine = `${match[1]}x] ${timeString}${match[2].substring(1)}`;
// Replace only the specific line
editor.replaceRange(
newLine,
{ line: taskLineIndex, ch: 0 },
{ line: taskLineIndex, ch: oldLine.length }
);
new Notice('Task marked as completed');
}
}
onunload() {
// Cleanup if needed
}
}