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

refactor: convert TaskLayoutOptions to enum #2631

Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 20 additions & 29 deletions src/Layout/TaskLayoutOptions.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,25 @@
export type TaskLayoutComponent =
/**
* {@link Task} fields used for rendering.
*
* The order here determines the order that task fields are rendered and written to markdown.
*/
export enum TaskLayoutComponent {
// NEW_TASK_FIELD_EDIT_REQUIRED
| 'description'
| 'priority'
| 'recurrenceRule'
| 'createdDate'
| 'startDate'
| 'scheduledDate'
| 'dueDate'
| 'doneDate'
| 'cancelledDate'
| 'blockedBy'
| 'id'
| 'blockLink';
Description = 'description',
claremacrae marked this conversation as resolved.
Show resolved Hide resolved
Id = 'id',
BlockedBy = 'blockedBy',
Priority = 'priority',
RecurrenceRule = 'recurrenceRule',
CreatedDate = 'createdDate',
StartDate = 'startDate',
ScheduledDate = 'scheduledDate',
DueDate = 'dueDate',
CancelledDate = 'cancelledDate',
DoneDate = 'doneDate',
BlockLink = 'blockLink',
}

// The order here determines the order that task fields are rendered and written to markdown.
export const taskLayoutComponents: TaskLayoutComponent[] = [
// NEW_TASK_FIELD_EDIT_REQUIRED
'description',
'id',
'blockedBy',
'priority',
'recurrenceRule',
'createdDate',
'startDate',
'scheduledDate',
'dueDate',
'cancelledDate',
'doneDate',
'blockLink',
];
export const taskLayoutComponents = Object.values(TaskLayoutComponent);

/**
* Various rendering options of tasks in a query.
Expand Down
40 changes: 20 additions & 20 deletions src/Query/Query.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import { TaskLayoutOptions } from '../Layout/TaskLayoutOptions';
import { getSettings } from '../Config/Settings';
import type { IQuery } from '../IQuery';
import { QueryLayoutOptions } from '../Layout/QueryLayoutOptions';
import { TaskLayoutComponent, TaskLayoutOptions } from '../Layout/TaskLayoutOptions';
import { errorMessageForException } from '../lib/ExceptionTools';
import { logging } from '../lib/logging';
import { expandPlaceholders } from '../Scripting/ExpandPlaceholders';
import { makeQueryContext } from '../Scripting/QueryContext';
import type { Task } from '../Task/Task';
import type { IQuery } from '../IQuery';
import { getSettings } from '../Config/Settings';
import { errorMessageForException } from '../lib/ExceptionTools';
import { logging } from '../lib/logging';
import { Sort } from './Sort/Sort';
import type { Sorter } from './Sort/Sorter';
import { TaskGroups } from './Group/TaskGroups';
import { Explainer } from './Explain/Explainer';
import type { Filter } from './Filter/Filter';
import * as FilterParser from './FilterParser';
import type { Grouper } from './Group/Grouper';
import type { Filter } from './Filter/Filter';
import { TaskGroups } from './Group/TaskGroups';
import { QueryResult } from './QueryResult';
import { scan } from './Scanner';
import { SearchInfo } from './SearchInfo';
import { Explainer } from './Explain/Explainer';
import { Sort } from './Sort/Sort';
import type { Sorter } from './Sort/Sorter';

claremacrae marked this conversation as resolved.
Show resolved Hide resolved
export class Query implements IQuery {
/** Note: source is the raw source, before expanding any placeholders */
Expand Down Expand Up @@ -283,28 +283,28 @@ Problem line: "${line}"`;
this._queryLayoutOptions.hidePostponeButton = hide;
break;
case 'priority':
this._taskLayoutOptions.setVisibility('priority', !hide);
this._taskLayoutOptions.setVisibility(TaskLayoutComponent.Priority, !hide);
break;
case 'cancelled date':
this._taskLayoutOptions.setVisibility('cancelledDate', !hide);
this._taskLayoutOptions.setVisibility(TaskLayoutComponent.CancelledDate, !hide);
break;
case 'created date':
this._taskLayoutOptions.setVisibility('createdDate', !hide);
this._taskLayoutOptions.setVisibility(TaskLayoutComponent.CreatedDate, !hide);
break;
case 'start date':
this._taskLayoutOptions.setVisibility('startDate', !hide);
this._taskLayoutOptions.setVisibility(TaskLayoutComponent.StartDate, !hide);
break;
case 'scheduled date':
this._taskLayoutOptions.setVisibility('scheduledDate', !hide);
this._taskLayoutOptions.setVisibility(TaskLayoutComponent.ScheduledDate, !hide);
break;
case 'due date':
this._taskLayoutOptions.setVisibility('dueDate', !hide);
this._taskLayoutOptions.setVisibility(TaskLayoutComponent.DueDate, !hide);
break;
case 'done date':
this._taskLayoutOptions.setVisibility('doneDate', !hide);
this._taskLayoutOptions.setVisibility(TaskLayoutComponent.DoneDate, !hide);
break;
case 'recurrence rule':
this._taskLayoutOptions.setVisibility('recurrenceRule', !hide);
this._taskLayoutOptions.setVisibility(TaskLayoutComponent.RecurrenceRule, !hide);
break;
case 'edit button':
this._queryLayoutOptions.hideEditButton = hide;
Expand All @@ -316,10 +316,10 @@ Problem line: "${line}"`;
this._taskLayoutOptions.setTagsVisibility(!hide);
break;
case 'id':
this._taskLayoutOptions.setVisibility('id', !hide);
this._taskLayoutOptions.setVisibility(TaskLayoutComponent.Id, !hide);
break;
case 'depends on':
this._taskLayoutOptions.setVisibility('blockedBy', !hide);
this._taskLayoutOptions.setVisibility(TaskLayoutComponent.BlockedBy, !hide);
break;
default:
this.setError('do not understand hide/show option', line);
Expand Down
10 changes: 5 additions & 5 deletions src/Renderer/TaskLineRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import type { Moment } from 'moment';
import { Component, MarkdownRenderer } from 'obsidian';
import { GlobalFilter } from '../Config/GlobalFilter';
import { TASK_FORMATS, getSettings } from '../Config/Settings';
import { replaceTaskWithTasks } from '../Obsidian/File';
import type { TaskLayoutComponent, TaskLayoutOptions } from '../Layout/TaskLayoutOptions';
import type { QueryLayoutOptions } from '../Layout/QueryLayoutOptions';
import type { Task } from '../Task/Task';
import { StatusMenu } from '../ui/Menus/StatusMenu';
import { TaskLayoutComponent, type TaskLayoutOptions } from '../Layout/TaskLayoutOptions';
import { replaceTaskWithTasks } from '../Obsidian/File';
import { StatusRegistry } from '../Statuses/StatusRegistry';
import type { Task } from '../Task/Task';
import { TaskRegularExpressions } from '../Task/TaskRegularExpressions';
import { StatusMenu } from '../ui/Menus/StatusMenu';
import { TaskFieldRenderer } from './TaskFieldRenderer';

/**
Expand Down Expand Up @@ -195,7 +195,7 @@ export class TaskLineRenderer {
// So if the priority was not rendered, force it through the pipe of getting the component data for the
// priority field.
if (li.dataset.taskPriority === undefined) {
fieldRenderer.addDataAttribute(li, task, 'priority');
fieldRenderer.addDataAttribute(li, task, TaskLayoutComponent.Priority);
}
}

Expand Down
9 changes: 6 additions & 3 deletions src/TaskSerializer/DataviewTaskSerializer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { TaskLayoutComponent } from '../Layout/TaskLayoutOptions';
import type { Task } from '../Task/Task';
import { TaskLayoutComponent } from '../Layout/TaskLayoutOptions';
import { Priority } from '../Task/Priority';
import type { Task } from '../Task/Task';
import { DefaultTaskSerializer } from './DefaultTaskSerializer';

/**
Expand Down Expand Up @@ -119,7 +119,10 @@ export class DataviewTaskSerializer extends DefaultTaskSerializer {

public componentToString(task: Task, shortMode: boolean, component: TaskLayoutComponent) {
const stringComponent = super.componentToString(task, shortMode, component);
const notInlineFieldComponents: TaskLayoutComponent[] = ['blockLink', 'description'];
const notInlineFieldComponents: TaskLayoutComponent[] = [
TaskLayoutComponent.BlockLink,
TaskLayoutComponent.Description,
];
const shouldMakeInlineField = stringComponent !== '' && !notInlineFieldComponents.includes(component);
return shouldMakeInlineField
? // Having 2 (TWO) leading spaces avoids a rendering issues that makes every other
Expand Down
42 changes: 21 additions & 21 deletions tests/Layout/TaskLayoutOptions.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { TaskLayoutOptions } from '../../src/Layout/TaskLayoutOptions';
import { TaskLayoutComponent, TaskLayoutOptions } from '../../src/Layout/TaskLayoutOptions';

describe('TaskLayoutOptions', () => {
it('should be constructable', () => {
Expand Down Expand Up @@ -26,25 +26,25 @@ describe('TaskLayoutOptions', () => {
it('should show fields by default', () => {
const options = new TaskLayoutOptions();

expect(options.isShown('priority')).toEqual(true);
expect(options.isShown('createdDate')).toEqual(true);
expect(options.isShown(TaskLayoutComponent.Priority)).toEqual(true);
expect(options.isShown(TaskLayoutComponent.CreatedDate)).toEqual(true);
});

it('should be able to hide a field', () => {
const options = new TaskLayoutOptions();
options.hide('createdDate');
options.hide(TaskLayoutComponent.CreatedDate);

expect(options.isShown('createdDate')).toEqual(false);
expect(options.isShown(TaskLayoutComponent.CreatedDate)).toEqual(false);
});

it('should be settable via a boolean', () => {
const options = new TaskLayoutOptions();

options.setVisibility('scheduledDate', false);
expect(options.isShown('scheduledDate')).toEqual(false);
options.setVisibility(TaskLayoutComponent.ScheduledDate, false);
expect(options.isShown(TaskLayoutComponent.ScheduledDate)).toEqual(false);

options.setVisibility('scheduledDate', true);
expect(options.isShown('scheduledDate')).toEqual(true);
options.setVisibility(TaskLayoutComponent.ScheduledDate, true);
expect(options.isShown(TaskLayoutComponent.ScheduledDate)).toEqual(true);
});

it('should set tag visibility', () => {
Expand Down Expand Up @@ -75,8 +75,8 @@ describe('TaskLayoutOptions', () => {
blockLink"
`);

options.setVisibility('dueDate', false);
options.setVisibility('blockLink', false);
options.setVisibility(TaskLayoutComponent.DueDate, false);
options.setVisibility(TaskLayoutComponent.BlockLink, false);

expect(options.shownComponents.join('\n')).toMatchInlineSnapshot(`
"description
Expand All @@ -96,8 +96,8 @@ describe('TaskLayoutOptions', () => {
const options = new TaskLayoutOptions();
expect(options.hiddenComponents.join('\n')).toMatchInlineSnapshot('""');

options.setVisibility('startDate', false);
options.setVisibility('doneDate', false);
options.setVisibility(TaskLayoutComponent.StartDate, false);
options.setVisibility(TaskLayoutComponent.DoneDate, false);

expect(options.hiddenComponents.join('\n')).toMatchInlineSnapshot(`
"startDate
Expand All @@ -108,26 +108,26 @@ describe('TaskLayoutOptions', () => {
it('should toggle visibility', () => {
const options = new TaskLayoutOptions();

options.setVisibility('cancelledDate', false);
options.setVisibility('priority', true);
options.setVisibility(TaskLayoutComponent.CancelledDate, false);
options.setVisibility(TaskLayoutComponent.Priority, true);
options.setTagsVisibility(true);

options.toggleVisibilityExceptDescriptionAndBlockLink();

expect(options.isShown('cancelledDate')).toEqual(true);
expect(options.isShown('priority')).toEqual(false);
expect(options.isShown(TaskLayoutComponent.CancelledDate)).toEqual(true);
expect(options.isShown(TaskLayoutComponent.Priority)).toEqual(false);
expect(options.areTagsShown()).toEqual(false);
});

it('should not toggle visibility of description and blockLink', () => {
const options = new TaskLayoutOptions();
options.setVisibility('description', true);
options.setVisibility('blockLink', true);
options.setVisibility(TaskLayoutComponent.Description, true);
options.setVisibility(TaskLayoutComponent.BlockLink, true);

options.toggleVisibilityExceptDescriptionAndBlockLink();

expect(options.isShown('description')).toEqual(true);
expect(options.isShown('blockLink')).toEqual(true);
expect(options.isShown(TaskLayoutComponent.Description)).toEqual(true);
expect(options.isShown(TaskLayoutComponent.BlockLink)).toEqual(true);
});

it('should provide toggleable components', () => {
Expand Down
15 changes: 8 additions & 7 deletions tests/Renderer/TaskFieldRenderer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* @jest-environment jsdom
*/
import moment from 'moment';
import { TaskLayoutComponent } from '../../src/Layout/TaskLayoutOptions';
import { TaskFieldHTMLData, TaskFieldRenderer } from '../../src/Renderer/TaskFieldRenderer';
import { TaskBuilder } from '../TestingTools/TaskBuilder';

Expand All @@ -23,7 +24,7 @@ describe('Field Layouts Container tests', () => {
const task = new TaskBuilder().dueDate('2023-11-20').build();
const span = document.createElement('span');

fieldRenderer.addDataAttribute(span, task, 'dueDate');
fieldRenderer.addDataAttribute(span, task, TaskLayoutComponent.DueDate);

expect(span).toHaveDataAttributes('taskDue: future-1d');
});
Expand All @@ -32,7 +33,7 @@ describe('Field Layouts Container tests', () => {
const task = TaskBuilder.createFullyPopulatedTask();
const span = document.createElement('span');

fieldRenderer.addDataAttribute(span, task, 'priority');
fieldRenderer.addDataAttribute(span, task, TaskLayoutComponent.Priority);

expect(span).toHaveDataAttributes('taskPriority: medium');
});
Expand All @@ -41,15 +42,15 @@ describe('Field Layouts Container tests', () => {
const task = new TaskBuilder().build();
const span = document.createElement('span');

fieldRenderer.addDataAttribute(span, task, 'recurrenceRule');
fieldRenderer.addDataAttribute(span, task, TaskLayoutComponent.RecurrenceRule);

expect(span).toHaveDataAttributes('');
});

it('should add a class name for a component', () => {
const span = document.createElement('span');

fieldRenderer.addClassName(span, 'startDate');
fieldRenderer.addClassName(span, TaskLayoutComponent.StartDate);

expect(span.classList.toString()).toEqual('task-start');
});
Expand All @@ -69,7 +70,7 @@ describe('Field Layout Detail tests', () => {
});
const span = document.createElement('span');

fieldLayoutDetail.addDataAttribute(span, new TaskBuilder().build(), 'priority');
fieldLayoutDetail.addDataAttribute(span, new TaskBuilder().build(), TaskLayoutComponent.Priority);

expect(span).toHaveDataAttributes('taskPriority: highest');
});
Expand All @@ -80,7 +81,7 @@ describe('Field Layout Detail tests', () => {
});
const span = document.createElement('span');

fieldLayoutDetail.addDataAttribute(span, new TaskBuilder().build(), 'dueDate');
fieldLayoutDetail.addDataAttribute(span, new TaskBuilder().build(), TaskLayoutComponent.DueDate);

expect(span).toHaveDataAttributes('');
});
Expand All @@ -91,7 +92,7 @@ describe('Field Layout Detail tests', () => {
});
const span = document.createElement('span');

fieldLayoutDetail.addDataAttribute(span, new TaskBuilder().build(), 'startDate');
fieldLayoutDetail.addDataAttribute(span, new TaskBuilder().build(), TaskLayoutComponent.StartDate);

expect(span).toHaveDataAttributes('');
});
Expand Down
Loading