diff --git a/src/hooks.ts b/src/hooks.ts index bb2c31c..78ffd9e 100644 --- a/src/hooks.ts +++ b/src/hooks.ts @@ -30,6 +30,8 @@ async function onMainWindowLoad(win: Window): Promise { UIFactory.registerRightClickMenuItem(); UIFactory.registerRightClickCollectionMenuItem(); + + UIFactory.registerTLDRItemBoxRow(); onLoad(); } diff --git a/src/modules/Common.ts b/src/modules/Common.ts index f19a5b1..7fa95a9 100644 --- a/src/modules/Common.ts +++ b/src/modules/Common.ts @@ -1,5 +1,6 @@ import { config } from "../../package.json"; import { getString } from "../utils/locale"; +import { tldrs } from "./dataStorage"; export class RegisterFactory { // 注册zotero的通知 @@ -69,4 +70,53 @@ export class UIFactory { icon: menuIcon, }); } + + + // tldr行 + static async registerTLDRItemBoxRow() { + await ztoolkit.ItemBox.register( + "TLDR", + getString("itembox-tldrlabel"), + (field, unformatted, includeBaseMapped, item, original) => { + const noteKey = tldrs.get()[item.key]; + if (noteKey) { + const obj = Zotero.Items.getByLibraryAndKey( + item.libraryID, + noteKey, + ); + if ( + obj && + obj instanceof Zotero.Item && + item.getNotes().includes(obj.id) + ) { + let str = obj.getNote(); + if (str.startsWith('

TL;DR

\n

')) { + str = str.slice('

TL;DR

\n

'.length); + } + if (str.endsWith('

')) { + str = str.slice(0, -4); + } + return str; + } + } + 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, + }, + ); + } + }