-
Notifications
You must be signed in to change notification settings - Fork 121
/
Copy pathhooks.ts
194 lines (161 loc) · 4.64 KB
/
hooks.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import {
BasicExampleFactory,
HelperExampleFactory,
KeyExampleFactory,
PromptExampleFactory,
UIExampleFactory,
} from "./modules/examples";
import { getString, initLocale } from "./utils/locale";
import { registerPrefsScripts } from "./modules/preferenceScript";
import { createZToolkit } from "./utils/ztoolkit";
async function onStartup() {
await Promise.all([
Zotero.initializationPromise,
Zotero.unlockPromise,
Zotero.uiReadyPromise,
]);
initLocale();
BasicExampleFactory.registerPrefs();
BasicExampleFactory.registerNotifier();
KeyExampleFactory.registerShortcuts();
await UIExampleFactory.registerExtraColumn();
await UIExampleFactory.registerExtraColumnWithCustomCell();
UIExampleFactory.registerItemPaneCustomInfoRow();
UIExampleFactory.registerItemPaneSection();
UIExampleFactory.registerReaderItemPaneSection();
await Promise.all(
Zotero.getMainWindows().map((win) => onMainWindowLoad(win)),
);
}
async function onMainWindowLoad(win: Window): Promise<void> {
// Create ztoolkit for every window
addon.data.ztoolkit = createZToolkit();
// @ts-ignore This is a moz feature
win.MozXULElement.insertFTLIfNeeded(
`${addon.data.config.addonRef}-mainWindow.ftl`,
);
const popupWin = new ztoolkit.ProgressWindow(addon.data.config.addonName, {
closeOnClick: true,
closeTime: -1,
})
.createLine({
text: getString("startup-begin"),
type: "default",
progress: 0,
})
.show();
await Zotero.Promise.delay(1000);
popupWin.changeLine({
progress: 30,
text: `[30%] ${getString("startup-begin")}`,
});
UIExampleFactory.registerStyleSheet(win);
UIExampleFactory.registerRightClickMenuItem();
UIExampleFactory.registerRightClickMenuPopup(win);
UIExampleFactory.registerWindowMenuWithSeparator();
PromptExampleFactory.registerNormalCommandExample();
PromptExampleFactory.registerAnonymousCommandExample(win);
PromptExampleFactory.registerConditionalCommandExample();
await Zotero.Promise.delay(1000);
popupWin.changeLine({
progress: 100,
text: `[100%] ${getString("startup-finish")}`,
});
popupWin.startCloseTimer(5000);
addon.hooks.onDialogEvents("dialogExample");
}
async function onMainWindowUnload(win: Window): Promise<void> {
ztoolkit.unregisterAll();
addon.data.dialog?.window?.close();
}
function onShutdown(): void {
ztoolkit.unregisterAll();
addon.data.dialog?.window?.close();
// Remove addon object
addon.data.alive = false;
// @ts-ignore - Plugin instance is not typed
delete Zotero[addon.data.config.addonInstance];
}
/**
* This function is just an example of dispatcher for Notify events.
* Any operations should be placed in a function to keep this funcion clear.
*/
async function onNotify(
event: string,
type: string,
ids: Array<string | number>,
extraData: { [key: string]: any },
) {
// You can add your code to the corresponding notify type
ztoolkit.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 data event data
*/
async function onPrefsEvent(type: string, data: { [key: string]: any }) {
switch (type) {
case "load":
registerPrefsScripts(data.window);
break;
default:
return;
}
}
function onShortcuts(type: string) {
switch (type) {
case "larger":
KeyExampleFactory.exampleShortcutLargerCallback();
break;
case "smaller":
KeyExampleFactory.exampleShortcutSmallerCallback();
break;
default:
break;
}
}
function onDialogEvents(type: string) {
switch (type) {
case "dialogExample":
HelperExampleFactory.dialogExample();
break;
case "clipboardExample":
HelperExampleFactory.clipboardExample();
break;
case "filePickerExample":
HelperExampleFactory.filePickerExample();
break;
case "progressWindowExample":
HelperExampleFactory.progressWindowExample();
break;
case "vtableExample":
HelperExampleFactory.vtableExample();
break;
default:
break;
}
}
// 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 maintain.
export default {
onStartup,
onShutdown,
onMainWindowLoad,
onMainWindowUnload,
onNotify,
onPrefsEvent,
onShortcuts,
onDialogEvents,
};