first commit
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
import { config } from "../../../package.json";
|
||||
import { getLocaleID, getString } from "../../utils/locale";
|
||||
import { ChatView } from "./chatView";
|
||||
|
||||
const views = new WeakMap<HTMLElement, ChatView>();
|
||||
|
||||
function iconURL(file: string) {
|
||||
return `chrome://${config.addonRef}/content/icons/${file}`;
|
||||
}
|
||||
|
||||
export function registerChatPane() {
|
||||
Zotero.ItemPaneManager.registerSection({
|
||||
paneID: "chatpapers-chat",
|
||||
pluginID: config.addonID,
|
||||
header: {
|
||||
l10nID: getLocaleID("item-section-chat-head"),
|
||||
icon: iconURL("chat.svg"),
|
||||
},
|
||||
sidenav: {
|
||||
l10nID: getLocaleID("item-section-chat-sidenav"),
|
||||
icon: iconURL("chat.svg"),
|
||||
},
|
||||
onInit: ({ body }) => {
|
||||
const doc = body.ownerDocument;
|
||||
if (doc) ensureStyles(doc);
|
||||
},
|
||||
onDestroy: ({ body }) => {
|
||||
views.get(body)?.destroy();
|
||||
views.delete(body);
|
||||
},
|
||||
onItemChange: ({ item, setEnabled, tabType }) => {
|
||||
// Enable in reader and library item pane when there is an item
|
||||
setEnabled(Boolean(item));
|
||||
void tabType;
|
||||
return true;
|
||||
},
|
||||
onRender: ({ body }) => {
|
||||
body.replaceChildren();
|
||||
const doc = body.ownerDocument;
|
||||
if (!doc) return;
|
||||
const loading = doc.createElement("div");
|
||||
loading.className = "chatpapers-loading";
|
||||
loading.textContent = getString("chat-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 ChatView(doc, body, item);
|
||||
views.set(body, view);
|
||||
await view.mount();
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export function registerPrefs() {
|
||||
Zotero.PreferencePanes.register({
|
||||
pluginID: config.addonID,
|
||||
src: rootURI + "content/preferences.xhtml",
|
||||
label: getString("prefs-title"),
|
||||
image: `chrome://${config.addonRef}/content/icons/favicon.png`,
|
||||
});
|
||||
}
|
||||
|
||||
function ensureStyles(doc: Document) {
|
||||
const id = "chatpapers-styles";
|
||||
if (doc.getElementById(id)) return;
|
||||
const link = doc.createElement("link");
|
||||
link.id = id;
|
||||
link.rel = "stylesheet";
|
||||
link.type = "text/css";
|
||||
link.href = `chrome://${config.addonRef}/content/chatpapers.css`;
|
||||
doc.documentElement?.appendChild(link);
|
||||
}
|
||||
Reference in New Issue
Block a user