Skip to content

Commit

Permalink
feat: add x-success and x-error param
Browse files Browse the repository at this point in the history
close #30
  • Loading branch information
Vinzent03 committed Nov 16, 2021
1 parent 0294f50 commit 5bda636
Showing 1 changed file with 37 additions and 14 deletions.
51 changes: 37 additions & 14 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { App, Command, FuzzySuggestModal, MarkdownView, normalizePath, Notice, parseFrontMatterAliases, parseFrontMatterEntry, Plugin, PluginSettingTab, Setting, SuggestModal, TFile } from "obsidian";
import { App, Command, FuzzySuggestModal, MarkdownView, normalizePath, Notice, parseFrontMatterAliases, parseFrontMatterEntry, Plugin, PluginSettingTab, request, Setting, SuggestModal, TFile } from "obsidian";
import { appHasDailyNotesPluginLoaded, createDailyNote, getAllDailyNotes, getDailyNote } from "obsidian-daily-notes-interface";
import { v4 as uuidv4 } from 'uuid';
import { getDailyNotePath } from "./daily_note_utils";

const DEFAULT_SETTINGS: AdvancedURISettings = {
openFileOnWrite: true,
openDailyInNewPane: false,
Expand Down Expand Up @@ -38,6 +39,8 @@ interface Parameters {
exists?: string;
viewmode?: "source" | "preview";
settingid?: string;
"x-success"?: string;
"x-error"?: string;
}

export default class AdvancedURI extends Plugin {
Expand Down Expand Up @@ -139,7 +142,7 @@ export default class AdvancedURI extends Plugin {
}

if (parameters.workspace) {
this.handleWorkspace(parameters.workspace);
this.handleWorkspace(parameters);

} else if (parameters.commandname || parameters.commandid) {
this.handleCommand(parameters);
Expand All @@ -162,7 +165,7 @@ export default class AdvancedURI extends Plugin {
} else if (parameters.filepath) {
this.handleOpen(parameters);
} else if (parameters.settingid) {
this.handleOpenSettings(parameters.settingid);
this.handleOpenSettings(parameters);
}
});

Expand All @@ -183,22 +186,33 @@ export default class AdvancedURI extends Plugin {
}));
}

success(parameters: Parameters) {
if (parameters["x-success"])
request({ url: parameters["x-success"], });
}

failure(parameters: Parameters) {
if (parameters["x-error"])
request({ url: parameters["x-error"] });
}

getFileFromUID(uid: string): TFile | undefined {
const files = this.app.vault.getFiles();
const idKey = this.settings.idField;
return files.find(file => parseFrontMatterEntry(this.app.metadataCache.getFileCache(file)?.frontmatter, idKey) == uid);
}

handleWorkspace(workspace: string) {
handleWorkspace(parameters: Parameters) {
const workspaces = (this.app as any)?.internalPlugins?.plugins?.workspaces;
if (!workspaces) {
new Notice("Cannot find Workspaces plugin. Please file an issue.");

this.failure(parameters);
} else if (workspaces.enabled) {
workspaces.instance.loadWorkspace(workspace);

workspaces.instance.loadWorkspace(parameters.workspace);
this.success(parameters);
} else {
new Notice("Workspaces plugin is not enabled");
this.failure(parameters);
}
}

Expand Down Expand Up @@ -238,15 +252,17 @@ export default class AdvancedURI extends Plugin {
} else {
rawCommands[command].checkCallback();
}
return;
break;
}
}
}
this.success(parameters);
}
async handleDoesFileExist(parameters: Parameters) {
const exists = await this.app.vault.adapter.exists(parameters.filepath);

this.copyText((exists ? 1 : 0).toString());
this.success(parameters);

}
async handleSearchAndReplace(parameters: Parameters) {
Expand All @@ -268,16 +284,20 @@ export default class AdvancedURI extends Plugin {
const [, , pattern, flags] = parameters.searchregex.match(/(\/?)(.+)\1([a-z]*)/i);
const regex = new RegExp(pattern, flags);
data = data.replace(regex, parameters.replace);
this.success(parameters);
} catch (error) {
new Notice(`Can't parse ${parameters.searchregex} as RegEx`);
this.failure(parameters);
}
} else {
data = data.replaceAll(parameters.search, parameters.replace);
this.success(parameters);
}

await this.writeAndOpenFile(file.path, data, parameters);
} else {
new Notice("Cannot find file");
this.failure(parameters);
}
}

Expand All @@ -287,26 +307,27 @@ export default class AdvancedURI extends Plugin {

if (parameters.mode === "overwrite") {
this.writeAndOpenFile(path, parameters.data, parameters);

this.success(parameters);
} else if (parameters.mode === "prepend") {
if (file instanceof TFile) {
this.prepend(file, parameters);
} else {
this.prepend(path, parameters);
}

this.success(parameters);
} else if (parameters.mode === "append") {
if (file instanceof TFile) {
this.append(file, parameters);
} else {
this.append(path, parameters);
}

this.success(parameters);
} else if (!createdDailyNote && file instanceof TFile) {
new Notice("File already exists");

this.failure(parameters);
} else {
this.writeAndOpenFile(path, parameters.data, parameters);
this.success(parameters);
}
}

Expand All @@ -324,6 +345,7 @@ export default class AdvancedURI extends Plugin {
if (!fileIsAlreadyOpened)
await this.app.workspace.openLinkText(parameters.filepath, "", this.settings.openFileWithoutWriteInNewPane, this.getViewStateFromMode(parameters));
await this.setCursor(parameters.mode);
this.success(parameters);
}

async append(file: TFile | string, parameters: Parameters) {
Expand Down Expand Up @@ -480,11 +502,12 @@ export default class AdvancedURI extends Plugin {
};
}

handleOpenSettings(id: string) {
handleOpenSettings(parameters: Parameters) {
if ((this.app as any).setting.containerEl.parentElement === null) {
(this.app as any).setting.open();
}
(this.app as any).setting.openTabById(id);
(this.app as any).setting.openTabById(parameters.settingid);
this.success(parameters);
}

async copyURI(parameters: Parameters) {
Expand Down

0 comments on commit 5bda636

Please sign in to comment.