Skip to content

Commit

Permalink
Merge pull request #2274 from obsidian-tasks-group/fix-issue-2112-cre…
Browse files Browse the repository at this point in the history
…ated-date

fix: 'Create or edit task' now adds Created date if adding global filter
  • Loading branch information
claremacrae authored Sep 21, 2023
2 parents bd19f8e + 57b341f commit 9fc111a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
15 changes: 10 additions & 5 deletions src/Commands/CreateOrEditTaskParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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;
}

/**
Expand Down
14 changes: 14 additions & 0 deletions tests/Commands/CreateOrEditTaskParser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ describe('CreateOrEditTaskParser - created date', () => {
afterEach(() => {
jest.useRealTimers();
resetSettings();
GlobalFilter.reset();
});

it.each([
Expand Down Expand Up @@ -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'));
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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' =>
Expand Down

0 comments on commit 9fc111a

Please sign in to comment.