From 48a123d14abc3b33e3a2d081257fea0490dcada5 Mon Sep 17 00:00:00 2001 From: martin-forge <228563004+martin-forge@users.noreply.github.com> Date: Fri, 20 Nov 2026 02:11:27 +0000 Subject: [PATCH 1/2] fix(calendar): confirm before a calendar move clears recurring instances Moving a recurring task's next occurrence in the Calendar view, or dropping the task onto the calendar from a note, deleted its completed and skipped instances on or after the new date without asking. Both paths reschedule without the confirmClearInstances hook that the scheduled-date picker and property updates already pass. Pass the hook on both paths, so the dialog lists the dates it will clear. Cancel reverts the calendar drag; for an external drop nothing is written, refreshed or offered for Undo. Make confirmClearRescheduledInstances public so the calendar can reuse it. Batch moves refuse recurring tasks, so they are unchanged. --- docs/features/recurring-tasks.md | 2 +- docs/releases/unreleased.md | 2 + src/bases/CalendarView.ts | 16 +- src/bases/calendarMutations.ts | 4 +- src/main.ts | 2 +- ...calendarRecurringRescheduleConfirm.test.ts | 185 ++++++++++++++++++ 6 files changed, 206 insertions(+), 5 deletions(-) create mode 100644 tests/unit/bases/calendarRecurringRescheduleConfirm.test.ts diff --git a/docs/features/recurring-tasks.md b/docs/features/recurring-tasks.md index ce9341ce8..ed16f123c 100644 --- a/docs/features/recurring-tasks.md +++ b/docs/features/recurring-tasks.md @@ -259,7 +259,7 @@ This means completion history and next-action planning stay synchronized automat ### Rescheduling clears later instances -When you change a recurring task's scheduled date (from the context menu or the scheduled-date field), TaskNotes restarts the timeline from the new date: it clears every completed and skipped instance dated **on or after** that date, while keeping older instances as history. This reactivates occurrences you had completed or skipped off-schedule so they can be acted on again. Because clearing is destructive, TaskNotes asks you to confirm first — listing the dates that will be cleared — and cancelling leaves both the schedule and the recorded instances untouched. +When you change a recurring task's scheduled date (from the context menu or the scheduled-date field, by dragging its next occurrence to another day in the Calendar view, or by dropping the task onto the calendar from a note), TaskNotes restarts the timeline from the new date: it clears every completed and skipped instance dated **on or after** that date, while keeping older instances as history. This reactivates occurrences you had completed or skipped off-schedule so they can be acted on again. Because clearing is destructive, TaskNotes asks you to confirm first — listing the dates that will be cleared — and cancelling leaves both the schedule and the recorded instances untouched. ## Flexible Scheduling diff --git a/docs/releases/unreleased.md b/docs/releases/unreleased.md index 9c45ebfb1..83a768cf3 100644 --- a/docs/releases/unreleased.md +++ b/docs/releases/unreleased.md @@ -49,3 +49,5 @@ When a change has user-facing documentation, include a canonical tasknotes.dev l - Thanks to @BrucePlumb for reporting - (#2624) Fixed the tops of the Sort, Group, Filter, Properties and Search labels being cut off in the Relationships widget toolbar when the toolbar wrapped onto two lines. The toolbar's header now grows to fit it, in the Relationships widget and in other TaskNotes Bases views. - Thanks to @prepare4robots for reporting +- Fixed moving a recurring task in the Calendar view clearing its completed and skipped occurrences without asking. Dragging the next occurrence to another day, or dropping the task onto the calendar from elsewhere, such as a task link in a note, removed every completed or skipped occurrence on or after the new date. The calendar now asks first and lists those dates, as changing the scheduled date from the date picker does; **Cancel** leaves the task where it was, with its history. See [Recurring Tasks](https://tasknotes.dev/features/recurring-tasks/#rescheduling-clears-later-instances). + - Thanks to @martin-forge for the fix diff --git a/src/bases/CalendarView.ts b/src/bases/CalendarView.ts index 1b6270716..b4140b72f 100644 --- a/src/bases/CalendarView.ts +++ b/src/bases/CalendarView.ts @@ -2208,8 +2208,20 @@ export class CalendarView extends BasesViewBase { return; } this.expectImmediateUpdate(); - const receipt = await captureTaskUndo(this.plugin, task, () => this.plugin.taskService.updateProperty(task, "scheduled", arg.startStr)); - if (receipt.task) await this.refreshAfterDirectCalendarTaskWrite(receipt.task); + // Declining the clear leaves the task as it was, so there is nothing to refresh or undo. + let declined = false; + const receipt = await captureTaskUndo(this.plugin, task, async () => { + const updated = await this.plugin.taskService.updateProperty(task, "scheduled", arg.startStr, { + confirmClearInstances: async (cleared) => { + const proceed = await this.plugin.confirmClearRescheduledInstances(cleared); + declined = !proceed; + return proceed; + }, + }); + return declined ? null : updated; + }); + if (!receipt.task) return; + await this.refreshAfterDirectCalendarTaskWrite(receipt.task); showTaskUndo(receipt.undo ? [receipt.undo] : [], false, 1, this.plugin.app.vault, task.title); } catch (error) { tasknotesLogger.error("[TaskNotes][CalendarView] Error scheduling dropped task", { diff --git a/src/bases/calendarMutations.ts b/src/bases/calendarMutations.ts index bdbda59cf..77013f20c 100644 --- a/src/bases/calendarMutations.ts +++ b/src/bases/calendarMutations.ts @@ -285,7 +285,9 @@ async function applyTaskCalendarGesture( end: info.event.end?.toISOString(), allDay: info.event.allDay, }, - context.timezone + context.timezone, + // Declining returns null, which reverts the gesture below and keeps the recorded instances. + { confirmClearInstances: (cleared) => context.plugin.confirmClearRescheduledInstances(cleared) } )); if (result.task) { await context.refreshTask(result.task); diff --git a/src/main.ts b/src/main.ts index b9e846521..dfea2300e 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1285,7 +1285,7 @@ export default class TaskNotesPlugin extends Plugin { * Ask the user to confirm clearing recorded completed/skipped instances that a * reschedule would remove (those on or after the new scheduled date). */ - private async confirmClearRescheduledInstances(cleared: { + async confirmClearRescheduledInstances(cleared: { complete: string[]; skipped: string[]; }): Promise { diff --git a/tests/unit/bases/calendarRecurringRescheduleConfirm.test.ts b/tests/unit/bases/calendarRecurringRescheduleConfirm.test.ts new file mode 100644 index 000000000..2fb9b2666 --- /dev/null +++ b/tests/unit/bases/calendarRecurringRescheduleConfirm.test.ts @@ -0,0 +1,185 @@ +// Calendar planning needs the real recurrence parser, and the drop feedback formats real dates. +jest.mock("rrule", () => jest.requireActual("rrule/dist/es5/rrule.js")); +jest.mock("date-fns", () => jest.requireActual("../../../node_modules/date-fns")); +jest.mock("../../../src/ui/taskMutationUndo", () => ({ + captureTaskUndo: async (_plugin: unknown, _task: unknown, operation: () => Promise) => ({ + task: await operation(), + }), +})); +jest.mock("../../../src/ui/taskPropertyPickers", () => ({ showTaskUndo: jest.fn() })); + +import { buildCalendarEntries } from "@tasknotes/model/calendar"; +import { CalendarView } from "../../../src/bases/CalendarView"; +import { eventApiFromItem, type CalendarEventChange } from "../../../src/bases/calendarEngine"; +import { + handleCalendarEventDrop, + type CalendarMutationContext, +} from "../../../src/bases/calendarMutations"; +import type TaskNotesPlugin from "../../../src/main"; +import { TaskService } from "../../../src/services/TaskService"; +import { showTaskUndo } from "../../../src/ui/taskPropertyPickers"; +import type { TaskInfo } from "../../../src/types"; +import { TASK_PATH_DRAG_TYPE } from "../../../src/utils/DragDropManager"; +import { PluginFactory, TaskFactory } from "../../helpers/mock-factories"; + +/** + * A weekly task whose next occurrence is Sunday 22 November, with the previous + * Sunday completed and the following Sunday skipped. Moving it to Saturday 21 + * November would clear the skipped 29 November. + */ +function weeklyTask(): TaskInfo { + return TaskFactory.createTask({ + id: "weekly", + path: "Tasks/Weekly review.md", + title: "Weekly review", + scheduled: "2026-11-22", + recurrence: "DTSTART:20261101;FREQ=WEEKLY", + complete_instances: ["2026-11-15"], + skipped_instances: ["2026-11-29"], + }); +} + +function harness(confirmed: boolean) { + const task = weeklyTask(); + const frontmatter: Record = { + scheduled: task.scheduled, + recurrence: task.recurrence, + complete_instances: [...(task.complete_instances ?? [])], + skipped_instances: [...(task.skipped_instances ?? [])], + }; + const confirm = jest.fn(async () => confirmed); + const plugin = PluginFactory.createMockPlugin({ + settings: { calendarViewSettings: { timeFormat: "24" } }, + cacheManager: { + getTaskInfo: jest.fn(async () => task), + updateTaskInfoInCache: jest.fn(), + }, + taskCalendarSyncService: { isEnabled: () => false }, + confirmClearRescheduledInstances: confirm, + }); + plugin.app.fileManager.processFrontMatter = jest.fn( + async (_file: unknown, update: (frontmatter: Record) => void) => { + update(frontmatter); + } + ); + plugin.taskService = new TaskService(plugin); + return { task, frontmatter, confirm, plugin: plugin as TaskNotesPlugin }; +} + +/** Drags the task's next occurrence to Saturday 21 November in the calendar grid. */ +function dragNextOccurrence(task: TaskInfo): CalendarEventChange { + const [entry] = buildCalendarEntries({ + tasks: [task], + timezone: "UTC", + now: "2026-11-20T12:00:00Z", + range: { start: "2026-11-16", end: "2026-11-23" }, + }).filter((candidate) => candidate.recurringKind === "next-scheduled"); + const oldEvent = eventApiFromItem({ + id: entry.id, + title: entry.title, + start: entry.start, + end: entry.end, + allDay: entry.allDay, + data: { modelEntry: entry, taskInfo: task, eventType: entry.type }, + }); + return { + oldEvent, + event: { ...oldEvent, start: new Date("2026-11-21T00:00:00Z"), end: null }, + isPointer: true, + jsEvent: null, + revert: jest.fn(), + }; +} + +/** Drops the task on Saturday 21 November from outside the calendar, such as from a task link in a note. */ +async function dropFromOutside(plugin: TaskNotesPlugin, task: TaskInfo) { + const view = Object.assign(Object.create(CalendarView.prototype), { + plugin, + expectImmediateUpdate: jest.fn(), + refreshAfterDirectCalendarTaskWrite: jest.fn(), + }) as CalendarView & { refreshAfterDirectCalendarTaskWrite: jest.Mock }; + const dataTransfer = { + types: [TASK_PATH_DRAG_TYPE], + getData: (type: string) => (type === TASK_PATH_DRAG_TYPE ? task.path : ""), + } as unknown as DataTransfer; + await (view as unknown as { + handleExternalTaskDrop(arg: { dataTransfer: DataTransfer; startStr: string }): Promise; + }).handleExternalTaskDrop({ dataTransfer, startStr: "2026-11-21" }); + return view; +} + +function mutationContext(plugin: TaskNotesPlugin): CalendarMutationContext { + return { + plugin, + options: { startDateProperty: null, endDateProperty: null }, + timezone: "UTC", + refreshTask: jest.fn(), + refreshEvent: jest.fn(), + }; +} + +describe("calendar reschedules that would clear a recurring task's recorded instances", () => { + describe("dragging the next occurrence within the calendar", () => { + it("asks first, and Cancel reverts the drop and keeps the schedule and its history", async () => { + const { task, frontmatter, confirm, plugin } = harness(false); + const info = dragNextOccurrence(task); + + await handleCalendarEventDrop(mutationContext(plugin), info); + + expect(confirm).toHaveBeenCalledWith({ complete: [], skipped: ["2026-11-29"] }); + expect(info.revert).toHaveBeenCalled(); + expect(plugin.app.fileManager.processFrontMatter).not.toHaveBeenCalled(); + expect(frontmatter).toMatchObject({ + scheduled: "2026-11-22", + complete_instances: ["2026-11-15"], + skipped_instances: ["2026-11-29"], + }); + expect(showTaskUndo).not.toHaveBeenCalled(); + }); + + it("clears the listed instances once confirmed", async () => { + const { task, frontmatter, confirm, plugin } = harness(true); + const info = dragNextOccurrence(task); + + await handleCalendarEventDrop(mutationContext(plugin), info); + + expect(confirm).toHaveBeenCalledWith({ complete: [], skipped: ["2026-11-29"] }); + expect(info.revert).not.toHaveBeenCalled(); + expect(frontmatter.scheduled).toBe("2026-11-21"); + expect(frontmatter.complete_instances).toEqual(["2026-11-15"]); + expect(frontmatter.skipped_instances ?? []).toEqual([]); + expect(showTaskUndo).toHaveBeenCalledTimes(1); + }); + }); + + describe("dropping the task into the calendar from outside it", () => { + it("asks first, and Cancel keeps the schedule and its history", async () => { + const { task, frontmatter, confirm, plugin } = harness(false); + + const view = await dropFromOutside(plugin, task); + + expect(confirm).toHaveBeenCalledWith({ complete: [], skipped: ["2026-11-29"] }); + expect(plugin.app.fileManager.processFrontMatter).not.toHaveBeenCalled(); + expect(frontmatter).toMatchObject({ + scheduled: "2026-11-22", + complete_instances: ["2026-11-15"], + skipped_instances: ["2026-11-29"], + }); + expect(view.refreshAfterDirectCalendarTaskWrite).not.toHaveBeenCalled(); + expect(showTaskUndo).not.toHaveBeenCalled(); + }); + + it("clears the listed instances once confirmed", async () => { + const { task, frontmatter, confirm, plugin } = harness(true); + + const view = await dropFromOutside(plugin, task); + + expect(confirm).toHaveBeenCalledWith({ complete: [], skipped: ["2026-11-29"] }); + expect(frontmatter.scheduled).toBe("2026-11-21"); + expect(frontmatter.complete_instances).toEqual(["2026-11-15"]); + expect(frontmatter.skipped_instances ?? []).toEqual([]); + expect(view.refreshAfterDirectCalendarTaskWrite).toHaveBeenCalledTimes(1); + expect(showTaskUndo).toHaveBeenCalledTimes(1); + }); + }); +}); From 6cbe4de68d65b0914d621c52cec602aaa2966864 Mon Sep 17 00:00:00 2001 From: martin-forge <228563004+martin-forge@users.noreply.github.com> Date: Fri, 27 Nov 2026 01:26:48 +0000 Subject: [PATCH 2/2] test(calendar): cover the moves that still clear after #2724 Since #2724 a scheduled-anchored move clears nothing when it records recurrence_original_date, so the drop tests no longer reached the confirmation. Use a completion-anchored task, which still clears every instance on or after the new date under both the current and the narrowed model rule, and add a Google-synced drop onto its skipped day, which clears only that day. --- ...calendarRecurringRescheduleConfirm.test.ts | 55 +++++++++++++++---- 1 file changed, 43 insertions(+), 12 deletions(-) diff --git a/tests/unit/bases/calendarRecurringRescheduleConfirm.test.ts b/tests/unit/bases/calendarRecurringRescheduleConfirm.test.ts index 2fb9b2666..752d981ff 100644 --- a/tests/unit/bases/calendarRecurringRescheduleConfirm.test.ts +++ b/tests/unit/bases/calendarRecurringRescheduleConfirm.test.ts @@ -24,10 +24,9 @@ import { PluginFactory, TaskFactory } from "../../helpers/mock-factories"; /** * A weekly task whose next occurrence is Sunday 22 November, with the previous - * Sunday completed and the following Sunday skipped. Moving it to Saturday 21 - * November would clear the skipped 29 November. + * Sunday completed and the following Sunday skipped. */ -function weeklyTask(): TaskInfo { +function weeklyTask(overrides: Partial): TaskInfo { return TaskFactory.createTask({ id: "weekly", path: "Tasks/Weekly review.md", @@ -36,11 +35,27 @@ function weeklyTask(): TaskInfo { recurrence: "DTSTART:20261101;FREQ=WEEKLY", complete_instances: ["2026-11-15"], skipped_instances: ["2026-11-29"], + ...overrides, }); } -function harness(confirmed: boolean) { - const task = weeklyTask(); +/** + * A completion-anchored move clears every recorded instance on or after the new + * date, so moving this task to Saturday 21 November clears the skipped 29 November. + */ +function completionAnchoredTask(): TaskInfo { + return weeklyTask({ recurrence_anchor: "completion" }); +} + +/** + * A task synced to Google Calendar gets no `recurrence_original_date` when it moves, + * so a move clears the day it lands on if that day is already completed or skipped. + */ +function syncedTask(): TaskInfo { + return weeklyTask({ googleCalendarEventId: "gcal-1" }); +} + +function harness(task: TaskInfo, confirmed: boolean) { const frontmatter: Record = { scheduled: task.scheduled, recurrence: task.recurrence, @@ -91,8 +106,8 @@ function dragNextOccurrence(task: TaskInfo): CalendarEventChange { }; } -/** Drops the task on Saturday 21 November from outside the calendar, such as from a task link in a note. */ -async function dropFromOutside(plugin: TaskNotesPlugin, task: TaskInfo) { +/** Drops the task on a day, Saturday 21 November by default, from outside the calendar, such as from a task link in a note. */ +async function dropFromOutside(plugin: TaskNotesPlugin, task: TaskInfo, startStr = "2026-11-21") { const view = Object.assign(Object.create(CalendarView.prototype), { plugin, expectImmediateUpdate: jest.fn(), @@ -104,7 +119,7 @@ async function dropFromOutside(plugin: TaskNotesPlugin, task: TaskInfo) { } as unknown as DataTransfer; await (view as unknown as { handleExternalTaskDrop(arg: { dataTransfer: DataTransfer; startStr: string }): Promise; - }).handleExternalTaskDrop({ dataTransfer, startStr: "2026-11-21" }); + }).handleExternalTaskDrop({ dataTransfer, startStr }); return view; } @@ -121,7 +136,7 @@ function mutationContext(plugin: TaskNotesPlugin): CalendarMutationContext { describe("calendar reschedules that would clear a recurring task's recorded instances", () => { describe("dragging the next occurrence within the calendar", () => { it("asks first, and Cancel reverts the drop and keeps the schedule and its history", async () => { - const { task, frontmatter, confirm, plugin } = harness(false); + const { task, frontmatter, confirm, plugin } = harness(completionAnchoredTask(), false); const info = dragNextOccurrence(task); await handleCalendarEventDrop(mutationContext(plugin), info); @@ -138,7 +153,7 @@ describe("calendar reschedules that would clear a recurring task's recorded inst }); it("clears the listed instances once confirmed", async () => { - const { task, frontmatter, confirm, plugin } = harness(true); + const { task, frontmatter, confirm, plugin } = harness(completionAnchoredTask(), true); const info = dragNextOccurrence(task); await handleCalendarEventDrop(mutationContext(plugin), info); @@ -154,7 +169,7 @@ describe("calendar reschedules that would clear a recurring task's recorded inst describe("dropping the task into the calendar from outside it", () => { it("asks first, and Cancel keeps the schedule and its history", async () => { - const { task, frontmatter, confirm, plugin } = harness(false); + const { task, frontmatter, confirm, plugin } = harness(completionAnchoredTask(), false); const view = await dropFromOutside(plugin, task); @@ -170,7 +185,7 @@ describe("calendar reschedules that would clear a recurring task's recorded inst }); it("clears the listed instances once confirmed", async () => { - const { task, frontmatter, confirm, plugin } = harness(true); + const { task, frontmatter, confirm, plugin } = harness(completionAnchoredTask(), true); const view = await dropFromOutside(plugin, task); @@ -181,5 +196,21 @@ describe("calendar reschedules that would clear a recurring task's recorded inst expect(view.refreshAfterDirectCalendarTaskWrite).toHaveBeenCalledTimes(1); expect(showTaskUndo).toHaveBeenCalledTimes(1); }); + + it("asks before clearing the recorded day a move without recurrence_original_date lands on", async () => { + const { task, frontmatter, confirm, plugin } = harness(syncedTask(), false); + + const view = await dropFromOutside(plugin, task, "2026-11-29"); + + expect(confirm).toHaveBeenCalledWith({ complete: [], skipped: ["2026-11-29"] }); + expect(plugin.app.fileManager.processFrontMatter).not.toHaveBeenCalled(); + expect(frontmatter).toMatchObject({ + scheduled: "2026-11-22", + complete_instances: ["2026-11-15"], + skipped_instances: ["2026-11-29"], + }); + expect(view.refreshAfterDirectCalendarTaskWrite).not.toHaveBeenCalled(); + expect(showTaskUndo).not.toHaveBeenCalled(); + }); }); });