-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
We decided that transforms should be a primary action, and we will iterate further to have transforms fit nicely in the toolbars.
- Loading branch information
1 parent
98859d5
commit 8729720
Showing
10 changed files
with
428 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __ } from '@wordpress/i18n'; | ||
import { Dropdown, Dashicon, IconButton, Toolbar, NavigableMenu } from '@wordpress/components'; | ||
import { getBlockType, getPossibleBlockTransformations, switchToBlockType, BlockIcon, withEditorSettings } from '@wordpress/blocks'; | ||
import { compose } from '@wordpress/element'; | ||
import { keycodes } from '@wordpress/utils'; | ||
import { withSelect, withDispatch } from '@wordpress/data'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import './style.scss'; | ||
|
||
/** | ||
* Module Constants | ||
*/ | ||
const { DOWN } = keycodes; | ||
|
||
export function BlockSwitcher( { blocks, onTransform, isLocked } ) { | ||
const allowedBlocks = getPossibleBlockTransformations( blocks ); | ||
|
||
if ( isLocked || ! allowedBlocks.length ) { | ||
return null; | ||
} | ||
|
||
const sourceBlockName = blocks[ 0 ].name; | ||
const blockType = getBlockType( sourceBlockName ); | ||
|
||
return ( | ||
<Dropdown | ||
className="editor-block-switcher" | ||
contentClassName="editor-block-switcher__popover" | ||
renderToggle={ ( { onToggle, isOpen } ) => { | ||
const openOnArrowDown = ( event ) => { | ||
if ( ! isOpen && event.keyCode === DOWN ) { | ||
event.preventDefault(); | ||
event.stopPropagation(); | ||
onToggle(); | ||
} | ||
}; | ||
const label = __( 'Change block type' ); | ||
|
||
return ( | ||
<Toolbar> | ||
<IconButton | ||
className="editor-block-switcher__toggle" | ||
icon={ <BlockIcon icon={ blockType.icon } /> } | ||
onClick={ onToggle } | ||
aria-haspopup="true" | ||
aria-expanded={ isOpen } | ||
label={ label } | ||
tooltip={ label } | ||
onKeyDown={ openOnArrowDown } | ||
> | ||
<Dashicon icon="arrow-down" /> | ||
</IconButton> | ||
</Toolbar> | ||
); | ||
} } | ||
renderContent={ ( { onClose } ) => ( | ||
<div> | ||
<span | ||
className="editor-block-switcher__menu-title" | ||
> | ||
{ __( 'Transform into:' ) } | ||
</span> | ||
<NavigableMenu | ||
role="menu" | ||
aria-label={ __( 'Block types' ) } | ||
> | ||
{ allowedBlocks.map( ( { name, title, icon } ) => ( | ||
<IconButton | ||
key={ name } | ||
onClick={ () => { | ||
onTransform( blocks, name ); | ||
onClose(); | ||
} } | ||
className="editor-block-switcher__menu-item" | ||
icon={ ( | ||
<span className="editor-block-switcher__block-icon"> | ||
<BlockIcon icon={ icon } /> | ||
</span> | ||
) } | ||
role="menuitem" | ||
> | ||
{ title } | ||
</IconButton> | ||
) ) } | ||
</NavigableMenu> | ||
</div> | ||
) } | ||
/> | ||
); | ||
} | ||
|
||
export default compose( | ||
withSelect( ( select, ownProps ) => { | ||
return { | ||
blocks: ownProps.uids.map( ( uid ) => select( 'core/editor' ).getBlock( uid ) ), | ||
}; | ||
} ), | ||
withDispatch( ( dispatch, ownProps ) => ( { | ||
onTransform( blocks, name ) { | ||
dispatch( 'core/editor' ).replaceBlocks( | ||
ownProps.uids, | ||
switchToBlockType( blocks, name ) | ||
); | ||
}, | ||
} ) ), | ||
withEditorSettings( ( settings ) => { | ||
const { templateLock } = settings; | ||
|
||
return { | ||
isLocked: !! templateLock, | ||
}; | ||
} ), | ||
)( BlockSwitcher ); |
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,29 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { withSelect } from '@wordpress/data'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import './style.scss'; | ||
import BlockSwitcher from './'; | ||
|
||
export function MultiBlocksSwitcher( { isMultiBlockSelection, selectedBlockUids } ) { | ||
if ( ! isMultiBlockSelection ) { | ||
return null; | ||
} | ||
return ( | ||
<BlockSwitcher key="switcher" uids={ selectedBlockUids } /> | ||
); | ||
} | ||
|
||
export default withSelect( | ||
( select ) => { | ||
const selectedBlockUids = select( 'core/editor' ).getMultiSelectedBlockUids(); | ||
return { | ||
isMultiBlockSelection: selectedBlockUids.length > 1, | ||
selectedBlockUids, | ||
}; | ||
} | ||
)( MultiBlocksSwitcher ); |
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,52 @@ | ||
.editor-block-switcher { | ||
position: relative; | ||
|
||
.components-toolbar { | ||
border-left: none; | ||
} | ||
} | ||
|
||
.editor-block-switcher__toggle { | ||
width: auto; | ||
margin: 0; | ||
padding: 8px; | ||
border-radius: 0; | ||
|
||
&:focus:before { | ||
top: -3px; | ||
right: -3px; | ||
bottom: -3px; | ||
left: -3px; | ||
} | ||
} | ||
|
||
.editor-block-switcher__popover .components-popover__content { | ||
width: 200px; | ||
} | ||
|
||
.editor-block-switcher__menu { | ||
box-shadow: $shadow-popover; | ||
border: 1px solid $light-gray-500; | ||
background: $white; | ||
padding: 3px 3px 0; | ||
} | ||
|
||
.editor-block-switcher__menu-title { | ||
display: block; | ||
padding: 6px; | ||
color: $dark-gray-300; | ||
} | ||
|
||
.editor-block-switcher__menu-item { | ||
color: $dark-gray-500; | ||
display: flex; | ||
align-items: center; | ||
width: 100%; | ||
padding: 6px; | ||
text-align: left; | ||
|
||
.editor-block-switcher__block-icon { | ||
margin-right: 8px; | ||
height: 20px; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
editor/components/block-switcher/test/__snapshots__/index.js.snap
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,10 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`BlockSwitcher should render switcher with blocks 1`] = ` | ||
<Dropdown | ||
className="editor-block-switcher" | ||
contentClassName="editor-block-switcher__popover" | ||
renderContent={[Function]} | ||
renderToggle={[Function]} | ||
/> | ||
`; |
13 changes: 13 additions & 0 deletions
13
editor/components/block-switcher/test/__snapshots__/multi-blocks-switcher.js.snap
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,13 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`MultiBlocksSwitcher should return a BlockSwitcher element matching the snapshot. 1`] = ` | ||
<WithSelect(WithDispatch(WithEditorSettings(BlockSwitcher))) | ||
key="switcher" | ||
uids={ | ||
Array [ | ||
"an-uid", | ||
"another-uid", | ||
] | ||
} | ||
/> | ||
`; |
Oops, something went wrong.