74 lines
2.0 KiB
TypeScript
74 lines
2.0 KiB
TypeScript
import type { OrgExportAnnotationsAddon } from "../addon";
|
|
|
|
export function registerNotifiers(addon: OrgExportAnnotationsAddon): string[] {
|
|
const ids: string[] = [];
|
|
|
|
const syncNotifierID = Zotero.Notifier.registerObserver(
|
|
{
|
|
notify: async (
|
|
event: string,
|
|
type: string,
|
|
_ids: number[],
|
|
_extraData: Record<string, unknown>
|
|
) => {
|
|
if (event === "finish" && type === "sync") {
|
|
addon.log("Sync completed, checking for exports...");
|
|
if (addon.prefs.autoExportOnSync) {
|
|
try {
|
|
await addon.exporter.syncAll();
|
|
} catch (error) {
|
|
addon.error("Auto-export after sync failed", error as Error);
|
|
}
|
|
}
|
|
}
|
|
},
|
|
},
|
|
["sync"],
|
|
"orgexportannotations-sync"
|
|
);
|
|
ids.push(syncNotifierID);
|
|
|
|
const tabNotifierID = Zotero.Notifier.registerObserver(
|
|
{
|
|
notify: async (
|
|
event: string,
|
|
type: string,
|
|
ids: number[],
|
|
extraData: Record<string, unknown>
|
|
) => {
|
|
if (event === "close" && type === "tab") {
|
|
if (!addon.prefs.exportOnTabClose) return;
|
|
|
|
addon.log("Tab closed, checking for export...");
|
|
|
|
for (const tabId of ids) {
|
|
try {
|
|
const tabInfo = extraData[tabId] as { itemID?: number } | undefined;
|
|
if (tabInfo?.itemID) {
|
|
const item = Zotero.Items.get(tabInfo.itemID);
|
|
if (item) {
|
|
await addon.exporter.exportItem(item);
|
|
}
|
|
}
|
|
} catch (error) {
|
|
addon.error(`Failed to export on tab close: ${tabId}`, error as Error);
|
|
}
|
|
}
|
|
}
|
|
},
|
|
},
|
|
["tab"],
|
|
"orgexportannotations-tab"
|
|
);
|
|
ids.push(tabNotifierID);
|
|
|
|
addon.log(`Registered ${ids.length} notifiers`);
|
|
return ids;
|
|
}
|
|
|
|
export function unregisterNotifiers(ids: string[]): void {
|
|
for (const id of ids) {
|
|
Zotero.Notifier.unregisterObserver(id);
|
|
}
|
|
}
|