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