PluginProbe
Blockenberg — 600+ Advanced Gutenberg Blocks & AI Agent for WordPress Block Editor / 2.0.7
Blockenberg — 600+ Advanced Gutenberg Blocks & AI Agent for WordPress Block Editor v2.0.7
2.0.13 2.0.12 2.0.11 2.0.10 2.0.9 trunk 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8
blockenberg / blocks / dice-roller / index.js

index.js in Blockenberg — 600+ Advanced Gutenberg Blocks & AI Agent for WordPress Block Editor 2.0.7, at blocks/dice-roller/index.js

216 lines 13.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 ToggleControl = wp.components.ToggleControl;
14 var SelectControl = wp.components.SelectControl;
15
16 var _drTC, _drTV;
17 function _tc() { return _drTC || (_drTC = window.bkbgTypographyControl); }
18 function _tv(t, p) { return (_drTV || (_drTV = window.bkbgTypoCssVars)) ? _drTV(t, p) : {}; }
19
20 var DICE_TYPES = ['d4','d6','d8','d10','d12','d20','d100'];
21
22 var DIE_SYMBOLS = { d4:'', d6:'', d8:'', d10:'', d12:'', d20:'', d100:'' };
23
24 function EditorPreview(props) {
25 var a = props.attributes;
26
27 var wrapStyle = {
28 background: a.sectionBg,
29 borderRadius:'16px',
30 padding: '32px 24px',
31 maxWidth: a.contentMaxWidth + 'px',
32 margin: '0 auto',
33 fontFamily: 'inherit',
34 boxSizing: 'border-box',
35 textAlign: 'center'
36 };
37
38 // Fake dice
39 var fakeDice = Array.from({ length: a.defaultCount }, function (_, i) { return i + 1; });
40
41 return el('div', { style: wrapStyle },
42 a.showTitle && el(RichText, {
43 tagName:'h3', value:a.title,
44 className:'bkbg-dr-title',
45 onChange:function(v){ props.setAttributes({title:v}); },
46 style:{ color:a.titleColor, margin:'0 0 5px 0' },
47 placeholder:'Title…'
48 }),
49 a.showSubtitle && el(RichText, {
50 tagName:'p', value:a.subtitle,
51 className:'bkbg-dr-subtitle',
52 onChange:function(v){ props.setAttributes({subtitle:v}); },
53 style:{ color:a.subtitleColor, margin:'0 0 18px 0' },
54 placeholder:'Subtitle…'
55 }),
56
57 // Controls row
58 el('div', { style:{ display:'flex', gap:'10px', justifyContent:'center', flexWrap:'wrap', marginBottom:'16px', alignItems:'center' } },
59 el('div', { style:{ display:'flex', flexDirection:'column', alignItems:'flex-start', gap:'3px' } },
60 el('label', { style:{ fontSize:'11px', fontWeight:'700', textTransform:'uppercase', letterSpacing:'.06em', color:a.labelColor } }, 'Die Type'),
61 el('select', { style:{ padding:'7px 12px', borderRadius:'8px', border:'1.5px solid #e5e7eb', fontSize:'14px', fontWeight:'700', color:a.labelColor, background:'#fff' } },
62 DICE_TYPES.map(function (d) {
63 return el('option', { key:d, selected:d===a.defaultDie }, d);
64 })
65 )
66 ),
67 el('div', { style:{ display:'flex', flexDirection:'column', alignItems:'flex-start', gap:'3px' } },
68 el('label', { style:{ fontSize:'11px', fontWeight:'700', textTransform:'uppercase', letterSpacing:'.06em', color:a.labelColor } }, 'Number'),
69 el('div', { style:{ display:'flex', gap:'6px', alignItems:'center' } },
70 el('button', { style:{ width:'30px', height:'30px', borderRadius:'7px', border:'1.5px solid #e5e7eb', background:'#f9fafb', fontWeight:'700', fontSize:'16px', cursor:'pointer' } }, ''),
71 el('span', { style:{ fontWeight:'700', fontSize:'16px', color:a.labelColor, width:'24px', textAlign:'center' } }, a.defaultCount),
72 el('button', { style:{ width:'30px', height:'30px', borderRadius:'7px', border:'1.5px solid #e5e7eb', background:'#f9fafb', fontWeight:'700', fontSize:'16px', cursor:'pointer' } }, '+')
73 )
74 ),
75 a.showModifier && el('div', { style:{ display:'flex', flexDirection:'column', alignItems:'flex-start', gap:'3px' } },
76 el('label', { style:{ fontSize:'11px', fontWeight:'700', textTransform:'uppercase', letterSpacing:'.06em', color:a.labelColor } }, 'Modifier'),
77 el('input', { type:'number', readOnly:true, style:{ width:'60px', padding:'7px 8px', borderRadius:'8px', border:'1.5px solid #e5e7eb', fontSize:'14px', textAlign:'center' }, defaultValue:'0' })
78 )
79 ),
80
81 // Roll button
82 el('button', {
83 style:{ padding:'13px 36px', borderRadius:'12px', border:'none', background:a.accentColor, color:'#fff', fontWeight:'800', fontSize:'16px', cursor:'pointer', marginBottom:'18px', letterSpacing:'.02em', boxShadow:'0 4px 12px rgba(220,38,38,.3)' }
84 }, '🎲 Roll ' + a.defaultDie),
85
86 // Dice display
87 el('div', { style:{ display:'flex', gap:'10px', flexWrap:'wrap', justifyContent:'center', marginBottom:'16px' } },
88 fakeDice.map(function (v) {
89 return el('div', { key:v,
90 style:{ width:'64px', height:'64px', borderRadius:'14px', background:a.dieColor, color:a.dieFace, display:'flex', alignItems:'center', justifyContent:'center', fontSize:'24px', fontWeight:'800', boxShadow:'0 4px 10px rgba(0,0,0,.25)', fontFamily:"'Courier New', monospace" }
91 }, v);
92 })
93 ),
94
95 // Total
96 a.showTotal && el('div', { style:{ background:a.totalBg, borderRadius:'12px', padding:'14px 20px', display:'inline-flex', gap:'16px', alignItems:'center', marginBottom:'16px' } },
97 el('span', { style:{ fontSize:'13px', fontWeight:'600', color:a.labelColor } }, 'Total'),
98 el('span', { style:{ fontSize:'32px', fontWeight:'800', color:a.totalColor } }, fakeDice.reduce(function (s,v){ return s+v; }, 0))
99 ),
100
101 // History
102 a.showHistory && el('div', { style:{ background:a.histBg, borderRadius:'10px', border:'1.5px solid #e5e7eb', padding:'0', overflow:'hidden' } },
103 el('div', { style:{ padding:'8px 14px', fontSize:'11px', fontWeight:'700', textTransform:'uppercase', letterSpacing:'.06em', color:a.labelColor, background:'#f3f4f6', display:'flex', justifyContent:'space-between' } },
104 el('span', null, 'Roll History'),
105 el('span', null, '3 rolls')
106 ),
107 ['Roll 3 — 2d6 — [4, 6] = 10','Roll 2 — 2d6 — [2, 5] = 7','Roll 1 — 2d6 — [3, 3] = 6'].map(function (row, i) {
108 return el('div', { key:i, style:{ padding:'7px 14px', fontSize:'13px', color:a.labelColor, borderTop:'1px solid #e5e7eb', display:'flex', justifyContent:'space-between' } },
109 el('span', null, row),
110 el('button', { style:{ background:'none', border:'none', color:a.accentColor, fontSize:'11px', fontWeight:'600', cursor:'pointer' } }, '↺ reroll')
111 );
112 })
113 )
114 );
115 }
116
117 registerBlockType('blockenberg/dice-roller', {
118 edit: function (props) {
119 var a = props.attributes;
120 var set = props.setAttributes;
121
122 var colorSettings = [
123 { value:a.accentColor, onChange:function(v){set({accentColor: v||'#dc2626'});}, label:'Accent / roll button' },
124 { value:a.dieColor, onChange:function(v){set({dieColor: v||'#1e1b4b'});}, label:'Die face background' },
125 { value:a.dieFace, onChange:function(v){set({dieFace: v||'#ffffff'});}, label:'Die number color' },
126 { value:a.totalBg, onChange:function(v){set({totalBg: v||'#fef2f2'});}, label:'Total background' },
127 { value:a.totalColor, onChange:function(v){set({totalColor: v||'#dc2626'});}, label:'Total number color' },
128 { value:a.histBg, onChange:function(v){set({histBg: v||'#f9fafb'});}, label:'History background' },
129 { value:a.sectionBg, onChange:function(v){set({sectionBg: v||'#fff1f2'});}, label:'Section background' },
130 { value:a.cardBg, onChange:function(v){set({cardBg: v||'#ffffff'});}, label:'Card background' },
131 { value:a.titleColor, onChange:function(v){set({titleColor: v||'#111827'});}, label:'Title color' },
132 { value:a.subtitleColor, onChange:function(v){set({subtitleColor:v||'#6b7280'});}, label:'Subtitle color' },
133 { value:a.labelColor, onChange:function(v){set({labelColor: v||'#374151'});}, label:'Label color' }
134 ];
135
136 return el(Fragment, null,
137 el(InspectorControls, null,
138 el(PanelBody, { title:'Dice Settings', initialOpen:true },
139 el(SelectControl, {
140 label:'Default die type',
141 value:a.defaultDie,
142 options:DICE_TYPES.map(function(d){ return {label:d,value:d}; }),
143 onChange:function(v){set({defaultDie:v});}
144 }),
145 el(RangeControl, { label:'Default die count', value:a.defaultCount, min:1, max:10, onChange:function(v){set({defaultCount:v});} }),
146 el(RangeControl, { label:'History shown', value:a.historySize, min:3, max:20, onChange:function(v){set({historySize:v});} }),
147 el(ToggleControl, { __nextHasNoMarginBottom:true, label:'Show title', checked:a.showTitle, onChange:function(v){set({showTitle:v});} }),
148 el(ToggleControl, { __nextHasNoMarginBottom:true, label:'Show subtitle', checked:a.showSubtitle, onChange:function(v){set({showSubtitle:v});} }),
149 el(ToggleControl, { __nextHasNoMarginBottom:true, label:'Show total', checked:a.showTotal, onChange:function(v){set({showTotal:v});} }),
150 el(ToggleControl, { __nextHasNoMarginBottom:true, label:'Show modifier', checked:a.showModifier, onChange:function(v){set({showModifier:v});} }),
151 el(ToggleControl, { __nextHasNoMarginBottom:true, label:'Show roll history',checked:a.showHistory, onChange:function(v){set({showHistory:v});} })
152 ),
153 el(PanelBody, { title:'Sizing', initialOpen:false },
154 el(RangeControl, { label:'Max width (px)', value:a.contentMaxWidth, min:320, max:900, step:20, onChange:function(v){set({contentMaxWidth:v});} })
155 ),
156
157 el( PanelBody, { title: __( 'Typography', 'blockenberg' ), initialOpen: false },
158 _tc() && el(_tc(), { label: __('Title'), typo: a.typoTitle || {}, onChange: function(v) { set({ typoTitle: v }); }, defaultSize: a.titleFontSize || 26 }),
159 _tc() && el(_tc(), { label: __('Subtitle'), typo: a.typoSubtitle || {}, onChange: function(v) { set({ typoSubtitle: v }); }, defaultSize: a.subtitleFontSize || 14 })
160 ),
161 el(PanelColorSettings, { title:'Colors', initialOpen:false, disableCustomGradients:true, colorSettings:colorSettings })
162 ),
163 el('div', useBlockProps({ style: Object.assign(
164 { '--bkbg-dr-ttl-fs': (a.titleFontSize || 26) + 'px',
165 '--bkbg-dr-sub-fs': (a.subtitleFontSize || 14) + 'px' },
166 _tv(a.typoTitle || {}, '--bkbg-dr-ttl-'),
167 _tv(a.typoSubtitle || {}, '--bkbg-dr-sub-')
168 ) }),
169 el(EditorPreview, { attributes:a, setAttributes:set })
170 )
171 );
172 },
173
174 save: function (props) {
175 var a = props.attributes;
176 return el('div', useBlockProps.save(),
177 el('div', {
178 className: 'bkbg-dr-app',
179 'data-opts': JSON.stringify({
180 title: a.title,
181 subtitle: a.subtitle,
182 showTitle: a.showTitle,
183 showSubtitle: a.showSubtitle,
184 showHistory: a.showHistory,
185 showTotal: a.showTotal,
186 showModifier: a.showModifier,
187 defaultDie: a.defaultDie,
188 defaultCount: a.defaultCount,
189 historySize: a.historySize,
190 accentColor: a.accentColor,
191 dieColor: a.dieColor,
192 dieFace: a.dieFace,
193 totalBg: a.totalBg,
194 totalColor: a.totalColor,
195 histBg: a.histBg,
196 sectionBg: a.sectionBg,
197 cardBg: a.cardBg,
198 titleColor: a.titleColor,
199 subtitleColor: a.subtitleColor,
200 labelColor: a.labelColor,
201 titleFontSize: a.titleFontSize,
202 titleFontWeight: a.titleFontWeight,
203 titleLineHeight: a.titleLineHeight,
204 subtitleFontSize: a.subtitleFontSize,
205 subtitleFontWeight: a.subtitleFontWeight,
206 subtitleLineHeight: a.subtitleLineHeight,
207 contentMaxWidth:a.contentMaxWidth,
208 typoTitle: a.typoTitle,
209 typoSubtitle: a.typoSubtitle
210 })
211 })
212 );
213 }
214 });
215 }() );
216