2019-05-09 00:47:39 +02:00
|
|
|
// @flow
|
|
|
|
import * as React from 'react';
|
2019-11-25 06:17:38 +01:00
|
|
|
import Villain from 'villain-react';
|
2020-05-08 20:50:18 +02:00
|
|
|
import LoadingScreen from 'component/common/loading-screen';
|
2020-05-08 21:09:36 +02:00
|
|
|
|
2020-05-09 03:40:52 +02:00
|
|
|
import useFileStream from 'effects/use-stream-file';
|
2020-05-08 21:09:36 +02:00
|
|
|
|
|
|
|
// Import default styles for Villain
|
2019-11-25 06:17:38 +01:00
|
|
|
import 'villain-react/dist/style.css';
|
2019-05-09 00:47:39 +02:00
|
|
|
|
|
|
|
type Props = {
|
2020-05-08 20:50:18 +02:00
|
|
|
source: {
|
|
|
|
file: (?string) => any,
|
|
|
|
stream: string,
|
|
|
|
},
|
2019-11-25 06:17:38 +01:00
|
|
|
theme: string,
|
2019-05-09 00:47:39 +02:00
|
|
|
};
|
|
|
|
|
2019-11-25 06:17:38 +01:00
|
|
|
let workerUrl = 'webworkers/worker-bundle.js';
|
|
|
|
|
2019-06-04 20:32:42 +02:00
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
// Don't add a leading slash in production because electron treats it as an absolute path
|
2019-11-25 06:17:38 +01:00
|
|
|
workerUrl = `/${workerUrl}`;
|
2019-06-04 20:32:42 +02:00
|
|
|
}
|
|
|
|
|
2020-05-09 03:40:52 +02:00
|
|
|
const ComicBookViewer = (props: Props) => {
|
|
|
|
const { source, theme } = props;
|
|
|
|
let finalSource;
|
2020-05-08 20:50:18 +02:00
|
|
|
|
2020-05-09 03:40:52 +02:00
|
|
|
finalSource = useFileStream(source.file);
|
2020-05-08 20:50:18 +02:00
|
|
|
|
2020-05-09 03:40:52 +02:00
|
|
|
// Villain options
|
|
|
|
const opts = {
|
|
|
|
theme: theme === 'dark' ? 'Dark' : 'Light',
|
|
|
|
allowFullScreen: true,
|
|
|
|
autoHideControls: false,
|
|
|
|
allowGlobalShortcuts: true,
|
|
|
|
};
|
2020-05-08 21:09:36 +02:00
|
|
|
|
2020-05-09 03:40:52 +02:00
|
|
|
const { error, loading, content } = finalSource;
|
2020-05-09 05:24:02 +02:00
|
|
|
const ready = content !== null && !loading && !error;
|
2020-05-09 03:40:52 +02:00
|
|
|
const errorMessage = __("Sorry, looks like we can't load the archive.");
|
2019-05-16 08:32:53 +02:00
|
|
|
|
2020-05-09 03:40:52 +02:00
|
|
|
return (
|
|
|
|
<div className="file-render__viewer file-render__viewer--comic">
|
2020-05-09 05:24:02 +02:00
|
|
|
{loading && <LoadingScreen status={__('Loading')} isDocument />}
|
2020-05-09 03:40:52 +02:00
|
|
|
{ready && (
|
|
|
|
<Villain source={finalSource.content} className={'comic-viewer'} options={opts} workerUrl={workerUrl} />
|
|
|
|
)}
|
|
|
|
{error && <LoadingScreen status={errorMessage} spinner={false} />}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
2019-05-09 00:47:39 +02:00
|
|
|
|
|
|
|
export default ComicBookViewer;
|