-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1595 from tomusborne/tweak/separate-style-replace…
…ments Tweak: Separate style replacements
- Loading branch information
Showing
3 changed files
with
45 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters