feat: caching for annotation modal

This commit is contained in:
huaqo
2026-04-24 16:07:56 +02:00
parent 28bcf2d874
commit d8dc81dcff
4 changed files with 20 additions and 40 deletions
+1
View File
@@ -1,6 +1,7 @@
import { App, FuzzyMatch, FuzzySuggestModal } from 'obsidian'; import { App, FuzzyMatch, FuzzySuggestModal } from 'obsidian';
import { ZoteroClient } from "./ZoteroClient"; import { ZoteroClient } from "./ZoteroClient";
import { ZoteroItem } from "./ZoteroItem"; import { ZoteroItem } from "./ZoteroItem";
import { loadItems, renderZoteroItem } from "./Utils"
const ZOTERO_BASE_URL = "zotero://select/library/items/"; const ZOTERO_BASE_URL = "zotero://select/library/items/";
+1
View File
@@ -2,6 +2,7 @@ import { shell } from "electron";
import { App, FuzzyMatch, FuzzySuggestModal } from 'obsidian'; import { App, FuzzyMatch, FuzzySuggestModal } from 'obsidian';
import { ZoteroClient } from "./ZoteroClient"; import { ZoteroClient } from "./ZoteroClient";
import { ZoteroItem } from "./ZoteroItem"; import { ZoteroItem } from "./ZoteroItem";
import { loadItems, renderZoteroItem } from "./Utils"
const ZOTERO_BASE_URL = "zotero://select/library/items/"; const ZOTERO_BASE_URL = "zotero://select/library/items/";
+11 -1
View File
@@ -9,6 +9,7 @@ export class ZoteroClient {
private baseURL: string; private baseURL: string;
private style?: string; private style?: string;
private params: URLSearchParams; private params: URLSearchParams;
private itemCache = new Map<string, Promise<ZoteroResponse>>();
constructor(port: string); constructor(port: string);
constructor(port: string, style: string); constructor(port: string, style: string);
@@ -58,6 +59,7 @@ export class ZoteroClient {
} }
async getItemsTop(): Promise<ZoteroItem[]> { async getItemsTop(): Promise<ZoteroItem[]> {
if (this.style) { if (this.style) {
this.params.set("style", this.style); this.params.set("style", this.style);
} }
@@ -68,6 +70,12 @@ export class ZoteroClient {
} }
private async getItemByKey(itemKey: string, include: string = "data"): Promise<ZoteroResponse> { private async getItemByKey(itemKey: string, include: string = "data"): Promise<ZoteroResponse> {
const cacheKey = `${itemKey}:${include}:${this.style ?? ""}`;
if (this.itemCache.has(cacheKey)) {
return this.itemCache.get(cacheKey)!;
}
const params = new URLSearchParams(); const params = new URLSearchParams();
params.set("format", "json"); params.set("format", "json");
params.set("include", include); params.set("include", include);
@@ -77,7 +85,9 @@ export class ZoteroClient {
} }
const url = `${this.baseURL}/items/${itemKey}?${params.toString()}`; const url = `${this.baseURL}/items/${itemKey}?${params.toString()}`;
return await this.request<ZoteroResponse>(url); const promise = this.request<ZoteroResponse>(url);
this.itemCache.set(cacheKey, promise);
return promise;
} }
private async resolveParentItem(annotation: ZoteroAnnotation): Promise<void> { private async resolveParentItem(annotation: ZoteroAnnotation): Promise<void> {
+7 -39
View File
@@ -11,36 +11,14 @@ export default class Cite extends Plugin {
await this.loadSettings(); await this.loadSettings();
this.addRibbonIcon('scroll-text', 'insert citation', (evt: MouseEvent) => { this.addSettingTab(new SettingTab(this.app, this));
const view = this.app.workspace.getActiveViewOfType(MarkdownView);
if (!view) { this.addCommand({
new Notice("No active Markdown view"); id: 'open-command',
console.log("No active Markdown view"); name: 'open',
return; callback: () => {
} new OpenModal(this.app, this.settings.portSetting).open();
},
const editor = view.editor;
new CiteModal(this.app, this.settings.portSetting, this.settings.linkSetting, this.settings.styleSetting, "citation", (citation: string) => {
editor.replaceSelection(citation);
}).open();
});
this.addRibbonIcon('scroll-text', 'insert bibliography', (evt: MouseEvent) => {
const view = this.app.workspace.getActiveViewOfType(MarkdownView);
if (!view) {
new Notice("No active Markdown view");
console.log("No active Markdown view");
return;
}
const editor = view.editor;
new CiteModal(this.app, this.settings.portSetting, this.settings.linkSetting, this.settings.styleSetting, "bibliography", (citation: string) => {
editor.replaceSelection(citation);
}).open();
}); });
this.addCommand({ this.addCommand({
@@ -65,14 +43,6 @@ export default class Cite extends Plugin {
} }
}); });
this.addCommand({
id: 'open-command',
name: 'open',
callback: () => {
new OpenModal(this.app, this.settings.portSetting).open();
},
});
this.addCommand({ this.addCommand({
id: 'annotation-command', id: 'annotation-command',
name: 'insert annotations', name: 'insert annotations',
@@ -84,8 +54,6 @@ export default class Cite extends Plugin {
} }
}); });
this.addSettingTab(new SettingTab(this.app, this));
this.registerInterval(window.setInterval(() => console.log('setInterval'), 5 * 60 * 1000)); this.registerInterval(window.setInterval(() => console.log('setInterval'), 5 * 60 * 1000));
} }