Created
June 26, 2019 09:59
-
-
Save olafghanizadeh/91435984e5c93e0a3b23c50941ba4b0a to your computer and use it in GitHub Desktop.
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
const { registerBlockType } = wp.blocks; | |
const { RichText, InspectorControls, MediaUpload } = wp.editor; | |
registerBlockType('custom/highlights', { | |
title: 'Highlights', | |
icon: 'universal-access-alt', | |
category: 'layout', | |
attributes: { | |
content: { | |
type: 'object', | |
default: { | |
heading: { | |
1: 'Heading', | |
2: 'Heading', | |
3: 'Heading', | |
}, | |
paragraph: { | |
1: 'Content', | |
2: 'Content', | |
3: 'Content', | |
} | |
}, | |
}, | |
}, | |
edit(props) { | |
const { | |
setAttributes, | |
attributes | |
} = props; | |
const {backgroundImage} = props.attributes; | |
function onImageSelect(imageObject) { | |
setAttributes({ | |
backgroundImage: imageObject.sizes.full.url | |
}) | |
} | |
const boxes = []; | |
for(let i of [1, 2, 3]) { | |
boxes.push( | |
<div className={'box'}> | |
<RichText | |
tagName="h2" | |
value={attributes.content.heading[i]} | |
onChange={(content) => { | |
attributes.content.heading[i] = content; | |
setAttributes({content: attributes.content}); | |
}} | |
/> | |
<RichText | |
tagName="p" | |
value={attributes.content.paragraph[i]} | |
onChange={(content) => { | |
attributes.content.paragraph[i] = content; | |
setAttributes({content: attributes.content}); | |
}} | |
/> | |
<RichText | |
tagName="a" | |
className={'button'} | |
value={attributes.content.button[i]} | |
onChange={(content) => { | |
attributes.content.button[i] = content; | |
setAttributes({content: attributes.content}); | |
}} | |
/> | |
</div> | |
); | |
} | |
return ([ | |
<section className={'highlights section'}> | |
<div className={'container'}> | |
<div className={'boxes'}> | |
{boxes} | |
</div> | |
</div> | |
</section> | |
]); | |
}, | |
save(props) { | |
const { attributes} = props; | |
const {backgroundImage } = props.attributes; | |
const boxes = []; | |
for(let i of [1, 2, 3]) { | |
boxes.push( | |
<div className={'box'}> | |
<RichText.Content | |
tagName="span" | |
value={attributes.content.heading[i]} | |
/> | |
<RichText.Content | |
tagName="p" | |
value={attributes.content.paragraph[i]} | |
/> | |
</div> | |
); | |
} | |
return ( | |
<section className={'highlights section'}> | |
<div className={'container'}> | |
<div className={'boxes'}> | |
{boxes} | |
</div> | |
</div> | |
</section> | |
); | |
}, | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment