Skip to content

Commit

Permalink
Merge branch 'master' into gh-3076
Browse files Browse the repository at this point in the history
  • Loading branch information
retorquere committed Nov 27, 2024
2 parents 5d7704b + 1fcfb62 commit 849f2b7
Show file tree
Hide file tree
Showing 26 changed files with 308 additions and 206 deletions.
2 changes: 1 addition & 1 deletion content/ajv.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// 2020 for prefixItems
import AJV from 'ajv/dist/2020'
import { discard, log } from './logger/simple'
import { discard, log } from './logger'

const options = {
strict: false,
Expand Down
7 changes: 3 additions & 4 deletions content/auto-export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -582,16 +582,15 @@ export const AutoExport = new class $AutoExport { // eslint-disable-line @typesc
recursive: job.recursive ?? false,
updated: job.updated || Date.now(),
enabled: true,

}

const valid = autoExport[job.translatorID]
const displayOptions = byId[job.translatorID]?.displayOptions || {}
for (const pref of valid.preferences) {
ae[pref] = ae[pref] ?? Preference[pref]
ae[pref] = ae[pref] ?? job[pref] ?? Preference[pref]
}
for (const option of valid.preferences) {
ae[option] = ae[option] ?? displayOptions[option] ?? false
for (const option of valid.options) {
ae[option] = ae[option] ?? job[option] ?? displayOptions[option] ?? false
}

blink.upsert(this.db, ae)
Expand Down
76 changes: 5 additions & 71 deletions content/better-bibtex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import type Bluebird from 'bluebird'
const Ready = Zotero.Promise.defer()

import { Shim } from './os'
import { is7, isBeta } from './client'
import { is7 } from './client'
const $OS = is7 ? Shim : OS

if (is7) Components.utils.importGlobalProperties(['FormData', 'indexedDB'])
Expand Down Expand Up @@ -769,19 +769,15 @@ export class BetterBibTeX {
})

if (is7) {
if (Zotero.ItemTreeManager.registerColumn && !isBeta) {
log.error('error: registerColumn has landed in release, please migrate')
// const columnDataKey = await Zotero.ItemTreeManager.registerColumn({...})
}
await Zotero.ItemTreeManager.registerColumns([{
const columnDataKey = await Zotero.ItemTreeManager.registerColumn({
dataKey: 'citationKey',
label: l10n.localize('better-bibtex_zotero-pane_column_citekey'),
pluginID: '[email protected]',
dataProvider: (item, _dataKey) => {
const citekey = Zotero.BetterBibTeX.KeyManager.get(item.id)
return citekey ? `${ citekey.citationKey }${ citekey.pinned ? icons.pin : '' }`.trim() : ''
},
}])
})

const rowID = Zotero.ItemPaneManager.registerInfoRow({
rowID: 'better-bibtex-citation-key',
Expand All @@ -803,7 +799,8 @@ export class BetterBibTeX {

Events.on('items-changed', () => {
Zotero.ItemPaneManager.refreshInfoRow(rowID)
// if (!ZoteroPane.itemPane.itemsView._columnPrefs[columnDataKey].hidden) Zotero.ItemTreeManager.refreshColumns()
// eslint-disable-next-line no-underscore-dangle
if (!Zotero.getActiveZoteroPane().itemPane.itemsView._columnPrefs[columnDataKey].hidden) Zotero.ItemTreeManager.refreshColumns()
})
}

Expand Down Expand Up @@ -862,69 +859,6 @@ export class BetterBibTeX {
}

async loadUI(win: Window): Promise<void> {
if (is7 && !isBeta) {
// const show = (item: ZoteroItem): { id: number, type: string, citekey: string } | boolean => item ? { id: item.id, type: Zotero.ItemTypes.getName(item.itemTypeID), citekey: item.getField('citationKey') as string } : false
if (Zotero.ItemPaneManager.registerInfoRow) {
log.error('Zotero.ItemPaneManager.registerInfoRow is GA')
}
else {
let $done: () => void
Zotero.ItemPaneManager.registerSection({
paneID: 'betterbibtex-section-citationkey',
pluginID: '[email protected]',
header: {
l10nID: 'better-bibtex_item-pane_section_header',
icon: `${ rootURI }content/skin/item-section/header.svg`,
},
sidenav: {
l10nID: 'better-bibtex_item-pane_section_sidenav',
icon: `${ rootURI }content/skin/item-section/sidenav.svg`,
},
bodyXHTML: 'Citation Key <html:input type="text" data-itemid="" id="better-bibtex-citation-key" readonly="true" style="flex: 1" xmlns:html="http://www.w3.org/1999/xhtml"/><html:span id="better-bibtex-citation-key-pinned"/>',
// onRender: ({ body, item, editable, tabType }) => {
onRender: ({ body, item, setSectionSummary }) => {
const citekey = Zotero.BetterBibTeX.KeyManager.get(item.id) || { citationKey: '', pinned: false }
const textbox = body.querySelector('#better-bibtex-citation-key')
body.style.display = 'flex'
// const was = textbox.dataset.itemid || '<node>'
textbox.value = citekey.citationKey
textbox.dataset.itemid = citekey.citationKey ? `${ item.id }` : ''

const pinned = body.querySelector('#better-bibtex-citation-key-pinned')
pinned.textContent = citekey.pinned ? icons.pin : ''

setSectionSummary(citekey || '')
},
onInit: ({ body, refresh }) => {
$done = Events.on('items-changed', ({ items }) => {
const textbox = body.querySelector('#better-bibtex-citation-key')
const itemID = textbox.dataset.itemid ? parseInt(textbox.dataset.itemid) : undefined
const displayed: ZoteroItem = textbox.dataset.itemid ? items.find(item => item.id === itemID) : undefined
if (displayed) refresh()
})
},
onItemChange: ({ setEnabled, body, item }) => {
const textbox = body.querySelector('#better-bibtex-citation-key')
if (item.isRegularItem() && !item.isFeedItem) {
const citekey = item.getField('citationKey')
// const was = textbox.dataset.itemid
textbox.dataset.itemid = citekey ? `${ item.id }` : ''
textbox.value = citekey || '\u274C'
setEnabled(true)
}
else {
textbox.dataset.itemid = ''
setEnabled(false)
}
},
onDestroy: () => {
$done?.()
$done = undefined
},
})
}
}

try {
await newZoteroPane(win)
if (!is7) await newZoteroItemPane(win)
Expand Down
2 changes: 1 addition & 1 deletion content/clean_pane_persist.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { log } from './logger/simple'
import { log } from './logger'

import { is7 } from './client'
export function clean_pane_persist(): void {
Expand Down
2 changes: 1 addition & 1 deletion content/db/cache.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { log } from '../logger/simple'
import { log } from '../logger'
import { stringify } from '../stringify'

import { is7, worker } from '../client'
Expand Down
2 changes: 1 addition & 1 deletion content/flash.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { log } from './logger/simple'
import { log } from './logger'

const seconds = 1000

Expand Down
2 changes: 1 addition & 1 deletion content/item-export-format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Shim } from './os'
import { is7 } from './client'
const $OS = is7 ? Shim : OS

import { log } from './logger/simple'
import { log } from './logger'
import { getItemsAsync } from './get-items-async'
import type { Attachment, RegularItem, Item, Note } from '../gen/typings/serialized-item'
export type Serialized = RegularItem | Attachment | Item
Expand Down
2 changes: 1 addition & 1 deletion content/key-manager/chinese.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Preference } from '../prefs'
import { Events } from '../events'
// import { CJK } from '../text'
import { discard } from '../logger/simple'
import { discard } from '../logger'

declare const ChromeUtils: any

Expand Down
133 changes: 85 additions & 48 deletions content/logger.ts
Original file line number Diff line number Diff line change
@@ -1,72 +1,109 @@
/* eslint-disable @typescript-eslint/no-empty-function, no-restricted-syntax */
/* eslint-disable @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/no-unsafe-return, @typescript-eslint/no-empty-function, no-restricted-syntax */

import type { Translators as Translator } from '../typings/translators'
declare const TranslationWorker: { job: Translator.Worker.Job }
import { $dump, run } from './logger/simple'
declare const Zotero: any

import { stringify } from './stringify'
import { worker } from './client'
import * as client from './client'
const version = require('./../gen/version.js')
export const run = `<${version} ${client.run}>`

function toString(obj): string {
try {
if (typeof obj === 'string') return obj
return stringify(obj, 0)
}
catch (err) {
return stringify(err, 0)
}
declare const dump: (msg: string) => void

export const discard = {
log(): void {},
error(): void {},
warn(): void {},
debug(): void {},
info(): void {},
clear(): void {},
dir(): void {},
table(): void {},
}

export const log = new class Logger {
protected timestamp: number
public prefix = ''
function stringifyXPCOM(obj): string {
if (!obj.QueryInterface) return ''
if (obj.message) return `[XPCOM error ${ obj.message }]`
if (obj.name) return `[XPCOM object ${ obj.name }]`
return '[XPCOM object]'
}

private format({ error = false }, msg) {
if (Array.isArray(msg)) msg = msg.map(toString).join(' ')
function stringifyError(obj) {
if (obj instanceof Error) return `[error: ${ obj.message || '<unspecified error>' }\n${ obj.stack }]`
// guess it is an errorevent
if (obj.error instanceof Error && obj.message) return `[errorevent: ${ obj.message } ${ stringifyError(obj.error) }]`
if (typeof ErrorEvent !== 'undefined' && obj instanceof ErrorEvent) return `[errorevent: ${ obj.message || '<unspecified errorevent>' }]`
return ''
}

let prefix = ''
if (worker) {
prefix += ' worker'
if (typeof TranslationWorker !== 'undefined') prefix += `:${ TranslationWorker.job.translator }`
function replacer() {
const seen = new WeakSet
return (key, value) => {
if (typeof value === 'object' && value !== null) {
if (seen.has(value)) return '[Circular]'
seen.add(value)
}

if (error) prefix += ' error:'
if (value === null) return value
if (value instanceof Set) return [...value]
if (value instanceof Map) return Object.fromEntries(value)

return `{better-bibtex ${run} ${ this.prefix }${ prefix }} ${ msg }`
}
switch (typeof value) {
case 'string':
case 'number':
case 'boolean':
return value

public get enabled(): boolean {
return (
(typeof TranslationWorker !== 'undefined' && TranslationWorker.job.debugEnabled)
|| !Zotero
|| Zotero.Debug?.enabled
|| Zotero.Prefs?.get('debug.store')
) as boolean
}
case 'object':
return stringifyXPCOM(value) || stringifyError(value) || value
}

public debug(...msg) {
Zotero.debug(this.format({}, msg))
if (Array.isArray(value)) return value

return undefined
}
}

public info(msg: string) {
Zotero.debug(this.format({}, msg))
function to_s(obj: any): string {
if (typeof obj === 'string') return obj
return JSON.stringify(obj, replacer(), 2)
}

export function format(...msg): void {
msg.map(to_s).join(' ')
}

export const log = new class {
public prefix = ''

#prefix(error?: any) {
return `{${ error ? 'error: ' : '' }${ client.worker ? 'worker: ' : '' }${this.prefix}better-bibtex: ${run}} `
}

public error(...msg) {
Zotero.debug(this.format({ error: true }, msg))
public debug(...msg): void {
Zotero.debug(`${this.#prefix()}${format(...msg)}\n`)
}

public status({ error = false }, ...msg) {
if (error || this.enabled) Zotero.debug(this.format({ error }, msg))
public info(...msg): void {
Zotero.debug(`${this.#prefix()}${format(...msg)}\n`)
}

public async timed(msg: string, code: () => void | Promise<void>) {
const start = Date.now()
await code()
this.debug(msg, 'took', Date.now() - start, 'ms')
public error(...msg): void {
Zotero.debug(`${this.#prefix(true)}${format(...msg)}\n`)
}

public dump(msg: string) {
$dump(msg)
public dump(msg: string, error?: Error): void {
if (error) {
dump(`${this.#prefix(error)}${format(msg, error)}\n`)
}
else {
dump(`${this.#prefix()}${format(msg)}\n`)
}
}
}

export function $dump(msg: string, error?: Error): void {
log.dump(msg, error)
}

export function trace(msg: string, mode = ''): void {
dump(`trace${ mode }\t${ Date.now() }\t${ msg }\n`)
}
48 changes: 0 additions & 48 deletions content/logger/simple.ts

This file was deleted.

2 changes: 1 addition & 1 deletion content/monkey-patch.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable @typescript-eslint/no-unsafe-function-type, prefer-rest-params, @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/no-unsafe-return, @typescript-eslint/no-shadow */

export type Trampoline = Function & { disabled?: boolean }
// import { log } from './logger/simple'
// import { log } from './logger'

type Patch = {
object: any
Expand Down
Loading

0 comments on commit 849f2b7

Please sign in to comment.