index.js
128 lines
| 1 | import { InspectorControls } from '@wordpress/block-editor'; |
| 2 | import { useContext, useState, useEffect } from '@wordpress/element'; |
| 3 | |
| 4 | import ControlsContext from '../../block-context'; |
| 5 | import ResponsiveTabs from './controls/responsive-tabs'; |
| 6 | import TypographyControls from './controls/typography'; |
| 7 | import SpacingControls from './controls/spacing'; |
| 8 | import BorderControls from './controls/borders'; |
| 9 | import ColorsControls from './controls/colors'; |
| 10 | import IconControls from './controls/icon'; |
| 11 | import SettingsPanel from './controls/settings-panel'; |
| 12 | import BackgroundPanel from './controls/background-panel'; |
| 13 | import ShapesPanel from './controls/shapes-panel'; |
| 14 | import LayoutControls from './controls/layout'; |
| 15 | import SizingControls from './controls/sizing'; |
| 16 | import getDeviceType from '../../utils/get-device-type'; |
| 17 | import { useSelectedBlockElement } from '../../hooks'; |
| 18 | |
| 19 | export default function GenerateBlocksInspectorControls( { attributes, setAttributes, children } ) { |
| 20 | const device = getDeviceType(); |
| 21 | const { |
| 22 | supports: { |
| 23 | responsiveTabs, |
| 24 | settingsPanel, |
| 25 | layout, |
| 26 | typography, |
| 27 | spacing, |
| 28 | borders, |
| 29 | colors, |
| 30 | backgroundPanel, |
| 31 | shapesPanel, |
| 32 | icon, |
| 33 | sizingPanel, |
| 34 | }, |
| 35 | } = useContext( ControlsContext ); |
| 36 | const selectedBlockElement = useSelectedBlockElement(); |
| 37 | const selectedBlockElementClientId = selectedBlockElement ? selectedBlockElement?.dataset.block : 0; |
| 38 | const [ computedStyles, setComputedStyles ] = useState( {} ); |
| 39 | |
| 40 | useEffect( () => { |
| 41 | if ( selectedBlockElement ) { |
| 42 | setComputedStyles( getComputedStyle( selectedBlockElement ) ); |
| 43 | } |
| 44 | }, [ selectedBlockElementClientId ] ); |
| 45 | |
| 46 | return ( |
| 47 | <InspectorControls> |
| 48 | { responsiveTabs && <ResponsiveTabs /> } |
| 49 | |
| 50 | { settingsPanel.enabled && !! children && |
| 51 | <SettingsPanel>{ children }</SettingsPanel> |
| 52 | } |
| 53 | |
| 54 | { layout.enabled && |
| 55 | <LayoutControls |
| 56 | attributes={ attributes } |
| 57 | setAttributes={ setAttributes } |
| 58 | computedStyles={ computedStyles } |
| 59 | /> |
| 60 | } |
| 61 | |
| 62 | { sizingPanel.enabled && |
| 63 | <SizingControls |
| 64 | attributes={ attributes } |
| 65 | setAttributes={ setAttributes } |
| 66 | computedStyles={ computedStyles } |
| 67 | /> |
| 68 | } |
| 69 | |
| 70 | { spacing.enabled && |
| 71 | <SpacingControls |
| 72 | attributes={ attributes } |
| 73 | setAttributes={ setAttributes } |
| 74 | computedStyles={ computedStyles } |
| 75 | /> |
| 76 | } |
| 77 | |
| 78 | { borders.enabled && |
| 79 | <BorderControls |
| 80 | attributes={ attributes } |
| 81 | setAttributes={ setAttributes } |
| 82 | computedStyles={ computedStyles } |
| 83 | /> |
| 84 | } |
| 85 | |
| 86 | { typography.enabled && ! attributes.removeText && |
| 87 | <TypographyControls |
| 88 | attributes={ attributes } |
| 89 | setAttributes={ setAttributes } |
| 90 | computedStyles={ computedStyles } |
| 91 | /> |
| 92 | } |
| 93 | |
| 94 | { colors.enabled && 'Desktop' === device && |
| 95 | <ColorsControls |
| 96 | attributes={ attributes } |
| 97 | setAttributes={ setAttributes } |
| 98 | computedStyles={ computedStyles } |
| 99 | /> |
| 100 | } |
| 101 | |
| 102 | { backgroundPanel.enabled && 'Desktop' === device && |
| 103 | <BackgroundPanel |
| 104 | attributes={ attributes } |
| 105 | setAttributes={ setAttributes } |
| 106 | computedStyles={ computedStyles } |
| 107 | /> |
| 108 | } |
| 109 | |
| 110 | { shapesPanel.enabled && ( 'Desktop' === device || attributes?.shapeDividers.length > 0 ) && |
| 111 | <ShapesPanel |
| 112 | attributes={ attributes } |
| 113 | setAttributes={ setAttributes } |
| 114 | computedStyles={ computedStyles } |
| 115 | /> |
| 116 | } |
| 117 | |
| 118 | { icon.enabled && ( 'Desktop' === device || !! attributes.icon ) && |
| 119 | <IconControls |
| 120 | attributes={ attributes } |
| 121 | setAttributes={ setAttributes } |
| 122 | computedStyles={ computedStyles } |
| 123 | /> |
| 124 | } |
| 125 | </InspectorControls> |
| 126 | ); |
| 127 | } |
| 128 |