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

Refactored Gallery: Add custom gutter size feature #29216

Closed
wants to merge 103 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
103 commits
Select commit Hold shift + click to select a range
b56f617
Refactor gallery to nested image blocks
Feb 15, 2021
05283d9
Fix issue with images not loading on first selection from media gallery
Nov 4, 2020
bca4501
Remove the default columns setting as we don't have access to innerBl…
Nov 4, 2020
04c1d48
Revert "Remove the default columns setting as we don't have access to…
Nov 4, 2020
ab143d5
Add image count so we can work out default columns as innerBlocks not…
Nov 4, 2020
35c963d
Disable the innerBlocks dropzones so drag drop works same as existing…
Nov 5, 2020
575d31b
Lint changes
Nov 5, 2020
92b4ba7
Revert "Lint changes"
Nov 6, 2020
98e87f5
Revert "Disable the innerBlocks dropzones so drag drop works same as …
Nov 6, 2020
60eee0f
Suggested solution for handling multiple file drop into gallery
Nov 6, 2020
142f399
Remove non image files from drag and drop and disable individual imag…
Jan 21, 2021
b01200d
Fix transform to individual images
Nov 8, 2020
35367ca
Fix transform from individual images
Nov 8, 2020
f37b808
Revert drag and drop transform changes
Nov 9, 2020
48aafa4
Add gallery transform to image block to override the default gallery …
Nov 9, 2020
8113dee
Move handling of file uploads to Gallery from media placeholder
Nov 16, 2020
a699082
split innerblocks mapping into separate effect to reduce chatter
Nov 17, 2020
7a1a78d
Add useMemo to currentImageOptions
Nov 17, 2020
9ed0e6a
reuse existing innerBlocks rather than recreating with every new imag…
Nov 17, 2020
1f205a7
Switch to useMemo for updating local image const instead of local com…
Nov 17, 2020
c94c231
Fix issue with image sizing not being available on initial load of co…
Nov 17, 2020
3fbd034
Memoise the useImageSizes hook
Nov 17, 2020
cb62535
Fix issue with media browser defaulting to edit gallery view
Nov 17, 2020
2a4c183
Fix missed incorrect use of addToGallery
Nov 17, 2020
b6b93ad
Add some extra effects for getting the imageData as the getMedia call…
Nov 18, 2020
9494b74
Simplify the imageData by using a useSelect
Nov 19, 2020
9c148fd
Another optimisation - only return a new imageData reference if all i…
Nov 19, 2020
924dae0
Refactored Gallery: Add loading state to gallery image size options (…
aaronrobertshaw Nov 19, 2020
2487d71
Initial deprecations commit
Jan 13, 2021
1956b50
Fix issue with linkDestination not being applied in migration
Nov 19, 2020
230eb38
Refactor gallery deprecations
aaronrobertshaw Nov 20, 2020
c158077
Fix missing attributes from migration
aaronrobertshaw Nov 20, 2020
16fd6be
Update deprecation to set allowResize
aaronrobertshaw Nov 22, 2020
d1b0ffc
Fix issue with crop not working when certain plugins are loaded
Nov 22, 2020
6520173
Fix SCSS lint errors
aaronrobertshaw Nov 22, 2020
5c4a98f
Update the block example
Nov 23, 2020
48abbc4
Linting fixes
Nov 23, 2020
b66ed22
Fix the e2e test and the accessibility issue with having aria group r…
Nov 23, 2020
ca1ece2
fix linting issues
Nov 23, 2020
2e66c57
Fix frontend omission of wp-block-image class
aaronrobertshaw Nov 24, 2020
348ab67
change deprecation to use imageCount as isEligible check as it seems …
Nov 25, 2020
49eaeb5
Move back to a single deprecations file and reorder in array
Nov 26, 2020
b3e5eed
Remove additional check in v5 isEligible
Nov 26, 2020
4799525
Fix the v4 migration
Nov 26, 2020
40e0b1f
Fix styles for Safari compatibility
aaronrobertshaw Nov 30, 2020
c267218
Remove unnecessary gallery editor styles
aaronrobertshaw Nov 30, 2020
7785529
Fix typo in deprecations
aaronrobertshaw Nov 30, 2020
b787eae
Restore styles to render deprecated gallery versions
Jan 21, 2021
d4e068b
Avoid applying flex styles to IE11
aaronrobertshaw Dec 1, 2020
72e3b09
Add additional selector to prevent the hidden individual image drop z…
Dec 1, 2020
2462580
IE11 styling improvements
aaronrobertshaw Dec 2, 2020
1d431ff
Apply default style class to new images added to gallery
Dec 11, 2020
b355741
fix linting issues
Dec 11, 2020
8891314
Move block props to the outer wrapper
Dec 13, 2020
fbf2449
Revert "Move block props to the outer wrapper"
Dec 13, 2020
5bff2e5
Revert "Revert "Move block props to the outer wrapper""
Dec 13, 2020
132a65a
Fetch media if isListItem is true
Feb 15, 2021
0e4d322
Change context from isListItem to isGrouped
Jan 26, 2021
9128e46
Remove wrapper div
Jan 21, 2021
71f6cb3
remove extra wrapper around media placeholder and caption and use fle…
Jan 13, 2021
7bbe7f8
Revert "remove extra wrapper around media placeholder and caption and…
Jan 13, 2021
3e5c05e
Revert "Remove external div wrapper by moving InnerBlocks to a fragment"
Jan 21, 2021
8bb8ca5
another update to image wrapper
Jan 13, 2021
2586312
put media uploader outside figure so structure matches front end
Dec 17, 2020
7d49db3
Replace div with View for the sake of native code
Dec 17, 2020
04630f3
Move setting of attributes to the child images
Jan 26, 2021
0a99f11
Gallery Block Refactor: Account for null image ids in gallery migrati…
glendaviesnz Dec 22, 2020
c384333
Remove the gradient and put caption under image if is-rounded style a…
glendaviesnz Dec 24, 2020
eaa2db1
Remove outer div wrapper
Jan 21, 2021
8f88776
Keep image margins while dragging sibling
aaronrobertshaw Jan 14, 2021
b74c0f0
Fix e2e test expected markup to match new structure
Jan 18, 2021
e3e6d11
Gallery Block Refactor: Add handling of short code transforms
Feb 15, 2021
ce88731
Removed unused prop
Jan 20, 2021
11c99ef
Account for undefined block and innerblocks in conversion to reusable…
glendaviesnz Jan 20, 2021
155368d
Add custom gutter sizes to refactored gallery (#28377)
aaronrobertshaw Jan 21, 2021
974bccf
Adjust editor styles to match new dom structure
Jan 21, 2021
5edc8f2
Remove redundant styles that are duplicated in nested image blocks
Jan 21, 2021
9d5f6bb
Fix issue with Image block dragged out of Gallery still having inheri…
Jan 22, 2021
81d18fd
When dragging an image block into a gallery make sure we don't wipe a…
Jan 25, 2021
fc50b95
fix issue with variable declaration order
Jan 25, 2021
97fc1b1
Fix bug with custom link being overwritten by gallery linkTo changes
Jan 26, 2021
0935217
Fix application of gutter size CSS var (#28759)
aaronrobertshaw Feb 5, 2021
71bd873
Fix mobile width for gallery images
aaronrobertshaw Feb 10, 2021
f15e8e0
Add missing dependency to inner images selector
aaronrobertshaw Feb 10, 2021
324abeb
Remove the convert to coverblock toolbar button when Image in gallery
Feb 11, 2021
0174ef3
Add fallback to old gallery edit and save for existing gallery (#28961)
glendaviesnz Feb 15, 2021
0c3dce0
Remove duplicate import
Feb 15, 2021
f1ba928
Remove need for temporary imageUploads attribute as we can just creat…
Feb 15, 2021
4181687
Remove handling of gallery attribute updates from child images
Feb 16, 2021
abd84c5
Move updating of attributes back to gallery and show snackbar to indi…
Feb 16, 2021
31a5f3d
Update transforms to work with both versions of gallery
Feb 16, 2021
0bc0292
Remove redundant changes
Feb 16, 2021
784baae
Remove redundant changes
Feb 16, 2021
d1a1a4e
Remove unused method
Feb 16, 2021
62256c8
Merge similar Image transforms.
Feb 16, 2021
b7d8965
Fix some issues missed when moving attribute setting back to gallery.
Feb 18, 2021
b359393
Simplify handling of images from media placeholder now that drag and …
Feb 19, 2021
74b6caa
Fix typo
aaronrobertshaw Feb 19, 2021
63f08f9
Fix broken upload from media placeholder
Feb 19, 2021
6633283
Fix issue with new file uploads overwriting existing blocks
Feb 19, 2021
5307562
Remove isGrouped dependency from call to get image as not needed now …
Feb 22, 2021
658db60
Remove custom gutter size feature
aaronrobertshaw Feb 22, 2021
04b3267
Re-apply original approach to custom gallery gutters
aaronrobertshaw Feb 22, 2021
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
27 changes: 27 additions & 0 deletions lib/experiments-page.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,17 @@ function gutenberg_initialize_experiments_settings() {
'id' => 'gutenberg-widgets-in-customizer',
)
);
add_settings_field(
'gutenberg-gallery-refactor',
__( 'Gallery Refactor', 'gutenberg' ),
'gutenberg_display_experiment_field',
'gutenberg-experiments',
'gutenberg_experiments_section',
array(
'label' => __( 'Enable the refactored gallery block', 'gutenberg' ),
'id' => 'gutenberg-gallery-refactor',
)
);
register_setting(
'gutenberg-experiments',
'gutenberg-experiments'
Expand Down Expand Up @@ -99,3 +110,19 @@ function gutenberg_display_experiment_section() {

<?php
}

/**
* Extends default editor settings with experiments settings.
*
* @param array $settings Default editor settings.
*
* @return array Filtered editor settings.
*/
function gutenberg_experiments_editor_settings( $settings ) {
$experiments_exist = get_option( 'gutenberg-experiments' );
$experiments_settings = array(
'__experimentalGalleryRefactor' => $experiments_exist ? array_key_exists( 'gutenberg-gallery-refactor', get_option( 'gutenberg-experiments' ) ) : false,
);
return array_merge( $settings, $experiments_settings );
}
add_filter( 'block_editor_settings', 'gutenberg_experiments_editor_settings' );
1 change: 1 addition & 0 deletions packages/block-editor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,7 @@ _Properties_
- _\_\_experimentalBlockDirectory_ `boolean`: Whether the user has enabled the Block Directory
- _\_\_experimentalBlockPatterns_ `Array`: Array of objects representing the block patterns
- _\_\_experimentalBlockPatternCategories_ `Array`: Array of objects representing the block pattern categories
- _\_\_experimentalGalleryRefactor_ `boolean`: Whether the user has enabled the refactored gallery block which uses InnerBlocks

<a name="SkipToSelectedBlock" href="#SkipToSelectedBlock">#</a> **SkipToSelectedBlock**

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export function MediaPlaceholder( {
accept,
addToGallery,
multiple = false,
handleUpload = true,
dropZoneUIOnly,
disableDropZone,
disableMediaButtons,
Expand Down Expand Up @@ -118,6 +119,9 @@ export function MediaPlaceholder( {
};

const onFilesUpload = ( files ) => {
if ( ! handleUpload ) {
return onSelect( files );
}
onFilesPreUpload( files );
let setMedia;
if ( multiple ) {
Expand Down
2 changes: 2 additions & 0 deletions packages/block-editor/src/store/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export const PREFERENCES_DEFAULTS = {
* @property {boolean} __experimentalBlockDirectory Whether the user has enabled the Block Directory
* @property {Array} __experimentalBlockPatterns Array of objects representing the block patterns
* @property {Array} __experimentalBlockPatternCategories Array of objects representing the block pattern categories
* @property {boolean} __experimentalGalleryRefactor Whether the user has enabled the refactored gallery block which uses InnerBlocks
*/
export const SETTINGS_DEFAULTS = {
alignWide: false,
Expand Down Expand Up @@ -151,6 +152,7 @@ export const SETTINGS_DEFAULTS = {
__experimentalBlockPatterns: [],
__experimentalBlockPatternCategories: [],
__experimentalSpotlightEntityBlocks: [],
__experimentalGalleryRefactor: false,

// gradients setting is not used anymore now defaults are passed from theme.json on the server and core has its own defaults.
// The setting is only kept for backward compatibility purposes.
Expand Down
29 changes: 29 additions & 0 deletions packages/block-library/src/gallery/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@
},
"default": []
},
"shortCodeTransforms": {
"type": "array",
"default": [],
"items": {
"type": "object"
}
},
"columns": {
"type": "number",
"minimum": 1,
Expand All @@ -68,14 +75,36 @@
"type": "boolean",
"default": true
},
"linkTarget": {
"type": "string"
},
"linkTo": {
"type": "string"
},
"sizeSlug": {
"type": "string",
"default": "large"
},
"allowResize": {
"type": "boolean",
"default": false
},
"isGrouped": {
"type": "boolean",
"default": true
},
"imageCount": {
"type": "number",
"default": 0
},
"gutterSize": {
"type": "number"
}
},
"providesContext": {
"allowResize": "allowResize",
"isGrouped": "isGrouped"
},
"supports": {
"anchor": true,
"align": true
Expand Down
4 changes: 2 additions & 2 deletions packages/block-library/src/gallery/constants.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export const LINK_DESTINATION_NONE = 'none';
export const LINK_DESTINATION_MEDIA = 'file';
export const LINK_DESTINATION_ATTACHMENT = 'post';
export const LINK_DESTINATION_MEDIA = 'media';
export const LINK_DESTINATION_ATTACHMENT = 'attachment';
22 changes: 15 additions & 7 deletions packages/block-library/src/gallery/deprecated.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,17 @@ import { map, some } from 'lodash';
import { RichText } from '@wordpress/block-editor';

/**
* Internal dependencies
* Original function to determine default number of columns from a block's
* attributes.
*
* Used in deprecations: v1-6.
*
* @param {Object} attributes Block attributes.
* @return {number} Default number of columns for the gallery.
*/
import { defaultColumnsNumber } from './shared';
export function defaultColumnsNumberV1( attributes ) {
return Math.min( 3, attributes.images.length );
}

const deprecated = [
{
Expand Down Expand Up @@ -114,7 +122,7 @@ const deprecated = [
save( { attributes } ) {
const {
images,
columns = defaultColumnsNumber( attributes ),
columns = defaultColumnsNumberV1( attributes ),
imageCrop,
caption,
linkTo,
Expand Down Expand Up @@ -270,7 +278,7 @@ const deprecated = [
save( { attributes } ) {
const {
images,
columns = defaultColumnsNumber( attributes ),
columns = defaultColumnsNumberV1( attributes ),
imageCrop,
caption,
linkTo,
Expand Down Expand Up @@ -409,7 +417,7 @@ const deprecated = [
save( { attributes } ) {
const {
images,
columns = defaultColumnsNumber( attributes ),
columns = defaultColumnsNumberV1( attributes ),
imageCrop,
linkTo,
} = attributes;
Expand Down Expand Up @@ -549,7 +557,7 @@ const deprecated = [
save( { attributes } ) {
const {
images,
columns = defaultColumnsNumber( attributes ),
columns = defaultColumnsNumberV1( attributes ),
imageCrop,
linkTo,
} = attributes;
Expand Down Expand Up @@ -655,7 +663,7 @@ const deprecated = [
save( { attributes } ) {
const {
images,
columns = defaultColumnsNumber( attributes ),
columns = defaultColumnsNumberV1( attributes ),
align,
imageCrop,
linkTo,
Expand Down
141 changes: 141 additions & 0 deletions packages/block-library/src/gallery/deprecated.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
// Deprecated gallery styles pre refactoring to use nested image blocks.
// https://github.com/WordPress/gutenberg/pull/25940.
.wp-block-gallery,
.blocks-gallery-grid {
display: flex;
flex-wrap: wrap;
list-style-type: none;
padding: 0;
// Some themes give all <ul> default margin instead of padding.
margin: 0;

.blocks-gallery-image,
.blocks-gallery-item {
// Add space between thumbnails, and unset right most thumbnails later.
margin: 0 1em 1em 0;
display: flex;
flex-grow: 1;
flex-direction: column;
justify-content: center;
position: relative;

// On mobile and responsive viewports, we allow only 1 or 2 columns at the most.
width: calc(50% - 1em);

&:nth-of-type(even) {
margin-right: 0;
}

figure {
margin: 0;
height: 100%;

// IE doesn't support flex so omit that.
@supports (position: sticky) {
display: flex;
align-items: flex-end;
justify-content: flex-start;
}
}

img {
display: block;
max-width: 100%;
height: auto;

// IE doesn't handle cropping, so we need an explicit width here.
width: 100%;

// IE11 doesn't read rules inside this query. They are applied only to modern browsers.
@supports (position: sticky) {
width: auto;
}
}

figcaption {
position: absolute;
bottom: 0;
width: 100%;
max-height: 100%;
overflow: auto;
padding: 3em 0.77em 0.7em;
color: $white;
text-align: center;
font-size: 0.8em;
background: linear-gradient(0deg, rgba($color: $black, $alpha: 0.7) 0, rgba($color: $black, $alpha: 0.3) 70%, transparent);
box-sizing: border-box;
margin: 0;

img {
display: inline;
}
}
}

figcaption {
flex-grow: 1;
}

// Cropped
&.is-cropped .blocks-gallery-image,
&.is-cropped .blocks-gallery-item {
a,
img {
// IE11 doesn't support object-fit, so just make sure images aren't skewed.
// The following rules are for all browsers.
width: 100%;

// IE11 doesn't read rules inside this query. They are applied only to modern browsers.
@supports (position: sticky) {
height: 100%;
flex: 1;
object-fit: cover;
}
}
}

&.columns-1 .blocks-gallery-image,
&.columns-1 .blocks-gallery-item {
width: 100%;
margin-right: 0;
}

// Beyond mobile viewports, we allow up to 8 columns.
@include break-small {
@for $i from 3 through 8 {
&.columns-#{ $i } .blocks-gallery-image,
&.columns-#{ $i } .blocks-gallery-item {
width: calc(#{ 100% / $i } - #{ 1em * ( $i - 1 ) / $i });
margin-right: 1em;
}
}

// Unset the right margin on every rightmost gallery item to ensure center balance.
@for $column-count from 1 through 8 {
&.columns-#{ $column-count } .blocks-gallery-image:nth-of-type(#{ $column-count }n),
&.columns-#{ $column-count } .blocks-gallery-item:nth-of-type(#{ $column-count }n) {
margin-right: 0;
}
}
}

// Last item always needs margins reset.
.blocks-gallery-image:last-child,
.blocks-gallery-item:last-child {
margin-right: 0;
}

// Apply max-width to floated items that have no intrinsic width.
&.alignleft,
&.alignright {
max-width: $content-width / 2;
width: 100%;
}

// If the gallery is centered, center the content inside as well.
&.aligncenter {
.blocks-gallery-item figure {
justify-content: center;
}
}
}
35 changes: 35 additions & 0 deletions packages/block-library/src/gallery/edit-wrapper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* WordPress dependencies
*/
import { store as blockEditorStore } from '@wordpress/block-editor';
import { useSelect } from '@wordpress/data';

/**
* Internal dependencies
*/
import GalleryEdit from './edit';
import GalleryEditV1 from './v1/edit';

/*
* Using a wrapper around the logic to load the edit for v1 of Gallery block
* or the refactored version with InnerBlocks. This is to prevent conditional
* use of hooks lint errors if adding this logic to the top of the edit component.
*/
export default function GalleryEditWrapper( props ) {
const { attributes } = props;

const __experimentalGalleryRefactor = useSelect( ( select ) => {
const settings = select( blockEditorStore ).getSettings();
return settings.__experimentalGalleryRefactor;
}, [] );

if (
! __experimentalGalleryRefactor ||
attributes?.ids?.length > 0 ||
attributes?.images?.length > 0
) {
return <GalleryEditV1 { ...props } />;
}

return <GalleryEdit { ...props } />;
}
Loading