Skip to content

Commit

Permalink
Gallery Block: Cleanup v1 code
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad committed Jul 9, 2024
1 parent 43966e2 commit 134a682
Show file tree
Hide file tree
Showing 21 changed files with 68 additions and 1,977 deletions.
61 changes: 6 additions & 55 deletions packages/block-library/src/gallery/deprecated.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import {
LINK_DESTINATION_MEDIA,
LINK_DESTINATION_NONE,
} from './constants';
import { isGalleryV2Enabled } from './shared';

const DEPRECATED_LINK_DESTINATION_MEDIA = 'file';
const DEPRECATED_LINK_DESTINATION_ATTACHMENT = 'post';
Expand Down Expand Up @@ -408,11 +407,7 @@ const v6 = {
);
},
migrate( attributes ) {
if ( isGalleryV2Enabled() ) {
return runV2Migration( attributes );
}

return attributes;
return runV2Migration( attributes );
},
};
const v5 = {
Expand Down Expand Up @@ -498,23 +493,7 @@ const v5 = {
return ! linkTo || linkTo === 'attachment' || linkTo === 'media';
},
migrate( attributes ) {
if ( isGalleryV2Enabled() ) {
return runV2Migration( attributes );
}

let linkTo = attributes.linkTo;

if ( ! attributes.linkTo ) {
linkTo = 'none';
} else if ( attributes.linkTo === 'attachment' ) {
linkTo = 'post';
} else if ( attributes.linkTo === 'media' ) {
linkTo = 'file';
}
return {
...attributes,
linkTo,
};
return runV2Migration( attributes );
},
save( { attributes } ) {
const {
Expand Down Expand Up @@ -661,17 +640,7 @@ const v4 = {
return ids && ids.some( ( id ) => typeof id === 'string' );
},
migrate( attributes ) {
if ( isGalleryV2Enabled() ) {
return runV2Migration( attributes );
}

return {
...attributes,
ids: ( attributes.ids ?? [] ).map( ( id ) => {
const parsedId = parseInt( id, 10 );
return Number.isInteger( parsedId ) ? parsedId : null;
} ),
};
return runV2Migration( attributes );
},
save( { attributes } ) {
const {
Expand Down Expand Up @@ -867,10 +836,7 @@ const v3 = {
);
},
migrate( attributes ) {
if ( isGalleryV2Enabled() ) {
return runV2Migration( attributes );
}
return attributes;
return runV2Migration( attributes );
},
};
const v2 = {
Expand Down Expand Up @@ -936,18 +902,7 @@ const v2 = {
);
},
migrate( attributes ) {
if ( isGalleryV2Enabled() ) {
return runV2Migration( attributes );
}
return {
...attributes,
ids: ( attributes.images ?? [] ).map( ( { id } ) => {
if ( ! id ) {
return null;
}
return parseInt( id, 10 );
} ),
};
return runV2Migration( attributes );
},
supports: {
align: true,
Expand Down Expand Up @@ -1100,11 +1055,7 @@ const v1 = {
);
},
migrate( attributes ) {
if ( isGalleryV2Enabled() ) {
return runV2Migration( attributes );
}

return attributes;
return runV2Migration( attributes );
},
};

Expand Down
27 changes: 0 additions & 27 deletions packages/block-library/src/gallery/edit-wrapper.js

This file was deleted.

8 changes: 5 additions & 3 deletions packages/block-library/src/gallery/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
ToggleControl,
RangeControl,
Spinner,
withNotices,
} from '@wordpress/components';
import {
store as blockEditorStore,
Expand Down Expand Up @@ -633,6 +634,7 @@ function GalleryEdit( props ) {
</>
);
}
export default compose( [ withViewportMatch( { isNarrow: '< small' } ) ] )(
GalleryEdit
);
export default compose( [
withNotices,
withViewportMatch( { isNarrow: '< small' } ),
] )( GalleryEdit );
2 changes: 0 additions & 2 deletions packages/block-library/src/gallery/gallery-styles.native.scss
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
@import "./v1/gallery-styles.native.scss";

.galleryAppender {
padding-top: $grid-unit-20;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/gallery/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { gallery as icon } from '@wordpress/icons';
*/
import initBlock from '../utils/init-block';
import deprecated from './deprecated';
import edit from './edit-wrapper';
import edit from './edit';
import metadata from './block.json';
import save from './save';
import transforms from './transforms';
Expand Down
10 changes: 0 additions & 10 deletions packages/block-library/src/gallery/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,7 @@ import {
__experimentalGetElementClassName,
} from '@wordpress/block-editor';

/**
* Internal dependencies
*/
import saveWithoutInnerBlocks from './v1/save';
import { isGalleryV2Enabled } from './shared';

export default function saveWithInnerBlocks( { attributes } ) {
if ( ! isGalleryV2Enabled() ) {
return saveWithoutInnerBlocks( { attributes } );
}

const { caption, columns, imageCrop } = attributes;

const className = clsx( 'has-nested-images', {
Expand Down
28 changes: 0 additions & 28 deletions packages/block-library/src/gallery/shared.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/**
* WordPress dependencies
*/
import { Platform } from '@wordpress/element';

export function defaultColumnsNumber( imageCount ) {
return imageCount ? Math.min( 3, imageCount ) : 3;
}
Expand All @@ -27,26 +22,3 @@ export const pickRelevantMediaFiles = ( image, sizeSlug = 'large' ) => {
}
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;
}
Loading

0 comments on commit 134a682

Please sign in to comment.