Skip to content

Commit

Permalink
fix: #231
Browse files Browse the repository at this point in the history
  • Loading branch information
windingwind committed Jan 4, 2024
1 parent d887e23 commit 6f7dc56
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 75 deletions.
16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,25 +33,25 @@
"homepage": "https://github.com/windingwind/zotero-actions-tags#readme",
"dependencies": {
"js-yaml": "^4.1.0",
"zotero-plugin-toolkit": "^2.3.14"
"zotero-plugin-toolkit": "^2.3.15"
},
"devDependencies": {
"@types/js-yaml": "^4.0.9",
"@types/node": "^20.10.4",
"@typescript-eslint/eslint-plugin": "^6.14.0",
"@typescript-eslint/parser": "^6.14.0",
"@types/node": "^20.10.6",
"@typescript-eslint/eslint-plugin": "^6.17.0",
"@typescript-eslint/parser": "^6.17.0",
"chokidar-cli": "^3.0.0",
"compressing": "^1.10.0",
"concurrently": "^8.2.2",
"cross-env": "^7.0.3",
"esbuild": "^0.19.9",
"eslint": "^8.55.0",
"esbuild": "^0.19.11",
"eslint": "^8.56.0",
"eslint-config-prettier": "^9.1.0",
"prettier": "^3.1.1",
"release-it": "^16.3.0",
"replace-in-file": "^7.0.2",
"replace-in-file": "^7.1.0",
"typescript": "^5.3.3",
"zotero-types": "^1.3.10"
"zotero-types": "^1.3.13"
},
"eslintConfig": {
"env": {
Expand Down
166 changes: 99 additions & 67 deletions src/modules/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,66 @@ function initItemMenu(win: Window) {
}

function initReaderMenu() {
Zotero.Reader.registerEventListener(
"renderToolbar",
readerToolbarCallback,
config.addonID,
);

Zotero.Reader._readers.forEach(buildReaderMenuButton);
}

function initReaderAnnotationMenu() {
Zotero.Reader.registerEventListener(
"createAnnotationContextMenu",
(event) => {
const { append, params, reader } = event;
const actions = getActionsByMenu("readerAnnotation");
for (const action of actions) {
append({
label: action.menu!,
onCommand: () => {
triggerMenuCommand(action.key, () =>
getItemsByKey(reader._item.libraryID, ...params.ids),
);
},
});
}
},
config.addonID,
);
}

async function buildReaderMenuButton(reader: _ZoteroTypes.ReaderInstance) {
await reader._initPromise;
const customSections = reader._iframeWindow?.document.querySelector(
".toolbar .custom-sections",
);
if (!customSections) {
return;
}
const append = (...args: (string | Node)[]) => {
customSections.append(
...Components.utils.cloneInto(args, reader._iframeWindow, {
wrapReflectors: true,
cloneFunctions: true,
}),
);
};

readerToolbarCallback({
append,
reader,
doc: customSections.ownerDocument,
type: "renderToolbar",
params: {},
});
}

function readerToolbarCallback(
event: Parameters<_ZoteroTypes.Reader.EventHandler<"renderToolbar">>[0],
) {
const { append, doc } = event;
const image =
"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAACXBIWXMAAAsSAAALEgHS3X78AAAA40lEQVRYCWP8//8/AyFwJL/hjsbXJ8oEFSKBG9wyd20mNqgQUsdESMGCypkppFoOAiA924s711PsAOmfb2NItRwGWP7/FaTYAbQGow4YdcCoA0YdMOqAUQcMuANYCCm4yS394AWboD05hn9k5XrgSqkDjgloPiDHcih4kENAwWgaGHXAqAModcBFKB4QB4AsdoBish1BrgPAli8rNvsAwpQ4ghwHLIRZDhNAcsRCUg0jWBSjW76s2CwBmwTUEQlRvadA3HhiDSQlBHBajuYQkBqiQ4JYBxQSYzmaIwoJKmRgYAAAgCNBYXH3oBUAAAAASUVORK5CYII=";
const readerButtonCSS = `
Expand All @@ -100,75 +160,47 @@ function initReaderMenu() {
z-index: 1;
}
`;
Zotero.Reader.registerEventListener(
"renderToolbar",
(event) => {
const { append, doc } = event;
append(
ztoolkit.UI.createElement(doc, "button", {
namespace: "html",
classList: ["toolbarButton", "actions-tags-reader-menu"],
properties: {
tabIndex: -1,
title: "Actions",
},
listeners: [
{
type: "click",
listener: (ev: Event) => {
document
.querySelector(`#${config.addonRef}-reader-popup`)
// @ts-ignore XUL.MenuPopup
?.openPopup(
doc.querySelector(".actions-tags-reader-menu"),
"after_start",
);
},
},
],
children: [
{
tag: "span",
classList: ["button-background"],
},
{
tag: "span",
classList: ["dropmarker"],
},
],
}),
);
append(
ztoolkit.UI.createElement(doc, "style", {
id: `${config.addonRef}-reader-button`,
properties: {
textContent: readerButtonCSS,
append(
ztoolkit.UI.createElement(doc, "button", {
namespace: "html",
classList: ["toolbarButton", "actions-tags-reader-menu"],
properties: {
tabIndex: -1,
title: "Actions",
},
listeners: [
{
type: "click",
listener: (ev: Event) => {
document
.querySelector(`#${config.addonRef}-reader-popup`)
// @ts-ignore XUL.MenuPopup
?.openPopup(
doc.querySelector(".actions-tags-reader-menu"),
"after_start",
);
},
}),
);
},
config.addonID,
},
],
children: [
{
tag: "span",
classList: ["button-background"],
},
{
tag: "span",
classList: ["dropmarker"],
},
],
}),
);
}

function initReaderAnnotationMenu() {
Zotero.Reader.registerEventListener(
"createAnnotationContextMenu",
(event) => {
const { append, params, reader } = event;
const actions = getActionsByMenu("readerAnnotation");
for (const action of actions) {
append({
label: action.menu!,
onCommand: () => {
triggerMenuCommand(action.key, () =>
getItemsByKey(reader._item.libraryID, ...params.ids),
);
},
});
}
},
config.addonID,
append(
ztoolkit.UI.createElement(doc, "style", {
id: `${config.addonRef}-reader-button`,
properties: {
textContent: readerButtonCSS,
},
}),
);
}

Expand Down

0 comments on commit 6f7dc56

Please sign in to comment.