-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
shared.js
57 lines (50 loc) · 1.61 KB
/
shared.js
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
/**
* External dependencies
*/
import { get } from 'lodash';
/**
* WordPress dependencies
*/
import { Platform } from '@wordpress/element';
export function defaultColumnsNumber( imageCount ) {
return imageCount ? Math.min( 3, imageCount ) : 3;
}
export const pickRelevantMediaFiles = ( image, sizeSlug = 'large' ) => {
const imageProps = Object.fromEntries(
Object.entries( image ?? {} ).filter( ( [ key ] ) =>
[ 'alt', 'id', 'link' ].includes( key )
)
);
imageProps.url =
get( image, [ 'sizes', sizeSlug, 'url' ] ) ||
get( image, [ 'media_details', 'sizes', sizeSlug, 'source_url' ] ) ||
image.url ||
image.source_url;
const fullUrl =
get( image, [ 'sizes', 'full', 'url' ] ) ||
get( image, [ 'media_details', 'sizes', 'full', 'source_url' ] );
if ( fullUrl ) {
imageProps.fullUrl = fullUrl;
}
return imageProps;
};
function getGalleryBlockV2Enabled() {
// We want to fail early here, at least during beta testing phase, to ensure
// there aren't instances where undefined values cause false negatives.
if ( ! window.wp || typeof window.wp.galleryBlockV2Enabled !== 'boolean' ) {
throw 'window.wp.galleryBlockV2Enabled is not defined';
}
return window.wp.galleryBlockV2Enabled;
}
/**
* The new gallery block format is not compatible with the use_BalanceTags option
* in WP versions <= 5.8 https://core.trac.wordpress.org/ticket/54130. The
* window.wp.galleryBlockV2Enabled flag is set in lib/compat.php. This method
* can be removed when minimum supported WP version >=5.9.
*/
export function isGalleryV2Enabled() {
if ( Platform.isNative ) {
return getGalleryBlockV2Enabled();
}
return true;
}