Repository navigation
Fix the Cloudinary Gallery block preview in the iframed editor #1368
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
4dcd972
5e48d24
588f8a5
a01624c
cc6c808
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,5 +9,5 @@ | |
| 'wp-element', | ||
| 'wp-i18n' | ||
| ), | ||
| 'version' => 'f4dd3a6a83f3bee704eb' | ||
| 'version' => '2c1d097420ba8c1ff297' | ||
| ); | ||
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| /* global cloudinaryGalleryApi CLD_GALLERY_CONFIG CLD_REST_ENDPOINT */ | ||
| /* global CLD_GALLERY_CONFIG CLD_GALLERY_DELIVERY CLD_REST_ENDPOINT */ | ||
|
|
||
| /** | ||
| * External dependencies | ||
|
|
@@ -12,7 +12,7 @@ import { __ } from '@wordpress/i18n'; | |
| import apiFetch from '@wordpress/api-fetch'; | ||
| import { Spinner } from '@wordpress/components'; | ||
| import '@wordpress/components/build-style/style.css'; | ||
| import { useEffect, useMemo, useState } from '@wordpress/element'; | ||
| import { useEffect, useMemo, useRef, useState } from '@wordpress/element'; | ||
| import { | ||
| InspectorControls, | ||
| MediaPlaceholder, | ||
|
|
@@ -25,7 +25,12 @@ import { | |
| import '../../css/gallery.scss'; | ||
| import Controls from './controls'; | ||
| import { ALLOWED_MEDIA_TYPES } from './options'; | ||
| import { generateId, setupAttributesForRendering, showNotice } from './utils'; | ||
| import { | ||
| generateId, | ||
| parseCustomSettings, | ||
| setupAttributesForRendering, | ||
| showNotice, | ||
| } from './utils'; | ||
|
|
||
| const dot = new Dot( '_' ); | ||
|
|
||
|
|
@@ -40,6 +45,40 @@ const galleryWidgetConfig = ( config, container ) => ( { | |
| zoom: false, | ||
| } ); | ||
|
|
||
| /** | ||
| * Get the Product Gallery widget factory of a given window. | ||
| * | ||
| * @param {Window} win The window to look the widget up in. | ||
| * @return {((config: Object) => Object)|undefined} The `galleryWidget` factory, if loaded. | ||
| */ | ||
| const getGalleryWidget = ( win ) => win?.cloudinary?.galleryWidget; | ||
|
|
||
| /** | ||
| * Get the delivery settings of the connected Cloudinary account. | ||
| * | ||
| * The front end always applies them over the block attributes when it | ||
| * renders a gallery, so the preview does too. Blocks may have no cloud name | ||
| * saved (e.g. created outside the editor) or a stale one, and the custom | ||
| * domain settings are not block attributes at all. | ||
| * | ||
| * `CLD_GALLERY_DELIVERY` is built by the same PHP method the front end uses | ||
| * (`Gallery::get_delivery_config()`), so both render with the same values. | ||
| * | ||
| * @return {Object} The cloud name and custom domain settings. | ||
| */ | ||
| const getDeliveryConfig = () => | ||
| 'undefined' === typeof CLD_GALLERY_DELIVERY ? {} : CLD_GALLERY_DELIVERY; | ||
|
|
||
| /** | ||
| * Log that the gallery library is missing from the frame holding the | ||
| * preview, which otherwise leaves the block blank without any message. | ||
| */ | ||
| const warnLibraryMissing = () => { | ||
| console.warn( | ||
| 'Cloudinary Gallery: the gallery library is not loaded in the editor canvas.' | ||
| ); | ||
| }; | ||
|
|
||
| // Prefix for the generated gallery container class. A literal prefix keeps | ||
| // the value a valid, unique CSS selector. Prior to the Block API v2 upgrade | ||
| // the block's `className` prop was used, but that prop is no longer passed to | ||
|
|
@@ -49,6 +88,7 @@ const CONTAINER_PREFIX = 'cld-gallery-'; | |
| const Edit = ( { setAttributes, attributes, isSelected } ) => { | ||
| const [ errorMessage, setErrorMessage ] = useState( null ); | ||
| const [ loading, setLoading ] = useState( false ); | ||
| const containerRef = useRef( null ); | ||
|
|
||
| const preparedAttributes = useMemo( () => { | ||
| // Do not override block settings with defaults on existing ones. | ||
|
|
@@ -108,30 +148,94 @@ const Edit = ( { setAttributes, attributes, isSelected } ) => { | |
| setErrorMessage( null ); | ||
| } | ||
|
|
||
| if ( attributes.selectedImages.length ) { | ||
| let gallery; | ||
| const container = containerRef.current; | ||
|
|
||
| if ( ! attributes.selectedImages.length || ! container ) { | ||
| return; | ||
| } | ||
|
|
||
| // In the iframed block editor the container lives in the editor | ||
| // canvas iframe, so the widget of that frame has to be used: it looks | ||
| // the container up in its own document and validates its config with | ||
| // checks bound to its own frame. | ||
| const win = container.ownerDocument.defaultView || window; | ||
|
|
||
| // Recreate the config in the target frame, so that its objects pass | ||
| // the widget's validation there. | ||
| const toFrame = ( config ) => | ||
| win === window | ||
| ? config | ||
| : win.JSON.parse( JSON.stringify( config ) ); | ||
|
|
||
| const { customSettings, ...attributesConfig } = | ||
| setupAttributesForRendering( attributes ); | ||
| const config = { ...attributesConfig, ...getDeliveryConfig() }; | ||
| const custom = parseCustomSettings( customSettings ); | ||
|
|
||
| let gallery; | ||
| let cancelled = false; | ||
|
|
||
| const render = () => { | ||
| if ( cancelled ) { | ||
| return; | ||
| } | ||
|
|
||
| const { customSettings, ...config } = | ||
| setupAttributesForRendering( attributes ); | ||
| const galleryWidget = getGalleryWidget( win ); | ||
|
|
||
| // The library failed to load in this frame. | ||
| if ( ! galleryWidget ) { | ||
| warnLibraryMissing(); | ||
| setLoading( false ); | ||
| return; | ||
| } | ||
|
|
||
| try { | ||
| gallery = cloudinary.galleryWidget( | ||
| galleryWidgetConfig( | ||
| { ...config, ...customSettings }, | ||
| attributes.container | ||
| ) | ||
| ); | ||
| } catch { | ||
| gallery = cloudinary.galleryWidget( | ||
| galleryWidgetConfig( config, attributes.container ) | ||
| // Shallow merge, matching how the front end applies Custom | ||
| // Settings over the block attributes. Fall back to the config | ||
| // without them if the widget rejects the merged one. | ||
| try { | ||
| gallery = galleryWidget( | ||
| toFrame( | ||
| galleryWidgetConfig( | ||
| { ...config, ...custom }, | ||
| attributes.container | ||
| ) | ||
| ) | ||
| ); | ||
| } catch { | ||
| gallery = galleryWidget( | ||
| toFrame( | ||
| galleryWidgetConfig( config, attributes.container ) | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| gallery.render(); | ||
| } catch ( error ) { | ||
| console.error( | ||
| 'Cloudinary Gallery: could not render the preview.', | ||
| error | ||
| ); | ||
| } finally { | ||
| setLoading( false ); | ||
| } | ||
| }; | ||
|
|
||
| gallery.render(); | ||
| if ( getGalleryWidget( win ) ) { | ||
| render(); | ||
| } else if ( 'complete' !== win.document.readyState ) { | ||
| // The library may still be loading in the editor canvas. | ||
| win.addEventListener( 'load', render, { once: true } ); | ||
| } else { | ||
| warnLibraryMissing(); | ||
| setLoading( false ); | ||
|
|
||
| return () => gallery.destroy(); | ||
| } | ||
|
Comment on lines
+226
to
232
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion (non-blocking): Gutenberg mounts the blocks in the canvas only after the iframe } else {
// eslint-disable-next-line no-console
console.warn( 'Cloudinary Gallery: the gallery library is not loaded in the editor canvas.' );
setLoading( false );
}
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed, a blank preview with no message is exactly what made this hard to track down. Added in cc6c808. The warning is in a small helper used in both places the library can turn out to be missing: this |
||
|
|
||
| return () => { | ||
| cancelled = true; | ||
| win.removeEventListener( 'load', render ); | ||
| gallery?.destroy(); | ||
| }; | ||
| }, [ errorMessage, attributes, setAttributes ] ); | ||
|
|
||
| const hasImages = !! attributes.selectedImages.length; | ||
|
|
@@ -157,7 +261,10 @@ const Edit = ( { setAttributes, attributes, isSelected } ) => { | |
| return ( | ||
| <div { ...blockProps }> | ||
| <> | ||
| <div className={ attributes.container }></div> | ||
| <div | ||
| ref={ containerRef } | ||
| className={ attributes.container } | ||
| ></div> | ||
| <div className="wp-block-cloudinary-gallery"> | ||
| <MediaPlaceholder | ||
| labels={ { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
issue (non-blocking):
maybe_enqueue_scripts()andenqueue_gallery_library()both stop whenwp_script_is( 'cld-gallery' )is true. Ifenqueue_block_assetsenqueues the library in the outer admin document beforeenqueue_block_editor_assetsruns,CLD_GALLERY_CONFIGandgallery-initare not added. Thendot.dot( CLD_GALLERY_CONFIG )throws for new blocks.No core screen does this now (the post, site and widgets editors all fire
enqueue_block_editor_assetsbeforeadmin-header.php), so only a plugin that fires the hook early can cause it. Adding the inline config one time, independent ofwp_script_is(), removes this coupling.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in cc6c808.
enqueue_gallery_library()now addsCLD_GALLERY_CONFIGonce, based on whether the script's inline data already contains it (has_inline_config()), instead of relying onwp_script_is(). I removed the "already enqueued" check frommaybe_enqueue_scripts()as well. Enqueueing again is a no-op and the config is only added once, so enqueuing the library for the canvas first can no longer drop the config orgallery-init.