2024-03-20 13:01:09 +02:00
|
|
|
|
|
|
|
import WebViewToRNMessenger from '../../../utils/ipc/WebViewToRNMessenger';
|
2024-07-16 20:27:08 +02:00
|
|
|
import { NoteViewerLocalApi, NoteViewerRemoteApi, RendererWebViewOptions, WebViewLib } from './types';
|
2024-03-20 13:01:09 +02:00
|
|
|
import Renderer from './Renderer';
|
|
|
|
|
|
|
|
declare global {
|
|
|
|
interface Window {
|
|
|
|
rendererWebViewOptions: RendererWebViewOptions;
|
2024-07-16 20:27:08 +02:00
|
|
|
webviewLib: WebViewLib;
|
2024-03-20 13:01:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2024-07-16 20:27:08 +02:00
|
|
|
declare const webviewLib: WebViewLib;
|
2024-03-20 13:01:09 +02:00
|
|
|
|
|
|
|
const messenger = new WebViewToRNMessenger<NoteViewerLocalApi, NoteViewerRemoteApi>(
|
|
|
|
'note-viewer',
|
|
|
|
null,
|
|
|
|
);
|
|
|
|
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2024-03-20 13:01:09 +02:00
|
|
|
(window as any).joplinPostMessage_ = (message: string, _args: any) => {
|
|
|
|
return messenger.remoteApi.onPostMessage(message);
|
|
|
|
};
|
|
|
|
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2024-03-20 13:01:09 +02:00
|
|
|
(window as any).webviewApi = {
|
|
|
|
postMessage: messenger.remoteApi.onPostPluginMessage,
|
|
|
|
};
|
|
|
|
|
|
|
|
webviewLib.initialize({
|
|
|
|
postMessage: (message: string) => {
|
|
|
|
messenger.remoteApi.onPostMessage(message);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2024-07-16 20:27:08 +02:00
|
|
|
window.webviewLib = webviewLib;
|
|
|
|
|
2024-03-20 13:01:09 +02:00
|
|
|
const renderer = new Renderer({
|
|
|
|
...window.rendererWebViewOptions,
|
|
|
|
fsDriver: messenger.remoteApi.fsDriver,
|
|
|
|
});
|
|
|
|
|
|
|
|
messenger.setLocalInterface({
|
|
|
|
renderer,
|
|
|
|
jumpToHash: (hash: string) => {
|
|
|
|
location.hash = `#${hash}`;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const lastScrollTop: number|null = null;
|
|
|
|
const onMainContentScroll = () => {
|
|
|
|
const newScrollTop = document.scrollingElement.scrollTop;
|
|
|
|
if (lastScrollTop !== newScrollTop) {
|
|
|
|
messenger.remoteApi.onScroll(newScrollTop);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Listen for events on both scrollingElement and window
|
|
|
|
// - On Android, scrollingElement.addEventListener('scroll', callback) doesn't call callback on
|
|
|
|
// scroll. However, window.addEventListener('scroll', callback) does.
|
|
|
|
// - iOS needs a listener to be added to scrollingElement -- events aren't received when
|
|
|
|
// the listener is added to window with window.addEventListener('scroll', ...).
|
|
|
|
document.scrollingElement.addEventListener('scroll', onMainContentScroll);
|
|
|
|
window.addEventListener('scroll', onMainContentScroll);
|