From 1c62f3cb3e09a4fe316b7addc8a28411489adc70 Mon Sep 17 00:00:00 2001 From: Emiliano Heyns Date: Thu, 4 Jan 2024 13:07:39 +0100 Subject: [PATCH] neuter _mergePDFAttachments --- .eslintrc.json | 26 +------------------------- bootstrap.ts | 4 ++-- lib.ts | 25 ++++++++++++++++++------- 3 files changed, 21 insertions(+), 34 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index 012e888..36c3632 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -32,31 +32,7 @@ ], "@typescript-eslint/await-thenable": "error", "@typescript-eslint/ban-ts-comment": "warn", - "@typescript-eslint/ban-types": [ - "warn", - { - "types": { - "Object": { - "message": "Avoid using the `Object` type. Did you mean `object`?" - }, - "Function": { - "message": "Avoid using the `Function` type. Prefer a specific function type, like `() => void`." - }, - "Boolean": { - "message": "Avoid using the `Boolean` type. Did you mean `boolean`?" - }, - "Number": { - "message": "Avoid using the `Number` type. Did you mean `number`?" - }, - "String": { - "message": "Avoid using the `String` type. Did you mean `string`?" - }, - "Symbol": { - "message": "Avoid using the `Symbol` type. Did you mean `symbol`?" - } - } - } - ], + "@typescript-eslint/ban-types": "off", "@typescript-eslint/consistent-type-assertions": "error", "@typescript-eslint/dot-notation": "error", "@typescript-eslint/explicit-module-boundary-types": "warn", diff --git a/bootstrap.ts b/bootstrap.ts index 593ae06..e60a3e9 100644 --- a/bootstrap.ts +++ b/bootstrap.ts @@ -130,7 +130,7 @@ export async function startup({ id, version, resourceURI, rootURI = resourceURI. } } -export function shutdown() { +export function shutdown(): void { log('Shutting down') if (typeof chromeHandle !== 'undefined') { @@ -149,7 +149,7 @@ export function shutdown() { } } -export function uninstall() { +export function uninstall(): void { // `Zotero` object isn't available in `uninstall()` in Zotero 6, so log manually if (typeof Zotero == 'undefined') { dump('EdTechHub: Uninstalled\n\n') diff --git a/lib.ts b/lib.ts index 28cf057..42e2044 100644 --- a/lib.ts +++ b/lib.ts @@ -20,19 +20,13 @@ Services.wm.addListener({ var EdTechHub: EdTechHubMain // eslint-disable-line no-var import { DebugLog as DebugLogSender } from 'zotero-plugin/debug-log' +import { patch as $patch$, unpatch as $unpatch$ } from './monkey-patch' Components.utils.import('resource://gre/modules/osfile.jsm') declare const OS: any import sanitize_filename = require('sanitize-filename') -const marker = 'EdTechHubMonkeyPatched' -function $patch$(object, method, patcher) { - if (object[method][marker]) return - object[method] = patcher(object[method]) - object[method][marker] = true -} - function flash(title, body = null, timeout = 8) { // eslint-disable-line @typescript-eslint/no-magic-numbers try { const pw = new Zotero.ProgressWindow() @@ -149,6 +143,8 @@ function debug(msg, err = null) { } function zotero_itemmenu_popupshowing() { + if (!Zotero.EdTechHub) return + const selected = Zotero.getActiveZoteroPane().getSelectedItems() const doc = Zotero.getMainWindow().document @@ -240,6 +236,7 @@ class EdTechHubMain { XUL: 'http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul', HTML: 'http://www.w3.org/1999/xhtml', } + // eslint-disable-next-line prefer-arrow/prefer-arrow-functions function create(name: string, attrs: Record = {}): HTMLElement { const children: HTMLElement[] = (attrs.$ as unknown as HTMLElement[]) || [] delete attrs.$ @@ -340,6 +337,19 @@ class EdTechHubMain { extra: Zotero.ItemFields.getID('extra'), } + $patch$(Zotero.Items, '_mergePDFAttachments', _original => async function(item, otherItems) { + Zotero.DB.requireTransaction() + for (const otherItem of otherItems) { + for (const otherAttachment of await this.getAsync(otherItem.getAttachments(true))) { + if (otherAttachment.isPDFAttachment()) { + otherAttachment.parentItemID = item.id + await otherAttachment.save() + } + } + } + return new Map + }) + $patch$(Zotero.Items, 'merge', original => async function(item, otherItems) { let alsoKnownAs: AlsoKnownAs = null let history: string = null @@ -433,6 +443,7 @@ class EdTechHubMain { const win = Zotero.getMainWindow() const doc = win.document + $unpatch$() doc.getElementById('zotero-itemmenu').removeEventListener('popupshowing', zotero_itemmenu_popupshowing, false) for (const elt of Array.from(doc.getElementsByClassName('edtechhub') as HTMLElement[])) { elt.remove()