components
1 year ago
css
2 years ago
attributes.js
3 years ago
block.js
1 year ago
deprecated.js
3 years ago
edit.js
2 years ago
editor.scss
1 year ago
element-icons.js
2 years ago
markformat.js
4 years ago
save.js
4 years ago
transforms.js
1 year ago
transforms.js
127 lines
| 1 | /** |
| 2 | * WordPress dependencies |
| 3 | */ |
| 4 | import { convertLegacyHtmlAttributes } from '@utils/convertLegacyHtmlAttributes'; |
| 5 | import { convertLocalToStyles } from '@utils/legacyStyleUtils'; |
| 6 | import { |
| 7 | createBlock, |
| 8 | getBlockType, |
| 9 | } from '@wordpress/blocks'; |
| 10 | |
| 11 | const elementToLevel = { h1: 1, h2: 2, h3: 3, h4: 4, h5: 5, h6: 6 }; |
| 12 | const levelToElement = { 1: 'h1', 2: 'h2', 3: 'h3', 4: 'h4', 5: 'h5', 6: 'h6' }; |
| 13 | |
| 14 | const transforms = { |
| 15 | from: [ |
| 16 | { |
| 17 | type: 'block', |
| 18 | blocks: [ 'core/paragraph' ], |
| 19 | transform: ( { content } ) => { |
| 20 | return createBlock( 'generateblocks/headline', { |
| 21 | content, |
| 22 | element: 'p', |
| 23 | } ); |
| 24 | }, |
| 25 | }, |
| 26 | { |
| 27 | type: 'block', |
| 28 | blocks: [ 'core/heading' ], |
| 29 | transform: ( { content, level } ) => { |
| 30 | return createBlock( 'generateblocks/headline', { |
| 31 | content, |
| 32 | element: levelToElement[ level ], |
| 33 | } ); |
| 34 | }, |
| 35 | }, |
| 36 | ], |
| 37 | to: [ |
| 38 | { |
| 39 | type: 'block', |
| 40 | blocks: [ 'core/paragraph' ], |
| 41 | transform: ( { content } ) => { |
| 42 | return createBlock( 'core/paragraph', { |
| 43 | content, |
| 44 | } ); |
| 45 | }, |
| 46 | }, |
| 47 | { |
| 48 | type: 'block', |
| 49 | blocks: [ 'core/heading' ], |
| 50 | transform: ( { content, element } ) => { |
| 51 | return createBlock( 'core/heading', { |
| 52 | content, |
| 53 | level: elementToLevel.hasOwnProperty( element ) ? elementToLevel[ element ] : 2, |
| 54 | } ); |
| 55 | }, |
| 56 | }, |
| 57 | { |
| 58 | type: 'block', |
| 59 | blocks: [ 'generateblocks/text' ], |
| 60 | isMatch: ( { |
| 61 | variantRole, |
| 62 | googleFont, |
| 63 | useGlobalStyle = false, |
| 64 | isGlobalStyle = false, |
| 65 | content, |
| 66 | } ) => { |
| 67 | if ( |
| 68 | variantRole || |
| 69 | useGlobalStyle || |
| 70 | isGlobalStyle || |
| 71 | googleFont || |
| 72 | undefined === content |
| 73 | ) { |
| 74 | return false; |
| 75 | } |
| 76 | |
| 77 | return true; |
| 78 | }, |
| 79 | transform: ( attributes ) => { |
| 80 | const { |
| 81 | element, |
| 82 | globalClasses, |
| 83 | content, |
| 84 | icon, |
| 85 | removeText, |
| 86 | htmlAttributes, |
| 87 | anchor, |
| 88 | className, |
| 89 | blockLabel, |
| 90 | ariaLabel, |
| 91 | } = attributes; |
| 92 | const attributeData = getBlockType( 'generateblocks/headline' )?.attributes; |
| 93 | const styles = convertLocalToStyles( attributeData, attributes, '&:is(:hover, :focus)' ); |
| 94 | const newHtmlAttributes = convertLegacyHtmlAttributes( htmlAttributes ); |
| 95 | |
| 96 | if ( anchor ) { |
| 97 | newHtmlAttributes.id = anchor; |
| 98 | } |
| 99 | |
| 100 | if ( ariaLabel ) { |
| 101 | newHtmlAttributes[ 'aria-label' ] = ariaLabel; |
| 102 | } |
| 103 | |
| 104 | const metaData = {}; |
| 105 | |
| 106 | if ( blockLabel ) { |
| 107 | metaData.name = blockLabel; |
| 108 | } |
| 109 | |
| 110 | return createBlock( 'generateblocks/text', { |
| 111 | globalClasses, |
| 112 | content, |
| 113 | tagName: element, |
| 114 | htmlAttributes: newHtmlAttributes, |
| 115 | styles, |
| 116 | icon, |
| 117 | iconOnly: removeText, |
| 118 | className, |
| 119 | metadata: metaData, |
| 120 | } ); |
| 121 | }, |
| 122 | }, |
| 123 | ], |
| 124 | }; |
| 125 | |
| 126 | export default transforms; |
| 127 |