PluginProbe ʕ •ᴥ•ʔ
GenerateBlocks / 2.0.0
GenerateBlocks v2.0.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 / hoc / withDynamicTag.js
generateblocks / src / hoc Last commit date
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