Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature - Add filter to disable dynamic tag previews in editor #1607

Merged
merged 4 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions includes/general.php
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ function( $package_name ) {
'dateFormat' => get_option( 'date_format' ),
'wpContentUrl' => content_url(),
'typographyFontFamilyList' => generateblocks_get_font_family_list(),
'dynamicTagsPreview' => apply_filters( 'generateblocks_dynamic_tags_preview', true ) ? 'enabled' : 'disabled',
]
);

Expand Down
19 changes: 11 additions & 8 deletions src/dynamic-tags/components/DynamicTagBlockToolbar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,10 @@ export function DynamicTagBlockToolbar( {
context,
isSelected,
} ) {
const previewEnabled = 'enabled' === generateBlocksEditor?.dynamicTagsPreview;
const allTags = generateBlocksEditor.dynamicTags;
const foundTags = getTags( value, allTags );

useEffect( () => {
if ( foundTags.length && ! isSelected ) {
setContentMode( 'preview' );
}
}, [ foundTags.length, isSelected ] );

const contentValue = useMemo( () => {
return value?.text || value;
}, [ value ] );
Expand All @@ -80,6 +75,12 @@ export function DynamicTagBlockToolbar( {
return contentValue.substring( selectionStart.offset, selectionEnd.offset );
}, [ selectionStart, selectionEnd, value ] );

useEffect( () => {
if ( foundTags.length && ! isSelected && previewEnabled ) {
setContentMode( 'preview' );
}
}, [ foundTags.length, isSelected ] );

return (
<BlockControls>
<ToolbarGroup>
Expand Down Expand Up @@ -116,7 +117,9 @@ export function DynamicTagBlockToolbar( {
}

onChange( new RichTextData( richTextValue ) );
setContentMode( 'preview' );
if ( previewEnabled ) {
setContentMode( 'preview' );
}
} }
onRemove={ ( tagToRemove ) => {
const newValue = replace( value, tagToRemove, '' );
Expand All @@ -138,7 +141,7 @@ export function DynamicTagBlockToolbar( {
foundTags={ foundTags }
context={ context }
/>
{ !! foundTags.length && (
{ ( !! foundTags.length && previewEnabled ) && (
<div className="gb-dynamic-tag-content-mode">
<ToolbarButton
onClick={ () => {
Expand Down
6 changes: 6 additions & 0 deletions src/editor/style-html-attribute.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ addFilter(
async( style, props ) => {
const { context, clientId } = props;

const previewEnabled = 'enabled' === generateBlocksEditor?.dynamicTagsPreview;

if ( ! previewEnabled ) {
return style;
}

// Check if any replacements need to be made
if ( ! style.includes( '{{' ) || ! style ) {
return style;
Expand Down
9 changes: 8 additions & 1 deletion src/hoc/withDynamicTag.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export function withDynamicTag( WrappedComponent ) {

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 );

Expand All @@ -62,7 +63,13 @@ export function withDynamicTag( WrappedComponent ) {
}, [ tagName, htmlAttributes?.src, content ] );

useEffect( () => {
if ( ! contentValue || ! contentValue.includes( '{{' ) ) {
if ( ! previewEnabled && 'preview' === contentMode ) {
setContentMode( 'edit' );
}
}, [ previewEnabled, contentMode ] );

useEffect( () => {
if ( ! contentValue || ! contentValue.includes( '{{' ) || ! previewEnabled ) {
setDynamicTagValue( false );
return;
}
Expand Down
Loading