-
Notifications
You must be signed in to change notification settings - Fork 12
/
itemBox.ts
301 lines (283 loc) · 9.65 KB
/
itemBox.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
import { BasicOptions, BasicTool, ManagerTool } from "../basic";
import {
FieldHookManager,
getFieldHookFunc,
setFieldHookFunc,
} from "./fieldHook";
import { PatcherManager } from "./patch";
import ToolkitGlobal from "./toolkitGlobal";
/**
* Register customized new row to the library itemBox (right-side info tab).
*/
export class ItemBoxManager extends ManagerTool {
private globalCache!: ItemBoxGlobal;
private localCache: string[];
private fieldHooks: FieldHookManager;
private patcherManager: PatcherManager;
private initializationLock: _ZoteroTypes.PromiseObject;
constructor(base?: BasicTool | BasicOptions) {
super(base);
this.initializationLock = this.getGlobal("Zotero").Promise.defer();
this.localCache = [];
this.fieldHooks = new FieldHookManager();
this.patcherManager = new PatcherManager();
this.initializeGlobal();
}
/**
* Register a custom row
* @param field Field name. Used in `getField` and `setField`.
* @param displayName The row header display text.
* @param getFieldHook Called when loading row content.
* If you registered the getField hook somewhere else (in ItemBox or FieldHooks), leave it undefined.
* @param options
* @param options.editable If the row is editable.
* To edit a row, either the `options.setFieldHook` or a custom hook for `setField` created by FieldHookManager is required.
* @param options.setFieldHook The `setField` hook.
* @param options.index Target index. By default it's placed at the end of rows.
* @param options.multiline If the row content is multiline.
* @param options.collapsible If the row content is collapsible (like abstract field).
*/
public async register(
field: string,
displayName: string,
getFieldHook: typeof getFieldHookFunc | undefined,
options: {
editable?: boolean;
setFieldHook?: typeof setFieldHookFunc;
index?: number;
multiline?: boolean;
collapsible?: boolean;
} = {}
) {
this.fieldHooks.register("isFieldOfBase", field, () => false);
if (getFieldHook) {
this.fieldHooks.register("getField", field, getFieldHook);
}
if (options.editable && options.setFieldHook) {
this.fieldHooks.register("setField", field, options.setFieldHook);
}
this.globalCache.fieldOptions[field] = {
field,
displayName,
editable: options.editable || false,
index: options.index || -1,
multiline: options.multiline || false,
collapsible: options.collapsible || false,
};
this.localCache.push(field);
await this.initializationLock.promise;
this.refresh();
}
/**
* Unregister a row of specific field.
* @param field
* @param options Skip unregister of certain hooks.
* This is useful when the hook is not initialized by this instance
* @param options.skipRefresh Skip refresh after unregister.
*/
unregister(
field: string,
options: {
skipIsFieldOfBase?: boolean;
skipGetField?: boolean;
skipSetField?: boolean;
skipRefresh?: boolean;
} = {}
) {
delete this.globalCache.fieldOptions[field];
if (!options.skipIsFieldOfBase) {
this.fieldHooks.unregister("isFieldOfBase", field);
}
if (!options.skipGetField) {
this.fieldHooks.unregister("getField", field);
}
if (!options.skipSetField) {
this.fieldHooks.unregister("setField", field);
}
const idx = this.localCache.indexOf(field);
if (idx > -1) {
this.localCache.splice(idx, 1);
}
if (!options.skipRefresh) {
this.refresh();
}
}
public unregisterAll() {
// Skip field hook unregister and use fieldHooks.unregisterAll
// to unregister those created by this manager only
[...this.localCache].forEach((field) =>
this.unregister(field, {
skipGetField: true,
skipSetField: true,
skipIsFieldOfBase: true,
skipRefresh: true,
})
);
this.fieldHooks.unregisterAll();
this.refresh();
}
/**
* Refresh all item boxes.
*/
public refresh() {
try {
Array.from(
this.getGlobal("document").querySelectorAll(
this.isZotero7() ? "item-box" : "zoteroitembox"
)
).forEach((elem) => (elem as any).refresh());
} catch (e) {
this.log(e);
}
}
private async initializeGlobal() {
const Zotero = this.getGlobal("Zotero");
await Zotero.uiReadyPromise;
const window = this.getGlobal("window");
this.globalCache = ToolkitGlobal.getInstance().itemBox;
const globalCache = this.globalCache;
const inZotero7 = this.isZotero7();
if (!globalCache._ready) {
globalCache._ready = true;
let itemBoxInstance;
if (inZotero7) {
itemBoxInstance = new (this.getGlobal("customElements").get(
"item-box"
)!)();
} else {
itemBoxInstance = window.document.querySelector(
"#zotero-editpane-item-box"
);
const wait = 5000;
let t = 0;
while (!itemBoxInstance && t < wait) {
itemBoxInstance = window.document.querySelector(
"#zotero-editpane-item-box"
);
await Zotero.Promise.delay(10);
t += 10;
}
if (!itemBoxInstance) {
globalCache._ready = false;
this.log("ItemBox initialization failed");
return;
}
}
this.patcherManager.register(
(itemBoxInstance as any).__proto__,
"refresh",
(original) =>
function () {
// @ts-ignore
const originalThis = this;
original.apply(originalThis, arguments);
for (const extraField of Object.values(globalCache.fieldOptions)) {
const fieldHeader = document.createElement(
inZotero7 ? "th" : "label"
);
fieldHeader.setAttribute("fieldname", extraField.field);
const prefKey = `extensions.zotero.pluginToolkit.fieldCollapsed.${extraField.field}`;
const collapsed =
extraField.multiline &&
extraField.collapsible &&
Zotero.Prefs.get(prefKey, true);
let headerContent = extraField.displayName;
if (collapsed) {
headerContent = `(...)${headerContent}`;
}
if (inZotero7) {
let label = document.createElement("label");
label.className = "key";
label.textContent = headerContent;
fieldHeader.appendChild(label);
} else {
fieldHeader.setAttribute("value", headerContent);
}
const _clickable = originalThis.clickable;
originalThis.clickable = extraField.editable;
const fieldValue = originalThis.createValueElement(
originalThis.item.getField(extraField.field),
extraField.field,
1099
) as HTMLElement;
originalThis.clickable = _clickable;
// Zotero 6 is multiline by default, while Zotero 7 is not
if (extraField.multiline && !Zotero.Prefs.get(prefKey, true)) {
fieldValue.classList.add("multiline");
} else if (!inZotero7) {
// disable multiline in Zotero 6
fieldValue.setAttribute("crop", "end");
fieldValue.setAttribute("value", fieldValue.innerHTML);
fieldValue.innerHTML = "";
}
if (extraField.collapsible) {
fieldHeader.addEventListener("click", function (ev) {
Zotero.Prefs.set(
prefKey,
!(Zotero.Prefs.get(prefKey, true) || false),
true
);
originalThis.refresh();
});
}
fieldHeader.addEventListener(
"click",
inZotero7
? function (ev) {
const inputField = (
ev.currentTarget as HTMLElement
).nextElementSibling?.querySelector(
"input, textarea"
) as any;
if (inputField) {
inputField.blur();
}
}
: function (ev) {
const inputField = (
(ev.currentTarget as HTMLElement)
.nextElementSibling as any
)?.inputField;
if (inputField) {
inputField.blur();
}
}
);
const table = inZotero7
? originalThis._infoTable
: originalThis._dynamicFields;
let fieldIndex = extraField.index;
// Index 0 must be itemType field.
if (fieldIndex === 0) {
fieldIndex = 1;
}
if (
fieldIndex &&
fieldIndex >= 0 &&
fieldIndex < table.children.length
) {
originalThis._beforeRow = table.children[fieldIndex];
originalThis.addDynamicRow(fieldHeader, fieldValue, true);
} else {
originalThis.addDynamicRow(fieldHeader, fieldValue);
}
}
}
);
}
this.initializationLock.resolve();
}
}
export interface ItemBoxGlobal {
_ready: boolean;
fieldOptions: {
[key: string]: {
field: string;
displayName: string;
index: number | undefined;
editable: boolean;
multiline: boolean;
collapsible: boolean;
};
};
}