2019-02-09 01:07:01 +02:00
|
|
|
const React = require('react');
|
|
|
|
const { connect } = require('react-redux');
|
|
|
|
|
|
|
|
class NoteTextViewerComponent extends React.Component {
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
|
|
|
|
this.initialized_ = false;
|
2019-06-05 18:41:30 +02:00
|
|
|
this.domReady_ = false;
|
2019-02-09 01:07:01 +02:00
|
|
|
|
|
|
|
this.webviewRef_ = React.createRef();
|
|
|
|
this.webviewListeners_ = null;
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
webview_domReady(event) {
|
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
|
|
|
}
|
|
|
|
|
|
|
|
webview_ipcMessage(event) {
|
2019-05-06 22:35:29 +02:00
|
|
|
if (this.props.onIpcMessage) this.props.onIpcMessage(event);
|
2019-02-09 01:07:01 +02:00
|
|
|
}
|
|
|
|
|
2019-12-17 02:45:27 +02:00
|
|
|
webview_load() {
|
|
|
|
this.webview_domReady();
|
|
|
|
}
|
|
|
|
|
|
|
|
webview_message(event) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-06-05 18:41:30 +02:00
|
|
|
domReady() {
|
|
|
|
return this.domReady_;
|
|
|
|
}
|
|
|
|
|
2019-02-09 01:07:01 +02:00
|
|
|
initWebview() {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
destroyWebview() {
|
|
|
|
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
|
|
|
|
2019-12-17 02:45:27 +02:00
|
|
|
this.webviewRef_.current.contentWindow.removeEventListener('message', this.webview_message);
|
|
|
|
|
2019-06-05 18:41:30 +02:00
|
|
|
this.initialized_ = false;
|
|
|
|
this.domReady_ = false;
|
2019-02-09 01:07:01 +02:00
|
|
|
}
|
|
|
|
|
2019-05-06 22:35:29 +02:00
|
|
|
tryInit() {
|
2019-02-09 01:07:01 +02:00
|
|
|
if (!this.initialized_ && this.webviewRef_.current) {
|
|
|
|
this.initWebview();
|
|
|
|
this.initialized_ = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-06 22:35:29 +02:00
|
|
|
componentDidMount() {
|
|
|
|
this.tryInit();
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidUpdate() {
|
|
|
|
this.tryInit();
|
|
|
|
}
|
|
|
|
|
2019-02-09 01:07:01 +02:00
|
|
|
componentWillUnmount() {
|
|
|
|
this.destroyWebview();
|
|
|
|
}
|
|
|
|
|
2019-02-10 19:16:11 +02:00
|
|
|
// ----------------------------------------------------------------
|
|
|
|
// Wrap WebView functions
|
|
|
|
// ----------------------------------------------------------------
|
|
|
|
|
2019-12-17 02:45:27 +02:00
|
|
|
send(channel, arg0 = null, arg1 = null) {
|
|
|
|
const win = this.webviewRef_.current.contentWindow;
|
|
|
|
|
|
|
|
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-12-17 02:45:27 +02:00
|
|
|
printToPDF() { // options, callback) {
|
2019-02-28 00:35:26 +02:00
|
|
|
// In Electron 4x, printToPDF is broken so need to use this hack:
|
|
|
|
// https://github.com/electron/electron/issues/16171#issuecomment-451090245
|
|
|
|
|
|
|
|
// return this.webviewRef_.current.printToPDF(options, callback);
|
2019-12-17 02:45:27 +02:00
|
|
|
// return this.webviewRef_.current.getWebContents().printToPDF(options, callback);
|
2019-02-09 01:07:01 +02:00
|
|
|
}
|
|
|
|
|
2019-07-29 14:13:23 +02:00
|
|
|
print() {
|
2019-02-28 00:35:26 +02:00
|
|
|
// In Electron 4x, print is broken so need to use this hack:
|
|
|
|
// https://github.com/electron/electron/issues/16219#issuecomment-451454948
|
|
|
|
// Note that this is not a perfect workaround since it means the options are ignored
|
|
|
|
// In particular it means that background images and colours won't be printed (printBackground property will be ignored)
|
|
|
|
|
|
|
|
// return this.webviewRef_.current.getWebContents().print({});
|
2019-07-29 14:13:23 +02:00
|
|
|
return this.webviewRef_.current.getWebContents().executeJavaScript('window.print()');
|
2019-02-09 01:07:01 +02:00
|
|
|
}
|
|
|
|
|
2019-02-10 19:16:11 +02:00
|
|
|
openDevTools() {
|
2019-12-17 02:45:27 +02:00
|
|
|
// return this.webviewRef_.current.openDevTools();
|
2019-02-10 19:16:11 +02:00
|
|
|
}
|
|
|
|
|
2019-06-05 18:41:30 +02:00
|
|
|
closeDevTools() {
|
2019-12-17 02:45:27 +02:00
|
|
|
// return this.webviewRef_.current.closeDevTools();
|
2019-06-05 18:41:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
isDevToolsOpened() {
|
2019-12-17 02:45:27 +02:00
|
|
|
// return this.webviewRef_.current.isDevToolsOpened();
|
2019-06-05 18:41:30 +02:00
|
|
|
}
|
|
|
|
|
2019-02-10 19:16:11 +02:00
|
|
|
// ----------------------------------------------------------------
|
|
|
|
// Wrap WebView functions (END)
|
|
|
|
// ----------------------------------------------------------------
|
|
|
|
|
2019-02-09 01:07:01 +02:00
|
|
|
render() {
|
2020-05-02 17:41:07 +02:00
|
|
|
const viewerStyle = Object.assign({}, { 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-29 14:13:23 +02:00
|
|
|
const mapStateToProps = state => {
|
2019-02-09 01:07:01 +02:00
|
|
|
return {
|
|
|
|
theme: state.settings.theme,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2019-07-29 14:13:23 +02:00
|
|
|
const NoteTextViewer = connect(
|
|
|
|
mapStateToProps,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
{ withRef: true }
|
|
|
|
)(NoteTextViewerComponent);
|
2019-02-09 01:07:01 +02:00
|
|
|
|
2019-07-29 14:13:23 +02:00
|
|
|
module.exports = NoteTextViewer;
|