components
4 years ago
css
4 years ago
attributes.js
4 years ago
block.js
4 years ago
deprecated.js
5 years ago
edit.js
4 years ago
editor.scss
4 years ago
element-icons.js
4 years ago
markformat.js
4 years ago
save.js
4 years ago
transforms.js
5 years ago
edit.js
115 lines
| 1 | import { useDeviceType } from '../../hooks'; |
| 2 | import './markformat'; |
| 3 | import { applyFilters } from '@wordpress/hooks'; |
| 4 | import BlockControls from './components/BlockControls'; |
| 5 | import InspectorControls from './components/InspectorControls'; |
| 6 | import { Fragment, useEffect, useRef, useState } from '@wordpress/element'; |
| 7 | import InspectorAdvancedControls from '../grid/components/InspectorAdvancedControls'; |
| 8 | import GoogleFontLink from '../../components/google-font-link'; |
| 9 | import ComponentCSS from './components/ComponentCSS'; |
| 10 | import { createBlock } from '@wordpress/blocks'; |
| 11 | import { compose } from '@wordpress/compose'; |
| 12 | import { withUniqueId } from '../../hoc'; |
| 13 | import withDynamicContent from '../../extend/dynamic-content/hoc/withDynamicContent'; |
| 14 | import HeadlineContentRenderer from './components/HeadlineContentRenderer'; |
| 15 | |
| 16 | const onSplit = ( attributes, clientId ) => ( ( value, isOriginal ) => { |
| 17 | let block; |
| 18 | |
| 19 | if ( isOriginal || value ) { |
| 20 | block = createBlock( 'generateblocks/headline', { |
| 21 | ...attributes, |
| 22 | content: value, |
| 23 | } ); |
| 24 | } else { |
| 25 | block = createBlock( 'core/paragraph' ); |
| 26 | } |
| 27 | |
| 28 | if ( isOriginal ) { |
| 29 | block.clientId = clientId; |
| 30 | } |
| 31 | |
| 32 | return block; |
| 33 | } ); |
| 34 | |
| 35 | const HeadlineEdit = ( props ) => { |
| 36 | const { |
| 37 | attributes, |
| 38 | setAttributes, |
| 39 | ContentRenderer = HeadlineContentRenderer, |
| 40 | context, |
| 41 | } = props; |
| 42 | |
| 43 | const { |
| 44 | uniqueId, |
| 45 | anchor, |
| 46 | fontFamily, |
| 47 | googleFont, |
| 48 | googleFontVariants, |
| 49 | icon, |
| 50 | hasIcon, |
| 51 | element, |
| 52 | isBlockPreview = false, |
| 53 | } = attributes; |
| 54 | |
| 55 | const ref = useRef( null ); |
| 56 | const [ computedStyles, setComputedStyles ] = useState( {} ); |
| 57 | const [ deviceType, setDeviceType ] = useDeviceType( 'Desktop' ); |
| 58 | |
| 59 | useEffect( () => { |
| 60 | if ( ! hasIcon && icon ) { |
| 61 | setAttributes( { hasIcon: true } ); |
| 62 | } |
| 63 | }, [] ); |
| 64 | |
| 65 | useEffect( () => { |
| 66 | const computedHeadlineStyles = getComputedStyle( ref.current ); |
| 67 | |
| 68 | setComputedStyles( { |
| 69 | marginTop: parseInt( computedHeadlineStyles.marginTop ) || '', |
| 70 | marginBottom: parseInt( computedHeadlineStyles.marginBottom ) || '', |
| 71 | fontSize: parseInt( computedHeadlineStyles.fontSize ) || '', |
| 72 | } ); |
| 73 | }, [ element ] ); |
| 74 | |
| 75 | return ( |
| 76 | <Fragment> |
| 77 | <BlockControls |
| 78 | attributes={ attributes } |
| 79 | setAttributes={ setAttributes } |
| 80 | deviceType={ deviceType } |
| 81 | context={ context } |
| 82 | /> |
| 83 | |
| 84 | <InspectorControls |
| 85 | { ...props } |
| 86 | uniqueId={ uniqueId } |
| 87 | deviceType={ deviceType } |
| 88 | setDeviceType={ setDeviceType } |
| 89 | blockState={ { deviceType } } |
| 90 | computedStyles={ computedStyles } |
| 91 | /> |
| 92 | |
| 93 | <InspectorAdvancedControls anchor={ anchor } setAttributes={ setAttributes } /> |
| 94 | |
| 95 | <ComponentCSS { ...props } deviceType={ deviceType } /> |
| 96 | |
| 97 | <GoogleFontLink |
| 98 | fontFamily={ fontFamily } |
| 99 | googleFont={ googleFont } |
| 100 | googleFontVariants={ googleFontVariants } |
| 101 | isBlockPreview={ isBlockPreview } |
| 102 | /> |
| 103 | |
| 104 | { applyFilters( 'generateblocks.editor.beforeHeadlineElement', '', props ) } |
| 105 | |
| 106 | <ContentRenderer { ...props } onSplit={ onSplit } headlineRef={ ref } /> |
| 107 | </Fragment> |
| 108 | ); |
| 109 | }; |
| 110 | |
| 111 | export default compose( |
| 112 | withDynamicContent, |
| 113 | withUniqueId |
| 114 | )( HeadlineEdit ); |
| 115 |