move from itemBox row to note

This commit is contained in:
shenyutao 2024-01-31 13:47:29 +08:00
parent 7f436b320f
commit fdd4d20a95
5 changed files with 22 additions and 69 deletions

View File

@ -2,6 +2,6 @@
"editor.formatOnType": false,
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
"source.fixAll.eslint": "explicit"
}
}

View File

@ -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<void> {
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;
});

View File

@ -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,
},
);
}
}

View File

@ -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<string | number, string>("TLDR.json");
export const tldrs = DataStorage.instance<string, string | false>("fetchedItems.json");

View File

@ -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(`<p>TL;DR</p>\n<p>${info.tldr}</p>`)
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;
}
}