Files
chatpapers/src/modules/lecture/ui/registerLecturePane.ts
T

90 lines
2.9 KiB
TypeScript

import { config } from "../../../../package.json";
import { getLocaleID, getString } from "../../../utils/locale";
import { findPdfAttachment } from "../../pdf/extractor";
import { ensureChatPapersStyles } from "../../ui/readerPane";
import {
applyAdaptiveHeight,
applyItemPaneSectionButtonIcons,
buildItemPaneSectionButtons,
onItemPaneSectionToggle,
prepareItemPaneBody,
refreshSectionOpenHeight,
} from "../../ui/itemPaneSection";
import { LecturePaneView } from "./lecturePane";
const views = new WeakMap<HTMLElement, LecturePaneView>();
function iconURL(file: string) {
return `chrome://${config.addonRef}/content/icons/${file}`;
}
export function registerLecturePane() {
Zotero.ItemPaneManager.registerSection({
paneID: "chatpapers-lecture",
pluginID: config.addonID,
header: {
l10nID: getLocaleID("item-section-lecture-head"),
icon: iconURL("lecture.svg"),
},
sidenav: {
l10nID: getLocaleID("item-section-lecture-sidenav"),
icon: iconURL("lecture.svg"),
},
sectionButtons: buildItemPaneSectionButtons("lecture"),
onInit: ({ body }) => {
const doc = body.ownerDocument;
if (doc) ensureChatPapersStyles(doc);
prepareItemPaneBody(body);
applyAdaptiveHeight(body, "lecture");
},
onDestroy: ({ body }) => {
views.get(body)?.destroy();
views.delete(body);
},
onItemChange: ({ item, setEnabled }) => {
setEnabled(Boolean(item && findPdfAttachment(item)));
return true;
},
onRender: ({ body }) => {
body.replaceChildren();
const doc = body.ownerDocument;
if (!doc) return;
applyItemPaneSectionButtonIcons(body);
const loading = doc.createElement("div");
loading.className = "chatpapers-loading";
loading.textContent = getString("lecture-loading");
body.append(loading);
},
onAsyncRender: async ({ body, item }) => {
if (!item) return;
const doc = body.ownerDocument;
if (!doc) return;
views.get(body)?.destroy();
const view = new LecturePaneView(doc, body, item);
views.set(body, view);
try {
await view.mount();
} catch (e) {
ztoolkit.log("[ChatPapers:Lecture] mount failed", e);
body.replaceChildren();
const err = doc.createElement("div");
err.className = "chatpapers-lecture-error";
err.textContent = getString("lecture-mount-error");
body.append(err);
}
applyAdaptiveHeight(body, "lecture");
applyItemPaneSectionButtonIcons(body);
const section = body.closest("collapsible-section");
if (section?.hasAttribute("open")) {
refreshSectionOpenHeight(body);
} else if (section) {
onItemPaneSectionToggle({ body });
}
},
onToggle: ({ body, event }) => {
applyItemPaneSectionButtonIcons(body);
onItemPaneSectionToggle({ body, event });
},
});
}