2021-07-13 03:47:06 +08:00
|
|
|
import React, { useState, useEffect } from 'react';
|
|
|
|
import Resizable from 're-resizable';
|
|
|
|
import Draggable from 'react-draggable';
|
2021-11-03 19:51:56 +08:00
|
|
|
import Styled from './styles';
|
2021-07-13 03:47:06 +08:00
|
|
|
import { ACTIONS, CAMERADOCK_POSITION } from '../layout/enums';
|
|
|
|
import DropAreaContainer from './drop-areas/container';
|
2021-06-22 04:16:59 +08:00
|
|
|
import VideoProviderContainer from '/imports/ui/components/video-provider/container';
|
2021-07-14 00:52:20 +08:00
|
|
|
import Storage from '/imports/ui/services/storage/session';
|
2022-01-26 00:56:52 +08:00
|
|
|
import { colorContentBackground } from '/imports/ui/stylesheets/styled-components/palette';
|
2021-05-25 01:05:39 +08:00
|
|
|
|
2021-06-22 04:16:59 +08:00
|
|
|
const WebcamComponent = ({
|
2021-07-13 03:47:06 +08:00
|
|
|
cameraDock,
|
2021-06-22 04:16:59 +08:00
|
|
|
swapLayout,
|
2021-08-05 19:03:24 +08:00
|
|
|
layoutContextDispatch,
|
2021-07-13 03:47:06 +08:00
|
|
|
fullscreen,
|
2021-08-04 22:38:20 +08:00
|
|
|
isPresenter,
|
2021-08-10 20:05:34 +08:00
|
|
|
displayPresentation,
|
2021-08-07 01:23:46 +08:00
|
|
|
cameraOptimalGridSize: cameraSize,
|
|
|
|
isRTL,
|
2021-07-13 03:47:06 +08:00
|
|
|
}) => {
|
|
|
|
const [isResizing, setIsResizing] = useState(false);
|
|
|
|
const [isDragging, setIsDragging] = useState(false);
|
|
|
|
const [isFullscreen, setIsFullScreen] = useState(false);
|
|
|
|
const [resizeStart, setResizeStart] = useState({ width: 0, height: 0 });
|
2021-08-04 22:38:20 +08:00
|
|
|
const [cameraMaxWidth, setCameraMaxWidth] = useState(0);
|
2022-05-26 01:18:37 +08:00
|
|
|
const [draggedAtLeastOneTime, setDraggedAtLeastOneTime] = useState(false);
|
2021-07-13 03:47:06 +08:00
|
|
|
|
2021-07-14 00:52:20 +08:00
|
|
|
const lastSize = Storage.getItem('webcamSize') || { width: 0, height: 0 };
|
|
|
|
const { width: lastWidth, height: lastHeight } = lastSize;
|
|
|
|
|
|
|
|
const isCameraTopOrBottom = cameraDock.position === CAMERADOCK_POSITION.CONTENT_TOP
|
|
|
|
|| cameraDock.position === CAMERADOCK_POSITION.CONTENT_BOTTOM;
|
|
|
|
const isCameraLeftOrRight = cameraDock.position === CAMERADOCK_POSITION.CONTENT_LEFT
|
|
|
|
|| cameraDock.position === CAMERADOCK_POSITION.CONTENT_RIGHT;
|
2021-08-07 01:23:46 +08:00
|
|
|
const isCameraSidebar = cameraDock.position === CAMERADOCK_POSITION.SIDEBAR_CONTENT_BOTTOM;
|
2021-07-14 00:52:20 +08:00
|
|
|
|
2021-08-18 20:38:59 +08:00
|
|
|
useEffect(() => {
|
|
|
|
const handleVisibility = () => {
|
|
|
|
if (document.hidden) {
|
|
|
|
document.dispatchEvent(new MouseEvent('mouseup', { bubbles: true }));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
document.addEventListener('visibilitychange', handleVisibility);
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
document.removeEventListener('visibilitychange', handleVisibility);
|
|
|
|
};
|
|
|
|
}, []);
|
|
|
|
|
2021-07-13 03:47:06 +08:00
|
|
|
useEffect(() => {
|
|
|
|
setIsFullScreen(fullscreen.group === 'webcams');
|
|
|
|
}, [fullscreen]);
|
|
|
|
|
2021-07-14 00:52:20 +08:00
|
|
|
useEffect(() => {
|
|
|
|
if (isCameraTopOrBottom && lastHeight > 0) {
|
2021-08-05 19:03:24 +08:00
|
|
|
layoutContextDispatch(
|
2021-07-14 00:52:20 +08:00
|
|
|
{
|
|
|
|
type: ACTIONS.SET_CAMERA_DOCK_SIZE,
|
|
|
|
value: {
|
|
|
|
width: cameraDock.width,
|
|
|
|
height: lastHeight,
|
|
|
|
browserWidth: window.innerWidth,
|
|
|
|
browserHeight: window.innerHeight,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (isCameraLeftOrRight && lastWidth > 0) {
|
2021-08-05 19:03:24 +08:00
|
|
|
layoutContextDispatch(
|
2021-07-14 00:52:20 +08:00
|
|
|
{
|
|
|
|
type: ACTIONS.SET_CAMERA_DOCK_SIZE,
|
|
|
|
value: {
|
|
|
|
width: lastWidth,
|
|
|
|
height: cameraDock.height,
|
|
|
|
browserWidth: window.innerWidth,
|
|
|
|
browserHeight: window.innerHeight,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}, [cameraDock.position, lastWidth, lastHeight]);
|
|
|
|
|
2021-08-04 22:38:20 +08:00
|
|
|
useEffect(() => {
|
2021-08-07 01:23:46 +08:00
|
|
|
const newCameraMaxWidth = (isPresenter && cameraDock.presenterMaxWidth) ? cameraDock.presenterMaxWidth : cameraDock.maxWidth;
|
2021-08-04 22:38:20 +08:00
|
|
|
setCameraMaxWidth(newCameraMaxWidth);
|
|
|
|
|
|
|
|
if (isCameraLeftOrRight && cameraDock.width > newCameraMaxWidth) {
|
2021-08-06 00:24:37 +08:00
|
|
|
layoutContextDispatch(
|
2021-08-04 22:38:20 +08:00
|
|
|
{
|
|
|
|
type: ACTIONS.SET_CAMERA_DOCK_SIZE,
|
|
|
|
value: {
|
|
|
|
width: newCameraMaxWidth,
|
|
|
|
height: cameraDock.height,
|
|
|
|
browserWidth: window.innerWidth,
|
|
|
|
browserHeight: window.innerHeight,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
);
|
|
|
|
Storage.setItem('webcamSize', { width: newCameraMaxWidth, height: lastHeight });
|
|
|
|
}
|
2021-09-03 02:54:12 +08:00
|
|
|
}, [cameraDock.position, cameraDock.maxWidth, isPresenter, displayPresentation]);
|
2021-08-04 22:38:20 +08:00
|
|
|
|
2021-07-13 03:47:06 +08:00
|
|
|
const onResizeHandle = (deltaWidth, deltaHeight) => {
|
|
|
|
if (cameraDock.resizableEdge.top || cameraDock.resizableEdge.bottom) {
|
2021-08-05 19:03:24 +08:00
|
|
|
layoutContextDispatch(
|
2021-07-13 03:47:06 +08:00
|
|
|
{
|
|
|
|
type: ACTIONS.SET_CAMERA_DOCK_SIZE,
|
|
|
|
value: {
|
|
|
|
width: cameraDock.width,
|
|
|
|
height: resizeStart.height + deltaHeight,
|
|
|
|
browserWidth: window.innerWidth,
|
|
|
|
browserHeight: window.innerHeight,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (cameraDock.resizableEdge.left || cameraDock.resizableEdge.right) {
|
2021-08-05 19:03:24 +08:00
|
|
|
layoutContextDispatch(
|
2021-07-13 03:47:06 +08:00
|
|
|
{
|
|
|
|
type: ACTIONS.SET_CAMERA_DOCK_SIZE,
|
|
|
|
value: {
|
|
|
|
width: resizeStart.width + deltaWidth,
|
|
|
|
height: cameraDock.height,
|
|
|
|
browserWidth: window.innerWidth,
|
|
|
|
browserHeight: window.innerHeight,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleWebcamDragStart = () => {
|
|
|
|
setIsDragging(true);
|
|
|
|
document.body.style.overflow = 'hidden';
|
2021-08-05 19:03:24 +08:00
|
|
|
layoutContextDispatch({
|
2021-07-13 03:47:06 +08:00
|
|
|
type: ACTIONS.SET_CAMERA_DOCK_IS_DRAGGING,
|
|
|
|
value: true,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleWebcamDragStop = (e) => {
|
|
|
|
setIsDragging(false);
|
2022-05-26 01:18:37 +08:00
|
|
|
setDraggedAtLeastOneTime(false);
|
2021-07-13 03:47:06 +08:00
|
|
|
document.body.style.overflow = 'auto';
|
|
|
|
|
2022-05-26 01:18:37 +08:00
|
|
|
if (Object.values(CAMERADOCK_POSITION).includes(e.target.id) && draggedAtLeastOneTime) {
|
2021-08-05 19:03:24 +08:00
|
|
|
layoutContextDispatch({
|
2021-07-13 03:47:06 +08:00
|
|
|
type: ACTIONS.SET_CAMERA_DOCK_POSITION,
|
|
|
|
value: e.target.id,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-08-05 19:03:24 +08:00
|
|
|
layoutContextDispatch({
|
2021-07-13 03:47:06 +08:00
|
|
|
type: ACTIONS.SET_CAMERA_DOCK_IS_DRAGGING,
|
|
|
|
value: false,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2021-08-07 01:23:46 +08:00
|
|
|
let draggableOffset = {
|
|
|
|
left: isDragging && (isCameraTopOrBottom || isCameraSidebar)
|
|
|
|
? ((cameraDock.width - cameraSize.width) / 2)
|
|
|
|
: 0,
|
|
|
|
top: isDragging && isCameraLeftOrRight
|
|
|
|
? ((cameraDock.height - cameraSize.height) / 2)
|
|
|
|
: 0,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (isRTL) {
|
|
|
|
draggableOffset.left = draggableOffset.left * -1;
|
|
|
|
}
|
|
|
|
|
2021-07-13 03:47:06 +08:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{isDragging ? <DropAreaContainer /> : null}
|
2021-11-03 19:51:56 +08:00
|
|
|
<Styled.ResizableWrapper
|
|
|
|
horizontal={cameraDock.position === CAMERADOCK_POSITION.CONTENT_TOP
|
|
|
|
|| cameraDock.position === CAMERADOCK_POSITION.CONTENT_BOTTOM}
|
|
|
|
vertical={cameraDock.position === CAMERADOCK_POSITION.CONTENT_LEFT
|
|
|
|
|| cameraDock.position === CAMERADOCK_POSITION.CONTENT_RIGHT}
|
2021-07-13 03:47:06 +08:00
|
|
|
>
|
2021-11-03 19:51:56 +08:00
|
|
|
<Draggable
|
|
|
|
handle="video"
|
|
|
|
bounds="html"
|
|
|
|
onStart={handleWebcamDragStart}
|
2022-05-26 01:18:37 +08:00
|
|
|
onDrag={() => {
|
|
|
|
if (!draggedAtLeastOneTime) {
|
|
|
|
setDraggedAtLeastOneTime(true);
|
|
|
|
}
|
|
|
|
}}
|
2021-11-03 19:51:56 +08:00
|
|
|
onStop={handleWebcamDragStop}
|
|
|
|
onMouseDown={
|
|
|
|
cameraDock.isDraggable ? (e) => e.preventDefault() : undefined
|
|
|
|
}
|
|
|
|
disabled={!cameraDock.isDraggable || isResizing || isFullscreen}
|
|
|
|
position={
|
|
|
|
{
|
|
|
|
x: cameraDock.left - cameraDock.right + draggableOffset.left,
|
|
|
|
y: cameraDock.top + draggableOffset.top,
|
2021-07-14 00:52:20 +08:00
|
|
|
}
|
2021-11-03 19:51:56 +08:00
|
|
|
}
|
2021-07-13 03:47:06 +08:00
|
|
|
>
|
2021-11-03 19:51:56 +08:00
|
|
|
<Resizable
|
|
|
|
minWidth={isDragging ? cameraSize.width : cameraDock.minWidth}
|
|
|
|
minHeight={isDragging ? cameraSize.height : cameraDock.minHeight}
|
|
|
|
maxWidth={isDragging ? cameraSize.width : cameraMaxWidth}
|
|
|
|
size={{
|
2021-08-07 01:23:46 +08:00
|
|
|
width: isDragging ? cameraSize.width : cameraDock.width,
|
|
|
|
height: isDragging ? cameraSize.height : cameraDock.height,
|
2021-11-03 19:51:56 +08:00
|
|
|
}}
|
|
|
|
onResizeStart={() => {
|
|
|
|
setIsResizing(true);
|
|
|
|
setResizeStart({ width: cameraDock.width, height: cameraDock.height });
|
|
|
|
layoutContextDispatch({
|
|
|
|
type: ACTIONS.SET_CAMERA_DOCK_IS_RESIZING,
|
|
|
|
value: true,
|
|
|
|
});
|
|
|
|
}}
|
|
|
|
onResize={(e, direction, ref, d) => {
|
|
|
|
onResizeHandle(d.width, d.height);
|
|
|
|
}}
|
|
|
|
onResizeStop={() => {
|
|
|
|
if (isCameraTopOrBottom) {
|
|
|
|
Storage.setItem('webcamSize', { width: lastWidth, height: cameraDock.height });
|
|
|
|
}
|
|
|
|
if (isCameraLeftOrRight) {
|
|
|
|
Storage.setItem('webcamSize', { width: cameraDock.width, height: lastHeight });
|
|
|
|
}
|
|
|
|
setResizeStart({ width: 0, height: 0 });
|
|
|
|
setTimeout(() => setIsResizing(false), 500);
|
|
|
|
layoutContextDispatch({
|
|
|
|
type: ACTIONS.SET_CAMERA_DOCK_IS_RESIZING,
|
|
|
|
value: false,
|
|
|
|
});
|
|
|
|
}}
|
|
|
|
enable={{
|
|
|
|
top: !isFullscreen && !isDragging && !swapLayout && cameraDock.resizableEdge.top,
|
|
|
|
bottom: !isFullscreen && !isDragging && !swapLayout && cameraDock.resizableEdge.bottom,
|
|
|
|
left: !isFullscreen && !isDragging && !swapLayout && cameraDock.resizableEdge.left,
|
|
|
|
right: !isFullscreen && !isDragging && !swapLayout && cameraDock.resizableEdge.right,
|
|
|
|
topLeft: false,
|
|
|
|
topRight: false,
|
|
|
|
bottomLeft: false,
|
|
|
|
bottomRight: false,
|
|
|
|
}}
|
|
|
|
style={{
|
|
|
|
position: 'absolute',
|
|
|
|
zIndex: cameraDock.zIndex,
|
2021-07-13 03:47:06 +08:00
|
|
|
}}
|
|
|
|
>
|
2021-11-03 19:51:56 +08:00
|
|
|
<Styled.Draggable
|
|
|
|
isDraggable={cameraDock.isDraggable && !isFullscreen && !isDragging}
|
|
|
|
isDragging={isDragging}
|
|
|
|
id="cameraDock"
|
2022-01-13 00:40:45 +08:00
|
|
|
role="region"
|
2021-11-03 19:51:56 +08:00
|
|
|
draggable={cameraDock.isDraggable && !isFullscreen ? 'true' : undefined}
|
|
|
|
style={{
|
|
|
|
width: isDragging ? cameraSize.width : cameraDock.width,
|
|
|
|
height: isDragging ? cameraSize.height : cameraDock.height,
|
|
|
|
opacity: isDragging ? 0.5 : undefined,
|
2022-01-26 00:56:52 +08:00
|
|
|
background: isCameraSidebar ? colorContentBackground : null,
|
2021-07-13 03:47:06 +08:00
|
|
|
}}
|
2021-11-03 19:51:56 +08:00
|
|
|
>
|
|
|
|
<VideoProviderContainer
|
|
|
|
{...{
|
|
|
|
swapLayout,
|
|
|
|
cameraDock,
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</Styled.Draggable>
|
|
|
|
</Resizable>
|
|
|
|
</Draggable>
|
|
|
|
</Styled.ResizableWrapper>
|
2021-07-13 03:47:06 +08:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
2021-05-25 01:05:39 +08:00
|
|
|
|
|
|
|
export default WebcamComponent;
|