PluginProbe ʕ •ᴥ•ʔ
GenerateBlocks / 1.9.0
GenerateBlocks v1.9.0
2.4.1 2.4.0 trunk 1.0 1.0.1 1.0.2 1.1.0 1.1.1 1.1.2 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.7.0 1.7.1 1.7.2 1.7.3 1.8.0 1.8.1 1.8.2 1.8.3 1.9.0 1.9.1 2.0.0 2.0.1 2.0.2 2.1.0 2.1.1 2.1.2 2.2.0 2.2.1 2.3.0
generateblocks / src / blocks / headline / components / HeadlineContentRenderer.js
generateblocks / src / blocks / headline / components Last commit date
BlockControls.js 3 years ago ComponentCSS.js 4 years ago HeadlineContentRenderer.js 3 years ago InspectorAdvancedControls.js 3 years ago TagName.js 3 years ago ToolbarGroup.js 4 years ago
HeadlineContentRenderer.js
103 lines
1 import classnames from 'classnames';
2 import { applyFilters, doAction } 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 doAction( 'generateblocks.editor.renderBlock', { ...props, ref: headlineRef } );
76
77 return (
78 <RootElement name={ name } clientId={ clientId }>
79 <Element tagName={ element } htmlAttrs={ blockProps }>
80 <IconWrapper
81 hasIcon={ hasIcon }
82 icon={ icon }
83 hideChildren={ removeText }
84 showWrapper={ ! removeText && hasIcon }
85 wrapperClassname={ 'gb-headline-text' }
86 ariaLabel={ ( !! removeText && !! ariaLabel ? ariaLabel : undefined ) }
87 >
88 <InnerContent
89 name={ name }
90 tagName={ tagName }
91 value={ content }
92 onChange={ ( newContent ) => setAttributes( { content: newContent } ) }
93 onSplit={ onSplit( attributes, clientId ) }
94 onReplace={ onReplace }
95 placeholder={ __( 'Headline', 'generateblocks' ) }
96 allowedFormats={ textFormats }
97 />
98 </IconWrapper>
99 </Element>
100 </RootElement>
101 );
102 }
103