| 1 |
( function () { |
| 2 |
var el = wp.element.createElement; |
| 3 |
var Fragment = wp.element.Fragment; |
| 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 ToggleControl = wp.components.ToggleControl; |
| 10 |
var RangeControl = wp.components.RangeControl; |
| 11 |
var SelectControl = wp.components.SelectControl; |
| 12 |
var TextControl = wp.components.TextControl; |
| 13 |
var TextareaControl = wp.components.TextareaControl; |
| 14 |
var Button = wp.components.Button; |
| 15 |
|
| 16 |
function getTypographyControl() { return (window.bkbgTypographyControl || function () { return null; }); } |
| 17 |
function getTypoCssVars() { return (window.bkbgTypoCssVars || function () { return {}; }); } |
| 18 |
function _tv(typo, prefix) { var fn = getTypoCssVars(); return fn(typo || {}, prefix); } |
| 19 |
var IP = function () { return window.bkbgIconPicker; }; |
| 20 |
|
| 21 |
function upd(arr, idx, field, val) { |
| 22 |
return arr.map(function (e, i) { |
| 23 |
if (i !== idx) return e; |
| 24 |
var u = {}; u[field] = val; |
| 25 |
return Object.assign({}, e, u); |
| 26 |
}); |
| 27 |
} |
| 28 |
|
| 29 |
var winnerOptions = [ |
| 30 |
{ label: 'A wins', value: 'A' }, |
| 31 |
{ label: 'B wins', value: 'B' }, |
| 32 |
{ label: 'Tie', value: 'tie' } |
| 33 |
]; |
| 34 |
|
| 35 |
var verdictOptions = [ |
| 36 |
{ label: 'A is recommended', value: 'A' }, |
| 37 |
{ label: 'B is recommended', value: 'B' }, |
| 38 |
{ label: 'Tie / Depends', value: 'tie' } |
| 39 |
]; |
| 40 |
|
| 41 |
function winnerBadge(winner, nameA, nameB, a) { |
| 42 |
if (winner === 'tie') return el('span', { style: { background: a.tieBg, color: a.tieColor, padding: '2px 8px', borderRadius: 100, fontSize: 11, fontWeight: 700 } }, '— Tie'); |
| 43 |
var name = winner === 'A' ? nameA : nameB; |
| 44 |
return el('span', { style: { background: a.winnerBadgeBg, color: a.winnerBadgeColor, padding: '2px 8px', borderRadius: 100, fontSize: 11, fontWeight: 700 } }, '🏆 ' + name); |
| 45 |
} |
| 46 |
|
| 47 |
function scoreBar(score) { |
| 48 |
return el('div', { style: { display: 'flex', alignItems: 'center', gap: 6 } }, |
| 49 |
el('div', { style: { flex: 1, height: 6, background: '#e5e7eb', borderRadius: 100 } }, |
| 50 |
el('div', { style: { width: score * 10 + '%', height: '100%', background: '#3b82f6', borderRadius: 100, transition: 'width .3s' } }) |
| 51 |
), |
| 52 |
el('span', { style: { fontSize: 11, fontWeight: 700, color: '#374151', minWidth: 20 } }, score) |
| 53 |
); |
| 54 |
} |
| 55 |
|
| 56 |
registerBlockType('blockenberg/comparison-guide', { |
| 57 |
edit: function (props) { |
| 58 |
var a = props.attributes; |
| 59 |
var set = props.setAttributes; |
| 60 |
var blockProps = useBlockProps({ className: 'bkbg-cg-editor-wrap' }); |
| 61 |
|
| 62 |
var verdictName = a.verdictWinner === 'A' ? a.nameA : (a.verdictWinner === 'B' ? a.nameB : 'Tie'); |
| 63 |
|
| 64 |
var preview = el('div', { style: { border: '1px solid ' + a.borderColor, borderRadius: a.borderRadius + 'px', overflow: 'hidden', background: a.bgColor } }, |
| 65 |
// Guide header |
| 66 |
el('div', { style: { background: a.headerBg, padding: '22px 28px 18px', borderBottom: '1px solid ' + a.borderColor } }, |
| 67 |
el('h2', { style: { margin: '0 0 8px', fontSize: a.titleFontSize + 'px', color: a.titleColor, lineHeight: 1.2 } }, a.guideTitle), |
| 68 |
a.showIntro && a.intro && el('p', { style: { margin: 0, fontSize: a.fontSize + 'px', color: a.introColor, lineHeight: (a.lineHeight / 100).toFixed(2), fontWeight: a.fontWeight } }, a.intro) |
| 69 |
), |
| 70 |
// Two-column header (A vs B) |
| 71 |
el('div', { style: { display: 'grid', gridTemplateColumns: '1fr auto 1fr', alignItems: 'stretch', borderBottom: '1px solid ' + a.borderColor } }, |
| 72 |
// Column A |
| 73 |
el('div', { style: { background: a.colABg, padding: '18px 22px', textAlign: 'center' } }, |
| 74 |
el('div', { className: 'bkbg-cg-emoji', style: { fontSize: 32, marginBottom: 6 } }, IP().buildEditorIcon(a.emojiAType || 'custom-char', a.emojiA, a.emojiADashicon, a.emojiAImageUrl, a.emojiADashiconColor)), |
| 75 |
el('div', { style: { fontWeight: 800, fontSize: 20, color: a.colAColor, marginBottom: 4 } }, a.nameA), |
| 76 |
el('div', { style: { fontSize: 12, color: a.introColor, marginBottom: a.showScores ? 10 : 0 } }, a.descA), |
| 77 |
a.showScores && el('div', null, |
| 78 |
el('div', { style: { fontSize: 28, fontWeight: 800, color: a.colAColor } }, a.scoreA), |
| 79 |
el('div', { style: { fontSize: 10, color: a.introColor, textTransform: 'uppercase', letterSpacing: '.06em' } }, 'Overall Score') |
| 80 |
) |
| 81 |
), |
| 82 |
// VS divider |
| 83 |
el('div', { style: { display: 'flex', alignItems: 'center', justifyContent: 'center', padding: '0 12px', background: '#0f172a', color: '#fff', fontWeight: 800, fontSize: 16, letterSpacing: '.05em', writingMode: 'vertical-lr', textOrientation: 'mixed' } }, 'VS'), |
| 84 |
// Column B |
| 85 |
el('div', { style: { background: a.colBBg, padding: '18px 22px', textAlign: 'center' } }, |
| 86 |
el('div', { className: 'bkbg-cg-emoji', style: { fontSize: 32, marginBottom: 6 } }, IP().buildEditorIcon(a.emojiBType || 'custom-char', a.emojiB, a.emojiBDashicon, a.emojiBImageUrl, a.emojiBDashiconColor)), |
| 87 |
el('div', { style: { fontWeight: 800, fontSize: 20, color: a.colBColor, marginBottom: 4 } }, a.nameB), |
| 88 |
el('div', { style: { fontSize: 12, color: a.introColor, marginBottom: a.showScores ? 10 : 0 } }, a.descB), |
| 89 |
a.showScores && el('div', null, |
| 90 |
el('div', { style: { fontSize: 28, fontWeight: 800, color: a.colBColor } }, a.scoreB), |
| 91 |
el('div', { style: { fontSize: 10, color: a.introColor, textTransform: 'uppercase', letterSpacing: '.06em' } }, 'Overall Score') |
| 92 |
) |
| 93 |
) |
| 94 |
), |
| 95 |
// Criteria table header |
| 96 |
el('div', { style: { display: 'grid', gridTemplateColumns: '2fr 1fr 1fr 1fr', background: '#0f172a', padding: '8px 16px', gap: 4 } }, |
| 97 |
el('div', { style: { fontSize: 10, fontWeight: 700, textTransform: 'uppercase', letterSpacing: '.07em', color: '#94a3b8' } }, 'Criterion'), |
| 98 |
el('div', { style: { fontSize: 10, fontWeight: 700, textTransform: 'uppercase', letterSpacing: '.07em', color: '#94a3b8', textAlign: 'center' } }, a.nameA), |
| 99 |
el('div', { style: { fontSize: 10, fontWeight: 700, textTransform: 'uppercase', letterSpacing: '.07em', color: '#94a3b8', textAlign: 'center' } }, a.nameB), |
| 100 |
el('div', { style: { fontSize: 10, fontWeight: 700, textTransform: 'uppercase', letterSpacing: '.07em', color: '#94a3b8', textAlign: 'center' } }, 'Winner') |
| 101 |
), |
| 102 |
// Criteria rows |
| 103 |
el('div', null, |
| 104 |
a.criteria.map(function (crit, i) { |
| 105 |
var isAlt = i % 2 === 1; |
| 106 |
return el('div', { key: i }, |
| 107 |
el('div', { style: { display: 'grid', gridTemplateColumns: '2fr 1fr 1fr 1fr', padding: '10px 16px', gap: 4, background: isAlt ? a.rowAltBg : a.bgColor, borderBottom: '1px solid ' + a.borderColor, alignItems: 'center' } }, |
| 108 |
el('div', { style: { fontWeight: 600, fontSize: a.fontSize + 'px', color: a.titleColor } }, crit.name), |
| 109 |
a.showCritScores |
| 110 |
? el('div', { style: { textAlign: 'center', fontWeight: 700, fontSize: 16, color: a.colAColor } }, crit.scoreA) |
| 111 |
: el('div'), |
| 112 |
a.showCritScores |
| 113 |
? el('div', { style: { textAlign: 'center', fontWeight: 700, fontSize: 16, color: a.colBColor } }, crit.scoreB) |
| 114 |
: el('div'), |
| 115 |
el('div', { style: { textAlign: 'center' } }, winnerBadge(crit.winner, a.nameA, a.nameB, a)) |
| 116 |
), |
| 117 |
a.showNotes && crit.notes && el('div', { style: { padding: '0 16px 10px', background: isAlt ? a.rowAltBg : a.bgColor, fontSize: (a.fontSize - 1) + 'px', color: a.notesColor, lineHeight: (a.lineHeight / 100).toFixed(2), fontStyle: 'italic', borderBottom: '1px solid ' + a.borderColor } }, crit.notes) |
| 118 |
); |
| 119 |
}) |
| 120 |
), |
| 121 |
// Verdict |
| 122 |
a.showVerdict && el('div', { style: { background: a.verdictBg, padding: '18px 24px', color: a.verdictColor } }, |
| 123 |
el('div', { style: { fontSize: 10, textTransform: 'uppercase', letterSpacing: '.1em', opacity: .55, marginBottom: 6 } }, 'Verdict'), |
| 124 |
el('div', { style: { display: 'flex', alignItems: 'center', gap: 10, marginBottom: 8 } }, |
| 125 |
el('span', { style: { fontWeight: 800, fontSize: 20 } }, verdictName + ' wins'), |
| 126 |
a.verdictWinner !== 'tie' && el('span', { className: 'bkbg-cg-emoji', style: { fontSize: 20 } }, a.verdictWinner === 'A' |
| 127 |
? IP().buildEditorIcon(a.emojiAType || 'custom-char', a.emojiA, a.emojiADashicon, a.emojiAImageUrl, a.emojiADashiconColor) |
| 128 |
: IP().buildEditorIcon(a.emojiBType || 'custom-char', a.emojiB, a.emojiBDashicon, a.emojiBImageUrl, a.emojiBDashiconColor) |
| 129 |
) |
| 130 |
), |
| 131 |
a.verdictSummary && el('p', { style: { margin: 0, fontSize: a.fontSize + 'px', opacity: .85, lineHeight: (a.lineHeight / 100).toFixed(2), fontWeight: a.fontWeight } }, a.verdictSummary) |
| 132 |
) |
| 133 |
); |
| 134 |
|
| 135 |
return el(Fragment, null, |
| 136 |
el('div', blockProps, preview), |
| 137 |
el(InspectorControls, null, |
| 138 |
// Guide Info |
| 139 |
el(PanelBody, { title: 'Guide Info', initialOpen: true }, |
| 140 |
el(TextControl, { label: 'Guide Title', value: a.guideTitle, __nextHasNoMarginBottom: true, onChange: function (v) { set({ guideTitle: v }); } }), |
| 141 |
el('div', { style: { marginTop: 8 } }, |
| 142 |
el(ToggleControl, { label: 'Show Introduction', checked: a.showIntro, __nextHasNoMarginBottom: true, onChange: function (v) { set({ showIntro: v }); } }) |
| 143 |
), |
| 144 |
el('div', { style: { marginTop: 8 } }, |
| 145 |
el(TextareaControl, { label: 'Introduction', value: a.intro, rows: 3, __nextHasNoMarginBottom: true, onChange: function (v) { set({ intro: v }); } }) |
| 146 |
) |
| 147 |
), |
| 148 |
// Item A |
| 149 |
el(PanelBody, { title: 'Item A', initialOpen: false }, |
| 150 |
el(TextControl, { label: 'Name', value: a.nameA, __nextHasNoMarginBottom: true, onChange: function (v) { set({ nameA: v }); } }), |
| 151 |
el('div', { style: { marginTop: 8 } }, |
| 152 |
el(IP().IconPickerControl, IP().iconPickerProps(a, set, { charAttr: 'emojiA', typeAttr: 'emojiAType', dashiconAttr: 'emojiADashicon', imageUrlAttr: 'emojiAImageUrl', colorAttr: 'emojiADashiconColor' })) |
| 153 |
), |
| 154 |
el('div', { style: { marginTop: 8 } }, |
| 155 |
el(TextareaControl, { label: 'Description', value: a.descA, rows: 2, __nextHasNoMarginBottom: true, onChange: function (v) { set({ descA: v }); } }) |
| 156 |
), |
| 157 |
el('div', { style: { marginTop: 8 } }, |
| 158 |
el(ToggleControl, { label: 'Show Overall Scores', checked: a.showScores, __nextHasNoMarginBottom: true, onChange: function (v) { set({ showScores: v }); } }) |
| 159 |
), |
| 160 |
a.showScores && el('div', { style: { marginTop: 8 } }, |
| 161 |
el(RangeControl, { label: 'Score A (out of 100)', value: a.scoreA, min: 0, max: 100, __nextHasNoMarginBottom: true, onChange: function (v) { set({ scoreA: v }); } }) |
| 162 |
) |
| 163 |
), |
| 164 |
// Item B |
| 165 |
el(PanelBody, { title: 'Item B', initialOpen: false }, |
| 166 |
el(TextControl, { label: 'Name', value: a.nameB, __nextHasNoMarginBottom: true, onChange: function (v) { set({ nameB: v }); } }), |
| 167 |
el('div', { style: { marginTop: 8 } }, |
| 168 |
el(IP().IconPickerControl, IP().iconPickerProps(a, set, { charAttr: 'emojiB', typeAttr: 'emojiBType', dashiconAttr: 'emojiBDashicon', imageUrlAttr: 'emojiBImageUrl', colorAttr: 'emojiBDashiconColor' })) |
| 169 |
), |
| 170 |
el('div', { style: { marginTop: 8 } }, |
| 171 |
el(TextareaControl, { label: 'Description', value: a.descB, rows: 2, __nextHasNoMarginBottom: true, onChange: function (v) { set({ descB: v }); } }) |
| 172 |
), |
| 173 |
a.showScores && el('div', { style: { marginTop: 8 } }, |
| 174 |
el(RangeControl, { label: 'Score B (out of 100)', value: a.scoreB, min: 0, max: 100, __nextHasNoMarginBottom: true, onChange: function (v) { set({ scoreB: v }); } }) |
| 175 |
) |
| 176 |
), |
| 177 |
// Criteria |
| 178 |
el(PanelBody, { title: 'Criteria', initialOpen: false }, |
| 179 |
el('div', { style: { display: 'grid', gridTemplateColumns: '1fr 1fr', gap: 4, marginBottom: 8 } }, |
| 180 |
el(ToggleControl, { label: 'Show Scores', checked: a.showCritScores, __nextHasNoMarginBottom: true, onChange: function (v) { set({ showCritScores: v }); } }), |
| 181 |
el(ToggleControl, { label: 'Show Notes', checked: a.showNotes, __nextHasNoMarginBottom: true, onChange: function (v) { set({ showNotes: v }); } }) |
| 182 |
), |
| 183 |
a.criteria.map(function (crit, i) { |
| 184 |
return el('div', { key: i, style: { marginBottom: 12, padding: '10px 12px', border: '1px solid #e5e7eb', borderRadius: 8, background: '#f8fafc' } }, |
| 185 |
el('div', { style: { display: 'flex', justifyContent: 'space-between', marginBottom: 8, alignItems: 'center' } }, |
| 186 |
el('strong', { style: { fontSize: 11, color: '#374151' } }, 'Criterion ' + (i + 1)), |
| 187 |
el(Button, { isDestructive: true, variant: 'link', style: { fontSize: 11 }, onClick: function () { set({ criteria: a.criteria.filter(function (_, j) { return j !== i; }) }); } }, '✕') |
| 188 |
), |
| 189 |
el(TextControl, { label: 'Name', value: crit.name, __nextHasNoMarginBottom: true, onChange: function (v) { set({ criteria: upd(a.criteria, i, 'name', v) }); } }), |
| 190 |
el('div', { style: { marginTop: 8 } }, |
| 191 |
el(SelectControl, { label: 'Winner', value: crit.winner, options: winnerOptions, __nextHasNoMarginBottom: true, onChange: function (v) { set({ criteria: upd(a.criteria, i, 'winner', v) }); } }) |
| 192 |
), |
| 193 |
a.showCritScores && el('div', { style: { marginTop: 8, display: 'grid', gridTemplateColumns: '1fr 1fr', gap: 8 } }, |
| 194 |
el(RangeControl, { label: a.nameA + ' Score', value: crit.scoreA, min: 0, max: 10, __nextHasNoMarginBottom: true, onChange: function (v) { set({ criteria: upd(a.criteria, i, 'scoreA', v) }); } }), |
| 195 |
el(RangeControl, { label: a.nameB + ' Score', value: crit.scoreB, min: 0, max: 10, __nextHasNoMarginBottom: true, onChange: function (v) { set({ criteria: upd(a.criteria, i, 'scoreB', v) }); } }) |
| 196 |
), |
| 197 |
a.showNotes && el('div', { style: { marginTop: 8 } }, |
| 198 |
el(TextareaControl, { label: 'Notes', value: crit.notes, rows: 2, __nextHasNoMarginBottom: true, onChange: function (v) { set({ criteria: upd(a.criteria, i, 'notes', v) }); } }) |
| 199 |
) |
| 200 |
); |
| 201 |
}), |
| 202 |
el(Button, { variant: 'primary', style: { width: '100%', justifyContent: 'center', marginTop: 4 }, onClick: function () { |
| 203 |
set({ criteria: a.criteria.concat([{ name: 'New Criterion', winner: 'tie', scoreA: 5, scoreB: 5, notes: '' }]) }); |
| 204 |
} }, '+ Add Criterion') |
| 205 |
), |
| 206 |
// Verdict |
| 207 |
el(PanelBody, { title: 'Verdict', initialOpen: false }, |
| 208 |
el(ToggleControl, { label: 'Show Verdict', checked: a.showVerdict, __nextHasNoMarginBottom: true, onChange: function (v) { set({ showVerdict: v }); } }), |
| 209 |
el('div', { style: { marginTop: 8 } }, |
| 210 |
el(SelectControl, { label: 'Recommended Winner', value: a.verdictWinner, options: verdictOptions, __nextHasNoMarginBottom: true, onChange: function (v) { set({ verdictWinner: v }); } }) |
| 211 |
), |
| 212 |
el('div', { style: { marginTop: 8 } }, |
| 213 |
el(TextareaControl, { label: 'Verdict Summary', value: a.verdictSummary, rows: 4, __nextHasNoMarginBottom: true, onChange: function (v) { set({ verdictSummary: v }); } }) |
| 214 |
) |
| 215 |
), |
| 216 |
// Typography |
| 217 |
el(PanelBody, { title: 'Typography', initialOpen: false }, |
| 218 |
el(getTypographyControl(), { label: __('Title Typography', 'blockenberg'), value: a.typoTitle, onChange: function (v) { set({ typoTitle: v }); } }), |
| 219 |
el(getTypographyControl(), { label: __('Body Typography', 'blockenberg'), value: a.typoBody, onChange: function (v) { set({ typoBody: v }); } }) |
| 220 |
), |
| 221 |
el(PanelBody, { title: 'Spacing', initialOpen: false }, |
| 222 |
el(RangeControl, { label: 'Border Radius (px)', value: a.borderRadius, min: 0, max: 24, __nextHasNoMarginBottom: true, onChange: function (v) { set({ borderRadius: v }); } }) |
| 223 |
), |
| 224 |
// Colors |
| 225 |
el(PanelColorSettings, { |
| 226 |
title: 'Layout Colors', |
| 227 |
initialOpen: false, |
| 228 |
colorSettings: [ |
| 229 |
{ label: 'Background', value: a.bgColor, onChange: function (v) { set({ bgColor: v || '#ffffff' }); } }, |
| 230 |
{ label: 'Border', value: a.borderColor, onChange: function (v) { set({ borderColor: v || '#e5e7eb' }); } }, |
| 231 |
{ label: 'Header BG', value: a.headerBg, onChange: function (v) { set({ headerBg: v || '#f8fafc' }); } }, |
| 232 |
{ label: 'Title', value: a.titleColor, onChange: function (v) { set({ titleColor: v || '#0f172a' }); } }, |
| 233 |
{ label: 'Intro Text', value: a.introColor, onChange: function (v) { set({ introColor: v || '#374151' }); } }, |
| 234 |
{ label: 'Row Alt BG', value: a.rowAltBg, onChange: function (v) { set({ rowAltBg: v || '#f8fafc' }); } }, |
| 235 |
{ label: 'Notes Text', value: a.notesColor, onChange: function (v) { set({ notesColor: v || '#6b7280' }); } } |
| 236 |
] |
| 237 |
}), |
| 238 |
el(PanelColorSettings, { |
| 239 |
title: 'Item & Badge Colors', |
| 240 |
initialOpen: false, |
| 241 |
colorSettings: [ |
| 242 |
{ label: 'A Column BG', value: a.colABg, onChange: function (v) { set({ colABg: v || '#eff6ff' }); } }, |
| 243 |
{ label: 'A Column Text', value: a.colAColor, onChange: function (v) { set({ colAColor: v || '#1e3a8a' }); } }, |
| 244 |
{ label: 'B Column BG', value: a.colBBg, onChange: function (v) { set({ colBBg: v || '#f0fdf4' }); } }, |
| 245 |
{ label: 'B Column Text', value: a.colBColor, onChange: function (v) { set({ colBColor: v || '#14532d' }); } }, |
| 246 |
{ label: 'Winner Badge BG', value: a.winnerBadgeBg, onChange: function (v) { set({ winnerBadgeBg: v || '#fef9c3' }); } }, |
| 247 |
{ label: 'Winner Badge Text',value: a.winnerBadgeColor,onChange: function (v) { set({ winnerBadgeColor: v || '#713f12' }); } }, |
| 248 |
{ label: 'Tie Badge BG', value: a.tieBg, onChange: function (v) { set({ tieBg: v || '#f1f5f9' }); } }, |
| 249 |
{ label: 'Tie Badge Text', value: a.tieColor, onChange: function (v) { set({ tieColor: v || '#475569' }); } }, |
| 250 |
{ label: 'Verdict BG', value: a.verdictBg, onChange: function (v) { set({ verdictBg: v || '#0f172a' }); } }, |
| 251 |
{ label: 'Verdict Text', value: a.verdictColor, onChange: function (v) { set({ verdictColor: v || '#ffffff' }); } } |
| 252 |
] |
| 253 |
}) |
| 254 |
) |
| 255 |
); |
| 256 |
}, |
| 257 |
|
| 258 |
save: function (props) { |
| 259 |
var a = props.attributes; |
| 260 |
var useBlockProps = wp.blockEditor.useBlockProps; |
| 261 |
var sv = Object.assign({}, _tv(a.typoTitle, '--bkcg-title-'), _tv(a.typoBody, '--bkcg-body-')); |
| 262 |
return wp.element.createElement('div', useBlockProps.save({ style: sv }), |
| 263 |
wp.element.createElement('div', { |
| 264 |
className: 'bkbg-comparison-guide-app', |
| 265 |
'data-opts': JSON.stringify(a) |
| 266 |
}) |
| 267 |
); |
| 268 |
} |
| 269 |
}); |
| 270 |
}() ); |
| 271 |
|