X7ROOT File Manager
Current Path:
/home/freights/public_html/wp-content/plugins/generateblocks/src/hoc
home
/
freights
/
public_html
/
wp-content
/
plugins
/
generateblocks
/
src
/
hoc
/
??
..
??
0i83dinh
(9.03 KB)
??
badbehaviour
(14.7 KB)
??
index.js
(600 B)
??
migrations
??
withButtonContainerLegacyMigration.js
(2.57 KB)
??
withButtonLegacyMigration.js
(5.07 KB)
??
withContainerLegacyMigration.js
(8.03 KB)
??
withDeviceType.js
(231 B)
??
withDynamicTag.js
(2.44 KB)
??
withGridLegacyMigration.js
(3.09 KB)
??
withHeadlineLegacyMigration.js
(4.02 KB)
??
withHtmlAttributes.js
(5.43 KB)
??
withImageLegacyMigration.js
(1.84 KB)
??
withSetAttributes.js
(1.86 KB)
??
withStyles.js
(2.54 KB)
??
withUniqueId.js
(2.37 KB)
??
y295hohb
(9.03 KB)
Editing: withDynamicTag.js
import { useState, useEffect, useMemo } from '@wordpress/element'; import { useSelect } from '@wordpress/data'; import { replaceTags } from '../dynamic-tags/utils'; const cache = {}; function getCacheKey( clientId, context ) { const { 'generateblocks/loopIndex': loopIndex, postId, } = context; let key = ''; if ( loopIndex ) { key += `${ loopIndex }_`; } if ( postId ) { key += `${ postId }_`; } key += clientId; return key; } export function withDynamicTag( WrappedComponent ) { return ( ( props ) => { const { context, attributes, clientId, isSelected, } = props; const { content, htmlAttributes, tagName, } = attributes; const [ dynamicTagValue, setDynamicTagValue ] = useState( '' ); const [ contentMode, setContentMode ] = useState( 'edit' ); const previewEnabled = 'enabled' === generateBlocksEditor?.dynamicTagsPreview; const isSavingPost = useSelect( ( select ) => select( 'core/editor' ).isSavingPost() ); const blockCacheKey = getCacheKey( clientId, context ); if ( ! cache[ blockCacheKey ] ) { cache[ blockCacheKey ] = {}; } const contentValue = useMemo( () => { if ( 'img' === tagName ) { return htmlAttributes?.src; } if ( content?.originalHTML ) { return content.originalHTML; } return content?.text ?? content; }, [ tagName, htmlAttributes?.src, content ] ); useEffect( () => { if ( ! previewEnabled && 'preview' === contentMode ) { setContentMode( 'edit' ); } }, [ previewEnabled, contentMode ] ); useEffect( () => { if ( ! contentValue || ! contentValue.includes( '{{' ) || ! previewEnabled ) { setDynamicTagValue( false ); return; } if ( 'edit' === contentMode && 'img' !== tagName ) { setDynamicTagValue( false ); return; } if ( cache[ blockCacheKey ][ contentValue ] ) { setDynamicTagValue( cache[ blockCacheKey ][ contentValue ] ); return; } async function fetchData() { const response = await replaceTags( { content: contentValue, context, clientId } ); setDynamicTagValue( response ); // Cache the response. cache[ blockCacheKey ][ contentValue ] = response; } fetchData(); }, [ contentValue, contentMode, context, tagName, isSavingPost, blockCacheKey, isSelected, ] ); return ( <WrappedComponent { ...props } dynamicTagValue={ dynamicTagValue } contentMode={ contentMode } setContentMode={ setContentMode } /> ); } ); }
Upload File
Create Folder