update pane registe

This commit is contained in:
ytshen 2024-04-16 23:30:59 +08:00
parent 5cc74c4986
commit 6056c4208f
4 changed files with 34 additions and 72 deletions

View File

@ -1,4 +1,4 @@
itemPaneSection-header = itemPaneSection-header =
.label = 翻译 .label = TLDR
itemPaneSection-sidenav = itemPaneSection-sidenav =
.tooltiptext = 翻译 .tooltiptext = TLDR

View File

@ -1,4 +1,4 @@
itemPaneSection-header = itemPaneSection-header =
.label = 翻译 .label = TLDR
itemPaneSection-sidenav = itemPaneSection-sidenav =
.tooltiptext = 翻译 .tooltiptext = TLDR

View File

@ -33,6 +33,10 @@ async function onMainWindowLoad(win: Window): Promise<void> {
// Create ztoolkit for every window // Create ztoolkit for every window
addon.data.ztoolkit = createZToolkit(); addon.data.ztoolkit = createZToolkit();
(win as any).MozXULElement.insertFTLIfNeeded(
`${config.addonRef}-mainWindow.ftl`,
);
UIFactory.registerRightClickMenuItem(); UIFactory.registerRightClickMenuItem();
UIFactory.registerRightClickCollectionMenuItem(); UIFactory.registerRightClickCollectionMenuItem();

View File

@ -73,7 +73,28 @@ export class UIFactory {
// tldr行 // tldr行
static async registerTLDRItemBoxRow() { 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("<p>TL;DR</p>\n<p>")) {
str = str.slice("<p>TL;DR</p>\n<p>".length);
}
if (str.endsWith("</p>")) {
str = str.slice(0, -4);
}
return str;
}
}
return "";
}
Zotero.ItemPaneManager.registerSection({
paneID: config.addonRef, paneID: config.addonRef,
pluginID: config.addonID, pluginID: config.addonID,
header: { header: {
@ -84,74 +105,11 @@ export class UIFactory {
l10nID: `${config.addonRef}-itemPaneSection-sidenav`, l10nID: `${config.addonRef}-itemPaneSection-sidenav`,
icon: `chrome://${config.addonRef}/content/icons/favicon@20.png`, icon: `chrome://${config.addonRef}/content/icons/favicon@20.png`,
}, },
onRender: ({ body, item, editable, tabType }: any) => { onRender: ({ body, item }: any) => {
const noteKey = tldrs.get()[item.key]; let tldr = itemTLDR(item);
let str = ""; if (tldr.length <= 0 && item.parentItem) { tldr = itemTLDR(item.parentItem); }
if (noteKey) { body.textContent = tldr;
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("<p>TL;DR</p>\n<p>")) {
str = str.slice("<p>TL;DR</p>\n<p>".length);
}
if (str.endsWith("</p>")) {
str = str.slice(0, -4);
}
}
}
body.textContent = str;
// body.textContent
// = JSON.stringify({
// id: item?.id,
// editable,
// tabType,
// });
}, },
}); });
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("<p>TL;DR</p>\n<p>")) {
str = str.slice("<p>TL;DR</p>\n<p>".length);
}
if (str.endsWith("</p>")) {
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,
},
);
} }
} }