-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Allow filtering min and max number of columns in blocks #12861
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ import memoize from 'memize'; | |
import { __ } from '@wordpress/i18n'; | ||
import { PanelBody, RangeControl, G, SVG, Path } from '@wordpress/components'; | ||
import { Fragment } from '@wordpress/element'; | ||
import { applyFilters } from '@wordpress/hooks'; | ||
import { createBlock } from '@wordpress/blocks'; | ||
import { | ||
InspectorControls, | ||
|
@@ -65,6 +66,27 @@ function getDeprecatedLayoutColumn( originalContent ) { | |
} | ||
} | ||
|
||
/** | ||
* Filters the default number of columns of the core/columns block. | ||
* | ||
* @param {Number} number The column count. Defaults to 2. | ||
*/ | ||
const DEFAULT_COLUMNS = applyFilters( 'blocks.columns.defaultColumns', 2 ); | ||
|
||
/** | ||
* Filters the minimum number of allowed columns of the core/columns block. | ||
* | ||
* @param {Number} number The column count. Defaults to 2. | ||
*/ | ||
const MIN_COLUMNS = applyFilters( 'blocks.columns.minColumns', 2 ); | ||
|
||
/** | ||
* Filters the maximum number of allowed columns of the core/columns block. | ||
* | ||
* @param {Number} number The column count. Defaults to 6. | ||
*/ | ||
const MAX_COLUMNS = applyFilters( 'blocks.columns.maxColumns', 6 ); | ||
|
||
export const name = 'core/columns'; | ||
|
||
export const settings = { | ||
|
@@ -77,7 +99,7 @@ export const settings = { | |
attributes: { | ||
columns: { | ||
type: 'number', | ||
default: 2, | ||
default: parseInt( DEFAULT_COLUMNS, 10 ), | ||
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. This one might be confusing as you are also able to update this value using the existing |
||
}, | ||
}, | ||
|
||
|
@@ -170,8 +192,8 @@ export const settings = { | |
columns: nextColumns, | ||
} ); | ||
} } | ||
min={ 2 } | ||
max={ 6 } | ||
min={ Math.max( parseInt( MIN_COLUMNS, 10 ), 1 ) } | ||
max={ parseInt( MAX_COLUMNS, 10 ) } | ||
/> | ||
</PanelBody> | ||
</InspectorControls> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ import { filter, pick, map, get } from 'lodash'; | |
*/ | ||
import { Component, Fragment } from '@wordpress/element'; | ||
import { __, sprintf } from '@wordpress/i18n'; | ||
import { applyFilters } from '@wordpress/hooks'; | ||
import { | ||
IconButton, | ||
DropZone, | ||
|
@@ -32,7 +33,20 @@ import { | |
*/ | ||
import GalleryImage from './gallery-image'; | ||
|
||
const MAX_COLUMNS = 8; | ||
/** | ||
* Filters the minimum number of allowed columns of the core/gallery block. | ||
* | ||
* @param {Number} number The column count. Defaults to 2. | ||
*/ | ||
const MIN_COLUMNS = applyFilters( 'blocks.gallery.minColumns', 2 ); | ||
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. What if I want to change the min columns of a gallery, that has certain attributes or that is nested in a specific block? I feel we should pass more information at least the block client id. |
||
|
||
/** | ||
* Filters the maximum number of allowed columns of the core/gallery block. | ||
* | ||
* @param {Number} number The column count. Defaults to 6. | ||
*/ | ||
const MAX_COLUMNS = applyFilters( 'blocks.gallery.maxColumns', 6 ); | ||
|
||
const linkOptions = [ | ||
{ value: 'attachment', label: __( 'Attachment Page' ) }, | ||
{ value: 'media', label: __( 'Media File' ) }, | ||
|
@@ -243,8 +257,8 @@ class GalleryEdit extends Component { | |
label={ __( 'Columns' ) } | ||
value={ columns } | ||
onChange={ this.setColumnsNumber } | ||
min={ 1 } | ||
max={ Math.min( MAX_COLUMNS, images.length ) } | ||
min={ Math.max( parseInt( MIN_COLUMNS, 10 ), 1 ) } | ||
max={ Math.min( parseInt( MAX_COLUMNS, 10 ), images.length ) } | ||
/> } | ||
<ToggleControl | ||
label={ __( 'Crop Images' ) } | ||
|
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.
I just want to mention that in #core-js chats we agreed to limit the use of hooks and filters. We noticed that for the JavaScript code, hooks and filters are not the best extensibility APIs.
It might be ok for these particular numbers but let's be very careful before adding new ones.
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.
That's very good to know, thanks! Do you happen to have a link to that discussion or a summary of it by chance? I'd be curious to learn more.
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.
Yes, more context in this summary https://make.wordpress.org/core/2018/11/07/javascript-chat-summary-november-6th/