From 482c773b2e3217c73db3223da60303f3037b4fe3 Mon Sep 17 00:00:00 2001 From: Miguel Fonseca Date: Mon, 5 Nov 2018 18:23:43 +0000 Subject: [PATCH 01/11] [TRY] Gallery: Duplicate images' IDs into easy-to-parse attribute --- packages/block-library/src/gallery/edit.js | 34 ++++++++++++++++----- packages/block-library/src/gallery/index.js | 11 +++++++ 2 files changed, 37 insertions(+), 8 deletions(-) diff --git a/packages/block-library/src/gallery/edit.js b/packages/block-library/src/gallery/edit.js index b9a125db38be3..0e9c5374f2462 100644 --- a/packages/block-library/src/gallery/edit.js +++ b/packages/block-library/src/gallery/edit.js @@ -1,7 +1,7 @@ /** * External Dependencies */ -import { filter, pick, get } from 'lodash'; +import { filter, pick, map, get } from 'lodash'; /** * WordPress dependencies @@ -63,12 +63,28 @@ class GalleryEdit extends Component { this.setImageAttributes = this.setImageAttributes.bind( this ); this.addFiles = this.addFiles.bind( this ); this.uploadFromFiles = this.uploadFromFiles.bind( this ); + this.setAttributes = this.setAttributes.bind( this ); this.state = { selectedImage: null, }; } + setAttributes( attributes ) { + if ( attributes.ids ) { + throw new Error( "Don't use this attribute, dangit." ); + } + + if ( attributes.images ) { + attributes = { + ...attributes, + id: map( attributes.images, 'id' ), + }; + } + + this.props.setAttributes( attributes ); + } + onSelectImage( index ) { return () => { if ( this.state.selectedImage !== index ) { @@ -84,7 +100,7 @@ class GalleryEdit extends Component { const images = filter( this.props.attributes.images, ( img, i ) => index !== i ); const { columns } = this.props.attributes; this.setState( { selectedImage: null } ); - this.props.setAttributes( { + this.setAttributes( { images, columns: columns ? Math.min( images.length, columns ) : columns, } ); @@ -92,21 +108,21 @@ class GalleryEdit extends Component { } onSelectImages( images ) { - this.props.setAttributes( { + this.setAttributes( { images: images.map( ( image ) => pickRelevantMediaFiles( image ) ), } ); } setLinkTo( value ) { - this.props.setAttributes( { linkTo: value } ); + this.setAttributes( { linkTo: value } ); } setColumnsNumber( value ) { - this.props.setAttributes( { columns: value } ); + this.setAttributes( { columns: value } ); } toggleImageCrop() { - this.props.setAttributes( { imageCrop: ! this.props.attributes.imageCrop } ); + this.setAttributes( { imageCrop: ! this.props.attributes.imageCrop } ); } getImageCropHelp( checked ) { @@ -114,7 +130,8 @@ class GalleryEdit extends Component { } setImageAttributes( index, attributes ) { - const { attributes: { images }, setAttributes } = this.props; + const { attributes: { images } } = this.props; + const { setAttributes } = this; if ( ! images[ index ] ) { return; } @@ -136,7 +153,8 @@ class GalleryEdit extends Component { addFiles( files ) { const currentImages = this.props.attributes.images || []; - const { noticeOperations, setAttributes } = this.props; + const { noticeOperations } = this.props; + const { setAttributes } = this; mediaUpload( { allowedTypes: ALLOWED_MEDIA_TYPES, filesList: files, diff --git a/packages/block-library/src/gallery/index.js b/packages/block-library/src/gallery/index.js index e347c806d2af3..9c8cf4d58685a 100644 --- a/packages/block-library/src/gallery/index.js +++ b/packages/block-library/src/gallery/index.js @@ -52,6 +52,10 @@ const blockAttributes = { }, }, }, + ids: { + type: 'array', + default: [], + }, columns: { type: 'number', }, @@ -89,6 +93,7 @@ export const settings = { if ( validImages.length > 0 ) { return createBlock( 'core/gallery', { images: validImages.map( ( { id, url, alt, caption } ) => ( { id, url, alt, caption } ) ), + ids: validImages.map( ( { id } ) => id ), } ); } return createBlock( 'core/gallery' ); @@ -110,6 +115,12 @@ export const settings = { } ) ); }, }, + ids: { + type: 'array', + shortcode: ( { named: { ids } } ) => { + return ids.split( ',' ); + }, + }, columns: { type: 'number', shortcode: ( { named: { columns = '3' } } ) => { From a4e6649b55c76e588c6f220ef9a31f28ca896080 Mon Sep 17 00:00:00 2001 From: Jorge Date: Thu, 15 Nov 2018 16:47:16 +0000 Subject: [PATCH 02/11] typo fix --- packages/block-library/src/gallery/edit.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/block-library/src/gallery/edit.js b/packages/block-library/src/gallery/edit.js index 0e9c5374f2462..7c644a689657d 100644 --- a/packages/block-library/src/gallery/edit.js +++ b/packages/block-library/src/gallery/edit.js @@ -78,7 +78,7 @@ class GalleryEdit extends Component { if ( attributes.images ) { attributes = { ...attributes, - id: map( attributes.images, 'id' ), + ids: map( attributes.images, 'id' ), }; } From 7c8404a0673881dbda9baafc83561e91db6f8c39 Mon Sep 17 00:00:00 2001 From: Jorge Date: Thu, 15 Nov 2018 17:26:37 +0000 Subject: [PATCH 03/11] Fixed a check in shortcode transform; Handled the file transform. --- packages/block-library/src/gallery/index.js | 28 ++++++++++++++------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/packages/block-library/src/gallery/index.js b/packages/block-library/src/gallery/index.js index 9c8cf4d58685a..54f1c93aa46d1 100644 --- a/packages/block-library/src/gallery/index.js +++ b/packages/block-library/src/gallery/index.js @@ -1,7 +1,7 @@ /** * External dependencies */ -import { filter, every } from 'lodash'; +import { filter, every, map } from 'lodash'; /** * WordPress dependencies @@ -71,6 +71,16 @@ const blockAttributes = { export const name = 'core/gallery'; +const parseShortCodeIds = ( ids ) => { + if ( ! ids ) { + return []; + } + + return ids.split( ',' ).map( ( id ) => ( + parseInt( id, 10 ) + ) ); +}; + export const settings = { title: __( 'Gallery' ), description: __( 'Display multiple images in a rich gallery.' ), @@ -106,19 +116,15 @@ export const settings = { images: { type: 'array', shortcode: ( { named: { ids } } ) => { - if ( ! ids ) { - return []; - } - - return ids.split( ',' ).map( ( id ) => ( { - id: parseInt( id, 10 ), + return parseShortCodeIds( ids ).map( ( id ) => ( { + id, } ) ); }, }, ids: { type: 'array', shortcode: ( { named: { ids } } ) => { - return ids.split( ',' ); + return parseShortCodeIds( ids ); }, }, columns: { @@ -150,8 +156,12 @@ export const settings = { mediaUpload( { filesList: files, onFileChange: ( images ) => { + const imagesAttr = images.map( + ( image ) => pickRelevantMediaFiles( image ) + ); onChange( block.clientId, { - images: images.map( ( image ) => pickRelevantMediaFiles( image ) ), + ids: map( imagesAttr, 'id' ), + images: imagesAttr, } ); }, allowedTypes: [ 'image' ], From a5e25dfecf07e38e1c25167bbee013c911be4ca5 Mon Sep 17 00:00:00 2001 From: Jorge Date: Thu, 15 Nov 2018 17:55:34 +0000 Subject: [PATCH 04/11] Corrected fixtures and snapshots. --- test/integration/__snapshots__/blocks-raw-handling.spec.js.snap | 2 +- test/integration/fixtures/wordpress-out.html | 2 +- test/integration/full-content/fixtures/core__gallery.json | 1 + .../full-content/fixtures/core__gallery__columns.json | 1 + 4 files changed, 4 insertions(+), 2 deletions(-) diff --git a/test/integration/__snapshots__/blocks-raw-handling.spec.js.snap b/test/integration/__snapshots__/blocks-raw-handling.spec.js.snap index 84028b7c5f539..f92c383e63768 100644 --- a/test/integration/__snapshots__/blocks-raw-handling.spec.js.snap +++ b/test/integration/__snapshots__/blocks-raw-handling.spec.js.snap @@ -29,7 +29,7 @@ exports[`Blocks raw handling rawHandler should convert HTML post to blocks with

Shortcode

- + " `; diff --git a/test/integration/fixtures/wordpress-out.html b/test/integration/fixtures/wordpress-out.html index b1d5977395c22..e179380c983da 100644 --- a/test/integration/fixtures/wordpress-out.html +++ b/test/integration/fixtures/wordpress-out.html @@ -18,6 +18,6 @@

More tag

Shortcode

- + diff --git a/test/integration/full-content/fixtures/core__gallery.json b/test/integration/full-content/fixtures/core__gallery.json index 8cfcc26d3b37a..591c118c69d9d 100644 --- a/test/integration/full-content/fixtures/core__gallery.json +++ b/test/integration/full-content/fixtures/core__gallery.json @@ -16,6 +16,7 @@ "caption": "" } ], + "ids": [], "imageCrop": true, "linkTo": "none" }, diff --git a/test/integration/full-content/fixtures/core__gallery__columns.json b/test/integration/full-content/fixtures/core__gallery__columns.json index b3daaa05f6e8a..4a1c83971db24 100644 --- a/test/integration/full-content/fixtures/core__gallery__columns.json +++ b/test/integration/full-content/fixtures/core__gallery__columns.json @@ -16,6 +16,7 @@ "caption": "" } ], + "ids": [], "columns": 1, "imageCrop": true, "linkTo": "none" From 063f64345b8ade96398e4c69f97767d3cd539c08 Mon Sep 17 00:00:00 2001 From: Jorge Date: Thu, 15 Nov 2018 18:49:17 +0000 Subject: [PATCH 05/11] Improved error message. --- packages/block-library/src/gallery/edit.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/block-library/src/gallery/edit.js b/packages/block-library/src/gallery/edit.js index 7c644a689657d..003570d3bdf7d 100644 --- a/packages/block-library/src/gallery/edit.js +++ b/packages/block-library/src/gallery/edit.js @@ -72,7 +72,7 @@ class GalleryEdit extends Component { setAttributes( attributes ) { if ( attributes.ids ) { - throw new Error( "Don't use this attribute, dangit." ); + throw new Error( 'The "ids" attribute should not be changed directly. It is managed automatically when "images" attribute changes' ); } if ( attributes.images ) { From 3dc86a751320b1f785c2071b3d12f12624b3f771 Mon Sep 17 00:00:00 2001 From: Miguel Fonseca Date: Mon, 19 Nov 2018 12:02:35 +0000 Subject: [PATCH 06/11] Update packages/block-library/src/gallery/index.js Co-Authored-By: jorgefilipecosta --- packages/block-library/src/gallery/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/block-library/src/gallery/index.js b/packages/block-library/src/gallery/index.js index 54f1c93aa46d1..044b4b943d9ce 100644 --- a/packages/block-library/src/gallery/index.js +++ b/packages/block-library/src/gallery/index.js @@ -157,7 +157,7 @@ export const settings = { filesList: files, onFileChange: ( images ) => { const imagesAttr = images.map( - ( image ) => pickRelevantMediaFiles( image ) + pickRelevantMediaFiles ); onChange( block.clientId, { ids: map( imagesAttr, 'id' ), From b93ec68632e7f16dabead43e4cbb295328de909e Mon Sep 17 00:00:00 2001 From: Miguel Fonseca Date: Mon, 19 Nov 2018 12:03:15 +0000 Subject: [PATCH 07/11] Update packages/block-library/src/gallery/index.js Co-Authored-By: jorgefilipecosta --- packages/block-library/src/gallery/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/block-library/src/gallery/index.js b/packages/block-library/src/gallery/index.js index 044b4b943d9ce..f19c1f36070a6 100644 --- a/packages/block-library/src/gallery/index.js +++ b/packages/block-library/src/gallery/index.js @@ -124,7 +124,7 @@ export const settings = { ids: { type: 'array', shortcode: ( { named: { ids } } ) => { - return parseShortCodeIds( ids ); + return parseShortcodeIds( ids ); }, }, columns: { From f24476105005234c5119d730eeb34a17f2690075 Mon Sep 17 00:00:00 2001 From: Miguel Fonseca Date: Mon, 19 Nov 2018 12:03:22 +0000 Subject: [PATCH 08/11] Update packages/block-library/src/gallery/index.js Co-Authored-By: jorgefilipecosta --- packages/block-library/src/gallery/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/block-library/src/gallery/index.js b/packages/block-library/src/gallery/index.js index f19c1f36070a6..92d90249785b9 100644 --- a/packages/block-library/src/gallery/index.js +++ b/packages/block-library/src/gallery/index.js @@ -116,7 +116,7 @@ export const settings = { images: { type: 'array', shortcode: ( { named: { ids } } ) => { - return parseShortCodeIds( ids ).map( ( id ) => ( { + return parseShortcodeIds( ids ).map( ( id ) => ( { id, } ) ); }, From a973f0ef1d9df578a05595d29daaac1532820041 Mon Sep 17 00:00:00 2001 From: Miguel Fonseca Date: Mon, 19 Nov 2018 12:03:42 +0000 Subject: [PATCH 09/11] Update packages/block-library/src/gallery/index.js Co-Authored-By: jorgefilipecosta --- packages/block-library/src/gallery/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/block-library/src/gallery/index.js b/packages/block-library/src/gallery/index.js index 92d90249785b9..e4c4de7bf5b7b 100644 --- a/packages/block-library/src/gallery/index.js +++ b/packages/block-library/src/gallery/index.js @@ -71,7 +71,7 @@ const blockAttributes = { export const name = 'core/gallery'; -const parseShortCodeIds = ( ids ) => { +const parseShortcodeIds = ( ids ) => { if ( ! ids ) { return []; } From 526b9568fe058d8ccd4e845e4d43fb487011e058 Mon Sep 17 00:00:00 2001 From: Jorge Date: Mon, 19 Nov 2018 12:57:59 +0000 Subject: [PATCH 10/11] Add deprecation logic. --- packages/block-library/src/gallery/index.js | 62 ++++++++++++++++++++- post-content.php | 4 +- 2 files changed, 63 insertions(+), 3 deletions(-) diff --git a/packages/block-library/src/gallery/index.js b/packages/block-library/src/gallery/index.js index e4c4de7bf5b7b..080da725b8d92 100644 --- a/packages/block-library/src/gallery/index.js +++ b/packages/block-library/src/gallery/index.js @@ -1,7 +1,7 @@ /** * External dependencies */ -import { filter, every, map } from 'lodash'; +import { filter, every, map, some } from 'lodash'; /** * WordPress dependencies @@ -220,6 +220,66 @@ export const settings = { }, deprecated: [ + { + attributes: blockAttributes, + isEligible( { images, ids } ) { + return images && + images.length > 0 && + ( + ( ! ids && images ) || + ( ids && images && ids.length !== images.length ) || + some( images, ( id, index ) => { + if ( ! id && ids[ index ] !== null ) { + return true; + } + return parseInt( id, 10 ) !== ids[ index ]; + } ) + ); + }, + migrate( attributes ) { + return { + ...attributes, + ids: map( attributes.images, ( { id } ) => { + if ( ! id ) { + return null; + } + return parseInt( id, 10 ); + } ), + }; + }, + save( { attributes } ) { + const { images, columns = defaultColumnsNumber( attributes ), imageCrop, linkTo } = attributes; + return ( +
    + { images.map( ( image ) => { + let href; + + switch ( linkTo ) { + case 'media': + href = image.url; + break; + case 'attachment': + href = image.link; + break; + } + + const img = {; + + return ( +
  • +
    + { href ? { img } : img } + { image.caption && image.caption.length > 0 && ( + + ) } +
    +
  • + ); + } ) } +
+ ); + }, + }, { attributes: blockAttributes, save( { attributes } ) { diff --git a/post-content.php b/post-content.php index 7dac5620a8efd..f34262d1eed4c 100644 --- a/post-content.php +++ b/post-content.php @@ -88,7 +88,7 @@

- +