|
1 | 1 | import { mediaApi } from '@sitecore-jss/sitecore-jss/media';
|
2 | 2 | import PropTypes from 'prop-types';
|
3 | 3 | import React from 'react';
|
4 |
| - |
5 | 4 | import {
|
6 | 5 | getEEMarkup,
|
7 | 6 | ImageProps,
|
8 | 7 | ImageField,
|
9 | 8 | ImageFieldValue,
|
| 9 | + withFieldMetadata, |
10 | 10 | } from '@sitecore-jss/sitecore-jss-react';
|
11 | 11 | import Image, { ImageProps as NextImageProperties } from 'next/image';
|
12 | 12 |
|
13 | 13 | type NextImageProps = ImageProps & Partial<NextImageProperties>;
|
14 | 14 |
|
15 |
| -export const NextImage: React.FC<NextImageProps> = ({ |
16 |
| - editable, |
17 |
| - imageParams, |
18 |
| - field, |
19 |
| - mediaUrlPrefix, |
20 |
| - fill, |
21 |
| - priority, |
22 |
| - ...otherProps |
23 |
| -}) => { |
24 |
| - // next handles src and we use a custom loader, |
25 |
| - // throw error if these are present |
26 |
| - if (otherProps.src) { |
27 |
| - throw new Error('Detected src prop. If you wish to use src, use next/image directly.'); |
28 |
| - } |
29 |
| - |
30 |
| - const dynamicMedia = field as ImageField | ImageFieldValue; |
31 |
| - |
32 |
| - if ( |
33 |
| - !field || |
34 |
| - (!dynamicMedia.editable && !dynamicMedia.value && !(dynamicMedia as ImageFieldValue).src) |
35 |
| - ) { |
36 |
| - return null; |
37 |
| - } |
38 |
| - |
39 |
| - const imageField = dynamicMedia as ImageField; |
40 |
| - |
41 |
| - // we likely have an experience editor value, should be a string |
42 |
| - if (editable && imageField.editable) { |
43 |
| - return getEEMarkup( |
44 |
| - imageField, |
45 |
| - imageParams as { [paramName: string]: string | number }, |
46 |
| - mediaUrlPrefix as RegExp, |
47 |
| - otherProps as { src: string } |
48 |
| - ); |
49 |
| - } |
50 |
| - |
51 |
| - // some wise-guy/gal is passing in a 'raw' image object value |
52 |
| - const img: ImageFieldValue = (dynamicMedia as ImageFieldValue).src |
53 |
| - ? (field as ImageFieldValue) |
54 |
| - : (dynamicMedia.value as ImageFieldValue); |
55 |
| - if (!img) { |
56 |
| - return null; |
| 15 | +export const NextImage: React.FC<NextImageProps> = withFieldMetadata<NextImageProps>( |
| 16 | + ({ editable, imageParams, field, mediaUrlPrefix, fill, priority, ...otherProps }) => { |
| 17 | + // next handles src and we use a custom loader, |
| 18 | + // throw error if these are present |
| 19 | + if (otherProps.src) { |
| 20 | + throw new Error('Detected src prop. If you wish to use src, use next/image directly.'); |
| 21 | + } |
| 22 | + |
| 23 | + const dynamicMedia = field as ImageField | ImageFieldValue; |
| 24 | + |
| 25 | + if ( |
| 26 | + !field || |
| 27 | + (!dynamicMedia.editable && !dynamicMedia.value && !(dynamicMedia as ImageFieldValue).src) |
| 28 | + ) { |
| 29 | + return null; |
| 30 | + } |
| 31 | + |
| 32 | + const imageField = dynamicMedia as ImageField; |
| 33 | + |
| 34 | + // we likely have an experience editor value, should be a string |
| 35 | + if (editable && imageField.editable) { |
| 36 | + return getEEMarkup( |
| 37 | + imageField, |
| 38 | + imageParams as { [paramName: string]: string | number }, |
| 39 | + mediaUrlPrefix as RegExp, |
| 40 | + otherProps as { src: string } |
| 41 | + ); |
| 42 | + } |
| 43 | + |
| 44 | + // some wise-guy/gal is passing in a 'raw' image object value |
| 45 | + const img: ImageFieldValue = (dynamicMedia as ImageFieldValue).src |
| 46 | + ? (field as ImageFieldValue) |
| 47 | + : (dynamicMedia.value as ImageFieldValue); |
| 48 | + if (!img) { |
| 49 | + return null; |
| 50 | + } |
| 51 | + |
| 52 | + const attrs = { |
| 53 | + ...img, |
| 54 | + ...otherProps, |
| 55 | + fill, |
| 56 | + priority, |
| 57 | + src: mediaApi.updateImageUrl( |
| 58 | + img.src as string, |
| 59 | + imageParams as { [paramName: string]: string | number }, |
| 60 | + mediaUrlPrefix as RegExp |
| 61 | + ), |
| 62 | + }; |
| 63 | + |
| 64 | + const imageProps = { |
| 65 | + ...attrs, |
| 66 | + // force replace /media with /jssmedia in src since we _know_ we will be adding a 'mw' query string parameter |
| 67 | + // this is required for Sitecore media API resizing to work properly |
| 68 | + src: mediaApi.replaceMediaUrlPrefix(attrs.src, mediaUrlPrefix as RegExp), |
| 69 | + }; |
| 70 | + |
| 71 | + // Exclude `width`, `height` in case image is responsive, `fill` is used |
| 72 | + if (imageProps.fill) { |
| 73 | + delete imageProps.width; |
| 74 | + delete imageProps.height; |
| 75 | + } |
| 76 | + |
| 77 | + if (attrs) { |
| 78 | + return <Image alt="" {...imageProps} />; |
| 79 | + } |
| 80 | + |
| 81 | + return null; // we can't handle the truth |
57 | 82 | }
|
58 |
| - |
59 |
| - const attrs = { |
60 |
| - ...img, |
61 |
| - ...otherProps, |
62 |
| - fill, |
63 |
| - priority, |
64 |
| - src: mediaApi.updateImageUrl( |
65 |
| - img.src as string, |
66 |
| - imageParams as { [paramName: string]: string | number }, |
67 |
| - mediaUrlPrefix as RegExp |
68 |
| - ), |
69 |
| - }; |
70 |
| - |
71 |
| - const imageProps = { |
72 |
| - ...attrs, |
73 |
| - // force replace /media with /jssmedia in src since we _know_ we will be adding a 'mw' query string parameter |
74 |
| - // this is required for Sitecore media API resizing to work properly |
75 |
| - src: mediaApi.replaceMediaUrlPrefix(attrs.src, mediaUrlPrefix as RegExp), |
76 |
| - }; |
77 |
| - |
78 |
| - // Exclude `width`, `height` in case image is responsive, `fill` is used |
79 |
| - if (imageProps.fill) { |
80 |
| - delete imageProps.width; |
81 |
| - delete imageProps.height; |
82 |
| - } |
83 |
| - |
84 |
| - if (attrs) { |
85 |
| - return <Image alt="" {...imageProps} />; |
86 |
| - } |
87 |
| - |
88 |
| - return null; // we can't handle the truth |
89 |
| -}; |
| 83 | +); |
90 | 84 |
|
91 | 85 | NextImage.propTypes = {
|
92 | 86 | field: PropTypes.oneOfType([
|
|
0 commit comments