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 / macro-calculator / index.js

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

264 lines 18.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 PanelBody = wp.components.PanelBody;
11 var RangeControl = wp.components.RangeControl;
12 var SelectControl = wp.components.SelectControl;
13 var TextControl = wp.components.TextControl;
14 var ToggleControl = wp.components.ToggleControl;
15
16 var _tc, _tv;
17 function getTypoControl() { return _tc || (_tc = window.bkbgTypographyControl); }
18 function getTypoCssVars() { return _tv || (_tv = window.bkbgTypoCssVars); }
19
20 var ACTIVITY_OPTS = [
21 {label:'Sedentary (little/no exercise)', value:'sedentary'},
22 {label:'Light (1-3 days/week)', value:'light'},
23 {label:'Moderate (3-5 days/week)', value:'moderate'},
24 {label:'Active (6-7 days/week)', value:'active'},
25 {label:'Very Active (twice/day, athlete)', value:'very_active'}
26 ];
27 var GOAL_OPTS = [
28 {label:'Lose weight (fast – deficit 25%)', value:'lose_fast'},
29 {label:'Lose weight (slow – deficit 15%)', value:'lose_slow'},
30 {label:'Maintain weight', value:'maintain'},
31 {label:'Gain muscle (slow – surplus 10%)', value:'gain_slow'},
32 {label:'Gain muscle (fast – surplus 20%)', value:'gain_fast'}
33 ];
34 var ACTIVITY_MULT = {sedentary:1.2, light:1.375, moderate:1.55, active:1.725, very_active:1.9};
35 var GOAL_MULT = {lose_fast:0.75, lose_slow:0.85, maintain:1, gain_slow:1.1, gain_fast:1.2};
36 // Macro ratios per goal (protein%/carb%/fat%)
37 var MACRO_RATIOS = {
38 lose_fast: [0.40, 0.30, 0.30],
39 lose_slow: [0.35, 0.35, 0.30],
40 maintain: [0.30, 0.40, 0.30],
41 gain_slow: [0.30, 0.45, 0.25],
42 gain_fast: [0.30, 0.50, 0.20]
43 };
44
45 function calcMacros(gender, age, weightKg, heightCm, activity, goal) {
46 // Mifflin-St Jeor BMR
47 var bmr = gender === 'female'
48 ? 10*weightKg + 6.25*heightCm - 5*age - 161
49 : 10*weightKg + 6.25*heightCm - 5*age + 5;
50 var tdee = bmr * (ACTIVITY_MULT[activity] || 1.55);
51 var calories = Math.round(tdee * (GOAL_MULT[goal] || 1));
52 var ratio = MACRO_RATIOS[goal] || MACRO_RATIOS['maintain'];
53 var protein = Math.round((calories * ratio[0]) / 4);
54 var carbs = Math.round((calories * ratio[1]) / 4);
55 var fat = Math.round((calories * ratio[2]) / 9);
56 return {calories:calories, protein:protein, carbs:carbs, fat:fat, bmr:Math.round(bmr), tdee:Math.round(tdee)};
57 }
58
59 function MacroPreview(props) {
60 var a = props.attributes;
61 var accent = a.accentColor || '#6c3fb5';
62 var protClr = a.proteinColor || '#3b82f6';
63 var carbClr = a.carbColor || '#f59e0b';
64 var fatClr = a.fatColor || '#ef4444';
65
66 var _unit = useState(a.defaultUnit || 'metric'); var unit = _unit[0]; var setUnit = _unit[1];
67 var _gender = useState(a.defaultGender || 'male'); var gender = _gender[0]; var setGender = _gender[1];
68 var _age = useState(a.defaultAge || 30); var age = _age[0]; var setAge = _age[1];
69 var _weight = useState(a.defaultWeight || 75); var weight = _weight[0]; var setWeight = _weight[1];
70 var _height = useState(a.defaultHeight || 175); var height = _height[0]; var setHeight = _height[1];
71 var _activity= useState(a.defaultActivity || 'moderate');var activity=_activity[0];var setActivity=_activity[1];
72 var _goal = useState(a.defaultGoal || 'maintain');var goal =_goal[0]; var setGoal =_goal[1];
73
74 var weightKg = unit==='imperial' ? weight*0.453592 : weight;
75 var heightCm = unit==='imperial' ? height*2.54 : height;
76 var r = calcMacros(gender, parseFloat(age)||25, weightKg, heightCm, activity, goal);
77
78 var inputStyle = {padding:'9px 12px',borderRadius:(a.inputRadius||8)+'px',border:'1.5px solid '+(a.inputBorder||'#e5e7eb'),fontSize:'14px',fontFamily:'inherit',outline:'none',width:'100%',background:'#fff'};
79 var lblStyle = {display:'block',fontSize:'12px',fontWeight:600,color:a.labelColor||'#374151',marginBottom:'5px',textTransform:'uppercase',letterSpacing:'.05em'};
80 var statStyle = {background:a.statBg||'#f3f4f6',border:'1px solid '+(a.statBorder||'#e5e7eb'),borderRadius:'10px',padding:'14px 12px',flex:'1',textAlign:'center'};
81
82 // SVG donut (simple, 3 arcs)
83 function DonutChart() {
84 var total = r.protein*4 + r.carbs*4 + r.fat*9;
85 function arc(pct, offset, color) {
86 var c = 2*Math.PI*40;
87 var sd = c*(1-pct);
88 return el('circle',{cx:50,cy:50,r:40,fill:'none',stroke:color,strokeWidth:18,
89 strokeDasharray:c,strokeDashoffset:sd,strokeLinecap:'butt',
90 transform:'rotate('+(offset*360-90)+' 50 50)'});
91 }
92 var p1 = total>0?(r.protein*4/total):0.33;
93 var p2 = total>0?(r.carbs*4/total):0.34;
94 var p3 = total>0?(r.fat*9/total):0.33;
95 return el('svg',{viewBox:'0 0 100 100',style:{width:'120px',height:'120px'}},
96 arc(p1, 0, protClr),
97 arc(p2, p1, carbClr),
98 arc(p3, p1+p2, fatClr),
99 el('text',{x:50,y:48,textAnchor:'middle',fontSize:'12',fill:'#374151',fontWeight:700},r.calories),
100 el('text',{x:50,y:60,textAnchor:'middle',fontSize:'7',fill:'#6b7280'},'kcal/day')
101 );
102 }
103
104 return el('div', {style:{paddingTop:(a.paddingTop||60)+'px',paddingBottom:(a.paddingBottom||60)+'px',background:a.sectionBg||undefined}},
105 el('div', {style:{background:a.cardBg,borderRadius:(a.cardRadius||16)+'px',padding:'36px 32px',maxWidth:(a.maxWidth||580)+'px',margin:'0 auto',boxShadow:'0 4px 24px rgba(0,0,0,.09)'}},
106
107 (a.showTitle||a.showSubtitle) && el('div',{style:{marginBottom:'22px'}},
108 a.showTitle && el('div',{className:'bkbg-mcr-title',style:{color:a.titleColor,marginBottom:'6px'}},a.title),
109 a.showSubtitle && el('div',{className:'bkbg-mcr-subtitle',style:{color:a.subtitleColor}},a.subtitle)
110 ),
111
112 // Unit + gender toggles
113 el('div',{style:{display:'flex',gap:'10px',marginBottom:'16px'}},
114 el('div',{style:{flex:1}},
115 el('label',{style:lblStyle},'Unit'),
116 el('div',{style:{display:'flex',borderRadius:'8px',overflow:'hidden',border:'1.5px solid '+(a.inputBorder||'#e5e7eb')}},
117 ['metric','imperial'].map(function(u){ return el('button',{key:u,onClick:function(){setUnit(u);},
118 style:{flex:1,padding:'8px',border:'none',background:unit===u?accent:'#fff',color:unit===u?'#fff':'#374151',fontWeight:600,fontSize:'13px',fontFamily:'inherit',cursor:'pointer'}},
119 u.charAt(0).toUpperCase()+u.slice(1)); })
120 )
121 ),
122 el('div',{style:{flex:1}},
123 el('label',{style:lblStyle},'Gender'),
124 el('div',{style:{display:'flex',borderRadius:'8px',overflow:'hidden',border:'1.5px solid '+(a.inputBorder||'#e5e7eb')}},
125 ['male','female'].map(function(g){ return el('button',{key:g,onClick:function(){setGender(g);},
126 style:{flex:1,padding:'8px',border:'none',background:gender===g?accent:'#fff',color:gender===g?'#fff':'#374151',fontWeight:600,fontSize:'13px',fontFamily:'inherit',cursor:'pointer'}},
127 g.charAt(0).toUpperCase()+g.slice(1)); })
128 )
129 )
130 ),
131
132 // Age / Weight / Height row
133 el('div',{style:{display:'grid',gridTemplateColumns:'1fr 1fr 1fr',gap:'12px',marginBottom:'12px'}},
134 el('div',null, el('label',{style:lblStyle},'Age'), el('input',{type:'number',value:age,min:10,max:100,style:inputStyle,onChange:function(e){setAge(e.target.value);}})),
135 el('div',null, el('label',{style:lblStyle},unit==='imperial'?'Weight (lbs)':'Weight (kg)'), el('input',{type:'number',value:weight,min:30,style:inputStyle,onChange:function(e){setWeight(e.target.value);}})),
136 el('div',null, el('label',{style:lblStyle},unit==='imperial'?'Height (in)':'Height (cm)'), el('input',{type:'number',value:height,min:50,style:inputStyle,onChange:function(e){setHeight(e.target.value);}}))
137 ),
138
139 // Activity + Goal
140 el('div',{style:{display:'grid',gridTemplateColumns:'1fr 1fr',gap:'12px',marginBottom:'20px'}},
141 el('div',null, el('label',{style:lblStyle},'Activity Level'),
142 el('select',{value:activity,style:{...inputStyle},onChange:function(e){setActivity(e.target.value);}},
143 ACTIVITY_OPTS.map(function(o){return el('option',{key:o.value,value:o.value},o.label);}))
144 ),
145 el('div',null, el('label',{style:lblStyle},'Goal'),
146 el('select',{value:goal,style:{...inputStyle},onChange:function(e){setGoal(e.target.value);}},
147 GOAL_OPTS.map(function(o){return el('option',{key:o.value,value:o.value},o.label);}))
148 )
149 ),
150
151 // Calorie result
152 el('div',{style:{background:a.calorieBg||accent,borderRadius:'12px',padding:'20px 24px',textAlign:'center',color:a.calorieColor||'#fff',marginBottom:'18px'}},
153 el('div',{style:{fontSize:'13px',fontWeight:600,opacity:.8,textTransform:'uppercase',letterSpacing:'.06em',marginBottom:'4px'}},'Daily Calories'),
154 el('div',{style:{fontSize:(a.calorieSize||52)+'px',fontWeight:800,lineHeight:1}},r.calories),
155 el('div',{style:{fontSize:'13px',opacity:.8,marginTop:'5px'}},'BMR: '+r.bmr+' · TDEE: '+r.tdee+' kcal')
156 ),
157
158 // Macro cards + optional donut
159 el('div',{style:{display:'flex',gap:'12px',alignItems:'center',marginBottom:'14px'}},
160 a.showPieChart && el('div',{style:{flexShrink:0}}, el(DonutChart)),
161 el('div',{style:{flex:1,display:'flex',flexDirection:'column',gap:'10px'}},
162 [[r.protein,'Protein','g','(4 kcal/g)',protClr],
163 [r.carbs, 'Carbohydrates','g','(4 kcal/g)',carbClr],
164 [r.fat, 'Fat','g','(9 kcal/g)',fatClr]].map(function(row){
165 return el('div',{key:row[1],style:{display:'flex',alignItems:'center',justifyContent:'space-between',background:a.statBg||'#f3f4f6',border:'1px solid '+(a.statBorder||'#e5e7eb'),borderRadius:'8px',padding:'10px 14px'}},
166 el('div',{style:{display:'flex',alignItems:'center',gap:'8px'}},
167 el('div',{style:{width:'12px',height:'12px',borderRadius:'50%',background:row[4],flexShrink:0}}),
168 el('span',{style:{fontWeight:600,fontSize:'14px',color:a.labelColor||'#374151'}},row[1]),
169 el('span',{style:{fontSize:'11px',color:'#9ca3af'}},row[3])
170 ),
171 el('span',{style:{fontWeight:800,fontSize:'20px',color:row[4]}},row[0]+row[2])
172 );
173 })
174 )
175 ),
176
177 // TDEE context row
178 el('div',{style:{display:'flex',gap:'10px'}},
179 el('div',{style:{...statStyle}}, el('div',{style:{fontSize:'20px',fontWeight:700,color:'#111827'}},r.bmr), el('div',{style:{fontSize:'11px',color:'#6b7280',marginTop:'3px'}},'BMR (base metabolic rate)')),
180 el('div',{style:{...statStyle}}, el('div',{style:{fontSize:'20px',fontWeight:700,color:'#111827'}},r.tdee), el('div',{style:{fontSize:'11px',color:'#6b7280',marginTop:'3px'}},'TDEE (maintenance calories)'))
181 )
182 )
183 );
184 }
185
186 registerBlockType('blockenberg/macro-calculator', {
187 edit: function(props) {
188 var a = props.attributes; var set = props.setAttributes;
189 var blockProps = useBlockProps((function () {
190 var _tvFn = getTypoCssVars();
191 var s = {};
192 if (_tvFn) {
193 Object.assign(s, _tvFn(a.titleTypo, '--bkbg-mcr-tt-'));
194 Object.assign(s, _tvFn(a.subtitleTypo, '--bkbg-mcr-st-'));
195 }
196 return { style: s };
197 })());
198 var colorSettings = [
199 {value:a.accentColor, onChange:function(v){set({accentColor:v});}, label:'Accent Color'},
200 {value:a.cardBg, onChange:function(v){set({cardBg:v});}, label:'Card Background'},
201 {value:a.calorieBg, onChange:function(v){set({calorieBg:v});}, label:'Calorie Card Background'},
202 {value:a.calorieColor, onChange:function(v){set({calorieColor:v});}, label:'Calorie Card Text'},
203 {value:a.proteinColor, onChange:function(v){set({proteinColor:v});}, label:'Protein Color'},
204 {value:a.carbColor, onChange:function(v){set({carbColor:v});}, label:'Carbs Color'},
205 {value:a.fatColor, onChange:function(v){set({fatColor:v});}, label:'Fat Color'},
206 {value:a.statBg, onChange:function(v){set({statBg:v});}, label:'Macro Card Background'},
207 {value:a.statBorder, onChange:function(v){set({statBorder:v});}, label:'Macro Card Border'},
208 {value:a.inputBorder, onChange:function(v){set({inputBorder:v});}, label:'Input Border'},
209 {value:a.labelColor, onChange:function(v){set({labelColor:v});}, label:'Label Color'},
210 {value:a.titleColor, onChange:function(v){set({titleColor:v});}, label:'Title Color'},
211 {value:a.subtitleColor, onChange:function(v){set({subtitleColor:v});}, label:'Subtitle Color'},
212 {value:a.sectionBg, onChange:function(v){set({sectionBg:v});}, label:'Section Background'}
213 ];
214 return el(Fragment, null,
215 el(InspectorControls, null,
216 el(PanelBody,{title:'Header',initialOpen:false},
217 el(ToggleControl,{__nextHasNoMarginBottom:true,label:'Show Title', checked:a.showTitle, onChange:function(v){set({showTitle:v});}}),
218 el(ToggleControl,{__nextHasNoMarginBottom:true,label:'Show Subtitle',checked:a.showSubtitle,onChange:function(v){set({showSubtitle:v});}}),
219 el(TextControl,{label:'Title', value:a.title, onChange:function(v){set({title:v});}}),
220 el(TextControl,{label:'Subtitle',value:a.subtitle,onChange:function(v){set({subtitle:v});}})
221 ),
222 el(PanelBody,{title:'Calculator Defaults',initialOpen:true},
223 el(SelectControl,{label:'Default Unit System',value:a.defaultUnit,options:[{label:'Metric (kg/cm)',value:'metric'},{label:'Imperial (lbs/in)',value:'imperial'}],onChange:function(v){set({defaultUnit:v});}}),
224 el(SelectControl,{label:'Default Gender',value:a.defaultGender,options:[{label:'Male',value:'male'},{label:'Female',value:'female'}],onChange:function(v){set({defaultGender:v});}}),
225 el(SelectControl,{label:'Default Activity',value:a.defaultActivity,options:ACTIVITY_OPTS,onChange:function(v){set({defaultActivity:v});}}),
226 el(SelectControl,{label:'Default Goal',value:a.defaultGoal,options:GOAL_OPTS,onChange:function(v){set({defaultGoal:v});}}),
227 el(ToggleControl,{__nextHasNoMarginBottom:true,label:'Show Donut Chart', checked:a.showPieChart, onChange:function(v){set({showPieChart:v});}}),
228 el(ToggleControl,{__nextHasNoMarginBottom:true,label:'Show Food Examples', checked:a.showFoodExamples,onChange:function(v){set({showFoodExamples:v});}})
229 ),
230
231 el( PanelBody, { title: __( 'Typography', 'blockenberg' ), initialOpen: false },
232 (function () { var C = getTypoControl(); return C ? el(C, { label: 'Title', value: a.titleTypo, onChange: function (v) { set({ titleTypo: v }); } }) : null; })(),
233 (function () { var C = getTypoControl(); return C ? el(C, { label: 'Subtitle', value: a.subtitleTypo, onChange: function (v) { set({ subtitleTypo: v }); } }) : null; })(),
234 el(RangeControl,{__nextHasNoMarginBottom:true,label:'Calorie Size', value:a.calorieSize, min:24,max:80,step:2, onChange:function(v){set({calorieSize:v});}})
235 ),
236 el(PanelColorSettings,{title:'Colors',initialOpen:false,colorSettings:colorSettings}),
237 el(PanelBody,{title:'Sizing & Layout',initialOpen:false},
238 el(RangeControl,{label:'Card Border Radius', value:a.cardRadius, min:0,max:40,step:1, onChange:function(v){set({cardRadius:v});}}),
239 el(RangeControl,{label:'Input Border Radius',value:a.inputRadius, min:0,max:20,step:1, onChange:function(v){set({inputRadius:v});}}),
240 el(RangeControl,{label:'Max Width (px)', value:a.maxWidth, min:340,max:960,step:10,onChange:function(v){set({maxWidth:v});}}),
241 el(RangeControl,{label:'Padding Top (px)', value:a.paddingTop, min:0,max:160,step:4, onChange:function(v){set({paddingTop:v});}}),
242 el(RangeControl,{label:'Padding Bottom (px)',value:a.paddingBottom,min:0,max:160,step:4, onChange:function(v){set({paddingBottom:v});}})
243 )
244 ),
245 el('div', blockProps, el(MacroPreview, {attributes:a}))
246 );
247 },
248 save: function(props) {
249 var a = props.attributes;
250 var _tvFn = getTypoCssVars();
251 var saveProps = wp.blockEditor.useBlockProps.save((function () {
252 var s = {};
253 if (_tvFn) {
254 Object.assign(s, _tvFn(a.titleTypo, '--bkbg-mcr-tt-'));
255 Object.assign(s, _tvFn(a.subtitleTypo, '--bkbg-mcr-st-'));
256 }
257 return { style: s };
258 })());
259 return el('div', saveProps,
260 el('div', {className:'bkbg-mcr-app','data-opts':JSON.stringify(a)}));
261 }
262 });
263 }() );
264