|
| 1 | +import { LineOperations } from "../domain/LineOperations"; |
| 2 | +import { TodoStatus } from "../domain/TodoItem"; |
| 3 | +import { FileOperations } from "../domain/FileOperations"; |
| 4 | +import { ProletarianWizardSettings } from "../domain/ProletarianWizardSettings"; |
| 5 | +import { ObsidianFile } from "../infrastructure/ObsidianFile"; |
| 6 | +import { Command, Editor, Hotkey, MarkdownView, TFile, App } from "obsidian"; |
| 7 | +import { DateTime } from "luxon"; |
| 8 | + |
| 9 | +export class MoveToTodayCommand implements Command { |
| 10 | + constructor( |
| 11 | + private lineOperations: LineOperations, |
| 12 | + private settings?: ProletarianWizardSettings, |
| 13 | + private app?: App |
| 14 | + ) {} |
| 15 | + |
| 16 | + id: string = "pw-move-todo-to-today-command"; |
| 17 | + name: string = "Mark todo to today"; |
| 18 | + icon?: string = "dot"; |
| 19 | + mobileOnly?: boolean = false; |
| 20 | + callback?: () => any; |
| 21 | + checkCallback?: (checking: boolean) => boolean | void; |
| 22 | + editorCallback(editor: Editor, view: MarkdownView) { |
| 23 | + const lineNumber = editor.getCursor("from").line; |
| 24 | + let line = editor.getLine(lineNumber); |
| 25 | + const todo = this.lineOperations.toTodo(line, lineNumber); |
| 26 | + if (todo.isTodo) { |
| 27 | + const dueDateAttribute = this.settings?.dueDateAttribute || "due"; |
| 28 | + |
| 29 | + if (view.file && this.app) { |
| 30 | + const fileOperations = new FileOperations(this.settings); |
| 31 | + const obsidianFile = new ObsidianFile(this.app, view.file); |
| 32 | + const todoWithFile = { |
| 33 | + ...todo.todo, |
| 34 | + file: obsidianFile, |
| 35 | + line: lineNumber, |
| 36 | + }; |
| 37 | + |
| 38 | + fileOperations |
| 39 | + .updateAttributeAsync( |
| 40 | + todoWithFile, |
| 41 | + this.settings.dueDateAttribute, |
| 42 | + DateTime.now().toISODate() |
| 43 | + ) |
| 44 | + .then(() => { |
| 45 | + // File operations will handle the update |
| 46 | + }) |
| 47 | + .catch(console.error); |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + editorCheckCallback?: ( |
| 52 | + checking: boolean, |
| 53 | + editor: Editor, |
| 54 | + view: MarkdownView |
| 55 | + ) => boolean | void; |
| 56 | + hotkeys?: Hotkey[] = []; |
| 57 | +} |
0 commit comments