From 7e42a594ccc213110d474f240311d6ffac56ced2 Mon Sep 17 00:00:00 2001 From: Gavin Nelson Date: Mon, 21 Feb 2022 21:25:12 -0800 Subject: [PATCH 1/2] migrate project creation to using IDs --- main.ts | 103 ++++++++++++++++++++++++++-------------------- package-lock.json | 4 +- 2 files changed, 61 insertions(+), 46 deletions(-) diff --git a/main.ts b/main.ts index 1e5f146..d312e93 100644 --- a/main.ts +++ b/main.ts @@ -1,3 +1,4 @@ +import { create } from 'domain'; import { Editor, EditorPosition, MarkdownView, Plugin, Vault, Workspace } from 'obsidian'; function getObsidianDeepLink(vault: Vault, workspace: Workspace) { @@ -47,8 +48,14 @@ function encodeLine(line: string) { return line } + +function createProject(title: string, deepLink: string) { + const project = `things:///add-project?title=${title}¬es=${deepLink}&x-success=obsidian://task-id` + window.open(project); +} + function createTask(line: string, deepLink: string) { - const task = `things:///add?title=${line}¬es=${deepLink}&x-success=obsidian://things-id` + const task = `things:///add?title=${line}¬es=${deepLink}&x-success=obsidian://task-id` window.open(task); } @@ -56,63 +63,71 @@ function createTask(line: string, deepLink: string) { export default class MyPlugin extends Plugin { async onload() { - - this.addCommand({ - id: 'create-things-project', - name: 'Create Things Project', - editorCallback: (editor: Editor, view: MarkdownView) => { - - const vault = this.app.vault; - const workspace = this.app.workspace; - const projectTitle = workspace.getActiveFile().basename.split('.')[0].split(/\s/).join('%20'); + this.registerObsidianProtocolHandler("project-id", async (id) => { - const obsidianDeepLink = getObsidianDeepLink(vault, workspace); + const projectID = id['x-things-id']; + + const workspace = this.app.workspace; + + const view = workspace.getActiveViewOfType(MarkdownView); + const editor = view.editor + - const thingsURL = `things:///add-project?title=${projectTitle}¬es=${obsidianDeepLink}`; + const thingsDeepLink = `things:///show?query=${projectID}`; + + let fileText = editor.getValue() + const lines = fileText.split('\n'); + const h1Index = lines.findIndex(line => line.startsWith('#')); - const thingsDeepLink = `things:///show?query=${projectTitle}`; + if (h1Index !== -1) { - window.open(thingsURL); - setTimeout(() => { - window.open(thingsDeepLink); - }, 500); - - let fileText = editor.getValue() - const lines = fileText.split('\n'); - const h1Index = lines.findIndex(line => line.startsWith('#')); - if (h1Index !== -1) { + let startRange: EditorPosition = { + line: h1Index, + ch:lines[h1Index].length + } + let endRange: EditorPosition = { + line: h1Index, + ch:lines[h1Index].length + } + + editor.replaceRange(`\n\n[Things](${thingsDeepLink})`, startRange, endRange); + + } else { let startRange: EditorPosition = { - line: h1Index, - ch:lines[h1Index].length - } + line: 0, + ch:0 + } - let endRange: EditorPosition = { - line: h1Index, - ch:lines[h1Index].length - } + let endRange: EditorPosition = { + line: 0, + ch:0 + } - editor.replaceRange(`\n\n[Things](${thingsDeepLink})`, startRange, endRange); + editor.replaceRange(`[Things](${thingsDeepLink})\n\n`, startRange, endRange); + } - } else { - let startRange: EditorPosition = { - line: 0, - ch:0 - } + }); + + this.addCommand({ + id: 'create-things-project', + name: 'Create Things Project', + editorCallback: (editor: Editor, view: MarkdownView) => { + + const vault = this.app.vault; + const workspace = this.app.workspace; - let endRange: EditorPosition = { - line: 0, - ch:0 - } + const projectTitle = workspace.getActiveFile().basename.split('.')[0].split(/\s/).join('%20'); - editor.replaceRange(`[Things](${thingsDeepLink})\n\n`, startRange, endRange); - } + const obsidianDeepLink = getObsidianDeepLink(vault, workspace); + + createProject(projectTitle, obsidianDeepLink); } }); - this.registerObsidianProtocolHandler("things-id", async (id) => { - const thingsID = id['x-things-id']; + this.registerObsidianProtocolHandler("task-id", async (id) => { + const taskID = id['x-things-id']; const view = this.app.workspace.getActiveViewOfType(MarkdownView); const editor = view.editor @@ -136,7 +151,7 @@ export default class MyPlugin extends Plugin { ch: lineLength } - view.editor.replaceRange(`[${line}](things:///show?id=${thingsID})`, startRange, endRange); + view.editor.replaceRange(`[${line}](things:///show?id=${taskID})`, startRange, endRange); }); diff --git a/package-lock.json b/package-lock.json index d5c55c2..8830770 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { - "name": "obsidian-sample-plugin", - "version": "1.0.1", + "name": "obsidian-things-link", + "version": "1.1.0", "lockfileVersion": 1, "requires": true, "dependencies": { From 71e1a8632e3bae6b218cfaca0fb0f59f5fe73c6e Mon Sep 17 00:00:00 2001 From: Gavin Nelson Date: Mon, 21 Feb 2022 21:36:40 -0800 Subject: [PATCH 2/2] use ID instead of query --- main.ts | 4 ++-- manifest.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/main.ts b/main.ts index d312e93..a288eda 100644 --- a/main.ts +++ b/main.ts @@ -50,7 +50,7 @@ function encodeLine(line: string) { function createProject(title: string, deepLink: string) { - const project = `things:///add-project?title=${title}¬es=${deepLink}&x-success=obsidian://task-id` + const project = `things:///add-project?title=${title}¬es=${deepLink}&x-success=obsidian://project-id` window.open(project); } @@ -74,7 +74,7 @@ export default class MyPlugin extends Plugin { const editor = view.editor - const thingsDeepLink = `things:///show?query=${projectID}`; + const thingsDeepLink = `things:///show?id=${projectID}`; let fileText = editor.getValue() const lines = fileText.split('\n'); diff --git a/manifest.json b/manifest.json index b5c7717..cb3be89 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { "id": "obsidian-things-link", "name": "Things Link", - "version": "1.1.0", + "version": "1.1.1", "minAppVersion": "0.12.0", "description": "Seamlessly create Things tasks and projects from Obsidian", "author": "@gavmn",