(function ( wpI18n, wpBlocks, wpElement, wpEditor, wpComponents ) { const { __ } = wpI18n; const { Component, Fragment } = wpElement; const { registerBlockType, createBlock } = wpBlocks; const { InspectorControls, RichText, ColorPalette, BlockControls } = wpEditor; const { BaseControl, RangeControl, PanelBody, IconButton, Dashicon, Toolbar } = wpComponents; class AdvList extends Component { constructor() { super( ...arguments ); } componentWillMount() { const { attributes, setAttributes } = this.props; const currentBlockConfig = advgbDefaultConfig['advgb-list']; // 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: 'advgblist-' + clientId } ) } } render() { const listIcons = [ { label: __( 'None' ), value: '' }, { label: __( 'Pushpin' ), value: 'admin-post' }, { label: __( 'Configuration' ), value: 'admin-generic' }, { label: __( 'Flag' ), value: 'flag' }, { label: __( 'Star' ), value: 'star-filled' }, { label: __( 'Checkmark' ), value: 'yes' }, { label: __( 'Minus' ), value: 'minus' }, { label: __( 'Plus' ), value: 'plus' }, { label: __( 'Play' ), value: 'controls-play' }, { label: __( 'Arrow right' ), value: 'arrow-right-alt' }, { label: __( 'X Cross' ), value: 'dismiss' }, { label: __( 'Warning' ), value: 'warning' }, { label: __( 'Help' ), value: 'editor-help' }, { label: __( 'Info' ), value: 'info' }, { label: __( 'Circle' ), value: 'marker' }, ]; const { attributes, isSelected, insertBlocksAfter, mergeBlocks, setAttributes, onReplace, className, clientId: blockID, } = this.props; const { id, values, icon, iconSize, iconColor, margin, padding, lineHeight, fontSize, } = attributes; const listClassName = [ className, id, icon && 'advgb-list', icon && `advgb-list-${icon}` ].filter( Boolean ).join( ' ' ); const size = typeof iconSize != 'undefined' ? parseInt(iconSize) : 16; const marg = typeof margin != 'undefined' ? parseInt(margin) : 2; const padd = typeof padding != 'undefined' ? parseInt(padding)*2 : 4; return ( setAttributes( { id: 'advgblist-' + blockID } ) } /> setAttributes( { fontSize: size } ) } min={ 10 } max={ 100 } beforeIcon="editor-textcolor" allowReset />
{ listIcons.map( (item, index) => (
setAttributes( { icon: item.value } ) } className={ [ item.value === icon && 'active', item.value === '' && 'remove-icon', ].filter( Boolean ).join( ' ' ) } >
) ) }
{icon && ( ] } initialOpen={ false } > setAttributes( { iconColor: value === undefined ? '#000' : value } ) } /> setAttributes( { iconSize: size } ) } min={ 10 } max={ 100 } allowReset /> setAttributes( { lineHeight: size } ) } min={ 0 } max={ 100 } allowReset /> setAttributes( { margin: size } ) } min={ 0 } max={ 100 } allowReset /> setAttributes( { padding: size } ) } min={ 0 } max={ 100 } allowReset /> ) }
setAttributes( { values: value } ) } value={ values } wrapperClassName="advgb-list-item" className={ listClassName } placeholder={__('Write advanced list…')} onMerge={ mergeBlocks } unstableOnSplit={ insertBlocksAfter ? ( before, after, ...blocks ) => { if ( ! blocks.length ) { blocks.push( createBlock( 'core/paragraph' ) ); } if ( after.length ) { blocks.push( createBlock( 'advgb/list', { ...attributes, values: after, id: undefined, } ) ); } setAttributes( { values: before } ); insertBlocksAfter( blocks ); } : undefined } onRemove={ () => onReplace( [] ) } isSelected={ isSelected } />
{icon && }
) } } const listBlockIcon = ( ); const listBlockAttrs = { id: { type: 'string' }, icon: { type: 'string' }, iconSize: { type: 'number', default: 16, }, iconColor: { type: 'string', default: '#000', }, fontSize: { type: 'number', default: 16, }, lineHeight: { type: 'number', default: 18, }, margin: { type: 'number', default: 2, }, padding: { type: 'number', default: 2, }, values: { type: 'array', source: 'children', selector: 'ul', default: [], }, changed: { type: 'boolean', default: false, }, }; registerBlockType( 'advgb/list', { title: __( 'Advanced List' ), description: __( 'List block with custom icons and styles.' ), icon: { src: listBlockIcon, foreground: typeof advgbBlocks !== 'undefined' ? advgbBlocks.color : undefined, }, category: 'advgb-category', keywords: [ __( 'list' ), __( 'icon' ) ], attributes: listBlockAttrs, transforms: { from: [ { type: 'block', blocks: [ 'core/list' ], transform: ( { values } ) => { return createBlock( 'advgb/list', { values: values, icon: 'controls-play', iconColor: '#ff0000', } ) } } ], to: [ { type: 'block', blocks: [ 'core/list' ], transform: ( { values } ) => { return createBlock( 'core/list', { nodeName: 'UL', values: values, } ) } } ] }, merge( attributes, attributesToMerge ) { const valuesToMerge = attributesToMerge.values || []; // Standard text-like block attribute. if ( attributesToMerge.content ) { valuesToMerge.push( attributesToMerge.content ); } return { ...attributes, values: [ ...attributes.values, ...valuesToMerge, ], }; }, edit: AdvList, save: function ( { attributes } ) { const { id, values, icon, } = attributes; const listClassName = [ id, icon && 'advgb-list', icon && `advgb-list-${icon}` ].filter( Boolean ).join( ' ' ); return
}, deprecated: [ { attributes: listBlockAttrs, save: function ( { attributes } ) { const { id, values, icon, iconSize, iconColor, margin, padding, lineHeight, fontSize, } = attributes; const listClassName = [ id, icon && 'advgb-list', icon && `advgb-list-${icon}` ].filter( Boolean ).join( ' ' ); const size = typeof iconSize != 'undefined' ? parseInt(iconSize) : 16; const marg = typeof margin != 'undefined' ? parseInt(margin) : 2; const padd = typeof padding != 'undefined' ? parseInt(padding)*2 : 4; return
{icon && }
}, }, { attributes: listBlockAttrs, save: function ( { attributes } ) { const { id, values, icon, iconSize, iconColor, margin, padding, lineHeight, fontSize, } = attributes; const listClassName = [ id, icon && 'advgb-list', icon && `advgb-list-${icon}` ].filter( Boolean ).join( ' ' ); return
{icon && }
} } ] } ); })( wp.i18n, wp.blocks, wp.element, wp.editor, wp.components );