Skip to content

Commit

Permalink
add setting and dont sync completed tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
rmevans9 committed Sep 2, 2022
1 parent f0a480b commit 68e84fc
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 12 deletions.
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "obsidian-omnifocus",
"name": "Omnifocus",
"version": "0.0.1",
"version": "0.0.2",
"minAppVersion": "0.15.0",
"description": "This is a sample plugin for Obsidian. This plugin demonstrates some of the capabilities of the Obsidian API.",
"author": "Richard Evans",
Expand Down
4 changes: 2 additions & 2 deletions src/dataviewApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ export function fetchOmnifocusTaggedTasks() {
}

// TODO: Consider having the pages this a setting!
const pages = dataViewApi.pages("").values as ObisidianPage[];
const pages = dataViewApi.pages("#omnifocus").values as ObisidianPage[];

const tasks = pages.reduce<ObsidianTask[]>((currTasks, page) => {
const pageTasks = page.file.tasks.values.reduce<ObsidianTask[]>(
(currPageTasks, task) => {
if (task.tags.indexOf("#omnifocus") > -1) {
if (!task.completed && task.tags.indexOf("#omnifocus") > -1) {
currPageTasks.push(task);
}

Expand Down
50 changes: 48 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,62 @@
import { Plugin } from "obsidian";
import { App, Plugin, PluginSettingTab, Setting } from "obsidian";

import { setApp } from "./obsidianApi";
import { DEFAULT_SETTINGS, ObsidianOmnifocusSettings } from "./settings";
import { syncTasks } from "./syncManager";

export default class ObsidianOmnifocus extends Plugin {
settings: ObsidianOmnifocusSettings;

async onload() {
await this.loadSettings();

setApp(this.app);

this.addCommand({
id: "obsidian-omnifocus-sync",
name: "Sync tagged tasks to OmniFocus",
callback: () => syncTasks(this.app.vault.getName()),
callback: () => syncTasks(this.app.vault.getName(), this.settings),
});

this.addSettingTab(new ObsidianOmnifocusSettingTab(this.app, this));
}

async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}

async saveSettings() {
await this.saveData(this.settings);
}
}

export class ObsidianOmnifocusSettingTab extends PluginSettingTab {
plugin: ObsidianOmnifocus;

constructor(app: App, plugin: ObsidianOmnifocus) {
super(app, plugin);
this.plugin = plugin;
}

display(): void {
const { containerEl } = this;

containerEl.empty();

containerEl.createEl("h2", { text: "Settings" });

new Setting(containerEl)
.setName("Mark task completed on sync?")
.setDesc(
"This will mark the task in Obsidian as completed when it is sync'd to OmniFocus"
)
.addToggle((toggle) => {
toggle
.setValue(this.plugin.settings.markCompletedOnSync)
.onChange(async (value) => {
this.plugin.settings.markCompletedOnSync = value;
await this.plugin.saveSettings();
});
});
}
}
13 changes: 12 additions & 1 deletion src/obsidianApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ export async function updateNote(
startPos: number,
endPos: number,
needle: string,
replace: string
replace: string,
markCompleted: boolean
) {
if (!_app) {
throw new Error("No app defined!");
Expand Down Expand Up @@ -41,6 +42,16 @@ export async function updateNote(
newText += replace;
newText += currentText.substring(startOfNeedle + needle.length);

if (markCompleted) {
// TODO: This could use a TON of optimization because we are rebuilding the text AGAIN!
const startOfCheckbox = newText.indexOf("[", startPos);
const prevStepText = newText;

newText = prevStepText.substring(0, startOfCheckbox + 1);
newText += "x";
newText += prevStepText.substring(startOfCheckbox + 2);
}

// For debugging for now
console.log("Original Text", currentText);
console.log("Updated Text", newText);
Expand Down
7 changes: 7 additions & 0 deletions src/settings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export interface ObsidianOmnifocusSettings {
markCompletedOnSync: boolean;
}

export const DEFAULT_SETTINGS: ObsidianOmnifocusSettings = {
markCompletedOnSync: false,
};
19 changes: 14 additions & 5 deletions src/syncManager.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { createTask, Task } from "@jacobx1/of-sdk";
import { createTask } from "@jacobx1/of-sdk";
import { fetchOmnifocusTaggedTasks, ObsidianTask } from "./dataviewApi";
import { updateNote } from "./obsidianApi";
import { ObsidianOmnifocusSettings } from "./settings";

async function createTaskAndUpdate(vaultName: string, task: ObsidianTask) {
async function createTaskAndUpdate(
vaultName: string,
task: ObsidianTask,
pluginSettings: ObsidianOmnifocusSettings
) {
await createTask(task.text, {
note: `obsidian://open?vault=${encodeURI(vaultName)}&file=${encodeURI(
task.path
Expand All @@ -14,11 +19,15 @@ async function createTaskAndUpdate(vaultName: string, task: ObsidianTask) {
task.position.start.offset,
task.position.end.offset,
"#omnifocus ", // TODO: Pull in via setting?
"#omnifocus-synced "
"#omnifocus-synced ",
pluginSettings.markCompletedOnSync
);
}

export async function syncTasks(vaultName: string) {
export async function syncTasks(
vaultName: string,
pluginSettings: ObsidianOmnifocusSettings
) {
// TODO: Consider if we should allow per page syncing of just do the entire vault?
console.log("[obsidian-omnifocus] Fetching Tagged Tasks...");
const tasks = fetchOmnifocusTaggedTasks();
Expand All @@ -27,7 +36,7 @@ export async function syncTasks(vaultName: string) {
console.log("[obsidian-omnifocus] Iterating over tasks to send to Omni...");
const creationTasks: Promise<void>[] = [];
tasks.forEach((task) => {
creationTasks.push(createTaskAndUpdate(vaultName, task));
creationTasks.push(createTaskAndUpdate(vaultName, task, pluginSettings));
});
console.log("[obsidian-omnifocus] Finished iterating over tasks.");

Expand Down
3 changes: 2 additions & 1 deletion versions.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"0.0.1": "0.15.0"
"0.0.1": "0.15.0",
"0.0.2": "0.15.0"
}

0 comments on commit 68e84fc

Please sign in to comment.