2019-07-10 07:11:48 +08:00
|
|
|
import React, { createContext, useReducer, useEffect } from 'react';
|
2020-03-25 20:52:23 +08:00
|
|
|
import Storage from '/imports/ui/services/storage/session';
|
2020-07-16 00:44:59 +08:00
|
|
|
|
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,
|
|
|
|
},
|
2019-12-06 02:01:23 +08:00
|
|
|
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,
|
|
|
|
};
|
|
|
|
}
|
2019-12-06 02:01:23 +08:00
|
|
|
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);
|
2020-01-17 21:20:03 +08:00
|
|
|
const {
|
|
|
|
placement,
|
|
|
|
lastPosition,
|
|
|
|
lastPlacementLandscape,
|
|
|
|
lastPlacementPortrait,
|
|
|
|
} = webcamDraggableState;
|
2019-07-10 07:11:48 +08:00
|
|
|
const { children } = props;
|
|
|
|
useEffect(() => {
|
2020-01-17 21:20:03 +08:00
|
|
|
Storage.setItem('webcamLastPlacementLandscape', lastPlacementLandscape);
|
|
|
|
Storage.setItem('webcamlastPlacementPortrait', lastPlacementPortrait);
|
2019-07-10 07:11:48 +08:00
|
|
|
Storage.setItem('webcamLastPosition', lastPosition);
|
|
|
|
}, [
|
|
|
|
placement,
|
2020-01-17 21:20:03 +08:00
|
|
|
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,
|
|
|
|
};
|