Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add created date to task #1723

Merged
11 changes: 11 additions & 0 deletions src/Commands/CreateOrEditTaskParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Priority, Task, TaskRegularExpressions } from '../Task';
import { DateFallback } from '../DateFallback';
import { StatusRegistry } from '../StatusRegistry';
import { TaskLocation } from '../TaskLocation';
import { getSettings } from '../Config/Settings';

/**
* Read any markdown line and treat it as a task, for the purposes of
Expand Down Expand Up @@ -30,12 +31,20 @@ export const taskFromLine = ({ line, path }: { line: string; path: string }): Ta
return task;
}

const { setCreatedDate } = getSettings();
let createdDate: moment.Moment | null = null;
if (setCreatedDate) {
console.dir(window.moment());
vanadium23 marked this conversation as resolved.
Show resolved Hide resolved
createdDate = window.moment();
}
claremacrae marked this conversation as resolved.
Show resolved Hide resolved

// If we are not on a line of a task, we take what we have.
// The non-task line can still be a checklist, for example if it is lacking the global filter.
const nonTaskMatch = line.match(TaskRegularExpressions.nonTaskRegex);
if (nonTaskMatch === null) {
// Should never happen; everything in the regex is optional.
console.error('Tasks: Cannot create task on line:', line);

return new Task({
status: Status.TODO,
description: '',
Expand All @@ -53,6 +62,7 @@ export const taskFromLine = ({ line, path }: { line: string; path: string }): Ta
tags: [],
originalMarkdown: '',
scheduledDateIsInferred: false,
createdDate,
});
}

Expand Down Expand Up @@ -88,5 +98,6 @@ export const taskFromLine = ({ line, path }: { line: string; path: string }): Ta
originalMarkdown: '',
// Not needed since the inferred status is always re-computed after submitting.
scheduledDateIsInferred: false,
createdDate,
});
};
2 changes: 2 additions & 0 deletions src/Config/Settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export interface Settings {
globalFilter: string;
removeGlobalFilter: boolean;
setDoneDate: boolean;
setCreatedDate: boolean;
autoSuggestInEditor: boolean;
autoSuggestMinMatch: number;
autoSuggestMaxItems: number;
Expand All @@ -43,6 +44,7 @@ const defaultSettings: Settings = {
globalFilter: '',
removeGlobalFilter: false,
setDoneDate: true,
setCreatedDate: false,
autoSuggestInEditor: true,
autoSuggestMinMatch: 0,
autoSuggestMaxItems: 6,
Expand Down
11 changes: 11 additions & 0 deletions src/Config/SettingsTab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,17 @@ export class SettingsTab extends PluginSettingTab {
});
});

new Setting(containerEl)
vanadium23 marked this conversation as resolved.
Show resolved Hide resolved
.setName('Set created date on every added task')
.setDesc('Enabling this will add a timestamp ➕ YYYY-MM-DD at the end when a task is created')
vanadium23 marked this conversation as resolved.
Show resolved Hide resolved
.addToggle((toggle) => {
const settings = getSettings();
toggle.setValue(settings.setCreatedDate).onChange(async (value) => {
updateSettings({ setCreatedDate: value });
await this.plugin.saveSettings();
});
});

new Setting(containerEl)
.setName('Use filename as Scheduled date for undated tasks')
.setDesc(
Expand Down
20 changes: 20 additions & 0 deletions src/Task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export const prioritySymbols = {

export const recurrenceSymbol = '🔁';
export const startDateSymbol = '🛫';
export const createdDateSymbol = '➕';
export const scheduledDateSymbol = '⏳';
export const dueDateSymbol = '📅';
export const doneDateSymbol = '✅';
Expand Down Expand Up @@ -92,6 +93,7 @@ export class TaskRegularExpressions {
// removed from the end until none are left.
public static readonly priorityRegex = /([⏫🔼🔽])$/u;
public static readonly startDateRegex = /🛫 *(\d{4}-\d{2}-\d{2})$/u;
public static readonly createdDateRegex = /➕ *(\d{4}-\d{2}-\d{2})$/u;
public static readonly scheduledDateRegex = /[⏳⌛] *(\d{4}-\d{2}-\d{2})$/u;
public static readonly dueDateRegex = /[📅📆🗓] *(\d{4}-\d{2}-\d{2})$/u;
public static readonly doneDateRegex = /✅ *(\d{4}-\d{2}-\d{2})$/u;
Expand Down Expand Up @@ -132,6 +134,7 @@ export class Task {
public readonly scheduledDate: Moment | null;
public readonly dueDate: Moment | null;
public readonly doneDate: Moment | null;
public readonly createdDate: Moment | null;
vanadium23 marked this conversation as resolved.
Show resolved Hide resolved

public readonly recurrence: Recurrence | null;
/** The blockLink is a "^" annotation after the dates/recurrence rules. */
Expand Down Expand Up @@ -163,6 +166,7 @@ export class Task {
tags,
originalMarkdown,
scheduledDateIsInferred,
createdDate,
}: {
status: Status;
description: string;
Expand All @@ -179,6 +183,7 @@ export class Task {
tags: string[] | [];
originalMarkdown: string;
scheduledDateIsInferred: boolean;
createdDate: moment.Moment | null;
vanadium23 marked this conversation as resolved.
Show resolved Hide resolved
}) {
this.status = status;
this.description = description;
Expand All @@ -194,6 +199,7 @@ export class Task {
this.scheduledDate = scheduledDate;
this.dueDate = dueDate;
this.doneDate = doneDate;
this.createdDate = createdDate;

this.recurrence = recurrence;
this.blockLink = blockLink;
Expand Down Expand Up @@ -264,6 +270,7 @@ export class Task {
let scheduledDateIsInferred = false;
let dueDate: Moment | null = null;
let doneDate: Moment | null = null;
let createdDate: Moment | null = null;
let recurrenceRule: string = '';
let recurrence: Recurrence | null = null;
let tags: any = [];
Expand Down Expand Up @@ -323,6 +330,13 @@ export class Task {
matched = true;
}

const createdDateMatch = description.match(TaskRegularExpressions.createdDateRegex);
if (createdDateMatch !== null) {
createdDate = window.moment(createdDateMatch[1], TaskRegularExpressions.dateFormat);
description = description.replace(TaskRegularExpressions.createdDateRegex, '').trim();
matched = true;
}

const recurrenceMatch = description.match(TaskRegularExpressions.recurrenceRegex);
if (recurrenceMatch !== null) {
// Save the recurrence rule, but *do not parse it yet*.
Expand Down Expand Up @@ -394,6 +408,7 @@ export class Task {
tags,
originalMarkdown: line,
scheduledDateIsInferred,
createdDate,
});
}

Expand Down Expand Up @@ -444,6 +459,11 @@ export class Task {
return layout.options.shortMode
? ' ' + startDateSymbol
: ` ${startDateSymbol} ${this.startDate.format(TaskRegularExpressions.dateFormat)}`;
case 'createdDate':
if (!this.createdDate) return '';
return layout.options.shortMode
? ' ' + createdDateSymbol
: ` ${createdDateSymbol} ${this.createdDate.format(TaskRegularExpressions.dateFormat)}`;
case 'scheduledDate':
if (!this.scheduledDate || this.scheduledDateIsInferred) return '';
return layout.options.shortMode
Expand Down
4 changes: 4 additions & 0 deletions src/TaskLayout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export class LayoutOptions {
hideTaskCount: boolean = false;
hideBacklinks: boolean = false;
hidePriority: boolean = false;
hideCreatedDate: boolean = false;
hideStartDate: boolean = false;
hideScheduledDate: boolean = false;
hideDoneDate: boolean = false;
Expand All @@ -20,6 +21,7 @@ export type TaskLayoutComponent =
| 'description'
| 'priority'
| 'recurrenceRule'
| 'createdDate'
vanadium23 marked this conversation as resolved.
Show resolved Hide resolved
| 'startDate'
| 'scheduledDate'
| 'dueDate'
Expand All @@ -36,6 +38,7 @@ export class TaskLayout {
'description',
'priority',
'recurrenceRule',
'createdDate',
'startDate',
'scheduledDate',
'dueDate',
Expand Down Expand Up @@ -81,6 +84,7 @@ export class TaskLayout {
let newComponents = this.layoutComponents;
newComponents = removeIf(newComponents, layoutOptions.hidePriority, 'priority');
newComponents = removeIf(newComponents, layoutOptions.hideRecurrenceRule, 'recurrenceRule');
newComponents = removeIf(newComponents, layoutOptions.hideCreatedDate, 'createdDate');
newComponents = removeIf(newComponents, layoutOptions.hideStartDate, 'startDate');
newComponents = removeIf(newComponents, layoutOptions.hideScheduledDate, 'scheduledDate');
newComponents = removeIf(newComponents, layoutOptions.hideDueDate, 'dueDate');
Expand Down
2 changes: 2 additions & 0 deletions src/TaskModal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ export class TaskModal extends Modal {
super(app);

this.task = task;
console.log(task);
this.onSubmit = (updatedTasks: Task[]) => {
console.log(updatedTasks);
vanadium23 marked this conversation as resolved.
Show resolved Hide resolved
updatedTasks.length && onSubmit(updatedTasks);
this.close();
};
Expand Down
2 changes: 2 additions & 0 deletions tests/Query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ describe('Query', () => {
tags: [],
originalMarkdown: '',
scheduledDateIsInferred: false,
createdDate: null,
}),
new Task({
status: Status.TODO,
Expand All @@ -310,6 +311,7 @@ describe('Query', () => {
tags: [],
originalMarkdown: '',
scheduledDateIsInferred: false,
createdDate: null,
}),
];
const input = 'path includes ab/c d';
Expand Down
2 changes: 2 additions & 0 deletions tests/TestingTools/TaskBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export class TaskBuilder {
private _scheduledDate: Moment | null = null;
private _dueDate: Moment | null = null;
private _doneDate: Moment | null = null;
private _createdDate: Moment | null = null;

private _recurrence: Recurrence | null = null;
private _blockLink: string = '';
Expand Down Expand Up @@ -83,6 +84,7 @@ export class TaskBuilder {
tags: this._tags,
originalMarkdown: '',
scheduledDateIsInferred: this._scheduledDateIsInferred,
createdDate: this._createdDate,
vanadium23 marked this conversation as resolved.
Show resolved Hide resolved
});
}

Expand Down