Skip to content

Commit

Permalink
Merge pull request #4 from gavinmn/1.1.1
Browse files Browse the repository at this point in the history
1.1.1
  • Loading branch information
gavinmn authored Feb 22, 2022
2 parents 8da1bf3 + 71e1a86 commit 4560217
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 47 deletions.
103 changes: 59 additions & 44 deletions main.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { create } from 'domain';
import { Editor, EditorPosition, MarkdownView, Plugin, Vault, Workspace } from 'obsidian';

function getObsidianDeepLink(vault: Vault, workspace: Workspace) {
Expand Down Expand Up @@ -47,72 +48,86 @@ function encodeLine(line: string) {
return line
}


function createProject(title: string, deepLink: string) {
const project = `things:///add-project?title=${title}&notes=${deepLink}&x-success=obsidian://project-id`
window.open(project);
}

function createTask(line: string, deepLink: string) {
const task = `things:///add?title=${line}&notes=${deepLink}&x-success=obsidian://things-id`
const task = `things:///add?title=${line}&notes=${deepLink}&x-success=obsidian://task-id`
window.open(task);
}


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}&notes=${obsidianDeepLink}`;
const thingsDeepLink = `things:///show?id=${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
Expand All @@ -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);
});


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": "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",
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4560217

Please sign in to comment.