diff --git a/src/modules/resolver.ts b/src/modules/resolver.ts index 2496e21..0cbf185 100644 --- a/src/modules/resolver.ts +++ b/src/modules/resolver.ts @@ -136,6 +136,27 @@ export default class NoteResolver { path: dir === "/" ? basename + ".md" : join(dir, basename + ".md"), }; }; + getFolderNotePathAfterMove: API["getFolderNotePathAfterMove"] = (folderAfterMove, folderBeforeMovePath, strategy) => { + if (!folderBeforeMovePath) { + return null; + } + + if (strategy === undefined) strategy = this.settings.folderNotePref; + + switch (strategy) { + case NoteLoc.Index: + return this.findFolderNote(this.getFolderNotePath(folderAfterMove.path, strategy)); + case NoteLoc.Inside: + const notePath = join(folderAfterMove.path, getBase(folderBeforeMovePath) + ".md") + const note = this.vault.getAbstractFileByPath(notePath); + if (note && note instanceof TFile) return note; + else return null; + case NoteLoc.Outside: + return this.findFolderNote(this.getFolderNotePath(folderBeforeMovePath, strategy)); + default: + assertNever(strategy); + } + }; // Note Operations diff --git a/src/modules/vault-handler.ts b/src/modules/vault-handler.ts index 0e87450..be485b0 100644 --- a/src/modules/vault-handler.ts +++ b/src/modules/vault-handler.ts @@ -34,7 +34,7 @@ export default class VaultHandler { registerEvent = () => { this.plugin.registerEvent(this.on("create", this.onChange)); - this.plugin.registerEvent(this.on("rename", this.onChange)); + this.plugin.registerEvent(this.on("rename", this.onRename)); this.plugin.registerEvent(this.on("delete", this.onDelete)); }; @@ -55,8 +55,12 @@ export default class VaultHandler { return renameOnly || syncLoc; } + onRename = (af: TAbstractFile, oldPath: string) => { + setTimeout(() => this.onChange(af, oldPath), 500); + } + onChange = (af: TAbstractFile, oldPath?: string) => { - const { getFolderNote, getFolderFromNote, getFolderNotePath } = this.finder; + const { getFolderNote, getFolderFromNote, getFolderNotePath, getFolderNotePathAfterMove } = this.finder; function getOldLinked(af: TFile): TFolder | null; function getOldLinked(af: TFolder): TFile | null; @@ -64,7 +68,7 @@ export default class VaultHandler { // eslint-disable-next-line prefer-arrow/prefer-arrow-functions function getOldLinked(af: TAbstractFile): TFile | TFolder | null { if (af instanceof TFolder) { - return oldPath ? getFolderNote(oldPath) : null; + return getFolderNotePathAfterMove(af, oldPath); } else if (af instanceof TFile) { return oldPath && isMd(oldPath) ? getFolderFromNote(oldPath) : null; } else return null; diff --git a/src/typings/api.ts b/src/typings/api.ts index 9fa3cda..63b2887 100644 --- a/src/typings/api.ts +++ b/src/typings/api.ts @@ -57,6 +57,8 @@ export default interface FolderNoteAPI { /** Generate folder note content for given folder based on template */ getNewFolderNote(folder: TFolder): string; + getFolderNotePathAfterMove(folderAfterMove: TFolder, folderBeforeMovePath?: string, strategy?: NoteLoc): TFile | null; + OpenFolderNote( folder: TFolder | string, dryrun?: boolean, @@ -154,6 +156,9 @@ export const getApi = (plugin: FNCore): FolderNoteAPI => { get getFolderNotePath() { return plugin.resolver.getFolderNotePath; }, + get getFolderNotePathAfterMove() { + return plugin.resolver.getFolderNotePathAfterMove; + }, get DeleteLinkedFolder() { return plugin.resolver.DeleteLinkedFolder; },