Skip to content

Commit

Permalink
update: types
Browse files Browse the repository at this point in the history
  • Loading branch information
windingwind committed Oct 26, 2023
1 parent bd83b6e commit ce7157a
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 67 deletions.
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/bug_report.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ body:
value: |
- OS:
- Zotero Version:
- Plugin Version:
- Plugin Version:
validations:
required: true

Expand Down
6 changes: 3 additions & 3 deletions .github/ISSUE_TEMPLATE/feature_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ body:
value: |
- OS:
- Zotero Version:
- Plugin Version:
- Plugin Version:
validations:
required: true

Expand Down Expand Up @@ -60,7 +60,7 @@ body:
attributes:
label: Anything else?
description: |
Links? References? Anything that will give us more context about the issue you are encountering!
Tip: You can attach images or log files by clicking this area to highlight it and then dragging files in.
Links? References? Anything that will give us more context about the issue you are encountering!
Tip: You can attach images or log files by clicking this area to highlight it and then dragging files in.
validations:
required: false
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,6 @@
"release-it": "^15.11.0",
"replace-in-file": "^7.0.1",
"typescript": "^5.2.2",
"zotero-types": "^1.3.2"
"zotero-types": "^1.3.4"
}
}
27 changes: 11 additions & 16 deletions src/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ import {
registerReaderTabPanel,
updateReaderTabPanels,
} from "./modules/tabpanel";
import { buildReaderPopup, updateReaderPopup } from "./modules/popup";
import { registerNotify } from "./modules/notify";
import {
registerReaderInitializer,
} from "./modules/reader";
ReaderPopupEvent,
buildReaderPopup,
updateReaderPopup,
} from "./modules/popup";
import { registerNotify } from "./modules/notify";
import { registerReaderInitializer } from "./modules/reader";
import { getPref, setPref } from "./utils/prefs";
import {
addTranslateAnnotationTask,
Expand Down Expand Up @@ -184,28 +186,22 @@ async function onTranslateInBatch(
}
}

async function onReaderTextSelection(
readerInstance: _ZoteroTypes.ReaderInstance,
) {
function onReaderPopupShow(event: ReaderPopupEvent) {
const selection = addon.data.translate.selectedText;
const task = getLastTranslateTask();
if (task?.raw === selection) {
await addon.hooks.onReaderPopupBuild(readerInstance);
buildReaderPopup(event);
addon.hooks.onReaderPopupRefresh();
return;
}
addTranslateTask(selection, readerInstance.itemID);
await addon.hooks.onReaderPopupBuild(readerInstance);
addTranslateTask(selection, event.reader.itemID);
buildReaderPopup(event);
addon.hooks.onReaderPopupRefresh();
if (getPref("enableAuto")) {
addon.hooks.onTranslate();
}
}

async function onReaderPopupBuild(readerInstance: _ZoteroTypes.ReaderInstance) {
await buildReaderPopup(readerInstance);
}

function onReaderPopupRefresh() {
updateReaderPopup();
}
Expand Down Expand Up @@ -236,8 +232,7 @@ export default {
onShortcuts,
onTranslate,
onTranslateInBatch,
onReaderTextSelection,
onReaderPopupBuild,
onReaderPopupShow,
onReaderPopupRefresh,
onReaderTabPanelRefresh,
onSwitchTitleColumnDisplay,
Expand Down
55 changes: 20 additions & 35 deletions src/modules/popup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ import { getString } from "../utils/locale";
import { getPref, setPref } from "../utils/prefs";
import { addTranslateTask, getLastTranslateTask } from "../utils/task";
import { slice } from "../utils/str";
import { waitUtilAsync } from "../utils/wait";

export declare type ReaderPopupEvent = Parameters<
_ZoteroTypes.Reader.ReaderEventHandler<
_ZoteroTypes.Reader.ReaderEventMap["renderTextSelectionPopup"],
"renderTextSelectionPopup"
>
>[0];

export function updateReaderPopup() {
const popup = addon.data.popup.currentPopup;
Expand Down Expand Up @@ -75,26 +81,15 @@ export function updateReaderPopup() {
updatePopupSize(popup, textarea);
}

export async function buildReaderPopup(
readerInstance: _ZoteroTypes.ReaderInstance,
) {
await waitUtilAsync(
() =>
!!readerInstance._iframeWindow?.document.querySelector(
".selection-popup",
),
);
const popup = readerInstance._iframeWindow?.document.querySelector(
".selection-popup",
) as HTMLDivElement;
if (!popup) {
return;
}
export function buildReaderPopup(event: ReaderPopupEvent) {
const { reader, doc, append } = event;
const annotation = event.params.annotation;
const popup = doc.querySelector(".selection-popup") as HTMLDivElement;
addon.data.popup.currentPopup = popup;
popup.style.height = "-moz-fit-content";
popup.style.maxWidth = "none";
popup.setAttribute(
`${config.addonRef}-prefix`,
`${config.addonRef}-${readerInstance._instanceID}`,
`${config.addonRef}-${reader._instanceID}`,
);

const colors = popup.querySelector(".colors") as HTMLDivElement;
Expand All @@ -104,14 +99,12 @@ export async function buildReaderPopup(
const keepSize = getPref("keepPopupSize") as boolean;

const makeId = (type: string) =>
`${config.addonRef}-${readerInstance._instanceID}-${type}`;
`${config.addonRef}-${reader._instanceID}-${type}`;

const onTextAreaCopy = getOnTextAreaCopy(popup, makeId("text"));
const hidePopupTextarea = getPref("enableHidePopupTextarea") as boolean;

ztoolkit.UI.appendElement(
{
tag: "fragment",
append(
ztoolkit.UI.createElement(doc, "fragment", {
children: [
{
tag: "div",
Expand Down Expand Up @@ -172,9 +165,6 @@ export async function buildReaderPopup(
keepSize ? Number(getPref("popupHeight")) : 30,
)}px`,
marginLeft: "2px",
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
scrollbarWidth: "none",
},
properties: {
onpointerup: (e: Event) => e.stopPropagation(),
Expand Down Expand Up @@ -253,13 +243,9 @@ export async function buildReaderPopup(
if (!editorInstance) {
return;
}
const annotation: Record<string, any> =
// @ts-ignore
readerInstance._internalReader._lastView._selectionPopup
.annotation;
const task = addTranslateTask(
annotation.text,
readerInstance.itemID,
addon.data.translate.selectedText,
reader.itemID,
"text",
);
if (!task) {
Expand All @@ -279,14 +265,13 @@ export async function buildReaderPopup(
annotation.comment = task.result;
}
// @ts-ignore
readerInstance._addToNote([annotation]);
reader._addToNote([annotation]);
},
},
],
},
],
},
popup,
}),
);
}

Expand Down
10 changes: 3 additions & 7 deletions src/modules/reader.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
import { config } from "../../package.json";
import { SVGIcon } from "../utils/config";
import { addTranslateAnnotationTask } from "../utils/task";
import { ReaderPopupEvent } from "./popup";

export function registerReaderInitializer() {
Zotero.Reader.registerEventListener(
"renderTextSelectionPopup",
(event: {
reader: _ZoteroTypes.ReaderInstance;
doc: Document;
params: { annotation: { text: string } };
append: (node: Node) => void;
}) => {
(event: ReaderPopupEvent) => {
const { reader, doc, params, append } = event;
addon.data.translate.selectedText = params.annotation.text.trim();
addon.hooks.onReaderTextSelection(reader);
addon.hooks.onReaderPopupShow(event);
},
config.addonID,
);
Expand Down
5 changes: 1 addition & 4 deletions src/modules/tabpanel.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { getString } from "../utils/locale";
import { config } from "../../package.json";
import {
LANG_CODE,
SERVICES,
} from "../utils/config";
import { LANG_CODE, SERVICES } from "../utils/config";
import { getPref, setPref } from "../utils/prefs";
import {
addTranslateTask,
Expand Down

0 comments on commit ce7157a

Please sign in to comment.