From 2fc64d41f145a538e61a022b26b17ddf4929ca2f Mon Sep 17 00:00:00 2001 From: Andrea Alberti Date: Sat, 1 Jun 2024 19:57:28 +0200 Subject: [PATCH] Extending database --- src/db.ts | 27 +++++++++++++++++++++++---- src/main.ts | 33 +++++++++++++++++++++++---------- styles/styles.css | 9 +++++++-- 3 files changed, 53 insertions(+), 16 deletions(-) diff --git a/src/db.ts b/src/db.ts index 75b8328..ccedf27 100644 --- a/src/db.ts +++ b/src/db.ts @@ -8,20 +8,39 @@ interface PluginAnnotation { [pluginId: string]: string; } +function isNodeJsError(error: any): error is NodeJS.ErrnoException { + return error && typeof error.code === 'string'; +} + +async function getDbFilePath(vault: Vault): Promise { + const pluginFolder = vault.configDir; + return `${pluginFolder}/${DB_FILE}`; +} + export async function loadAnnotations(vault: Vault): Promise { + const filePath = await getDbFilePath(vault); + try { - const file = await vault.adapter.read(DB_FILE); + const file = await vault.adapter.read(filePath); return JSON.parse(file); } catch (error) { - console.error('Failed to load annotations:', error); - return {}; + if (isNodeJsError(error) && error.code === 'ENOENT') { + // File does not exist, return an empty object + console.log('Annotations file not found, loading empty annotations.'); + return {}; + } else { + console.error('Failed to load annotations:', error); + return {}; + } } } export async function saveAnnotations(vault: Vault, annotations: PluginAnnotation): Promise { + const filePath = await getDbFilePath(vault); + try { const data = JSON.stringify(annotations, null, 2); - await vault.adapter.write(DB_FILE, data); + await vault.adapter.write(filePath, data); } catch (error) { console.error('Failed to save annotations:', error); } diff --git a/src/main.ts b/src/main.ts index c48e3f5..c5d4b75 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,9 +1,17 @@ -import { Plugin, SettingTab, PluginSettingTab, App, Setting } from 'obsidian'; +import { Plugin, SettingTab, PluginSettingTab, App } from 'obsidian'; import { around } from 'monkey-around'; +import { loadAnnotations, saveAnnotations } from './db'; + +interface PluginAnnotation { + [pluginId: string]: string; +} export default class PluginComment extends Plugin { + private annotations: PluginAnnotation = {}; + async onload() { console.log('Loading Plugin Comment'); + this.annotations = await loadAnnotations(this.app.vault); this.app.workspace.onLayoutReady(() => { this.patchSettings(); @@ -11,16 +19,14 @@ export default class PluginComment extends Plugin { } patchSettings() { - const self = this; - // Patch openTab to detect when a tab is opened this.register( around(this.app.setting, { openTab: (next: (tab: SettingTab) => void) => { - return function(this: Setting, tab: SettingTab) { + return (tab: SettingTab) => { const result = next.call(this, tab); if (tab && tab.id === 'community-plugins') { - self.observeTab(tab); + this.observeTab(tab); } return result; }; @@ -53,6 +59,7 @@ export default class PluginComment extends Plugin { if (settingItemInfo) { const pluginNameDiv = plugin.querySelector('.setting-item-name'); const pluginName = pluginNameDiv ? pluginNameDiv.textContent : 'Unknown Plugin'; + const pluginId = pluginName.replace(/\s+/g, '-').toLowerCase(); const descriptionDiv = settingItemInfo.querySelector('.setting-item-description'); if (descriptionDiv) { @@ -60,20 +67,25 @@ export default class PluginComment extends Plugin { if (!commentDiv) { const label = document.createElement('div'); label.innerText = `Personal annotation:`; - label.className = 'plugin-comment-label' + label.className = 'plugin-comment-label'; descriptionDiv.appendChild(label); const comment = document.createElement('div'); comment.className = 'plugin-comment'; comment.contentEditable = 'true'; - comment.innerText = `Add your comment about ${pluginName} here...`; + comment.innerText = this.annotations[pluginId] || `Add your comment about ${pluginName} here...`; // Prevent click event propagation to parent comment.addEventListener('click', (event) => { - console.log("Triggede"); event.stopPropagation(); }); + // Save the comment on input change + comment.addEventListener('input', () => { + this.annotations[pluginId] = comment.innerText; + saveAnnotations(this.app.vault, this.annotations); + }); + descriptionDiv.appendChild(comment); } } @@ -81,12 +93,12 @@ export default class PluginComment extends Plugin { }); } - onunload() { console.log('Unloading Plugin Comment'); } } +/* class CommentSettingTab extends PluginSettingTab { constructor(app: App, plugin: Plugin) { super(app, plugin); @@ -100,4 +112,5 @@ class CommentSettingTab extends PluginSettingTab { // Add any settings here if necessary } -} \ No newline at end of file +} +*/ diff --git a/styles/styles.css b/styles/styles.css index 05aec2d..3c04035 100644 --- a/styles/styles.css +++ b/styles/styles.css @@ -3,7 +3,12 @@ padding-top: 5px; } -.plugin-comment-annotation { +.plugin-comment { padding-top: 5px; font-style: italic; -} \ No newline at end of file + width: 100%; + margin-top: 5px; + border: 1px solid var(--background-modifier-border); + padding: 5px; + font-style: italic; +}