chore: initial project commit
This commit is contained in:
73
src/modules/notifier.ts
Normal file
73
src/modules/notifier.ts
Normal file
@@ -0,0 +1,73 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user