| 1 |
( function () { |
| 2 |
const el = window.wp.element.createElement; |
| 3 |
const { registerBlockType } = window.wp.blocks; |
| 4 |
const { InspectorControls, useBlockProps, PanelColorSettings } = window.wp.blockEditor; |
| 5 |
const { PanelBody, RangeControl, ToggleControl, TextControl, Button } = window.wp.components; |
| 6 |
const { __ } = window.wp.i18n; |
| 7 |
|
| 8 |
var _tc, _tv; |
| 9 |
function getTypoControl() { return _tc || (_tc = window.bkbgTypographyControl); } |
| 10 |
function getTypoCssVars() { return _tv || (_tv = window.bkbgTypoCssVars); } |
| 11 |
var IP = function () { return window.bkbgIconPicker; }; |
| 12 |
|
| 13 |
// ── Single link item ────────────────────────────────────────────────────── |
| 14 |
function LinkItem( { link, a } ) { |
| 15 |
return el( 'a', { |
| 16 |
href: link.url || '#', |
| 17 |
className: 'bkbg-ll-item', |
| 18 |
style: { |
| 19 |
display: 'flex', |
| 20 |
alignItems: 'flex-start', |
| 21 |
gap: 14, |
| 22 |
background: a.cardBg, |
| 23 |
border: '1px solid ' + a.borderColor, |
| 24 |
borderRadius: a.itemRadius + 'px', |
| 25 |
padding: a.itemPadding + 'px', |
| 26 |
textDecoration: 'none', |
| 27 |
boxSizing: 'border-box', |
| 28 |
transition: 'border-color 0.15s, box-shadow 0.15s', |
| 29 |
borderLeft: '3px solid ' + ( link.accentColor || a.linkColor ), |
| 30 |
} |
| 31 |
}, |
| 32 |
// Icon |
| 33 |
a.showIcon && link.icon && el( 'div', { className: 'bkbg-ll-icon', style: { |
| 34 |
width: 38, height: 38, borderRadius: 8, |
| 35 |
background: ( link.accentColor || a.linkColor ) + '18', |
| 36 |
display: 'flex', alignItems: 'center', justifyContent: 'center', |
| 37 |
fontSize: 18, flexShrink: 0, |
| 38 |
} }, (link.iconType || 'custom-char') !== 'custom-char' && IP() ? IP().buildSaveIcon(link.iconType, link.icon, link.iconDashicon, link.iconImageUrl, link.iconDashiconColor) : link.icon ), |
| 39 |
|
| 40 |
// Content |
| 41 |
el( 'div', { style: { flex: 1, minWidth: 0 } }, |
| 42 |
el( 'div', { style: { display: 'flex', alignItems: 'center', gap: 8, flexWrap: 'wrap' } }, |
| 43 |
el( 'span', { className: 'bkbg-ll-title', style: { color: a.titleColor } }, link.title ), |
| 44 |
a.showBadge && link.badge && el( 'span', { className: 'bkbg-ll-badge', style: { |
| 45 |
background: a.badgeBg, color: a.badgeColor, |
| 46 |
padding: '1px 8px', borderRadius: 99, |
| 47 |
} }, link.badge ), |
| 48 |
), |
| 49 |
a.showDescription && link.desc && el( 'p', { className: 'bkbg-ll-desc', style: { |
| 50 |
margin: '3px 0 0', color: a.textColor, |
| 51 |
} }, link.desc ), |
| 52 |
), |
| 53 |
|
| 54 |
// Chevron |
| 55 |
el( 'div', { style: { fontSize: 16, color: a.textColor, opacity: 0.5, alignSelf: 'center', flexShrink: 0 } }, '›' ), |
| 56 |
); |
| 57 |
} |
| 58 |
|
| 59 |
// ── Full render ─────────────────────────────────────────────────────────── |
| 60 |
function renderLinkList( a ) { |
| 61 |
return el( 'div', { className: 'bkbg-ll-grid', style: { |
| 62 |
display: 'grid', |
| 63 |
gridTemplateColumns: 'repeat(' + a.columns + ', 1fr)', |
| 64 |
gap: a.gap + 'px', |
| 65 |
} }, |
| 66 |
( a.links || [] ).map( ( link, i ) => el( LinkItem, { key: i, link, a } ) ) |
| 67 |
); |
| 68 |
} |
| 69 |
|
| 70 |
function updLink( setAttributes, links, li, field, val ) { |
| 71 |
setAttributes( { links: links.map( ( l, i ) => i === li ? { ...l, [field]: val } : l ) } ); |
| 72 |
} |
| 73 |
|
| 74 |
// ── Block ───────────────────────────────────────────────────────────────── |
| 75 |
registerBlockType( 'blockenberg/link-list', { |
| 76 |
edit: function ( props ) { |
| 77 |
const { attributes: a, setAttributes } = props; |
| 78 |
const blockProps = useBlockProps( (function () { |
| 79 |
var _tvFn = getTypoCssVars(); |
| 80 |
var s = { background: a.bgColor }; |
| 81 |
if (_tvFn) { |
| 82 |
Object.assign(s, _tvFn(a.headingTypo, '--bkbg-ll-h-')); |
| 83 |
Object.assign(s, _tvFn(a.subtitleTypo, '--bkbg-ll-st-')); |
| 84 |
Object.assign(s, _tvFn(a.linkTitleTypo, '--bkbg-ll-lt-')); |
| 85 |
Object.assign(s, _tvFn(a.descTypo, '--bkbg-ll-d-')); |
| 86 |
} |
| 87 |
return { className: 'bkbg-ll-wrap', style: s }; |
| 88 |
})() ); |
| 89 |
|
| 90 |
return el( 'div', blockProps, |
| 91 |
el( InspectorControls, {}, |
| 92 |
|
| 93 |
el( PanelBody, { title: __( 'Settings', 'blockenberg' ), initialOpen: true }, |
| 94 |
el( ToggleControl, { label: __( 'Show Title', 'blockenberg' ), checked: a.showTitle, onChange: v => setAttributes( { showTitle: v } ) } ), |
| 95 |
a.showTitle && el( TextControl, { label: __( 'Title', 'blockenberg' ), value: a.title, onChange: v => setAttributes( { title: v } ) } ), |
| 96 |
el( ToggleControl, { label: __( 'Show Subtitle', 'blockenberg' ), checked: a.showSubtitle, onChange: v => setAttributes( { showSubtitle: v } ) } ), |
| 97 |
a.showSubtitle && el( TextControl, { label: __( 'Subtitle', 'blockenberg' ), value: a.subtitle, onChange: v => setAttributes( { subtitle: v } ) } ), |
| 98 |
el( RangeControl, { label: __( 'Columns', 'blockenberg' ), value: a.columns, onChange: v => setAttributes( { columns: v } ), min: 1, max: 3 } ), |
| 99 |
el( ToggleControl, { label: __( 'Show Icons', 'blockenberg' ), checked: a.showIcon, onChange: v => setAttributes( { showIcon: v } ) } ), |
| 100 |
el( ToggleControl, { label: __( 'Show Descriptions', 'blockenberg' ),checked: a.showDescription, onChange: v => setAttributes( { showDescription: v } ) } ), |
| 101 |
el( ToggleControl, { label: __( 'Show Badges', 'blockenberg' ), checked: a.showBadge, onChange: v => setAttributes( { showBadge: v } ) } ), |
| 102 |
), |
| 103 |
|
| 104 |
el( PanelBody, { title: __( 'Style', 'blockenberg' ), initialOpen: false }, |
| 105 |
el( RangeControl, { label: __( 'Item Radius', 'blockenberg' ), value: a.itemRadius, onChange: v => setAttributes( { itemRadius: v } ), min: 0, max: 24 } ), |
| 106 |
el( RangeControl, { label: __( 'Item Padding', 'blockenberg' ), value: a.itemPadding, onChange: v => setAttributes( { itemPadding: v } ), min: 8, max: 40 } ), |
| 107 |
el( RangeControl, { label: __( 'Gap', 'blockenberg' ), value: a.gap, onChange: v => setAttributes( { gap: v } ), min: 4, max: 32 } ), |
| 108 |
), |
| 109 |
|
| 110 |
|
| 111 |
el( PanelBody, { title: __( 'Typography', 'blockenberg' ), initialOpen: false }, |
| 112 |
getTypoControl() && el(getTypoControl(), { label: __('Heading'), value: a.headingTypo || {}, onChange: v => setAttributes( { headingTypo: v } ) }), |
| 113 |
getTypoControl() && el(getTypoControl(), { label: __('Subtitle'), value: a.subtitleTypo || {}, onChange: v => setAttributes( { subtitleTypo: v } ) }), |
| 114 |
getTypoControl() && el(getTypoControl(), { label: __('Link Title'), value: a.linkTitleTypo || {}, onChange: v => setAttributes( { linkTitleTypo: v } ) }), |
| 115 |
getTypoControl() && el(getTypoControl(), { label: __('Description'), value: a.descTypo || {}, onChange: v => setAttributes( { descTypo: v } ) }) |
| 116 |
), |
| 117 |
el( PanelColorSettings, { |
| 118 |
title: __( 'Colors', 'blockenberg' ), initialOpen: false, |
| 119 |
colorSettings: [ |
| 120 |
{ label: __( 'Background', 'blockenberg' ), value: a.bgColor, onChange: v => setAttributes( { bgColor: v || '#ffffff' } ) }, |
| 121 |
{ label: __( 'Card Background', 'blockenberg' ),value: a.cardBg, onChange: v => setAttributes( { cardBg: v || '#f9fafb' } ) }, |
| 122 |
{ label: __( 'Border', 'blockenberg' ), value: a.borderColor, onChange: v => setAttributes( { borderColor: v || '#e5e7eb' } ) }, |
| 123 |
{ label: __( 'Heading', 'blockenberg' ), value: a.headingColor, onChange: v => setAttributes( { headingColor: v || '#111827' } ) }, |
| 124 |
{ label: __( 'Subtitle', 'blockenberg' ), value: a.subtitleColor,onChange: v => setAttributes( { subtitleColor:v || '#6b7280' } ) }, |
| 125 |
{ label: __( 'Link Title', 'blockenberg' ), value: a.titleColor, onChange: v => setAttributes( { titleColor: v || '#111827' } ) }, |
| 126 |
{ label: __( 'Description', 'blockenberg' ), value: a.textColor, onChange: v => setAttributes( { textColor: v || '#6b7280' } ) }, |
| 127 |
{ label: __( 'Default Accent', 'blockenberg' ), value: a.linkColor, onChange: v => setAttributes( { linkColor: v || '#6366f1' } ) }, |
| 128 |
{ label: __( 'Badge Background', 'blockenberg' ),value: a.badgeBg, onChange: v => setAttributes( { badgeBg: v || '#ede9fe' } ) }, |
| 129 |
{ label: __( 'Badge Text', 'blockenberg' ), value: a.badgeColor, onChange: v => setAttributes( { badgeColor: v || '#6366f1' } ) }, |
| 130 |
] |
| 131 |
} ), |
| 132 |
|
| 133 |
el( PanelBody, { title: __( 'Links', 'blockenberg' ), initialOpen: false }, |
| 134 |
el( Button, { |
| 135 |
variant: 'secondary', style: { marginBottom: 10 }, |
| 136 |
onClick: () => setAttributes( { links: [ ...( a.links || [] ), { icon: '🔗', iconType: 'custom-char', iconDashicon: '', iconImageUrl: '', title: 'New Link', desc: 'A short description.', url: '', badge: 'New', accentColor: '#6366f1' } ] } ), |
| 137 |
}, __( '+ Add Link', 'blockenberg' ) ), |
| 138 |
a.links.map( ( link, li ) => |
| 139 |
el( PanelBody, { key: li, title: ( link.icon ? link.icon + ' ' : '' ) + link.title, initialOpen: false }, |
| 140 |
IP() && el(IP().IconPickerControl, { |
| 141 |
iconType: link.iconType || 'custom-char', |
| 142 |
customChar: link.icon || '', |
| 143 |
dashicon: link.iconDashicon || '', |
| 144 |
imageUrl: link.iconImageUrl || '', |
| 145 |
onChangeType: v => updLink( setAttributes, a.links, li, 'iconType', v ), |
| 146 |
onChangeChar: v => updLink( setAttributes, a.links, li, 'icon', v ), |
| 147 |
onChangeDashicon: v => updLink( setAttributes, a.links, li, 'iconDashicon', v ), |
| 148 |
onChangeImageUrl: v => updLink( setAttributes, a.links, li, 'iconImageUrl', v ) |
| 149 |
}), |
| 150 |
el( TextControl, { label: __( 'Title', 'blockenberg' ), value: link.title, onChange: v => updLink( setAttributes, a.links, li, 'title', v ) } ), |
| 151 |
el( TextControl, { label: __( 'Description', 'blockenberg' ), value: link.desc, onChange: v => updLink( setAttributes, a.links, li, 'desc', v ) } ), |
| 152 |
el( TextControl, { label: __( 'URL', 'blockenberg' ), value: link.url, onChange: v => updLink( setAttributes, a.links, li, 'url', v ) } ), |
| 153 |
el( TextControl, { label: __( 'Badge', 'blockenberg' ), value: link.badge, onChange: v => updLink( setAttributes, a.links, li, 'badge', v ) } ), |
| 154 |
el( 'div', { style: { display: 'flex', alignItems: 'center', gap: 8, marginBottom: 8 } }, |
| 155 |
el( 'label', { style: { fontSize: 11 } }, __( 'Accent Color', 'blockenberg' ) ), |
| 156 |
el( 'input', { type: 'color', value: link.accentColor || '#6366f1', style: { width: 40, height: 28, border: 'none', borderRadius: 4, cursor: 'pointer' }, onChange: e => updLink( setAttributes, a.links, li, 'accentColor', e.target.value ) } ), |
| 157 |
), |
| 158 |
el( Button, { isDestructive: true, isSmall: true, onClick: () => setAttributes( { links: a.links.filter( ( _, x ) => x !== li ) } ) }, __( 'Remove', 'blockenberg' ) ), |
| 159 |
) |
| 160 |
), |
| 161 |
), |
| 162 |
), |
| 163 |
|
| 164 |
( a.showTitle && a.title ) && el( 'h3', { className: 'bkbg-ll-heading', style: { color: a.headingColor, margin: '0 0 6px' } }, a.title ), |
| 165 |
( a.showSubtitle && a.subtitle ) && el( 'p', { className: 'bkbg-ll-subtitle', style: { color: a.subtitleColor, margin: '0 0 16px' } }, a.subtitle ), |
| 166 |
renderLinkList( a ), |
| 167 |
); |
| 168 |
}, |
| 169 |
|
| 170 |
save: function ( { attributes: a } ) { |
| 171 |
const blockProps = useBlockProps.save( (function () { |
| 172 |
var _tvFn = getTypoCssVars(); |
| 173 |
var s = { background: a.bgColor }; |
| 174 |
if (_tvFn) { |
| 175 |
Object.assign(s, _tvFn(a.headingTypo, '--bkbg-ll-h-')); |
| 176 |
Object.assign(s, _tvFn(a.subtitleTypo, '--bkbg-ll-st-')); |
| 177 |
Object.assign(s, _tvFn(a.linkTitleTypo, '--bkbg-ll-lt-')); |
| 178 |
Object.assign(s, _tvFn(a.descTypo, '--bkbg-ll-d-')); |
| 179 |
} |
| 180 |
return { className: 'bkbg-ll-wrap', style: s }; |
| 181 |
})() ); |
| 182 |
return el( 'div', blockProps, |
| 183 |
( a.showTitle && a.title ) ? el( 'h3', { className: 'bkbg-ll-heading', style: { color: a.headingColor, margin: '0 0 6px' } }, a.title ) : null, |
| 184 |
( a.showSubtitle && a.subtitle ) ? el( 'p', { className: 'bkbg-ll-subtitle', style: { color: a.subtitleColor, margin: '0 0 16px' } }, a.subtitle ) : null, |
| 185 |
renderLinkList( a ), |
| 186 |
); |
| 187 |
}, |
| 188 |
} ); |
| 189 |
}() ); |
| 190 |
|