migrations
2 years ago
index.js
1 year ago
withButtonContainerLegacyMigration.js
2 years ago
withButtonLegacyMigration.js
2 years ago
withContainerLegacyMigration.js
2 years ago
withDeviceType.js
3 years ago
withDynamicTag.js
1 year ago
withGridLegacyMigration.js
2 years ago
withHeadlineLegacyMigration.js
2 years ago
withHtmlAttributes.js
1 year ago
withImageLegacyMigration.js
2 years ago
withSetAttributes.js
2 years ago
withStyles.js
1 year ago
withUniqueId.js
2 years ago
withDynamicTag.js
117 lines
| 1 | import { useState, useEffect, useMemo } from '@wordpress/element'; |
| 2 | import { useSelect } from '@wordpress/data'; |
| 3 | import { replaceTags } from '../dynamic-tags/utils'; |
| 4 | |
| 5 | const cache = {}; |
| 6 | |
| 7 | function getCacheKey( clientId, context ) { |
| 8 | const { |
| 9 | 'generateblocks/loopIndex': loopIndex, |
| 10 | postId, |
| 11 | } = context; |
| 12 | |
| 13 | let key = ''; |
| 14 | |
| 15 | if ( loopIndex ) { |
| 16 | key += `${ loopIndex }_`; |
| 17 | } |
| 18 | |
| 19 | if ( postId ) { |
| 20 | key += `${ postId }_`; |
| 21 | } |
| 22 | |
| 23 | key += clientId; |
| 24 | |
| 25 | return key; |
| 26 | } |
| 27 | |
| 28 | export function withDynamicTag( WrappedComponent ) { |
| 29 | return ( ( props ) => { |
| 30 | const { |
| 31 | context, |
| 32 | attributes, |
| 33 | clientId, |
| 34 | isSelected, |
| 35 | } = props; |
| 36 | |
| 37 | const { |
| 38 | content, |
| 39 | htmlAttributes, |
| 40 | tagName, |
| 41 | } = attributes; |
| 42 | |
| 43 | const [ dynamicTagValue, setDynamicTagValue ] = useState( '' ); |
| 44 | const [ contentMode, setContentMode ] = useState( 'edit' ); |
| 45 | const previewEnabled = 'enabled' === generateBlocksEditor?.dynamicTagsPreview; |
| 46 | const isSavingPost = useSelect( ( select ) => select( 'core/editor' ).isSavingPost() ); |
| 47 | const blockCacheKey = getCacheKey( clientId, context ); |
| 48 | |
| 49 | if ( ! cache[ blockCacheKey ] ) { |
| 50 | cache[ blockCacheKey ] = {}; |
| 51 | } |
| 52 | |
| 53 | const contentValue = useMemo( () => { |
| 54 | if ( 'img' === tagName ) { |
| 55 | return htmlAttributes?.src; |
| 56 | } |
| 57 | |
| 58 | if ( content?.originalHTML ) { |
| 59 | return content.originalHTML; |
| 60 | } |
| 61 | |
| 62 | return content?.text ?? content; |
| 63 | }, [ tagName, htmlAttributes?.src, content ] ); |
| 64 | |
| 65 | useEffect( () => { |
| 66 | if ( ! previewEnabled && 'preview' === contentMode ) { |
| 67 | setContentMode( 'edit' ); |
| 68 | } |
| 69 | }, [ previewEnabled, contentMode ] ); |
| 70 | |
| 71 | useEffect( () => { |
| 72 | if ( ! contentValue || ! contentValue.includes( '{{' ) || ! previewEnabled ) { |
| 73 | setDynamicTagValue( false ); |
| 74 | return; |
| 75 | } |
| 76 | |
| 77 | if ( 'edit' === contentMode && 'img' !== tagName ) { |
| 78 | setDynamicTagValue( false ); |
| 79 | return; |
| 80 | } |
| 81 | |
| 82 | if ( cache[ blockCacheKey ][ contentValue ] ) { |
| 83 | setDynamicTagValue( cache[ blockCacheKey ][ contentValue ] ); |
| 84 | return; |
| 85 | } |
| 86 | |
| 87 | async function fetchData() { |
| 88 | const response = await replaceTags( { content: contentValue, context, clientId } ); |
| 89 | |
| 90 | setDynamicTagValue( response ); |
| 91 | |
| 92 | // Cache the response. |
| 93 | cache[ blockCacheKey ][ contentValue ] = response; |
| 94 | } |
| 95 | |
| 96 | fetchData(); |
| 97 | }, [ |
| 98 | contentValue, |
| 99 | contentMode, |
| 100 | context, |
| 101 | tagName, |
| 102 | isSavingPost, |
| 103 | blockCacheKey, |
| 104 | isSelected, |
| 105 | ] ); |
| 106 | |
| 107 | return ( |
| 108 | <WrappedComponent |
| 109 | { ...props } |
| 110 | dynamicTagValue={ dynamicTagValue } |
| 111 | contentMode={ contentMode } |
| 112 | setContentMode={ setContentMode } |
| 113 | /> |
| 114 | ); |
| 115 | } ); |
| 116 | } |
| 117 |