2019-07-10 07:11:48 +08:00
|
|
|
import React, { createContext, useReducer, useEffect } from 'react';
|
|
|
|
import Storage from '../../../services/storage/session';
|
|
|
|
|
|
|
|
export const WebcamDraggableContext = createContext();
|
|
|
|
|
|
|
|
const initialState = {
|
|
|
|
placement: 'top',
|
|
|
|
mediaSize: {
|
|
|
|
width: 0,
|
|
|
|
height: 0,
|
|
|
|
},
|
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,
|
|
|
|
},
|
|
|
|
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) {
|
|
|
|
case 'setplacementToTop': {
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
placement: 'top',
|
|
|
|
};
|
|
|
|
}
|
|
|
|
case 'setplacementToBottom': {
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
placement: 'bottom',
|
|
|
|
};
|
|
|
|
}
|
|
|
|
case 'setplacementToFloating': {
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
placement: 'floating',
|
|
|
|
};
|
|
|
|
}
|
|
|
|
case 'setMediaSize': {
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
mediaSize: {
|
|
|
|
width: action.value.width,
|
|
|
|
height: action.value.height,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
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 'setLastPosition': {
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
lastPosition: {
|
|
|
|
x: action.value.x,
|
|
|
|
y: action.value.y,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
case 'setVideoRef': {
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
videoRef: action.value,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
case 'setVideoListRef': {
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
videoListRef: action.value,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
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 } = webcamDraggableState;
|
|
|
|
const { children } = props;
|
|
|
|
useEffect(() => {
|
|
|
|
Storage.setItem('webcamPlacement', placement);
|
|
|
|
Storage.setItem('webcamLastPosition', lastPosition);
|
|
|
|
}, [
|
|
|
|
placement,
|
|
|
|
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,
|
|
|
|
};
|