Skip to content

Commit

Permalink
Added ScreenSize AppExtras with screen store
Browse files Browse the repository at this point in the history
This is needed for saving the width & height of the screen on resize
  • Loading branch information
razvanMiu committed Apr 8, 2021
1 parent 42e64b0 commit fbdb2c8
Show file tree
Hide file tree
Showing 12 changed files with 147 additions and 102 deletions.
82 changes: 82 additions & 0 deletions src/AppExtras/ScreenSize/ScreenSize.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/* eslint-disable no-extend-native */
import React from 'react';
import { connect } from 'react-redux';
import { compose } from 'redux';
import { setScreen } from '@eeacms/volto-block-style/actions';

const pixelToNumber = (pixel) => {
return parseInt(pixel.replace('px', ''));
};

Number.prototype.toPixel = function toPixel() {
return `${this}px`;
};

const ScreenSize = (props) => {
const updateScreen = () => {
const screenHeight =
window.innerHeight ||
document.documentElement.clientHeight ||
document.body.clientHeight ||
0;
const screenWidth =
window.innerWidth ||
document.documentElement.clientWidth ||
document.body.clientWidth ||
0;
const headerWrapper = document.querySelector(
'.header-wrapper:not(.mobile)',
);
const contentArea = document.querySelector('.ui.segment.content-area');
const toolbar = document.querySelector('#toolbar .toolbar.expanded');
const firstHeading = document.querySelector('.documentFirstHeading');
const headerWrapperStyle = headerWrapper
? window.getComputedStyle(headerWrapper)
: {};
const contentAreaStyle =
contentArea && !firstHeading
? window.getComputedStyle(contentArea)
: { marginTop: '0px', paddingTop: '0px' };

const offsetHeight =
(headerWrapperStyle.display !== 'none'
? headerWrapper?.offsetHeight || 0
: 0) +
(pixelToNumber(contentAreaStyle.marginTop) +
pixelToNumber(contentAreaStyle.paddingTop) || 0) +
((toolbar?.offsetHeight || 0) < screenHeight
? toolbar?.offsetHeight || 0
: 0);
const newScreen = {
screenHeight,
screenWidth,
offsetHeight,
};

props.setScreen({ ...props.screen, ...newScreen });
};

React.useEffect(() => {
if (__CLIENT__) {
updateScreen();
window.addEventListener('resize', updateScreen);
}
return () => {
if (__CLIENT__) {
window.removeEventListener('resize', updateScreen);
}
};
/* eslint-disable-next-line */
}, []);

return '';
};

export default compose(
connect(
(state, props) => ({
screen: state.screen,
}),
{ setScreen },
),
)(ScreenSize);
3 changes: 3 additions & 0 deletions src/AppExtras/ScreenSize/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import ScreenSize from './ScreenSize';

export default ScreenSize;
13 changes: 13 additions & 0 deletions src/AppExtras/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import ScreenSize from './ScreenSize';

export default (config) => {
config.settings.appExtras = [
...(config.settings.appExtras || []),
{
match: '/**',
component: ScreenSize,
},
];

return config;
};
19 changes: 13 additions & 6 deletions src/StyleWrapper/StyleWrapperView.jsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
import React from 'react';
import { connect } from 'react-redux';
import cx from 'classnames';
import config from '@plone/volto/registry';
import {
withCachedImages,
withScreenHeight,
} from '@eeacms/volto-block-style/hocs';
import { withCachedImages } from '@eeacms/volto-block-style/hocs';

export function getInlineStyles(data, props = {}) {
return {
...(data.backgroundColor ? { backgroundColor: data.backgroundColor } : {}),
...(data.textColor ? { color: data.textColor } : {}),
...(data.textAlign ? { textAlign: data.textAlign } : {}),
...(data.fontSize ? { fontSize: data.fontSize } : {}),
...(data.isScreenHeight ? { minHeight: props.screenHeight } : {}),
...(data.isScreenHeight && props.screen.screenHeight
? {
minHeight: (
props.screen.screenHeight - props.screen.offsetHeight
).toPixel(),
}
: {}),
// fill in more
};
}
Expand All @@ -39,6 +43,7 @@ const StyleWrapperView = (props) => {
const style = getStyle(style_name);
const inlineStyles = getInlineStyles(styleData, props);
const styled =
props.styled ||
Object.keys(inlineStyles).length > 0 ||
backgroundImage ||
style ||
Expand Down Expand Up @@ -108,7 +113,9 @@ const StyleWrapperView = (props) => {
);
};

export default withScreenHeight(
export default connect((state, props) => ({
screen: state.screen,
}))(
withCachedImages(StyleWrapperView, {
getImage: (props) => props.styleData.backgroundImage || null,
}),
Expand Down
1 change: 1 addition & 0 deletions src/actions/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './screen';
6 changes: 6 additions & 0 deletions src/actions/screen.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export const setScreen = (screen = {}) => {
return {
type: 'SET_SCREEN',
screen,
};
};
3 changes: 1 addition & 2 deletions src/hocs/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import withCachedImages from './withCachedImages';
import withScreenHeight from './withScreenHeight';

export { withCachedImages, withScreenHeight };
export { withCachedImages };
92 changes: 0 additions & 92 deletions src/hocs/withScreenHeight.jsx

This file was deleted.

7 changes: 6 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import React from 'react';
import installAppExtras from './AppExtras';
import installReducers from './reducers';
import {
BlockStyleWrapperEdit,
BlockStyleWrapperView,
Expand Down Expand Up @@ -57,7 +59,10 @@ const applyConfig = (config) => {
config.settings.layoutOnlyBlockStyles = false;
}

return config;
return [installAppExtras, installReducers].reduce(
(acc, apply) => apply(acc),
config,
);
};

export default applyConfig;
Expand Down
7 changes: 7 additions & 0 deletions src/reducers/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import screen from './screen';

export default (config) => {
config.addonReducers = { ...(config.addonReducers || {}), screen };

return config;
};
14 changes: 14 additions & 0 deletions src/reducers/screen.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const initialState = {};

export default function screen(state = initialState, action = {}) {
switch (action.type) {
case 'SET_SCREEN':
return {
...state,
...action.screen,
};

default:
return state;
}
}
2 changes: 1 addition & 1 deletion src/styles.less
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
img.bg {
/* Set up positioning */
position: absolute;
z-index: -1;
z-index: 0;
top: 0;
left: 0;
/* Set up proportionate scaling */
Expand Down

0 comments on commit fbdb2c8

Please sign in to comment.