Skip to content
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

Block library: Extract deprecated field to their own files #15057

Merged
merged 3 commits into from
May 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
198 changes: 198 additions & 0 deletions packages/block-library/src/gallery/deprecated.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
/**
* External dependencies
*/
import classnames from 'classnames';
import { map, some } from 'lodash';

/**
* WordPress dependencies
*/
import { RichText } from '@wordpress/block-editor';

/**
* Internal dependencies
*/
import { defaultColumnsNumber } from './shared';

const deprecated = [
{
attributes: {
images: {
type: 'array',
default: [],
source: 'query',
selector: 'ul.wp-block-gallery .blocks-gallery-item',
query: {
url: {
source: 'attribute',
selector: 'img',
attribute: 'src',
},
alt: {
source: 'attribute',
selector: 'img',
attribute: 'alt',
default: '',
},
id: {
source: 'attribute',
selector: 'img',
attribute: 'data-id',
},
link: {
source: 'attribute',
selector: 'img',
attribute: 'data-link',
},
caption: {
type: 'array',
source: 'children',
selector: 'figcaption',
},
},
},
columns: {
type: 'number',
},
imageCrop: {
type: 'boolean',
default: true,
},
linkTo: {
type: 'string',
default: 'none',
},
},
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 (
<ul className={ `columns-${ columns } ${ imageCrop ? 'is-cropped' : '' }` } >
{ images.map( ( image ) => {
let href;

switch ( linkTo ) {
case 'media':
href = image.url;
break;
case 'attachment':
href = image.link;
break;
}

const img = <img src={ image.url } alt={ image.alt } data-id={ image.id } data-link={ image.link } className={ image.id ? `wp-image-${ image.id }` : null } />;

return (
<li key={ image.id || image.url } className="blocks-gallery-item">
<figure>
{ href ? <a href={ href }>{ img }</a> : img }
{ image.caption && image.caption.length > 0 && (
<RichText.Content tagName="figcaption" value={ image.caption } />
) }
</figure>
</li>
);
} ) }
</ul>
);
},
},
{
attributes: {
images: {
type: 'array',
default: [],
source: 'query',
selector: 'div.wp-block-gallery figure.blocks-gallery-image img',
query: {
url: {
source: 'attribute',
attribute: 'src',
},
alt: {
source: 'attribute',
attribute: 'alt',
default: '',
},
id: {
source: 'attribute',
attribute: 'data-id',
},
},
},
columns: {
type: 'number',
},
imageCrop: {
type: 'boolean',
default: true,
},
linkTo: {
type: 'string',
default: 'none',
},
align: {
type: 'string',
default: 'none',
},
},

save( { attributes } ) {
const { images, columns = defaultColumnsNumber( attributes ), align, imageCrop, linkTo } = attributes;
const className = classnames( `columns-${ columns }`, {
alignnone: align === 'none',
'is-cropped': imageCrop,
} );
return (
<div className={ className } >
{ images.map( ( image ) => {
let href;

switch ( linkTo ) {
case 'media':
href = image.url;
break;
case 'attachment':
href = image.link;
break;
}

const img = <img src={ image.url } alt={ image.alt } data-id={ image.id } />;

return (
<figure key={ image.id || image.url } className="blocks-gallery-image">
{ href ? <a href={ href }>{ img }</a> : img }
</figure>
);
} ) }
</div>
);
},
},
];

export default deprecated;
Loading