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

185 lines
5 KiB
React
Raw Permalink Normal View History

2018-06-11 08:41:25 +02:00
// @flow
import { remote } from 'electron';
import React from 'react';
import { lazyImport } from 'util/lazyImport';
2020-01-06 19:32:35 +01:00
import classnames from 'classnames';
import * as RENDER_MODES from 'constants/file_render_modes';
2021-08-31 09:05:42 +02:00
import * as KEYCODES from 'constants/keycodes';
import VideoViewer from 'component/viewers/videoViewer';
2019-09-06 02:26:03 +02:00
import { withRouter } from 'react-router-dom';
2019-08-02 08:28:14 +02:00
import fs from 'fs';
import analytics from 'analytics';
2019-04-03 07:56:58 +02:00
2019-11-07 20:39:22 +01:00
import DocumentViewer from 'component/viewers/documentViewer';
// @if TARGET='app'
// should match
import DocxViewer from 'component/viewers/docxViewer';
2019-11-07 20:39:22 +01:00
import ComicBookViewer from 'component/viewers/comicBookViewer';
import ThreeViewer from 'component/viewers/threeViewer';
2019-03-08 20:20:17 +01:00
// @endif
2018-06-11 08:41:25 +02:00
const AppViewer = lazyImport(() => import('component/viewers/appViewer' /* webpackChunkName: "appViewer" */));
const HtmlViewer = lazyImport(() => import('component/viewers/htmlViewer' /* webpackChunkName: "htmlViewer" */));
const ImageViewer = lazyImport(() => import('component/viewers/imageViewer' /* webpackChunkName: "imageViewer" */));
const PdfViewer = lazyImport(() => import('component/viewers/pdfViewer' /* webpackChunkName: "pdfViewer" */));
2021-06-11 08:06:29 +02:00
2018-06-11 08:41:25 +02:00
type Props = {
2019-08-06 05:25:33 +02:00
uri: string,
2019-08-02 08:28:14 +02:00
streamingUrl: string,
2020-01-27 22:37:54 +01:00
embedded?: boolean,
2019-08-02 08:28:14 +02:00
contentType: string,
2019-04-24 16:02:08 +02:00
claim: StreamClaim,
2018-06-11 08:41:25 +02:00
currentTheme: string,
2019-08-06 05:25:33 +02:00
downloadPath: string,
fileExtension: string,
2020-01-27 20:32:20 +01:00
autoplay: boolean,
renderMode: string,
2020-01-31 16:46:50 +01:00
thumbnail: string,
desktopPlayStartTime?: number,
2020-07-22 22:56:58 +02:00
className?: string,
2020-01-27 19:52:25 +01:00
};
2020-04-29 21:31:11 +02:00
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() {
const { embedded } = this.props;
window.addEventListener('keydown', this.escapeListener, true);
analytics.playerLoadedEvent(embedded);
}
componentWillUnmount() {
window.removeEventListener('keydown', this.escapeListener, true);
}
2019-01-19 19:54:06 +01:00
escapeListener(e: SyntheticKeyboardEvent<*>) {
2021-08-31 09:05:42 +02:00
if (e.keyCode === KEYCODES.ESCAPE) {
e.preventDefault();
this.exitFullscreen();
return false;
}
}
exitFullscreen() {
remote.getCurrentWindow().setFullScreen(false);
}
renderViewer() {
const {
currentTheme,
contentType,
downloadPath,
fileExtension,
streamingUrl,
uri,
renderMode,
desktopPlayStartTime,
} = this.props;
const source = streamingUrl;
switch (renderMode) {
case RENDER_MODES.AUDIO:
case RENDER_MODES.VIDEO:
return (
<VideoViewer
uri={uri}
source={source}
contentType={contentType}
desktopPlayStartTime={desktopPlayStartTime}
/>
);
case RENDER_MODES.IMAGE:
2021-06-11 08:06:29 +02:00
return (
<React.Suspense fallback={null}>
2021-07-09 15:27:10 +02:00
<ImageViewer uri={uri} source={source} />
2021-06-11 08:06:29 +02:00
</React.Suspense>
);
case RENDER_MODES.HTML:
2021-06-11 08:06:29 +02:00
return (
<React.Suspense fallback={null}>
2021-07-09 15:27:10 +02:00
<HtmlViewer source={downloadPath || source} />
2021-06-11 08:06:29 +02:00
</React.Suspense>
);
case RENDER_MODES.DOCUMENT:
case RENDER_MODES.MARKDOWN:
return (
<DocumentViewer
source={{
// @if TARGET='app'
file: (options) => fs.createReadStream(downloadPath, options),
// @endif
stream: source,
fileExtension,
contentType,
}}
renderMode={renderMode}
theme={currentTheme}
/>
);
case RENDER_MODES.DOCX:
return <DocxViewer source={downloadPath} />;
case RENDER_MODES.PDF:
2021-06-11 08:06:29 +02:00
return (
<React.Suspense fallback={null}>
2021-07-09 15:27:10 +02:00
<PdfViewer source={downloadPath || source} />
2021-06-11 08:06:29 +02:00
</React.Suspense>
);
case RENDER_MODES.CAD:
return (
<ThreeViewer
source={{
2020-04-16 21:57:03 +02:00
fileType: fileExtension,
downloadPath,
}}
theme={currentTheme}
/>
);
case RENDER_MODES.COMIC:
2020-05-08 20:45:48 +02:00
return (
<ComicBookViewer
source={{
// @if TARGET='app'
file: (options) => fs.createReadStream(downloadPath, options),
2020-05-08 20:45:48 +02:00
// @endif
2020-07-22 22:56:58 +02:00
stream: source,
2020-05-08 20:45:48 +02:00
}}
theme={currentTheme}
2020-07-22 22:56:58 +02:00
/>
2020-05-08 20:45:48 +02:00
);
case RENDER_MODES.APPLICATION:
2021-06-11 08:06:29 +02:00
return (
<React.Suspense fallback={null}>
2021-07-09 15:27:10 +02:00
<AppViewer uri={uri} />
2021-06-11 08:06:29 +02:00
</React.Suspense>
);
}
return null;
2018-06-11 08:41:25 +02:00
}
2018-06-11 08:41:25 +02:00
render() {
2020-07-22 22:56:58 +02:00
const { embedded, renderMode, className } = this.props;
2020-01-06 19:32:35 +01:00
2019-03-27 05:40:02 +01:00
return (
2020-01-27 22:37:54 +01:00
<div
2020-07-22 22:56:58 +02:00
className={classnames('file-render', className, {
'file-render--document': RENDER_MODES.TEXT_MODES.includes(renderMode) && !embedded,
2020-04-14 01:48:11 +02:00
'file-render--embed': embedded,
'file-render--video': renderMode === RENDER_MODES.VIDEO || renderMode === RENDER_MODES.AUDIO,
2020-01-27 22:37:54 +01:00
})}
>
{this.renderViewer()}
2019-03-27 05:40:02 +01:00
</div>
);
2018-06-11 08:41:25 +02:00
}
}
2019-09-06 02:26:03 +02:00
export default withRouter(FileRender);