update markComplete to work with selection or whole document

This commit is contained in:
Neil Weisenfeld 2023-05-24 10:22:57 -04:00
parent 3f96123b56
commit a941b2acb5

View file

@ -26,8 +26,7 @@ export default class TasksToOmnifocus extends Plugin {
id: 'extract-tasks',
name: 'Extract Tasks Into OmniFocus',
editorCallback: (editor: Editor, view: MarkdownView) => {
const editorText = editor.getValue()
this.addToOmnifocus(editorText, editor, view);
this.addToOmnifocus(false, editor, view);
},
});
@ -35,15 +34,20 @@ export default class TasksToOmnifocus extends Plugin {
id: 'extract-tasks-selection',
name: 'Extract Tasks from selection Into OmniFocus',
editorCallback: (editor: Editor, view: MarkdownView) => {
const editorText = editor.getSelection();
this.addToOmnifocus(editorText, editor, view);
this.addToOmnifocus(true, editor, view);
},
});
this.addSettingTab(new TasksToOmnifocusSettingTab(this.app, this));
}
async addToOmnifocus(editorText: string, editor: Editor, view: MarkdownView) {
async addToOmnifocus(isSelection: bool, editor: Editor, view: MarkdownView) {
var editorText;
if (isSelection) {
editorText = editor.getSelection();
} else {
editorText = editor.getValue();
}
try {
let tasks = editorText.match(/- \[ \] .*/g);
@ -61,11 +65,15 @@ export default class TasksToOmnifocus extends Plugin {
if (this.settings.markComplete) {
let completedText = editorText.replace(/- \[ \]/g, "- [x]");
if (isSelection) {
editor.replaceSelection(completedText);
} else {
editor.setValue(completedText)
}
}
} catch (err) {
// do you really want to hide any and all errors from the user?
}
}
@ -73,7 +81,7 @@ export default class TasksToOmnifocus extends Plugin {
await this.saveData(this.settings);
}
onunload() {}
onunload() { }
private async loadSettings() {