Now it unloads and loads the plugin properly from the preference pane.

This commit is contained in:
Andrea Alberti 2024-06-03 18:40:16 +02:00
parent 42948f23cc
commit e67f97dff0
2 changed files with 29 additions and 7 deletions

View file

@ -1,7 +1,7 @@
{
"id": "plugins-annotations",
"name": "Plugins Annotations",
"version": "1.0.6",
"version": "1.0.7",
"minAppVersion": "1.5.0",
"description": "Allows adding personal comments to each installed plugin.",
"author": "Obsidian",

View file

@ -24,6 +24,7 @@ export default class PluginsAnnotations extends Plugin {
private skipNextAddComments = false;
private saveTimeout: number | null = null;
private fsWatcher: fs.FSWatcher | null = null;
private observedTab: SettingTab | null = null;
async onload() {
// console.log('Loading Plugins Annotations');
@ -38,6 +39,11 @@ export default class PluginsAnnotations extends Plugin {
this.app.workspace.onLayoutReady(() => {
this.patchSettings();
const activeTab = this.app.setting.activeTab;
if (activeTab && activeTab.id === 'community-plugins') {
this.observeTab(activeTab);
}
});
}
@ -50,7 +56,7 @@ export default class PluginsAnnotations extends Plugin {
return filePath;
}
getPluginNameToIdMap(): Record < string, string > {
constructPluginNameToIdMap() {
const map: Record < string, string > = {};
for (const pluginId in this.app.plugins.manifests) {
const plugin = this.app.plugins.manifests[pluginId];
@ -58,7 +64,7 @@ export default class PluginsAnnotations extends Plugin {
map[plugin.name] = plugin.id;
}
}
return map;
this.pluginNameToIdMap = map;
}
patchSettings() {
@ -71,8 +77,6 @@ export default class PluginsAnnotations extends Plugin {
return function(this: Setting, tab: SettingTab) {
next.call(this, tab);
if (tab && tab.id === 'community-plugins') {
// Create a mapping of plugin names to IDs
self.pluginNameToIdMap = self.getPluginNameToIdMap();
self.observeTab(tab);
}
};
@ -88,8 +92,13 @@ export default class PluginsAnnotations extends Plugin {
});
}
observeTab(tab: SettingTab) {
observeTab(tab: SettingTab) {
// Create a mapping of plugin names to IDs
this.constructPluginNameToIdMap();
if(!this.mutationObserver) {
this.observedTab = tab;
const observer = new MutationObserver(() => {
if(!this.skipNextAddComments){
this.skipNextAddComments = true;
@ -113,6 +122,7 @@ export default class PluginsAnnotations extends Plugin {
if (this.mutationObserver) {
this.mutationObserver.disconnect();
this.mutationObserver = null;
this.observedTab = null;
}
}
@ -225,15 +235,27 @@ export default class PluginsAnnotations extends Plugin {
}, timeout_ms);
}
removeCommentsFromTab() {
if (this.observedTab) {
const commentElements = this.observedTab.containerEl.querySelectorAll('.plugin-comment');
commentElements.forEach(element => {
element.remove();
});
}
}
onunload() {
// console.log('Unloading Plugins Annotations');
// Remove all comments
this.removeCommentsFromTab();
// Just in case, disconnect observers if they still exist
this.disconnectObservers();
// Uninstall the monkey patch
if (this.removeMonkeyPatch) {
this.removeMonkeyPatch();
}
}
}
}