homeflowjs 0.8.10 → 0.8.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,5 +1,31 @@
|
|
|
1
|
-
const replaceImageDimensions = (src, width, height) => {
|
|
2
|
-
src.
|
|
1
|
+
const replaceImageDimensions = (src, width = '_', height = '_') => {
|
|
2
|
+
const srcToArray = src.split('/');
|
|
3
|
+
// /files/photos/12345/6789/my-photo.jpg
|
|
4
|
+
const srcPathWithoutSize = !isNaN(srcToArray[srcToArray.length - 2])
|
|
5
|
+
&& !isNaN(srcToArray[srcToArray.length - 3]);
|
|
6
|
+
// /files/photos/12345/6789/_x_/my-photo.jpg
|
|
7
|
+
const srcPathWithSize = !isNaN(srcToArray[srcToArray.length - 3])
|
|
8
|
+
&& !isNaN(srcToArray[srcToArray.length - 4]);
|
|
9
|
+
const widthXheight = `${width}x${height}`;
|
|
10
|
+
const lastSlashIndex = src.lastIndexOf('/');
|
|
11
|
+
const secondTolastSlashIndex = src.lastIndexOf('/', lastSlashIndex - 1);
|
|
12
|
+
const pathBeforeFileName = src.substring(secondTolastSlashIndex, lastSlashIndex + 1);
|
|
13
|
+
let formatedSrc = null; // default to null
|
|
14
|
+
if ((srcPathWithoutSize || srcPathWithSize) && src.includes('_x_')) {
|
|
15
|
+
formatedSrc = src.replace('_x_', widthXheight);
|
|
16
|
+
}
|
|
17
|
+
if ((srcPathWithoutSize || srcPathWithSize) && !src.includes('_x_') && !pathBeforeFileName.includes('x')) {
|
|
18
|
+
formatedSrc = `${src.substring(0, lastSlashIndex + 1)}${widthXheight}${src.substr(lastSlashIndex)}`;
|
|
19
|
+
}
|
|
20
|
+
// src contains width or height set on the cms but not the component
|
|
21
|
+
if ((srcPathWithoutSize || srcPathWithSize) && !src.includes('_x_') && pathBeforeFileName.includes('x')) {
|
|
22
|
+
formatedSrc = src;
|
|
23
|
+
}
|
|
24
|
+
if (srcPathWithoutSize === srcPathWithSize) {
|
|
25
|
+
console.log('Source not supported', src);
|
|
26
|
+
formatedSrc = null;
|
|
27
|
+
}
|
|
28
|
+
return formatedSrc;
|
|
3
29
|
};
|
|
4
30
|
|
|
5
31
|
export default replaceImageDimensions;
|
|
@@ -3,10 +3,10 @@ import PropTypes from 'prop-types';
|
|
|
3
3
|
import replaceImageDimensions from './replaceImageDimensions';
|
|
4
4
|
|
|
5
5
|
const ResponsiveImage = ({
|
|
6
|
-
src, alt, srcSet, className, ...otherProps
|
|
6
|
+
src, alt, srcSet, className, lazyload, ...otherProps
|
|
7
7
|
}) => {
|
|
8
8
|
const renderSrcSet = () => {
|
|
9
|
-
if (srcSet) {
|
|
9
|
+
if (!srcSet) {
|
|
10
10
|
return (
|
|
11
11
|
`${replaceImageDimensions(src, 175)} 175w,
|
|
12
12
|
${replaceImageDimensions(src, 375)} 375w,
|
|
@@ -16,34 +16,36 @@ const ResponsiveImage = ({
|
|
|
16
16
|
${replaceImageDimensions(src, 1200)} 1200w,
|
|
17
17
|
${replaceImageDimensions(src, 1400)} 1400w,
|
|
18
18
|
${replaceImageDimensions(src, 1920)} 1920w,`
|
|
19
|
-
|
|
19
|
+
);
|
|
20
20
|
}
|
|
21
21
|
return srcSet;
|
|
22
22
|
};
|
|
23
23
|
|
|
24
24
|
return (
|
|
25
25
|
<img
|
|
26
|
-
src={src}
|
|
26
|
+
src={replaceImageDimensions(src, 1200)}
|
|
27
27
|
alt={alt}
|
|
28
28
|
className={className}
|
|
29
|
-
loading=
|
|
29
|
+
loading={`${lazyload ? 'lazy' : 'eager'}`}
|
|
30
30
|
srcSet={renderSrcSet()}
|
|
31
31
|
{...otherProps}
|
|
32
32
|
/>
|
|
33
33
|
);
|
|
34
34
|
};
|
|
35
35
|
|
|
36
|
-
|
|
37
36
|
ResponsiveImage.propTypes = {
|
|
38
37
|
src: PropTypes.string.isRequired,
|
|
39
|
-
alt: PropTypes.string
|
|
38
|
+
alt: PropTypes.string,
|
|
40
39
|
srcSet: PropTypes.string,
|
|
40
|
+
lazyload: PropTypes.bool,
|
|
41
41
|
className: PropTypes.string,
|
|
42
42
|
};
|
|
43
43
|
|
|
44
44
|
ResponsiveImage.defaultProps = {
|
|
45
45
|
srcSet: '',
|
|
46
46
|
className: '',
|
|
47
|
+
lazyload: true,
|
|
48
|
+
alt: '', // Decorative images shouldn't have alt
|
|
47
49
|
};
|
|
48
50
|
|
|
49
51
|
export default ResponsiveImage;
|