Skip to content

Commit

Permalink
fix: reader menu in reader window
Browse files Browse the repository at this point in the history
fix: #318
  • Loading branch information
windingwind committed Apr 27, 2024
1 parent 1a0db2b commit 8aeb168
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 28 deletions.
2 changes: 1 addition & 1 deletion src/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ async function onPrefsEvent(type: string, data: { [key: string]: any }) {
async function onMenuEvent(type: "showing", data: { [key: string]: any }) {
switch (type) {
case "showing":
buildItemMenu(data.window, data.target);
buildItemMenu(data.window, data.target, data.extraData);
break;
default:
return;
Expand Down
73 changes: 48 additions & 25 deletions src/modules/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,22 +58,6 @@ function initItemMenu(win: Window) {
},
],
});

ztoolkit.UI.appendElement(
{
tag: "menupopup",
id: `${config.addonRef}-reader-popup`,
listeners: [
{
type: "popupshowing",
listener: (ev) => {
addon.hooks.onMenuEvent("showing", { window, target: "reader" });
},
},
],
},
win.document.querySelector("popupset")!,
);
}

async function initReaderMenu() {
Expand All @@ -89,6 +73,45 @@ async function initReaderMenu() {
Zotero.Reader._readers.forEach(buildReaderMenuButton);
}

function getReaderMenuPopup(reader: _ZoteroTypes.ReaderInstance) {
const doc = reader._iframe?.ownerDocument;
if (!doc) {
return;
}
let popup = doc.querySelector(
`#${config.addonRef}-reader-popup`,
) as XUL.MenuPopup;
if (!popup) {
popup = ztoolkit.UI.appendElement(
{
tag: "menupopup",
id: `${config.addonRef}-reader-popup`,
listeners: [
{
type: "popupshowing",
listener: (ev) => {
addon.hooks.onMenuEvent("showing", {
window: reader._window,
target: "reader",
extraData: {
readerID:
// Do not pass readerID if the reader is in main window
reader._window?.location.href ===
"chrome://zotero/content/zoteroPane.xhtml"
? undefined
: reader._instanceID,
},
});
},
},
],
},
doc.querySelector("popupset")!,
) as XUL.MenuPopup;
}
return popup;
}

function initReaderAnnotationMenu() {
Zotero.Reader.registerEventListener(
"createAnnotationContextMenu",
Expand Down Expand Up @@ -139,7 +162,8 @@ async function buildReaderMenuButton(reader: _ZoteroTypes.ReaderInstance) {
function readerToolbarCallback(
event: Parameters<_ZoteroTypes.Reader.EventHandler<"renderToolbar">>[0],
) {
const { append, doc } = event;
const { append, doc, reader } = event;
getReaderMenuPopup(reader);
const button = ztoolkit.UI.createElement(doc, "button", {
namespace: "html",
classList: [
Expand All @@ -155,13 +179,11 @@ function readerToolbarCallback(
{
type: "click",
listener: (ev: Event) => {
document
.querySelector(`#${config.addonRef}-reader-popup`)
// @ts-ignore XUL.MenuPopup
?.openPopup(
doc.querySelector(`.${config.addonRef}-reader-button`),
"after_start",
);
// @ts-ignore TODO: update types
getReaderMenuPopup(reader)?.openPopup(
doc.querySelector(`.${config.addonRef}-reader-button`),
"after_start",
);
},
},
],
Expand All @@ -179,6 +201,7 @@ function readerToolbarCallback(
function buildItemMenu(
win: Window,
target: "item" | "collection" | "tools" | "reader",
extraData?: { readerID?: string },
) {
const doc = win.document;
const popup = doc.querySelector(
Expand Down Expand Up @@ -217,7 +240,7 @@ function buildItemMenu(
listener: (event) => {
triggerMenuCommand(
action.key,
() => getCurrentItems(target),
() => getCurrentItems(target, extraData),
target === "collection",
);
},
Expand Down
22 changes: 20 additions & 2 deletions src/utils/items.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ import { ActionShowInMenu } from "./actions";

export { getCurrentItems, getItemsByKey };

async function getCurrentItems(type?: ActionShowInMenu) {
async function getCurrentItems(
type?: ActionShowInMenu,
extraData?: {
readerID?: string;
},
) {
let items = [] as Zotero.Item[];
if (!type || type === "tools") {
type = getCurrentTargetType();
Expand All @@ -25,7 +30,20 @@ async function getCurrentItems(type?: ActionShowInMenu) {
break;
}
case "reader": {
const reader = Zotero.Reader.getByTabID(Zotero_Tabs.selectedID);
let reader: _ZoteroTypes.ReaderInstance;
if (extraData?.readerID) {
const _reader = Zotero.Reader._readers.find(
(r) => r._instanceID === extraData.readerID,
);
if (!_reader) {
throw new Error(
`Reader ${extraData.readerID} not found in getCurrentItems()`,
);
}
reader = _reader;
} else {
reader = Zotero.Reader.getByTabID(Zotero_Tabs.selectedID);
}
const annotationIDs =
// @ts-ignore TODO: update types
reader?._internalReader._lastView._selectedAnnotationIDs as string[];
Expand Down

0 comments on commit 8aeb168

Please sign in to comment.