components
1 year ago
css
2 years ago
attributes.js
2 years ago
block-controls.js
2 years ago
block.js
1 year ago
deprecated.js
3 years ago
edit.js
2 years ago
editor.scss
1 year ago
transforms.js
1 year ago
transforms.js
94 lines
| 1 | import { convertLegacyHtmlAttributes } from '@utils/convertLegacyHtmlAttributes'; |
| 2 | import { convertLocalToStyles } from '@utils/legacyStyleUtils'; |
| 3 | import { createBlock, getBlockType } from '@wordpress/blocks'; |
| 4 | |
| 5 | export const transforms = { |
| 6 | to: [ |
| 7 | { |
| 8 | type: 'block', |
| 9 | blocks: [ 'generateblocks/element' ], |
| 10 | isMatch: ( { |
| 11 | useInnerContainer, |
| 12 | variantRole, |
| 13 | shapeDividers, |
| 14 | googleFont, |
| 15 | isGrid, |
| 16 | isQueryLoopItem, |
| 17 | useGlobalStyle = false, |
| 18 | isGlobalStyle = false, |
| 19 | } ) => { |
| 20 | if ( |
| 21 | useInnerContainer || |
| 22 | variantRole || |
| 23 | shapeDividers.length > 0 || |
| 24 | googleFont || |
| 25 | isGrid || |
| 26 | isQueryLoopItem || |
| 27 | useGlobalStyle || |
| 28 | isGlobalStyle |
| 29 | ) { |
| 30 | return false; |
| 31 | } |
| 32 | |
| 33 | return true; |
| 34 | }, |
| 35 | transform: ( attributes, blocks ) => { |
| 36 | const { |
| 37 | tagName, |
| 38 | htmlAttributes, |
| 39 | blockLabel, |
| 40 | globalClasses, |
| 41 | anchor, |
| 42 | className, |
| 43 | url, |
| 44 | } = attributes; |
| 45 | const attributeData = getBlockType( 'generateblocks/container' )?.attributes; |
| 46 | const styles = convertLocalToStyles( attributeData, attributes, '&:is(:hover, :focus)' ); |
| 47 | const newHtmlAttributes = convertLegacyHtmlAttributes( htmlAttributes ); |
| 48 | |
| 49 | if ( anchor ) { |
| 50 | newHtmlAttributes.id = anchor; |
| 51 | } |
| 52 | |
| 53 | if ( url ) { |
| 54 | newHtmlAttributes.href = url; |
| 55 | } |
| 56 | |
| 57 | const metaData = {}; |
| 58 | |
| 59 | if ( blockLabel ) { |
| 60 | metaData.name = blockLabel; |
| 61 | } |
| 62 | |
| 63 | // Clone the Blocks to be Grouped |
| 64 | // Failing to create new block references causes the original blocks |
| 65 | // to be replaced in the switchToBlockType call thereby meaning they |
| 66 | // are removed both from their original location and within the |
| 67 | // new group block. |
| 68 | const groupInnerBlocks = blocks.map( ( block ) => { |
| 69 | return createBlock( |
| 70 | block.name, |
| 71 | block.attributes, |
| 72 | block.innerBlocks |
| 73 | ); |
| 74 | } ); |
| 75 | |
| 76 | const newTagName = url ? 'a' : tagName; |
| 77 | |
| 78 | return createBlock( |
| 79 | 'generateblocks/element', |
| 80 | { |
| 81 | tagName: newTagName, |
| 82 | styles, |
| 83 | htmlAttributes: newHtmlAttributes, |
| 84 | metadata: metaData, |
| 85 | globalClasses, |
| 86 | className, |
| 87 | }, |
| 88 | groupInnerBlocks |
| 89 | ); |
| 90 | }, |
| 91 | }, |
| 92 | ], |
| 93 | }; |
| 94 |