Skip to content

Commit e8e8b4b

Browse files
authored
Merge pull request #329 from cloudinary/fix/use-render-block
Use render_block to prep gallery for rendering
2 parents ae7d34f + 08ae08d commit e8e8b4b

16 files changed

+144
-82
lines changed

js/gallery-block.asset.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<?php return array('dependencies' => array('react', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-components/build-style/style.css', 'wp-data', 'wp-element', 'wp-i18n', 'wp-polyfill'), 'version' => '0cab79e094af694490067db4b65978cb');
1+
<?php return array('dependencies' => array('react', 'wp-api-fetch', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-components/build-style/style.css', 'wp-data', 'wp-element', 'wp-i18n', 'wp-polyfill'), 'version' => '5d10da56cc3f0e4a9cf2b2a3e7ee85d0');

js/gallery-block.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/gallery-init.asset.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<?php return array('dependencies' => array('wp-polyfill'), 'version' => '958298cb6f697dd90babde36fa3f9bb1');
1+
<?php return array('dependencies' => array('wp-polyfill'), 'version' => '83fbbb5c2ca5ee6af15fda00e03b6459');

js/gallery-init.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/gallery.asset.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<?php return array('dependencies' => array('react', 'wp-block-editor', 'wp-components', 'wp-components/build-style/style.css', 'wp-data', 'wp-element', 'wp-i18n', 'wp-polyfill'), 'version' => 'cce044f0d02652ce16aaaa158bbd7196');
1+
<?php return array('dependencies' => array('react', 'wp-block-editor', 'wp-components', 'wp-components/build-style/style.css', 'wp-data', 'wp-element', 'wp-i18n', 'wp-polyfill'), 'version' => '8208197bd5ef998a03d54b699afa3696');

js/gallery.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/src/components/gallery-init.js

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
1-
/* global cloudinaryGalleryConfig */
1+
/* global CLD_GALLERY_CONFIG */
22

3-
const configElements = document.querySelectorAll(
4-
'[data-cloudinary-gallery-config]'
5-
);
3+
( () => {
4+
/**
5+
* A way to catch the galleryWidget function which comes from the `cloudinary` object.
6+
* The `cloudinary` object is later overwritten by the video player library by Cloudinary,
7+
* which effectively removes the gallery widget library from the page.
8+
*/
9+
const { galleryWidget } = cloudinary;
610

7-
if ( configElements.length ) {
8-
configElements.forEach( function ( el ) {
9-
const configJson = decodeURIComponent(
10-
el.getAttribute( 'data-cloudinary-gallery-config' )
11-
);
12-
const options = JSON.parse( configJson );
13-
options.container = '.' + options.container;
14-
cloudinary.galleryWidget( options ).render();
11+
window.addEventListener( 'load', function () {
12+
if (
13+
document.querySelector( '.woocommerce-page' ) &&
14+
CLD_GALLERY_CONFIG &&
15+
CLD_GALLERY_CONFIG?.mediaAssets?.length
16+
) {
17+
galleryWidget( CLD_GALLERY_CONFIG ).render();
18+
}
1519
} );
16-
} else if (
17-
document.querySelector( '.woocommerce-page' ) &&
18-
cloudinaryGalleryConfig &&
19-
cloudinaryGalleryConfig?.mediaAssets?.length
20-
) {
21-
cloudinary.galleryWidget( cloudinaryGalleryConfig ).render();
22-
}
20+
} )();

js/src/components/settings-gallery.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*global CLD_THEME_COLORS, cloudinaryGalleryConfig */
1+
/*global CLD_THEME_COLORS, CLD_GALLERY_CONFIG */
22

33
import React from 'react';
44
import Dot from 'dot-object';
@@ -11,7 +11,7 @@ import {
1111
} from '../gallery-block/utils';
1212

1313
const { cloudName, mediaAssets, ...attributes } = toBlockAttributes(
14-
new Dot( '_' ).dot( cloudinaryGalleryConfig )
14+
new Dot( '_' ).dot( CLD_GALLERY_CONFIG )
1515
);
1616

1717
const parsedAttrs = {};

js/src/gallery-block/controls.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,13 @@ const Controls = ( { attributes, setAttributes, colors } ) => {
6767
onChange={ ( value ) => {
6868
setAttributes( {
6969
displayProps_mode: value.type,
70-
displayProps_columns: value.columns,
70+
displayProps_columns: value.columns || 1,
7171
} );
7272
} }
7373
icon={ item.icon }
7474
current={ {
7575
type: attributes.displayProps_mode,
76-
columns: attributes.displayProps_columns,
76+
columns: attributes.displayProps_columns || 1,
7777
} }
7878
>
7979
{ item.label }

js/src/gallery-block/edit.js

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* global cloudinaryGalleryApi cloudinaryGalleryConfig */
1+
/* global cloudinaryGalleryApi CLD_GALLERY_CONFIG CLD_REST_ENDPOINT */
22

33
/**
44
* External dependencies
@@ -9,6 +9,7 @@ import Dot from 'dot-object';
99
* WordPress dependencies
1010
*/
1111
import { __ } from '@wordpress/i18n';
12+
import apiFetch from '@wordpress/api-fetch';
1213
import { Spinner } from '@wordpress/components';
1314
import '@wordpress/components/build-style/style.css';
1415
import { useEffect, useMemo, useState } from '@wordpress/element';
@@ -43,9 +44,7 @@ const Edit = ( { setAttributes, attributes, className, isSelected } ) => {
4344
const defaultAttrs = {};
4445

4546
// eslint-disable-next-line no-unused-vars
46-
const { container, ...flattenedAttrs } = dot.dot(
47-
cloudinaryGalleryConfig
48-
);
47+
const { container, ...flattenedAttrs } = dot.dot( CLD_GALLERY_CONFIG );
4948

5049
Object.keys( flattenedAttrs ).forEach( ( attr ) => {
5150
if ( ! attributes[ attr ] ) {
@@ -70,17 +69,16 @@ const Edit = ( { setAttributes, attributes, className, isSelected } ) => {
7069
setLoading( true );
7170

7271
try {
73-
const res = await fetch( cloudinaryGalleryApi.endpoint, {
72+
const selectedImages = await apiFetch( {
73+
path: CLD_REST_ENDPOINT + '/image_data',
7474
method: 'POST',
75-
body: JSON.stringify( { images } ),
76-
headers: {
77-
'X-WP-Nonce': cloudinaryGalleryApi.nonce,
78-
},
75+
data: { images },
7976
} );
8077

81-
const selectedImages = await res.json();
8278
setAttributes( { selectedImages } );
8379
} catch {
80+
setLoading( false );
81+
8482
setErrorMessage(
8583
__(
8684
'Could not load selected images. Please try again.',

0 commit comments

Comments
 (0)