BlockControls.js
4 years ago
ComponentCSS.js
4 years ago
HeadlineContentRenderer.js
4 years ago
InspectorControls.js
4 years ago
ToolbarGroup.js
4 years ago
HeadlineContentRenderer.js
101 lines
| 1 | import classnames from 'classnames'; |
| 2 | import { applyFilters } from '@wordpress/hooks'; |
| 3 | import { RichText, useBlockProps } from '@wordpress/block-editor'; |
| 4 | import { __ } from '@wordpress/i18n'; |
| 5 | import RootElement from '../../../components/root-element'; |
| 6 | import IconWrapper from '../../../components/icon-wrapper'; |
| 7 | import Element from '../../../components/element'; |
| 8 | import { useSelect } from '@wordpress/data'; |
| 9 | import { useMemo } from '@wordpress/element'; |
| 10 | |
| 11 | export default function HeadlineContentRenderer( props ) { |
| 12 | const { |
| 13 | name, |
| 14 | clientId, |
| 15 | attributes, |
| 16 | setAttributes, |
| 17 | onSplit, |
| 18 | onReplace, |
| 19 | InnerContent = RichText, |
| 20 | headlineRef, |
| 21 | } = props; |
| 22 | |
| 23 | const { |
| 24 | uniqueId, |
| 25 | element, |
| 26 | content, |
| 27 | icon, |
| 28 | hasIcon, |
| 29 | anchor, |
| 30 | removeText, |
| 31 | ariaLabel, |
| 32 | dynamicContentType, |
| 33 | dynamicLinkType, |
| 34 | } = attributes; |
| 35 | |
| 36 | let htmlAttributes = { |
| 37 | className: classnames( { |
| 38 | 'gb-headline': true, |
| 39 | [ `gb-headline-${ uniqueId }` ]: true, |
| 40 | 'gb-headline-text': ! hasIcon, |
| 41 | } ), |
| 42 | id: anchor ? anchor : null, |
| 43 | ref: headlineRef, |
| 44 | }; |
| 45 | |
| 46 | htmlAttributes = applyFilters( |
| 47 | 'generateblocks.frontend.htmlAttributes', |
| 48 | htmlAttributes, |
| 49 | 'generateblocks/headline', |
| 50 | attributes |
| 51 | ); |
| 52 | |
| 53 | const blockProps = useBlockProps( htmlAttributes ); |
| 54 | |
| 55 | const richTextFormats = applyFilters( |
| 56 | 'generateblocks.editor.headlineDisableFormatting', |
| 57 | false, |
| 58 | props |
| 59 | ) ? [] : null; |
| 60 | |
| 61 | const tagName = ( 'terms' !== dynamicContentType && !! dynamicLinkType ) ? 'a' : 'span'; |
| 62 | |
| 63 | const linkAllowedFormats = useSelect( ( select ) => ( select( 'core/rich-text' ).getFormatTypes() ), [] ); |
| 64 | |
| 65 | const textFormats = useMemo( () => { |
| 66 | if ( linkAllowedFormats && !! dynamicLinkType ) { |
| 67 | return linkAllowedFormats |
| 68 | .filter( ( format ) => ( 'core/link' !== format.name ) ) |
| 69 | .map( ( formatNames ) => ( formatNames.name ) ); |
| 70 | } |
| 71 | |
| 72 | return richTextFormats; |
| 73 | }, [ linkAllowedFormats, richTextFormats, dynamicLinkType ] ); |
| 74 | |
| 75 | return ( |
| 76 | <RootElement name={ name } clientId={ clientId }> |
| 77 | <Element tagName={ element } htmlAttrs={ blockProps }> |
| 78 | <IconWrapper |
| 79 | hasIcon={ hasIcon } |
| 80 | icon={ icon } |
| 81 | hideChildren={ removeText } |
| 82 | showWrapper={ ! removeText && hasIcon } |
| 83 | wrapperClassname={ 'gb-headline-text' } |
| 84 | ariaLabel={ ( !! removeText && !! ariaLabel ? ariaLabel : undefined ) } |
| 85 | > |
| 86 | <InnerContent |
| 87 | name={ name } |
| 88 | tagName={ tagName } |
| 89 | value={ content } |
| 90 | onChange={ ( newContent ) => setAttributes( { content: newContent } ) } |
| 91 | onSplit={ onSplit( attributes, clientId ) } |
| 92 | onReplace={ onReplace } |
| 93 | placeholder={ __( 'Headline', 'generateblocks' ) } |
| 94 | allowedFormats={ textFormats } |
| 95 | /> |
| 96 | </IconWrapper> |
| 97 | </Element> |
| 98 | </RootElement> |
| 99 | ); |
| 100 | } |
| 101 |