add: notify hook example

This commit is contained in:
xiangyu 2023-01-05 20:28:03 +08:00
parent aa9763e9a9
commit 84beaab5fd
3 changed files with 46 additions and 23 deletions

View File

@ -1,6 +1,6 @@
<vbox <vbox
id="zotero-prefpane-__addonRef__" id="zotero-prefpane-__addonRef__"
onload="Zotero.AddonTemplate.hooks.onCustomEvent('prefLoad', {window})" onload="Zotero.AddonTemplate.hooks.onPrefsEvent('load', {window})"
> >
<groupbox> <groupbox>
<label><html:h2>&zotero.__addonRef__.pref.title;</html:h2></label> <label><html:h2>&zotero.__addonRef__.pref.title;</html:h2></label>

View File

@ -10,20 +10,20 @@ import { registerPrefsScripts } from "./modules/preferenceScript";
async function onStartup() { async function onStartup() {
initLocale(); initLocale();
const w = showProgressWindow( const progWin = showProgressWindow(
config.addonName, config.addonName,
getString("startup.begin"), getString("startup.begin"),
"default", "default",
-1 -1
); );
changeProgressWindowLine(w, { newProgress: 0 }); changeProgressWindowLine(progWin, { newProgress: 0 });
BasicExampleFactory.registerPrefs(); BasicExampleFactory.registerPrefs();
BasicExampleFactory.registerNotifier(); BasicExampleFactory.registerNotifier();
await Zotero.Promise.delay(1000); await Zotero.Promise.delay(1000);
changeProgressWindowLine(w, { changeProgressWindowLine(progWin, {
newProgress: 30, newProgress: 30,
newText: `[30%] ${getString("startup.begin")}`, newText: `[30%] ${getString("startup.begin")}`,
}); });
@ -47,11 +47,11 @@ async function onStartup() {
await UIExampleFactory.registerReaderTabPanel(); await UIExampleFactory.registerReaderTabPanel();
await Zotero.Promise.delay(1000); await Zotero.Promise.delay(1000);
changeProgressWindowLine(w, { changeProgressWindowLine(progWin, {
newProgress: 100, newProgress: 100,
newText: `[100%] ${getString("startup.finish")}`, newText: `[100%] ${getString("startup.finish")}`,
}); });
w.startCloseTimer(5000); progWin.startCloseTimer(5000);
} }
function onShutdown(): void { function onShutdown(): void {
@ -63,14 +63,37 @@ function onShutdown(): void {
} }
/** /**
* This function is just a dispatcher for UI events. * This function is just an example of dispatcher for Notify events.
* Any operations should be placed in a function to keep this funcion clear * Any operations should be placed in a function to keep this funcion clear.
*/
async function onNotify(
event: string,
type: string,
ids: Array<string>,
extraData: { [key: string]: any }
) {
// You can add your code to the corresponding notify type
ztoolkit.Tool.log("notify", event, type, ids, extraData);
if (
event == "select" &&
type == "tab" &&
extraData[ids[0]].type == "reader"
) {
BasicExampleFactory.exampleNotifierCallback();
} else {
return;
}
}
/**
* This function is just an example of dispatcher for Preference UI events.
* Any operations should be placed in a function to keep this funcion clear.
* @param type event type * @param type event type
* @param data event data * @param data event data
*/ */
function onCustomEvent(type: string, data: { [key: string]: any }) { async function onPrefsEvent(type: string, data: { [key: string]: any }) {
switch (type) { switch (type) {
case "prefLoad": case "load":
registerPrefsScripts(data.window); registerPrefsScripts(data.window);
break; break;
default: default:
@ -78,8 +101,13 @@ function onCustomEvent(type: string, data: { [key: string]: any }) {
} }
} }
// Add your hooks here. For element click, etc.
// Keep in mind hooks only do dispatch. Don't add code that does real jobs in hooks.
// Otherwise the code would be hard to read and maintian.
export default { export default {
onStartup, onStartup,
onShutdown, onShutdown,
onCustomEvent, onNotify,
onPrefsEvent,
}; };

View File

@ -1,5 +1,6 @@
import { config } from "../../package.json"; import { config } from "../../package.json";
import { getString } from "./locale"; import { getString } from "./locale";
import { showProgressWindow } from "./progressWindow";
function example( function example(
target: any, target: any,
@ -38,18 +39,7 @@ export class BasicExampleFactory {
this.unregisterNotifier(notifierID); this.unregisterNotifier(notifierID);
return; return;
} }
ztoolkit.Tool.log("notify", event, type, ids, extraData); addon.hooks.onNotify(event, type, ids, extraData);
// You can add your code to the corresponding notify type
if (
event == "select" &&
type == "tab" &&
extraData[ids[0]].type == "reader"
) {
// Select a reader tab
}
if (event == "add" && type == "item") {
// Add an item
}
}, },
}; };
@ -70,6 +60,11 @@ export class BasicExampleFactory {
); );
} }
@example
static exampleNotifierCallback() {
showProgressWindow(config.addonName, "Open Tab Detected!", "success");
}
@example @example
private static unregisterNotifier(notifierID: string) { private static unregisterNotifier(notifierID: string) {
Zotero.Notifier.unregisterObserver(notifierID); Zotero.Notifier.unregisterObserver(notifierID);