diff --git a/CHANGELOG.md b/CHANGELOG.md index fae0da7..5e35e91 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,6 @@ ## Planned - Add support for [task repeats](https://github.com/joshuatazrein/obsidian-time-ruler/issues/5#issuecomment-1646958839) - Option to [add tasks at start or end of headings](https://github.com/joshuatazrein/obsidian-time-ruler/issues/12) -- Right-click option to [schedule tasks for now](https://github.com/joshuatazrein/obsidian-time-ruler/issues/16#event-9959008621) - More specific Dataview custom filter [at task level](https://github.com/joshuatazrein/obsidian-time-ruler/issues/18) - Options to drag [deadlines and reminder times](https://github.com/joshuatazrein/obsidian-time-ruler/issues/20) in addition to scheduled time @@ -17,6 +16,7 @@ # Changelog ## 1.1.0 (Upcoming) +- **Added:** Right-click option to [schedule tasks for now](https://github.com/joshuatazrein/obsidian-time-ruler/issues/16#event-9959008621) and to unschedule tasks - **Added:** Support [emoji and custom status](https://github.com/joshuatazrein/obsidian-time-ruler/issues/26) displaying in tasks - **Added:** Filter by [custom status](https://github.com/joshuatazrein/obsidian-time-ruler/issues/25) - **Added:** A [simple mode](https://github.com/joshuatazrein/obsidian-time-ruler/issues/21) with `HH:mm-HH:mm` formatting for scheduled times. diff --git a/src/main.ts b/src/main.ts index fc15508..a9c180e 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,8 +1,19 @@ -import { App, MarkdownFileInfo, MarkdownView, Notice, Plugin } from 'obsidian' +import { DateTime } from 'luxon' +import { + App, + MarkdownFileInfo, + MarkdownView, + Menu, + Notice, + Plugin, + setIcon, +} from 'obsidian' import { getAPI } from 'obsidian-dataview' import TimeRulerView, { TIME_RULER_VIEW } from './index' import SettingsTab from './plugin/SettingsTab' import { openTaskInRuler } from './services/obsidianApi' +import { taskToText, textToTask } from './services/parser' +import { getters, setters } from './app/store' interface TimeRulerSettings { calendars: string[] @@ -85,10 +96,10 @@ export default class TimeRulerPlugin extends Plugin { openTaskInRuler(cursor.line, path) } - openMenu(menu, context) { + openMenu(menu: Menu, context: MarkdownView | MarkdownFileInfo) { const cursor = context.editor?.getCursor() - if (!cursor) return - const line = context.editor?.getLine(cursor.line) + if (!cursor || !(context instanceof MarkdownView)) return + const line = context.editor.getLine(cursor.line) if (!line || !/ *- \[ \] /.test(line)) return menu.addItem((item) => item @@ -96,6 +107,41 @@ export default class TimeRulerPlugin extends Plugin { .setTitle('Reveal in Time Ruler') .onClick(() => this.jumpToTask(context)) ) + menu.addItem((item) => + item + .setIcon('ruler') + .setTitle('Do now') + .onClick(() => this.editTask(context, cursor.line, 'now')) + ) + menu.addItem((item) => + item + .setIcon('ruler') + .setTitle('Unschedule') + .onClick(() => this.editTask(context, cursor.line, 'unschedule')) + ) + } + + async editTask( + context: MarkdownView, + line: number, + modification: 'now' | 'unschedule' + ) { + const id = context.file.path.replace('.md', '') + '::' + line + let scheduled: TaskProps['scheduled'] + switch (modification) { + case 'now': + let now = DateTime.now().startOf('minute') + while (now.minute % 15 !== 0) now = now.plus({ minute: 1 }) + scheduled = now.toISO({ + includeOffset: false, + suppressMilliseconds: true, + suppressSeconds: true, + }) as string + break + case 'unschedule': + scheduled = '' + } + setters.patchTasks([id], { scheduled }) } async activateView() { diff --git a/src/services/parser.ts b/src/services/parser.ts index f42016e..4fd9f7f 100644 --- a/src/services/parser.ts +++ b/src/services/parser.ts @@ -32,7 +32,7 @@ export function textToTask(item: any): TaskProps { ` ?${keyToTasksEmoji.reminder} ?(${ISO_MATCH}( \\d{2}:\\d{2})?)|\\(@(\\d{4}-\\d{2}-\\d{2}( \\d{2}:\\d{2})?)\\)|@\\{(\\d{4}-\\d{2}-\\d{2}( \\d{2}:\\d{2})?)\\}` ) const SIMPLE_SCHEDULED = /^\d+:\d+( ?- ?\d+:\d+)?/ - const SIMPLE_PRIORITY = / (?|!|!!|!!!)$/ + const SIMPLE_PRIORITY = / (\?|\!{1,3})$/ const SIMPLE_DUE = / > (\d{4}-d{2}-d{2})/ const titleLine: string = item.text.match(/(.*?)(\n|$)/)?.[1] ?? ''