From d8dc81dcff2ee37d8bb583becbe3712d5c66ff63 Mon Sep 17 00:00:00 2001 From: huaqo Date: Fri, 24 Apr 2026 16:07:56 +0200 Subject: [PATCH] feat: caching for annotation modal --- src/CiteModal.ts | 1 + src/OpenModal.ts | 1 + src/ZoteroClient.ts | 12 +++++++++++- src/main.ts | 46 +++++++-------------------------------------- 4 files changed, 20 insertions(+), 40 deletions(-) diff --git a/src/CiteModal.ts b/src/CiteModal.ts index 43d7509..59a4ce4 100644 --- a/src/CiteModal.ts +++ b/src/CiteModal.ts @@ -1,6 +1,7 @@ import { App, FuzzyMatch, FuzzySuggestModal } from 'obsidian'; import { ZoteroClient } from "./ZoteroClient"; import { ZoteroItem } from "./ZoteroItem"; +import { loadItems, renderZoteroItem } from "./Utils" const ZOTERO_BASE_URL = "zotero://select/library/items/"; diff --git a/src/OpenModal.ts b/src/OpenModal.ts index 5713cc2..9aa7c66 100644 --- a/src/OpenModal.ts +++ b/src/OpenModal.ts @@ -2,6 +2,7 @@ import { shell } from "electron"; import { App, FuzzyMatch, FuzzySuggestModal } from 'obsidian'; import { ZoteroClient } from "./ZoteroClient"; import { ZoteroItem } from "./ZoteroItem"; +import { loadItems, renderZoteroItem } from "./Utils" const ZOTERO_BASE_URL = "zotero://select/library/items/"; diff --git a/src/ZoteroClient.ts b/src/ZoteroClient.ts index 944412f..4990120 100644 --- a/src/ZoteroClient.ts +++ b/src/ZoteroClient.ts @@ -9,6 +9,7 @@ export class ZoteroClient { private baseURL: string; private style?: string; private params: URLSearchParams; + private itemCache = new Map>(); constructor(port: string); constructor(port: string, style: string); @@ -58,6 +59,7 @@ export class ZoteroClient { } async getItemsTop(): Promise { + if (this.style) { this.params.set("style", this.style); } @@ -68,6 +70,12 @@ export class ZoteroClient { } private async getItemByKey(itemKey: string, include: string = "data"): Promise { + const cacheKey = `${itemKey}:${include}:${this.style ?? ""}`; + + if (this.itemCache.has(cacheKey)) { + return this.itemCache.get(cacheKey)!; + } + const params = new URLSearchParams(); params.set("format", "json"); params.set("include", include); @@ -77,7 +85,9 @@ export class ZoteroClient { } const url = `${this.baseURL}/items/${itemKey}?${params.toString()}`; - return await this.request(url); + const promise = this.request(url); + this.itemCache.set(cacheKey, promise); + return promise; } private async resolveParentItem(annotation: ZoteroAnnotation): Promise { diff --git a/src/main.ts b/src/main.ts index 2171701..a74da4c 100644 --- a/src/main.ts +++ b/src/main.ts @@ -11,36 +11,14 @@ export default class Cite extends Plugin { await this.loadSettings(); - this.addRibbonIcon('scroll-text', 'insert citation', (evt: MouseEvent) => { - const view = this.app.workspace.getActiveViewOfType(MarkdownView); + this.addSettingTab(new SettingTab(this.app, this)); - 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, "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({ + id: 'open-command', + name: 'open', + callback: () => { + new OpenModal(this.app, this.settings.portSetting).open(); + }, }); 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({ id: 'annotation-command', 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)); }