From 605b6b12615468783b83ef91762c064e16c50c7c Mon Sep 17 00:00:00 2001 From: Michael Naumov Date: Wed, 11 May 2022 18:35:11 -0600 Subject: [PATCH 1/2] Accurately handle folder notes after note moved --- src/modules/resolver.ts | 21 +++++++++++++++++++++ src/modules/vault-handler.ts | 4 ++-- src/typings/api.ts | 5 +++++ 3 files changed, 28 insertions(+), 2 deletions(-) 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..374d948 100644 --- a/src/modules/vault-handler.ts +++ b/src/modules/vault-handler.ts @@ -56,7 +56,7 @@ export default class VaultHandler { } 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 +64,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; }, From 9c8f99c3f123406ec7c069b5d6b6d6679cb08e5b Mon Sep 17 00:00:00 2001 From: Michael Naumov Date: Wed, 11 May 2022 18:36:13 -0600 Subject: [PATCH 2/2] Add delay before processing renames to wait for... cache update --- src/modules/vault-handler.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/modules/vault-handler.ts b/src/modules/vault-handler.ts index 374d948..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,6 +55,10 @@ 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, getFolderNotePathAfterMove } = this.finder;