Skip to content

Commit 7eb4f46

Browse files
committed
refactor: add toggleWithRecurrenceInUsersOrder()
1 parent eb24743 commit 7eb4f46

File tree

5 files changed

+32
-10
lines changed

5 files changed

+32
-10
lines changed

src/Commands/ToggleDone.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ export const toggleLine = (line: string, path: string): EditorInsertion => {
7272
fallbackDate: null, // We don't need this to toggle it here in the editor.
7373
});
7474
if (task !== null) {
75-
const lines = task.toggle().map((t) => t.toFileLineString());
75+
const lines = task.toggleWithRecurrenceInUsersOrder().map((t) => t.toFileLineString());
7676
return { text: lines.join('\n'), moveTo: { line: lines.length - 1 } };
7777
} else {
7878
// If the task is null this means that we have one of:

src/LivePreviewExtension.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class LivePreviewExtension implements PluginValue {
7575
event.preventDefault();
7676

7777
// Clicked on a task's checkbox. Toggle the task and set it.
78-
const toggled = task.toggle();
78+
const toggled = task.toggleWithRecurrenceInUsersOrder();
7979
const toggledString = toggled.map((t) => t.toFileLineString()).join(state.lineBreak);
8080

8181
// Creates a CodeMirror transaction in order to update the document.

src/Task.ts

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -291,12 +291,16 @@ export class Task {
291291
}
292292

293293
/**
294-
* Toggles this task and returns the resulting tasks.
294+
* Toggles this task and returns the resulting task(s). If the
295+
* task is not recurring, it will return `[toggled]`.
295296
*
296297
* Toggling can result in more than one returned task in the case of
297-
* recurrence. If it is a recurring task, the toggled task will be returned
298-
* together with the next occurrence in the order `[next, toggled]`. If the
299-
* task is not recurring, it will return `[toggled]`.
298+
* recurrence. In this case, the toggled task will be returned
299+
* together with the next occurrence in the order `[next, toggled]`.
300+
*
301+
* There is a possibility to use user set order `[next, toggled]`
302+
* or `[toggled, next]` - {@link toggleWithRecurrenceInUsersOrder}.
303+
*
300304
*/
301305
public toggle(): Task[] {
302306
const newStatus = StatusRegistry.getInstance().getNextStatusOrCreate(this.status);
@@ -353,6 +357,24 @@ export class Task {
353357
// Write next occurrence before previous occurrence.
354358
newTasks.push(toggledTask);
355359

360+
return newTasks;
361+
}
362+
363+
/**
364+
* Toggles this task and returns the resulting task(s). If the
365+
* task is not recurring, it will return `[toggled]`.
366+
*
367+
* Toggling can result in more than one returned task in the case of
368+
* recurrence. In this case, the toggled task will be returned in
369+
* user set order `[next, toggled]` or `[toggled, next]` depending
370+
* on {@link Settings}.
371+
*
372+
* If there is no need to consider user settings call {@link toggle}.
373+
*
374+
*/
375+
public toggleWithRecurrenceInUsersOrder(): Task[] {
376+
const newTasks = this.toggle();
377+
356378
const { recurrenceOnNextLine: recurrenceOnNextLine } = getSettings();
357379
return recurrenceOnNextLine ? newTasks.reverse() : newTasks;
358380
}

src/TaskLineRenderer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ export async function renderTaskLine(
8989

9090
// Should be re-rendered as enabled after update in file.
9191
checkbox.disabled = true;
92-
const toggledTasks = task.toggle();
92+
const toggledTasks = task.toggleWithRecurrenceInUsersOrder();
9393
replaceTaskWithTasks({
9494
originalTask: task,
9595
newTasks: toggledTasks,

tests/Task.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1139,7 +1139,7 @@ describe('next task recurrence appearance', () => {
11391139
const task = fromLine({ line: '- [ ] this is a recurring task 🔁 every day' });
11401140

11411141
// Act
1142-
const lines = task.toggle().map((t) => t.toFileLineString());
1142+
const lines = task.toggleWithRecurrenceInUsersOrder().map((t) => t.toFileLineString());
11431143

11441144
// Assert
11451145
expect(lines.length).toEqual(2);
@@ -1153,7 +1153,7 @@ describe('next task recurrence appearance', () => {
11531153
updateSettings({ recurrenceOnNextLine: false });
11541154

11551155
// Act
1156-
const lines = task.toggle().map((t) => t.toFileLineString());
1156+
const lines = task.toggleWithRecurrenceInUsersOrder().map((t) => t.toFileLineString());
11571157

11581158
// Assert
11591159
expect(lines.length).toEqual(2);
@@ -1167,7 +1167,7 @@ describe('next task recurrence appearance', () => {
11671167
updateSettings({ recurrenceOnNextLine: true });
11681168

11691169
// Act
1170-
const lines = task.toggle().map((t) => t.toFileLineString());
1170+
const lines = task.toggleWithRecurrenceInUsersOrder().map((t) => t.toFileLineString());
11711171

11721172
// Assert
11731173
expect(lines.length).toEqual(2);

0 commit comments

Comments
 (0)