Skip to content

Commit e0b1b6b

Browse files
committed
✨ allow task toggle in mark cycle; resolves #456
1 parent a76f791 commit e0b1b6b

7 files changed

+72
-16
lines changed

src/@types/settings.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ export type TaskCollectorSettings = {
33
collectionEnabled: boolean;
44
previewClickModal: boolean;
55
markCycle: string;
6+
markCycleRemoveTask: boolean;
67
skipSectionMatch: string;
78
contextMenu: {
89
markTask: boolean;

src/taskcollector-Constants.ts

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export const DEFAULT_SETTINGS: TaskCollectorSettings = {
3131
},
3232
},
3333
markCycle: "",
34+
markCycleRemoveTask: false,
3435
collectionEnabled: false,
3536
previewClickModal: true,
3637
contextMenu: {

src/taskcollector-Data.ts

+4
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ function sanitize(tcp: TaskCollectorPlugin, settings: TaskCollectorSettings) {
4141
// Make sure characters in cycle are unique
4242
if (settings.markCycle) {
4343
settings.markCycle = [...new Set(settings.markCycle)].join("");
44+
settings.markCycle.replace("§", "");
45+
if (settings.markCycleRemoveTask) {
46+
settings.markCycle += "§";
47+
}
4448
}
4549

4650
// resolve groups with a key / mts.name mismatch

src/taskcollector-SettingsTab.ts

+27-3
Original file line numberDiff line numberDiff line change
@@ -121,18 +121,42 @@ export class TaskCollectorSettingsTab extends PluginSettingTab {
121121
new Setting(this.containerEl)
122122
.setName("Define task mark cycle")
123123
.setDesc(
124-
"Specify characters (as a string) for Previous/Next commands",
124+
"Specify characters (as a string) for Previous/Next commands. Use the button to include checkbox removal in the cycle.",
125125
)
126126
.addText((input) =>
127127
input
128128
.setPlaceholder("")
129-
.setValue(this.newSettings.markCycle)
129+
.setValue(this.newSettings.markCycle.replace("§", ""))
130130
.onChange(async (value) => {
131131
this.newSettings.markCycle = [...new Set(value)].join(
132132
"",
133133
);
134134
}),
135-
);
135+
)
136+
.addExtraButton((button) => {
137+
const el = button
138+
.setTooltip(
139+
"Include checkbox removal in the cycle: " +
140+
this.newSettings.markCycleRemoveTask,
141+
)
142+
.setIcon("cross-in-box")
143+
.onClick(() => {
144+
this.newSettings.markCycleRemoveTask =
145+
!this.newSettings.markCycleRemoveTask;
146+
el.classList.toggle(
147+
"is-active",
148+
this.newSettings.markCycleRemoveTask,
149+
);
150+
button.setTooltip(
151+
"Include checkbox removal in the cycle: " +
152+
this.newSettings.markCycleRemoveTask,
153+
);
154+
}).extraSettingsEl;
155+
el.classList.toggle(
156+
"is-active",
157+
this.newSettings.markCycleRemoveTask,
158+
);
159+
});
136160

137161
new Setting(this.containerEl)
138162
.setName("Convert non-list lines")

src/taskcollector-TaskCollector.ts

+17-13
Original file line numberDiff line numberDiff line change
@@ -166,23 +166,27 @@ export class TaskCollector {
166166
const i = this.settings.markCycle.indexOf(old);
167167
const next =
168168
i < 0
169-
? d == Direction.NEXT
170-
? 0
171-
: len - 1
172-
: d == Direction.NEXT
173-
? (i + 1) % len
174-
: (i + len - 1) % len;
175-
176-
split[n] = this.doMarkTask(
177-
split[n],
178-
old,
179-
this.settings.markCycle[next],
180-
);
169+
? d == Direction.NEXT // i < 0
170+
? 0 // NEXT
171+
: len - 1 // PREV
172+
: d == Direction.NEXT // i >= 0
173+
? (i + 1) % len // NEXT
174+
: (i + len - 1) % len; // PREV
175+
176+
const chosenMark = this.settings.markCycle[next];
177+
if (chosenMark === "§") {
178+
split[n] = this.doRemoveTask(split[n]);
179+
} else {
180+
split[n] = this.doMarkTask(split[n], old, chosenMark);
181+
}
181182
} else if (listMatch && listMatch[2]) {
183+
const cycle = this.settings.markCycle.replace("§", "");
184+
const chosenMark =
185+
cycle[d == Direction.NEXT ? 0 : cycle.length - 1];
182186
// convert to a task, and then mark
183187
split[n] = this.updateLineText(
184188
`${listMatch[1]}[ ] ${listMatch[2]}`,
185-
this.settings.markCycle[d == Direction.NEXT ? 0 : len - 1],
189+
chosenMark,
186190
);
187191
}
188192
}

test/dataMigration.test.ts

+2
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ export const DEFAULT_MIGRATION = {
5858
}
5959
},
6060
markCycle: "",
61+
markCycleRemoveTask: false,
6162
collectionEnabled: true,
6263
previewClickModal: false,
6364
contextMenu: {
@@ -130,6 +131,7 @@ test('Task Marker: User configuration', async () => {
130131
JSON.parse(JSON.stringify(DEFAULT_MIGRATION)),
131132
{
132133
markCycle: " x/>-",
134+
markCycleRemoveTask: false,
133135
previewClickModal: false,
134136
collectionEnabled: false,
135137
contextMenu: {

test/markTasks.test.ts

+20
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,26 @@ describe('Task mark cycle', () => {
149149
expect(tc.markInCycle('- something', Direction.NEXT, [0])).toEqual("- [a] something");
150150
expect(tc.markInCycle('- something', Direction.PREV, [0])).toEqual("- [c] something");
151151
});
152+
153+
test('Mark tasks forward in a cycle (next) include remove task', () => {
154+
config.markCycle = " ab§";
155+
config.markCycleRemoveTask = true;
156+
tc.init(config);
157+
expect(tc.markInCycle('- something', Direction.NEXT, [0])).toEqual("- [ ] something");
158+
expect(tc.markInCycle('- [ ] something', Direction.NEXT, [0])).toEqual("- [a] something");
159+
expect(tc.markInCycle('- [a] something', Direction.NEXT, [0])).toEqual("- [b] something");
160+
expect(tc.markInCycle('- [b] something', Direction.NEXT, [0])).toEqual("- something");
161+
});
162+
163+
test('Mark tasks backward in a cycle (prev) include remove task', () => {
164+
config.markCycle = " ab§";
165+
config.markCycleRemoveTask = true;
166+
tc.init(config);
167+
expect(tc.markInCycle('- [b] something', Direction.PREV, [0])).toEqual("- [a] something");
168+
expect(tc.markInCycle('- [a] something', Direction.PREV, [0])).toEqual("- [ ] something");
169+
expect(tc.markInCycle('- [ ] something', Direction.PREV, [0])).toEqual("- something");
170+
expect(tc.markInCycle('- something', Direction.PREV, [0])).toEqual("- [b] something");
171+
});
152172
});
153173

154174

0 commit comments

Comments
 (0)