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-04-01 16:34:22 +02:00
|
|
|
import { focus } from '@joplin/lib/utils/focusHandler';
|
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-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2022-11-14 18:48:41 +02:00
|
|
|
export default class NoteTextViewerComponent extends React.Component<Props, any> {
|
2020-09-29 09:40:14 +02:00
|
|
|
|
2023-06-30 10:07:03 +02:00
|
|
|
private initialized_ = false;
|
|
|
|
private domReady_ = false;
|
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:13:28 +02:00
|
|
|
private webviewRef_: any;
|
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:13:28 +02:00
|
|
|
private webviewListeners_: any = null;
|
2020-09-29 09:40:14 +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
|
2023-03-06 16:22:01 +02:00
|
|
|
public constructor(props: any) {
|
2020-09-29 09:40:14 +02:00
|
|
|
super(props);
|
2019-02-09 01:07:01 +02:00
|
|
|
|
|
|
|
this.webviewRef_ = React.createRef();
|
|
|
|
|
2021-01-12 01:33:10 +02:00
|
|
|
PostMessageService.instance().registerResponder(ResponderComponentType.NoteTextViewer, '', (message: MessageResponse) => {
|
|
|
|
if (!this.webviewRef_?.current?.contentWindow) {
|
|
|
|
reg.logger().warn('Cannot respond to message because target is gone', message);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.webviewRef_.current.contentWindow.postMessage({
|
|
|
|
target: 'webview',
|
|
|
|
name: 'postMessageService.response',
|
|
|
|
data: message,
|
|
|
|
}, '*');
|
|
|
|
});
|
|
|
|
|
2019-02-09 01:07:01 +02:00
|
|
|
this.webview_domReady = this.webview_domReady.bind(this);
|
|
|
|
this.webview_ipcMessage = this.webview_ipcMessage.bind(this);
|
2019-12-17 02:45:27 +02:00
|
|
|
this.webview_load = this.webview_load.bind(this);
|
|
|
|
this.webview_message = this.webview_message.bind(this);
|
2019-02-09 01:07:01 +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
|
2023-03-06 16:22:01 +02:00
|
|
|
private webview_domReady(event: any) {
|
2019-06-05 18:41:30 +02:00
|
|
|
this.domReady_ = true;
|
2019-05-06 22:35:29 +02:00
|
|
|
if (this.props.onDomReady) this.props.onDomReady(event);
|
2019-02-09 01:07:01 +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
|
2023-03-06 16:22:01 +02:00
|
|
|
private webview_ipcMessage(event: any) {
|
2019-05-06 22:35:29 +02:00
|
|
|
if (this.props.onIpcMessage) this.props.onIpcMessage(event);
|
2019-02-09 01:07:01 +02:00
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
private webview_load() {
|
2020-09-29 09:40:14 +02:00
|
|
|
this.webview_domReady({});
|
2019-12-17 02:45:27 +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
|
2023-03-06 16:22:01 +02:00
|
|
|
private webview_message(event: any) {
|
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;
|
|
|
|
|
2020-03-14 01:57:34 +02:00
|
|
|
if (this.props.onIpcMessage) {
|
|
|
|
this.props.onIpcMessage({
|
|
|
|
channel: callName,
|
|
|
|
args: args,
|
|
|
|
});
|
|
|
|
}
|
2019-12-17 02:45:27 +02:00
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public domReady() {
|
2019-06-05 18:41:30 +02:00
|
|
|
return this.domReady_;
|
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public initWebview() {
|
2019-02-09 01:07:01 +02:00
|
|
|
const wv = this.webviewRef_.current;
|
|
|
|
|
|
|
|
if (!this.webviewListeners_) {
|
|
|
|
this.webviewListeners_ = {
|
|
|
|
'dom-ready': this.webview_domReady.bind(this),
|
|
|
|
'ipc-message': this.webview_ipcMessage.bind(this),
|
2019-12-17 02:45:27 +02:00
|
|
|
'load': this.webview_load.bind(this),
|
2019-02-09 01:07:01 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-03-14 01:46:14 +02:00
|
|
|
for (const n in this.webviewListeners_) {
|
2019-02-09 01:07:01 +02:00
|
|
|
if (!this.webviewListeners_.hasOwnProperty(n)) continue;
|
|
|
|
const fn = this.webviewListeners_[n];
|
|
|
|
wv.addEventListener(n, fn);
|
|
|
|
}
|
2019-03-08 19:14:17 +02:00
|
|
|
|
2019-12-17 02:45:27 +02:00
|
|
|
this.webviewRef_.current.contentWindow.addEventListener('message', this.webview_message);
|
2019-02-09 01:07:01 +02:00
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public destroyWebview() {
|
2019-02-09 01:07:01 +02:00
|
|
|
const wv = this.webviewRef_.current;
|
|
|
|
if (!wv || !this.initialized_) return;
|
|
|
|
|
2020-03-14 01:46:14 +02:00
|
|
|
for (const n in this.webviewListeners_) {
|
2019-02-09 01:07:01 +02:00
|
|
|
if (!this.webviewListeners_.hasOwnProperty(n)) continue;
|
|
|
|
const fn = this.webviewListeners_[n];
|
|
|
|
wv.removeEventListener(n, fn);
|
|
|
|
}
|
2019-06-05 18:41:30 +02:00
|
|
|
|
2020-09-29 09:40:14 +02:00
|
|
|
try {
|
|
|
|
// It seems this can throw a cross-origin error in a way that is hard to replicate so just wrap
|
|
|
|
// it in try/catch since it's not critical.
|
|
|
|
// https://github.com/laurent22/joplin/issues/3835
|
|
|
|
this.webviewRef_.current.contentWindow.removeEventListener('message', this.webview_message);
|
|
|
|
} catch (error) {
|
|
|
|
reg.logger().warn('Error destroying note viewer', error);
|
|
|
|
}
|
2019-12-17 02:45:27 +02:00
|
|
|
|
2019-06-05 18:41:30 +02:00
|
|
|
this.initialized_ = false;
|
|
|
|
this.domReady_ = false;
|
2019-02-09 01:07:01 +02:00
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public focus() {
|
2021-03-14 17:06:38 +02:00
|
|
|
if (this.webviewRef_.current) {
|
2024-04-01 16:34:22 +02:00
|
|
|
focus('NoteTextViewer::focus', this.webviewRef_.current);
|
2021-03-14 17:06:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public tryInit() {
|
2019-02-09 01:07:01 +02:00
|
|
|
if (!this.initialized_ && this.webviewRef_.current) {
|
|
|
|
this.initWebview();
|
|
|
|
this.initialized_ = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public componentDidMount() {
|
2019-05-06 22:35:29 +02:00
|
|
|
this.tryInit();
|
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public componentDidUpdate() {
|
2019-05-06 22:35:29 +02:00
|
|
|
this.tryInit();
|
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public componentWillUnmount() {
|
2019-02-09 01:07:01 +02:00
|
|
|
this.destroyWebview();
|
|
|
|
}
|
|
|
|
|
2019-02-10 19:16:11 +02:00
|
|
|
// ----------------------------------------------------------------
|
|
|
|
// Wrap WebView functions
|
|
|
|
// ----------------------------------------------------------------
|
|
|
|
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2023-03-06 16:22:01 +02:00
|
|
|
public send(channel: string, arg0: any = null, arg1: any = null) {
|
2019-12-17 02:45:27 +02:00
|
|
|
const win = this.webviewRef_.current.contentWindow;
|
|
|
|
|
2021-10-30 19:00:01 +02:00
|
|
|
if (channel === 'focus') {
|
|
|
|
win.postMessage({ target: 'webview', name: 'focus', data: {} }, '*');
|
|
|
|
}
|
|
|
|
|
2019-12-17 02:45:27 +02:00
|
|
|
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 } }, '*');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (channel === 'setPercentScroll') {
|
|
|
|
win.postMessage({ target: 'webview', name: 'setPercentScroll', data: { percent: arg0 } }, '*');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (channel === 'setMarkers') {
|
|
|
|
win.postMessage({ target: 'webview', name: 'setMarkers', data: { keywords: arg0, options: arg1 } }, '*');
|
|
|
|
}
|
2019-02-09 01:07:01 +02:00
|
|
|
}
|
|
|
|
|
2019-02-10 19:16:11 +02:00
|
|
|
// ----------------------------------------------------------------
|
|
|
|
// Wrap WebView functions (END)
|
|
|
|
// ----------------------------------------------------------------
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public render() {
|
2023-06-01 13:02:36 +02:00
|
|
|
const viewerStyle = { border: 'none', ...this.props.viewerStyle };
|
2019-12-17 02:45:27 +02:00
|
|
|
return <iframe className="noteTextViewer" ref={this.webviewRef_} style={viewerStyle} src="gui/note-viewer/index.html"></iframe>;
|
2019-02-09 01:07:01 +02:00
|
|
|
}
|
|
|
|
}
|