-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.ts
More file actions
285 lines (256 loc) · 8.53 KB
/
Copy pathmain.ts
File metadata and controls
285 lines (256 loc) · 8.53 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
import {
Notice,
ObsidianProtocolData,
Platform,
Plugin,
TAbstractFile,
WorkspaceLeaf,
} from "obsidian";
import { TodoStore, deriveLists, deriveTags } from "./src/store";
import { DEFAULT_SETTINGS, TodoSettings, TodoSettingTab } from "./src/settings";
import { TodoView, VIEW_TYPE_TODO } from "./src/view";
import { TaskModal } from "./src/modal";
import { matchHotkey } from "./src/hotkey";
import { Priority, Task, todayStr } from "./src/todotxt";
// The subset of Electron's remote surface the badge needs, as exposed by
// Obsidian to desktop plugins — window.electron.remote in current builds, or
// window.require("electron").remote in older ones.
interface RemoteElectron {
remote?: { app?: { dock?: { setBadge?: (text: string) => void } } };
}
interface ElectronWindow extends Window {
electron?: RemoteElectron;
require?: (module: string) => RemoteElectron | undefined;
}
// The macOS Dock API. Null anywhere the Dock doesn't exist.
function getDock(): { setBadge: (text: string) => void } | null {
if (!Platform.isMacOS) return null;
const w = window as ElectronWindow;
let remote: RemoteElectron["remote"];
try {
remote = w.electron?.remote ?? w.require?.("electron")?.remote;
} catch {
return null;
}
const dock = remote?.app?.dock;
const setBadge = dock?.setBadge;
if (typeof setBadge !== "function") return null;
return { setBadge: (text: string) => setBadge.call(dock, text) };
}
// Accepts "A"/"B"/"C" (case-insensitive) or the words used in the modal.
function parsePriority(raw: string | undefined): Priority {
if (!raw) return null;
const v = raw.trim().toLowerCase();
if (v === "a" || v === "high") return "A";
if (v === "b" || v === "med" || v === "medium") return "B";
if (v === "c" || v === "low") return "C";
return null;
}
export default class NudgePlugin extends Plugin {
settings!: TodoSettings;
store!: TodoStore;
async onload(): Promise<void> {
await this.loadSettings();
this.store = new TodoStore(this.app, this.settings.path);
this.registerView(
VIEW_TYPE_TODO,
(leaf: WorkspaceLeaf) => new TodoView(leaf, this)
);
this.addRibbonIcon("list-todo", "Open Nudge", () => {
void this.activateView();
});
this.addRibbonIcon("plus-circle", "New task", () => {
void this.newTask();
});
this.addCommand({
id: "open-task-list",
name: "Open task list",
callback: () => void this.activateView(),
});
this.addCommand({
id: "new-task",
name: "New task",
callback: () => void this.newTask(),
});
this.addSettingTab(new TodoSettingTab(this.app, this));
// obsidian://nudge?text=...&list=...&due=YYYY-MM-DD&
// priority=high|med|low|A|B|C&link=...&rec=<RRULE>&modal=1
// With text present it creates the item directly; pass modal=1 (or omit
// text) to open the prefilled create modal instead.
this.registerObsidianProtocolHandler("nudge", (params) => {
void this.handleUri(params);
});
// Open and focus the view once the workspace is ready.
if (this.settings.openOnStartup) {
this.app.workspace.onLayoutReady(() => void this.activateView());
}
// Configurable global shortcut to open the new-task window. Uses a
// capture-phase listener so it can override Obsidian's own binding for
// the same combo (e.g. Cmd+N → New note).
this.registerDomEvent(
activeDocument,
"keydown",
(e: KeyboardEvent) => {
const hk = this.settings.newItemHotkey;
if (!hk) return;
const target = e.target as HTMLElement | null;
if (target?.closest(".todo-hotkey-input")) return; // recording
if (!matchHotkey(e, hk)) return;
e.preventDefault();
e.stopPropagation();
e.stopImmediatePropagation();
void this.newTask();
},
{ capture: true }
);
// Re-render open views when the backing file changes on disk (e.g. an
// external agent rewriting todo.txt).
this.registerEvent(
this.app.vault.on("modify", (file: TAbstractFile) => {
if (file.path === this.store.getPath()) {
this.refreshViews();
void this.updateBadge();
}
})
);
// Badge reflects state even with no Nudge pane open. Deferred to
// layout-ready so the vault index already knows the file.
this.app.workspace.onLayoutReady(() => void this.updateBadge());
// Day rollover: at midnight (or on wake past it) the overdue/due-today
// populations change even though the file didn't. A cheap minute tick
// spots the date flip, then refreshes open views and the badge.
let lastDay = todayStr();
this.registerInterval(
window.setInterval(() => {
const day = todayStr();
if (day === lastDay) return;
lastDay = day;
this.refreshViews();
void this.updateBadge();
}, 60_000)
);
}
onunload(): void {
// Leaves are detached automatically by Obsidian on unload.
getDock()?.setBadge(""); // don't leave a stale badge behind
}
// Recompute the Dock badge: uncompleted tasks that are overdue (or also
// due today, per settings). Zero, badge disabled, or a read error all
// clear it — never show a number we can't stand behind.
async updateBadge(): Promise<void> {
const dock = getDock();
if (!dock) return;
if (!this.settings.dockBadge) {
dock.setBadge("");
return;
}
let count = 0;
try {
const tasks = await this.store.readTasks();
const today = todayStr();
const includeToday = this.settings.dockBadgeIncludeToday;
count = tasks.filter(
({ task }) =>
!task.completed &&
!!task.due &&
(includeToday ? task.due <= today : task.due < today)
).length;
} catch {
count = 0;
}
dock.setBadge(count > 0 ? String(count) : "");
}
async handleUri(params: ObsidianProtocolData): Promise<void> {
const text = (params.text ?? "").trim();
const list =
(params.list ?? "").replace(/\s+/g, "") || this.settings.defaultList;
const prefill: Partial<Task> = {
text,
projects: [list],
due: params.due || null,
priority: parsePriority(params.priority),
link: params.link || null,
rec: params.rec || null,
};
const wantsModal = !!params.modal || text.length === 0;
if (wantsModal) {
const tasksOnDisk = await this.store.readTasks();
const lists = deriveLists(tasksOnDisk);
const tags = deriveTags(tasksOnDisk);
new TaskModal(this.app, lists, tags, null, list, async (task) => {
await this.store.addTask(task);
this.refreshViews();
}, prefill).open();
return;
}
const task: Task = {
completed: false,
completionDate: null,
creationDate: null, // stamped by the store
priority: prefill.priority ?? null,
text,
projects: [list],
due: prefill.due ?? null,
link: prefill.link ?? null,
rec: prefill.rec ?? null,
raw: "",
};
await this.store.addTask(task);
new Notice(`Added to ${list}`);
this.refreshViews();
}
async activateView(): Promise<void> {
const { workspace } = this.app;
let leaf: WorkspaceLeaf | null =
workspace.getLeavesOfType(VIEW_TYPE_TODO)[0] ?? null;
if (!leaf) {
leaf = workspace.getLeaf(true);
await leaf.setViewState({ type: VIEW_TYPE_TODO, active: true });
}
await workspace.revealLeaf(leaf);
}
// Open the create modal from anywhere (command / ribbon), independent of
// whether a view is focused. Presets the list to the active view's
// selection when it's a real project list.
async newTask(): Promise<void> {
const tasks = await this.store.readTasks();
const lists = deriveLists(tasks);
const tags = deriveTags(tasks);
// Preset to the focused view's selected project, else the default list.
let preset: string | null = null;
for (const leaf of this.app.workspace.getLeavesOfType(VIEW_TYPE_TODO)) {
if (leaf.view instanceof TodoView) {
preset = leaf.view.getSelectedList();
break;
}
}
if (!preset) preset = this.settings.defaultList;
new TaskModal(this.app, lists, tags, null, preset, async (task) => {
await this.store.addTask(task);
this.refreshViews();
}).open();
}
refreshViews(): void {
for (const leaf of this.app.workspace.getLeavesOfType(VIEW_TYPE_TODO)) {
const view = leaf.view;
if (view instanceof TodoView) void view.refresh();
}
}
onPathChanged(): void {
this.store.setPath(this.settings.path);
this.refreshViews();
void this.updateBadge();
}
async loadSettings(): Promise<void> {
const data = ((await this.loadData()) ?? {}) as Partial<TodoSettings>;
this.settings = Object.assign({}, DEFAULT_SETTINGS, data);
// Clone the styles array/objects so editing them never mutates
// DEFAULT_SETTINGS (which would leak across reloads).
this.settings.listStyles = (
data.listStyles ?? DEFAULT_SETTINGS.listStyles
).map((s) => ({ ...s }));
}
async saveSettings(): Promise<void> {
await this.saveData(this.settings);
}
}