components
3 years ago
css
3 years ago
attributes.js
3 years ago
block.js
3 years ago
deprecated.js
5 years ago
edit.js
3 years ago
editor.scss
3 years ago
save.js
3 years ago
edit.js
120 lines
| 1 | import BlockControls from './components/BlockControls'; |
| 2 | import InspectorAdvancedControls from './components/InspectorAdvancedControls'; |
| 3 | import ComponentCSS from './components/ComponentCSS'; |
| 4 | import GoogleFontLink from '../../components/google-font-link'; |
| 5 | import { Fragment, useRef, useState, useEffect } from '@wordpress/element'; |
| 6 | import { compose } from '@wordpress/compose'; |
| 7 | import { withButtonLegacyMigration, withDeviceType, withUniqueId } from '../../hoc'; |
| 8 | import withDynamicContent from '../../extend/dynamic-content/hoc/withDynamicContent'; |
| 9 | import ButtonContentRenderer from './components/ButtonContentRenderer'; |
| 10 | import wasBlockJustInserted from '../../utils/was-block-just-inserted'; |
| 11 | import { useSelect } from '@wordpress/data'; |
| 12 | import { withBlockContext } from '../../block-context'; |
| 13 | import GenerateBlocksInspectorControls from '../../extend/inspector-control'; |
| 14 | import { applyFilters } from '@wordpress/hooks'; |
| 15 | import getDeviceType from '../../utils/get-device-type'; |
| 16 | import './components/ConditionalColors'; |
| 17 | |
| 18 | const ButtonEdit = ( props ) => { |
| 19 | const { |
| 20 | attributes, |
| 21 | setAttributes, |
| 22 | ContentRenderer = ButtonContentRenderer, |
| 23 | clientId, |
| 24 | } = props; |
| 25 | |
| 26 | const { |
| 27 | anchor, |
| 28 | ariaLabel, |
| 29 | fontFamily, |
| 30 | googleFont, |
| 31 | googleFontVariants, |
| 32 | isBlockPreview = false, |
| 33 | hasButtonContainer, |
| 34 | blockVersion, |
| 35 | buttonType, |
| 36 | variantRole, |
| 37 | } = attributes; |
| 38 | |
| 39 | const ref = useRef( null ); |
| 40 | const [ computedStyles, setComputedStyles ] = useState( {} ); |
| 41 | const deviceType = getDeviceType(); |
| 42 | const { |
| 43 | getBlockParents, |
| 44 | getBlocksByClientId, |
| 45 | } = useSelect( ( select ) => select( 'core/block-editor' ), [] ); |
| 46 | |
| 47 | useEffect( () => { |
| 48 | const computedButtonStyles = getComputedStyle( ref.current ); |
| 49 | |
| 50 | setComputedStyles( { |
| 51 | fontSize: parseInt( computedButtonStyles.fontSize ) || '', |
| 52 | } ); |
| 53 | }, [] ); |
| 54 | |
| 55 | useEffect( () => { |
| 56 | const parentBlockId = getBlockParents( clientId, true ); |
| 57 | |
| 58 | if ( parentBlockId.length > 0 ) { |
| 59 | const parentBlocks = getBlocksByClientId( parentBlockId ); |
| 60 | |
| 61 | if ( parentBlocks.length > 0 && 'generateblocks/button-container' === parentBlocks[ 0 ].name ) { |
| 62 | setAttributes( { hasButtonContainer: true } ); |
| 63 | } else if ( !! hasButtonContainer ) { |
| 64 | setAttributes( { hasButtonContainer: false } ); |
| 65 | } |
| 66 | } else if ( !! hasButtonContainer ) { |
| 67 | setAttributes( { hasButtonContainer: false } ); |
| 68 | } |
| 69 | }, [] ); |
| 70 | |
| 71 | useEffect( () => { |
| 72 | // Add our default Button styles when inserted. |
| 73 | if ( wasBlockJustInserted( attributes ) && ! blockVersion && ! variantRole ) { |
| 74 | setAttributes( generateBlocksStyling.button ); |
| 75 | } |
| 76 | }, [] ); |
| 77 | |
| 78 | return ( |
| 79 | <Fragment> |
| 80 | <BlockControls |
| 81 | { ...props } |
| 82 | /> |
| 83 | |
| 84 | <GenerateBlocksInspectorControls |
| 85 | attributes={ attributes } |
| 86 | setAttributes={ setAttributes } |
| 87 | computedStyles={ computedStyles } |
| 88 | > |
| 89 | { applyFilters( 'generateblocks.editor.settingsPanel', undefined, { ...props, device: deviceType } ) } |
| 90 | </GenerateBlocksInspectorControls> |
| 91 | |
| 92 | <InspectorAdvancedControls |
| 93 | anchor={ anchor } |
| 94 | ariaLabel={ ariaLabel } |
| 95 | buttonType={ buttonType } |
| 96 | setAttributes={ setAttributes } |
| 97 | /> |
| 98 | |
| 99 | <ComponentCSS { ...props } deviceType={ deviceType } /> |
| 100 | |
| 101 | <GoogleFontLink |
| 102 | fontFamily={ fontFamily } |
| 103 | googleFont={ googleFont } |
| 104 | googleFontVariants={ googleFontVariants } |
| 105 | isBlockPreview={ isBlockPreview } |
| 106 | /> |
| 107 | |
| 108 | <ContentRenderer { ...props } buttonRef={ ref } /> |
| 109 | </Fragment> |
| 110 | ); |
| 111 | }; |
| 112 | |
| 113 | export default compose( |
| 114 | withDeviceType, |
| 115 | withBlockContext, |
| 116 | withDynamicContent, |
| 117 | withUniqueId, |
| 118 | withButtonLegacyMigration |
| 119 | )( ButtonEdit ); |
| 120 |