diff --git a/.vscode/settings.json b/.vscode/settings.json index be8156d..ee7b675 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -2,6 +2,6 @@ "editor.formatOnType": false, "editor.formatOnSave": true, "editor.codeActionsOnSave": { - "source.fixAll.eslint": true + "source.fixAll.eslint": "explicit" } } diff --git a/src/hooks.ts b/src/hooks.ts index 69cd7ca..86f7748 100644 --- a/src/hooks.ts +++ b/src/hooks.ts @@ -16,8 +16,6 @@ async function onStartup() { await tldrs.getAsync(); - RegisterFactory.registerPrefs(); - RegisterFactory.registerNotifier(); await onMainWindowLoad(window); @@ -33,8 +31,6 @@ async function onMainWindowLoad(win: Window): Promise { UIFactory.registerRightClickCollectionMenuItem(); - await UIFactory.registerTLDRItemBoxRow(); - onLoad(); } @@ -127,8 +123,8 @@ function onUpdateItems(items: Zotero.Item[], forceFetch: boolean = false) { if (!item.getField("title")) { return false; } - if (!forceFetch) { - return tldrs.get()[item.id] === undefined; + if (!forceFetch && (item.key in tldrs.get())) { + return false; } return true; }); diff --git a/src/modules/Common.ts b/src/modules/Common.ts index 0473ac4..f19a5b1 100644 --- a/src/modules/Common.ts +++ b/src/modules/Common.ts @@ -1,6 +1,5 @@ import { config } from "../../package.json"; import { getString } from "../utils/locale"; -import { tldrs, TLDRItemNotFound, TLDRUnrelated } from "./dataStorage"; export class RegisterFactory { // 注册zotero的通知 @@ -36,19 +35,6 @@ export class RegisterFactory { private static unregisterNotifier(notifierID: string) { Zotero.Notifier.unregisterObserver(notifierID); } - - // 注册首选项配置 - static registerPrefs() { - // const prefOptions = { - // pluginID: config.addonID, - // src: rootURI + "chrome/content/preferences.xhtml", - // label: getString("prefs.title"), - // image: `chrome://${config.addonRef}/content/icons/favicon.png`, - // extraDTD: [`chrome://${config.addonRef}/locale/overlay.dtd`], - // defaultXUL: true, - // }; - // ztoolkit.PreferencePane.register(prefOptions); - } } export class UIFactory { @@ -83,39 +69,4 @@ export class UIFactory { icon: menuIcon, }); } - - // tldr行 - static async registerTLDRItemBoxRow() { - await ztoolkit.ItemBox.register( - "TLDR", - getString("itembox-tldrlabel"), - (field, unformatted, includeBaseMapped, item, original) => { - const tldrInfo = tldrs.get()[item.id]; - if (tldrInfo === TLDRUnrelated) { - return getString(TLDRUnrelated); - } else if (tldrInfo === TLDRItemNotFound) { - return getString(TLDRItemNotFound); - } else if (tldrInfo) { - return tldrInfo; - } else { - return ""; - } - }, - { - editable: true, - setFieldHook: (field, value, loadIn, item, original) => { - (async () => { - await tldrs.modify((data: any) => { - data[item.id] = value; - return data; - }); - ztoolkit.ItemBox.refresh(); - })(); - return true; - }, - index: 2, - multiline: true, - }, - ); - } } diff --git a/src/modules/dataStorage.ts b/src/modules/dataStorage.ts index c213b9b..46aeb13 100644 --- a/src/modules/dataStorage.ts +++ b/src/modules/dataStorage.ts @@ -119,6 +119,4 @@ export class DataStorage { } } -export const TLDRUnrelated = "tldr-unrelated"; // semantic scholar 找到了该item,但是该item没有tldr -export const TLDRItemNotFound = "tldr-itemnotfound"; // semantic scholar 找不到该item -export const tldrs = DataStorage.instance("TLDR.json"); +export const tldrs = DataStorage.instance("fetchedItems.json"); diff --git a/src/modules/tldrFetcher.ts b/src/modules/tldrFetcher.ts index fac592d..9c035e9 100644 --- a/src/modules/tldrFetcher.ts +++ b/src/modules/tldrFetcher.ts @@ -1,4 +1,4 @@ -import { tldrs, TLDRUnrelated, TLDRItemNotFound } from "./dataStorage"; +import { tldrs } from "./dataStorage"; type SemanticScholarItemInfo = { title?: string; @@ -23,6 +23,7 @@ export class TLDRFetcher { if (!this.title || this.title.length <= 0) { return false; } + const noteKey = (await tldrs.getAsync())[this.zoteroItem.key]; try { const infos = await this.fetchRelevanceItemInfos(this.title); for (const info of infos) { @@ -36,23 +37,30 @@ export class TLDRFetcher { ) { match = true; } - if (match) { - const result = info.tldr ?? TLDRUnrelated; - tldrs.modify((data: any) => { - data[this.zoteroItem.id] = result; + if (match && info.tldr) { + let note = new Zotero.Item('note'); + if (noteKey) { + const obj = Zotero.Items.getByLibraryAndKey(this.zoteroItem.libraryID, noteKey); + if (obj && obj instanceof Zotero.Item && this.zoteroItem.getNotes().includes(obj.id)) { + note = obj; + } + } + note.setNote(`

TL;DR

\n

${info.tldr}

`) + note.parentID = this.zoteroItem.id; + await note.saveTx(); + await tldrs.modify((data: any) => { + data[this.zoteroItem.key] = note.key; return data; }); - return true; + return true } } - tldrs.modify((data: any) => { - data[this.zoteroItem.id] = TLDRItemNotFound; + await tldrs.modify((data: any) => { + data[this.zoteroItem.key] = false; return data; }); - return false; } catch (error) { Zotero.log(`post semantic scholar request error: ${error}`); - return false; } }