Skip to content

Commit

Permalink
There should be an option to put the global filter at the end
Browse files Browse the repository at this point in the history
  • Loading branch information
sytone committed May 23, 2022
1 parent 292f684 commit f94572d
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 18 deletions.
13 changes: 12 additions & 1 deletion src/Feature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,19 @@ export class Feature {
false,
);

static readonly APPEND_GLOBAL_FILTER = new Feature(
'APPEND_GLOBAL_FILTER',
0,
'Enabling this places the global filter at the end of the task description. Some plugins, such as Day Planner,\n' +
'might require this, or you might prefer how it looks. If you change this when tasks are modified using the\n' +
'Task edit box they will have the tag moved to the beginning or end of the description.',
'Creates / Supports tasks with the global filter at end',
false,
false,
);

static get values(): Feature[] {
return [this.TASK_STATUS_MENU];
return [this.TASK_STATUS_MENU, this.TASK_STATUS_MENU];
}

static get settingsFlags(): FeatureFlag {
Expand Down
30 changes: 25 additions & 5 deletions src/Task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import type { Status } from './Status';
import { replaceTaskWithTasks } from './File';
import { LayoutOptions } from './LayoutOptions';
import { Recurrence } from './Recurrence';
import { getSettings } from './Settings';
import { getSettings, isFeatureEnabled } from './Settings';
import { Urgency } from './Urgency';
import { Feature } from './Feature';

/**
* When sorting, make sure low always comes after none. This way any tasks with low will be below any exiting
Expand Down Expand Up @@ -180,7 +181,14 @@ export class Task {
return null;
}

let description = body;
// Global filter is applied via edit or to string and no
// longer needs to be on the description. If this happens
// there may be a double space. So all double spaces are made
// single like the UI processing.
let description = body
.replace(globalFilter, '')
.replace(' ', ' ')
.trim();
const indentation = regexMatch[1];

// Get the status of the task.
Expand Down Expand Up @@ -356,6 +364,8 @@ export class Task {
}): Promise<HTMLLIElement> {
let taskAsString = this.toString(layoutOptions);
const { globalFilter, removeGlobalFilter } = getSettings();

// Hide the global filter when rendering the query results.
if (removeGlobalFilter) {
taskAsString = taskAsString.replace(globalFilter, '').trim();
}
Expand Down Expand Up @@ -439,15 +449,25 @@ export class Task {
}

/**
*
* Returns a string representation of the task. This is the entire body after the
* markdown task prefix. ( - [ ] )
*
* @param {LayoutOptions} [layoutOptions]
* @return {*} {string}
* @memberof Task
*/
public toString(layoutOptions?: LayoutOptions): string {
layoutOptions = layoutOptions ?? new LayoutOptions();
let taskString = this.description;

let taskString = this.description.trim();
const { globalFilter } = getSettings();

if (isFeatureEnabled(Feature.APPEND_GLOBAL_FILTER.internalName)) {
taskString = `${taskString} ${globalFilter}`.trim();
} else {
// Default is to have filter at front.
taskString = `${globalFilter} ${taskString}`.trim();
}

if (!layoutOptions.hidePriority) {
let priority: string = '';
Expand Down Expand Up @@ -513,7 +533,7 @@ export class Task {
public toFileLineString(): string {
return `${this.indentation}- [${
this.status.indicator
}] ${this.toString()}`;
}] ${this.toString().trim()}`;
}

/**
Expand Down
13 changes: 11 additions & 2 deletions src/ui/EditTask.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
import { Status } from '../Status';
import { onMount } from 'svelte';
import { Recurrence } from '../Recurrence';
import { getSettings } from '../Settings';
import { getSettings, isFeatureEnabled } from '../Settings';
import { Priority, Task } from '../Task';
import { StatusRegistry } from '../StatusRegistry';
import { Feature } from 'Feature';
export let task: Task;
export let onSubmit: (updatedTasks: Task[]) => void | Promise<void>;
Expand Down Expand Up @@ -158,9 +159,17 @@
const _onSubmit = () => {
const { globalFilter } = getSettings();
let description = editableTask.description.trim();
// Check to see if the global filter was added by user.
if (!description.includes(globalFilter)) {
description = globalFilter + ' ' + description;
if (isFeatureEnabled(Feature.APPEND_GLOBAL_FILTER.internalName)) {
description = `${description} ${globalFilter}`;
} else {
// Default is to have filter at front.
description = `${globalFilter} ${description}`;
}
}
let startDate: moment.Moment | null = null;
Expand Down
95 changes: 85 additions & 10 deletions tests/Task.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
import moment from 'moment';
import { Status } from '../src/Status';
import { Task } from '../src/Task';
import { getSettings, updateSettings } from '../src/Settings';
import { getSettings, toggleFeature, updateSettings } from '../src/Settings';
import { Feature } from '../src/Feature';

jest.mock('obsidian');
window.moment = moment;
Expand Down Expand Up @@ -192,40 +193,36 @@ describe('parsing tags', () => {
{
markdownTask:
'- [x] #someglobaltasktag this is a done task #tagone πŸ—“ 2021-09-12 βœ… 2021-06-20',
expectedDescription:
'#someglobaltasktag this is a done task #tagone',
expectedDescription: 'this is a done task #tagone',
extractedTags: ['#tagone'],
globalFilter: '#someglobaltasktag',
},
{
markdownTask:
'- [x] #someglobaltasktag this is a done task #tagone #tagtwo πŸ—“ 2021-09-12 βœ… 2021-06-20',
expectedDescription:
'#someglobaltasktag this is a done task #tagone #tagtwo',
expectedDescription: 'this is a done task #tagone #tagtwo',
extractedTags: ['#tagone', '#tagtwo'],
globalFilter: '#someglobaltasktag',
},
{
markdownTask:
'- [ ] #someglobaltasktag this is a normal task #tagone πŸ—“ 2021-09-12 βœ… 2021-06-20',
expectedDescription:
'#someglobaltasktag this is a normal task #tagone',
expectedDescription: 'this is a normal task #tagone',
extractedTags: ['#tagone'],
globalFilter: '#someglobaltasktag',
},
{
markdownTask:
'- [ ] #someglobaltasktag this is a normal task #tagone #tagtwo πŸ—“ 2021-09-12 βœ… 2021-06-20',
expectedDescription:
'#someglobaltasktag this is a normal task #tagone #tagtwo',
expectedDescription: 'this is a normal task #tagone #tagtwo',
extractedTags: ['#tagone', '#tagtwo'],
globalFilter: '#someglobaltasktag',
},
{
markdownTask:
'- [ ] #someglobaltasktag this is a normal task #tagone #tag/with/depth #tagtwo πŸ—“ 2021-09-12 βœ… 2021-06-20',
expectedDescription:
'#someglobaltasktag this is a normal task #tagone #tag/with/depth #tagtwo',
'this is a normal task #tagone #tag/with/depth #tagtwo',
extractedTags: ['#tagone', '#tag/with/depth', '#tagtwo'],
globalFilter: '#someglobaltasktag',
},
Expand Down Expand Up @@ -275,6 +272,13 @@ describe('parsing tags', () => {
);
});

type GlobalFilterParsingExpectations = {
location: string;
scenario: string;
initialTask: string;
expectedTask: string;
};

describe('to string', () => {
it('retains the block link', () => {
// Arrange
Expand Down Expand Up @@ -311,6 +315,77 @@ describe('to string', () => {
// Assert
expect(task.toFileLineString()).toStrictEqual(line);
});

test.each<GlobalFilterParsingExpectations>([
{
location: 'append',
scenario: 'at front',
initialTask:
'- [x] #globalfilter this is a done task #tagone #journal/daily πŸ“… 2021-09-12 βœ… 2021-06-20',
expectedTask:
'- [x] this is a done task #tagone #journal/daily #globalfilter πŸ“… 2021-09-12 βœ… 2021-06-20',
},
{
location: 'append',
scenario: 'at end',
initialTask:
'- [x] this is a done task #tagone #journal/daily #globalfilter πŸ“… 2021-09-12 βœ… 2021-06-20',
expectedTask:
'- [x] this is a done task #tagone #journal/daily #globalfilter πŸ“… 2021-09-12 βœ… 2021-06-20',
},
{
location: 'append',
scenario: 'in middle',
initialTask:
'- [x] this is a done task #globalfilter #tagone #journal/daily πŸ“… 2021-09-12 βœ… 2021-06-20',
expectedTask:
'- [x] this is a done task #tagone #journal/daily #globalfilter πŸ“… 2021-09-12 βœ… 2021-06-20',
},
{
location: 'prepend',
scenario: 'at front',
initialTask:
'- [x] #globalfilter this is a done task #tagone #journal/daily πŸ“… 2021-09-12 βœ… 2021-06-20',
expectedTask:
'- [x] #globalfilter this is a done task #tagone #journal/daily πŸ“… 2021-09-12 βœ… 2021-06-20',
},
{
location: 'prepend',
scenario: 'at end',
initialTask:
'- [x] this is a done task #tagone #journal/daily #globalfilter πŸ“… 2021-09-12 βœ… 2021-06-20',
expectedTask:
'- [x] #globalfilter this is a done task #tagone #journal/daily πŸ“… 2021-09-12 βœ… 2021-06-20',
},
{
location: 'prepend',
scenario: 'in middle',
initialTask:
'- [x] this is a done task #globalfilter #tagone #journal/daily πŸ“… 2021-09-12 βœ… 2021-06-20',
expectedTask:
'- [x] #globalfilter this is a done task #tagone #journal/daily πŸ“… 2021-09-12 βœ… 2021-06-20',
},
])(
'should $location global filter when $scenario',
({ location, initialTask, expectedTask }) => {
// Arrange
const originalSettings = getSettings();
updateSettings({ globalFilter: '#globalfilter' });
if (location === 'append') {
toggleFeature(Feature.APPEND_GLOBAL_FILTER.internalName, true);
}
// Act
const task = constructTaskFromLine(initialTask);

// Assert
expect(task).not.toBeNull();
expect(task?.toFileLineString()).toStrictEqual(expectedTask);

// Cleanup
updateSettings(originalSettings);
toggleFeature(Feature.APPEND_GLOBAL_FILTER.internalName, false);
},
);
});

describe('toggle done', () => {
Expand Down

0 comments on commit f94572d

Please sign in to comment.