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';
|
2023-09-12 21:42:55 +08:00
|
|
|
import { defineMessages } from 'react-intl';
|
|
|
|
import {
|
|
|
|
isPresenter,
|
|
|
|
} from '/imports/ui/components/settings/service';
|
|
|
|
|
|
|
|
const intlMessages = defineMessages({
|
|
|
|
layoutToastLabelAuto: {
|
|
|
|
id: 'app.layout.modal.layoutToastLabelAuto',
|
|
|
|
description: 'Layout toast label',
|
|
|
|
},
|
|
|
|
});
|
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,
|
2022-02-05 02:08:19 +08:00
|
|
|
focusedId,
|
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,
|
2023-05-12 04:20:26 +08:00
|
|
|
isGridEnabled,
|
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-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 });
|
|
|
|
}
|
2022-07-07 04:03:04 +08:00
|
|
|
|
|
|
|
const cams = document.getElementById('cameraDock');
|
|
|
|
cams?.setAttribute("data-position", cameraDock.position);
|
2021-09-03 02:54:12 +08:00
|
|
|
}, [cameraDock.position, cameraDock.maxWidth, isPresenter, displayPresentation]);
|
2021-08-04 22:38:20 +08:00
|
|
|
|
2022-02-05 02:08:19 +08:00
|
|
|
const handleVideoFocus = (id) => {
|
|
|
|
layoutContextDispatch({
|
|
|
|
type: ACTIONS.SET_FOCUSED_CAMERA_ID,
|
|
|
|
value: focusedId !== id ? id : false,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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) {
|
2022-09-27 04:15:48 +08:00
|
|
|
const layout = document.getElementById('layout');
|
|
|
|
layout?.setAttribute("data-cam-position", 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_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;
|
|
|
|
}
|
2023-04-26 22:28:59 +08:00
|
|
|
const isIphone = !!(navigator.userAgent.match(/iPhone/i));
|
2021-08-07 01:23:46 +08:00
|
|
|
|
2023-04-26 22:28:59 +08:00
|
|
|
const mobileWidth = `${isDragging ? cameraSize.width : cameraDock.width}pt`;
|
|
|
|
const mobileHeight = `${isDragging ? cameraSize.height : cameraDock.height}pt`;
|
|
|
|
const isDesktopWidth = isDragging ? cameraSize.width : cameraDock.width;
|
|
|
|
const isDesktopHeight = isDragging ? cameraSize.height : cameraDock.height;
|
2023-04-27 01:54:30 +08:00
|
|
|
const camOpacity = isDragging ? 0.5 : undefined;
|
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}
|
2022-02-24 21:24:05 +08:00
|
|
|
maxHeight={isDragging ? cameraSize.height : cameraDock.maxHeight}
|
2021-11-03 19:51:56 +08:00
|
|
|
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 });
|
2022-02-24 21:24:05 +08:00
|
|
|
onResizeHandle(cameraDock.width, cameraDock.height);
|
2021-11-03 19:51:56 +08:00
|
|
|
layoutContextDispatch({
|
|
|
|
type: ACTIONS.SET_CAMERA_DOCK_IS_RESIZING,
|
|
|
|
value: true,
|
|
|
|
});
|
|
|
|
}}
|
|
|
|
onResize={(e, direction, ref, d) => {
|
|
|
|
onResizeHandle(d.width, d.height);
|
|
|
|
}}
|
|
|
|
onResizeStop={() => {
|
|
|
|
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,
|
2023-05-25 01:44:26 +08:00
|
|
|
bottom: !isFullscreen && !isDragging && !swapLayout
|
|
|
|
&& cameraDock.resizableEdge.bottom,
|
2021-11-03 19:51:56 +08:00
|
|
|
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',
|
2023-05-26 03:42:46 +08:00
|
|
|
zIndex: isCameraSidebar && !isDragging ? 0 : 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={{
|
2023-04-26 22:28:59 +08:00
|
|
|
width: isIphone ? mobileWidth : isDesktopWidth,
|
|
|
|
height: isIphone ? mobileHeight : isDesktopHeight,
|
2023-04-27 01:59:45 +08:00
|
|
|
opacity: camOpacity,
|
2023-05-25 01:44:26 +08:00
|
|
|
background: null,
|
2021-07-13 03:47:06 +08:00
|
|
|
}}
|
2021-11-03 19:51:56 +08:00
|
|
|
>
|
|
|
|
<VideoProviderContainer
|
|
|
|
{...{
|
|
|
|
swapLayout,
|
|
|
|
cameraDock,
|
2022-02-05 02:08:19 +08:00
|
|
|
focusedId,
|
|
|
|
handleVideoFocus,
|
2023-05-12 04:20:26 +08:00
|
|
|
isGridEnabled,
|
2021-11-03 19:51:56 +08:00
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</Styled.Draggable>
|
|
|
|
</Resizable>
|
|
|
|
</Draggable>
|
|
|
|
</Styled.ResizableWrapper>
|
2021-07-13 03:47:06 +08:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
2021-05-25 01:05:39 +08:00
|
|
|
|
2023-04-24 05:03:38 +08:00
|
|
|
export default WebcamComponent;
|