Last active
August 16, 2018 12:59
-
-
Save MarieComet/3ac9d00775b30319e4d2b25151a436d7 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
var addFilter = wp.hooks.addFilter; | |
var Fragment = wp.element.Fragment; | |
var RichText = wp.editor.RichText; | |
var InnerBlocks = wp.editor.InnerBlocks; | |
var el = wp.element.createElement; | |
// Hook function to add a caption to the core code block | |
function addColorColumns(settings) { | |
if (settings.name !== "core/columns") { | |
return settings; | |
} | |
const newCodeBlockSettings = Object.assign({}, settings, { | |
attributes: Object.assign({}, settings.attributes, { | |
backgroundColor: { | |
selector: ".wp-block-columns", | |
type: 'string', | |
default: '#fff', | |
} | |
}), | |
edit: function(props) { | |
function setBackgroundColor( newbg_color ) { | |
console.log(newbg_color); | |
props.setAttributes( { backgroundColor: newbg_color } ); | |
} | |
var backgroundColor = props.attributes.backgroundColor; | |
return el( | |
Fragment, | |
{}, | |
el( | |
settings.edit, | |
props | |
), | |
el( | |
wp.editor.InspectorControls, | |
{}, | |
el( | |
wp.components.PanelColor, | |
{}, | |
'Background color', | |
el( | |
wp.editor.ColorPalette, | |
{ | |
value: backgroundColor, | |
onChange: setBackgroundColor | |
} | |
) | |
) | |
) | |
); | |
}, | |
save: function(props) { | |
const { columns } = props.attributes; | |
return el( | |
"div", | |
{ | |
style: { backgroundColor: props.attributes.backgroundColor }, | |
className: `has-${ columns }-columns`, | |
}, InnerBlocks.Content, | |
el(settings.save, props) | |
); | |
} | |
}); | |
return newCodeBlockSettings; | |
} | |
// Registering the filter to edit the block settings | |
addFilter( | |
"blocks.registerBlockType", | |
"myplugin/add-code-caption", | |
addColorColumns | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment