-
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
Blocks: Support reusable nested blocks (reusable blocks refactor) #5228
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
e53d036
Revert "Blocks: Disable Convert to Reusable for nested blocks"
aduth e0ea667
Block: Move block context provides into BlockEdit
aduth 8501e7d
Blocks: Avoid dismissing editable controls
aduth edc7681
State: Enhance history reducer utils to support ignores
aduth 928c2d1
Block List: Hide disabled default block appender
aduth 7aff115
State: Update reusable blocks as pointers to state blocks
aduth f7dabde
Block List: Inject inner block list creator as function
aduth d5da664
Typo: saved -> fetched
noisysocks 4aac14a
Typo: preferencially -> preferentially
noisysocks b4afb64
Explain why dispatching RECEIVE_BLOCKS is necessary
noisysocks b2f16d0
Remove unnecessary getState variable
noisysocks 04bfd26
Update getInserterItems to respect new reusable block data layout
noisysocks c169500
Provide createInnerBlockList context in BlockPreview
noisysocks 2ede29e
Fix flash of 'Not found' error message when fetching a reusable block
noisysocks 1b3f1ba
When converting reusable -> regular, replace the block with a copy
noisysocks 4025b7a
Avoid iterating through reusable blocks twice
noisysocks 3f0180c
Move createInnerBlockList into `editor/utils`
noisysocks 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 |
---|---|---|
@@ -1,14 +1,14 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { pickBy, noop } from 'lodash'; | ||
import { connect } from 'react-redux'; | ||
import { noop, partial } from 'lodash'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { Component, Fragment } from '@wordpress/element'; | ||
import { Component, Fragment, compose } from '@wordpress/element'; | ||
import { Placeholder, Spinner, Disabled } from '@wordpress/components'; | ||
import { withSelect, withDispatch } from '@wordpress/data'; | ||
import { __ } from '@wordpress/i18n'; | ||
|
||
/** | ||
|
@@ -25,12 +25,11 @@ class ReusableBlockEdit extends Component { | |
this.stopEditing = this.stopEditing.bind( this ); | ||
this.setAttributes = this.setAttributes.bind( this ); | ||
this.setTitle = this.setTitle.bind( this ); | ||
this.updateReusableBlock = this.updateReusableBlock.bind( this ); | ||
this.save = this.save.bind( this ); | ||
|
||
this.state = { | ||
isEditing: !! ( reusableBlock && reusableBlock.isTemporary ), | ||
title: null, | ||
attributes: null, | ||
}; | ||
} | ||
|
||
|
@@ -40,68 +39,63 @@ class ReusableBlockEdit extends Component { | |
} | ||
} | ||
|
||
componentWillReceiveProps( nextProps ) { | ||
if ( this.props.focus && ! nextProps.focus ) { | ||
this.stopEditing(); | ||
} | ||
} | ||
|
||
startEditing() { | ||
this.setState( { isEditing: true } ); | ||
const { reusableBlock } = this.props; | ||
|
||
this.setState( { | ||
isEditing: true, | ||
title: reusableBlock.title, | ||
} ); | ||
} | ||
|
||
stopEditing() { | ||
this.setState( { | ||
isEditing: false, | ||
title: null, | ||
attributes: null, | ||
} ); | ||
} | ||
|
||
setAttributes( attributes ) { | ||
this.setState( ( prevState ) => ( { | ||
attributes: { ...prevState.attributes, ...attributes }, | ||
} ) ); | ||
const { updateAttributes, block } = this.props; | ||
updateAttributes( block.uid, attributes ); | ||
} | ||
|
||
setTitle( title ) { | ||
this.setState( { title } ); | ||
} | ||
|
||
updateReusableBlock() { | ||
const { title, attributes } = this.state; | ||
save() { | ||
const { reusableBlock, onUpdateTitle, onSave } = this.props; | ||
|
||
// Use pickBy to include only changed (assigned) values in payload | ||
const payload = pickBy( { | ||
title, | ||
attributes, | ||
} ); | ||
const { title } = this.state; | ||
if ( title !== reusableBlock.title ) { | ||
onUpdateTitle( title ); | ||
} | ||
|
||
onSave(); | ||
|
||
this.props.updateReusableBlock( payload ); | ||
this.props.saveReusableBlock(); | ||
this.stopEditing(); | ||
} | ||
|
||
render() { | ||
const { isSelected, reusableBlock, isFetching, isSaving } = this.props; | ||
const { isEditing, title, attributes } = this.state; | ||
const { isSelected, reusableBlock, block, isFetching, isSaving } = this.props; | ||
const { isEditing, title } = this.state; | ||
|
||
if ( ! reusableBlock && isFetching ) { | ||
return <Placeholder><Spinner /></Placeholder>; | ||
} | ||
|
||
if ( ! reusableBlock ) { | ||
if ( ! reusableBlock || ! block ) { | ||
return <Placeholder>{ __( 'Block has been deleted or is unavailable.' ) }</Placeholder>; | ||
} | ||
|
||
const reusableBlockAttributes = { ...reusableBlock.attributes, ...attributes }; | ||
|
||
let element = ( | ||
<BlockEdit | ||
{ ...this.props } | ||
name={ reusableBlock.type } | ||
isSelected={ isEditing && isSelected } | ||
attributes={ reusableBlockAttributes } | ||
id={ block.uid } | ||
name={ block.name } | ||
attributes={ block.attributes } | ||
setAttributes={ isEditing ? this.setAttributes : noop } | ||
/> | ||
); | ||
|
@@ -113,14 +107,14 @@ class ReusableBlockEdit extends Component { | |
return ( | ||
<Fragment> | ||
{ element } | ||
{ isSelected && ( | ||
{ ( isSelected || isEditing ) && ( | ||
<ReusableBlockEditPanel | ||
isEditing={ isEditing } | ||
title={ title !== null ? title : reusableBlock.title } | ||
isSaving={ isSaving && ! reusableBlock.isTemporary } | ||
onEdit={ this.startEditing } | ||
onChangeTitle={ this.setTitle } | ||
onSave={ this.updateReusableBlock } | ||
onSave={ this.save } | ||
onCancel={ this.stopEditing } | ||
/> | ||
) } | ||
|
@@ -129,34 +123,41 @@ class ReusableBlockEdit extends Component { | |
} | ||
} | ||
|
||
const ConnectedReusableBlockEdit = connect( | ||
( state, ownProps ) => ( { | ||
reusableBlock: state.reusableBlocks.data[ ownProps.attributes.ref ], | ||
isFetching: state.reusableBlocks.isFetching[ ownProps.attributes.ref ], | ||
isSaving: state.reusableBlocks.isSaving[ ownProps.attributes.ref ], | ||
const EnhancedReusableBlockEdit = compose( [ | ||
withSelect( ( select, ownProps ) => { | ||
const { | ||
getReusableBlock, | ||
isFetchingReusableBlock, | ||
isSavingReusableBlock, | ||
getBlock, | ||
} = select( 'core/editor' ); | ||
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 is great ❤️ |
||
const { ref } = ownProps.attributes; | ||
const reusableBlock = getReusableBlock( ref ); | ||
|
||
return { | ||
reusableBlock, | ||
isFetching: isFetchingReusableBlock( ref ), | ||
isSaving: isSavingReusableBlock( ref ), | ||
block: reusableBlock ? getBlock( reusableBlock.uid ) : null, | ||
}; | ||
} ), | ||
( dispatch, ownProps ) => ( { | ||
fetchReusableBlock() { | ||
dispatch( { | ||
type: 'FETCH_REUSABLE_BLOCKS', | ||
id: ownProps.attributes.ref, | ||
} ); | ||
}, | ||
updateReusableBlock( reusableBlock ) { | ||
dispatch( { | ||
type: 'UPDATE_REUSABLE_BLOCK', | ||
id: ownProps.attributes.ref, | ||
reusableBlock, | ||
} ); | ||
}, | ||
saveReusableBlock() { | ||
dispatch( { | ||
type: 'SAVE_REUSABLE_BLOCK', | ||
id: ownProps.attributes.ref, | ||
} ); | ||
}, | ||
} ) | ||
)( ReusableBlockEdit ); | ||
withDispatch( ( dispatch, ownProps ) => { | ||
const { | ||
fetchReusableBlocks, | ||
updateBlockAttributes, | ||
updateReusableBlockTitle, | ||
saveReusableBlock, | ||
} = dispatch( 'core/editor' ); | ||
const { ref } = ownProps.attributes; | ||
|
||
return { | ||
fetchReusableBlock: partial( fetchReusableBlocks, ref ), | ||
updateAttributes: updateBlockAttributes, | ||
onUpdateTitle: partial( updateReusableBlockTitle, ref ), | ||
onSave: partial( saveReusableBlock, ref ), | ||
}; | ||
} ), | ||
] )( ReusableBlockEdit ); | ||
|
||
export const name = 'core/block'; | ||
|
||
|
@@ -176,6 +177,6 @@ export const settings = { | |
html: false, | ||
}, | ||
|
||
edit: ConnectedReusableBlockEdit, | ||
edit: EnhancedReusableBlockEdit, | ||
save: () => null, | ||
}; |
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.
I've seen this pattern in a few places now. I wonder if it's worth encapsulating this in a new
withUserCapabilities
HOC?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.
Probably not necessary for now.
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.
With #5219, I might imagine this could be a simple selector on a core data module.