49 lines
1.3 KiB
JavaScript
49 lines
1.3 KiB
JavaScript
/**
|
|
* MessageBridge — postMessage protocol between the PDF viewer iframe
|
|
* and the parent Leptos application.
|
|
*
|
|
* Inbound (parent → iframe): "pdf.page.next" | "pdf.page.prev"
|
|
* Outbound (iframe → parent): { type: "brittle:keydown", key, ctrlKey, ... }
|
|
*/
|
|
export class MessageBridge {
|
|
/**
|
|
* @param {Function} onPageNext - () => void
|
|
* @param {Function} onPagePrev - () => void
|
|
*/
|
|
constructor(onPageNext, onPagePrev) {
|
|
this._handler = ev => {
|
|
if (ev.data === "pdf.page.next") onPageNext();
|
|
if (ev.data === "pdf.page.prev") onPagePrev();
|
|
};
|
|
window.addEventListener("message", this._handler);
|
|
}
|
|
|
|
/** Send the current viewer state (zoom + scroll) to the parent window. */
|
|
postViewerState(refId, zoom, scrollTop) {
|
|
if (window.parent === window) return;
|
|
window.parent.postMessage({
|
|
type: "brittle:viewer-state",
|
|
refId,
|
|
zoom,
|
|
scrollTop,
|
|
}, "*");
|
|
}
|
|
|
|
/** Forward a keydown event to the parent window for global keybindings. */
|
|
forwardKeydown(ev) {
|
|
if (window.parent === window) return;
|
|
window.parent.postMessage({
|
|
type: "brittle:keydown",
|
|
key: ev.key,
|
|
ctrlKey: ev.ctrlKey,
|
|
shiftKey: ev.shiftKey,
|
|
altKey: ev.altKey,
|
|
metaKey: ev.metaKey,
|
|
}, "*");
|
|
}
|
|
|
|
disconnect() {
|
|
window.removeEventListener("message", this._handler);
|
|
}
|
|
}
|