| 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 RichText = wp.blockEditor.RichText; |
| 11 |
var PanelBody = wp.components.PanelBody; |
| 12 |
var RangeControl = wp.components.RangeControl; |
| 13 |
var SelectControl = wp.components.SelectControl; |
| 14 |
var ToggleControl = wp.components.ToggleControl; |
| 15 |
|
| 16 |
var _tc; function getTypoControl() { return _tc || (_tc = window.bkbgTypographyControl); } |
| 17 |
var _tv; function getTypoCssVars() { return _tv || (_tv = window.bkbgTypoCssVars); } |
| 18 |
|
| 19 |
var TETROMINOES = { |
| 20 |
I: { cells: [[0,1],[1,1],[2,1],[3,1]], color: '#06b6d4' }, |
| 21 |
O: { cells: [[0,0],[0,1],[1,0],[1,1]], color: '#f59e0b' }, |
| 22 |
T: { cells: [[0,1],[1,0],[1,1],[1,2]], color: '#a855f7' }, |
| 23 |
S: { cells: [[0,1],[0,2],[1,0],[1,1]], color: '#22c55e' }, |
| 24 |
Z: { cells: [[0,0],[0,1],[1,1],[1,2]], color: '#ef4444' }, |
| 25 |
L: { cells: [[0,2],[1,0],[1,1],[1,2]], color: '#f97316' }, |
| 26 |
J: { cells: [[0,0],[1,0],[1,1],[1,2]], color: '#3b82f6' } |
| 27 |
}; |
| 28 |
|
| 29 |
var PIECES = Object.keys(TETROMINOES); |
| 30 |
|
| 31 |
// Demo board for editor preview — show a partially filled board |
| 32 |
var DEMO_BOARD = (function () { |
| 33 |
var board = []; |
| 34 |
for (var r = 0; r < 20; r++) { |
| 35 |
board.push([]); |
| 36 |
for (var c = 0; c < 10; c++) { |
| 37 |
var filled = false; |
| 38 |
if (r >= 16 && c !== 4 && c !== 5) filled = true; |
| 39 |
if (r === 15 && (c <= 2 || c >= 8)) filled = true; |
| 40 |
board[r].push(filled ? (r % 7 === 0 ? '#06b6d4' : r % 5 === 0 ? '#ef4444' : r % 3 === 0 ? '#22c55e' : '#a855f7') : null); |
| 41 |
} |
| 42 |
} |
| 43 |
// Place a falling T piece |
| 44 |
[[2,4],[2,5],[2,6],[3,5]].forEach(function (pos) { board[pos[0]][pos[1]] = '#a855f7'; }); |
| 45 |
return board; |
| 46 |
})(); |
| 47 |
|
| 48 |
// Demo next piece |
| 49 |
var NEXT_PREVIEW = [ [false, true, false], [true, true, false], [false, false, false] ]; |
| 50 |
|
| 51 |
function EditorPreview(props) { |
| 52 |
var a = props.attributes; |
| 53 |
var setAttributes = props.setAttributes; |
| 54 |
var cs = a.cellSize || 30; |
| 55 |
var W = 10 * cs; |
| 56 |
var H = 20 * cs; |
| 57 |
|
| 58 |
var blockProps = (function () { |
| 59 |
var s = { background: a.sectionBg || '#0f0e17', padding: '28px 16px' }; |
| 60 |
var _tvf = getTypoCssVars(); |
| 61 |
if (_tvf) { Object.assign(s, _tvf(a.titleTypo, '--bkttr-tt-'), _tvf(a.subtitleTypo, '--bkttr-st-')); } |
| 62 |
return useBlockProps({ style: s }); |
| 63 |
})(); |
| 64 |
|
| 65 |
function StatBox(label, val) { |
| 66 |
return el('div', { style: { background: a.bgColor, border: '1px solid ' + a.accentColor + '44', borderRadius: 8, padding: '10px 14px', marginBottom: 8 } }, |
| 67 |
el('div', { style: { fontSize: 11, fontWeight: 700, letterSpacing: 1, color: a.accentColor, textTransform: 'uppercase', marginBottom: 2 } }, label), |
| 68 |
el('div', { style: { fontSize: 24, fontWeight: 700, color: a.titleColor } }, val) |
| 69 |
); |
| 70 |
} |
| 71 |
|
| 72 |
var boardCells = []; |
| 73 |
for (var r = 0; r < 20; r++) { |
| 74 |
for (var c = 0; c < 10; c++) { |
| 75 |
var cellColor = DEMO_BOARD[r][c]; |
| 76 |
boardCells.push(el('div', { |
| 77 |
key: r + '_' + c, |
| 78 |
style: { |
| 79 |
width: cs, height: cs, |
| 80 |
background: cellColor || a.boardBg, |
| 81 |
border: '1px solid ' + (a.gridColor || '#16213e'), |
| 82 |
boxSizing: 'border-box', |
| 83 |
borderRadius: cellColor ? 3 : 0, |
| 84 |
boxShadow: cellColor ? 'inset 0 2px 0 rgba(255,255,255,0.25), inset 0 -2px 0 rgba(0,0,0,0.25)' : 'none' |
| 85 |
} |
| 86 |
})); |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
return el(Fragment, null, |
| 91 |
el(InspectorControls, null, |
| 92 |
el(PanelBody, { title: __('Content', 'blockenberg'), initialOpen: true }, |
| 93 |
), |
| 94 |
el(PanelBody, { title: __('Game Settings', 'blockenberg'), initialOpen: false }, |
| 95 |
el(RangeControl, { label: __('Starting Level', 'blockenberg'), value: a.startLevel, min: 1, max: 15, |
| 96 |
onChange: function (v) { setAttributes({ startLevel: v }); } }), |
| 97 |
el(ToggleControl, { __nextHasNoMarginBottom: true, label: __('Show Ghost Piece', 'blockenberg'), checked: a.showGhost, onChange: function (v) { setAttributes({ showGhost: v }); } }), |
| 98 |
el(ToggleControl, { __nextHasNoMarginBottom: true, label: __('Show Next Piece', 'blockenberg'), checked: a.showNext, onChange: function (v) { setAttributes({ showNext: v }); } }), |
| 99 |
el(ToggleControl, { __nextHasNoMarginBottom: true, label: __('Show Score', 'blockenberg'), checked: a.showScore, onChange: function (v) { setAttributes({ showScore: v }); } }), |
| 100 |
el(ToggleControl, { __nextHasNoMarginBottom: true, label: __('Show Level', 'blockenberg'), checked: a.showLevel, onChange: function (v) { setAttributes({ showLevel: v }); } }), |
| 101 |
el(ToggleControl, { __nextHasNoMarginBottom: true, label: __('Show Lines', 'blockenberg'), checked: a.showLines, onChange: function (v) { setAttributes({ showLines: v }); } }) |
| 102 |
), |
| 103 |
|
| 104 |
el( PanelBody, { title: __( 'Typography', 'blockenberg' ), initialOpen: false }, |
| 105 |
getTypoControl() && getTypoControl()({ |
| 106 |
label: __('Title', 'blockenberg'), |
| 107 |
value: a.titleTypo || {}, |
| 108 |
onChange: function (v) { setAttributes({ titleTypo: v }); } |
| 109 |
}), |
| 110 |
getTypoControl() && getTypoControl()({ |
| 111 |
label: __('Subtitle', 'blockenberg'), |
| 112 |
value: a.subtitleTypo || {}, |
| 113 |
onChange: function (v) { setAttributes({ subtitleTypo: v }); } |
| 114 |
}), |
| 115 |
el(RangeControl, { label: __('Cell Size (px)', 'blockenberg'), value: a.cellSize, min: 18, max: 40, |
| 116 |
onChange: function (v) { setAttributes({ cellSize: v }); } }) |
| 117 |
), |
| 118 |
el(PanelColorSettings, { |
| 119 |
title: __('Colors', 'blockenberg'), initialOpen: false, |
| 120 |
colorSettings: [ |
| 121 |
{ label: __('Section Background', 'blockenberg'), value: a.sectionBg, onChange: function (v) { setAttributes({ sectionBg: v || '#0f0e17' }); } }, |
| 122 |
{ label: __('UI Background', 'blockenberg'), value: a.bgColor, onChange: function (v) { setAttributes({ bgColor: v || '#0f0e17' }); } }, |
| 123 |
{ label: __('Board Background', 'blockenberg'), value: a.boardBg, onChange: function (v) { setAttributes({ boardBg: v || '#1a1a2e' }); } }, |
| 124 |
{ label: __('Grid Lines', 'blockenberg'), value: a.gridColor, onChange: function (v) { setAttributes({ gridColor: v || '#16213e' }); } }, |
| 125 |
{ label: __('Ghost Piece', 'blockenberg'), value: a.ghostColor, onChange: function (v) { setAttributes({ ghostColor: v || 'rgba(255,255,255,0.12)' }); } }, |
| 126 |
{ label: __('Title / Text', 'blockenberg'), value: a.titleColor, onChange: function (v) { setAttributes({ titleColor: v || '#fffffe' }); } }, |
| 127 |
{ label: __('Accent', 'blockenberg'), value: a.accentColor, onChange: function (v) { setAttributes({ accentColor: v || '#ff8906' }); } } |
| 128 |
] |
| 129 |
}) |
| 130 |
), |
| 131 |
el('div', blockProps, |
| 132 |
el(RichText, { tagName: 'h3', className: 'bkbg-ttr-title', value: a.title, |
| 133 |
onChange: function (v) { setAttributes({ title: v }); }, |
| 134 |
placeholder: 'Tetris', |
| 135 |
style: { color: a.titleColor, textAlign: 'center', margin: '0 0 4px' } }), |
| 136 |
el(RichText, { tagName: 'p', className: 'bkbg-ttr-subtitle', value: a.subtitle, |
| 137 |
onChange: function (v) { setAttributes({ subtitle: v }); }, |
| 138 |
placeholder: 'Clear lines to score points!', |
| 139 |
style: { color: a.titleColor, opacity: 0.65, textAlign: 'center', margin: '0 0 16px' } }), |
| 140 |
el('div', { style: { display: 'flex', justifyContent: 'center', gap: 16, alignItems: 'flex-start', flexWrap: 'wrap' } }, |
| 141 |
// Board |
| 142 |
el('div', { style: { display: 'grid', gridTemplateColumns: 'repeat(10,1fr)', width: W, height: H, flexShrink: 0, border: '2px solid ' + a.accentColor + '55', borderRadius: 4, overflow: 'hidden' } }, |
| 143 |
boardCells |
| 144 |
), |
| 145 |
// Side panel |
| 146 |
el('div', { style: { display: 'flex', flexDirection: 'column', minWidth: 110 } }, |
| 147 |
a.showNext && el('div', { style: { background: a.bgColor, border: '1px solid ' + a.accentColor + '44', borderRadius: 8, padding: 10, marginBottom: 8 } }, |
| 148 |
el('div', { style: { fontSize: 11, fontWeight: 700, letterSpacing: 1, color: a.accentColor, textTransform: 'uppercase', marginBottom: 6 } }, __('Next', 'blockenberg')), |
| 149 |
el('div', { style: { display: 'grid', gridTemplateColumns: 'repeat(4,18px)', gap: 2 } }, |
| 150 |
[0,1,2,3,4,5,6,7,8,9,10,11].map(function (i) { |
| 151 |
var r2 = Math.floor(i / 4), c2 = i % 4; |
| 152 |
var show = (r2 === 0 && c2 >= 1 && c2 <= 2) || (r2 === 1 && c2 >= 0 && c2 <= 2) || (r2 === 2 && false); |
| 153 |
return el('div', { key: i, style: { width: 18, height: 18, background: show ? '#a855f7' : 'transparent', borderRadius: 2, boxShadow: show ? 'inset 0 2px 0 rgba(255,255,255,0.25)' : 'none' } }); |
| 154 |
}) |
| 155 |
) |
| 156 |
), |
| 157 |
a.showScore && StatBox(__('Score', 'blockenberg'), '0'), |
| 158 |
a.showLevel && StatBox(__('Level', 'blockenberg'), a.startLevel), |
| 159 |
a.showLines && StatBox(__('Lines', 'blockenberg'), '0'), |
| 160 |
el('div', { style: { fontSize: 11, color: a.titleColor, opacity: 0.5, marginTop: 6, lineHeight: 1.6 } }, |
| 161 |
el('div', null, '← → Move'), |
| 162 |
el('div', null, '↑ Rotate'), |
| 163 |
el('div', null, '↓ Soft drop'), |
| 164 |
el('div', null, 'Space: Drop') |
| 165 |
) |
| 166 |
) |
| 167 |
) |
| 168 |
) |
| 169 |
); |
| 170 |
} |
| 171 |
|
| 172 |
registerBlockType('blockenberg/tetris', { |
| 173 |
edit: EditorPreview, |
| 174 |
save: function (props) { |
| 175 |
var a = props.attributes; |
| 176 |
var _tvf = getTypoCssVars(); |
| 177 |
return el('div', (function () { |
| 178 |
var s = {}; |
| 179 |
if (_tvf) { Object.assign(s, _tvf(a.titleTypo, '--bkttr-tt-'), _tvf(a.subtitleTypo, '--bkttr-st-')); } |
| 180 |
return useBlockProps.save({ style: s }); |
| 181 |
})(), |
| 182 |
el('div', { className: 'bkbg-ttr-app', 'data-opts': JSON.stringify(a) }, |
| 183 |
el(RichText.Content, { tagName: 'h3', className: 'bkbg-ttr-title', value: a.title }), |
| 184 |
el(RichText.Content, { tagName: 'p', className: 'bkbg-ttr-subtitle', value: a.subtitle }) |
| 185 |
) |
| 186 |
); |
| 187 |
} |
| 188 |
}); |
| 189 |
}() ); |
| 190 |
|