Skip to content

Commit

Permalink
✨ Run commands that mark all tasks in reader mode
Browse files Browse the repository at this point in the history
  • Loading branch information
ebullient committed Jan 8, 2022
1 parent ebb92ae commit 205e2a1
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 26 deletions.
23 changes: 16 additions & 7 deletions src/taskcollector-Plugin.ts
Expand Up @@ -50,26 +50,35 @@ export class TaskCollectorPlugin extends Plugin {
id: "task-collector-mark-all-done",
name: "Complete all tasks",
icon: "tc-complete-all-items",
editorCallback: (editor: Editor, view: MarkdownView) => {
this.taskCollector.markAllTasks(editor, 'x');
}
callback: async () => {
const activeFile = this.app.workspace.getActiveFile();
const source = await this.app.vault.read(activeFile);
const result = this.taskCollector.markAllTasks(source, 'x');
this.app.vault.modify(activeFile, result);
}
};

const clearAllTasksCommand: Command = {
id: "task-collector-clear-all-items",
name: "Reset all completed tasks",
icon: "tc-clear-all-items",
editorCallback: (editor: Editor, view: MarkdownView) => {
this.taskCollector.resetAllTasks(editor);
callback: async () => {
const activeFile = this.app.workspace.getActiveFile();
const source = await this.app.vault.read(activeFile);
const result = this.taskCollector.resetAllTasks(source);
this.app.vault.modify(activeFile, result);
}
};

const moveTaskCommand: Command = {
id: "task-collector-move-completed-tasks",
name: "Move all completed tasks to configured heading",
icon: "tc-move-all-checked-items",
editorCallback: async (editor: Editor, view: MarkdownView) => {
this.taskCollector.moveCompletedTasksInFile(editor);
callback: async () => {
const activeFile = this.app.workspace.getActiveFile();
const source = await this.app.vault.read(activeFile);
const result = this.taskCollector.moveCompletedTasksInFile(source);
this.app.vault.modify(activeFile, result);
},
};

Expand Down
28 changes: 13 additions & 15 deletions src/taskcollector-TaskCollector.ts
Expand Up @@ -5,9 +5,11 @@ export class TaskCollector {
settings: TaskCollectorSettings;
initSettings: CompiledTasksSettings;
completedOrCanceled: RegExp;
stripTask: RegExp;

constructor(private app: App) {
this.completedOrCanceled = new RegExp(/^(\s*- \[)[xX-](\] .*)$/);
this.stripTask = new RegExp(/^(\s*-) \[[xX-]\] (.*)$/);
}

updateSettings(settings: TaskCollectorSettings): void {
Expand Down Expand Up @@ -80,6 +82,10 @@ export class TaskCollector {
: new RegExp(/^(\s*- \[) (\] .*)$/);
}

removeCheckboxFromLine(lineText: string): string {
return lineText.replace(this.stripTask, '$1 $2')
}

updateTaskLine(lineText: string, mark: string): string {
let marked = lineText.replace(this.initSettings.incompleteTaskRegExp, '$1' + mark + '$2');
if (this.initSettings.removeRegExp) {
Expand Down Expand Up @@ -124,8 +130,7 @@ export class TaskCollector {
}
}

markAllTasks(editor: Editor, mark: string): void {
const source = editor.getValue();
markAllTasks(source: string, mark: string): string {
const lines = source.split("\n");
const result: string[] = [];

Expand All @@ -136,7 +141,7 @@ export class TaskCollector {
result.push(line);
}
}
editor.setValue(result.join("\n"));
return result.join("\n");
}

resetTaskLine(lineText: string): string {
Expand Down Expand Up @@ -174,9 +179,8 @@ export class TaskCollector {
}
}

resetAllTasks(editor: Editor): void {
resetAllTasks(source: string): string {
const LOG_HEADING = this.settings.completedAreaHeader || '## Log';
const source = editor.getValue();
const lines = source.split("\n");

const result: string[] = [];
Expand All @@ -196,17 +200,11 @@ export class TaskCollector {
result.push(line);
}
}
editor.setValue(result.join("\n"));
}

removeCheckboxFromLine(line: string): string {
const lineWithoutCheckbox = line.replace(/(?<=([-] ))\[.\] /, '');
return lineWithoutCheckbox;
return result.join("\n");
}

moveCompletedTasksInFile(editor: Editor): void {
moveCompletedTasksInFile(source: string): string {
const LOG_HEADING = this.settings.completedAreaHeader || '## Log';
const source = editor.getValue();
const lines = source.split("\n");

if (!source.contains(LOG_HEADING)) {
Expand Down Expand Up @@ -240,7 +238,7 @@ export class TaskCollector {
// console.log(taskMatch);
if (this.isCompletedTask(taskMatch)) {
if (this.settings.completedAreaRemoveCheckbox) {
line = this.removeCheckboxFromLine(line);
line = line.replace(this.stripTask, '$1 $2')
}
inTask = true;
newTasks.push(line);
Expand All @@ -259,7 +257,7 @@ export class TaskCollector {
if (completedItemsIndex < remaining.length - 1) {
result = result.concat(remaining.slice(completedItemsIndex + 1));
}
editor.setValue(result.join("\n"));
return result.join("\n");
}

isCompletedTask(taskMatch: RegExpMatchArray): boolean {
Expand Down
8 changes: 4 additions & 4 deletions test/generatedRegExp.test.ts
Expand Up @@ -179,13 +179,13 @@ describe('Set an append date', () => {
config.completedAreaRemoveCheckbox = true;
tc.updateSettings(config);

const lineWithCompletedTask = '- [x] something'
const lineWithCancledTask = '- [-] something'
const lineIncompletedTask = '- [ ] something'
const lineWithCompletedTask = '- [x] something';
const lineWithCancledTask = '- [-] something';
const lineIncompletedTask = '- [ ] something'; // not a complete/canceled task
const lineWithoutCheckbox = '- something';
expect(tc.removeCheckboxFromLine(lineWithCompletedTask)).toEqual(lineWithoutCheckbox);
expect(tc.removeCheckboxFromLine(lineWithCancledTask)).toEqual(lineWithoutCheckbox);
expect(tc.removeCheckboxFromLine(lineIncompletedTask)).toEqual(lineWithoutCheckbox);
expect(tc.removeCheckboxFromLine(lineIncompletedTask)).toEqual(lineIncompletedTask); // unchanged

});
});
Expand Down

0 comments on commit 205e2a1

Please sign in to comment.