-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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 API: Reimplement align as common extension #4069
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5824af3
Block API: Add getBlockSupport API
aduth 5300a56
Block List: Support wrapper props via incoming prop
aduth 0058e5a
Hooks: Add block hook for alignment
aduth 9bf6e72
Blocks: Port Audio block to use align supports
aduth ebfa989
Hooks: Pass valid alignments to rendered align toolbar
aduth 06a301d
Blocks: Small tweaks to supports align property
gziolo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import classnames from 'classnames'; | ||
import { assign, includes } from 'lodash'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { getWrapperDisplayName } from '@wordpress/element'; | ||
import { addFilter } from '@wordpress/hooks'; | ||
import BlockControls from '../block-controls'; | ||
import BlockAlignmentToolbar from '../block-alignment-toolbar'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { getBlockSupport, hasBlockSupport } from '../api'; | ||
|
||
/** | ||
* Filters registered block settings, extending attributes to include `align`. | ||
* | ||
* @param {Object} settings Original block settings | ||
* @return {Object} Filtered block settings | ||
*/ | ||
export function addAttribute( settings ) { | ||
if ( hasBlockSupport( settings, 'align' ) ) { | ||
// Use Lodash's assign to gracefully handle if attributes are undefined | ||
settings.attributes = assign( settings.attributes, { | ||
align: { | ||
type: 'string', | ||
}, | ||
} ); | ||
} | ||
|
||
return settings; | ||
} | ||
|
||
/** | ||
* Returns an array of valid alignments for a block type depending on its | ||
* defined supports. Returns an empty array if block does not support align. | ||
* | ||
* @param {string} blockName Block name to check | ||
* @return {string[]} Valid alignments for block | ||
*/ | ||
export function getBlockValidAlignments( blockName ) { | ||
// Explicitly defined array set of valid alignments | ||
const blockAlign = getBlockSupport( blockName, 'align' ); | ||
if ( Array.isArray( blockAlign ) ) { | ||
return blockAlign; | ||
} | ||
|
||
const validAlignments = []; | ||
if ( true === blockAlign ) { | ||
// `true` includes all alignments... | ||
validAlignments.push( 'left', 'center', 'right' ); | ||
|
||
// ...including wide alignments unless explicitly `false`. | ||
if ( hasBlockSupport( blockName, 'wideAlign', true ) ) { | ||
validAlignments.push( 'wide', 'full' ); | ||
} | ||
} | ||
|
||
return validAlignments; | ||
} | ||
|
||
/** | ||
* Override the default edit UI to include new toolbar controls for block | ||
* alignment, if block defines support. | ||
* | ||
* @param {Function} BlockEdit Original component | ||
* @return {Function} Wrapped component | ||
*/ | ||
export function withToolbarControls( BlockEdit ) { | ||
const WrappedBlockEdit = ( props ) => { | ||
const validAlignments = getBlockValidAlignments( props.name ); | ||
|
||
const updateAlignment = ( nextAlign ) => props.setAttributes( { align: nextAlign } ); | ||
|
||
return [ | ||
validAlignments.length > 0 && props.isSelected && ( | ||
<BlockControls key="align-controls"> | ||
<BlockAlignmentToolbar | ||
value={ props.attributes.align } | ||
onChange={ updateAlignment } | ||
controls={ validAlignments } | ||
/> | ||
</BlockControls> | ||
), | ||
<BlockEdit key="edit" { ...props } />, | ||
]; | ||
}; | ||
WrappedBlockEdit.displayName = getWrapperDisplayName( BlockEdit, 'align' ); | ||
|
||
return WrappedBlockEdit; | ||
} | ||
|
||
/** | ||
* Override the default block element to add alignment wrapper props. | ||
* | ||
* @param {Function} BlockListBlock Original component | ||
* @return {Function} Wrapped component | ||
*/ | ||
export function withAlign( BlockListBlock ) { | ||
const WrappedComponent = ( props ) => { | ||
const { align } = props.block.attributes; | ||
const validAlignments = getBlockValidAlignments( props.block.name ); | ||
|
||
let wrapperProps = props.wrapperProps; | ||
if ( includes( validAlignments, align ) ) { | ||
wrapperProps = { ...wrapperProps, 'data-align': align }; | ||
} | ||
|
||
return <BlockListBlock { ...props } wrapperProps={ wrapperProps } />; | ||
}; | ||
|
||
WrappedComponent.displayName = getWrapperDisplayName( BlockListBlock, 'align' ); | ||
|
||
return WrappedComponent; | ||
} | ||
|
||
/** | ||
* Override props assigned to save component to inject alignment class name if | ||
* block supports it. | ||
* | ||
* @param {Object} props Additional props applied to save element | ||
* @param {Object} blockType Block type | ||
* @param {Object} attributes Block attributes | ||
* @return {Object} Filtered props applied to save element | ||
*/ | ||
export function addAssignedAlign( props, blockType, attributes ) { | ||
const { align } = attributes; | ||
|
||
if ( includes( getBlockValidAlignments( blockType ), align ) ) { | ||
props.className = classnames( `align${ align }`, props.className ); | ||
} | ||
|
||
return props; | ||
} | ||
|
||
addFilter( 'blocks.registerBlockType', 'core/align/addAttribute', addAttribute ); | ||
addFilter( 'editor.BlockListBlock', 'core/align/withAlign', withAlign ); | ||
addFilter( 'blocks.BlockEdit', 'core/align/withToolbarControls', withToolbarControls ); | ||
addFilter( 'blocks.getSaveContent.extraProps', 'core/align/addAssignedAlign', addAssignedAlign ); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Do we want to allow custom values here? At the moment you can pass anything :)