Skip to content

Commit

Permalink
update right-click options
Browse files Browse the repository at this point in the history
  • Loading branch information
Joshua Tazman Reinier committed Aug 9, 2023
1 parent 2f8a399 commit 4456c75
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 6 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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.
Expand Down
54 changes: 50 additions & 4 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -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[]
Expand Down Expand Up @@ -85,17 +96,52 @@ 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
.setIcon('ruler')
.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() {
Expand Down
2 changes: 1 addition & 1 deletion src/services/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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] ?? ''
Expand Down

0 comments on commit 4456c75

Please sign in to comment.