import { Notice, TFile } from "obsidian"; import ExternalLink from "@lucide/svelte/icons/external-link"; import FileText from "@lucide/svelte/icons/file-text"; import PenLine from "@lucide/svelte/icons/pen-line"; import ScanEye from "@lucide/svelte/icons/scan-eye"; import SquarePen from "@lucide/svelte/icons/square-pen"; import { STATUS_META } from "../lib/vocab"; import { STATUS_ORDER } from "../model/types"; import type { Task } from "../model/types"; import { replaceRaw, setStatus } from "../vault/mutate"; import { tasksApi, warnMissingTasks } from "../tasks-api"; import type { MenuItem } from "./menu-body.svelte"; import type { ViewContext } from "./context"; export async function revealInFile(view: ViewContext, task: Task): Promise { const file = view.app.vault.getAbstractFileByPath(task.path); if (!(file instanceof TFile)) return; const leaf = view.app.workspace.getLeaf("tab"); await leaf.openFile(file); const editor = view.app.workspace.activeEditor?.editor; if (editor) { editor.setCursor({ line: task.line, ch: 0 }); editor.scrollIntoView( { from: { line: task.line, ch: 0 }, to: { line: task.line, ch: 0 } }, true, ); } } export async function editInTasks(view: ViewContext, task: Task): Promise { const api = tasksApi(view.app); if (!api) { warnMissingTasks(); return; } const edited = await api.editTaskLineModal(task.raw); if (!edited || edited === task.raw) return; await replaceRaw(view.app, task, edited); } export function taskMenu(view: ViewContext, task: Task): MenuItem[] { const statuses: MenuItem[] = STATUS_ORDER.filter( (symbol) => symbol !== task.status, ).map((symbol) => ({ dot: STATUS_META[symbol].color, label: STATUS_META[symbol].label, onselect: () => void setStatus(view.app, task, symbol), })); return [ ...statuses, { icon: ScanEye, label: "Quick look", hint: "space", onselect: () => { view.store.cursorTask = task.id; view.store.keyboard = true; if (!view.store.peeking) view.store.peek(); }, }, { icon: PenLine, label: "Edit", hint: "e", onselect: () => { view.store.cursorTask = task.id; view.store.edit(task.id); }, }, { icon: SquarePen, label: "Edit in Tasks", onselect: () => void editInTasks(view, task), }, { icon: FileText, label: "Reveal in file", hint: task.sphere, onselect: () => void revealInFile(view, task), }, { icon: ExternalLink, label: "Copy raw line", onselect: () => { void navigator.clipboard.writeText(task.raw); new Notice("Beaver Calendar: line copied"); }, }, ]; }