diff --git a/src/ui/hooks/use-edit/create-edit-handlers.ts b/src/ui/hooks/use-edit/create-edit-handlers.ts index afc246599..53847defa 100644 --- a/src/ui/hooks/use-edit/create-edit-handlers.ts +++ b/src/ui/hooks/use-edit/create-edit-handlers.ts @@ -4,7 +4,7 @@ import { get, Readable, Writable } from "svelte/store"; import { ObsidianFacade } from "../../../service/obsidian-facade"; import { PlacedTask, UnscheduledTask } from "../../../types"; -import { createTask } from "../../../util/task-utils"; +import { createTask, getSchedueldDayFromText } from "../../../util/task-utils"; import { EditMode, EditOperation } from "./types"; @@ -55,12 +55,17 @@ export function createEditHandlers({ const withAddedTime = { ...task, startMinutes: get(cursorMinutes), - // todo: add a proper fix - startTime: task.location - ? getDateFromPath(task.location.path, "day") || window.moment() - : window.moment(), + startTime: + getSchedueldDayFromText(task) || + getDateFromPath(task.location.path, "day"), }; + if (!withAddedTime.startTime) { + throw new Error( + "Could not get the day of the task. It should be provided through the Tasks Plugins schduled feature, or the path of the daily note. If you did provide the day through one of these options, please report the issue at https://github.com/ivan-lednev/obsidian-day-planner/issues", + ); + } + startEdit({ task: withAddedTime, mode: EditMode.DRAG, day }); } diff --git a/src/util/task-utils.ts b/src/util/task-utils.ts index 3ea1fad41..314957528 100644 --- a/src/util/task-utils.ts +++ b/src/util/task-utils.ts @@ -9,7 +9,7 @@ import { scheduledPropRegExp, shortScheduledPropRegExp, } from "../regexp"; -import type { Task } from "../types"; +import type { Task, UnscheduledTask } from "../types"; import { PlacedTask } from "../types"; import { getId } from "./id"; @@ -131,3 +131,18 @@ export function createTask(day: Moment, startMinutes: number): PlacedTask { }, }; } + +export function getSchedueldDayFromText(task: UnscheduledTask) { + const matches = + scheduledPropRegExp.exec(task.text) || + keylessScheduledPropRegExp.exec(task.text) || + shortScheduledPropRegExp.exec(task.text); + + if (!matches) { + return undefined; + } + const [fullMatch, preFix] = matches; + const scheduledDay = fullMatch.replace(preFix, ""); + + return window.moment(scheduledDay); +}