Extending database

This commit is contained in:
Andrea Alberti 2024-06-01 19:57:28 +02:00
parent 8a41a7d27c
commit 2fc64d41f1
3 changed files with 53 additions and 16 deletions

View file

@ -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<string> {
const pluginFolder = vault.configDir;
return `${pluginFolder}/${DB_FILE}`;
}
export async function loadAnnotations(vault: Vault): Promise<PluginAnnotation> {
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<void> {
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);
}

View file

@ -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
}
}
}
*/

View file

@ -3,7 +3,12 @@
padding-top: 5px;
}
.plugin-comment-annotation {
.plugin-comment {
padding-top: 5px;
font-style: italic;
}
width: 100%;
margin-top: 5px;
border: 1px solid var(--background-modifier-border);
padding: 5px;
font-style: italic;
}