From 6056c4208f931610cd51e3ea725a350d874814c5 Mon Sep 17 00:00:00 2001 From: ytshen Date: Tue, 16 Apr 2024 23:30:59 +0800 Subject: [PATCH] update pane registe --- addon/locale/en-US/mainWindow.ftl | 4 +- addon/locale/zh-CN/mainWindow.ftl | 4 +- src/hooks.ts | 4 ++ src/modules/Common.ts | 94 +++++++++---------------------- 4 files changed, 34 insertions(+), 72 deletions(-) diff --git a/addon/locale/en-US/mainWindow.ftl b/addon/locale/en-US/mainWindow.ftl index 73c8f57..e93225e 100644 --- a/addon/locale/en-US/mainWindow.ftl +++ b/addon/locale/en-US/mainWindow.ftl @@ -1,4 +1,4 @@ itemPaneSection-header = - .label = 翻译 + .label = TLDR itemPaneSection-sidenav = - .tooltiptext = 翻译 \ No newline at end of file + .tooltiptext = TLDR \ No newline at end of file diff --git a/addon/locale/zh-CN/mainWindow.ftl b/addon/locale/zh-CN/mainWindow.ftl index 73c8f57..e93225e 100644 --- a/addon/locale/zh-CN/mainWindow.ftl +++ b/addon/locale/zh-CN/mainWindow.ftl @@ -1,4 +1,4 @@ itemPaneSection-header = - .label = 翻译 + .label = TLDR itemPaneSection-sidenav = - .tooltiptext = 翻译 \ No newline at end of file + .tooltiptext = TLDR \ No newline at end of file diff --git a/src/hooks.ts b/src/hooks.ts index 2b3dfbc..c0f3460 100644 --- a/src/hooks.ts +++ b/src/hooks.ts @@ -33,6 +33,10 @@ async function onMainWindowLoad(win: Window): Promise { // Create ztoolkit for every window addon.data.ztoolkit = createZToolkit(); + (win as any).MozXULElement.insertFTLIfNeeded( + `${config.addonRef}-mainWindow.ftl`, + ); + UIFactory.registerRightClickMenuItem(); UIFactory.registerRightClickCollectionMenuItem(); diff --git a/src/modules/Common.ts b/src/modules/Common.ts index 7099869..ad2bda8 100644 --- a/src/modules/Common.ts +++ b/src/modules/Common.ts @@ -73,7 +73,28 @@ export class UIFactory { // tldr行 static async registerTLDRItemBoxRow() { - const registeredID = Zotero.ItemPaneManager.registerSection({ + const itemTLDR = (item: Zotero.Item) => { + 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 ""; + } + Zotero.ItemPaneManager.registerSection({ paneID: config.addonRef, pluginID: config.addonID, header: { @@ -84,74 +105,11 @@ export class UIFactory { l10nID: `${config.addonRef}-itemPaneSection-sidenav`, icon: `chrome://${config.addonRef}/content/icons/favicon@20.png`, }, - onRender: ({ body, item, editable, tabType }: any) => { - const noteKey = tldrs.get()[item.key]; - let str = ""; - if (noteKey) { - const obj = Zotero.Items.getByLibraryAndKey(item.libraryID, noteKey); - if ( - obj && - obj instanceof Zotero.Item && - item.getNotes().includes(obj.id) - ) { - str = obj.getNote(); - if (str.startsWith("

TL;DR

\n

")) { - str = str.slice("

TL;DR

\n

".length); - } - if (str.endsWith("

")) { - str = str.slice(0, -4); - } - } - } - body.textContent = str; - // body.textContent - // = JSON.stringify({ - // id: item?.id, - // editable, - // tabType, - // }); + onRender: ({ body, item }: any) => { + let tldr = itemTLDR(item); + if (tldr.length <= 0 && item.parentItem) { tldr = itemTLDR(item.parentItem); } + body.textContent = tldr; }, }); - return; - 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, - }, - ); } }