Skip to content

Commit

Permalink
feat: hooks for folder config
Browse files Browse the repository at this point in the history
  • Loading branch information
RafaelGB committed Jun 17, 2024
1 parent cd1698c commit 01079e3
Showing 1 changed file with 39 additions and 3 deletions.
42 changes: 39 additions & 3 deletions src/hooks/VaultHooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import ZettelFlow from "main";
import { canvas } from 'architecture/plugin/canvas';
import { log } from "architecture";
import { SelectorMenuModal } from "zettelkasten";
import { MarkdownView } from "obsidian";
import { MarkdownView, TFile, TFolder } from "obsidian";
import { checkSemaphore } from "architecture/plugin";
export class VaultHooks {
public static setup(plugin: ZettelFlow) {
Expand All @@ -15,6 +15,23 @@ export class VaultHooks {
}

private onRename = this.plugin.app.vault.on("rename", (file, oldPath) => {
if (file instanceof TFolder) {
this.onRenameFolder(file, oldPath);
} else if (file instanceof TFile) {
this.onRenameFile(file, oldPath);
}
});

private onRenameFolder(folder: TFolder, oldPath: string) {
const potentialCanvasConfig = `${this.plugin.settings.foldersFlowsPath}/${oldPath.replace(/\//g, "_")}.canvas`;
const potentialCanvasFile = this.plugin.app.vault.getAbstractFileByPath(potentialCanvasConfig);
// If the folder has an associated canvas file, we need to update the canvas file path
if (potentialCanvasFile) {
this.plugin.app.vault.rename(potentialCanvasFile, `${this.plugin.settings.foldersFlowsPath}/${folder.path.replace(/\//g, "_")}.canvas`);
}
}

private onRenameFile(file: TFile, oldPath: string) {
if (oldPath === this.plugin.settings.ribbonCanvas) {
canvas.flows.delete(oldPath);
this.plugin.settings.ribbonCanvas = file.path;
Expand All @@ -25,9 +42,28 @@ export class VaultHooks {
this.plugin.saveSettings();
log.info("Renamed js library folder");
}
});
}

private onDelete = this.plugin.app.vault.on("delete", (file) => {
if (file instanceof TFolder) {
this.onDeleteFolder(file);
} else if (file instanceof TFile) {
this.onDeleteFile(file);
}

});

private onDeleteFolder = (folder: TFolder) => {
const potentialCanvasConfig = `${this.plugin.settings.foldersFlowsPath}/${folder.path.replace(/\//g, "_")}.canvas`;
const potentialCanvasFile = this.plugin.app.vault.getAbstractFileByPath(potentialCanvasConfig);
if (potentialCanvasFile) {
canvas.flows.delete(potentialCanvasFile.path);
this.plugin.app.vault.delete(potentialCanvasFile);
log.info(`Deleted canvas file for folder ${folder.path}: ${potentialCanvasFile.path}`);
}
}

private onDeleteFile = (file: TFile) => {
if (file.path === this.plugin.settings.ribbonCanvas) {
canvas.flows.delete(file.path);
this.plugin.settings.ribbonCanvas = "";
Expand All @@ -37,7 +73,7 @@ export class VaultHooks {
this.plugin.settings.jsLibraryFolderPath = "";
log.info("Deleted canvas file");
}
});
}

private onCreate = this.plugin.app.vault.on("create", async (file) => {
const parent = file.parent;
Expand Down

0 comments on commit 01079e3

Please sign in to comment.