2021-01-12 01:33:10 +02:00
|
|
|
import PostMessageService, { MessageResponse, ResponderComponentType } from '@joplin/lib/services/PostMessageService';
|
2020-09-29 09:40:14 +02:00
|
|
|
import * as React from 'react';
|
2021-01-29 20:45:11 +02:00
|
|
|
import { reg } from '@joplin/lib/registry';
|
2024-07-26 13:22:49 +02:00
|
|
|
import bridge from '../services/bridge';
|
2024-04-01 16:34:22 +02:00
|
|
|
import { focus } from '@joplin/lib/utils/focusHandler';
|
2024-11-08 17:32:05 +02:00
|
|
|
import { ForwardedRef, forwardRef, RefObject, useContext, useEffect, useImperativeHandle, useMemo, useRef, useState } from 'react';
|
|
|
|
import { WindowIdContext } from './NewWindowOrIFrame';
|
|
|
|
import useDocument from './hooks/useDocument';
|
2024-11-09 14:50:06 +02:00
|
|
|
import { _ } from '@joplin/lib/locale';
|
2019-02-09 01:07:01 +02:00
|
|
|
|
2020-09-29 09:40:14 +02:00
|
|
|
interface Props {
|
2023-06-30 11:30:29 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-types -- Old code before rule was applied
|
2020-11-12 21:29:22 +02:00
|
|
|
onDomReady: Function;
|
2023-06-30 11:30:29 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-types -- Old code before rule was applied
|
2020-11-12 21:29:22 +02:00
|
|
|
onIpcMessage: Function;
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2020-11-12 21:29:22 +02:00
|
|
|
viewerStyle: any;
|
2023-01-19 19:19:06 +02:00
|
|
|
contentMaxWidth?: number;
|
|
|
|
themeId: number;
|
2020-09-29 09:40:14 +02:00
|
|
|
}
|
2019-02-09 01:07:01 +02:00
|
|
|
|
2024-07-26 13:22:49 +02:00
|
|
|
|
|
|
|
interface SetHtmlOptions {
|
|
|
|
pluginAssets: { path: string }[];
|
|
|
|
}
|
|
|
|
|
2024-11-08 17:32:05 +02:00
|
|
|
export interface NoteViewerControl {
|
|
|
|
domReady(): boolean;
|
|
|
|
setHtml(html: string, options: SetHtmlOptions): void;
|
|
|
|
send(channel: string, arg0?: unknown, arg1?: unknown): void;
|
|
|
|
focus(): void;
|
|
|
|
hasFocus(): boolean;
|
|
|
|
}
|
2019-02-09 01:07:01 +02:00
|
|
|
|
2024-11-08 17:32:05 +02:00
|
|
|
const usePluginMessageResponder = (webviewRef: RefObject<HTMLIFrameElement>) => {
|
|
|
|
const windowId = useContext(WindowIdContext);
|
2019-02-09 01:07:01 +02:00
|
|
|
|
2024-11-08 17:32:05 +02:00
|
|
|
useEffect(() => {
|
|
|
|
PostMessageService.instance().registerResponder(ResponderComponentType.NoteTextViewer, '', windowId, (message: MessageResponse) => {
|
|
|
|
if (!webviewRef?.current?.contentWindow) {
|
2021-01-12 01:33:10 +02:00
|
|
|
reg.logger().warn('Cannot respond to message because target is gone', message);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-11-08 17:32:05 +02:00
|
|
|
webviewRef.current.contentWindow.postMessage({
|
2021-01-12 01:33:10 +02:00
|
|
|
target: 'webview',
|
|
|
|
name: 'postMessageService.response',
|
|
|
|
data: message,
|
|
|
|
}, '*');
|
|
|
|
});
|
|
|
|
|
2024-11-08 17:32:05 +02:00
|
|
|
return () => {
|
|
|
|
PostMessageService.instance().unregisterResponder(ResponderComponentType.NoteTextViewer, '', windowId);
|
|
|
|
};
|
|
|
|
}, [webviewRef, windowId]);
|
|
|
|
};
|
|
|
|
|
|
|
|
const NoteTextViewer = forwardRef((props: Props, ref: ForwardedRef<NoteViewerControl>) => {
|
|
|
|
const [webview, setWebview] = useState<HTMLIFrameElement|null>(null);
|
|
|
|
const webviewRef = useRef<HTMLIFrameElement|null>(null);
|
|
|
|
webviewRef.current = webview;
|
|
|
|
usePluginMessageResponder(webviewRef);
|
|
|
|
|
|
|
|
const domReadyRef = useRef(false);
|
|
|
|
type RemovePluginAssetsCallback = ()=> void;
|
|
|
|
const removePluginAssetsCallbackRef = useRef<RemovePluginAssetsCallback|null>(null);
|
|
|
|
|
|
|
|
const parentDoc = useDocument(webview);
|
|
|
|
const containerWindow = parentDoc?.defaultView;
|
|
|
|
|
|
|
|
useImperativeHandle(ref, () => {
|
|
|
|
const result: NoteViewerControl = {
|
|
|
|
domReady: () => domReadyRef.current,
|
|
|
|
setHtml: (html: string, options: SetHtmlOptions) => {
|
|
|
|
const protocolHandler = bridge().electronApp().getCustomProtocolHandler();
|
|
|
|
|
|
|
|
// Grant & remove asset access.
|
|
|
|
if (options.pluginAssets) {
|
|
|
|
removePluginAssetsCallbackRef.current?.();
|
|
|
|
|
|
|
|
const pluginAssetPaths: string[] = options.pluginAssets.map((asset) => asset.path);
|
|
|
|
const assetAccesses = pluginAssetPaths.map(
|
|
|
|
path => protocolHandler.allowReadAccessToFile(path),
|
|
|
|
);
|
|
|
|
|
|
|
|
removePluginAssetsCallbackRef.current = () => {
|
|
|
|
for (const accessControl of assetAccesses) {
|
|
|
|
accessControl.remove();
|
|
|
|
}
|
|
|
|
|
|
|
|
removePluginAssetsCallbackRef.current = null;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
result.send('setHtml', html, {
|
|
|
|
...options,
|
|
|
|
mediaAccessKey: protocolHandler.getMediaAccessKey(),
|
|
|
|
});
|
|
|
|
},
|
|
|
|
send: (channel: string, arg0: unknown = null, arg1: unknown = null) => {
|
|
|
|
const win = webviewRef.current?.contentWindow;
|
2019-02-09 01:07:01 +02:00
|
|
|
|
2024-11-08 17:32:05 +02:00
|
|
|
// Window may already be closed
|
|
|
|
if (!win) return;
|
2019-02-09 01:07:01 +02:00
|
|
|
|
2024-11-08 17:32:05 +02:00
|
|
|
if (channel === 'focus') {
|
|
|
|
win.postMessage({ target: 'webview', name: 'focus', data: {} }, '*');
|
|
|
|
}
|
|
|
|
|
|
|
|
// External code should use .setHtml (rather than send('setHtml', ...))
|
|
|
|
if (channel === 'setHtml') {
|
|
|
|
win.postMessage({ target: 'webview', name: 'setHtml', data: { html: arg0, options: arg1 } }, '*');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (channel === 'scrollToHash') {
|
|
|
|
win.postMessage({ target: 'webview', name: 'scrollToHash', data: { hash: arg0 } }, '*');
|
|
|
|
}
|
2019-02-09 01:07:01 +02:00
|
|
|
|
2024-11-08 17:32:05 +02:00
|
|
|
if (channel === 'setPercentScroll') {
|
|
|
|
win.postMessage({ target: 'webview', name: 'setPercentScroll', data: { percent: arg0 } }, '*');
|
|
|
|
}
|
2019-12-17 02:45:27 +02:00
|
|
|
|
2024-11-08 17:32:05 +02:00
|
|
|
if (channel === 'setMarkers') {
|
|
|
|
win.postMessage({ target: 'webview', name: 'setMarkers', data: { keywords: arg0, options: arg1 } }, '*');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
focus: () => {
|
|
|
|
if (webviewRef.current) {
|
|
|
|
// Calling focus on webviewRef seems to be necessary when NoteTextViewer.focus
|
|
|
|
// is called outside of a user event (e.g. in a setTimeout) or during automated
|
|
|
|
// tests:
|
|
|
|
focus('NoteTextViewer::focus', webviewRef.current);
|
|
|
|
|
|
|
|
// Calling .focus on this.webviewRef.current isn't sufficient.
|
|
|
|
// To allow arrow-key scrolling, focus must also be set within the iframe:
|
|
|
|
result.send('focus');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
hasFocus: () => {
|
|
|
|
return webviewRef.current?.contains(parentDoc.activeElement);
|
|
|
|
},
|
|
|
|
};
|
|
|
|
return result;
|
|
|
|
}, [parentDoc]);
|
|
|
|
|
|
|
|
const webview_domReadyRef = useRef<EventListener>();
|
|
|
|
webview_domReadyRef.current = (event: Event) => {
|
|
|
|
domReadyRef.current = true;
|
|
|
|
if (props.onDomReady) props.onDomReady(event);
|
|
|
|
};
|
|
|
|
|
|
|
|
const webview_ipcMessageRef = useRef<EventListener>();
|
|
|
|
webview_ipcMessageRef.current = (event: Event) => {
|
|
|
|
if (props.onIpcMessage) props.onIpcMessage(event);
|
|
|
|
};
|
|
|
|
|
|
|
|
const webview_loadRef = useRef<EventListener>();
|
|
|
|
webview_loadRef.current = (event: Event) => {
|
|
|
|
webview_domReadyRef.current(event);
|
|
|
|
};
|
|
|
|
|
|
|
|
type MessageEventListener = (event: MessageEvent)=> void;
|
|
|
|
const webview_messageRef = useRef<MessageEventListener>();
|
|
|
|
webview_messageRef.current = (event: MessageEvent) => {
|
|
|
|
if (event.source !== webviewRef.current?.contentWindow) return;
|
2019-12-17 02:45:27 +02:00
|
|
|
if (!event.data || event.data.target !== 'main') return;
|
|
|
|
|
|
|
|
const callName = event.data.name;
|
|
|
|
const args = event.data.args;
|
|
|
|
|
2024-11-08 17:32:05 +02:00
|
|
|
if (props.onIpcMessage) {
|
|
|
|
props.onIpcMessage({
|
2020-03-14 01:57:34 +02:00
|
|
|
channel: callName,
|
|
|
|
args: args,
|
|
|
|
});
|
|
|
|
}
|
2024-11-08 17:32:05 +02:00
|
|
|
};
|
2019-12-17 02:45:27 +02:00
|
|
|
|
2024-11-08 17:32:05 +02:00
|
|
|
useEffect(() => {
|
|
|
|
const wv = webviewRef.current;
|
|
|
|
if (!wv || !containerWindow) return () => {};
|
2019-06-05 18:41:30 +02:00
|
|
|
|
2024-11-08 17:32:05 +02:00
|
|
|
const webviewListeners: Record<string, EventListener> = {
|
|
|
|
'dom-ready': (event) => webview_domReadyRef.current(event),
|
|
|
|
'ipc-message': (event) => webview_ipcMessageRef.current(event),
|
|
|
|
'load': (event) => webview_loadRef.current(event),
|
|
|
|
};
|
2019-02-09 01:07:01 +02:00
|
|
|
|
2024-11-08 17:32:05 +02:00
|
|
|
for (const n in webviewListeners) {
|
|
|
|
if (!webviewListeners.hasOwnProperty(n)) continue;
|
|
|
|
const fn = webviewListeners[n];
|
2019-02-09 01:07:01 +02:00
|
|
|
wv.addEventListener(n, fn);
|
|
|
|
}
|
2019-03-08 19:14:17 +02:00
|
|
|
|
2024-11-08 17:32:05 +02:00
|
|
|
const messageListener: MessageEventListener = event => webview_messageRef.current(event);
|
|
|
|
containerWindow.addEventListener('message', messageListener);
|
2024-07-26 13:22:49 +02:00
|
|
|
|
2024-11-08 17:32:05 +02:00
|
|
|
return () => {
|
|
|
|
domReadyRef.current = false;
|
2021-03-14 17:06:38 +02:00
|
|
|
|
2024-11-08 17:32:05 +02:00
|
|
|
const wv = webviewRef.current;
|
|
|
|
if (!wv) return;
|
2024-09-12 18:54:10 +02:00
|
|
|
|
2024-11-08 17:32:05 +02:00
|
|
|
for (const n in webviewListeners) {
|
|
|
|
if (!webviewListeners.hasOwnProperty(n)) continue;
|
|
|
|
const fn = webviewListeners[n];
|
|
|
|
wv.removeEventListener(n, fn);
|
|
|
|
}
|
2024-07-26 13:22:49 +02:00
|
|
|
|
2024-11-08 17:32:05 +02:00
|
|
|
containerWindow?.removeEventListener('message', messageListener);
|
|
|
|
|
|
|
|
removePluginAssetsCallbackRef.current?.();
|
|
|
|
};
|
|
|
|
}, [containerWindow]);
|
|
|
|
|
|
|
|
const viewerStyle = useMemo(() => {
|
|
|
|
return { border: 'none', ...props.viewerStyle };
|
|
|
|
}, [props.viewerStyle]);
|
|
|
|
|
|
|
|
// allow=fullscreen: Required to allow the user to fullscreen videos.
|
|
|
|
return (
|
|
|
|
<iframe
|
|
|
|
className="noteTextViewer"
|
|
|
|
ref={setWebview}
|
|
|
|
style={viewerStyle}
|
|
|
|
allow='clipboard-write=(self) fullscreen=(self) autoplay=(self) local-fonts=(self) encrypted-media=(self)'
|
|
|
|
allowFullScreen={true}
|
2024-11-09 14:50:06 +02:00
|
|
|
aria-label={_('Note editor')}
|
2024-11-08 17:32:05 +02:00
|
|
|
src={`joplin-content://note-viewer/${__dirname}/note-viewer/index.html`}
|
|
|
|
></iframe>
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
export default NoteTextViewer;
|