| 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 useBlockProps = wp.blockEditor.useBlockProps; |
| 7 |
var PanelBody = wp.components.PanelBody; |
| 8 |
var PanelColorSettings = wp.blockEditor.PanelColorSettings; |
| 9 |
var SelectControl = wp.components.SelectControl; |
| 10 |
var ToggleControl = wp.components.ToggleControl; |
| 11 |
var RangeControl = wp.components.RangeControl; |
| 12 |
var TextControl = wp.components.TextControl; |
| 13 |
|
| 14 |
var _tc; function getTypoControl() { return _tc || (_tc = window.bkbgTypographyControl); } |
| 15 |
var _tv; function getTypoCssVars() { return _tv || (_tv = window.bkbgTypoCssVars); } |
| 16 |
|
| 17 |
var v1Attributes = { |
| 18 |
relation:{type:'string',default:'category'}, |
| 19 |
count:{type:'number',default:3}, |
| 20 |
layout:{type:'string',default:'grid'}, |
| 21 |
columns:{type:'number',default:3}, |
| 22 |
showImage:{type:'boolean',default:true}, |
| 23 |
showExcerpt:{type:'boolean',default:false}, |
| 24 |
showDate:{type:'boolean',default:true}, |
| 25 |
imageRatio:{type:'string',default:'16/9'}, |
| 26 |
heading:{type:'string',default:'Related Posts'}, |
| 27 |
headingTag:{type:'string',default:'h3'}, |
| 28 |
cardBg:{type:'string',default:'#ffffff'}, |
| 29 |
cardBorder:{type:'string',default:'#e5e7eb'}, |
| 30 |
cardRadius:{type:'number',default:10}, |
| 31 |
cardShadow:{type:'boolean',default:true}, |
| 32 |
gap:{type:'number',default:20}, |
| 33 |
placeholderColor:{type:'string',default:'#e5e7eb'}, |
| 34 |
headingFontSize:{type:'number',default:20}, |
| 35 |
headingFontWeight:{type:'number',default:700}, |
| 36 |
headingLineHeight:{type:'number',default:1.3} |
| 37 |
}; |
| 38 |
|
| 39 |
function EditorPreview( attrs ) { |
| 40 |
var wrapStyle = { |
| 41 |
'--bkrp-cols': attrs.columns, |
| 42 |
'--bkrp-gap': attrs.gap + 'px', |
| 43 |
'--bkrp-cBg': attrs.cardBg, |
| 44 |
'--bkrp-cBrd': attrs.cardBorder, |
| 45 |
'--bkrp-cR': attrs.cardRadius + 'px', |
| 46 |
'--bkrp-ph': attrs.placeholderColor, |
| 47 |
'--bkrp-shadow': attrs.cardShadow ? '0 4px 16px rgba(0,0,0,.08)' : 'none' |
| 48 |
}; |
| 49 |
var cards = []; |
| 50 |
for ( var i = 0; i < attrs.count; i++ ) { |
| 51 |
cards.push( el( 'div', { key: i, className: 'bkrp-card' }, |
| 52 |
attrs.showImage && el( 'div', { className: 'bkrp-img-ph' } ), |
| 53 |
el( 'div', { className: 'bkrp-body' }, |
| 54 |
attrs.showDate && el( 'div', { className: 'bkrp-date-ph' } ), |
| 55 |
el( 'div', { className: 'bkrp-title-ph' } ), |
| 56 |
attrs.showExcerpt && el( 'div', { className: 'bkrp-exc-ph' } ) |
| 57 |
) |
| 58 |
) ); |
| 59 |
} |
| 60 |
return el( 'div', { className: 'bkrp-wrap bkrp-layout-' + attrs.layout, style: wrapStyle }, |
| 61 |
attrs.heading && el( attrs.headingTag || 'h3', { className: 'bkrp-heading' }, attrs.heading ), |
| 62 |
el( 'div', { className: 'bkrp-grid' }, cards ), |
| 63 |
el( 'p', { className: 'bkrp-note' }, __( 'Related posts load dynamically on the frontend.', 'blockenberg' ) ) |
| 64 |
); |
| 65 |
} |
| 66 |
|
| 67 |
registerBlockType( 'blockenberg/related-posts', { |
| 68 |
edit: function ( props ) { |
| 69 |
var attrs = props.attributes; |
| 70 |
var setAttr = props.setAttributes; |
| 71 |
var TC = getTypoControl(); |
| 72 |
|
| 73 |
var blockProps = useBlockProps((function () { |
| 74 |
var _tvFn = getTypoCssVars(); |
| 75 |
var s = {}; |
| 76 |
if (_tvFn) Object.assign(s, _tvFn(attrs.headingTypo || {}, '--bkrp-ht-')); |
| 77 |
return { style: s }; |
| 78 |
})()); |
| 79 |
|
| 80 |
return el( 'div', blockProps, |
| 81 |
el( InspectorControls, null, |
| 82 |
el( PanelBody, { title: __('Query','blockenberg'), initialOpen: true }, |
| 83 |
el( SelectControl, { label:__('Relation','blockenberg'), value:attrs.relation, |
| 84 |
options:[ |
| 85 |
{label:'Category', value:'category'}, |
| 86 |
{label:'Tag', value:'tag'}, |
| 87 |
{label:'Both (and)', value:'both'} |
| 88 |
], |
| 89 |
onChange:function(v){setAttr({relation:v});} |
| 90 |
} ), |
| 91 |
el( RangeControl, { label:__('Number of Posts','blockenberg'), value:attrs.count, min:1, max:12, onChange:function(v){setAttr({count:v});} } ) |
| 92 |
), |
| 93 |
el( PanelBody, { title: __('Display','blockenberg'), initialOpen: false }, |
| 94 |
el( TextControl, { label:__('Section Heading','blockenberg'), value:attrs.heading, onChange:function(v){setAttr({heading:v});} } ), |
| 95 |
el( SelectControl, { label:__('Heading Tag','blockenberg'), value:attrs.headingTag, options:['h2','h3','h4','p'].map(function(t){return{label:t,value:t};}), onChange:function(v){setAttr({headingTag:v});} } ), |
| 96 |
el( SelectControl, { label:__('Layout','blockenberg'), value:attrs.layout, options:[{label:'Grid',value:'grid'},{label:'List',value:'list'},{label:'Carousel',value:'carousel'}], onChange:function(v){setAttr({layout:v});} } ), |
| 97 |
attrs.layout === 'grid' && el( RangeControl, { label:__('Columns','blockenberg'), value:attrs.columns, min:1, max:4, onChange:function(v){setAttr({columns:v});} } ), |
| 98 |
el( RangeControl, { label:__('Gap (px)','blockenberg'), value:attrs.gap, min:8, max:48, onChange:function(v){setAttr({gap:v});} } ), |
| 99 |
el( ToggleControl, { label:__('Show Image','blockenberg'), checked:attrs.showImage, onChange:function(v){setAttr({showImage:v});}, __nextHasNoMarginBottom:true } ), |
| 100 |
el( ToggleControl, { label:__('Show Excerpt','blockenberg'), checked:attrs.showExcerpt, onChange:function(v){setAttr({showExcerpt:v});}, __nextHasNoMarginBottom:true } ), |
| 101 |
el( ToggleControl, { label:__('Show Date','blockenberg'), checked:attrs.showDate, onChange:function(v){setAttr({showDate:v});}, __nextHasNoMarginBottom:true } ), |
| 102 |
el( SelectControl, { label:__('Image Ratio','blockenberg'), value:attrs.imageRatio, options:[{label:'16:9',value:'16/9'},{label:'4:3',value:'4/3'},{label:'1:1',value:'1/1'},{label:'3:2',value:'3/2'}], onChange:function(v){setAttr({imageRatio:v});} } ), |
| 103 |
el( RangeControl, { label:__('Card Radius (px)','blockenberg'), value:attrs.cardRadius, min:0, max:24, onChange:function(v){setAttr({cardRadius:v});} } ), |
| 104 |
el( ToggleControl, { label:__('Card Shadow','blockenberg'), checked:attrs.cardShadow, onChange:function(v){setAttr({cardShadow:v});}, __nextHasNoMarginBottom:true } ) |
| 105 |
), |
| 106 |
el( PanelBody, { title: __('Typography','blockenberg'), initialOpen: false }, |
| 107 |
TC && el(TC, { label: __('Heading','blockenberg'), value: attrs.headingTypo || {}, onChange: function(v) { setAttr({ headingTypo: v }); } }) |
| 108 |
), |
| 109 |
el( PanelBody, { title: __('Colors','blockenberg'), initialOpen: false }, |
| 110 |
el( PanelColorSettings, { title:__('Colors','blockenberg'), colorSettings:[ |
| 111 |
{ value:attrs.cardBg, onChange:function(v){setAttr({cardBg:v||'#fff'});}, label:__('Card Background') }, |
| 112 |
{ value:attrs.cardBorder, onChange:function(v){setAttr({cardBorder:v||'#e5e7eb'});}, label:__('Card Border') }, |
| 113 |
{ value:attrs.placeholderColor,onChange:function(v){setAttr({placeholderColor:v||'#e5e7eb'});},label:__('Placeholder Shimmer') } |
| 114 |
] } ) |
| 115 |
) |
| 116 |
), |
| 117 |
EditorPreview( attrs ) |
| 118 |
); |
| 119 |
}, |
| 120 |
save: function ( props ) { |
| 121 |
var attrs = props.attributes; |
| 122 |
var _tvFn = window.bkbgTypoCssVars; |
| 123 |
var wrapStyle = { |
| 124 |
'--bkrp-cols': attrs.columns, |
| 125 |
'--bkrp-gap': attrs.gap + 'px', |
| 126 |
'--bkrp-cBg': attrs.cardBg, |
| 127 |
'--bkrp-cBrd': attrs.cardBorder, |
| 128 |
'--bkrp-cR': attrs.cardRadius + 'px', |
| 129 |
'--bkrp-ph': attrs.placeholderColor, |
| 130 |
'--bkrp-shadow': attrs.cardShadow ? '0 4px 16px rgba(0,0,0,.08)' : 'none' |
| 131 |
}; |
| 132 |
if (_tvFn) Object.assign(wrapStyle, _tvFn(attrs.headingTypo || {}, '--bkrp-ht-')); |
| 133 |
|
| 134 |
return el( 'div', { |
| 135 |
className: 'bkrp-wrap bkrp-layout-' + attrs.layout, |
| 136 |
style: wrapStyle, |
| 137 |
'data-relation': attrs.relation, |
| 138 |
'data-count': attrs.count, |
| 139 |
'data-show-image': attrs.showImage ? '1' : '0', |
| 140 |
'data-show-excerpt': attrs.showExcerpt ? '1' : '0', |
| 141 |
'data-show-date': attrs.showDate ? '1' : '0', |
| 142 |
'data-image-ratio': attrs.imageRatio |
| 143 |
}, |
| 144 |
attrs.heading && el( attrs.headingTag || 'h3', { className: 'bkrp-heading' }, attrs.heading ), |
| 145 |
el( 'div', { className: 'bkrp-grid bkrp-loading' } ) |
| 146 |
); |
| 147 |
}, |
| 148 |
deprecated: [ |
| 149 |
{ |
| 150 |
attributes: v1Attributes, |
| 151 |
save: function ( props ) { |
| 152 |
var attrs = props.attributes; |
| 153 |
return el( 'div', { |
| 154 |
className: 'bkrp-wrap bkrp-layout-' + attrs.layout, |
| 155 |
style: { |
| 156 |
'--bkrp-cols': attrs.columns, |
| 157 |
'--bkrp-gap': attrs.gap + 'px', |
| 158 |
'--bkrp-cBg': attrs.cardBg, |
| 159 |
'--bkrp-cBrd': attrs.cardBorder, |
| 160 |
'--bkrp-cR': attrs.cardRadius + 'px', |
| 161 |
'--bkrp-ph': attrs.placeholderColor, |
| 162 |
'--bkrp-shadow': attrs.cardShadow ? '0 4px 16px rgba(0,0,0,.08)' : 'none', |
| 163 |
'--bkrp-heading-fs': (attrs.headingFontSize || 20) + 'px', |
| 164 |
'--bkrp-heading-fw': attrs.headingFontWeight || 700, |
| 165 |
'--bkrp-heading-lh': attrs.headingLineHeight || 1.3 |
| 166 |
}, |
| 167 |
'data-relation': attrs.relation, |
| 168 |
'data-count': attrs.count, |
| 169 |
'data-show-image': attrs.showImage ? '1' : '0', |
| 170 |
'data-show-excerpt': attrs.showExcerpt ? '1' : '0', |
| 171 |
'data-show-date': attrs.showDate ? '1' : '0', |
| 172 |
'data-image-ratio': attrs.imageRatio |
| 173 |
}, |
| 174 |
attrs.heading && el( attrs.headingTag || 'h3', { className: 'bkrp-heading', style: { fontSize: (attrs.headingFontSize || 20) + 'px', fontWeight: attrs.headingFontWeight || 700, lineHeight: attrs.headingLineHeight || 1.3 } }, attrs.heading ), |
| 175 |
el( 'div', { className: 'bkrp-grid bkrp-loading' } ) |
| 176 |
); |
| 177 |
} |
| 178 |
} |
| 179 |
] |
| 180 |
} ); |
| 181 |
} )(); |
| 182 |
|