Enable plugins adding custom content to reader UI and context menus

#3373
This commit is contained in:
Martynas Bagdonas 2023-09-13 14:02:39 +02:00
parent 40b30f2b63
commit 96652ea9e8
2 changed files with 58 additions and 1 deletions

View file

@ -185,6 +185,16 @@ class ReaderInstance {
}
};
this._iframeWindow.addEventListener('customEvent', (event) => {
let data = event.detail.wrappedJSObject;
let append = data.append;
data.append = (...args) => {
append(...Components.utils.cloneInto(args, this._iframeWindow, { wrapReflectors: true, cloneFunctions: true }));
};
data.reader = this;
Zotero.Reader._dispatchEvent(data);
});
this._internalReader = this._iframeWindow.wrappedJSObject.createReader(Components.utils.cloneInto({
type: this._type,
data,
@ -1208,6 +1218,7 @@ class Reader {
this._bottomPlaceholderHeight = 0;
this._readers = [];
this._notifierID = Zotero.Notifier.registerObserver(this, ['item', 'tab'], 'reader');
this._registeredListeners = [];
this.onChangeSidebarWidth = null;
this.onToggleSidebar = null;
@ -1219,6 +1230,52 @@ class Reader {
this._setSidebarState();
}, 500);
}
_dispatchEvent(event) {
for (let listener of this._registeredListeners) {
if (listener.type === event.type) {
listener.handler(event);
}
}
}
/**
* Inject DOM nodes to reader UI parts:
* - renderTextSelectionPopup
* - renderSidebarAnnotationHeader
* - renderToolbar
*
* Zotero.Reader.registerEventListener('renderTextSelectionPopup', (event) => {
* let { reader, doc, params, append } = event;
* let container = doc.createElement('div');
* container.append('Loading…');
* append(container);
* setTimeout(() => container.replaceChildren('Translated text: ' + params.annotation.text), 1000);
* });
*
*
* Add options to context menus:
* - createColorContextMenu
* - createViewContextMenu
* - createAnnotationContextMenu
* - createThumbnailContextMenu
* - createSelectorContextMenu
*
* Zotero.Reader.registerEventListener('createAnnotationContextMenu', (event) => {
* let { reader, params, append } = event;
* append({
* label: 'Test',
* onCommand(){ reader._iframeWindow.alert('Selected annotations: ' + params.ids.join(', ')); }
* });
* });
*/
registerEventListener(type, handler) {
this._registeredListeners.push({ type, handler });
}
unregisterEventListener(type, handler) {
this._registeredListeners = this._registeredListeners.filter(x => x.type === type && x.handler === handler);
}
getSidebarWidth() {
return this._sidebarWidth;

2
reader

@ -1 +1 @@
Subproject commit 49fe24cfbfeb3f3e20b3711b9a71d6aaf9f72206
Subproject commit 0ffdc5f6a5f74b11ee1d9fd2b65595bf5dea9e22