Skip to content

Commit 4805534

Browse files
committed
refactor: - convert TaskLayoutOptions to enum
1 parent 2b24d7a commit 4805534

File tree

7 files changed

+94
-100
lines changed

7 files changed

+94
-100
lines changed

src/Layout/TaskLayoutOptions.ts

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,21 @@
1-
export type TaskLayoutComponent =
1+
export enum TaskLayoutComponent {
22
// NEW_TASK_FIELD_EDIT_REQUIRED
3-
| 'description'
4-
| 'priority'
5-
| 'recurrenceRule'
6-
| 'createdDate'
7-
| 'startDate'
8-
| 'scheduledDate'
9-
| 'dueDate'
10-
| 'doneDate'
11-
| 'cancelledDate'
12-
| 'blockedBy'
13-
| 'id'
14-
| 'blockLink';
3+
Description = 'description',
4+
Id = 'id',
5+
BlockedBy = 'blockedBy',
6+
Priority = 'priority',
7+
RecurrenceRule = 'recurrenceRule',
8+
CreatedDate = 'createdDate',
9+
StartDate = 'startDate',
10+
ScheduledDate = 'scheduledDate',
11+
DueDate = 'dueDate',
12+
CancelledDate = 'cancelledDate',
13+
DoneDate = 'doneDate',
14+
BlockLink = 'blockLink',
15+
}
1516

1617
// The order here determines the order that task fields are rendered and written to markdown.
17-
export const taskLayoutComponents: TaskLayoutComponent[] = [
18-
// NEW_TASK_FIELD_EDIT_REQUIRED
19-
'description',
20-
'id',
21-
'blockedBy',
22-
'priority',
23-
'recurrenceRule',
24-
'createdDate',
25-
'startDate',
26-
'scheduledDate',
27-
'dueDate',
28-
'cancelledDate',
29-
'doneDate',
30-
'blockLink',
31-
];
18+
export const taskLayoutComponents = Object.values(TaskLayoutComponent);
3219

3320
/**
3421
* Various rendering options of tasks in a query.

src/Query/Query.ts

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
import { TaskLayoutOptions } from '../Layout/TaskLayoutOptions';
1+
import { getSettings } from '../Config/Settings';
2+
import type { IQuery } from '../IQuery';
23
import { QueryLayoutOptions } from '../Layout/QueryLayoutOptions';
4+
import { TaskLayoutComponent, TaskLayoutOptions } from '../Layout/TaskLayoutOptions';
5+
import { errorMessageForException } from '../lib/ExceptionTools';
6+
import { logging } from '../lib/logging';
37
import { expandPlaceholders } from '../Scripting/ExpandPlaceholders';
48
import { makeQueryContext } from '../Scripting/QueryContext';
59
import type { Task } from '../Task/Task';
6-
import type { IQuery } from '../IQuery';
7-
import { getSettings } from '../Config/Settings';
8-
import { errorMessageForException } from '../lib/ExceptionTools';
9-
import { logging } from '../lib/logging';
10-
import { Sort } from './Sort/Sort';
11-
import type { Sorter } from './Sort/Sorter';
12-
import { TaskGroups } from './Group/TaskGroups';
10+
import { Explainer } from './Explain/Explainer';
11+
import type { Filter } from './Filter/Filter';
1312
import * as FilterParser from './FilterParser';
1413
import type { Grouper } from './Group/Grouper';
15-
import type { Filter } from './Filter/Filter';
14+
import { TaskGroups } from './Group/TaskGroups';
1615
import { QueryResult } from './QueryResult';
1716
import { scan } from './Scanner';
1817
import { SearchInfo } from './SearchInfo';
19-
import { Explainer } from './Explain/Explainer';
18+
import { Sort } from './Sort/Sort';
19+
import type { Sorter } from './Sort/Sorter';
2020

2121
export class Query implements IQuery {
2222
/** Note: source is the raw source, before expanding any placeholders */
@@ -283,28 +283,28 @@ Problem line: "${line}"`;
283283
this._queryLayoutOptions.hidePostponeButton = hide;
284284
break;
285285
case 'priority':
286-
this._taskLayoutOptions.setVisibility('priority', !hide);
286+
this._taskLayoutOptions.setVisibility(TaskLayoutComponent.Priority, !hide);
287287
break;
288288
case 'cancelled date':
289-
this._taskLayoutOptions.setVisibility('cancelledDate', !hide);
289+
this._taskLayoutOptions.setVisibility(TaskLayoutComponent.CancelledDate, !hide);
290290
break;
291291
case 'created date':
292-
this._taskLayoutOptions.setVisibility('createdDate', !hide);
292+
this._taskLayoutOptions.setVisibility(TaskLayoutComponent.CreatedDate, !hide);
293293
break;
294294
case 'start date':
295-
this._taskLayoutOptions.setVisibility('startDate', !hide);
295+
this._taskLayoutOptions.setVisibility(TaskLayoutComponent.StartDate, !hide);
296296
break;
297297
case 'scheduled date':
298-
this._taskLayoutOptions.setVisibility('scheduledDate', !hide);
298+
this._taskLayoutOptions.setVisibility(TaskLayoutComponent.ScheduledDate, !hide);
299299
break;
300300
case 'due date':
301-
this._taskLayoutOptions.setVisibility('dueDate', !hide);
301+
this._taskLayoutOptions.setVisibility(TaskLayoutComponent.DueDate, !hide);
302302
break;
303303
case 'done date':
304-
this._taskLayoutOptions.setVisibility('doneDate', !hide);
304+
this._taskLayoutOptions.setVisibility(TaskLayoutComponent.DoneDate, !hide);
305305
break;
306306
case 'recurrence rule':
307-
this._taskLayoutOptions.setVisibility('recurrenceRule', !hide);
307+
this._taskLayoutOptions.setVisibility(TaskLayoutComponent.RecurrenceRule, !hide);
308308
break;
309309
case 'edit button':
310310
this._queryLayoutOptions.hideEditButton = hide;
@@ -316,10 +316,10 @@ Problem line: "${line}"`;
316316
this._taskLayoutOptions.setTagsVisibility(!hide);
317317
break;
318318
case 'id':
319-
this._taskLayoutOptions.setVisibility('id', !hide);
319+
this._taskLayoutOptions.setVisibility(TaskLayoutComponent.Id, !hide);
320320
break;
321321
case 'depends on':
322-
this._taskLayoutOptions.setVisibility('blockedBy', !hide);
322+
this._taskLayoutOptions.setVisibility(TaskLayoutComponent.BlockedBy, !hide);
323323
break;
324324
default:
325325
this.setError('do not understand hide/show option', line);

src/Renderer/TaskLineRenderer.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ import type { Moment } from 'moment';
22
import { Component, MarkdownRenderer } from 'obsidian';
33
import { GlobalFilter } from '../Config/GlobalFilter';
44
import { TASK_FORMATS, getSettings } from '../Config/Settings';
5-
import { replaceTaskWithTasks } from '../Obsidian/File';
6-
import type { TaskLayoutComponent, TaskLayoutOptions } from '../Layout/TaskLayoutOptions';
75
import type { QueryLayoutOptions } from '../Layout/QueryLayoutOptions';
8-
import type { Task } from '../Task/Task';
9-
import { StatusMenu } from '../ui/Menus/StatusMenu';
6+
import { TaskLayoutComponent, type TaskLayoutOptions } from '../Layout/TaskLayoutOptions';
7+
import { replaceTaskWithTasks } from '../Obsidian/File';
108
import { StatusRegistry } from '../Statuses/StatusRegistry';
9+
import type { Task } from '../Task/Task';
1110
import { TaskRegularExpressions } from '../Task/TaskRegularExpressions';
11+
import { StatusMenu } from '../ui/Menus/StatusMenu';
1212
import { TaskFieldRenderer } from './TaskFieldRenderer';
1313

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

src/TaskSerializer/DataviewTaskSerializer.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import type { TaskLayoutComponent } from '../Layout/TaskLayoutOptions';
2-
import type { Task } from '../Task/Task';
1+
import { TaskLayoutComponent } from '../Layout/TaskLayoutOptions';
32
import { Priority } from '../Task/Priority';
3+
import type { Task } from '../Task/Task';
44
import { DefaultTaskSerializer } from './DefaultTaskSerializer';
55

66
/**
@@ -119,7 +119,10 @@ export class DataviewTaskSerializer extends DefaultTaskSerializer {
119119

120120
public componentToString(task: Task, shortMode: boolean, component: TaskLayoutComponent) {
121121
const stringComponent = super.componentToString(task, shortMode, component);
122-
const notInlineFieldComponents: TaskLayoutComponent[] = ['blockLink', 'description'];
122+
const notInlineFieldComponents: TaskLayoutComponent[] = [
123+
TaskLayoutComponent.BlockLink,
124+
TaskLayoutComponent.Description,
125+
];
123126
const shouldMakeInlineField = stringComponent !== '' && !notInlineFieldComponents.includes(component);
124127
return shouldMakeInlineField
125128
? // Having 2 (TWO) leading spaces avoids a rendering issues that makes every other

tests/Layout/TaskLayoutOptions.test.ts

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { TaskLayoutOptions } from '../../src/Layout/TaskLayoutOptions';
1+
import { TaskLayoutComponent, TaskLayoutOptions } from '../../src/Layout/TaskLayoutOptions';
22

33
describe('TaskLayoutOptions', () => {
44
it('should be constructable', () => {
@@ -26,25 +26,25 @@ describe('TaskLayoutOptions', () => {
2626
it('should show fields by default', () => {
2727
const options = new TaskLayoutOptions();
2828

29-
expect(options.isShown('priority')).toEqual(true);
30-
expect(options.isShown('createdDate')).toEqual(true);
29+
expect(options.isShown(TaskLayoutComponent.Priority)).toEqual(true);
30+
expect(options.isShown(TaskLayoutComponent.CreatedDate)).toEqual(true);
3131
});
3232

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

37-
expect(options.isShown('createdDate')).toEqual(false);
37+
expect(options.isShown(TaskLayoutComponent.CreatedDate)).toEqual(false);
3838
});
3939

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

43-
options.setVisibility('scheduledDate', false);
44-
expect(options.isShown('scheduledDate')).toEqual(false);
43+
options.setVisibility(TaskLayoutComponent.ScheduledDate, false);
44+
expect(options.isShown(TaskLayoutComponent.ScheduledDate)).toEqual(false);
4545

46-
options.setVisibility('scheduledDate', true);
47-
expect(options.isShown('scheduledDate')).toEqual(true);
46+
options.setVisibility(TaskLayoutComponent.ScheduledDate, true);
47+
expect(options.isShown(TaskLayoutComponent.ScheduledDate)).toEqual(true);
4848
});
4949

5050
it('should set tag visibility', () => {
@@ -75,8 +75,8 @@ describe('TaskLayoutOptions', () => {
7575
blockLink"
7676
`);
7777

78-
options.setVisibility('dueDate', false);
79-
options.setVisibility('blockLink', false);
78+
options.setVisibility(TaskLayoutComponent.DueDate, false);
79+
options.setVisibility(TaskLayoutComponent.BlockLink, false);
8080

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

99-
options.setVisibility('startDate', false);
100-
options.setVisibility('doneDate', false);
99+
options.setVisibility(TaskLayoutComponent.StartDate, false);
100+
options.setVisibility(TaskLayoutComponent.DoneDate, false);
101101

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

111-
options.setVisibility('cancelledDate', false);
112-
options.setVisibility('priority', true);
111+
options.setVisibility(TaskLayoutComponent.CancelledDate, false);
112+
options.setVisibility(TaskLayoutComponent.Priority, true);
113113
options.setTagsVisibility(true);
114114

115115
options.toggleVisibilityExceptDescriptionAndBlockLink();
116116

117-
expect(options.isShown('cancelledDate')).toEqual(true);
118-
expect(options.isShown('priority')).toEqual(false);
117+
expect(options.isShown(TaskLayoutComponent.CancelledDate)).toEqual(true);
118+
expect(options.isShown(TaskLayoutComponent.Priority)).toEqual(false);
119119
expect(options.areTagsShown()).toEqual(false);
120120
});
121121

122122
it('should not toggle visibility of description and blockLink', () => {
123123
const options = new TaskLayoutOptions();
124-
options.setVisibility('description', true);
125-
options.setVisibility('blockLink', true);
124+
options.setVisibility(TaskLayoutComponent.Description, true);
125+
options.setVisibility(TaskLayoutComponent.BlockLink, true);
126126

127127
options.toggleVisibilityExceptDescriptionAndBlockLink();
128128

129-
expect(options.isShown('description')).toEqual(true);
130-
expect(options.isShown('blockLink')).toEqual(true);
129+
expect(options.isShown(TaskLayoutComponent.Description)).toEqual(true);
130+
expect(options.isShown(TaskLayoutComponent.BlockLink)).toEqual(true);
131131
});
132132

133133
it('should provide toggleable components', () => {

tests/Renderer/TaskFieldRenderer.test.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* @jest-environment jsdom
33
*/
44
import moment from 'moment';
5+
import { TaskLayoutComponent } from '../../src/Layout/TaskLayoutOptions';
56
import { TaskFieldHTMLData, TaskFieldRenderer } from '../../src/Renderer/TaskFieldRenderer';
67
import { TaskBuilder } from '../TestingTools/TaskBuilder';
78

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

26-
fieldRenderer.addDataAttribute(span, task, 'dueDate');
27+
fieldRenderer.addDataAttribute(span, task, TaskLayoutComponent.DueDate);
2728

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

35-
fieldRenderer.addDataAttribute(span, task, 'priority');
36+
fieldRenderer.addDataAttribute(span, task, TaskLayoutComponent.Priority);
3637

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

44-
fieldRenderer.addDataAttribute(span, task, 'recurrenceRule');
45+
fieldRenderer.addDataAttribute(span, task, TaskLayoutComponent.RecurrenceRule);
4546

4647
expect(span).toHaveDataAttributes('');
4748
});
4849

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

52-
fieldRenderer.addClassName(span, 'startDate');
53+
fieldRenderer.addClassName(span, TaskLayoutComponent.StartDate);
5354

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

72-
fieldLayoutDetail.addDataAttribute(span, new TaskBuilder().build(), 'priority');
73+
fieldLayoutDetail.addDataAttribute(span, new TaskBuilder().build(), TaskLayoutComponent.Priority);
7374

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

83-
fieldLayoutDetail.addDataAttribute(span, new TaskBuilder().build(), 'dueDate');
84+
fieldLayoutDetail.addDataAttribute(span, new TaskBuilder().build(), TaskLayoutComponent.DueDate);
8485

8586
expect(span).toHaveDataAttributes('');
8687
});
@@ -91,7 +92,7 @@ describe('Field Layout Detail tests', () => {
9192
});
9293
const span = document.createElement('span');
9394

94-
fieldLayoutDetail.addDataAttribute(span, new TaskBuilder().build(), 'startDate');
95+
fieldLayoutDetail.addDataAttribute(span, new TaskBuilder().build(), TaskLayoutComponent.StartDate);
9596

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

0 commit comments

Comments
 (0)