diff --git a/manifest.json b/manifest.json index 9ae4f34..ceb5ef1 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { "id": "tasks-to-omnifocus", "name": "Send Tasks to OmniFocus", - "version": "1.0.5", + "version": "1.0.6", "minAppVersion": "0.12.0", "description": "An Obsidian plugin will extract tasks from the current note and create them in OmniFocus.", "author": "Henry Gustafson", diff --git a/src/main.ts b/src/main.ts index 0f92bfa..57f9333 100644 --- a/src/main.ts +++ b/src/main.ts @@ -21,40 +21,13 @@ export default class TasksToOmnifocus extends Plugin { async onload() { await this.loadSettings(); - // this.addCommand({ - // id: "extract-tasks", - // name: "Extract Tasks Into OmniFocus", - // callback: () => this.sendToOF(), - // }); this.addCommand({ id: 'extract-tasks', name: 'Extract Tasks Into OmniFocus', editorCallback: (editor: Editor, view: MarkdownView) => { const editorText = editor.getValue() - try { - let tasks = editorText.match(/- \[ \] .*/g); - - for (let task of tasks) { - let taskName = task.replace("- [ ] ", ""); - let taskNameEncoded = encodeURIComponent(taskName); - let noteURL = view.file.path.replace(/ /g, "%20").replace(/\//g, "%2F"); - let vaultName = app.vault.getName(); - let taskNoteEncoded = encodeURIComponent("obsidian://open?=" + vaultName + "&file=" + noteURL); - - window.open( - `omnifocus:///add?name=${taskNameEncoded}¬e=${taskNoteEncoded}` - ); - } - - if (this.settings.markComplete) { - let completedText = editorText.replace(/- \[ \]/g, "- [x]"); - editor.setValue(completedText); - } - - } catch (err) { - - } + this.addToOmnifocus(editorText, editor, view); }, }); @@ -62,76 +35,46 @@ 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() - try { - let tasks = editorText.match(/- \[ \] .*/g); - - for (let task of tasks) { - let taskName = task.replace("- [ ] ", ""); - let taskNameEncoded = encodeURIComponent(taskName); - let noteURL = view.file.path.replace(/ /g, "%20").replace(/\//g, "%2F"); - let vaultName = app.vault.getName(); - let taskNoteEncoded = encodeURIComponent("obsidian://open?=" + vaultName + "&file=" + noteURL); - - window.open( - `omnifocus:///add?name=${taskNameEncoded}¬e=${taskNoteEncoded}` - ); - } - - if (this.settings.markComplete) { - let completedText = editorText.replace(/- \[ \]/g, "- [x]"); - editor.replaceSelection(completedText); - } - - } catch (err) { - - } + const editorText = editor.getSelection(); + this.addToOmnifocus(editorText, editor, view); }, }); this.addSettingTab(new TasksToOmnifocusSettingTab(this.app, this)); } + async addToOmnifocus(editorText: string, editor: Editor, view: MarkdownView) { + try { + let tasks = editorText.match(/- \[ \] .*/g); + + for (let task of tasks) { + let taskName = task.replace("- [ ] ", ""); + let taskNameEncoded = encodeURIComponent(taskName); + let noteURL = view.file.path.replace(/ /g, "%20").replace(/\//g, "%2F"); + let vaultName = app.vault.getName(); + let taskNoteEncoded = encodeURIComponent("obsidian://open?=" + vaultName + "&file=" + noteURL); + + window.open( + `omnifocus:///add?name=${taskNameEncoded}¬e=${taskNoteEncoded}` + ); + } + + if (this.settings.markComplete) { + let completedText = editorText.replace(/- \[ \]/g, "- [x]"); + editor.replaceSelection(completedText); + } + + } catch (err) { + + } + } + async saveSettings() { await this.saveData(this.settings); } onunload() {} - // // the first function that runs - // async sendToOF() { - // // const app = window.app as App; - // const view = this.app.workspace.getActiveViewOfType(MarkdownView); - // if (view!=null) { - // const editor = view.editor; - // const editorText = editor.getValue(); - - // try { - // let tasks = editorText.match(/- \[ \] .*/g); - - // for (let task of tasks) { - // let taskName = task.replace("- [ ] ", ""); - // let taskNameEncoded = encodeURIComponent(taskName); - // let noteURL = view.file.path.replace(/ /g, "%20").replace(/\//g, "%2F"); - // let vaultName = app.vault.getName(); - // let taskNoteEncoded = encodeURIComponent("obsidian://open?=" + vaultName + "&file="+noteURL); - - // window.open( - // `omnifocus:///add?name=${taskNameEncoded}¬e=${taskNoteEncoded}` - // ); - // } - - // if (this.settings.markComplete) { - // let completedText = editorText.replace(/- \[ \]/g, "- [x]"); - // editor.setValue(completedText); - // } - - // } catch (err) { - - // } - // } - // } - private async loadSettings() { this.settings = Object.assign(