114 lines
3.1 KiB
TypeScript
114 lines
3.1 KiB
TypeScript
import { App, TFile, Notice, moment } from "obsidian";
|
|
import { Task, Category } from "./types";
|
|
|
|
export function formatDuration(milliseconds: number): string {
|
|
const totalSeconds = Math.floor(milliseconds / 1000);
|
|
const hours = Math.floor(totalSeconds / 3600);
|
|
const minutes = Math.floor((totalSeconds % 3600) / 60);
|
|
const seconds = totalSeconds % 60;
|
|
|
|
if (hours > 0) {
|
|
return `${hours}h ${minutes}m ${seconds}s`;
|
|
} else if (minutes > 0) {
|
|
return `${minutes}m ${seconds}s`;
|
|
} else {
|
|
return `${seconds}s`;
|
|
}
|
|
}
|
|
|
|
export function generateUniqueId(): string {
|
|
return `${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
|
|
}
|
|
|
|
async function getDailyNote(app: App): Promise<TFile | null> {
|
|
const { vault } = app;
|
|
|
|
// @ts-ignore - Access internal plugins
|
|
const dailyNotesPlugin = app.internalPlugins.plugins["daily-notes"];
|
|
|
|
if (!dailyNotesPlugin || !dailyNotesPlugin.enabled) {
|
|
new Notice("Daily notes plugin is not enabled. Please enable it in Settings → Core plugins.");
|
|
return null;
|
|
}
|
|
|
|
// @ts-ignore - Access the daily notes instance
|
|
const dailyNotesInterface = dailyNotesPlugin.instance;
|
|
|
|
if (!dailyNotesInterface) {
|
|
new Notice("Could not access daily notes interface");
|
|
return null;
|
|
}
|
|
|
|
try {
|
|
// @ts-ignore
|
|
const { format, folder, template } = dailyNotesInterface.options || {};
|
|
|
|
const dateFormat = format || "YYYY-MM-DD";
|
|
const dailyNotesFolder = folder || "";
|
|
const today = moment().format(dateFormat);
|
|
|
|
const fileName = `${today}.md`;
|
|
const filePath = dailyNotesFolder ? `${dailyNotesFolder}/${fileName}` : fileName;
|
|
|
|
let dailyNote = vault.getAbstractFileByPath(filePath);
|
|
|
|
if (!dailyNote || !(dailyNote instanceof TFile)) {
|
|
if (dailyNotesFolder && !vault.getAbstractFileByPath(dailyNotesFolder)) {
|
|
await vault.createFolder(dailyNotesFolder);
|
|
}
|
|
|
|
let initialContent = "";
|
|
if (template) {
|
|
const templateFile = vault.getAbstractFileByPath(template);
|
|
if (templateFile instanceof TFile) {
|
|
initialContent = await vault.read(templateFile);
|
|
}
|
|
}
|
|
|
|
dailyNote = await vault.create(filePath, initialContent);
|
|
}
|
|
|
|
if (dailyNote instanceof TFile) {
|
|
return dailyNote;
|
|
}
|
|
|
|
return null;
|
|
} catch (error) {
|
|
console.error("Error getting daily note:", error);
|
|
new Notice("Error accessing daily note: " + error.message);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
export async function logTaskToDailyNote(
|
|
app: App,
|
|
task: Task,
|
|
category: Category | undefined,
|
|
format: string
|
|
): Promise<void> {
|
|
try {
|
|
const dailyNote = await getDailyNote(app);
|
|
|
|
if (!dailyNote) {
|
|
new Notice("Could not find or create daily note");
|
|
return;
|
|
}
|
|
|
|
const duration = formatDuration(task.totalElapsed);
|
|
const logEntry = format
|
|
.replace("{{title}}", task.title)
|
|
.replace("{{duration}}", duration)
|
|
.replace("{{emoji}}", category?.emoji || "")
|
|
.replace("{{category}}", category?.name || "");
|
|
|
|
const content = await app.vault.read(dailyNote);
|
|
const newContent = content ? `${content}\n${logEntry}` : logEntry;
|
|
await app.vault.modify(dailyNote, newContent);
|
|
|
|
new Notice("Task logged to daily note");
|
|
} catch (error) {
|
|
console.error("Failed to log task to daily note:", error);
|
|
new Notice("Failed to log task: " + error.message);
|
|
}
|
|
}
|