| 1 |
( function () { |
| 2 |
var el = wp.element.createElement; |
| 3 |
var __ = wp.i18n.__; |
| 4 |
var registerBlockType = wp.blocks.registerBlockType; |
| 5 |
var InspectorControls = wp.blockEditor.InspectorControls; |
| 6 |
var MediaUpload = wp.blockEditor.MediaUpload; |
| 7 |
var MediaUploadCheck = wp.blockEditor.MediaUploadCheck; |
| 8 |
var RichText = wp.blockEditor.RichText; |
| 9 |
var PanelBody = wp.components.PanelBody; |
| 10 |
var PanelColorSettings = wp.blockEditor.PanelColorSettings; |
| 11 |
var SelectControl = wp.components.SelectControl; |
| 12 |
var RangeControl = wp.components.RangeControl; |
| 13 |
var TextControl = wp.components.TextControl; |
| 14 |
var Button = wp.components.Button; |
| 15 |
|
| 16 |
function getTypographyControl() { |
| 17 |
return (typeof window.bkbgTypographyControl !== 'undefined') ? window.bkbgTypographyControl : null; |
| 18 |
} |
| 19 |
function getTypoCssVars() { |
| 20 |
return (typeof window.bkbgTypoCssVars !== 'undefined') ? window.bkbgTypoCssVars : function() { return {}; }; |
| 21 |
} |
| 22 |
|
| 23 |
function renderGroup( attrs ) { |
| 24 |
var shown = attrs.avatars.slice( 0, attrs.maxShown ); |
| 25 |
var extra = attrs.avatars.length - shown.length; |
| 26 |
var overlap = attrs.direction === 'left' |
| 27 |
? attrs.overlap : -attrs.overlap; |
| 28 |
|
| 29 |
var _tv = getTypoCssVars(); |
| 30 |
var wrapStyle = { |
| 31 |
'--bkag-size': attrs.avatarSize + 'px', |
| 32 |
'--bkag-brd': attrs.borderWidth + 'px solid ' + attrs.borderColor, |
| 33 |
'--bkag-cBg': attrs.counterBg, |
| 34 |
'--bkag-cText': attrs.counterText |
| 35 |
}; |
| 36 |
Object.assign( wrapStyle, _tv( attrs.counterTypo || {}, '--bkag-counter-' ) ); |
| 37 |
Object.assign( wrapStyle, _tv( attrs.labelTypo || {}, '--bkag-label-' ) ); |
| 38 |
|
| 39 |
return el( 'div', { className: 'bkag-wrap', style: wrapStyle }, |
| 40 |
el( 'div', { className: 'bkag-group' }, |
| 41 |
shown.map( function ( av, i ) { |
| 42 |
return el( 'img', { |
| 43 |
key: i, |
| 44 |
className: 'bkag-avatar', |
| 45 |
src: av.url || 'data:image/svg+xml,%3Csvg xmlns="http://www.w3.org/2000/svg" width="48" height="48" viewBox="0 0 48 48"%3E%3Ccircle cx="24" cy="24" r="24" fill="%23ccc"/%3E%3C/svg%3E', |
| 46 |
alt: av.alt || '', |
| 47 |
title: av.alt || '', |
| 48 |
style: { marginLeft: i === 0 ? 0 : ( -attrs.overlap + 'px' ) } |
| 49 |
} ); |
| 50 |
} ), |
| 51 |
extra > 0 && el( 'div', { |
| 52 |
className: 'bkag-counter', |
| 53 |
style: { marginLeft: -attrs.overlap + 'px' } |
| 54 |
}, '+' + extra ) |
| 55 |
), |
| 56 |
attrs.label && el( 'span', { className: 'bkag-label' }, attrs.label ) |
| 57 |
); |
| 58 |
} |
| 59 |
|
| 60 |
registerBlockType( 'blockenberg/avatar-group', { |
| 61 |
edit: function ( props ) { |
| 62 |
var attrs = props.attributes; |
| 63 |
var setAttr = props.setAttributes; |
| 64 |
|
| 65 |
return el( 'div', null, |
| 66 |
el( InspectorControls, null, |
| 67 |
el( PanelBody, { title: __( 'Avatars', 'blockenberg' ), initialOpen: true }, |
| 68 |
attrs.avatars.map( function ( av, i ) { |
| 69 |
return el( 'div', { key: i, style: { display: 'flex', alignItems: 'center', gap: 8, marginBottom: 8 } }, |
| 70 |
av.url && el( 'img', { src: av.url, style: { width: 32, height: 32, borderRadius: '50%', objectFit: 'cover' } } ), |
| 71 |
el( TextControl, { |
| 72 |
label: __( 'Alt Text', 'blockenberg' ), |
| 73 |
value: av.alt || '', |
| 74 |
onChange: function (v) { |
| 75 |
var n = attrs.avatars.slice(); |
| 76 |
n[ i ] = Object.assign( {}, av, { alt: v } ); |
| 77 |
setAttr( { avatars: n } ); |
| 78 |
} |
| 79 |
} ), |
| 80 |
el( Button, { isSmall: true, isDestructive: true, onClick: function () { |
| 81 |
setAttr( { avatars: attrs.avatars.filter( function ( _, j ) { return j !== i; } ) } ); |
| 82 |
} }, '✕' ) |
| 83 |
); |
| 84 |
} ), |
| 85 |
el( MediaUploadCheck, null, |
| 86 |
el( MediaUpload, { |
| 87 |
onSelect: function ( imgs ) { |
| 88 |
var newAvs = ( Array.isArray( imgs ) ? imgs : [ imgs ] ).map( function (img) { |
| 89 |
return { url: img.url, id: img.id, alt: img.alt || '' }; |
| 90 |
} ); |
| 91 |
setAttr( { avatars: attrs.avatars.concat( newAvs ) } ); |
| 92 |
}, |
| 93 |
allowedTypes: [ 'image' ], |
| 94 |
multiple: true, |
| 95 |
render: function ( ref ) { |
| 96 |
return el( Button, { variant: 'secondary', onClick: ref.open, style: { marginTop: 8 } }, |
| 97 |
__( '+ Add Avatars', 'blockenberg' ) |
| 98 |
); |
| 99 |
} |
| 100 |
} ) |
| 101 |
), |
| 102 |
el( RangeControl, { |
| 103 |
label: __( 'Max Shown', 'blockenberg' ), |
| 104 |
value: attrs.maxShown, min: 1, max: Math.max( attrs.avatars.length, 10 ), |
| 105 |
onChange: function (v) { setAttr({ maxShown: v }); } |
| 106 |
} ) |
| 107 |
), |
| 108 |
el( PanelBody, { title: __( 'Style', 'blockenberg' ), initialOpen: false }, |
| 109 |
el( SelectControl, { |
| 110 |
label: __( 'Stack Direction', 'blockenberg' ), |
| 111 |
value: attrs.direction, |
| 112 |
options: [ |
| 113 |
{ label: 'Right (left overlap)', value: 'right' }, |
| 114 |
{ label: 'Left (right overlap)', value: 'left' } |
| 115 |
], |
| 116 |
onChange: function (v) { setAttr({ direction: v }); } |
| 117 |
} ), |
| 118 |
el( RangeControl, { |
| 119 |
label: __( 'Avatar Size (px)', 'blockenberg' ), |
| 120 |
value: attrs.avatarSize, min: 24, max: 100, |
| 121 |
onChange: function (v) { setAttr({ avatarSize: v }); } |
| 122 |
} ), |
| 123 |
el( RangeControl, { |
| 124 |
label: __( 'Overlap (px)', 'blockenberg' ), |
| 125 |
value: attrs.overlap, min: 0, max: attrs.avatarSize - 4, |
| 126 |
onChange: function (v) { setAttr({ overlap: v }); } |
| 127 |
} ), |
| 128 |
el( RangeControl, { |
| 129 |
label: __( 'Border Width (px)', 'blockenberg' ), |
| 130 |
value: attrs.borderWidth, min: 0, max: 8, |
| 131 |
onChange: function (v) { setAttr({ borderWidth: v }); } |
| 132 |
} ), |
| 133 |
el( TextControl, { |
| 134 |
label: __( 'Label Text', 'blockenberg' ), |
| 135 |
value: attrs.label, |
| 136 |
onChange: function (v) { setAttr({ label: v }); } |
| 137 |
} ) |
| 138 |
), |
| 139 |
el( PanelBody, { title: __( 'Typography', 'blockenberg' ), initialOpen: false }, |
| 140 |
(function () { |
| 141 |
var TC = getTypographyControl(); |
| 142 |
if (!TC) return el('p', { style: { color: '#999', fontSize: '12px', padding: '8px 0' } }, __('Typography control loading…', 'blockenberg')); |
| 143 |
return el(wp.element.Fragment, {}, |
| 144 |
el(TC, { label: __('Counter', 'blockenberg'), value: attrs.counterTypo || {}, onChange: function (v) { setAttr({ counterTypo: v }); } }), |
| 145 |
el(TC, { label: __('Label', 'blockenberg'), value: attrs.labelTypo || {}, onChange: function (v) { setAttr({ labelTypo: v }); } }) |
| 146 |
); |
| 147 |
})() |
| 148 |
), |
| 149 |
el( PanelBody, { title: __( 'Colors', 'blockenberg' ), initialOpen: false }, |
| 150 |
el( PanelColorSettings, { |
| 151 |
title: __( 'Colors', 'blockenberg' ), |
| 152 |
colorSettings: [ |
| 153 |
{ value: attrs.borderColor, onChange: function(v){setAttr({borderColor:v||'#fff'});}, label: __('Avatar Border') }, |
| 154 |
{ value: attrs.counterBg, onChange: function(v){setAttr({counterBg:v||'#6c3fb5'});}, label: __('Counter Background') }, |
| 155 |
{ value: attrs.counterText, onChange: function(v){setAttr({counterText:v||'#ffffff'});}, label: __('Counter Text') } |
| 156 |
] |
| 157 |
} ) |
| 158 |
) |
| 159 |
), |
| 160 |
renderGroup( attrs ) |
| 161 |
); |
| 162 |
}, |
| 163 |
save: function ( props ) { |
| 164 |
return renderGroup( props.attributes ); |
| 165 |
} |
| 166 |
} ); |
| 167 |
} )(); |
| 168 |
|