diff --git a/src/Commands/CreateOrEditTaskParser.ts b/src/Commands/CreateOrEditTaskParser.ts index 78cba913f9..f21823dc37 100644 --- a/src/Commands/CreateOrEditTaskParser.ts +++ b/src/Commands/CreateOrEditTaskParser.ts @@ -4,6 +4,7 @@ import { DateFallback } from '../DateFallback'; import { StatusRegistry } from '../StatusRegistry'; import { TaskLocation } from '../TaskLocation'; import { getSettings } from '../Config/Settings'; +import { GlobalFilter } from '../Config/GlobalFilter'; function getDefaultCreatedDate() { const { setCreatedDate } = getSettings(); @@ -23,12 +24,16 @@ function shouldUpdateCreatedDateForTask(task: Task) { return false; } - if (task.description !== '') { - // The task already had a description, so had been created previously: don't change it. - return false; - } + // If the description was empty, treat it as new and add a creation date. + const descriptionIsEmpty = task.description === ''; + + // If the global filter will be added when the task is saved, treat it as new and add a creation date. + // See issue #2112. + const globalFilterEnabled = !GlobalFilter.isEmpty(); + const taskDoesNotContainGlobalFilter = !GlobalFilter.includedIn(task.description); + const needsGlobalFilterToBeAdded = globalFilterEnabled && taskDoesNotContainGlobalFilter; - return true; + return descriptionIsEmpty || needsGlobalFilterToBeAdded; } /** diff --git a/tests/Commands/CreateOrEditTaskParser.test.ts b/tests/Commands/CreateOrEditTaskParser.test.ts index 547fc52f08..a4a7488122 100644 --- a/tests/Commands/CreateOrEditTaskParser.test.ts +++ b/tests/Commands/CreateOrEditTaskParser.test.ts @@ -107,6 +107,7 @@ describe('CreateOrEditTaskParser - created date', () => { afterEach(() => { jest.useRealTimers(); resetSettings(); + GlobalFilter.reset(); }); it.each([ @@ -151,4 +152,17 @@ describe('CreateOrEditTaskParser - created date', () => { expect(task.toFileLineString()).toStrictEqual('- [ ] hope created date will not be added'); expect(task.createdDate).toEqual(null); }); + + it('should add created date if adding the global filter', () => { + updateSettings({ setCreatedDate: true }); + GlobalFilter.set('#task'); + const path = 'a/b/c.md'; + const line = '- [ ] did not have the global filter'; + + const task = taskFromLine({ line, path }); + + // The global filter doesn't get added until the Modal rewrites the line + expect(task.toFileLineString()).toStrictEqual('- [ ] did not have the global filter ➕ 2023-09-17'); + expect(task.createdDate).toEqualMoment(moment('2023-09-17')); + }); }); diff --git a/tests/EditTask.test.Exhaustive_editing_Edit_and_save_All_inputs.approved.txt b/tests/EditTask.test.Exhaustive_editing_Edit_and_save_All_inputs.approved.txt index b8dce19b58..18062dd7fb 100644 --- a/tests/EditTask.test.Exhaustive_editing_Edit_and_save_All_inputs.approved.txt +++ b/tests/EditTask.test.Exhaustive_editing_Edit_and_save_All_inputs.approved.txt @@ -151,7 +151,7 @@ KEY: (globalFilter, set created date) ('#task', true) '- [ ] checkbox with initial description' => - '- [ ] #task checkbox with initial description' + '- [ ] #task checkbox with initial description ➕ 2023-07-18' ('#task', true) '- [ ] checkbox with initial description and created date ➕ 2023-01-01' =>