| 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; function getTypoControl() { return _tc || (_tc = window.bkbgTypographyControl); } |
| 16 |
var _tv; function getTypoCssVars() { return _tv || (_tv = window.bkbgTypoCssVars); } |
| 17 |
|
| 18 |
// Demo puzzle for editor preview (0 = empty) |
| 19 |
var DEMO = [ |
| 20 |
[5,3,0, 0,7,0, 0,0,0], |
| 21 |
[6,0,0, 1,9,5, 0,0,0], |
| 22 |
[0,9,8, 0,0,0, 0,6,0], |
| 23 |
|
| 24 |
[8,0,0, 0,6,0, 0,0,3], |
| 25 |
[4,0,0, 8,0,3, 0,0,1], |
| 26 |
[7,0,0, 0,2,0, 0,0,6], |
| 27 |
|
| 28 |
[0,6,0, 0,0,0, 2,8,0], |
| 29 |
[0,0,0, 4,1,9, 0,0,5], |
| 30 |
[0,0,0, 0,8,0, 0,7,9] |
| 31 |
]; |
| 32 |
|
| 33 |
function SudokuGrid(props) { |
| 34 |
var a = props; |
| 35 |
var rows = []; |
| 36 |
for (var r = 0; r < 9; r++) { |
| 37 |
var cells = []; |
| 38 |
for (var c = 0; c < 9; c++) { |
| 39 |
var val = DEMO[r][c]; |
| 40 |
var borderRight = (c === 2 || c === 5) ? '2px solid ' + a.givenColor : '1px solid #d1d5db'; |
| 41 |
var borderBottom = (r === 2 || r === 5) ? '2px solid ' + a.givenColor : '1px solid #d1d5db'; |
| 42 |
cells.push(el('div', { |
| 43 |
key: c, |
| 44 |
style: { |
| 45 |
width: 40, height: 40, |
| 46 |
display: 'flex', alignItems: 'center', justifyContent: 'center', |
| 47 |
fontSize: 18, fontWeight: val ? 700 : 400, |
| 48 |
color: val ? a.givenColor : a.enteredColor, |
| 49 |
background: val ? a.gridBg : a.highlightBg, |
| 50 |
borderRight: borderRight, borderBottom: borderBottom, |
| 51 |
cursor: 'pointer', userSelect: 'none', |
| 52 |
transition: 'background 0.15s' |
| 53 |
} |
| 54 |
}, val || '')); |
| 55 |
} |
| 56 |
rows.push(el('div', { key: r, style: { display: 'flex' } }, cells)); |
| 57 |
} |
| 58 |
return el('div', { |
| 59 |
style: { |
| 60 |
display: 'inline-block', |
| 61 |
border: '2px solid ' + a.givenColor, |
| 62 |
borderRadius: 8, |
| 63 |
overflow: 'hidden', |
| 64 |
background: a.gridBg |
| 65 |
} |
| 66 |
}, rows); |
| 67 |
} |
| 68 |
|
| 69 |
function EditorPreview(props) { |
| 70 |
var a = props.attributes; |
| 71 |
var setAttributes = props.setAttributes; |
| 72 |
var blockProps = useBlockProps((function () { |
| 73 |
var _tvf = getTypoCssVars(); |
| 74 |
var s = { background: a.sectionBg, borderRadius: 16, padding: '28px 20px', textAlign: 'center' }; |
| 75 |
if (_tvf) { |
| 76 |
Object.assign(s, _tvf(a.titleTypo, '--bksdk-tt-')); |
| 77 |
Object.assign(s, _tvf(a.subtitleTypo, '--bksdk-st-')); |
| 78 |
} |
| 79 |
return { className: 'bkbg-sdk-wrap', style: s }; |
| 80 |
})()); |
| 81 |
|
| 82 |
return el(Fragment, null, |
| 83 |
el(InspectorControls, null, |
| 84 |
el(PanelBody, { title: __('Content', 'blockenberg'), initialOpen: true }, |
| 85 |
), |
| 86 |
el(PanelBody, { title: __('Game Settings', 'blockenberg'), initialOpen: false }, |
| 87 |
el(SelectControl, { |
| 88 |
label: __('Default Difficulty', 'blockenberg'), |
| 89 |
value: a.defaultDifficulty, |
| 90 |
options: [ |
| 91 |
{ label: 'Easy', value: 'easy' }, |
| 92 |
{ label: 'Medium', value: 'medium' }, |
| 93 |
{ label: 'Hard', value: 'hard' }, |
| 94 |
{ label: 'Expert', value: 'expert' } |
| 95 |
], |
| 96 |
onChange: function (v) { setAttributes({ defaultDifficulty: v }); } |
| 97 |
}), |
| 98 |
el(RangeControl, { |
| 99 |
label: __('Max Mistakes Allowed', 'blockenberg'), |
| 100 |
value: a.maxMistakes, min: 1, max: 10, |
| 101 |
onChange: function (v) { setAttributes({ maxMistakes: v }); } |
| 102 |
}), |
| 103 |
el(RangeControl, { |
| 104 |
label: __('Hints Allowed', 'blockenberg'), |
| 105 |
value: a.hintsAllowed, min: 0, max: 10, |
| 106 |
onChange: function (v) { setAttributes({ hintsAllowed: v }); } |
| 107 |
}), |
| 108 |
el(ToggleControl, { __nextHasNoMarginBottom: true, label: __('Show Timer', 'blockenberg'), checked: a.showTimer, onChange: function (v) { setAttributes({ showTimer: v }); } }), |
| 109 |
el(ToggleControl, { __nextHasNoMarginBottom: true, label: __('Show Mistake Counter', 'blockenberg'), checked: a.showMistakes, onChange: function (v) { setAttributes({ showMistakes: v }); } }), |
| 110 |
el(ToggleControl, { __nextHasNoMarginBottom: true, label: __('Highlight Related Cells', 'blockenberg'), checked: a.highlightRelated, onChange: function (v) { setAttributes({ highlightRelated: v }); } }), |
| 111 |
el(ToggleControl, { __nextHasNoMarginBottom: true, label: __('Highlight Same Number', 'blockenberg'), checked: a.highlightSameNumber, onChange: function (v) { setAttributes({ highlightSameNumber: v }); } }) |
| 112 |
), |
| 113 |
|
| 114 |
el( PanelBody, { title: __( 'Typography', 'blockenberg' ), initialOpen: false }, |
| 115 |
getTypoControl()({ label: __('Title', 'blockenberg'), value: a.titleTypo, onChange: function (v) { setAttributes({ titleTypo: v }); } }), |
| 116 |
getTypoControl()({ label: __('Subtitle', 'blockenberg'), value: a.subtitleTypo, onChange: function (v) { setAttributes({ subtitleTypo: v }); } }) |
| 117 |
), |
| 118 |
el(PanelColorSettings, { |
| 119 |
title: __('Colors', 'blockenberg'), initialOpen: false, |
| 120 |
colorSettings: [ |
| 121 |
{ label: __('Accent', 'blockenberg'), value: a.accentColor, onChange: function (v) { setAttributes({ accentColor: v || '#6366f1' }); } }, |
| 122 |
{ label: __('Given Numbers', 'blockenberg'), value: a.givenColor, onChange: function (v) { setAttributes({ givenColor: v || '#1e1b4b' }); } }, |
| 123 |
{ label: __('Entered Numbers', 'blockenberg'), value: a.enteredColor, onChange: function (v) { setAttributes({ enteredColor: v || '#6366f1' }); } }, |
| 124 |
{ label: __('Error Color', 'blockenberg'), value: a.errorColor, onChange: function (v) { setAttributes({ errorColor: v || '#ef4444' }); } }, |
| 125 |
{ label: __('Related Highlight', 'blockenberg'), value: a.highlightBg, onChange: function (v) { setAttributes({ highlightBg: v || '#e0e7ff' }); } }, |
| 126 |
{ label: __('Selected Cell', 'blockenberg'), value: a.selectedBg, onChange: function (v) { setAttributes({ selectedBg: v || '#c7d2fe' }); } }, |
| 127 |
{ label: __('Grid Background', 'blockenberg'), value: a.gridBg, onChange: function (v) { setAttributes({ gridBg: v || '#ffffff' }); } }, |
| 128 |
{ label: __('Section Background', 'blockenberg'), value: a.sectionBg, onChange: function (v) { setAttributes({ sectionBg: v || '#f5f3ff' }); } }, |
| 129 |
{ label: __('Title Color', 'blockenberg'), value: a.titleColor, onChange: function (v) { setAttributes({ titleColor: v || '#1e1b4b' }); } } |
| 130 |
] |
| 131 |
}) |
| 132 |
), |
| 133 |
el('div', blockProps, |
| 134 |
el(RichText, { |
| 135 |
tagName: 'h3', className: 'bkbg-sdk-title', |
| 136 |
value: a.title, onChange: function (v) { setAttributes({ title: v }); }, |
| 137 |
placeholder: __('Sudoku', 'blockenberg'), |
| 138 |
style: { color: a.titleColor, margin: '0 0 4px' } |
| 139 |
}), |
| 140 |
el(RichText, { |
| 141 |
tagName: 'p', className: 'bkbg-sdk-subtitle', |
| 142 |
value: a.subtitle, onChange: function (v) { setAttributes({ subtitle: v }); }, |
| 143 |
placeholder: __('Subtitle', 'blockenberg'), |
| 144 |
style: { color: a.titleColor + 'bb', margin: '0 0 18px' } |
| 145 |
}), |
| 146 |
// Stats bar |
| 147 |
el('div', { style: { display: 'flex', justifyContent: 'center', gap: 16, marginBottom: 14, flexWrap: 'wrap' } }, |
| 148 |
['Easy','Medium','Hard','Expert'].map(function (d) { |
| 149 |
var active = d.toLowerCase() === a.defaultDifficulty; |
| 150 |
return el('button', { |
| 151 |
key: d, |
| 152 |
style: { |
| 153 |
padding: '5px 14px', borderRadius: 20, fontSize: 13, fontWeight: 600, |
| 154 |
border: '2px solid ' + a.accentColor, cursor: 'default', |
| 155 |
background: active ? a.accentColor : 'transparent', |
| 156 |
color: active ? '#fff' : a.accentColor |
| 157 |
} |
| 158 |
}, d); |
| 159 |
}) |
| 160 |
), |
| 161 |
// Info row |
| 162 |
el('div', { style: { display: 'flex', justifyContent: 'center', gap: 20, marginBottom: 14 } }, |
| 163 |
a.showTimer && el('div', { style: { fontWeight: 700, color: a.titleColor } }, '⏱ 0:00'), |
| 164 |
a.showMistakes && el('div', { style: { fontWeight: 700, color: a.errorColor } }, '✕ 0 / ' + a.maxMistakes), |
| 165 |
el('div', { style: { fontWeight: 700, color: a.accentColor } }, '💡 ' + a.hintsAllowed) |
| 166 |
), |
| 167 |
el('div', { style: { display: 'flex', justifyContent: 'center', marginBottom: 14 } }, |
| 168 |
el(SudokuGrid, a) |
| 169 |
), |
| 170 |
// Number pad |
| 171 |
el('div', { style: { display: 'flex', justifyContent: 'center', gap: 8, marginBottom: 14, flexWrap: 'wrap' } }, |
| 172 |
[1,2,3,4,5,6,7,8,9].map(function (n) { |
| 173 |
return el('button', { |
| 174 |
key: n, |
| 175 |
style: { |
| 176 |
width: 40, height: 40, borderRadius: 8, border: '2px solid ' + a.accentColor, |
| 177 |
background: 'transparent', color: a.accentColor, fontSize: 18, fontWeight: 700, cursor: 'default' |
| 178 |
} |
| 179 |
}, n); |
| 180 |
}), |
| 181 |
el('button', { |
| 182 |
style: { |
| 183 |
width: 40, height: 40, borderRadius: 8, border: '2px solid #9ca3af', |
| 184 |
background: 'transparent', color: '#9ca3af', fontSize: 18, cursor: 'default' |
| 185 |
} |
| 186 |
}, '✕') |
| 187 |
), |
| 188 |
el('div', { style: { display: 'flex', justifyContent: 'center', gap: 10, flexWrap: 'wrap' } }, |
| 189 |
el('button', { style: { background: a.accentColor, color: '#fff', border: 'none', borderRadius: 8, padding: '9px 22px', fontWeight: 700, cursor: 'default' } }, 'New Game'), |
| 190 |
el('button', { style: { background: 'transparent', border: '2px solid ' + a.accentColor, color: a.accentColor, borderRadius: 8, padding: '9px 18px', fontWeight: 700, cursor: 'default' } }, '💡 Hint') |
| 191 |
) |
| 192 |
) |
| 193 |
); |
| 194 |
} |
| 195 |
|
| 196 |
registerBlockType('blockenberg/sudoku', { |
| 197 |
edit: EditorPreview, |
| 198 |
save: function (props) { |
| 199 |
var a = props.attributes; |
| 200 |
return el('div', useBlockProps.save(), |
| 201 |
el('div', { className: 'bkbg-sdk-app', 'data-opts': JSON.stringify(a) }, |
| 202 |
el(RichText.Content, { tagName: 'h3', className: 'bkbg-sdk-title', value: a.title }), |
| 203 |
el(RichText.Content, { tagName: 'p', className: 'bkbg-sdk-subtitle', value: a.subtitle }) |
| 204 |
) |
| 205 |
); |
| 206 |
} |
| 207 |
}); |
| 208 |
}() ); |
| 209 |
|