feat: caching for annotation modal
This commit is contained in:
@@ -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/";
|
||||
|
||||
|
||||
@@ -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/";
|
||||
|
||||
|
||||
+11
-1
@@ -9,6 +9,7 @@ export class ZoteroClient {
|
||||
private baseURL: string;
|
||||
private style?: string;
|
||||
private params: URLSearchParams;
|
||||
private itemCache = new Map<string, Promise<ZoteroResponse>>();
|
||||
|
||||
constructor(port: string);
|
||||
constructor(port: string, style: string);
|
||||
@@ -58,6 +59,7 @@ export class ZoteroClient {
|
||||
}
|
||||
|
||||
async getItemsTop(): Promise<ZoteroItem[]> {
|
||||
|
||||
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<ZoteroResponse> {
|
||||
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<ZoteroResponse>(url);
|
||||
const promise = this.request<ZoteroResponse>(url);
|
||||
this.itemCache.set(cacheKey, promise);
|
||||
return promise;
|
||||
}
|
||||
|
||||
private async resolveParentItem(annotation: ZoteroAnnotation): Promise<void> {
|
||||
|
||||
+7
-39
@@ -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));
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user