From 900e1d99bac606629f6efe8ff1f1630f251b6acc Mon Sep 17 00:00:00 2001 From: caffae Date: Sat, 8 Mar 2025 18:19:16 +0800 Subject: [PATCH] fixed the cleaning up of the leaves (panels) --- current-folder-notes/manifest.json | 4 +- main.ts | 89 ++++++++++++++---------------- prepareForGitReleaseNreplace.sh | 12 ++-- 3 files changed, 49 insertions(+), 56 deletions(-) diff --git a/current-folder-notes/manifest.json b/current-folder-notes/manifest.json index 79ea0ba..fac8ef1 100644 --- a/current-folder-notes/manifest.json +++ b/current-folder-notes/manifest.json @@ -1,11 +1,11 @@ { "id": "current-folder-notes-pamphlet", "name": "Current Folder Notes", - "version": "1.7.3", + "version": "1.7.4", "minAppVersion": "1.7.7", "description": "Shows a list of notes in the current folder, and allows you to filter the titles to include or exclude notes.", "author": "Pamela Wang", "authorUrl": "https://github.com/Caffa", "fundingUrl": "https://ko-fi.com/pamelawang_mwahacookie", "isDesktopOnly": false -} +} \ No newline at end of file diff --git a/main.ts b/main.ts index ddea2e1..b4c1b1f 100644 --- a/main.ts +++ b/main.ts @@ -41,11 +41,10 @@ export default class CurrentFolderNotesDisplay extends Plugin { // add a panel to the right sidebar - view - // this.registerView(VIEW_TYPE_CURRENT_FOLDER_NOTES_DISPLAY, (leaf) => new CurrentFolderNotesDisplayView(leaf)); this.registerView(VIEW_TYPE_CURRENT_FOLDER_NOTES_DISPLAY, (leaf) => new CurrentFolderNotesDisplayView(leaf, this)); + // Add a ribbon icon this.addRibbonIcon('folder', 'Activate folder notes display', () => { - // new Notice('This is a notice!'); this.activateView(); }); @@ -60,17 +59,9 @@ export default class CurrentFolderNotesDisplay extends Plugin { // when file is changes (opened) in the editor, update the view this.registerEvent(this.app.workspace.on('file-open', async (file) => { - // let views = this.app.workspace.getLeavesOfType(CurrentFolderNotesDisplayView); - // let view = this.app.workspace.getActiveViewOfType(CurrentFolderNotesDisplayView); this.refreshView(); - })); - // when a file is saved, update the view - // this.registerEvent(this.app.vault.on('modify', async (file) => { - // this.refreshView(); - // })); - // when a file is deleted, update the view this.registerEvent(this.app.vault.on('delete', async (file) => { this.refreshView(); @@ -85,41 +76,46 @@ export default class CurrentFolderNotesDisplay extends Plugin { this.registerEvent(this.app.vault.on('rename', async (file) => { this.refreshView(); })); - - // when I switch active files, update the view - // this.registerEvent(this.app.workspace.on('active-leaf-change', async (leaf) => { - // this.refreshView(); - // })); - - } async onunload() { console.log('unloading plugin'); - // Don't detach leaves in onunload. When the user updates your plugin, any open leaves will be reinitialized at their original position, regardless of where the user had moved them. - // this.leaves.forEach(leaf => { - // leaf.detach(); - // }); - + // Clean up by detaching any leaves created by this plugin + const leavesToDetach = this.app.workspace.getLeavesOfType(VIEW_TYPE_CURRENT_FOLDER_NOTES_DISPLAY); + leavesToDetach.forEach(leaf => { + leaf.detach(); + }); - + // Clear the leaves array + this.leaves = []; } async activateView() { const { workspace } = this.app; + + // Clean up existing leaves first to prevent duplicates + const existingLeaves = workspace.getLeavesOfType(VIEW_TYPE_CURRENT_FOLDER_NOTES_DISPLAY); + + // Keep only the first leaf if multiple exist + if (existingLeaves.length > 1) { + for (let i = 1; i < existingLeaves.length; i++) { + existingLeaves[i].detach(); + } + } + let leaf: WorkspaceLeaf | null = null; - const leaves = workspace.getLeavesOfType(VIEW_TYPE_CURRENT_FOLDER_NOTES_DISPLAY); - - if (leaves.length) { + + if (existingLeaves.length) { // A leaf with our view already exists, use that - leaf = leaves[0]; + leaf = existingLeaves[0]; } else { // Our view could not be found in the workspace, create a new leaf // in the right sidebar for it leaf = workspace.getRightLeaf(false); if (leaf) { await leaf.setViewState({ type: VIEW_TYPE_CURRENT_FOLDER_NOTES_DISPLAY, active: true }); + // Only add to leaves array if it's a new leaf this.leaves.push(leaf); } } @@ -134,29 +130,23 @@ export default class CurrentFolderNotesDisplay extends Plugin { async refreshView() { const { workspace } = this.app; const leaves = workspace.getLeavesOfType(VIEW_TYPE_CURRENT_FOLDER_NOTES_DISPLAY); - if (leaves.length) { - // A leaf with our view already exists, use that - const view = leaves[0].view as CurrentFolderNotesDisplayView; - await view.displayNotesInCurrentFolder(); - - // Remove all other leaves + + // Make sure we only have one leaf of our type + if (leaves.length > 1) { + // Keep the first one, detach others for (let i = 1; i < leaves.length; i++) { leaves[i].detach(); } - - } else { - // new Notice('Could not find the view'); + } + + if (leaves.length === 1) { + // A leaf with our view exists, update it + const view = leaves[0].view as CurrentFolderNotesDisplayView; + await view.displayNotesInCurrentFolder(); + } else if (leaves.length === 0) { + // No leaf exists, create one this.activateView(); } - - // // if this view is not open do nothing - // let view = this.app.workspace.getActiveViewOfType(CurrentFolderNotesDisplayView); - // if (view) { - // await view.displayNotesInCurrentFolder(); - // } - - - } async loadSettings() { @@ -204,9 +194,12 @@ export class CurrentFolderNotesDisplayView extends ItemView { } async onClose() { - // close current view - - } + // Remove this leaf from the plugin's leaves array to avoid memory leaks + const index = this.plugin.leaves.indexOf(this.leaf); + if (index > -1) { + this.plugin.leaves.splice(index, 1); + } + } // main.ts diff --git a/prepareForGitReleaseNreplace.sh b/prepareForGitReleaseNreplace.sh index 200f9b5..9b8b5ac 100644 --- a/prepareForGitReleaseNreplace.sh +++ b/prepareForGitReleaseNreplace.sh @@ -9,13 +9,13 @@ cp main.js current-folder-notes cp manifest.json current-folder-notes cp styles.css current-folder-notes -rm -f "/Users/caffae/Notes/Char V4/.obsidian/plugins/Current Folder Notes/main.js" -rm -f "/Users/caffae/Notes/Char V4/.obsidian/plugins/Current Folder Notes/manifest.json" -rm -f "/Users/caffae/Notes/Char V4/.obsidian/plugins/Current Folder Notes/styles.css" +rm -f "/Users/caffae/Notes/2024 Juntaria/.obsidian/plugins/Current Folder Notes/main.js" +rm -f "/Users/caffae/Notes/2024 Juntaria/.obsidian/plugins/Current Folder Notes/manifest.json" +rm -f "/Users/caffae/Notes/2024 Juntaria/.obsidian/plugins/Current Folder Notes/styles.css" -cp main.js "/Users/caffae/Notes/Char V4/.obsidian/plugins/Current Folder Notes" -cp manifest.json "/Users/caffae/Notes/Char V4/.obsidian/plugins/Current Folder Notes" -cp styles.css "/Users/caffae/Notes/Char V4/.obsidian/plugins/Current Folder Notes" +cp main.js "/Users/caffae/Notes/2024 Juntaria/.obsidian/plugins/Current Folder Notes" +cp manifest.json "/Users/caffae/Notes/2024 Juntaria/.obsidian/plugins/Current Folder Notes" +cp styles.css "/Users/caffae/Notes/2024 Juntaria/.obsidian/plugins/Current Folder Notes" # compress the current_release folder into a zip file # zip -r release.zip current_release