Skip to content

Commit

Permalink
Render due dates
Browse files Browse the repository at this point in the history
  • Loading branch information
larslockefeer authored Dec 31, 2021
1 parent a801664 commit bb2fb97
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 0 deletions.
8 changes: 8 additions & 0 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { TodoItem, TodoItemStatus } from './model/TodoItem';
import { TodoIndex } from './model/TodoIndex';
import { TodoPluginSettings, DEFAULT_SETTINGS } from './model/TodoPluginSettings';
import { SettingsTab } from './ui/SettingsTab';
import { DateFormatter } from 'util/DateFormatter';

export default class TodoPlugin extends Plugin {
private todoIndex: TodoIndex;
Expand All @@ -23,6 +24,7 @@ export default class TodoPlugin extends Plugin {
this.registerView(VIEW_TYPE_TODO, (leaf: WorkspaceLeaf) => {
const todos: TodoItem[] = [];
const props = {
dateFormatter: new DateFormatter(this.settings.dateFormat),
todos: todos,
openFile: (filePath: string) => {
const file = this.app.vault.getAbstractFileByPath(filePath) as TFile;
Expand Down Expand Up @@ -65,6 +67,12 @@ export default class TodoPlugin extends Plugin {

async updateSettings(settings: TodoPluginSettings): Promise<void> {
this.settings = settings;
this.view.setProps((currentProps: TodoItemViewProps) => {
return {
...currentProps,
dateFormatter: new DateFormatter(this.settings.dateFormat),
};
});
await this.saveData(this.settings);
this.todoIndex.setSettings(settings);
}
Expand Down
12 changes: 12 additions & 0 deletions styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,18 @@ div.todo-item-view-item p {
margin: 0;
}

div.todo-item-view-item span.due-date {
padding: 5px 8px 5px 8px;
background-color: #fbb034;
font-size: 0.6em;
border-color: #fbb034;
border-radius: 12px;
}

div.todo-item-view-item span.due-date.overdue {
background-color: #ff2400;
}

div.todo-item-view-item-checkbox {
flex-basis: 25px;
flex-shrink: 0;
Expand Down
10 changes: 10 additions & 0 deletions ui/TodoItemView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ItemView, MarkdownRenderer, WorkspaceLeaf } from 'obsidian';
import { VIEW_TYPE_TODO } from '../constants';
import { TodoItem, TodoItemStatus } from '../model/TodoItem';
import { RenderIcon, Icon } from '../ui/icons';
import { DateFormatter } from '../util/DateFormatter';

enum TodoItemViewPane {
Today,
Expand All @@ -11,6 +12,7 @@ enum TodoItemViewPane {
Someday,
}
export interface TodoItemViewProps {
dateFormatter: DateFormatter;
todos: TodoItem[];
openFile: (filePath: string) => void;
toggleTodo: (todo: TodoItem, newStatus: TodoItemStatus) => void;
Expand Down Expand Up @@ -118,6 +120,14 @@ export class TodoItemView extends ItemView {
});
el.createDiv('todo-item-view-item-description', (el) => {
MarkdownRenderer.renderMarkdown(todo.description, el, todo.sourceFilePath, this);
if (todo.actionDate) {
el.createSpan('due-date', (el) => {
if (todo.actionDate.startOf('day') < DateTime.now().startOf('day')) {
el.classList.add('overdue');
}
el.setText(this.props.dateFormatter.formatDate(todo.actionDate));
});
}
});
el.createDiv('todo-item-view-item-link', (el) => {
el.appendChild(RenderIcon(Icon.Reveal, 'Open file'));
Expand Down
11 changes: 11 additions & 0 deletions util/DateFormatter.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { DateTime } from 'luxon';
import { DateFormatter } from './DateFormatter';

test('Formatting dates works', () => {
const firstDateFormatter = new DateFormatter('yyyy-MM-dd');
const date = DateTime.fromISO('2021-12-29');
expect(firstDateFormatter.formatDate(date)).toBe('2021-12-29');

const secondDateFormatter = new DateFormatter('yyyy LLL dd');
expect(secondDateFormatter.formatDate(date)).toBe('2021 Dec 29');
});
13 changes: 13 additions & 0 deletions util/DateFormatter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { DateTime } from 'luxon';

export class DateFormatter {
private dateFormat: string;

constructor(dateFormat: string) {
this.dateFormat = dateFormat;
}

public formatDate(date: DateTime): string {
return date.toFormat(this.dateFormat);
}
}

0 comments on commit bb2fb97

Please sign in to comment.