components
3 years ago
hoc
4 years ago
hooks
3 years ago
inspector-controls
3 years ago
utils
3 years ago
DynamicRenderer.js
3 years ago
InspectorControls.js
3 years ago
attributes.js
3 years ago
InspectorControls.js
204 lines
| 1 | import { InspectorControls } from '@wordpress/block-editor'; |
| 2 | import { __ } from '@wordpress/i18n'; |
| 3 | import { ToggleControl } from '@wordpress/components'; |
| 4 | import { useEffect } from '@wordpress/element'; |
| 5 | import PanelArea from '../../components/panel-area'; |
| 6 | import DynamicSourceControl from './inspector-controls/DynamicSourceControl'; |
| 7 | import ContentTypeControl from './inspector-controls/ContentTypeControl'; |
| 8 | import LinkTypeControl from './inspector-controls/LinkTypeControl'; |
| 9 | import PostDateControl from './inspector-controls/PostDateControl'; |
| 10 | import PostMetaControl from './inspector-controls/PostMetaControl'; |
| 11 | import AuthorMetaControl from './inspector-controls/AuthorMetaControl'; |
| 12 | import CommentsControl from './inspector-controls/CommentsControl'; |
| 13 | import TermsControl from './inspector-controls/TermsControl'; |
| 14 | import ExcerptControl from './inspector-controls/ExcerptControl'; |
| 15 | import getIcon from '../../utils/get-icon'; |
| 16 | |
| 17 | export default ( { context, attributes, setAttributes, name } ) => { |
| 18 | const { |
| 19 | postType, |
| 20 | postId, |
| 21 | useDynamicData, |
| 22 | dynamicSource, |
| 23 | dynamicContentType, |
| 24 | dateType, |
| 25 | dateReplacePublished, |
| 26 | metaFieldName, |
| 27 | noCommentsText, |
| 28 | singleCommentText, |
| 29 | multipleCommentsText, |
| 30 | termTaxonomy, |
| 31 | termSeparator, |
| 32 | dynamicLinkType, |
| 33 | linkMetaFieldName, |
| 34 | linkMetaFieldType, |
| 35 | isPagination, |
| 36 | isQueryLoopItem, |
| 37 | isCaption, |
| 38 | excerptLength, |
| 39 | useDefaultMoreLink, |
| 40 | customMoreLinkText, |
| 41 | dynamicLinkRemoveIfEmpty, |
| 42 | } = attributes; |
| 43 | |
| 44 | const currentPostType = dynamicSource === 'current-post' ? context.postType : postType; |
| 45 | const currentPostId = dynamicSource === 'current-post' ? context.postId : postId; |
| 46 | const isInQueryLoop = 'undefined' !== typeof context[ 'generateblocks/queryId' ]; |
| 47 | |
| 48 | useEffect( () => { |
| 49 | if ( |
| 50 | 'generateblocks/container' === name && |
| 51 | useDynamicData && |
| 52 | '' !== dynamicContentType && |
| 53 | ( isQueryLoopItem || isInQueryLoop ) |
| 54 | ) { |
| 55 | setAttributes( { |
| 56 | bgImageInline: true, |
| 57 | } ); |
| 58 | } |
| 59 | }, [ |
| 60 | dynamicContentType, |
| 61 | isQueryLoopItem, |
| 62 | ] ); |
| 63 | |
| 64 | useEffect( () => { |
| 65 | if ( |
| 66 | 'generateblocks/headline' === name && |
| 67 | isCaption && |
| 68 | useDynamicData |
| 69 | ) { |
| 70 | if ( context[ 'generateblocks/mediaId' ] ) { |
| 71 | setAttributes( { |
| 72 | postId: context[ 'generateblocks/mediaId' ], |
| 73 | postType: 'attachment', |
| 74 | dynamicSource: 'current-post', |
| 75 | dynamicContentType: dynamicContentType || 'caption', |
| 76 | } ); |
| 77 | } else { |
| 78 | setAttributes( { |
| 79 | postId: '', |
| 80 | postType: 'post', |
| 81 | dynamicSource: 'current-post', |
| 82 | dynamicContentType: dynamicContentType || 'caption', |
| 83 | } ); |
| 84 | } |
| 85 | } |
| 86 | }, [ |
| 87 | isCaption, |
| 88 | context[ 'generateblocks/mediaId' ], |
| 89 | useDynamicData, |
| 90 | dynamicContentType, |
| 91 | ] ); |
| 92 | |
| 93 | return ( |
| 94 | <InspectorControls> |
| 95 | <PanelArea |
| 96 | id={ 'dynamicDataControls' } |
| 97 | title={ __( 'Dynamic Data', 'generateblocks' ) } |
| 98 | initialOpen={ false } |
| 99 | icon={ getIcon( 'dynamic' ) } |
| 100 | className="gblocks-panel-label" |
| 101 | > |
| 102 | <ToggleControl |
| 103 | label={ __( 'Enable Dynamic Data', 'generateblocks' ) } |
| 104 | checked={ useDynamicData } |
| 105 | onChange={ ( value ) => { |
| 106 | setAttributes( { useDynamicData: value } ); |
| 107 | } } |
| 108 | /> |
| 109 | |
| 110 | { useDynamicData && |
| 111 | <> |
| 112 | <DynamicSourceControl |
| 113 | dynamicSource={ dynamicSource } |
| 114 | postType={ postType } |
| 115 | postId={ postId } |
| 116 | setAttributes={ setAttributes } |
| 117 | dynamicContentType={ dynamicContentType } |
| 118 | /> |
| 119 | |
| 120 | <ContentTypeControl |
| 121 | dynamicContentType={ dynamicContentType } |
| 122 | setAttributes={ setAttributes } |
| 123 | name={ name } |
| 124 | isCaption={ isCaption } |
| 125 | /> |
| 126 | |
| 127 | <PostDateControl |
| 128 | isActive={ 'post-date' === dynamicContentType } |
| 129 | dateType={ dateType } |
| 130 | dateReplacePublished={ dateReplacePublished } |
| 131 | setAttributes={ setAttributes } |
| 132 | /> |
| 133 | |
| 134 | <PostMetaControl |
| 135 | isActive={ 'post-meta' === dynamicContentType } |
| 136 | postType={ currentPostType } |
| 137 | postId={ currentPostId } |
| 138 | metaFieldName={ metaFieldName } |
| 139 | setAttributes={ setAttributes } |
| 140 | attributes={ attributes } |
| 141 | /> |
| 142 | |
| 143 | <AuthorMetaControl |
| 144 | isActive={ 'author-meta' === dynamicContentType } |
| 145 | postType={ currentPostType } |
| 146 | postId={ currentPostId } |
| 147 | metaFieldName={ metaFieldName } |
| 148 | setAttributes={ setAttributes } |
| 149 | attributes={ attributes } |
| 150 | /> |
| 151 | |
| 152 | <CommentsControl |
| 153 | isActive={ 'comments-number' === dynamicContentType } |
| 154 | noCommentsText={ noCommentsText } |
| 155 | singleCommentText={ singleCommentText } |
| 156 | multipleCommentsText={ multipleCommentsText } |
| 157 | setAttributes={ setAttributes } |
| 158 | /> |
| 159 | |
| 160 | <TermsControl |
| 161 | isActive={ 'terms' === dynamicContentType } |
| 162 | postType={ currentPostType } |
| 163 | termTaxonomy={ termTaxonomy } |
| 164 | termSeparator={ termSeparator } |
| 165 | setAttributes={ setAttributes } |
| 166 | name={ name } |
| 167 | /> |
| 168 | |
| 169 | <ExcerptControl |
| 170 | isActive={ 'post-excerpt' === dynamicContentType } |
| 171 | excerptLength={ excerptLength } |
| 172 | useDefaultMoreLink={ useDefaultMoreLink } |
| 173 | customMoreLinkText={ customMoreLinkText } |
| 174 | setAttributes={ setAttributes } |
| 175 | attributes={ attributes } |
| 176 | /> |
| 177 | |
| 178 | <LinkTypeControl |
| 179 | isActive={ |
| 180 | 'generateblocks/container' !== name || |
| 181 | ( |
| 182 | 'generateblocks/container' === name && |
| 183 | 'undefined' !== typeof attributes.url |
| 184 | ) |
| 185 | } |
| 186 | postType={ currentPostType } |
| 187 | postId={ currentPostId } |
| 188 | attributes={ attributes } |
| 189 | linkType={ dynamicLinkType } |
| 190 | dynamicLinkRemoveIfEmpty={ dynamicLinkRemoveIfEmpty } |
| 191 | dynamicContentType={ dynamicContentType } |
| 192 | linkMetaFieldName={ linkMetaFieldName } |
| 193 | linkMetaFieldType={ linkMetaFieldType } |
| 194 | isPagination={ isPagination } |
| 195 | setAttributes={ setAttributes } |
| 196 | name={ name } |
| 197 | /> |
| 198 | </> |
| 199 | } |
| 200 | </PanelArea> |
| 201 | </InspectorControls> |
| 202 | ); |
| 203 | }; |
| 204 |