-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcore-image.tsx
343 lines (306 loc) · 8.16 KB
/
core-image.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
import { decode } from 'html-entities';
import type { ComponentProps, CSSProperties } from 'react';
import {
cn,
getClassNamesFromString,
getStylesFromAttributes,
} from '@snapwp/core';
import { Image, Link, Parse } from '@snapwp/next';
import type {
CoreImageAttributes,
CoreImageConnectedMediaItem,
CoreImageMediaDetails,
CoreImage as CoreImageType,
CoreImageProps,
FigureProps,
} from '@snapwp/types';
/**
* Renders a figure element with optional link and caption.
*
* @param props - The properties for the figure element.
* @param [props.children] - The children of the figure element.
* @param [props.classNames] - The class names for the figure element.
* @param [props.renderedHtml] - The rendered HTML.
* @param [props.href] - The href for the link.
* @param [props.linkClass] - The class for the link.
* @param [props.linkTarget] - The target for the link.
* @param [props.rel] - The rel for the link.
* @param [props.lightbox] - The lightbox attribute.
*
* @return {React.JSX.Element} The rendered figure element.
*/
const Figure = ( {
children,
classNames,
renderedHtml,
href,
linkClass,
linkTarget,
rel,
lightbox,
}: FigureProps ): React.JSX.Element => {
let props: ComponentProps< 'figure' > = {};
if ( isLightboxEnabled( lightbox ) ) {
props = extractInteractivityAttributesForElement(
'figure',
renderedHtml
);
}
return (
<figure className={ classNames } { ...props }>
{ href ? (
<Link
href={ href }
className={ linkClass }
target={ linkTarget }
{ ...( rel && { rel } ) }
>
{ children }
</Link>
) : (
children
) }
</figure>
);
};
/**
* Renders a core image block with optional link and caption.
*
* @todo Expose context and state to frontend for lightbox functionality.
*
* @param props - The properties for the core image block.
* @param [props.attributes] - The attributes for the image.
* @param [props.connectedMediaItem] - The connected media item.
* @param [props.mediaDetails] - The media details.
* @param [props.renderedHtml] - The rendered HTML.
*
* @return {React.JSX.Element|null} The rendered core image block or null if no URL is provided.
*/
const CoreImage: CoreImageType = ( {
attributes,
connectedMediaItem,
mediaDetails,
renderedHtml,
}: CoreImageProps ) => {
// @todo: fetchPriority is missing
const { caption, url, lightbox } = attributes || {};
if ( ! url ) {
return null;
}
const classNamesFromString = renderedHtml
? getClassNamesFromString( renderedHtml )
: '';
const classNames = cn( classNamesFromString );
const imageProps = getImageProps(
attributes,
connectedMediaItem,
mediaDetails
);
let imgInteractiveAttributes = {};
if ( isLightboxEnabled( lightbox ) ) {
imgInteractiveAttributes = extractInteractivityAttributesForElement(
'img',
renderedHtml
);
}
let buttonAriaAttributes = {};
if ( isLightboxEnabled( lightbox ) ) {
// Need to extract it because ARIA Label attribute is dynamic in PHP.
buttonAriaAttributes = extractAriaAttributesForElement(
'button',
renderedHtml
);
}
return (
<Figure
renderedHtml={ renderedHtml }
classNames={ classNames }
{ ...attributes }
>
<Image { ...imageProps } { ...imgInteractiveAttributes } />
{ isLightboxEnabled( lightbox ) && (
<button
className="lightbox-trigger"
type="button"
data-wp-init="callbacks.initTriggerButton"
data-wp-on-async--click="actions.showLightbox"
data-wp-style--right="state.imageButtonRight"
data-wp-style--top="state.imageButtonTop"
{ ...buttonAriaAttributes }
>
<svg
xmlns="http://www.w3.org/2000/svg"
width="12"
height="12"
fill="none"
viewBox="0 0 12 12"
>
<path
fill="#fff"
d="M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z"
/>
</svg>
</button>
) }
{ caption && (
<figcaption className="wp-element-caption">
<Parse html={ caption } />
</figcaption>
) }
</Figure>
);
};
/**
* Returns the props for the image component.
*
* @param [attributes] The attributes for the image.
* @param [connectedMediaItem] The connected media item.
* @param [mediaDetails] The media details.
*
* @return {Object} The props for the image component.
*/
const getImageProps = (
attributes?: CoreImageAttributes,
connectedMediaItem?: CoreImageConnectedMediaItem,
mediaDetails?: CoreImageMediaDetails
): ComponentProps< typeof Image > => {
const {
alt,
aspectRatio,
scale,
style,
title,
url,
imageHeight,
width: imageWidth,
sizeSlug,
} = attributes || {};
const styleObject = getStylesFromAttributes( { style } );
const imageStyles: CSSProperties = {
...styleObject,
aspectRatio,
objectFit: scale as CSSProperties[ 'objectFit' ],
};
const imageProps: ComponentProps< typeof Image > = {
src: url,
alt,
title,
};
if ( connectedMediaItem?.node ) {
imageProps.sizes = connectedMediaItem.node.sizes;
imageProps.className = `wp-image-${ connectedMediaItem.node.databaseId }`;
}
// WordPress may not hydrate height/width correctly, so we need to ensure they are set.
if ( mediaDetails ) {
if ( sizeSlug ) {
const size = mediaDetails.sizes?.find(
( _size ) => _size.name === sizeSlug
);
if ( size ) {
imageProps.width = size.width;
imageProps.height = size.height;
} else {
imageProps.width = mediaDetails.width;
imageProps.height = mediaDetails.height;
}
} else {
imageProps.width = mediaDetails.width;
imageProps.height = mediaDetails.height;
}
}
if ( imageWidth ) {
imageStyles.width = imageWidth;
}
if ( imageHeight ) {
imageStyles.height = imageHeight;
}
imageProps.style = imageStyles;
return imageProps;
};
/**
* Is Lightbox enabled for the image.
*
* @param lightbox - The lightbox attribute.
*
* @return {boolean} Whether the lightbox is enabled.
*/
//@ts-ignore -- Stubbed until lightbox support is fixed.
const isLightboxEnabled = ( lightbox?: string | null ): boolean => {
// if ( ! lightbox ) {
// return false;
// }
// const lightboxObj = JSON.parse( lightbox ) as LightBoxProp;
//
// return lightboxObj.enabled;
// Disabling lightbox support for now.
return false;
};
/**
* Extracts interactivity attributes from the rendered HTML for a given element.
*
* @param element - The element to extract attributes for.
* @param renderedHtml - The rendered HTML.
*
* @return {Object} The extracted interactivity attributes.
*/
const extractInteractivityAttributesForElement = (
element: string,
renderedHtml?: string | null
): Record< string, string > => {
if ( ! renderedHtml ) {
return {};
}
const regex = new RegExp( `<${ element }([^>]*data-wp-[^>]*)>`, 'g' );
const dataAttributesRegex = /data-wp-([\w-]+)="([^"]*)"/g;
const result: Record< string, string > = {};
let match;
while ( ( match = regex.exec( renderedHtml ) ) !== null ) {
const [ , attributes ] = match;
let dataMatch;
if ( ! attributes ) {
continue;
}
while (
( dataMatch = dataAttributesRegex.exec( attributes ) ) !== null
) {
const [ , key, value ] = dataMatch;
result[ `data-wp-${ key }` ] = decode( value );
}
}
return result;
};
/**
* Extracts ARIA attributes from the rendered HTML for a given element.
*
* @param element - The element to extract attributes for.
* @param renderedHtml - The rendered HTML.
*
* @return {Object} The extracted ARIA attributes.
*/
const extractAriaAttributesForElement = (
element: string,
renderedHtml?: string | null
): Record< string, string > => {
if ( ! renderedHtml ) {
return {};
}
const regex = new RegExp( `<${ element }([^>]*aria-[^>]*)>`, 'g' );
const dataAttributesRegex = /aria-([\w-]+)="([^"]*)"/g;
const result: Record< string, string > = {};
let match;
while ( ( match = regex.exec( renderedHtml ) ) !== null ) {
const [ , attributes ] = match;
let dataMatch;
if ( ! attributes ) {
continue;
}
while (
( dataMatch = dataAttributesRegex.exec( attributes ) ) !== null
) {
const [ , key, value ] = dataMatch;
result[ `aria-${ key }` ] = decode( value );
}
}
return result;
};
export default CoreImage;