Skip to content

Commit

Permalink
Merge pull request #37 from cloud-atlas-ai:muness/issue35
Browse files Browse the repository at this point in the history
Make link back to Obsidian configurable
  • Loading branch information
muness authored Dec 28, 2023
2 parents f2bd85d + ce02ec5 commit 0ad796f
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 30 deletions.
33 changes: 20 additions & 13 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,12 @@ export default class AmazingMarvinPlugin extends Plugin {
}

if (notePath && notePath !== '') {
let encodedVaultName = vaultName !== '' ? encodeURIComponent(vaultName) : '';
let encodedNotePath = encodeURIComponent(encodeURIComponent(notePath));
requestBody.note = `[🏷️](obsidian://advanced-uri?filepath=${encodedNotePath}${encodedVaultName !== '' ? `&vault=${encodedVaultName}` : ''})`;
let link = `obsidian://open?file=${encodeURI(notePath)}${vaultName !== '' ? `&vault=${encodeURI(vaultName)}` : ''}`;
if (this.settings.linkBackToObsidianText !== '') {
requestBody.note = `[${this.settings.linkBackToObsidianText}](${link})`;
} else {
requestBody.note = link;
}
}

try {
Expand Down Expand Up @@ -253,21 +256,25 @@ export default class AmazingMarvinPlugin extends Plugin {
body: JSON.stringify(requestBody)
});

const note = document.createDocumentFragment();
const a = document.createElement('a');
a.href = 'https://app.amazingmarvin.com/#t=' + taskId;

a.target = '_blank';

if (remoteResponse.status === 200) {
new Notice("Task marked as done in Amazing Marvin.");
a.text = 'Task';
note.append(a);
note.appendText(' marked as done in Amazing Marvin.');
new Notice(note, 5000);
return remoteResponse.json;
} else if (remoteResponse.status === 429) {
const errorNote = document.createDocumentFragment();
errorNote.appendText('Your request was throttled by Amazing Marvin. Or do it manually at ');
console.error('Your request was throttled by Amazing Marvin. Wait a few minutes and try again. Or do it manually.');
const a = document.createElement('a');
a.href = 'https://app.amazingmarvin.com/#t=' + taskId;
a.text = 'manually';
a.target = '_blank';
errorNote.appendChild(a);

new Notice(errorNote, 0);
note.appendText('Your request was throttled by Amazing Marvin. Do it manually at ');
console.error('Your request was throttled by Amazing Marvin. Wait a few minutes and try again. Or do it manually.');
note.appendChild(a);

new Notice(note, 0);
}
} catch (error) {
const errorNote = document.createDocumentFragment();
Expand Down
53 changes: 36 additions & 17 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { App, Platform, PluginSettingTab, Setting } from "obsidian";
import AmazingMarvinPlugin from "./main";

export interface AmazingMarvinPluginSettings {
linkBackToObsidianText: string;
attemptToMarkTasksAsDone: any;
useLocalServer: boolean;
localServerHost: string;
Expand All @@ -14,6 +15,7 @@ export interface AmazingMarvinPluginSettings {
}

export const DEFAULT_SETTINGS: AmazingMarvinPluginSettings = {
linkBackToObsidianText: '',
useLocalServer: false,
localServerHost: "localhost",
localServerPort: 12082,
Expand All @@ -33,30 +35,24 @@ export class AmazingMarvinSettingsTab extends PluginSettingTab {
this.plugin = plugin;
}

private getAPILink(): HTMLAnchorElement {
const a = document.createElement('a');
a.href = 'https://app.amazingmarvin.com/pre?api';
a.text = 'API page';
a.target = '_blank';
return a;
}
// refactor a function for link creation that takes the href and text as parameters

private getLocalAPIDocs(): HTMLAnchorElement {
const a = document.createElement('a');
a.href = 'https://help.amazingmarvin.com/en/articles/5165191-desktop-local-api-server';
a.text = 'Desktop Local API Server';
a.target = '_blank';
return a;
}
private a(href: string, text: string) {
const a = document.createElement('a');
a.href = href;
a.text = text;
a.target = '_blank';
return a;

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

containerEl.empty();

const TokenDescEl = document.createDocumentFragment();
TokenDescEl.appendText('Get your Token at the ');
TokenDescEl.appendChild(this.getAPILink());
TokenDescEl.appendChild(this.a('https://app.amazingmarvin.com/pre?api', 'API page'));

new Setting(containerEl)
.setName("API Token")
Expand All @@ -73,7 +69,7 @@ export class AmazingMarvinSettingsTab extends PluginSettingTab {

new Setting(containerEl)
.setName("Mark tasks as done")
.setDesc("Attempt to mark tasks as done in Amazing Marvin")
.setDesc("Attempt to mark tasks as done in Amazing Marvin. Note that this only applies to Amazing Marvins tasks imported or created with this plugin.")
.addToggle(toggle => toggle
.setValue(this.plugin.settings.attemptToMarkTasksAsDone)
.onChange(async (value) => {
Expand All @@ -99,6 +95,29 @@ export class AmazingMarvinSettingsTab extends PluginSettingTab {
})
);

new Setting(containerEl)
.setHeading().setName("Task creation");


const noteLink = document.createDocumentFragment();
// make this text much shorter
noteLink.appendText('Text for note back to Obsidian on tasks created with this plugin. If empty, a link be added.');
noteLink.append(document.createElement('br'));

new Setting(containerEl)
.setName("Note link text")
.setDesc(noteLink)
.addText((text) =>
text
.setPlaceholder("Note link text")
.setValue(this.plugin.settings.linkBackToObsidianText)
.onChange(async (value) => {
this.plugin.settings.linkBackToObsidianText = value.trim();
await this.plugin.saveSettings();
})
);


new Setting(containerEl)
.setHeading().setName("Task formatting");

Expand Down Expand Up @@ -135,7 +154,7 @@ export class AmazingMarvinSettingsTab extends PluginSettingTab {
if (Platform.isDesktopApp) {
const lsDescEl = document.createDocumentFragment();
lsDescEl.appendText('The local API can speed up the plugin. See the ');
lsDescEl.appendChild(this.getLocalAPIDocs());
lsDescEl.appendChild(this.a('https://help.amazingmarvin.com/en/articles/5165191-desktop-local-api-server', 'Desktop Local API Server'));
lsDescEl.appendText(' for more information.');

let ls = new Setting(containerEl)
Expand Down

0 comments on commit 0ad796f

Please sign in to comment.