Skip to content

Commit

Permalink
Merge pull request #2631 from ilandikov/refactor-TaskLayoutOptions-enum
Browse files Browse the repository at this point in the history
refactor: convert `TaskLayoutOptions` to `enum`
  • Loading branch information
claremacrae authored Feb 3, 2024
2 parents ccfc77f + 9018fc6 commit 7a9a4a6
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 117 deletions.
52 changes: 22 additions & 30 deletions src/Layout/TaskLayoutOptions.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,26 @@
export type TaskLayoutComponent =
/**
* {@link Task} fields used for rendering. Use references to this enum ({@link TaskLayoutComponent.Id})
* instead of plain string values (`id`).
*
* 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',
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 Expand Up @@ -88,7 +80,7 @@ export class TaskLayoutOptions {
public get toggleableComponents() {
return taskLayoutComponents.filter((component) => {
// Description and blockLink are always shown
return component !== 'description' && component !== 'blockLink';
return component !== TaskLayoutComponent.Description && component !== TaskLayoutComponent.BlockLink;
});
}

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';

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
14 changes: 7 additions & 7 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 All @@ -208,7 +208,7 @@ export class TaskLineRenderer {
component: TaskLayoutComponent,
task: Task,
) {
if (component === 'description') {
if (component === TaskLayoutComponent.Description) {
componentString = GlobalFilter.getInstance().removeAsWordFromDependingOnSettings(componentString);

const { debugSettings } = getSettings();
Expand Down Expand Up @@ -270,7 +270,7 @@ export class TaskLineRenderer {
else return null;
}

if (component === 'description') {
if (component === TaskLayoutComponent.Description) {
const tags = internalSpan.getElementsByClassName('tag');
for (let i = 0; i < tags.length; i++) {
const tagName = tags[i].textContent;
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
26 changes: 13 additions & 13 deletions src/TaskSerializer/DefaultTaskSerializer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Moment } from 'moment';
import { type TaskLayoutComponent, TaskLayoutOptions } from '../Layout/TaskLayoutOptions';
import { TaskLayoutComponent, TaskLayoutOptions } from '../Layout/TaskLayoutOptions';
import { Recurrence } from '../Task/Recurrence';
import { Task } from '../Task/Task';
import { Priority } from '../Task/Priority';
Expand Down Expand Up @@ -136,9 +136,9 @@ export class DefaultTaskSerializer implements TaskSerializer {

switch (component) {
// NEW_TASK_FIELD_EDIT_REQUIRED
case 'description':
case TaskLayoutComponent.Description:
return task.description;
case 'priority': {
case TaskLayoutComponent.Priority: {
let priority: string = '';

if (task.priority === Priority.Highest) {
Expand All @@ -154,29 +154,29 @@ export class DefaultTaskSerializer implements TaskSerializer {
}
return priority;
}
case 'startDate':
case TaskLayoutComponent.StartDate:
return symbolAndDateValue(shortMode, startDateSymbol, task.startDate);
case 'createdDate':
case TaskLayoutComponent.CreatedDate:
return symbolAndDateValue(shortMode, createdDateSymbol, task.createdDate);
case 'scheduledDate':
case TaskLayoutComponent.ScheduledDate:
if (task.scheduledDateIsInferred) return '';
return symbolAndDateValue(shortMode, scheduledDateSymbol, task.scheduledDate);
case 'doneDate':
case TaskLayoutComponent.DoneDate:
return symbolAndDateValue(shortMode, doneDateSymbol, task.doneDate);
case 'cancelledDate':
case TaskLayoutComponent.CancelledDate:
return symbolAndDateValue(shortMode, cancelledDateSymbol, task.cancelledDate);
case 'dueDate':
case TaskLayoutComponent.DueDate:
return symbolAndDateValue(shortMode, dueDateSymbol, task.dueDate);
case 'recurrenceRule':
case TaskLayoutComponent.RecurrenceRule:
if (!task.recurrence) return '';
return symbolAndStringValue(shortMode, recurrenceSymbol, task.recurrence.toText());
case 'blockedBy': {
case TaskLayoutComponent.BlockedBy: {
if (task.blockedBy.length === 0) return '';
return symbolAndStringValue(shortMode, blockedBySymbol, task.blockedBy.join(','));
}
case 'id':
case TaskLayoutComponent.Id:
return symbolAndStringValue(shortMode, idSymbol, task.id);
case 'blockLink':
case TaskLayoutComponent.BlockLink:
return task.blockLink ?? '';
default:
throw new Error(`Don't know how to render task component of type '${component}'`);
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
Loading

0 comments on commit 7a9a4a6

Please sign in to comment.