| 1 |
( function () { |
| 2 |
const el = window.wp.element.createElement; |
| 3 |
const { registerBlockType } = window.wp.blocks; |
| 4 |
const { InspectorControls, MediaUpload, MediaUploadCheck, PanelColorSettings } = window.wp.blockEditor; |
| 5 |
const { PanelBody, RangeControl, SelectControl, TextControl, TextareaControl, ToggleControl, Button } = window.wp.components; |
| 6 |
const { __ } = window.wp.i18n; |
| 7 |
const useState = window.wp.element.useState; |
| 8 |
|
| 9 |
var _tc; function getTypoControl() { return _tc || (_tc = window.bkbgTypographyControl); } |
| 10 |
var _tv; function getTypoCssVars() { return _tv || (_tv = window.bkbgTypoCssVars); } |
| 11 |
|
| 12 |
function StarRating( { count, color } ) { |
| 13 |
return el( 'div', { className: 'bktc-stars' }, |
| 14 |
[1,2,3,4,5].map( n => |
| 15 |
el( 'span', { key: n, className: 'bktc-star', style: { color: n <= count ? color : '#ddd' } }, '� |
| 16 |
' ) |
| 17 |
) |
| 18 |
); |
| 19 |
} |
| 20 |
|
| 21 |
function ItemEditor( { item, index, onChange, onRemove, showStars, starColor } ) { |
| 22 |
function upd( key, val ) { |
| 23 |
const next = Object.assign( {}, item, { [key]: val } ); |
| 24 |
onChange( index, next ); |
| 25 |
} |
| 26 |
return el( 'div', { className: 'bktc-item-editor', style: { border: '1px solid #e0e0e0', borderRadius: '8px', padding: '12px', marginBottom: '10px', background: '#fafafa' } }, |
| 27 |
el( TextareaControl, { |
| 28 |
label: __( 'Quote' ) + ' ' + ( index + 1 ), |
| 29 |
value: item.quote, |
| 30 |
onChange: v => upd( 'quote', v ), |
| 31 |
rows: 3 |
| 32 |
} ), |
| 33 |
el( TextControl, { |
| 34 |
label: __( 'Author Name' ), |
| 35 |
value: item.author, |
| 36 |
onChange: v => upd( 'author', v ) |
| 37 |
} ), |
| 38 |
el( TextControl, { |
| 39 |
label: __( 'Role / Company' ), |
| 40 |
value: item.role, |
| 41 |
onChange: v => upd( 'role', v ) |
| 42 |
} ), |
| 43 |
showStars && el( RangeControl, { |
| 44 |
label: __( 'Stars' ), |
| 45 |
value: item.stars, |
| 46 |
min: 1, max: 5, |
| 47 |
onChange: v => upd( 'stars', v ) |
| 48 |
} ), |
| 49 |
el( 'label', { style: { fontSize: '11px', fontWeight: 600, display: 'block', marginBottom: '4px' } }, __( 'Avatar' ) ), |
| 50 |
item.avatarUrl |
| 51 |
? el( 'div', { style: { display: 'flex', alignItems: 'center', gap: '8px', marginBottom: '8px' } }, |
| 52 |
el( 'img', { src: item.avatarUrl, style: { width: 48, height: 48, borderRadius: '50%', objectFit: 'cover' } } ), |
| 53 |
el( Button, { isDestructive: true, isSmall: true, onClick: () => { upd( 'avatarUrl', '' ); upd( 'avatarId', 0 ); } }, __( 'Remove' ) ) |
| 54 |
) |
| 55 |
: el( MediaUploadCheck, null, |
| 56 |
el( MediaUpload, { |
| 57 |
onSelect: m => { upd( 'avatarUrl', m.url ); upd( 'avatarId', m.id ); }, |
| 58 |
allowedTypes: ['image'], |
| 59 |
value: item.avatarId, |
| 60 |
render: ( { open } ) => el( Button, { onClick: open, isSmall: true, variant: 'secondary' }, __( 'Upload Avatar' ) ) |
| 61 |
} ) |
| 62 |
), |
| 63 |
el( Button, { isDestructive: true, isSmall: true, onClick: () => onRemove( index ) }, __( 'Remove Item' ) ) |
| 64 |
); |
| 65 |
} |
| 66 |
|
| 67 |
function renderCard( item, showStars, starColor, cardAlign, cardBg, textColor, accentColor, cardPadding, borderRadius ) { |
| 68 |
const style = { |
| 69 |
'--bktc-card-bg': cardBg, |
| 70 |
'--bktc-text': textColor, |
| 71 |
'--bktc-accent': accentColor, |
| 72 |
'--bktc-pad': cardPadding + 'px', |
| 73 |
'--bktc-radius': borderRadius + 'px', |
| 74 |
textAlign: cardAlign, |
| 75 |
}; |
| 76 |
return el( 'div', { className: 'bktc-card', style }, |
| 77 |
el( 'div', { className: 'bktc-quote-mark' }, '\u201C' ), |
| 78 |
el( 'p', { className: 'bktc-quote' }, item.quote ), |
| 79 |
showStars && el( StarRating, { count: item.stars, color: starColor } ), |
| 80 |
el( 'div', { className: 'bktc-author-row' }, |
| 81 |
item.avatarUrl |
| 82 |
? el( 'img', { src: item.avatarUrl, alt: item.author, className: 'bktc-avatar' } ) |
| 83 |
: el( 'div', { className: 'bktc-avatar bktc-avatar-initials' }, |
| 84 |
el( 'span', null, ( item.author || 'A' ).charAt(0) ) |
| 85 |
), |
| 86 |
el( 'div', null, |
| 87 |
el( 'strong', { className: 'bktc-name' }, item.author ), |
| 88 |
el( 'span', { className: 'bktc-role' }, item.role ) |
| 89 |
) |
| 90 |
) |
| 91 |
); |
| 92 |
} |
| 93 |
|
| 94 |
registerBlockType( 'blockenberg/testimonial-carousel', { |
| 95 |
edit: function ( props ) { |
| 96 |
const { attributes: attr, setAttributes } = props; |
| 97 |
const [activeIdx, setActiveIdx] = useState(0); |
| 98 |
|
| 99 |
function updateItem( idx, next ) { |
| 100 |
const items = attr.items.slice(); |
| 101 |
items[idx] = next; |
| 102 |
setAttributes( { items } ); |
| 103 |
} |
| 104 |
function removeItem( idx ) { |
| 105 |
const items = attr.items.filter( (_, i) => i !== idx ); |
| 106 |
setAttributes( { items } ); |
| 107 |
if ( activeIdx >= items.length ) setActiveIdx( Math.max(0, items.length - 1) ); |
| 108 |
} |
| 109 |
function addItem() { |
| 110 |
setAttributes( { items: [ ...attr.items, { quote: 'New testimonial quote here.', author: 'New Author', role: 'Title', avatarUrl: '', avatarId: 0, stars: 5 } ] } ); |
| 111 |
} |
| 112 |
|
| 113 |
const wrapStyle = (function () { |
| 114 |
var _tvf = getTypoCssVars(); |
| 115 |
var s = { |
| 116 |
'--bktc-bg': attr.bgColor, |
| 117 |
'--bktc-accent': attr.accentColor, |
| 118 |
'--bktc-star': attr.starColor, |
| 119 |
background: attr.bgColor, |
| 120 |
borderRadius: attr.borderRadius + 'px', |
| 121 |
padding: '32px', |
| 122 |
}; |
| 123 |
if (_tvf) { |
| 124 |
Object.assign(s, _tvf(attr.quoteTypo, '--bktc-qt-')); |
| 125 |
Object.assign(s, _tvf(attr.nameTypo, '--bktc-nm-')); |
| 126 |
} |
| 127 |
return s; |
| 128 |
})(); |
| 129 |
|
| 130 |
const current = attr.items[ activeIdx ] || attr.items[0]; |
| 131 |
|
| 132 |
return el( 'div', { className: 'bktc-wrap', style: wrapStyle }, |
| 133 |
|
| 134 |
el( InspectorControls, null, |
| 135 |
|
| 136 |
el( PanelBody, { title: __( 'Testimonials' ), initialOpen: true }, |
| 137 |
attr.items.map( (item, i) => |
| 138 |
el( ItemEditor, { key: i, item, index: i, onChange: updateItem, onRemove: removeItem, showStars: attr.showStars, starColor: attr.starColor } ) |
| 139 |
), |
| 140 |
el( Button, { variant: 'secondary', onClick: addItem, style: { width: '100%', justifyContent: 'center' } }, __( '+ Add Testimonial' ) ) |
| 141 |
), |
| 142 |
|
| 143 |
el( PanelBody, { title: __( 'Carousel Settings' ), initialOpen: false }, |
| 144 |
el( ToggleControl, { label: __( 'Autoplay' ), checked: attr.autoplay, onChange: v => setAttributes( { autoplay: v } ), __nextHasNoMarginBottom: true } ), |
| 145 |
attr.autoplay && el( RangeControl, { label: __( 'Autoplay Speed (ms)' ), value: attr.speed, min: 1000, max: 8000, step: 500, onChange: v => setAttributes( { speed: v } ) } ), |
| 146 |
el( ToggleControl, { label: __( 'Pause on Hover' ), checked: attr.pauseOnHover, onChange: v => setAttributes( { pauseOnHover: v } ), __nextHasNoMarginBottom: true } ), |
| 147 |
el( ToggleControl, { label: __( 'Loop' ), checked: attr.loop, onChange: v => setAttributes( { loop: v } ), __nextHasNoMarginBottom: true } ), |
| 148 |
el( ToggleControl, { label: __( 'Show Arrows' ), checked: attr.showArrows, onChange: v => setAttributes( { showArrows: v } ), __nextHasNoMarginBottom: true } ), |
| 149 |
el( ToggleControl, { label: __( 'Show Dots' ), checked: attr.showDots, onChange: v => setAttributes( { showDots: v } ), __nextHasNoMarginBottom: true } ), |
| 150 |
el( ToggleControl, { label: __( 'Show Stars' ), checked: attr.showStars, onChange: v => setAttributes( { showStars: v } ), __nextHasNoMarginBottom: true } ), |
| 151 |
el( SelectControl, { |
| 152 |
label: __( 'Card Alignment' ), |
| 153 |
value: attr.cardAlign, |
| 154 |
options: [ { label: 'Center', value: 'center' }, { label: 'Left', value: 'left' } ], |
| 155 |
onChange: v => setAttributes( { cardAlign: v } ) |
| 156 |
} ) |
| 157 |
), |
| 158 |
|
| 159 |
el( PanelBody, { title: __( 'Card Style' ), initialOpen: false }, |
| 160 |
el( RangeControl, { label: __( 'Border Radius (px)' ), value: attr.borderRadius, min: 0, max: 40, onChange: v => setAttributes( { borderRadius: v } ) } ), |
| 161 |
el( RangeControl, { label: __( 'Card Padding (px)' ), value: attr.cardPadding, min: 12, max: 80, onChange: v => setAttributes( { cardPadding: v } ) } ) |
| 162 |
), |
| 163 |
|
| 164 |
|
| 165 |
el( PanelBody, { title: __( 'Typography', 'blockenberg' ), initialOpen: false }, |
| 166 |
getTypoControl()({ label: __( 'Quote', 'blockenberg' ), value: attr.quoteTypo, onChange: v => setAttributes( { quoteTypo: v } ) }), |
| 167 |
getTypoControl()({ label: __( 'Author Name', 'blockenberg' ), value: attr.nameTypo, onChange: v => setAttributes( { nameTypo: v } ) }) |
| 168 |
), |
| 169 |
el( PanelColorSettings, { |
| 170 |
title: __( 'Colors' ), |
| 171 |
initialOpen: false, |
| 172 |
colorSettings: [ |
| 173 |
{ label: __( 'Wrapper Background' ), value: attr.bgColor, onChange: v => setAttributes( { bgColor: v || '#f8f7ff' } ) }, |
| 174 |
{ label: __( 'Card Background' ), value: attr.cardBg, onChange: v => setAttributes( { cardBg: v || '#ffffff' } ) }, |
| 175 |
{ label: __( 'Text Color' ), value: attr.textColor, onChange: v => setAttributes( { textColor: v || '#222222' } ) }, |
| 176 |
{ label: __( 'Accent / Quote Mark' ), value: attr.accentColor, onChange: v => setAttributes( { accentColor: v || '#6c3fb5' } ) }, |
| 177 |
{ label: __( 'Star Color' ), value: attr.starColor, onChange: v => setAttributes( { starColor: v || '#f5a623' } ) }, |
| 178 |
] |
| 179 |
} ) |
| 180 |
), |
| 181 |
|
| 182 |
/* ── Editor preview ── */ |
| 183 |
el( 'div', { className: 'bktc-preview' }, |
| 184 |
current && renderCard( current, attr.showStars, attr.starColor, attr.cardAlign, attr.cardBg, attr.textColor, attr.accentColor, attr.cardPadding, attr.borderRadius ), |
| 185 |
|
| 186 |
attr.showArrows && el( 'div', { className: 'bktc-arrows bktc-arrows-editor' }, |
| 187 |
el( 'button', { className: 'bktc-arrow bktc-prev', onClick: () => setActiveIdx( (activeIdx - 1 + attr.items.length) % attr.items.length ) }, '‹' ), |
| 188 |
el( 'button', { className: 'bktc-arrow bktc-next', onClick: () => setActiveIdx( (activeIdx + 1) % attr.items.length ) }, '›' ) |
| 189 |
), |
| 190 |
|
| 191 |
attr.showDots && el( 'div', { className: 'bktc-dots' }, |
| 192 |
attr.items.map( (_, i) => |
| 193 |
el( 'button', { key: i, className: 'bktc-dot' + ( i === activeIdx ? ' bktc-dot-active' : '' ), onClick: () => setActiveIdx(i) } ) |
| 194 |
) |
| 195 |
) |
| 196 |
) |
| 197 |
); |
| 198 |
}, |
| 199 |
|
| 200 |
save: function ( { attributes: attr } ) { |
| 201 |
const wrapStyle = (function () { |
| 202 |
var _tvf = getTypoCssVars(); |
| 203 |
var s = { |
| 204 |
'--bktc-bg': attr.bgColor, |
| 205 |
'--bktc-card-bg': attr.cardBg, |
| 206 |
'--bktc-text': attr.textColor, |
| 207 |
'--bktc-accent': attr.accentColor, |
| 208 |
'--bktc-star': attr.starColor, |
| 209 |
'--bktc-pad': attr.cardPadding + 'px', |
| 210 |
'--bktc-radius': attr.borderRadius + 'px', |
| 211 |
'--bktc-speed': attr.speed + 'ms', |
| 212 |
}; |
| 213 |
if (_tvf) { |
| 214 |
Object.assign(s, _tvf(attr.quoteTypo, '--bktc-qt-')); |
| 215 |
Object.assign(s, _tvf(attr.nameTypo, '--bktc-nm-')); |
| 216 |
} |
| 217 |
return s; |
| 218 |
})(); |
| 219 |
|
| 220 |
return el( 'div', { |
| 221 |
className: 'bktc-wrap', |
| 222 |
style: wrapStyle, |
| 223 |
'data-autoplay': attr.autoplay ? '1' : '0', |
| 224 |
'data-speed': attr.speed, |
| 225 |
'data-loop': attr.loop ? '1' : '0', |
| 226 |
'data-hover': attr.pauseOnHover ? '1' : '0', |
| 227 |
'data-arrows': attr.showArrows ? '1' : '0', |
| 228 |
'data-dots': attr.showDots ? '1' : '0', |
| 229 |
}, |
| 230 |
el( 'div', { className: 'bktc-track' }, |
| 231 |
attr.items.map( (item, i) => |
| 232 |
el( 'div', { key: i, className: 'bktc-slide' + ( i === 0 ? ' bktc-active' : '' ) }, |
| 233 |
el( 'div', { className: 'bktc-card', style: { textAlign: attr.cardAlign } }, |
| 234 |
el( 'div', { className: 'bktc-quote-mark' }, '\u201C' ), |
| 235 |
el( 'p', { className: 'bktc-quote' }, item.quote ), |
| 236 |
attr.showStars && el( 'div', { className: 'bktc-stars' }, |
| 237 |
[1,2,3,4,5].map( n => |
| 238 |
el( 'span', { key: n, className: 'bktc-star', style: { color: n <= item.stars ? attr.starColor : '#ddd' } }, '� |
| 239 |
' ) |
| 240 |
) |
| 241 |
), |
| 242 |
el( 'div', { className: 'bktc-author-row' }, |
| 243 |
item.avatarUrl |
| 244 |
? el( 'img', { src: item.avatarUrl, alt: item.author, className: 'bktc-avatar' } ) |
| 245 |
: el( 'div', { className: 'bktc-avatar bktc-avatar-initials' }, |
| 246 |
el( 'span', null, (item.author || 'A').charAt(0) ) |
| 247 |
), |
| 248 |
el( 'div', null, |
| 249 |
el( 'strong', { className: 'bktc-name' }, item.author ), |
| 250 |
el( 'span', { className: 'bktc-role' }, item.role ) |
| 251 |
) |
| 252 |
) |
| 253 |
) |
| 254 |
) |
| 255 |
) |
| 256 |
), |
| 257 |
attr.showArrows && el( 'div', { className: 'bktc-arrows' }, |
| 258 |
el( 'button', { className: 'bktc-arrow bktc-prev', 'aria-label': 'Previous' }, '‹' ), |
| 259 |
el( 'button', { className: 'bktc-arrow bktc-next', 'aria-label': 'Next' }, '›' ) |
| 260 |
), |
| 261 |
attr.showDots && el( 'div', { className: 'bktc-dots' } ) |
| 262 |
); |
| 263 |
} |
| 264 |
} ); |
| 265 |
} )(); |
| 266 |
|