| 1 |
( function () { |
| 2 |
var el = wp.element.createElement; |
| 3 |
var Fragment = wp.element.Fragment; |
| 4 |
var registerBlockType = wp.blocks.registerBlockType; |
| 5 |
var __ = wp.i18n.__; |
| 6 |
var InspectorControls = wp.blockEditor.InspectorControls; |
| 7 |
var PanelColorSettings = wp.blockEditor.PanelColorSettings; |
| 8 |
var useBlockProps = wp.blockEditor.useBlockProps; |
| 9 |
var RichText = wp.blockEditor.RichText; |
| 10 |
var PanelBody = wp.components.PanelBody; |
| 11 |
var RangeControl = wp.components.RangeControl; |
| 12 |
var ToggleControl = wp.components.ToggleControl; |
| 13 |
var SelectControl = wp.components.SelectControl; |
| 14 |
|
| 15 |
var _tc, _tvf; |
| 16 |
Object.defineProperty(window, '_bkbgTypoCtrlCache', { get: function () { if (!_tc) { _tc = window.bkbgTypographyControl; } return _tc; } }); |
| 17 |
Object.defineProperty(window, '_bkbgTypoVarsCache', { get: function () { if (!_tvf) { _tvf = window.bkbgTypoCssVars; } return _tvf; } }); |
| 18 |
function getTypoControl(props, attrName, label) { return window._bkbgTypoCtrlCache(props, attrName, label); } |
| 19 |
|
| 20 |
var CATEGORIES = [ |
| 21 |
{ label: 'Mixed (all)', value: 'mixed' }, |
| 22 |
{ label: 'Science & Nature', value: 'science' }, |
| 23 |
{ label: 'History', value: 'history' }, |
| 24 |
{ label: 'Geography', value: 'geography' }, |
| 25 |
{ label: 'Pop Culture & Entertainment', value: 'popculture' }, |
| 26 |
{ label: 'Sports', value: 'sports' }, |
| 27 |
{ label: 'Technology', value: 'technology' } |
| 28 |
]; |
| 29 |
|
| 30 |
var DEMO_Q = 'How many bones are in the adult human body?'; |
| 31 |
var DEMO_A = ['206', '187', '215', '232']; |
| 32 |
|
| 33 |
function EditorPreview(props) { |
| 34 |
var a = props.attributes; |
| 35 |
var setAttributes = props.setAttributes; |
| 36 |
var blockProps = (function () { |
| 37 |
var s = { background: a.sectionBg || '#f0f4ff', padding: '28px 20px' }; |
| 38 |
Object.assign(s, window._bkbgTypoVarsCache(a.titleTypo, '--bktrv-tt-'), window._bkbgTypoVarsCache(a.subtitleTypo, '--bktrv-st-')); |
| 39 |
return useBlockProps({ style: s }); |
| 40 |
})(); |
| 41 |
|
| 42 |
return el(Fragment, null, |
| 43 |
el(InspectorControls, null, |
| 44 |
el(PanelBody, { title: __('Content', 'blockenberg'), initialOpen: true }, |
| 45 |
), |
| 46 |
el(PanelBody, { title: __('Game Settings', 'blockenberg'), initialOpen: false }, |
| 47 |
el(SelectControl, { label: __('Category', 'blockenberg'), value: a.category, options: CATEGORIES, |
| 48 |
onChange: function (v) { setAttributes({ category: v }); } }), |
| 49 |
el(SelectControl, { label: __('Difficulty', 'blockenberg'), value: a.difficulty, |
| 50 |
options: [{ label: 'Mixed', value: 'mixed' }, { label: 'Easy', value: 'easy' }, { label: 'Medium', value: 'medium' }, { label: 'Hard', value: 'hard' }], |
| 51 |
onChange: function (v) { setAttributes({ difficulty: v }); } }), |
| 52 |
el(RangeControl, { label: __('Questions per Round', 'blockenberg'), value: a.questionsPerRound, min: 3, max: 30, |
| 53 |
onChange: function (v) { setAttributes({ questionsPerRound: v }); } }), |
| 54 |
el(RangeControl, { label: __('Seconds per Question', 'blockenberg'), value: a.timePerQuestion, min: 5, max: 60, |
| 55 |
onChange: function (v) { setAttributes({ timePerQuestion: v }); } }), |
| 56 |
el(ToggleControl, { __nextHasNoMarginBottom: true, label: __('Show Timer', 'blockenberg'), checked: a.showTimer, onChange: function (v) { setAttributes({ showTimer: v }); } }), |
| 57 |
el(ToggleControl, { __nextHasNoMarginBottom: true, label: __('Show Progress Bar', 'blockenberg'), checked: a.showProgress, onChange: function (v) { setAttributes({ showProgress: v }); } }), |
| 58 |
el(ToggleControl, { __nextHasNoMarginBottom: true, label: __('Show Answer Explanation', 'blockenberg'), checked: a.showExplanation, onChange: function (v) { setAttributes({ showExplanation: v }); } }) |
| 59 |
), |
| 60 |
|
| 61 |
el( PanelBody, { title: __( 'Typography', 'blockenberg' ), initialOpen: false }, |
| 62 |
getTypoControl(props, 'titleTypo', __('Title', 'blockenberg')), |
| 63 |
getTypoControl(props, 'subtitleTypo', __('Subtitle', 'blockenberg')) |
| 64 |
), |
| 65 |
el(PanelColorSettings, { |
| 66 |
title: __('Colors', 'blockenberg'), initialOpen: false, |
| 67 |
colorSettings: [ |
| 68 |
{ label: __('Accent', 'blockenberg'), value: a.accentColor, onChange: function (v) { setAttributes({ accentColor: v || '#6366f1' }); } }, |
| 69 |
{ label: __('Correct Answer', 'blockenberg'), value: a.correctColor, onChange: function (v) { setAttributes({ correctColor: v || '#22c55e' }); } }, |
| 70 |
{ label: __('Wrong Answer', 'blockenberg'), value: a.wrongColor, onChange: function (v) { setAttributes({ wrongColor: v || '#ef4444' }); } }, |
| 71 |
{ label: __('Card Background', 'blockenberg'), value: a.cardBg, onChange: function (v) { setAttributes({ cardBg: v || '#ffffff' }); } }, |
| 72 |
{ label: __('Section Background', 'blockenberg'), value: a.sectionBg, onChange: function (v) { setAttributes({ sectionBg: v || '#f0f4ff' }); } }, |
| 73 |
{ label: __('Title Color', 'blockenberg'), value: a.titleColor, onChange: function (v) { setAttributes({ titleColor: v || '#1e1b4b' }); } } |
| 74 |
] |
| 75 |
}) |
| 76 |
), |
| 77 |
el('div', blockProps, |
| 78 |
el(RichText, { tagName: 'h3', className: 'bkbg-trv-title', value: a.title, |
| 79 |
onChange: function (v) { setAttributes({ title: v }); }, |
| 80 |
placeholder: 'Trivia Challenge', |
| 81 |
style: { color: a.titleColor, textAlign: 'center', margin: '0 0 4px' } }), |
| 82 |
el(RichText, { tagName: 'p', className: 'bkbg-trv-subtitle', value: a.subtitle, |
| 83 |
onChange: function (v) { setAttributes({ subtitle: v }); }, |
| 84 |
placeholder: 'Test your knowledge!', |
| 85 |
style: { color: a.titleColor, opacity: 0.65, textAlign: 'center', margin: '0 0 18px' } }), |
| 86 |
|
| 87 |
// Question card preview |
| 88 |
el('div', { style: { maxWidth: 640, margin: '0 auto' } }, |
| 89 |
// Progress bar |
| 90 |
a.showProgress && el('div', { style: { display: 'flex', alignItems: 'center', gap: 10, marginBottom: 12 } }, |
| 91 |
el('div', { style: { flex: 1, height: 8, background: '#e5e7eb', borderRadius: 99 } }, |
| 92 |
el('div', { style: { width: '30%', height: '100%', background: a.accentColor, borderRadius: 99, transition: 'width 0.4s' } }) |
| 93 |
), |
| 94 |
el('span', { style: { fontSize: 13, fontWeight: 600, color: a.titleColor, opacity: 0.6 } }, '3 / ' + a.questionsPerRound) |
| 95 |
), |
| 96 |
// Timer |
| 97 |
a.showTimer && el('div', { style: { display: 'flex', justifyContent: 'center', marginBottom: 12 } }, |
| 98 |
el('div', { style: { display: 'flex', alignItems: 'center', gap: 6, background: a.accentColor + '18', borderRadius: 99, padding: '4px 14px' } }, |
| 99 |
el('div', { style: { width: 8, height: 8, borderRadius: '50%', background: a.accentColor, animation: 'none' } }), |
| 100 |
el('span', { style: { fontWeight: 700, fontSize: 15, color: a.accentColor } }, a.timePerQuestion + 's') |
| 101 |
) |
| 102 |
), |
| 103 |
// Question card |
| 104 |
el('div', { style: { background: a.cardBg, borderRadius: 14, boxShadow: '0 2px 20px rgba(0,0,0,0.08)', padding: '24px 20px', marginBottom: 14 } }, |
| 105 |
el('div', { style: { fontSize: 13, color: a.accentColor, fontWeight: 700, textTransform: 'uppercase', letterSpacing: 1, marginBottom: 8 } }, 'Science & Nature'), |
| 106 |
el('div', { style: { fontSize: 19, fontWeight: 700, color: a.titleColor, lineHeight: 1.4, marginBottom: 18 } }, DEMO_Q), |
| 107 |
el('div', { style: { display: 'grid', gridTemplateColumns: '1fr 1fr', gap: 10 } }, |
| 108 |
DEMO_A.map(function (ans, i) { |
| 109 |
return el('button', { |
| 110 |
key: i, |
| 111 |
style: { |
| 112 |
border: '2px solid ' + a.accentColor + '44', |
| 113 |
borderRadius: 10, padding: '12px 14px', |
| 114 |
background: 'transparent', cursor: 'pointer', |
| 115 |
fontSize: 15, fontWeight: 600, color: a.titleColor, |
| 116 |
textAlign: 'left', transition: 'all 0.15s' |
| 117 |
} |
| 118 |
}, ans); |
| 119 |
}) |
| 120 |
) |
| 121 |
) |
| 122 |
) |
| 123 |
) |
| 124 |
); |
| 125 |
} |
| 126 |
|
| 127 |
registerBlockType('blockenberg/trivia-game', { |
| 128 |
edit: EditorPreview, |
| 129 |
save: function (props) { |
| 130 |
var a = props.attributes; |
| 131 |
var bp = (function () { |
| 132 |
var tv = Object.assign({}, window._bkbgTypoVarsCache(a.titleTypo, '--bktrv-tt-'), window._bkbgTypoVarsCache(a.subtitleTypo, '--bktrv-st-')); |
| 133 |
var parts = []; Object.keys(tv).forEach(function (k) { parts.push(k + ':' + tv[k]); }); |
| 134 |
return useBlockProps.save(parts.length ? { style: parts.join(';') } : {}); |
| 135 |
})(); |
| 136 |
return el('div', bp, |
| 137 |
el('div', { className: 'bkbg-trv-app', 'data-opts': JSON.stringify(a) }, |
| 138 |
el(RichText.Content, { tagName: 'h3', className: 'bkbg-trv-title', value: a.title }), |
| 139 |
el(RichText.Content, { tagName: 'p', className: 'bkbg-trv-subtitle', value: a.subtitle }) |
| 140 |
) |
| 141 |
); |
| 142 |
} |
| 143 |
}); |
| 144 |
}() ); |
| 145 |
|