Skip to content

Commit

Permalink
add new setting to create tasks automatically when finishing create a…
Browse files Browse the repository at this point in the history
… new task
  • Loading branch information
Heiss committed Aug 18, 2024
1 parent 36749d4 commit 005711f
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 36 deletions.
18 changes: 9 additions & 9 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
43 changes: 19 additions & 24 deletions src/processing/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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;
}
Expand All @@ -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) {
Expand Down
17 changes: 17 additions & 0 deletions src/settings/mainSetting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export interface VikunjaPluginSettings {
cronInterval: number,
updateOnStartup: boolean,
updateOnCursorMovement: boolean,
createTaskOnCursorMovement: boolean,
pullTasksOnlyFromDefaultProject: boolean,
availableViews: ModelsProjectView[],
selectedView: number,
Expand Down Expand Up @@ -58,6 +59,7 @@ export const DEFAULT_SETTINGS: VikunjaPluginSettings = {
cronInterval: 500,
updateOnStartup: false,
updateOnCursorMovement: false,
createTaskOnCursorMovement: false,
pullTasksOnlyFromDefaultProject: false,
availableViews: [],
selectedView: 0,
Expand Down Expand Up @@ -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.")
Expand Down
3 changes: 2 additions & 1 deletion versions.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

0 comments on commit 005711f

Please sign in to comment.