components
1 year ago
block.json
1 year ago
edit.js
1 year ago
editor.scss
1 year ago
index.js
1 year ago
save.js
1 year ago
transforms.js
1 year ago
transforms.js
54 lines
| 1 | /** |
| 2 | * WordPress dependencies |
| 3 | */ |
| 4 | import { createBlock } from '@wordpress/blocks'; |
| 5 | |
| 6 | const elementToLevel = { h1: 1, h2: 2, h3: 3, h4: 4, h5: 5, h6: 6 }; |
| 7 | const levelToElement = { 1: 'h1', 2: 'h2', 3: 'h3', 4: 'h4', 5: 'h5', 6: 'h6' }; |
| 8 | |
| 9 | export const transforms = { |
| 10 | from: [ |
| 11 | { |
| 12 | type: 'block', |
| 13 | blocks: [ 'core/paragraph' ], |
| 14 | transform: ( { content } ) => { |
| 15 | return createBlock( 'generateblocks/text', { |
| 16 | content, |
| 17 | tagName: 'p', |
| 18 | } ); |
| 19 | }, |
| 20 | }, |
| 21 | { |
| 22 | type: 'block', |
| 23 | blocks: [ 'core/heading' ], |
| 24 | transform: ( { content, level } ) => { |
| 25 | return createBlock( 'generateblocks/text', { |
| 26 | content, |
| 27 | tagName: levelToElement[ level ], |
| 28 | } ); |
| 29 | }, |
| 30 | }, |
| 31 | ], |
| 32 | to: [ |
| 33 | { |
| 34 | type: 'block', |
| 35 | blocks: [ 'core/paragraph' ], |
| 36 | transform: ( { content } ) => { |
| 37 | return createBlock( 'core/paragraph', { |
| 38 | content, |
| 39 | } ); |
| 40 | }, |
| 41 | }, |
| 42 | { |
| 43 | type: 'block', |
| 44 | blocks: [ 'core/heading' ], |
| 45 | transform: ( { content, tagName } ) => { |
| 46 | return createBlock( 'core/heading', { |
| 47 | content, |
| 48 | level: elementToLevel.hasOwnProperty( tagName ) ? elementToLevel[ tagName ] : 2, |
| 49 | } ); |
| 50 | }, |
| 51 | }, |
| 52 | ], |
| 53 | }; |
| 54 |