inspector-controls
4 years ago
AlignmentControls.js
4 years ago
AnchorTag.js
4 years ago
BlockControls.js
4 years ago
ComponentCSS.js
4 years ago
Image.js
4 years ago
ImageContentRenderer.js
4 years ago
ImagePlaceholder.js
4 years ago
InspectorControls.js
4 years ago
AlignmentControls.js
128 lines
| 1 | import { store as blockEditorStore } from '@wordpress/block-editor'; |
| 2 | import { ToolbarGroup } from '@wordpress/components'; |
| 3 | import { __ } from '@wordpress/i18n'; |
| 4 | import { useSelect } from '@wordpress/data'; |
| 5 | import getAttribute from '../../../utils/get-attribute'; |
| 6 | import { |
| 7 | alignLeft, |
| 8 | alignRight, |
| 9 | alignCenter, |
| 10 | positionLeft, |
| 11 | positionRight, |
| 12 | stretchFullWidth, |
| 13 | stretchWide, |
| 14 | } from '@wordpress/icons'; |
| 15 | |
| 16 | export default function AlignmentControls( props ) { |
| 17 | const { |
| 18 | attributes, |
| 19 | setAttributes, |
| 20 | deviceType, |
| 21 | } = props; |
| 22 | |
| 23 | const { align } = attributes; |
| 24 | |
| 25 | const { wideControlsEnabled = false } = useSelect( |
| 26 | ( select ) => { |
| 27 | const { getSettings } = select( blockEditorStore ); |
| 28 | const settings = getSettings(); |
| 29 | return { |
| 30 | wideControlsEnabled: settings.alignWide, |
| 31 | }; |
| 32 | }, |
| 33 | [] |
| 34 | ); |
| 35 | |
| 36 | const selectedIcons = { |
| 37 | left: alignLeft, |
| 38 | center: alignCenter, |
| 39 | right: alignRight, |
| 40 | floatLeft: positionLeft, |
| 41 | floatRight: positionRight, |
| 42 | wide: stretchWide, |
| 43 | full: stretchFullWidth, |
| 44 | }; |
| 45 | |
| 46 | const selectedIcon = |
| 47 | align |
| 48 | ? selectedIcons[ align ] |
| 49 | : selectedIcons[ getAttribute( 'alignment', props ) ] || |
| 50 | alignLeft; |
| 51 | |
| 52 | const updateAlignment = ( value ) => setAttributes( { |
| 53 | [ getAttribute( 'alignment', props, true ) ]: |
| 54 | value !== getAttribute( 'alignment', props ) |
| 55 | ? value |
| 56 | : '', |
| 57 | align: '', |
| 58 | } ); |
| 59 | |
| 60 | const alignmentOptions = [ |
| 61 | { |
| 62 | icon: alignLeft, |
| 63 | title: __( 'Align left', 'generateblocks' ), |
| 64 | onClick: () => updateAlignment( 'left' ), |
| 65 | isActive: 'left' === getAttribute( 'alignment', props ), |
| 66 | }, |
| 67 | { |
| 68 | icon: alignCenter, |
| 69 | title: __( 'Align center', 'generateblocks' ), |
| 70 | onClick: () => updateAlignment( 'center' ), |
| 71 | isActive: 'center' === getAttribute( 'alignment', props ), |
| 72 | }, |
| 73 | { |
| 74 | icon: alignRight, |
| 75 | title: __( 'Align right', 'generateblocks' ), |
| 76 | onClick: () => updateAlignment( 'right' ), |
| 77 | isActive: 'right' === getAttribute( 'alignment', props ), |
| 78 | }, |
| 79 | { |
| 80 | icon: positionLeft, |
| 81 | title: __( 'Float left', 'generateblocks' ), |
| 82 | onClick: () => updateAlignment( 'floatLeft' ), |
| 83 | isActive: 'floatLeft' === getAttribute( 'alignment', props ), |
| 84 | }, |
| 85 | { |
| 86 | icon: positionRight, |
| 87 | title: __( 'Float right', 'generateblocks' ), |
| 88 | onClick: () => updateAlignment( 'floatRight' ), |
| 89 | isActive: 'floatRight' === getAttribute( 'alignment', props ), |
| 90 | }, |
| 91 | ]; |
| 92 | |
| 93 | if ( wideControlsEnabled && 'Desktop' === deviceType ) { |
| 94 | alignmentOptions.push( |
| 95 | { |
| 96 | icon: stretchWide, |
| 97 | title: __( 'Wide width', 'generateblocks' ), |
| 98 | onClick: () => setAttributes( { |
| 99 | align: 'wide' !== align ? 'wide' : '', |
| 100 | alignment: '', |
| 101 | alignmentTablet: '', |
| 102 | alignmentMobile: '', |
| 103 | } ), |
| 104 | isActive: 'wide' === align, |
| 105 | }, |
| 106 | { |
| 107 | icon: stretchFullWidth, |
| 108 | title: __( 'Full width', 'generateblocks' ), |
| 109 | onClick: () => setAttributes( { |
| 110 | align: 'full' !== align ? 'full' : '', |
| 111 | alignment: '', |
| 112 | alignmentTablet: '', |
| 113 | alignmentMobile: '', |
| 114 | } ), |
| 115 | isActive: 'full' === align, |
| 116 | }, |
| 117 | ); |
| 118 | } |
| 119 | |
| 120 | return ( |
| 121 | <ToolbarGroup |
| 122 | isCollapsed={ true } |
| 123 | icon={ selectedIcon } |
| 124 | controls={ alignmentOptions } |
| 125 | /> |
| 126 | ); |
| 127 | } |
| 128 |