2019-02-09 01:07:01 +02:00
|
|
|
const React = require('react');
|
|
|
|
const { connect } = require('react-redux');
|
|
|
|
const { themeStyle } = require('../theme.js');
|
|
|
|
const { _ } = require('lib/locale.js');
|
|
|
|
|
|
|
|
class NoteTextViewerComponent extends React.Component {
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
|
|
|
|
this.initialized_ = false;
|
|
|
|
|
|
|
|
this.webviewRef_ = React.createRef();
|
|
|
|
this.webviewListeners_ = null;
|
|
|
|
|
|
|
|
this.webview_domReady = this.webview_domReady.bind(this);
|
|
|
|
this.webview_ipcMessage = this.webview_ipcMessage.bind(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
webview_domReady(event) {
|
|
|
|
this.props.onDomReady(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
webview_ipcMessage(event) {
|
|
|
|
this.props.onIpcMessage(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
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),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let n in this.webviewListeners_) {
|
|
|
|
if (!this.webviewListeners_.hasOwnProperty(n)) continue;
|
|
|
|
const fn = this.webviewListeners_[n];
|
|
|
|
wv.addEventListener(n, fn);
|
|
|
|
}
|
2019-03-08 19:14:17 +02:00
|
|
|
|
|
|
|
let isAlreadyReady = false;
|
|
|
|
try {
|
|
|
|
isAlreadyReady = !this.webviewRef_.current.isLoading()
|
|
|
|
} catch (error) {
|
|
|
|
// Ignore - it means the view has not started loading, and the DOM ready event has not been emitted yet
|
|
|
|
// Error is "The WebView must be attached to the DOM and the dom-ready event emitted before this method can be called."
|
|
|
|
}
|
|
|
|
|
|
|
|
// Edge-case - the webview was already ready by the time initWebview was
|
|
|
|
// called - so manually call the domReady event to notify caller.
|
|
|
|
if (isAlreadyReady) {
|
|
|
|
this.webview_domReady({});
|
|
|
|
}
|
2019-02-09 01:07:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
destroyWebview() {
|
|
|
|
const wv = this.webviewRef_.current;
|
|
|
|
if (!wv || !this.initialized_) return;
|
|
|
|
|
|
|
|
for (let n in this.webviewListeners_) {
|
|
|
|
if (!this.webviewListeners_.hasOwnProperty(n)) continue;
|
|
|
|
const fn = this.webviewListeners_[n];
|
|
|
|
wv.removeEventListener(n, fn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidUpdate() {
|
|
|
|
if (!this.initialized_ && this.webviewRef_.current) {
|
|
|
|
this.initWebview();
|
|
|
|
this.initialized_ = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
this.destroyWebview();
|
|
|
|
}
|
|
|
|
|
2019-02-10 19:16:11 +02:00
|
|
|
// ----------------------------------------------------------------
|
|
|
|
// Wrap WebView functions
|
|
|
|
// ----------------------------------------------------------------
|
|
|
|
|
2019-02-09 01:07:01 +02:00
|
|
|
send(channel, arg0 = null, arg1 = null, arg2 = null, arg3 = null) {
|
|
|
|
return this.webviewRef_.current.send(channel, arg0, arg1, arg2, arg3);
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
return this.webviewRef_.current.getWebContents().printToPDF(options, callback);
|
2019-02-09 01:07:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
print(options = {}) {
|
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-02-28 00:44:10 +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() {
|
|
|
|
return this.webviewRef_.current.openDevTools();
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------
|
|
|
|
// Wrap WebView functions (END)
|
|
|
|
// ----------------------------------------------------------------
|
|
|
|
|
2019-02-09 01:07:01 +02:00
|
|
|
render() {
|
|
|
|
return <webview
|
|
|
|
ref={this.webviewRef_}
|
|
|
|
style={this.props.viewerStyle}
|
|
|
|
preload="gui/note-viewer/preload.js"
|
|
|
|
src="gui/note-viewer/index.html"
|
|
|
|
webpreferences="contextIsolation"
|
|
|
|
/>
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
const mapStateToProps = (state) => {
|
|
|
|
return {
|
|
|
|
theme: state.settings.theme,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
const NoteTextViewer = connect(mapStateToProps, null, null, { withRef: true })(NoteTextViewerComponent);
|
|
|
|
|
|
|
|
module.exports = NoteTextViewer;
|