| 1 |
( function () { |
| 2 |
var el = wp.element.createElement; |
| 3 |
var useState = wp.element.useState; |
| 4 |
var Fragment = wp.element.Fragment; |
| 5 |
var registerBlockType = wp.blocks.registerBlockType; |
| 6 |
var __ = wp.i18n.__; |
| 7 |
var InspectorControls = wp.blockEditor.InspectorControls; |
| 8 |
var PanelColorSettings = wp.blockEditor.PanelColorSettings; |
| 9 |
var useBlockProps = wp.blockEditor.useBlockProps; |
| 10 |
var PanelBody = wp.components.PanelBody; |
| 11 |
var RangeControl = wp.components.RangeControl; |
| 12 |
var SelectControl = wp.components.SelectControl; |
| 13 |
var TextControl = wp.components.TextControl; |
| 14 |
var ToggleControl = wp.components.ToggleControl; |
| 15 |
var Button = wp.components.Button; |
| 16 |
var _tc; function getTypoControl() { return _tc || (_tc = window.bkbgTypographyControl); } |
| 17 |
var _tv; function getTypoCssVars() { return _tv || (_tv = window.bkbgTypoCssVars); } |
| 18 |
|
| 19 |
function parseItems(text, mode) { |
| 20 |
if (mode === 'comma') { |
| 21 |
return text.split(',').map(function(s){ return s.trim(); }).filter(Boolean); |
| 22 |
} |
| 23 |
return text.split('\n').map(function(s){ return s.trim(); }).filter(Boolean); |
| 24 |
} |
| 25 |
|
| 26 |
function RandomPickerPreview(props) { |
| 27 |
var a = props.attributes; |
| 28 |
var sa = props.setAttributes; |
| 29 |
var accent = a.accentColor || '#6c3fb5'; |
| 30 |
|
| 31 |
var _itemsText = useState(a.defaultItems || 'Alice\nBob\nCarol\nDave\nEve\nFrank'); |
| 32 |
var itemsText = _itemsText[0]; var setItemsText = _itemsText[1]; |
| 33 |
var _winner = useState(null); var winner = _winner[0]; var setWinner = _winner[1]; |
| 34 |
var _highlight= useState(-1); var highlight = _highlight[0]; var setHighlight = _highlight[1]; |
| 35 |
var _history = useState([]); var history = _history[0]; var setHistory = _history[1]; |
| 36 |
var _picking = useState(false); var picking = _picking[0]; var setPicking = _picking[1]; |
| 37 |
|
| 38 |
var items = parseItems(itemsText, a.inputMode || 'lines'); |
| 39 |
|
| 40 |
function pick() { |
| 41 |
if (items.length === 0 || picking) return; |
| 42 |
setPicking(true); setWinner(null); |
| 43 |
var steps = a.animSteps || 12; |
| 44 |
var speed = a.animSpeedMs || 60; |
| 45 |
var count = 0; |
| 46 |
function step() { |
| 47 |
var idx = Math.floor(Math.random() * items.length); |
| 48 |
setHighlight(idx); |
| 49 |
count++; |
| 50 |
if (count < steps) { |
| 51 |
setTimeout(step, speed + count * 8); |
| 52 |
} else { |
| 53 |
var finalIdx = Math.floor(Math.random() * items.length); |
| 54 |
setHighlight(finalIdx); |
| 55 |
setWinner(items[finalIdx]); |
| 56 |
if (a.showHistory) setHistory(function(h){ return [items[finalIdx]].concat(h).slice(0,10); }); |
| 57 |
setPicking(false); |
| 58 |
} |
| 59 |
} |
| 60 |
step(); |
| 61 |
} |
| 62 |
|
| 63 |
return el('div', { style:{ paddingTop:a.paddingTop+'px', paddingBottom:a.paddingBottom+'px', background:a.sectionBg||undefined } }, |
| 64 |
el('div', { style:{ background:a.cardBg, borderRadius:a.cardRadius+'px', padding:'36px 32px', maxWidth:a.maxWidth+'px', margin:'0 auto', boxShadow:'0 4px 24px rgba(0,0,0,0.09)' }}, |
| 65 |
|
| 66 |
(a.showTitle||a.showSubtitle) && el('div', { style:{marginBottom:'20px'}}, |
| 67 |
a.showTitle && el('div', { className:'bkbg-rpk-title', style:{color:a.titleColor,marginBottom:'6px'} }, a.title), |
| 68 |
a.showSubtitle && el('div', { className:'bkbg-rpk-subtitle', style:{color:a.subtitleColor} }, a.subtitle) |
| 69 |
), |
| 70 |
|
| 71 |
// Items textarea |
| 72 |
el('div', { style:{marginBottom:'16px'} }, |
| 73 |
el('label', { style:{display:'block',fontSize:'12px',fontWeight:600,textTransform:'uppercase',letterSpacing:'.04em',color:a.labelColor,marginBottom:'6px'} }, |
| 74 |
'Items (' + items.length + ')' |
| 75 |
), |
| 76 |
el('textarea', { |
| 77 |
rows: 6, value: itemsText, |
| 78 |
style:{ width:'100%', padding:'10px 12px', border:'1.5px solid #e5e7eb', borderRadius:(a.itemRadius||8)+'px', fontSize:'14px', fontFamily:'monospace', resize:'vertical', outline:'none', boxSizing:'border-box' }, |
| 79 |
onChange: function(e){ setItemsText(e.target.value); setWinner(null); setHighlight(-1); } |
| 80 |
}) |
| 81 |
), |
| 82 |
|
| 83 |
// Pick button |
| 84 |
el('button', { |
| 85 |
onClick: pick, |
| 86 |
disabled: picking || items.length === 0, |
| 87 |
style:{ width:'100%', padding:'14px', borderRadius:(a.itemRadius||8)+'px', background: picking?'#9ca3af':accent, color:a.buttonColor||'#fff', border:'none', fontWeight:700, fontSize:'16px', cursor: items.length===0?'not-allowed':'pointer', fontFamily:'inherit', transition:'background .15s', marginBottom:'20px' } |
| 88 |
}, picking ? 'Picking…' : (a.buttonLabel||'Pick Random!')), |
| 89 |
|
| 90 |
// Item grid |
| 91 |
items.length > 0 && el('div', { style:{display:'flex',flexWrap:'wrap',gap:'8px',marginBottom:'20px'} }, |
| 92 |
items.map(function(item, idx) { |
| 93 |
var isWin = winner === item && highlight === idx; |
| 94 |
var isHi = highlight === idx && !winner; |
| 95 |
return el('div', { key: String(idx) + item, style:{ |
| 96 |
padding:'8px 14px', borderRadius:(a.itemRadius||8)+'px', fontSize:'14px', fontWeight: isWin?700:500, |
| 97 |
background: isWin ? (a.winnerBg||accent) : isHi ? (a.highlightBg||'#ede9fe') : (a.itemBg||'#f5f3ff'), |
| 98 |
color: isWin ? (a.winnerColor||'#fff') : (a.itemColor||'#374151'), |
| 99 |
transition:'background .1s,color .1s', cursor:'default' |
| 100 |
}}, item); |
| 101 |
}) |
| 102 |
), |
| 103 |
|
| 104 |
// Winner box |
| 105 |
winner && el('div', { style:{ background:a.winnerBg||accent, borderRadius:(a.itemRadius||8)+'px', padding:'20px', textAlign:'center', marginBottom:'16px' }}, |
| 106 |
el('div', { style:{fontSize:'13px',fontWeight:600,color:a.winnerColor||'#fff',opacity:.8,marginBottom:'6px'} }, '🎉 Winner!'), |
| 107 |
el('div', { className:'bkbg-rpk-winner-text', style:{color:a.winnerColor||'#fff'} }, winner) |
| 108 |
), |
| 109 |
|
| 110 |
// History |
| 111 |
a.showHistory && history.length > 0 && el('div', { style:{background:a.historyBg||'#f9fafb',borderRadius:(a.itemRadius||8)+'px',padding:'14px 16px'} }, |
| 112 |
el('div', { style:{fontSize:'12px',fontWeight:700,textTransform:'uppercase',letterSpacing:'.04em',color:a.labelColor,marginBottom:'8px'} }, 'Pick History'), |
| 113 |
el('div', { style:{display:'flex',flexWrap:'wrap',gap:'6px'} }, |
| 114 |
history.map(function(h, i) { |
| 115 |
return el('span', { key:String(i)+h, style:{padding:'4px 10px',background:'#ffffff',borderRadius:'100px',fontSize:'13px',color:a.historyColor||'#6b7280',fontWeight:500,border:'1px solid #e5e7eb'} }, h); |
| 116 |
}) |
| 117 |
) |
| 118 |
) |
| 119 |
) |
| 120 |
); |
| 121 |
} |
| 122 |
|
| 123 |
registerBlockType('blockenberg/random-picker', { |
| 124 |
edit: function(props) { |
| 125 |
var a = props.attributes; var set = props.setAttributes; |
| 126 |
var TC = getTypoControl(); |
| 127 |
var blockProps = useBlockProps((function() { |
| 128 |
var _tvFn = getTypoCssVars(); |
| 129 |
var s = {}; |
| 130 |
if (_tvFn) { |
| 131 |
Object.assign(s, _tvFn(a.titleTypo || {}, '--bkrpk-tt-')); |
| 132 |
Object.assign(s, _tvFn(a.subtitleTypo || {}, '--bkrpk-st-')); |
| 133 |
Object.assign(s, _tvFn(a.winnerTypo || {}, '--bkrpk-wn-')); |
| 134 |
} |
| 135 |
return { style: s }; |
| 136 |
})()); |
| 137 |
var colorSettings = [ |
| 138 |
{ value: a.accentColor, onChange: function(v){ set({accentColor:v}); }, label: 'Accent / Button' }, |
| 139 |
{ value: a.cardBg, onChange: function(v){ set({cardBg:v}); }, label: 'Card Background' }, |
| 140 |
{ value: a.winnerBg, onChange: function(v){ set({winnerBg:v}); }, label: 'Winner Background' }, |
| 141 |
{ value: a.winnerColor, onChange: function(v){ set({winnerColor:v}); }, label: 'Winner Text' }, |
| 142 |
{ value: a.itemBg, onChange: function(v){ set({itemBg:v}); }, label: 'Item Chip Background' }, |
| 143 |
{ value: a.itemColor, onChange: function(v){ set({itemColor:v}); }, label: 'Item Chip Text' }, |
| 144 |
{ value: a.highlightBg, onChange: function(v){ set({highlightBg:v}); }, label: 'Highlight Background' }, |
| 145 |
{ value: a.historyBg, onChange: function(v){ set({historyBg:v}); }, label: 'History Background' }, |
| 146 |
{ value: a.historyColor, onChange: function(v){ set({historyColor:v}); }, label: 'History Text' }, |
| 147 |
{ value: a.labelColor, onChange: function(v){ set({labelColor:v}); }, label: 'Label Color' }, |
| 148 |
{ value: a.titleColor, onChange: function(v){ set({titleColor:v}); }, label: 'Title Color' }, |
| 149 |
{ value: a.subtitleColor, onChange: function(v){ set({subtitleColor:v}); }, label: 'Subtitle Color' }, |
| 150 |
{ value: a.sectionBg, onChange: function(v){ set({sectionBg:v}); }, label: 'Section Background' } |
| 151 |
]; |
| 152 |
return el(Fragment, null, |
| 153 |
el(InspectorControls, null, |
| 154 |
el(PanelBody, { title: 'Header', initialOpen: false }, |
| 155 |
el(ToggleControl, { __nextHasNoMarginBottom: true, label: 'Show Title', checked: a.showTitle, onChange: function(v){ set({showTitle:v}); } }), |
| 156 |
el(ToggleControl, { __nextHasNoMarginBottom: true, label: 'Show Subtitle', checked: a.showSubtitle, onChange: function(v){ set({showSubtitle:v}); } }), |
| 157 |
el(TextControl, { label: 'Title', value: a.title, onChange: function(v){ set({title:v}); } }), |
| 158 |
el(TextControl, { label: 'Subtitle', value: a.subtitle, onChange: function(v){ set({subtitle:v}); } }) |
| 159 |
), |
| 160 |
el(PanelBody, { title: 'Picker Settings', initialOpen: true }, |
| 161 |
el(TextControl, { label: 'Button Label', value: a.buttonLabel, onChange: function(v){ set({buttonLabel:v}); } }), |
| 162 |
el(SelectControl, { label: 'Item Separator', value: a.inputMode, options:[{label:'One Per Line',value:'lines'},{label:'Comma Separated',value:'comma'}], onChange: function(v){ set({inputMode:v}); } }), |
| 163 |
el(ToggleControl, { __nextHasNoMarginBottom: true, label: 'Allow Re-pick', checked: a.allowRepick, onChange: function(v){ set({allowRepick:v}); } }), |
| 164 |
el(ToggleControl, { __nextHasNoMarginBottom: true, label: 'Show Pick History', checked: a.showHistory, onChange: function(v){ set({showHistory:v}); } }), |
| 165 |
el(RangeControl, { label: 'Animation Steps', value: a.animSteps, min:4, max:30, step:1, onChange: function(v){ set({animSteps:v}); } }), |
| 166 |
el(RangeControl, { label: 'Animation Speed (ms)', value: a.animSpeedMs, min:20, max:200, step:10, onChange: function(v){ set({animSpeedMs:v}); } }) |
| 167 |
), |
| 168 |
|
| 169 |
el( PanelBody, { title: __( 'Typography', 'blockenberg' ), initialOpen: false }, |
| 170 |
TC && el(TC, { label: __('Title', 'blockenberg'), value: a.titleTypo || {}, onChange: function(v) { set({ titleTypo: v }); } }), |
| 171 |
TC && el(TC, { label: __('Subtitle', 'blockenberg'), value: a.subtitleTypo || {}, onChange: function(v) { set({ subtitleTypo: v }); } }), |
| 172 |
TC && el(TC, { label: __('Winner', 'blockenberg'), value: a.winnerTypo || {}, onChange: function(v) { set({ winnerTypo: v }); } }) |
| 173 |
), |
| 174 |
el(PanelColorSettings, { title: 'Colors', initialOpen: false, colorSettings: colorSettings }), |
| 175 |
el(PanelBody, { title: 'Sizing & Layout', initialOpen: false }, |
| 176 |
el(RangeControl, { label: 'Card Border Radius',value: a.cardRadius, min:0, max:40, step:1, onChange: function(v){ set({cardRadius:v}); } }), |
| 177 |
el(RangeControl, { label: 'Item Border Radius', value: a.itemRadius, min:0, max:24, step:1, onChange: function(v){ set({itemRadius:v}); } }), |
| 178 |
el(RangeControl, { label: 'Max Width (px)', value: a.maxWidth, min:300,max:900, step:10, onChange: function(v){ set({maxWidth:v}); } }), |
| 179 |
el(RangeControl, { label: 'Padding Top (px)', value: a.paddingTop, min:0, max:160, step:4, onChange: function(v){ set({paddingTop:v}); } }), |
| 180 |
el(RangeControl, { label: 'Padding Bottom (px)',value: a.paddingBottom, min:0, max:160, step:4, onChange: function(v){ set({paddingBottom:v}); } }) |
| 181 |
) |
| 182 |
), |
| 183 |
el('div', blockProps, el(RandomPickerPreview, { attributes: a, setAttributes: set })) |
| 184 |
); |
| 185 |
}, |
| 186 |
save: function(props) { |
| 187 |
var a = props.attributes; |
| 188 |
return el('div', wp.blockEditor.useBlockProps.save(), el('div', { className: 'bkbg-rpk-app', 'data-opts': JSON.stringify(a) })); |
| 189 |
} |
| 190 |
}); |
| 191 |
}() ); |
| 192 |
|