(function ( wpI18n, wpBlocks, wpElement, wpEditor, wpComponents ) {
const { __ } = wpI18n;
const { Component, Fragment } = wpElement;
const { registerBlockType, createBlock } = wpBlocks;
const { InspectorControls, BlockControls, BlockAlignmentToolbar, RichText, PanelColorSettings } = wpEditor;
const { RangeControl, PanelBody, TextControl, ToggleControl, SelectControl, IconButton, Toolbar } = wpComponents;
class AdvButton extends Component {
constructor() {
super( ...arguments );
}
componentWillMount() {
const { attributes, setAttributes } = this.props;
const currentBlockConfig = advgbDefaultConfig['advgb-button'];
// No override attributes of blocks inserted before
if (attributes.changed !== true) {
if (typeof currentBlockConfig === 'object' && currentBlockConfig !== null) {
Object.keys(currentBlockConfig).map((attribute) => {
if (typeof attributes[attribute] === 'boolean') {
attributes[attribute] = !!currentBlockConfig[attribute];
} else {
attributes[attribute] = currentBlockConfig[attribute];
}
});
}
// Finally set changed attribute to true, so we don't modify anything again
setAttributes( { changed: true } );
}
}
componentDidMount() {
const { attributes, setAttributes, clientId } = this.props;
if ( !attributes.id ) {
setAttributes( { id: 'advgbbtn-' + clientId } );
}
}
render() {
const listBorderStyles = [
{ label: __( 'None' ), value: 'none' },
{ label: __( 'Solid' ), value: 'solid' },
{ label: __( 'Dotted' ), value: 'dotted' },
{ label: __( 'Dashed' ), value: 'dashed' },
{ label: __( 'Double' ), value: 'double' },
{ label: __( 'Groove' ), value: 'groove' },
{ label: __( 'Ridge' ), value: 'ridge' },
{ label: __( 'Inset' ), value: 'inset' },
{ label: __( 'Outset' ), value: 'outset' },
];
const {
attributes,
setAttributes,
isSelected,
className,
clientId: blockID,
} = this.props;
const {
id,
align,
url,
urlOpenNewTab,
title,
text,
bgColor,
textColor,
textSize,
paddingTop,
paddingRight,
paddingBottom,
paddingLeft,
borderWidth,
borderColor,
borderRadius,
borderStyle,
hoverTextColor,
hoverBgColor,
hoverShadowColor,
hoverShadowH,
hoverShadowV,
hoverShadowBlur,
hoverShadowSpread,
transitionSpeed,
} = attributes;
return (
setAttributes( { align: align } ) } />
setAttributes( { id: 'advgbbutton-' + blockID } ) }
/>
setAttributes( { text: value } ) }
formattingControls={ [ 'bold', 'italic', 'strikethrough' ] }
isSelected={ isSelected }
className={ `wp-block-advgb-button_link ${id}` }
keepPlaceholderOnFocus
/>
{ __( 'Preview' ) }
)
] }
value={ url || '' }
placeholder={ __( 'Enter URL…' ) }
onChange={ ( text ) => setAttributes( { url: text } ) }
/>
setAttributes( { urlOpenNewTab: !attributes.urlOpenNewTab } ) }
/>
setAttributes( { textSize: size } ) }
min={ 10 }
max={ 100 }
beforeIcon="editor-textcolor"
allowReset
/>
setAttributes( { bgColor: value === undefined ? '#2196f3' : value } ),
},
{
label: __( 'Text Color' ),
value: textColor,
onChange: ( value ) => setAttributes( { textColor: value === undefined ? '#fff' : value } ),
},
] }
/>
setAttributes( { borderRadius: value } ) }
min={ 0 }
max={ 100 }
/>
setAttributes( { borderStyle: value } ) }
/>
{borderStyle !== 'none' && (
setAttributes( { borderColor: value === undefined ? '#2196f3' : value } ),
},
] }
/>
setAttributes( { borderWidth: value } ) }
min={ 0 }
max={ 100 }
/>
) }
setAttributes( { paddingTop: value } ) }
min={ 0 }
max={ 100 }
/>
setAttributes( { paddingRight: value } ) }
min={ 0 }
max={ 100 }
/>
setAttributes( { paddingBottom: value } ) }
min={ 0 }
max={ 100 }
/>
setAttributes( { paddingLeft: value } ) }
min={ 0 }
max={ 100 }
/>
setAttributes( { hoverBgColor: value === undefined ? '#2196f3' : value } ),
},
{
label: __( 'Text Color' ),
value: hoverTextColor,
onChange: ( value ) => setAttributes( { hoverTextColor: value === undefined ? '#fff' : value } ),
},
{
label: __( 'Shadow Color' ),
value: hoverShadowColor,
onChange: ( value ) => setAttributes( { hoverShadowColor: value === undefined ? '#ccc' : value } ),
},
] }
/>
setAttributes( { hoverShadowH: value } ) }
min={ -50 }
max={ 50 }
/>
setAttributes( { hoverShadowV: value } ) }
min={ -50 }
max={ 50 }
/>
setAttributes( { hoverShadowBlur: value } ) }
min={ 0 }
max={ 50 }
/>
setAttributes( { hoverShadowSpread: value } ) }
min={ 0 }
max={ 50 }
/>
setAttributes( { transitionSpeed: value } ) }
min={ 0 }
max={ 3 }
/>
)
}
}
const buttonBlockIcon = (
);
const blockAttrs = {
id: {
type: 'string',
},
url: {
type: 'string',
},
urlOpenNewTab: {
type: 'boolean',
default: true,
},
title: {
type: 'string',
},
text: {
source: 'children',
selector: 'a',
},
bgColor: {
type: 'string',
default: '#2196f3',
},
textColor: {
type: 'string',
default: '#fff',
},
textSize: {
type: 'number',
default: 18,
},
paddingTop: {
type: 'number',
default: 6,
},
paddingRight: {
type: 'number',
default: 12,
},
paddingBottom: {
type: 'number',
default: 6,
},
paddingLeft: {
type: 'number',
default: 12,
},
borderWidth: {
type: 'number',
default: 1,
},
borderColor: {
type: 'string',
default: '#2196f3'
},
borderStyle: {
type: 'string',
default: 'solid',
},
borderRadius: {
type: 'number',
default: 50
},
hoverTextColor: {
type: 'string',
default: '#fff'
},
hoverBgColor: {
type: 'string',
default: '#2196f3'
},
hoverShadowColor: {
type: 'string',
default: '#ccc'
},
hoverShadowH: {
type: 'number',
default: 3,
},
hoverShadowV: {
type: 'number',
default: 3,
},
hoverShadowBlur: {
type: 'number',
default: 1,
},
hoverShadowSpread: {
type: 'number',
default: 0,
},
transitionSpeed: {
type: 'number',
default: 0.2,
},
align: {
type: 'string',
default: 'none',
},
changed: {
type: 'boolean',
default: false,
},
};
registerBlockType( 'advgb/button', {
title: __( 'Advanced Button' ),
description: __( 'New button with more styles.' ),
icon: {
src: buttonBlockIcon,
foreground: typeof advgbBlocks !== 'undefined' ? advgbBlocks.color : undefined,
},
category: 'advgb-category',
keywords: [ __('button'), __('link') ],
attributes: blockAttrs,
transforms: {
from: [
{
type: 'block',
blocks: [ 'core/button' ],
transform: ( attributes ) => {
return createBlock( 'advgb/button', {
...attributes,
bgColor: attributes.color,
} )
}
}
],
to: [
{
type: 'block',
blocks: [ 'core/button' ],
transform: ( attributes ) => {
return createBlock( 'core/button', {
...attributes,
color: attributes.bgColor,
} )
}
}
]
},
edit: AdvButton,
save: function ( { attributes } ) {
const {
id,
align,
url,
urlOpenNewTab,
title,
text,
} = attributes;
return (
);
},
getEditWrapperProps( attributes ) {
const { align } = attributes;
const props = { 'data-resized': true };
if ( 'left' === align || 'right' === align || 'center' === align ) {
props[ 'data-align' ] = align;
}
return props;
},
deprecated: [
{
attributes: blockAttrs,
save: function ( { attributes } ) {
const {
id,
align,
url,
urlOpenNewTab,
title,
text,
bgColor,
textColor,
textSize,
paddingTop,
paddingRight,
paddingBottom,
paddingLeft,
borderWidth,
borderColor,
borderRadius,
borderStyle,
hoverTextColor,
hoverBgColor,
hoverShadowColor,
hoverShadowH,
hoverShadowV,
hoverShadowBlur,
hoverShadowSpread,
transitionSpeed,
} = attributes;
return (
);
},
},
{
attributes: blockAttrs,
save: function ( { attributes } ) {
const {
id,
align,
url,
urlOpenNewTab,
title,
text,
bgColor,
textColor,
textSize,
paddingTop,
paddingRight,
paddingBottom,
paddingLeft,
borderWidth,
borderColor,
borderRadius,
borderStyle,
hoverTextColor,
hoverBgColor,
hoverShadowColor,
hoverShadowH,
hoverShadowV,
hoverShadowBlur,
hoverShadowSpread,
transitionSpeed,
} = attributes;
return (
);
}
}
]
} );
})( wp.i18n, wp.blocks, wp.element, wp.editor, wp.components );