2018-03-26 23:32:43 +02:00
|
|
|
// @flow
|
2019-08-13 07:35:13 +02:00
|
|
|
import * as ICONS from 'constants/icons';
|
2020-04-01 20:43:50 +02:00
|
|
|
import * as RENDER_MODES from 'constants/file_render_modes';
|
2020-05-01 18:41:32 +02:00
|
|
|
import React, { useEffect, useState } from 'react';
|
2019-08-13 07:35:13 +02:00
|
|
|
import Button from 'component/button';
|
2018-03-26 23:32:43 +02:00
|
|
|
import classnames from 'classnames';
|
2018-07-05 04:49:12 +02:00
|
|
|
import LoadingScreen from 'component/common/loading-screen';
|
2019-08-02 08:28:14 +02:00
|
|
|
import FileRender from 'component/fileRender';
|
2019-08-13 07:35:13 +02:00
|
|
|
import UriIndicator from 'component/uriIndicator';
|
2019-09-27 20:56:15 +02:00
|
|
|
import usePersistedState from 'effects/use-persisted-state';
|
2020-04-01 20:43:50 +02:00
|
|
|
import { FILE_WRAPPER_CLASS } from 'page/file/view';
|
2019-08-13 07:35:13 +02:00
|
|
|
import Draggable from 'react-draggable';
|
|
|
|
import Tooltip from 'component/common/tooltip';
|
2019-10-14 00:28:12 +02:00
|
|
|
import { onFullscreenChange } from 'util/full-screen';
|
2020-08-10 22:47:39 +02:00
|
|
|
import { useIsMobile } from 'effects/use-screensize';
|
2018-05-16 21:34:38 +02:00
|
|
|
|
2018-03-26 23:32:43 +02:00
|
|
|
type Props = {
|
2020-04-14 01:48:11 +02:00
|
|
|
isFloating: boolean,
|
2019-08-06 05:25:33 +02:00
|
|
|
fileInfo: FileListItem,
|
2018-03-26 23:32:43 +02:00
|
|
|
uri: string,
|
2019-08-02 08:28:14 +02:00
|
|
|
streamingUrl?: string,
|
2019-08-13 07:35:13 +02:00
|
|
|
title: ?string,
|
|
|
|
floatingPlayerEnabled: boolean,
|
2020-04-29 22:50:06 +02:00
|
|
|
closeFloatingPlayer: () => void,
|
2020-04-01 20:43:50 +02:00
|
|
|
renderMode: string,
|
2018-03-26 23:32:43 +02:00
|
|
|
};
|
2017-04-23 11:56:50 +02:00
|
|
|
|
2020-04-14 01:48:11 +02:00
|
|
|
export default function FileRenderFloating(props: Props) {
|
2020-04-29 22:50:06 +02:00
|
|
|
const {
|
|
|
|
fileInfo,
|
|
|
|
uri,
|
|
|
|
streamingUrl,
|
|
|
|
title,
|
|
|
|
isFloating,
|
|
|
|
closeFloatingPlayer,
|
|
|
|
floatingPlayerEnabled,
|
|
|
|
renderMode,
|
|
|
|
} = props;
|
2020-05-21 17:38:28 +02:00
|
|
|
|
2019-12-18 06:27:08 +01:00
|
|
|
const isMobile = useIsMobile();
|
2020-05-01 18:41:32 +02:00
|
|
|
const [fileViewerRect, setFileViewerRect] = useState();
|
2020-05-05 20:02:12 +02:00
|
|
|
const [desktopPlayStartTime, setDesktopPlayStartTime] = useState();
|
2020-08-27 08:02:43 +02:00
|
|
|
const [wasDragging, setWasDragging] = useState(false);
|
2019-08-13 07:35:13 +02:00
|
|
|
const [position, setPosition] = usePersistedState('floating-file-viewer:position', {
|
|
|
|
x: -25,
|
|
|
|
y: window.innerHeight - 400,
|
|
|
|
});
|
2020-04-14 01:48:11 +02:00
|
|
|
|
2020-04-01 20:43:50 +02:00
|
|
|
const isPlayable = RENDER_MODES.FLOATING_MODES.includes(renderMode);
|
|
|
|
const isReadyToPlay = isPlayable && (streamingUrl || (fileInfo && fileInfo.completed));
|
2019-08-02 08:28:14 +02:00
|
|
|
const loadingMessage =
|
2020-04-01 20:43:50 +02:00
|
|
|
fileInfo && fileInfo.blobs_completed >= 1 && (!fileInfo.download_path || !fileInfo.written_bytes)
|
2019-08-02 08:28:14 +02:00
|
|
|
? __("It looks like you deleted or moved this file. We're rebuilding it now. It will only take a few seconds.")
|
|
|
|
: __('Loading');
|
2019-08-14 05:04:08 +02:00
|
|
|
|
2019-08-02 08:28:14 +02:00
|
|
|
useEffect(() => {
|
2019-08-13 07:35:13 +02:00
|
|
|
function handleResize() {
|
|
|
|
const element = document.querySelector(`.${FILE_WRAPPER_CLASS}`);
|
|
|
|
if (!element) {
|
2019-08-17 16:32:24 +02:00
|
|
|
return;
|
2018-08-03 17:54:10 +02:00
|
|
|
}
|
2019-08-13 07:35:13 +02:00
|
|
|
|
|
|
|
const rect = element.getBoundingClientRect();
|
2020-05-01 18:41:32 +02:00
|
|
|
// $FlowFixMe
|
2019-08-13 07:35:13 +02:00
|
|
|
setFileViewerRect(rect);
|
|
|
|
}
|
|
|
|
|
2019-12-31 04:09:20 +01:00
|
|
|
handleResize();
|
|
|
|
window.addEventListener('resize', handleResize);
|
|
|
|
onFullscreenChange(window, 'add', handleResize);
|
2019-12-19 21:43:49 +01:00
|
|
|
return () => {
|
|
|
|
window.removeEventListener('resize', handleResize);
|
|
|
|
onFullscreenChange(window, 'remove', handleResize);
|
|
|
|
};
|
2020-04-14 01:48:11 +02:00
|
|
|
}, [setFileViewerRect, isFloating]);
|
2020-04-01 20:43:50 +02:00
|
|
|
|
2020-05-05 20:02:12 +02:00
|
|
|
useEffect(() => {
|
|
|
|
// @if TARGET='app'
|
|
|
|
setDesktopPlayStartTime(Date.now());
|
|
|
|
// @endif
|
2020-05-05 22:45:59 +02:00
|
|
|
|
|
|
|
return () => {
|
|
|
|
// @if TARGET='app'
|
|
|
|
setDesktopPlayStartTime(undefined);
|
|
|
|
// @endif
|
|
|
|
};
|
2020-05-05 20:02:12 +02:00
|
|
|
}, [uri]);
|
|
|
|
|
2020-04-16 23:43:09 +02:00
|
|
|
if (!isPlayable || !uri || (isFloating && (isMobile || !floatingPlayerEnabled))) {
|
2020-04-01 20:43:50 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-09-09 04:14:51 +02:00
|
|
|
function handleDragStart(e, ui) {
|
2020-08-27 08:02:43 +02:00
|
|
|
// Not really necessary, but reset just in case 'handleStop' didn't fire.
|
|
|
|
setWasDragging(false);
|
|
|
|
}
|
|
|
|
|
2020-09-09 04:14:51 +02:00
|
|
|
function handleDragMove(e, ui) {
|
2020-08-27 08:02:43 +02:00
|
|
|
setWasDragging(true);
|
|
|
|
|
2019-08-14 05:04:08 +02:00
|
|
|
const { x, y } = position;
|
|
|
|
const newX = x + ui.deltaX;
|
|
|
|
const newY = y + ui.deltaY;
|
|
|
|
setPosition({
|
|
|
|
x: newX,
|
|
|
|
y: newY,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-09-09 04:14:51 +02:00
|
|
|
function handleDragStop(e, ui) {
|
2020-08-27 08:02:43 +02:00
|
|
|
if (wasDragging) {
|
|
|
|
e.stopPropagation();
|
|
|
|
setWasDragging(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-02 08:28:14 +02:00
|
|
|
return (
|
2019-08-13 07:35:13 +02:00
|
|
|
<Draggable
|
2020-09-09 04:14:51 +02:00
|
|
|
onDrag={handleDragMove}
|
|
|
|
onStart={handleDragStart}
|
|
|
|
onStop={handleDragStop}
|
2019-08-13 07:35:13 +02:00
|
|
|
defaultPosition={position}
|
2020-04-14 01:48:11 +02:00
|
|
|
position={isFloating ? position : { x: 0, y: 0 }}
|
2019-08-13 07:35:13 +02:00
|
|
|
bounds="parent"
|
2020-04-14 01:48:11 +02:00
|
|
|
disabled={!isFloating}
|
2020-01-08 21:00:50 +01:00
|
|
|
handle=".draggable"
|
2019-08-13 07:35:13 +02:00
|
|
|
cancel=".button"
|
2019-08-02 08:28:14 +02:00
|
|
|
>
|
2019-08-13 07:35:13 +02:00
|
|
|
<div
|
|
|
|
className={classnames('content__viewer', {
|
2020-04-14 01:48:11 +02:00
|
|
|
'content__viewer--floating': isFloating,
|
|
|
|
'content__viewer--inline': !isFloating,
|
2019-08-13 07:35:13 +02:00
|
|
|
})}
|
|
|
|
style={
|
2020-04-14 01:48:11 +02:00
|
|
|
!isFloating && fileViewerRect
|
2019-08-13 07:35:13 +02:00
|
|
|
? { width: fileViewerRect.width, height: fileViewerRect.height, left: fileViewerRect.x }
|
|
|
|
: {}
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<div
|
|
|
|
className={classnames('content__wrapper', {
|
2020-04-14 01:48:11 +02:00
|
|
|
'content__wrapper--floating': isFloating,
|
2019-08-02 08:28:14 +02:00
|
|
|
})}
|
2019-08-13 07:35:13 +02:00
|
|
|
>
|
2020-04-14 01:48:11 +02:00
|
|
|
{isFloating && (
|
2020-07-22 22:56:58 +02:00
|
|
|
<Tooltip label={__('Close')}>
|
|
|
|
<Button
|
|
|
|
onClick={closeFloatingPlayer}
|
|
|
|
icon={ICONS.REMOVE}
|
|
|
|
button="primary"
|
|
|
|
className="content__floating-close"
|
|
|
|
/>
|
|
|
|
</Tooltip>
|
2019-08-13 07:35:13 +02:00
|
|
|
)}
|
|
|
|
|
2020-05-05 20:02:12 +02:00
|
|
|
{isReadyToPlay ? (
|
|
|
|
<FileRender
|
2020-07-22 22:56:58 +02:00
|
|
|
className="draggable"
|
2020-05-05 20:02:12 +02:00
|
|
|
uri={uri}
|
|
|
|
// @if TARGET='app'
|
|
|
|
desktopPlayStartTime={desktopPlayStartTime}
|
|
|
|
// @endif
|
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
<LoadingScreen status={loadingMessage} />
|
|
|
|
)}
|
2020-04-14 01:48:11 +02:00
|
|
|
{isFloating && (
|
2020-01-08 21:00:50 +01:00
|
|
|
<div className="draggable content__info">
|
2020-01-30 23:25:15 +01:00
|
|
|
<div className="claim-preview__title" title={title || uri}>
|
2020-07-22 22:56:58 +02:00
|
|
|
<Button label={title || uri} navigate={uri} button="link" className="content__floating-link" />
|
2019-08-13 07:35:13 +02:00
|
|
|
</div>
|
|
|
|
<UriIndicator link addTooltip={false} uri={uri} />
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</Draggable>
|
2019-08-02 08:28:14 +02:00
|
|
|
);
|
2017-04-23 11:56:50 +02:00
|
|
|
}
|