From 005711f4d4f50c117849b7e0b22db6796818916e Mon Sep 17 00:00:00 2001 From: Peter Heiss Date: Sun, 18 Aug 2024 15:32:13 +0200 Subject: [PATCH] add new setting to create tasks automatically when finishing create a new task --- main.ts | 18 ++++++++-------- manifest.json | 2 +- package.json | 2 +- src/processing/processor.ts | 43 ++++++++++++++++--------------------- src/settings/mainSetting.ts | 17 +++++++++++++++ versions.json | 3 ++- 6 files changed, 49 insertions(+), 36 deletions(-) diff --git a/main.ts b/main.ts index c17c7eb..e07bdff 100644 --- a/main.ts +++ b/main.ts @@ -141,15 +141,15 @@ export default class VikunjaPlugin extends Plugin { const tasks = await this.processor.getVaultSearcher().getTasksFromFile(this.processor.getTaskParser(), currentFile); for (const task of tasks) { - if (task.task.id) { - const cachedTask = this.cache.get(task.task.id); - if (cachedTask === undefined || !cachedTask.isTaskEqual(task.task)) { - this.cache.update(task); - } else { - if (cachedTask.lineno !== task.lineno || cachedTask.filepath !== task.filepath) { - this.cache.updateFileInfos(task.task.id, task.filepath, task.lineno); - } - } + if (task.task.id === undefined) { + continue; + } + + const cachedTask = this.cache.get(task.task.id); + if (cachedTask === undefined || !cachedTask.isTaskEqual(task.task)) { + this.cache.update(task); + } else if (cachedTask.lineno !== task.lineno || cachedTask.filepath !== task.filepath) { + this.cache.updateFileInfos(task.task.id, task.filepath, task.lineno); } } // FIXME the update line stuff should be communicated in settings diff --git a/manifest.json b/manifest.json index 6f8b1db..09b71a7 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { "id": "vikunja-sync", "name": "Vikunja Sync", - "version": "1.0.17", + "version": "1.0.18", "minAppVersion": "0.15.0", "description": "Integrates Vikunja.", "author": "Peter Heiss", diff --git a/package.json b/package.json index 1965d9d..c5fa02f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "obsidian-vikunja-plugin", - "version": "1.0.17", + "version": "1.0.18", "description": "This is a sample plugin for Obsidian (https://obsidian.md)", "main": "main.js", "scripts": { diff --git a/src/processing/processor.ts b/src/processing/processor.ts index 0847d8c..619d213 100644 --- a/src/processing/processor.ts +++ b/src/processing/processor.ts @@ -129,24 +129,15 @@ class Processor { return; } - const view = this.app.workspace.getActiveViewOfType(MarkdownView) + const view = this.app.workspace.getActiveViewOfType(MarkdownView); if (!view) { if (this.plugin.settings.debugging) console.log("Processor: No markdown view found"); return; } - const cursor = view.app.workspace.getActiveViewOfType(MarkdownView)?.editor.getCursor() - - const currentFilename = view.app.workspace.getActiveViewOfType(MarkdownView)?.app.workspace.activeEditor?.file?.name; - if (!currentFilename) { - - if (this.plugin.settings.debugging) console.log("Processor: No filename found"); - return; - } - - const currentLine = cursor?.line - if (!currentLine) { - if (this.plugin.settings.debugging) console.log("Processor: No line found"); + const cursor = view.editor.getCursor(); + if (!cursor) { + if (this.plugin.settings.debugging) console.log("Processor: No cursor found"); return; } @@ -156,28 +147,31 @@ class Processor { return; } + const currentFilename = file.name; + const lastLine = this.lastLineChecked.get(currentFilename); - let updatedTask = undefined; - if (!!lastLine) { + let updatedTask: PluginTask | undefined = undefined; + if (lastLine !== undefined) { const lastLineText = view.editor.getLine(lastLine); if (this.plugin.settings.debugging) console.log("Processor: Last line,", lastLine, "Last line text", lastLineText); try { const parsedTask = await this.taskParser.parse(lastLineText); updatedTask = new PluginTask(file.path, lastLine, parsedTask); - if (updatedTask.task.id === undefined) { - return; - } - const cacheTask = this.plugin.cache.get(updatedTask.task.id); - if (cacheTask === undefined) { - if (this.plugin.settings.debugging) console.error("Processor: Should not be here, because if this task is not in cache, but has an id, it circumvented the cache.") - return; + + if (updatedTask.task.id === undefined && this.plugin.settings.createTaskOnCursorMovement) { + await this.createTaskAndUpdateToVault(updatedTask); } - await this.plugin.tasksApi.updateTask(updatedTask); } catch (e) { - if (this.plugin.settings.debugging) console.log("Processor: Error while parsing task", e); + if (this.plugin.settings.debugging) console.log("Processor: Error while parsing task, mostly because there is no task", e); } } + const currentLine = cursor.line + if (!currentLine) { + if (this.plugin.settings.debugging) console.log("Processor: No line found"); + return; + } + this.lastLineChecked.set(currentFilename, currentLine); return updatedTask; } @@ -186,6 +180,7 @@ class Processor { * If the second parameter set to false, the vikunja metadata will not entered. But per default, the metadata will be entered. */ async updateToVault(task: PluginTask, metadata: boolean = true) { + if (this.plugin.settings.debugging) console.log("Processor: Update task in vault", task, "metadata", metadata); const newTask = (metadata) ? this.getTaskContent(task) : this.getTaskContentWithoutVikunja(task); const file = this.app.vault.getFileByPath(task.filepath); if (file === null) { diff --git a/src/settings/mainSetting.ts b/src/settings/mainSetting.ts index 622dce2..3afaa13 100644 --- a/src/settings/mainSetting.ts +++ b/src/settings/mainSetting.ts @@ -26,6 +26,7 @@ export interface VikunjaPluginSettings { cronInterval: number, updateOnStartup: boolean, updateOnCursorMovement: boolean, + createTaskOnCursorMovement: boolean, pullTasksOnlyFromDefaultProject: boolean, availableViews: ModelsProjectView[], selectedView: number, @@ -58,6 +59,7 @@ export const DEFAULT_SETTINGS: VikunjaPluginSettings = { cronInterval: 500, updateOnStartup: false, updateOnCursorMovement: false, + createTaskOnCursorMovement: false, pullTasksOnlyFromDefaultProject: false, availableViews: [], selectedView: 0, @@ -610,8 +612,23 @@ export class MainSetting extends PluginSettingTab { .onChange(async (value: boolean) => { this.plugin.settings.updateOnCursorMovement = value; await this.plugin.saveSettings(); + this.display(); })); + if (this.plugin.settings.updateOnCursorMovement) { + new Setting(containerEl) + .setName("Create task on cursor movement") + .setDesc("This will create a task in Vikunja, if you move the cursor and the found task on last line was not synced to Vikunja. Useful, if you want to create a task quickly and do not want to sync manually.") + .addToggle(toggle => + toggle + .setValue(this.plugin.settings.createTaskOnCursorMovement) + .onChange(async (value: boolean) => { + this.plugin.settings.createTaskOnCursorMovement = value; + await this.plugin.saveSettings(); + } + )); + } + new Setting(containerEl) .setName("Update completed status immediately") .setDesc("This will update the completed status of tasks immediately to Vikunja.") diff --git a/versions.json b/versions.json index 1c11f61..d1cb30c 100644 --- a/versions.json +++ b/versions.json @@ -3,5 +3,6 @@ "1.0.14": "0.15.0", "1.0.15": "0.15.0", "1.0.16": "0.15.0", - "1.0.17": "0.15.0" + "1.0.17": "0.15.0", + "1.0.18": "0.15.0" } \ No newline at end of file