diff --git a/LICENSE b/LICENSE index 8aa2645..51456f5 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) [year] [fullname] +Copyright (c) 2024 Andrea Alberti Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/manifest.json b/manifest.json index 2d6a5b8..b97ef3b 100644 --- a/manifest.json +++ b/manifest.json @@ -4,7 +4,7 @@ "version": "1.0.10", "minAppVersion": "1.5.0", "description": "Allows adding personal comments to each installed plugin.", - "author": "Obsidian", + "author": "Andrea Alberti", "authorUrl": "https://www.linkedin.com/in/dr-andrea-alberti/", "fundingUrl": "https://buymeacoffee.com/alberti", "isDesktopOnly": false diff --git a/src/db.ts b/src/db.ts index dc9e052..32cf6b3 100644 --- a/src/db.ts +++ b/src/db.ts @@ -2,55 +2,32 @@ import { Vault } from 'obsidian'; -let annotationFilePath: string | null = null; +import PluginsAnnotations from './main'; -interface PluginAnnotation { - [pluginId: string]: string; +import { PluginAnnotationDict } from './types'; + +let plugin: PluginsAnnotations | null = null; + +function setPluginObj(p: PluginsAnnotations) { + plugin = p; } -function setAnnotationFilePath(path: string) { - annotationFilePath = path; -} - - -function isNodeJsError(error: unknown): error is NodeJS.ErrnoException { - return error instanceof Error && 'code' in error; -} - -async function loadAnnotations(vault: Vault): Promise < PluginAnnotation > { - if (!annotationFilePath) { - console.error('Could not load annotations. Failed to retrieve the path of the annotation file.'); +async function loadAnnotations(vault: Vault): Promise < PluginAnnotationDict > { + if(!plugin) { return {}; } - try { - const file = await vault.adapter.read(annotationFilePath); - return JSON.parse(file); - } catch (error) { - if (isNodeJsError(error) && error.code === 'ENOENT') { - // File does not exist, return an empty object - // console.warn('Annotations file not found, loading empty annotations.'); - return {}; - } else { - // Failed, return an empty object - console.error('Failed to load annotations:', error); - return {}; + return Object.assign({}, {}, await plugin.loadData()); +} + +async function saveAnnotations(vault: Vault, annotationsDict: PluginAnnotationDict): Promise < void > { + if (plugin) { + try { + plugin.saveData(annotationsDict); + } catch (error) { + console.error('Failed to save annotations:', error); } } } -async function saveAnnotations(vault: Vault, annotations: PluginAnnotation): Promise < void > { - if (!annotationFilePath) { - console.error('Could not save annotations. Failed to retrieve the path of the annotation file.'); - return; - } - - try { - const data = JSON.stringify(annotations, null, 2); - await vault.adapter.write(annotationFilePath, data); - } catch (error) { - console.error('Failed to save annotations:', error); - } -} - -export { saveAnnotations, loadAnnotations, setAnnotationFilePath } +export { saveAnnotations, loadAnnotations, setPluginObj } diff --git a/src/main.ts b/src/main.ts index c3433af..5e575a0 100644 --- a/src/main.ts +++ b/src/main.ts @@ -5,38 +5,26 @@ import { Setting, SettingTab, Platform, - normalizePath, // PluginSettingTab, // App, } from 'obsidian'; import { around } from 'monkey-around'; -import { setAnnotationFilePath, loadAnnotations, saveAnnotations } from './db'; -import * as fs from 'fs'; - -interface PluginAnnotation { - [pluginId: string]: string; -} +import * as db from './db'; +import { PluginAnnotationDict } from './types'; export default class PluginsAnnotations extends Plugin { - private annotations: PluginAnnotation = {}; + private annotations: PluginAnnotationDict = {}; private pluginNameToIdMap ? : Record < string, string >; private mutationObserver: MutationObserver | null = null; private removeMonkeyPatch: (() => void) | null = null; 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'); - const annotationsFilePath = await this.getAnnotationsFilePath(); - if (!annotationsFilePath) { - console.error(`The plugin '${this.manifest.name}' could not be loaded. The path to the annotation file could not be found.`); - return; - } - - setAnnotationFilePath(annotationsFilePath); + db.setPluginObj(this); this.app.workspace.onLayoutReady(() => { this.patchSettings(); @@ -48,15 +36,6 @@ export default class PluginsAnnotations extends Plugin { }); } - async getAnnotationsFilePath(): Promise { - if (!this.manifest.id) { - return null; - } - const pluginFolder = this.app.vault.configDir; - const filePath = normalizePath(`${pluginFolder}/plugins/${this.manifest.id}/data.json`); - return filePath; - } - constructPluginNameToIdMap() { const map: Record < string, string > = {}; for (const pluginId in this.app.plugins.manifests) { @@ -128,7 +107,7 @@ export default class PluginsAnnotations extends Plugin { } async addComments(tab: SettingTab) { - this.annotations = await loadAnnotations(this.app.vault); + this.annotations = await db.loadAnnotations(this.app.vault); const pluginsContainer = tab.containerEl.querySelector('.installed-plugins-container'); if (!pluginsContainer) return; @@ -239,7 +218,7 @@ export default class PluginsAnnotations extends Plugin { } this.saveTimeout = window.setTimeout(() => { - saveAnnotations(this.app.vault, this.annotations); + db.saveAnnotations(this.app.vault, this.annotations); this.saveTimeout = null; }, timeout_ms); } diff --git a/styles/styles.css b/styles/styles.css index 8399c98..b4200a4 100644 --- a/styles/styles.css +++ b/styles/styles.css @@ -1,10 +1,5 @@ /* styles.css */ -/* Ensure the parent container uses full width */ -.setting-item-info { - width: 100%; -} - /* Make sure the input field takes the full width of its parent */ .plugin-comment { width: 100%;