-
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
Block Switcher: Adding the transformations API and the block switcher #429
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
c013f29
Block Switcher: Addjng the transformations API and the block switcher
youknowriad 3e8b5b5
Updating the transformations API to allow transformations from and to…
youknowriad 9380c38
Block Switcher: Giving priority to the "to" transformation
youknowriad 0c8e6b4
Fix switching text blocks to headings
nylen 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
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,98 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { connect } from 'react-redux'; | ||
import { uniq, get, reduce } from 'lodash'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import './style.scss'; | ||
import IconButton from 'components/icon-button'; | ||
|
||
class BlockSwitcher extends wp.element.Component { | ||
constructor() { | ||
super( ...arguments ); | ||
this.toggleMenu = this.toggleMenu.bind( this ); | ||
this.state = { | ||
open: false | ||
}; | ||
} | ||
|
||
toggleMenu() { | ||
this.setState( { | ||
open: ! this.state.open | ||
} ); | ||
} | ||
|
||
switchBlockType( blockType ) { | ||
return () => { | ||
this.setState( { | ||
open: false | ||
} ); | ||
this.props.onTransform( this.props.block, blockType ); | ||
}; | ||
} | ||
|
||
render() { | ||
const blockSettings = wp.blocks.getBlockSettings( this.props.block.blockType ); | ||
const blocksToBeTransformedFrom = reduce( wp.blocks.getBlocks(), ( memo, block ) => { | ||
const transformFrom = get( block, 'transforms.from', [] ); | ||
const transformation = transformFrom.find( t => t.blocks.indexOf( this.props.block.blockType ) !== -1 ); | ||
return transformation ? memo.concat( [ block.slug ] ) : memo; | ||
}, [] ); | ||
const blocksToBeTransformedTo = get( blockSettings, 'transforms.to', [] ) | ||
.reduce( ( memo, transformation ) => memo.concat( transformation.blocks ), [] ); | ||
const allowedBlocks = uniq( blocksToBeTransformedFrom.concat( blocksToBeTransformedTo ) ) | ||
.reduce( ( memo, blockType ) => { | ||
const block = wp.blocks.getBlockSettings( blockType ); | ||
return !! block ? memo.concat( block ) : memo; | ||
}, [] ); | ||
|
||
if ( ! allowedBlocks.length ) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<div className="editor-block-switcher"> | ||
<IconButton | ||
className="editor-block-switcher__toggle" | ||
icon={ blockSettings.icon } | ||
onClick={ this.toggleMenu } | ||
> | ||
<div className="editor-block-switcher__arrow" /> | ||
</IconButton> | ||
{ this.state.open && | ||
<div className="editor-block-switcher__menu"> | ||
<div className="editor-block-switcher__menu-arrow" /> | ||
{ allowedBlocks.map( ( { slug, title, icon } ) => ( | ||
<IconButton | ||
key={ slug } | ||
onClick={ this.switchBlockType( slug ) } | ||
className="editor-block-switcher__menu-item" | ||
icon={ icon } | ||
> | ||
{ title } | ||
</IconButton> | ||
) ) } | ||
</div> | ||
} | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
export default connect( | ||
( state, ownProps ) => ( { | ||
block: state.blocks.byUid[ ownProps.uid ] | ||
} ), | ||
( dispatch, ownProps ) => ( { | ||
onTransform( block, blockType ) { | ||
dispatch( { | ||
type: 'SWITCH_BLOCK_TYPE', | ||
uid: ownProps.uid, | ||
block: wp.blocks.switchToBlockType( block, blockType ) | ||
} ); | ||
} | ||
} ) | ||
)( BlockSwitcher ); |
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.
Minor: Thinking about conventions of property order, I quite like in a typical React component the
render
is usually the last member of the class, and had thought we could do similar withedit
/save
, having other properties occur earlier. Do you think there's any value in this or another convention?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, I always forget about this convention sorry. Will fix later