mirror of
https://github.com/alberti42/obsidian-plugins-annotations.git
synced 2026-07-22 10:10:24 +00:00
Made requested changed from reviewer.
This commit is contained in:
parent
962fe35aa0
commit
b1bfb6be7a
5 changed files with 27 additions and 76 deletions
2
LICENSE
2
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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
61
src/db.ts
61
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 }
|
||||
|
|
|
|||
33
src/main.ts
33
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<string | null> {
|
||||
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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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%;
|
||||
|
|
|
|||
Loading…
Reference in a new issue