lbry-desktop/src/ui/component/fileRender/view.jsx

164 lines
4.6 KiB
React
Raw Normal View History

2018-06-11 08:41:25 +02:00
// @flow
import { remote } from 'electron';
2018-06-11 08:41:25 +02:00
import React from 'react';
import LoadingScreen from 'component/common/loading-screen';
import PdfViewer from 'component/viewers/pdfViewer';
import DocumentViewer from 'component/viewers/documentViewer';
2018-07-27 04:06:39 +02:00
import DocxViewer from 'component/viewers/docxViewer';
import HtmlViewer from 'component/viewers/htmlViewer';
2019-02-22 06:01:59 +01:00
import AudioVideoViewer from 'component/viewers/audioVideoViewer';
2019-03-08 20:20:17 +01:00
// @if TARGET='app'
import ThreeViewer from 'component/viewers/threeViewer';
// @endif
2018-06-11 08:41:25 +02:00
type Props = {
mediaType: string,
2019-02-22 06:01:59 +01:00
poster?: string,
source: {
stream: string => void,
fileName: string,
2018-06-11 08:41:25 +02:00
fileType: string,
contentType: string,
downloadPath: string,
2019-01-19 19:54:06 +01:00
url: ?string,
2018-06-11 08:41:25 +02:00
},
currentTheme: string,
};
class FileRender extends React.PureComponent<Props> {
2019-01-19 19:54:06 +01:00
constructor(props: Props) {
super(props);
2019-01-19 19:54:06 +01:00
(this: any).escapeListener = this.escapeListener.bind(this);
}
componentDidMount() {
window.addEventListener('keydown', this.escapeListener, true);
}
componentWillUnmount() {
window.removeEventListener('keydown', this.escapeListener, true);
}
2019-01-19 19:54:06 +01:00
// This should use React.createRef()
processSandboxRef(element: any) {
if (!element) {
return;
}
window.sandbox = element;
element.addEventListener('permissionrequest', e => {
// eslint-disable-next-line no-console
console.log('permissionrequest', e);
});
2019-01-19 19:54:06 +01:00
element.addEventListener('console-message', (e: { message: string }) => {
if (/^\$LBRY_IPC:/.test(e.message)) {
// Process command
let message = {};
try {
2019-02-22 06:01:59 +01:00
// $FlowFixMe
message = JSON.parse(/^\$LBRY_IPC:(.*)/.exec(e.message)[1]);
} catch (err) {}
// eslint-disable-next-line no-console
console.log('IPC', message);
} else {
// eslint-disable-next-line no-console
console.log('Sandbox:', e.message);
}
});
element.addEventListener('enter-html-full-screen', () => {
// stub
});
element.addEventListener('leave-html-full-screen', () => {
// stub
});
}
2019-01-19 19:54:06 +01:00
escapeListener(e: SyntheticKeyboardEvent<*>) {
if (e.keyCode === 27) {
e.preventDefault();
this.exitFullscreen();
return false;
}
}
exitFullscreen() {
remote.getCurrentWindow().setFullScreen(false);
}
renderViewer() {
2019-02-22 06:01:59 +01:00
const { source, mediaType, currentTheme, poster } = this.props;
// Extract relevant data to render file
2019-02-22 06:01:59 +01:00
const { stream, fileType, contentType, downloadPath, fileName } = source;
2018-06-11 08:41:25 +02:00
// Human-readable files (scripts and plain-text files)
const readableFiles = ['text', 'document', 'script'];
2018-06-11 08:41:25 +02:00
// Supported mediaTypes
const mediaTypes = {
2019-03-08 20:20:17 +01:00
// @if TARGET='app'
'3D-file': <ThreeViewer source={{ fileType, downloadPath }} theme={currentTheme} />,
2019-03-08 20:20:17 +01:00
// @endif
2019-01-19 19:54:06 +01:00
application: !source.url ? null : (
<webview
ref={element => this.processSandboxRef(element)}
title=""
sandbox="allow-scripts allow-forms allow-pointer-lock"
src={source.url}
autosize="on"
style={{ border: 0, width: '100%', height: '100%' }}
useragent="Mozilla/5.0 AppleWebKit/537 Chrome/60 Safari/537"
enableremotemodule="false"
webpreferences="sandbox=true,contextIsolation=true,webviewTag=false,enableRemoteModule=false,devTools=false"
/>
),
2019-02-22 06:01:59 +01:00
video: (
<AudioVideoViewer
source={{ downloadPath, fileName }}
contentType={contentType}
poster={poster}
/>
),
audio: <AudioVideoViewer source={{ downloadPath, fileName }} contentType={contentType} />,
2018-06-11 08:41:25 +02:00
// Add routes to viewer...
};
// Supported fileType
const fileTypes = {
pdf: <PdfViewer source={downloadPath} />,
docx: <DocxViewer source={downloadPath} />,
html: <HtmlViewer source={downloadPath} />,
// Add routes to viewer...
};
// Check for a valid fileType or mediaType
let viewer = fileTypes[fileType] || mediaTypes[mediaType];
// Check for Human-readable files
if (!viewer && readableFiles.includes(mediaType)) {
viewer = <DocumentViewer source={{ stream, fileType, contentType }} theme={currentTheme} />;
}
// Message Error
2018-07-09 06:57:31 +02:00
const unsupportedMessage = __("Sorry, looks like we can't preview this file.");
const unsupported = <LoadingScreen status={unsupportedMessage} spinner={false} />;
2018-06-11 08:41:25 +02:00
// Return viewer
return viewer || unsupported;
2018-06-11 08:41:25 +02:00
}
render() {
return <div className="file-render">{this.renderViewer()}</div>;
2018-06-11 08:41:25 +02:00
}
}
export default FileRender;