add use-context-selector changes to smartLayout

This commit is contained in:
Ramon Souza 2021-09-09 11:43:02 -03:00
parent d1a0020f0d
commit 998de7dfd5

View File

@ -1,4 +1,4 @@
import React, { Component } from 'react';
import { useEffect, useRef } from 'react';
import _ from 'lodash';
import { LayoutContextFunc } from '/imports/ui/components/layout/context';
import DEFAULT_VALUES from '/imports/ui/components/layout/defaultValues';
@ -10,19 +10,42 @@ const windowHeight = () => window.document.documentElement.clientHeight;
const min = (value1, value2) => (value1 <= value2 ? value1 : value2);
const max = (value1, value2) => (value1 >= value2 ? value1 : value2);
class SmartLayout extends Component {
constructor(props) {
super(props);
this.throttledCalculatesLayout = _.throttle(() => this.calculatesLayout(),
50, { trailing: true, leading: true });
const SmartLayout = () => {
function usePrevious(value) {
const ref = useRef();
useEffect(() => {
ref.current = value;
});
return ref.current;
}
componentDidMount() {
this.init();
const { layoutContextDispatch } = this.props;
const { layoutContextSelector } = LayoutContextFunc;
const layoutDispatch = layoutContextSelector.layoutDispatch();
const input = layoutContextSelector.select((i) => i.input);
const deviceType = layoutContextSelector.select((i) => i.deviceType);
const isRTL = layoutContextSelector.select((i) => i.isRTL);
const fullscreen = layoutContextSelector.select((i) => i.fullscreen);
const fontSize = layoutContextSelector.select((i) => i.fontSize);
const currentPanelType = layoutContextSelector.select((i) => i.currentPanelType);
const bannerBarInput = layoutContextSelector.selectInput((i) => i.bannerBar);
const notificationsBarInput = layoutContextSelector.selectInput((i) => i.notificationsBar);
const presentationInput = layoutContextSelector.selectInput((i) => i.presentation);
const sidebarNavigationInput = layoutContextSelector.selectInput((i) => i.sidebarNavigation);
const sidebarContentInput = layoutContextSelector.selectInput((i) => i.sidebarContent);
const cameraDockInput = layoutContextSelector.selectInput((i) => i.cameraDock);
const actionbarInput = layoutContextSelector.selectInput((i) => i.actionBar);
const navbarInput = layoutContextSelector.selectInput((i) => i.navBar);
const prevDeviceType = usePrevious(deviceType);
const throttledCalculatesLayout = _.throttle(() => calculatesLayout(),
50, { 'trailing': true, 'leading': true });
useEffect(() => {
window.addEventListener('resize', () => {
layoutContextDispatch({
layoutDispatch({
type: ACTIONS.SET_BROWSER_SIZE,
value: {
width: window.document.documentElement.clientWidth,
@ -30,71 +53,60 @@ class SmartLayout extends Component {
},
});
});
}
}, []);
shouldComponentUpdate(nextProps) {
const { layoutContextState } = this.props;
return layoutContextState.input !== nextProps.layoutContextState.input
|| layoutContextState.deviceType !== nextProps.layoutContextState.deviceType
|| layoutContextState.isRTL !== nextProps.layoutContextState.isRTL
|| layoutContextState.fontSize !== nextProps.layoutContextState.fontSize
|| layoutContextState.fullscreen !== nextProps.layoutContextState.fullscreen;
}
useEffect(() => {
if (deviceType === null) return;
componentDidUpdate(prevProps) {
const { layoutContextState } = this.props;
const { deviceType } = layoutContextState;
if (prevProps.layoutContextState.deviceType !== deviceType) {
this.init();
if (deviceType !== prevDeviceType) {
// reset layout if deviceType changed
// not all options is supported in all devices
init();
} else {
this.throttledCalculatesLayout();
}
throttledCalculatesLayout();
}
}, [input, deviceType]);
bannerAreaHeight() {
const { layoutContextState } = this.props;
const { input } = layoutContextState;
const { bannerBar, notificationsBar } = input;
const bannerHeight = bannerBar.hasBanner ? DEFAULT_VALUES.bannerHeight : 0;
const notificationHeight = notificationsBar.hasNotification ? DEFAULT_VALUES.bannerHeight : 0;
const bannerAreaHeight = () => {
const { hasNotification } = notificationsBarInput;
const { hasBanner } = bannerBarInput;
const bannerHeight = hasBanner ? DEFAULT_VALUES.bannerHeight : 0;
const notificationHeight = hasNotification ? DEFAULT_VALUES.bannerHeight : 0;
return bannerHeight + notificationHeight;
}
};
init() {
const { layoutContextState, layoutContextDispatch } = this.props;
const { deviceType, input } = layoutContextState;
const init = () => {
if (deviceType === DEVICE_TYPE.MOBILE) {
layoutContextDispatch({
layoutDispatch({
type: ACTIONS.SET_LAYOUT_INPUT,
value: _.defaultsDeep({
sidebarNavigation: {
isOpen: false,
sidebarNavPanel: input.sidebarNavigation.sidebarNavPanel,
sidebarNavPanel: sidebarNavigationInput.sidebarNavPanel,
},
sidebarContent: {
isOpen: false,
sidebarContentPanel: input.sidebarContent.sidebarContentPanel,
sidebarContentPanel: sidebarContentInput.sidebarContentPanel,
},
SidebarContentHorizontalResizer: {
isOpen: false,
},
presentation: {
slidesLength: input.presentation.slidesLength,
slidesLength: presentationInput.slidesLength,
currentSlide: {
...input.presentation.currentSlide,
...presentationInput.currentSlide,
},
},
cameraDock: {
numCameras: input.cameraDock.numCameras,
numCameras: cameraDockInput.numCameras,
},
}, INITIAL_INPUT_STATE),
});
} else {
const { sidebarContentPanel } = input.sidebarContent;
const { sidebarContentPanel } = sidebarContentInput;
layoutContextDispatch({
layoutDispatch({
type: ACTIONS.SET_LAYOUT_INPUT,
value: _.defaultsDeep({
sidebarNavigation: {
@ -108,41 +120,31 @@ class SmartLayout extends Component {
isOpen: false,
},
presentation: {
slidesLength: input.presentation.slidesLength,
slidesLength: presentationInput.slidesLength,
currentSlide: {
...input.presentation.currentSlide,
...presentationInput.currentSlide,
},
},
cameraDock: {
numCameras: input.cameraDock.numCameras,
numCameras: cameraDockInput.numCameras,
},
}, INITIAL_INPUT_STATE),
});
}
this.throttledCalculatesLayout();
}
reset() {
this.init();
}
calculatesNavbarBounds(mediaAreaBounds) {
const { layoutContextState } = this.props;
const { isRTL } = layoutContextState;
throttledCalculatesLayout();
};
const calculatesNavbarBounds = (mediaAreaBounds) => {
return {
width: mediaAreaBounds.width,
height: DEFAULT_VALUES.navBarHeight,
top: DEFAULT_VALUES.navBarTop + this.bannerAreaHeight(),
top: DEFAULT_VALUES.navBarTop + bannerAreaHeight(),
left: !isRTL ? mediaAreaBounds.left : 0,
zIndex: 1,
};
}
calculatesActionbarHeight() {
const { layoutContextState } = this.props;
const { fontSize } = layoutContextState;
};
const calculatesActionbarHeight = () => {
const BASE_FONT_SIZE = 14; // 90% font size
const BASE_HEIGHT = DEFAULT_VALUES.actionBarHeight;
const PADDING = DEFAULT_VALUES.actionBarPadding;
@ -154,16 +156,13 @@ class SmartLayout extends Component {
innerHeight: actionBarHeight,
padding: PADDING,
};
}
};
calculatesActionbarBounds(mediaAreaBounds) {
const { layoutContextState } = this.props;
const { input, isRTL } = layoutContextState;
const actionBarHeight = this.calculatesActionbarHeight();
const calculatesActionbarBounds = (mediaAreaBounds) => {
const actionBarHeight = calculatesActionbarHeight();
return {
display: input.actionBar.hasActionBar,
display: actionbarInput.hasActionBar,
width: mediaAreaBounds.width,
height: actionBarHeight.height,
innerHeight: actionBarHeight.innerHeight,
@ -172,11 +171,9 @@ class SmartLayout extends Component {
left: !isRTL ? mediaAreaBounds.left : 0,
zIndex: 1,
};
}
};
calculatesSidebarNavWidth() {
const { layoutContextState } = this.props;
const { deviceType, input } = layoutContextState;
const calculatesSidebarNavWidth = () => {
const {
sidebarNavMinWidth,
sidebarNavMaxWidth,
@ -184,16 +181,16 @@ class SmartLayout extends Component {
let minWidth = 0;
let width = 0;
let maxWidth = 0;
if (input.sidebarNavigation.isOpen) {
if (sidebarNavigationInput.isOpen) {
if (deviceType === DEVICE_TYPE.MOBILE) {
minWidth = windowWidth();
width = windowWidth();
maxWidth = windowWidth();
} else {
if (input.sidebarNavigation.width === 0) {
if (sidebarNavigationInput.width === 0) {
width = min(max((windowWidth() * 0.2), sidebarNavMinWidth), sidebarNavMaxWidth);
} else {
width = min(max(input.sidebarNavigation.width, sidebarNavMinWidth), sidebarNavMaxWidth);
width = min(max(sidebarNavigationInput.width, sidebarNavMinWidth), sidebarNavMaxWidth);
}
minWidth = sidebarNavMinWidth;
maxWidth = sidebarNavMaxWidth;
@ -204,31 +201,27 @@ class SmartLayout extends Component {
width,
maxWidth,
};
}
};
calculatesSidebarNavHeight() {
const { layoutContextState } = this.props;
const { deviceType, input } = layoutContextState;
const calculatesSidebarNavHeight = () => {
let sidebarNavHeight = 0;
if (input.sidebarNavigation.isOpen) {
if (sidebarNavigationInput.isOpen) {
if (deviceType === DEVICE_TYPE.MOBILE) {
sidebarNavHeight = windowHeight() - DEFAULT_VALUES.navBarHeight;
} else {
sidebarNavHeight = windowHeight();
}
sidebarNavHeight -= this.bannerAreaHeight();
sidebarNavHeight -= bannerAreaHeight();
}
return sidebarNavHeight;
}
};
calculatesSidebarNavBounds() {
const { layoutContextState } = this.props;
const { deviceType, isRTL } = layoutContextState;
const calculatesSidebarNavBounds = () => {
const { sidebarNavTop, navBarHeight, sidebarNavLeft } = DEFAULT_VALUES;
let top = sidebarNavTop + this.bannerAreaHeight();
let top = sidebarNavTop + bannerAreaHeight();
if (deviceType === DEVICE_TYPE.MOBILE) top = navBarHeight + this.bannerAreaHeight();
if (deviceType === DEVICE_TYPE.MOBILE) top = navBarHeight + bannerAreaHeight();
return {
top,
@ -236,11 +229,9 @@ class SmartLayout extends Component {
right: isRTL ? sidebarNavLeft : null,
zIndex: deviceType === DEVICE_TYPE.MOBILE ? 11 : 2,
};
}
};
calculatesSidebarContentWidth() {
const { layoutContextState } = this.props;
const { deviceType, input } = layoutContextState;
const calculatesSidebarContentWidth = () => {
const {
sidebarContentMinWidth,
sidebarContentMaxWidth,
@ -248,18 +239,18 @@ class SmartLayout extends Component {
let minWidth = 0;
let width = 0;
let maxWidth = 0;
if (input.sidebarContent.isOpen) {
if (sidebarContentInput.isOpen) {
if (deviceType === DEVICE_TYPE.MOBILE) {
minWidth = windowWidth();
width = windowWidth();
maxWidth = windowWidth();
} else {
if (input.sidebarContent.width === 0) {
if (sidebarContentInput.width === 0) {
width = min(
max((windowWidth() * 0.2), sidebarContentMinWidth), sidebarContentMaxWidth,
);
} else {
width = min(max(input.sidebarContent.width, sidebarContentMinWidth),
width = min(max(sidebarContentInput.width, sidebarContentMinWidth),
sidebarContentMaxWidth);
}
minWidth = sidebarContentMinWidth;
@ -271,31 +262,27 @@ class SmartLayout extends Component {
width,
maxWidth,
};
}
};
calculatesSidebarContentHeight() {
const { layoutContextState } = this.props;
const { deviceType, input } = layoutContextState;
const calculatesSidebarContentHeight = () => {
let sidebarContentHeight = 0;
if (input.sidebarContent.isOpen) {
if (sidebarContentInput.isOpen) {
if (deviceType === DEVICE_TYPE.MOBILE) {
sidebarContentHeight = windowHeight() - DEFAULT_VALUES.navBarHeight;
} else {
sidebarContentHeight = windowHeight();
}
sidebarContentHeight -= this.bannerAreaHeight();
sidebarContentHeight -= bannerAreaHeight();
}
return sidebarContentHeight;
}
};
calculatesSidebarContentBounds(sidebarNavWidth) {
const { layoutContextState } = this.props;
const { deviceType, isRTL } = layoutContextState;
const calculatesSidebarContentBounds = (sidebarNavWidth) => {
const { sidebarNavTop, navBarHeight } = DEFAULT_VALUES;
let top = sidebarNavTop + this.bannerAreaHeight();
let top = sidebarNavTop + bannerAreaHeight();
if (deviceType === DEVICE_TYPE.MOBILE) top = navBarHeight + this.bannerAreaHeight();
if (deviceType === DEVICE_TYPE.MOBILE) top = navBarHeight + bannerAreaHeight();
let left = deviceType === DEVICE_TYPE.MOBILE ? 0 : sidebarNavWidth;
left = !isRTL ? left : null;
@ -309,13 +296,11 @@ class SmartLayout extends Component {
right,
zIndex: deviceType === DEVICE_TYPE.MOBILE ? 11 : 1,
};
}
};
calculatesMediaAreaBounds(sidebarNavWidth, sidebarContentWidth) {
const { layoutContextState } = this.props;
const { deviceType, isRTL } = layoutContextState;
const calculatesMediaAreaBounds = (sidebarNavWidth, sidebarContentWidth) => {
const { navBarHeight } = DEFAULT_VALUES;
const { height: actionBarHeight } = this.calculatesActionbarHeight();
const { height: actionBarHeight } = calculatesActionbarHeight();
let left = 0;
let width = 0;
if (deviceType === DEVICE_TYPE.MOBILE) {
@ -328,19 +313,14 @@ class SmartLayout extends Component {
return {
width,
height: windowHeight() - (navBarHeight + actionBarHeight + this.bannerAreaHeight()),
top: navBarHeight + this.bannerAreaHeight(),
height: windowHeight() - (navBarHeight + actionBarHeight + bannerAreaHeight()),
top: navBarHeight + bannerAreaHeight(),
left,
};
}
};
calculatesCameraDockBounds(mediaAreaBounds, mediaBounds, sidebarSize) {
const { layoutContextState } = this.props;
const {
input, fullscreen, isRTL, deviceType,
} = layoutContextState;
const { presentation } = input;
const { isOpen } = presentation;
const calculatesCameraDockBounds = (mediaAreaBounds, mediaBounds, sidebarSize) => {
const { isOpen } = presentationInput;
const { camerasMargin, presentationToolbarMinWidth } = DEFAULT_VALUES;
const cameraDockBounds = {};
@ -350,7 +330,7 @@ class SmartLayout extends Component {
? mediaBounds.width
: presentationToolbarMinWidth;
if (input.cameraDock.numCameras > 0) {
if (cameraDockInput.numCameras > 0) {
cameraDockBounds.top = mediaAreaBounds.top;
cameraDockBounds.left = mediaAreaBounds.left;
cameraDockBounds.right = isRTL ? sidebarSize + (camerasMargin * 2) : null;
@ -399,13 +379,10 @@ class SmartLayout extends Component {
}
return cameraDockBounds;
}
};
calculatesSlideSize(mediaAreaBounds) {
const { layoutContextState } = this.props;
const { input } = layoutContextState;
const { presentation } = input;
const { currentSlide } = presentation;
const calculatesSlideSize = (mediaAreaBounds) => {
const { currentSlide } = presentationInput;
if (currentSlide.size.width === 0 && currentSlide.size.height === 0) {
return {
@ -429,15 +406,10 @@ class SmartLayout extends Component {
width: slideWidth,
height: slideHeight,
};
}
};
calculatesMediaBounds(mediaAreaBounds, slideSize, sidebarSize) {
const { layoutContextState } = this.props;
const {
input, fullscreen, isRTL, deviceType,
} = layoutContextState;
const { presentation } = input;
const { isOpen } = presentation;
const calculatesMediaBounds = (mediaAreaBounds, slideSize, sidebarSize) => {
const { isOpen } = presentationInput;
const mediaBounds = {};
const { element: fullscreenElement } = fullscreen;
@ -461,7 +433,7 @@ class SmartLayout extends Component {
return mediaBounds;
}
if (input.cameraDock.numCameras > 0 && !input.cameraDock.isDragging) {
if (cameraDockInput.numCameras > 0 && !cameraDockInput.isDragging) {
if (slideSize.width !== 0 && slideSize.height !== 0) {
if (slideSize.width < mediaAreaBounds.width && deviceType !== DEVICE_TYPE.MOBILE) {
if (slideSize.width < (mediaAreaBounds.width * 0.8)) {
@ -508,38 +480,32 @@ class SmartLayout extends Component {
mediaBounds.zIndex = 1;
return mediaBounds;
}
};
calculatesLayout() {
const { layoutContextState, layoutContextDispatch } = this.props;
const { deviceType, input, isRTL } = layoutContextState;
const calculatesLayout = () => {
const { camerasMargin, captionsMargin } = DEFAULT_VALUES;
const sidebarNavWidth = this.calculatesSidebarNavWidth();
const sidebarNavHeight = this.calculatesSidebarNavHeight();
const sidebarContentWidth = this.calculatesSidebarContentWidth();
const sidebarContentHeight = this.calculatesSidebarContentHeight();
const sidebarNavBounds = this
.calculatesSidebarNavBounds(sidebarNavWidth.width, sidebarContentWidth.width);
const sidebarContentBounds = this
.calculatesSidebarContentBounds(sidebarNavWidth.width, sidebarContentWidth.width);
const mediaAreaBounds = this
.calculatesMediaAreaBounds(sidebarNavWidth.width, sidebarContentWidth.width);
const navbarBounds = this.calculatesNavbarBounds(mediaAreaBounds);
const actionbarBounds = this.calculatesActionbarBounds(mediaAreaBounds);
const slideSize = this.calculatesSlideSize(mediaAreaBounds);
const sidebarNavWidth = calculatesSidebarNavWidth();
const sidebarNavHeight = calculatesSidebarNavHeight();
const sidebarContentWidth = calculatesSidebarContentWidth();
const sidebarContentHeight = calculatesSidebarContentHeight();
const sidebarNavBounds = calculatesSidebarNavBounds();
const sidebarContentBounds = calculatesSidebarContentBounds(sidebarNavWidth.width);
const mediaAreaBounds = calculatesMediaAreaBounds(sidebarNavWidth.width, sidebarContentWidth.width);
const navbarBounds = calculatesNavbarBounds(mediaAreaBounds);
const actionbarBounds = calculatesActionbarBounds(mediaAreaBounds);
const slideSize = calculatesSlideSize(mediaAreaBounds);
const sidebarSize = sidebarContentWidth.width + sidebarNavWidth.width;
const mediaBounds = this.calculatesMediaBounds(mediaAreaBounds, slideSize, sidebarSize);
const cameraDockBounds = this
.calculatesCameraDockBounds(mediaAreaBounds, mediaBounds, sidebarSize);
const mediaBounds = calculatesMediaBounds(mediaAreaBounds, slideSize, sidebarSize);
const cameraDockBounds = calculatesCameraDockBounds(mediaAreaBounds, mediaBounds, sidebarSize);
const horizontalCameraDiff = cameraDockBounds.isCameraHorizontal
? cameraDockBounds.width + (camerasMargin * 2)
: 0;
layoutContextDispatch({
layoutDispatch({
type: ACTIONS.SET_NAVBAR_OUTPUT,
value: {
display: input.navBar.hasNavBar,
display: navbarInput.hasNavBar,
width: navbarBounds.width,
height: navbarBounds.height,
top: navbarBounds.top,
@ -549,10 +515,10 @@ class SmartLayout extends Component {
},
});
layoutContextDispatch({
layoutDispatch({
type: ACTIONS.SET_ACTIONBAR_OUTPUT,
value: {
display: input.actionBar.hasActionBar,
display: actionbarInput.hasActionBar,
width: actionbarBounds.width,
height: actionbarBounds.height,
innerHeight: actionbarBounds.innerHeight,
@ -564,7 +530,7 @@ class SmartLayout extends Component {
},
});
layoutContextDispatch({
layoutDispatch({
type: ACTIONS.SET_CAPTIONS_OUTPUT,
value: {
left: !isRTL ? (mediaBounds.left + captionsMargin) : null,
@ -573,10 +539,10 @@ class SmartLayout extends Component {
},
});
layoutContextDispatch({
layoutDispatch({
type: ACTIONS.SET_SIDEBAR_NAVIGATION_OUTPUT,
value: {
display: input.sidebarNavigation.isOpen,
display: sidebarNavigationInput.isOpen,
minWidth: sidebarNavWidth.minWidth,
width: sidebarNavWidth.width,
maxWidth: sidebarNavWidth.maxWidth,
@ -591,7 +557,7 @@ class SmartLayout extends Component {
},
});
layoutContextDispatch({
layoutDispatch({
type: ACTIONS.SET_SIDEBAR_NAVIGATION_RESIZABLE_EDGE,
value: {
top: false,
@ -601,10 +567,10 @@ class SmartLayout extends Component {
},
});
layoutContextDispatch({
layoutDispatch({
type: ACTIONS.SET_SIDEBAR_CONTENT_OUTPUT,
value: {
display: input.sidebarContent.isOpen,
display: sidebarContentInput.isOpen,
minWidth: sidebarContentWidth.minWidth,
width: sidebarContentWidth.width,
maxWidth: sidebarContentWidth.maxWidth,
@ -612,7 +578,7 @@ class SmartLayout extends Component {
top: sidebarContentBounds.top,
left: sidebarContentBounds.left,
right: sidebarContentBounds.right,
currentPanelType: input.currentPanelType,
currentPanelType,
tabOrder: DEFAULT_VALUES.sidebarContentTabOrder,
isResizable: deviceType !== DEVICE_TYPE.MOBILE
&& deviceType !== DEVICE_TYPE.TABLET,
@ -620,7 +586,7 @@ class SmartLayout extends Component {
},
});
layoutContextDispatch({
layoutDispatch({
type: ACTIONS.SET_SIDEBAR_CONTENT_RESIZABLE_EDGE,
value: {
top: false,
@ -630,7 +596,7 @@ class SmartLayout extends Component {
},
});
layoutContextDispatch({
layoutDispatch({
type: ACTIONS.SET_MEDIA_AREA_SIZE,
value: {
width: mediaAreaBounds.width,
@ -638,10 +604,10 @@ class SmartLayout extends Component {
},
});
layoutContextDispatch({
layoutDispatch({
type: ACTIONS.SET_CAMERA_DOCK_OUTPUT,
value: {
display: input.cameraDock.numCameras > 0,
display: cameraDockInput.numCameras > 0,
minWidth: cameraDockBounds.minWidth,
width: cameraDockBounds.width,
maxWidth: cameraDockBounds.maxWidth,
@ -663,10 +629,10 @@ class SmartLayout extends Component {
},
});
layoutContextDispatch({
layoutDispatch({
type: ACTIONS.SET_PRESENTATION_OUTPUT,
value: {
display: input.presentation.isOpen,
display: presentationInput.isOpen,
width: mediaBounds.width,
height: mediaBounds.height,
top: mediaBounds.top,
@ -678,7 +644,7 @@ class SmartLayout extends Component {
},
});
layoutContextDispatch({
layoutDispatch({
type: ACTIONS.SET_SCREEN_SHARE_OUTPUT,
value: {
width: mediaBounds.width,
@ -690,7 +656,7 @@ class SmartLayout extends Component {
},
});
layoutContextDispatch({
layoutDispatch({
type: ACTIONS.SET_EXTERNAL_VIDEO_OUTPUT,
value: {
width: mediaBounds.width,
@ -700,13 +666,9 @@ class SmartLayout extends Component {
right: isRTL ? (mediaBounds.right + horizontalCameraDiff) : null,
},
});
}
};
render() {
return (
<></>
);
}
}
return null;
};
export default LayoutContextFunc.withConsumer(SmartLayout);
export default SmartLayout;