-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1571 from tomusborne/feature/add-alignment-toolbar
Feature: Add alignment toolbar
- Loading branch information
Showing
10 changed files
with
244 additions
and
14 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
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,11 +0,0 @@ | ||
.wp-block-generateblocks-media { | ||
display: block; | ||
|
||
&:not(.is-selected) { | ||
display: none; | ||
} | ||
|
||
&.is-selected + [class*="gb-media"] { | ||
display: none; | ||
} | ||
} | ||
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 @@ | ||
import { __ } from '@wordpress/i18n'; | ||
import { ToolbarButton, Dropdown, MenuGroup, MenuItem } from '@wordpress/components'; | ||
import { BlockControls } from '@wordpress/block-editor'; | ||
import { alignLeft, alignCenter, alignRight, alignJustify } from '@wordpress/icons'; | ||
import { useBlockStyles } from '@hooks/useBlockStyles'; | ||
import { BlockWidth } from './BlockWidth'; | ||
|
||
const POPOVER_PROPS = { | ||
position: 'bottom right', | ||
}; | ||
|
||
export function AlignmentToolbar( { | ||
getStyleValue, | ||
onStyleChange, | ||
align, | ||
setAttributes, | ||
clientId, | ||
withTextAlign = false, | ||
withBlockWidth = false, | ||
} ) { | ||
const { | ||
currentAtRule, | ||
} = useBlockStyles(); | ||
|
||
function getAlignmentIcon() { | ||
if ( withTextAlign ) { | ||
const alignment = getStyleValue( 'textAlign', currentAtRule ); | ||
|
||
switch ( alignment ) { | ||
case 'center': | ||
return alignCenter; | ||
case 'right': | ||
return alignRight; | ||
case 'justify': | ||
return alignJustify; | ||
case 'left': | ||
default: | ||
return alignLeft; | ||
} | ||
} | ||
} | ||
|
||
const textAlignments = [ | ||
{ | ||
icon: alignLeft, | ||
value: 'left', | ||
label: __( 'Left', 'generateblocks' ), | ||
}, | ||
{ | ||
icon: alignCenter, | ||
value: 'center', | ||
label: __( 'Center', 'generateblocks' ), | ||
}, | ||
{ | ||
icon: alignRight, | ||
value: 'right', | ||
label: __( 'Right', 'generateblocks' ), | ||
}, | ||
{ | ||
icon: alignJustify, | ||
value: 'justify', | ||
label: __( 'Justify', 'generateblocks' ), | ||
}, | ||
]; | ||
|
||
if ( ! withTextAlign && ! withBlockWidth ) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<BlockControls group="inline"> | ||
<Dropdown | ||
popoverProps={ POPOVER_PROPS } | ||
renderToggle={ ( { isOpen, onToggle } ) => ( | ||
<ToolbarButton | ||
icon={ getAlignmentIcon() } | ||
label={ __( 'Alignment', 'generateblocks' ) } | ||
onClick={ onToggle } | ||
aria-expanded={ isOpen } | ||
isPressed={ !! isOpen } | ||
/> | ||
) } | ||
renderContent={ () => ( | ||
<> | ||
{ !! withTextAlign && ( | ||
<MenuGroup label={ __( 'Text Alignment', 'generateblocks' ) }> | ||
{ textAlignments.map( ( { icon, value, label } ) => ( | ||
<MenuItem | ||
key={ value } | ||
icon={ icon } | ||
isPressed={ value === getStyleValue( 'textAlign', currentAtRule ) } | ||
onClick={ () => { | ||
if ( value === getStyleValue( 'textAlign', currentAtRule ) ) { | ||
onStyleChange( 'textAlign', '', currentAtRule ); | ||
return; | ||
} | ||
|
||
onStyleChange( 'textAlign', value, currentAtRule ); | ||
} } | ||
> | ||
{ label } | ||
</MenuItem> | ||
) ) } | ||
</MenuGroup> | ||
) } | ||
|
||
{ !! withBlockWidth && '' === currentAtRule && undefined !== align && ( | ||
<BlockWidth | ||
align={ align } | ||
onChange={ ( value ) => setAttributes( { align: value } ) } | ||
clientId={ clientId } | ||
/> | ||
) } | ||
</> | ||
) } | ||
/> | ||
</BlockControls> | ||
); | ||
} |
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,73 @@ | ||
import { __ } from '@wordpress/i18n'; | ||
import { MenuGroup, MenuItem } from '@wordpress/components'; | ||
import { useSelect } from '@wordpress/data'; | ||
import { store as blockEditorStore, useSetting } from '@wordpress/block-editor'; | ||
import { alignNone, stretchWide, stretchFullWidth } from '@wordpress/icons'; | ||
import { applyFilters } from '@wordpress/hooks'; | ||
|
||
export function BlockWidth( { align, onChange, clientId } ) { | ||
const hasWideSize = useSetting( 'layout.wideSize' ); | ||
const { themeSupportsAlignWide } = useSelect( | ||
( select ) => { | ||
const { getSettings } = select( blockEditorStore ); | ||
return { | ||
themeSupportsAlignWide: getSettings()?.alignWide, | ||
}; | ||
}, | ||
[] | ||
); | ||
const { | ||
getBlockRootClientId, | ||
} = useSelect( ( select ) => select( blockEditorStore ), [] ); | ||
|
||
const removeBlockWidthOptions = applyFilters( | ||
'generateblocks.editor.removeBlockWidthOptions', | ||
false, | ||
{ clientId } | ||
); | ||
|
||
if ( ( ! themeSupportsAlignWide && ! hasWideSize ) || removeBlockWidthOptions ) { | ||
return null; | ||
} | ||
|
||
const parentBlockId = getBlockRootClientId( clientId ); | ||
|
||
if ( parentBlockId ) { | ||
return null; | ||
} | ||
|
||
const options = [ | ||
{ | ||
label: __( 'Default', 'generateblocks' ), | ||
value: '', | ||
icon: alignNone, | ||
}, | ||
{ | ||
label: __( 'Wide', 'generateblocks' ), | ||
value: 'wide', | ||
icon: stretchWide, | ||
}, | ||
{ | ||
label: __( 'Full', 'generateblocks' ), | ||
value: 'full', | ||
icon: stretchFullWidth, | ||
}, | ||
]; | ||
|
||
return ( | ||
<MenuGroup label={ __( 'Block Width', 'generateblocks' ) }> | ||
{ options.map( ( option ) => { | ||
return ( | ||
<MenuItem | ||
key={ option.value } | ||
isPressed={ align === option.value } | ||
onClick={ () => onChange( option.value ) } | ||
icon={ option.icon } | ||
> | ||
{ option.label } | ||
</MenuItem> | ||
); | ||
} ) } | ||
</MenuGroup> | ||
); | ||
} |
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