components
1 year ago
css
2 years ago
attributes.js
3 years ago
block.js
1 year ago
deprecated.js
3 years ago
edit.js
2 years ago
editor.scss
2 years ago
save.js
3 years ago
transforms.js
1 year ago
edit.js
129 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, useEffect, useState } 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 | url, |
| 39 | } = attributes; |
| 40 | |
| 41 | const ref = useRef( null ); |
| 42 | const deviceType = getDeviceType(); |
| 43 | const { |
| 44 | getBlockParents, |
| 45 | getBlocksByClientId, |
| 46 | } = useSelect( ( select ) => select( 'core/block-editor' ), [] ); |
| 47 | const [ buttonPreviewElement, setButtonPreviewElement ] = useState( 'span' ); |
| 48 | |
| 49 | useEffect( () => { |
| 50 | const parentBlockId = getBlockParents( clientId, true ); |
| 51 | |
| 52 | if ( parentBlockId.length > 0 ) { |
| 53 | const parentBlocks = getBlocksByClientId( parentBlockId ); |
| 54 | |
| 55 | if ( parentBlocks.length > 0 && 'generateblocks/button-container' === parentBlocks[ 0 ].name ) { |
| 56 | setAttributes( { hasButtonContainer: true } ); |
| 57 | } else if ( !! hasButtonContainer ) { |
| 58 | setAttributes( { hasButtonContainer: false } ); |
| 59 | } |
| 60 | } else if ( !! hasButtonContainer ) { |
| 61 | setAttributes( { hasButtonContainer: false } ); |
| 62 | } |
| 63 | }, [] ); |
| 64 | |
| 65 | useEffect( () => { |
| 66 | if ( 'link' === buttonType ) { |
| 67 | setButtonPreviewElement( url ? 'a' : 'span' ); |
| 68 | } |
| 69 | |
| 70 | if ( 'button' === buttonType ) { |
| 71 | setButtonPreviewElement( 'button' ); |
| 72 | } |
| 73 | }, [ buttonType ] ); |
| 74 | |
| 75 | useEffect( () => { |
| 76 | // Add our default Button styles when inserted. |
| 77 | if ( wasBlockJustInserted( attributes ) && ! blockVersion && ! variantRole ) { |
| 78 | setAttributes( generateBlocksStyling.button ); |
| 79 | } |
| 80 | }, [] ); |
| 81 | |
| 82 | return ( |
| 83 | <Fragment> |
| 84 | <BlockControls |
| 85 | { ...props } |
| 86 | setButtonPreviewElement={ setButtonPreviewElement } |
| 87 | /> |
| 88 | |
| 89 | <GenerateBlocksInspectorControls |
| 90 | attributes={ attributes } |
| 91 | setAttributes={ setAttributes } |
| 92 | > |
| 93 | { applyFilters( 'generateblocks.editor.settingsPanel', undefined, { ...props, device: deviceType } ) } |
| 94 | </GenerateBlocksInspectorControls> |
| 95 | |
| 96 | <InspectorAdvancedControls |
| 97 | anchor={ anchor } |
| 98 | ariaLabel={ ariaLabel } |
| 99 | buttonType={ buttonType } |
| 100 | setAttributes={ setAttributes } |
| 101 | /> |
| 102 | |
| 103 | <ComponentCSS { ...props } deviceType={ deviceType } /> |
| 104 | |
| 105 | <GoogleFontLink |
| 106 | fontFamily={ typography.fontFamily } |
| 107 | googleFont={ googleFont } |
| 108 | googleFontVariants={ googleFontVariants } |
| 109 | isBlockPreview={ isBlockPreview } |
| 110 | /> |
| 111 | |
| 112 | <ContentRenderer |
| 113 | { ...props } |
| 114 | buttonRef={ ref } |
| 115 | buttonPreviewElement={ buttonPreviewElement } |
| 116 | /> |
| 117 | </Fragment> |
| 118 | ); |
| 119 | }; |
| 120 | |
| 121 | export default compose( |
| 122 | withSetAttributes, |
| 123 | withDeviceType, |
| 124 | withBlockContext, |
| 125 | withDynamicContent, |
| 126 | withUniqueId, |
| 127 | withButtonLegacyMigration |
| 128 | )( ButtonEdit ); |
| 129 |