Skip to content

Commit

Permalink
Merge pull request #1595 from tomusborne/tweak/separate-style-replace…
Browse files Browse the repository at this point in the history
…ments

Tweak: Separate style replacements
  • Loading branch information
tomusborne authored Dec 17, 2024
2 parents 0f2e11a + 7fecf8b commit 9904881
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 28 deletions.
1 change: 1 addition & 0 deletions src/editor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import './stores.js';
import './disable-blocks.js';
import './toolbar-appenders.js';
import './global-max-width.js';
import './style-html-attribute.js';
import './editor.scss';

wpDomReady( () => {
Expand Down
31 changes: 31 additions & 0 deletions src/editor/style-html-attribute.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { addFilter } from '@wordpress/hooks';
import { replaceTags } from '../dynamic-tags/utils';

addFilter(
'generateblocks.editor.htmlAttributes.style',
'generateblocks/styleWithReplacements',
async( style, props ) => {
const { context } = props;

// Check if any replacements need to be made
if ( ! style.includes( '{{' ) ) {
return style;
}

const replacements = await replaceTags( style, context );

if ( ! replacements.length ) {
return style;
}

const withReplacements = replacements.reduce( ( acc, { original, replacement, fallback } ) => {
if ( ! replacement ) {
return acc.replaceAll( original, fallback );
}

return acc.replaceAll( original, replacement );
}, style );

return withReplacements ? withReplacements : style;
}
);
41 changes: 13 additions & 28 deletions src/hoc/withHtmlAttributes.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { useEffect, useMemo, useState } from '@wordpress/element';
import { InspectorAdvancedControls } from '@wordpress/block-editor';
import { TextControl } from '@wordpress/components';
import { applyFilters } from '@wordpress/hooks';

import { useUpdateEffect } from 'react-use';

import { convertInlineStyleStringToObject } from '@utils/convertInlineStyleStringToObject';
import { replaceTags } from '../dynamic-tags/utils';

export const booleanAttributes = [
'allowfullscreen',
Expand Down Expand Up @@ -74,37 +74,22 @@ export function withHtmlAttributes( WrappedComponent ) {
align,
} = attributes;

const [ styleWithReplacements, setStyleWithReplacements ] = useState( '' );
const { style = '', href, ...otherAttributes } = htmlAttributes;
const [ processedStyle, setProcessedStyle ] = useState( style );

useEffect( () => {
async function getReplacements() {
// Check if any replacements need to be made if not, do nothing.
if ( ! style.includes( '{{' ) ) {
setStyleWithReplacements( style );
return;
}

const replacements = await replaceTags( style, context );

if ( ! replacements.length ) {
setStyleWithReplacements( style );
return;
}

const withReplacements = replacements.reduce( ( acc, { original, replacement, fallback } ) => {
if ( ! replacement ) {
return acc.replaceAll( original, fallback );
}

return acc.replaceAll( original, replacement );
}, style );
async function fetchProcessedStyle() {
const styleValue = await applyFilters(
'generateblocks.editor.htmlAttributes.style',
style,
{ ...props }
);

setStyleWithReplacements( withReplacements ? withReplacements : style );
setProcessedStyle( styleValue );
}

getReplacements();
}, [ style, context ] );
fetchProcessedStyle();
}, [ style, props ] );

useUpdateEffect( () => {
const layoutClasses = [ 'alignwide', 'alignfull' ];
Expand All @@ -120,8 +105,8 @@ export function withHtmlAttributes( WrappedComponent ) {
setAttributes( { className: newClasses.join( ' ' ) } );
}, [ align ] );

const inlineStyleObject = typeof styleWithReplacements === 'string'
? convertInlineStyleStringToObject( styleWithReplacements )
const inlineStyleObject = typeof processedStyle === 'string'
? convertInlineStyleStringToObject( processedStyle )
: '';
const combinedAttributes = {
...otherAttributes,
Expand Down

0 comments on commit 9904881

Please sign in to comment.