bigbluebutton-Github/bigbluebutton-html5/imports/ui/components/media/webcam-draggable-overlay/context.jsx

160 lines
3.2 KiB
React
Raw Normal View History

2019-07-10 07:11:48 +08:00
import React, { createContext, useReducer, useEffect } from 'react';
import Storage from '/imports/ui/services/storage/session';
2019-07-10 07:11:48 +08:00
export const WebcamDraggableContext = createContext();
const initialState = {
2019-09-18 02:25:28 +08:00
videoListSize: {
width: 0,
height: 0,
},
2019-07-10 07:11:48 +08:00
initialRef: {
x: 0,
y: 0,
},
tempPosition: {
x: 0,
y: 0,
},
lastPosition: {
x: 0,
y: 0,
},
optimalGrid: {},
2019-07-10 07:11:48 +08:00
dragging: false,
videoRef: null,
videoListRef: null,
2019-07-27 03:24:53 +08:00
isCameraFullscreen: false,
2019-07-10 07:11:48 +08:00
};
const reducer = (state, action) => {
switch (action.type) {
2019-09-18 02:25:28 +08:00
case 'setVideoListSize': {
return {
...state,
videoListSize: {
width: action.value.width,
height: action.value.height,
},
};
}
2019-07-10 07:11:48 +08:00
case 'setWebcamRef': {
return {
...state,
webcamRef: action.value,
};
}
case 'setInitialRef': {
return {
...state,
initialRef: {
x: action.value.x,
y: action.value.y,
},
};
}
case 'setTempPosition': {
return {
...state,
tempPosition: {
x: action.value.x,
y: action.value.y,
},
};
}
case 'setVideoRef': {
return {
...state,
videoRef: action.value,
};
}
case 'setVideoListRef': {
return {
...state,
videoListRef: action.value,
};
}
case 'setOptimalGrid': {
return {
...state,
optimalGrid: action.value,
};
}
2019-07-10 07:11:48 +08:00
case 'dragStart': {
return {
...state,
dragging: true,
};
}
case 'dragEnd': {
return {
...state,
dragging: false,
};
}
2019-07-27 03:24:53 +08:00
case 'setIsCameraFullscreen': {
2019-07-27 00:48:51 +08:00
return {
...state,
2019-07-27 03:24:53 +08:00
isCameraFullscreen: action.value,
2019-07-27 00:48:51 +08:00
};
}
2019-07-10 07:11:48 +08:00
default: {
throw new Error('Unexpected action');
}
}
};
const ContextConsumer = Component => props => (
<WebcamDraggableContext.Consumer>
{contexts => <Component {...props} {...contexts} />}
</WebcamDraggableContext.Consumer>
);
const ContextProvider = (props) => {
const [webcamDraggableState, webcamDraggableDispatch] = useReducer(reducer, initialState);
const {
placement,
lastPosition,
lastPlacementLandscape,
lastPlacementPortrait,
} = webcamDraggableState;
2019-07-10 07:11:48 +08:00
const { children } = props;
useEffect(() => {
Storage.setItem('webcamLastPlacementLandscape', lastPlacementLandscape);
Storage.setItem('webcamlastPlacementPortrait', lastPlacementPortrait);
2019-07-10 07:11:48 +08:00
Storage.setItem('webcamLastPosition', lastPosition);
}, [
placement,
lastPlacementLandscape,
lastPlacementPortrait,
2019-07-10 07:11:48 +08:00
lastPosition,
]);
return (
<WebcamDraggableContext.Provider value={{
webcamDraggableState,
webcamDraggableDispatch,
...props,
}}
>
{children}
</WebcamDraggableContext.Provider>
);
};
const withProvider = Component => props => (
<ContextProvider {...props}>
<Component />
</ContextProvider>
);
2019-07-11 21:49:06 +08:00
const withDraggableConsumer = Component => ContextConsumer(Component);
2019-07-10 07:11:48 +08:00
2019-07-11 21:49:06 +08:00
const withDraggableContext = Component => withProvider(withDraggableConsumer(Component));
2019-07-10 07:11:48 +08:00
export {
withProvider,
2019-07-11 21:49:06 +08:00
withDraggableConsumer,
2019-07-10 07:11:48 +08:00
withDraggableContext,
};