Skip to content

Commit

Permalink
neuter _mergePDFAttachments
Browse files Browse the repository at this point in the history
  • Loading branch information
retorquere committed Jan 4, 2024
1 parent 6fc7b4e commit 1c62f3c
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 34 deletions.
26 changes: 1 addition & 25 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand All @@ -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')
Expand Down
25 changes: 18 additions & 7 deletions lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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<string, number | string | Function | HTMLElement[]> = {}): HTMLElement {
const children: HTMLElement[] = (attrs.$ as unknown as HTMLElement[]) || []
delete attrs.$
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 1c62f3c

Please sign in to comment.