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 / gradient-generator / index.js

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

206 lines 15.6 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 useEffect = wp.element.useEffect;
5 var Fragment = wp.element.Fragment;
6 var registerBlockType = wp.blocks.registerBlockType;
7 var __ = wp.i18n.__;
8 var InspectorControls = wp.blockEditor.InspectorControls;
9 var PanelColorSettings = wp.blockEditor.PanelColorSettings;
10 var useBlockProps = wp.blockEditor.useBlockProps;
11 var PanelBody = wp.components.PanelBody;
12 var RangeControl = wp.components.RangeControl;
13 var SelectControl = wp.components.SelectControl;
14 var TextControl = wp.components.TextControl;
15 var ToggleControl = wp.components.ToggleControl;
16 var Button = wp.components.Button;
17
18 /* ── Typography lazy getters ── */
19 function _tc() { return window.bkbgTypographyControl || null; }
20 function _tv() { return window.bkbgTypoCssVars || function () { return {}; }; }
21
22 var PRESETS = [
23 { label:'Purple Dream', stops:'#6c3fb5 0%,#06b6d4 100%', type:'linear', angle:135 },
24 { label:'Sunset', stops:'#f97316 0%,#ef4444 50%,#a855f7 100%', type:'linear', angle:135 },
25 { label:'Ocean', stops:'#0ea5e9 0%,#10b981 100%', type:'linear', angle:135 },
26 { label:'Midnight', stops:'#1e1b4b 0%,#6c3fb5 50%,#0ea5e9 100%', type:'linear', angle:225 },
27 { label:'Peach', stops:'#f9a8d4 0%,#fed7aa 100%', type:'linear', angle:90 },
28 { label:'Radial Burst', stops:'#fde68a 0%,#f97316 60%,#ef4444 100%', type:'radial', angle:135 },
29 { label:'Forest', stops:'#064e3b 0%,#10b981 100%', type:'linear', angle:135 },
30 { label:'Rose Gold', stops:'#fda4af 0%,#fb7185 40%,#f43f5e 100%', type:'linear', angle:135 }
31 ];
32
33 function parseStops(str) {
34 return str.split(',').map(function(s) {
35 var parts = s.trim().match(/^(#[\w]+|rgba?\([^)]+\))\s+(\d+%?)$/);
36 if (parts) return { color: parts[1], pos: parts[2] };
37 return { color: s.trim(), pos: '' };
38 });
39 }
40
41 function buildCSS(type, angle, stops) {
42 var stopStr = stops.map(function(s){ return s.color + (s.pos ? ' ' + s.pos : ''); }).join(', ');
43 if (type === 'radial') return 'radial-gradient(circle, ' + stopStr + ')';
44 if (type === 'conic') return 'conic-gradient(from ' + angle + 'deg, ' + stopStr + ')';
45 return 'linear-gradient(' + angle + 'deg, ' + stopStr + ')';
46 }
47
48 function GradientPreview(props) {
49 var a = props.attributes;
50 var accent = a.accentColor || '#6c3fb5';
51
52 var _stops = useState(parseStops(a.defaultStops || '#6c3fb5 0%,#06b6d4 100%'));
53 var stops = _stops[0]; var setStops = _stops[1];
54 var _type = useState(a.defaultType || 'linear'); var type = _type[0]; var setType = _type[1];
55 var _angle = useState(a.defaultAngle || 135); var angle= _angle[0];var setAngle= _angle[1];
56 var _copied= useState(false); var copied=_copied[0];var setCopied=_copied[1];
57
58 var css = buildCSS(type, angle, stops);
59
60 function updateStop(i, key, val) {
61 var ns = stops.map(function(s,idx){ return idx===i ? Object.assign({},s,((function(o){o[key]=val;return o;})({}))) : s; });
62 setStops(ns);
63 }
64 function addStop() {
65 setStops(stops.concat([{color:'#ffffff',pos:stops.length?'100%':'50%'}]));
66 }
67 function removeStop(i) {
68 if (stops.length <= 2) return;
69 setStops(stops.filter(function(_,idx){return idx!==i;}));
70 }
71
72 function copyCSS() {
73 var text = 'background: ' + css + ';';
74 try { navigator.clipboard.writeText(text).then(function(){ setCopied(true); setTimeout(function(){setCopied(false);},1500); }); } catch(e){}
75 }
76
77 var inputStyle = {padding:'8px 10px',borderRadius:'6px',border:'1.5px solid '+(a.controlsBorder||'#e5e7eb'),fontSize:'14px',fontFamily:'inherit',outline:'none',background:'#fff'};
78
79 var wrapS = Object.assign({paddingTop:(a.paddingTop||60)+'px',paddingBottom:(a.paddingBottom||60)+'px',background:a.sectionBg||undefined}, _tv()(a.typoTitle,'--bkbg-gg-tt-'), _tv()(a.typoBody,'--bkbg-gg-bd-'));
80 return el('div', {style:wrapS},
81 el('div', {style:{background:a.cardBg,borderRadius:(a.cardRadius||16)+'px',padding:'36px 32px',maxWidth:(a.maxWidth||600)+'px',margin:'0 auto',boxShadow:'0 4px 24px rgba(0,0,0,.09)'}},
82
83 (a.showTitle||a.showSubtitle) && el('div', {style:{marginBottom:'22px'}},
84 a.showTitle && el('div', {className:'bkbg-gg-title',style:{color:a.titleColor,marginBottom:'6px'}}, a.title),
85 a.showSubtitle && el('div', {className:'bkbg-gg-subtitle',style:{color:a.subtitleColor}}, a.subtitle)
86 ),
87
88 // Preview swatch
89 el('div', {style:{height:(a.previewHeight||200)+'px',borderRadius:(a.previewRadius||12)+'px',background:css,marginBottom:'20px',boxShadow:'0 2px 12px rgba(0,0,0,.12)',transition:'background .2s'}}),
90
91 // Controls row
92 el('div', {style:{background:a.controlsBg||'#f9fafb',border:'1.5px solid '+(a.controlsBorder||'#e5e7eb'),borderRadius:'10px',padding:'16px',marginBottom:'14px'}},
93 el('div', {style:{display:'flex',gap:'12px',flexWrap:'wrap',alignItems:'center',marginBottom:'14px'}},
94 el('div', null,
95 el('label', {style:{display:'block',fontSize:'12px',fontWeight:600,color:a.labelColor||'#374151',marginBottom:'4px'}}, 'Type'),
96 el('select', {value:type, onChange:function(e){setType(e.target.value);}, style:Object.assign({},inputStyle,{cursor:'pointer'})},
97 [{label:'Linear',value:'linear'},{label:'Radial',value:'radial'},{label:'Conic',value:'conic'}].map(function(o){return el('option',{key:o.value,value:o.value},o.label);}))
98 ),
99 (type==='linear'||type==='conic') && el('div', null,
100 el('label', {style:{display:'block',fontSize:'12px',fontWeight:600,color:a.labelColor||'#374151',marginBottom:'4px'}}, 'Angle'),
101 el('div', {style:{display:'flex',alignItems:'center',gap:'6px'}},
102 el('input', {type:'range',value:angle,min:0,max:360,step:5,style:{width:'100px',cursor:'pointer'},onChange:function(e){setAngle(parseInt(e.target.value));}}),
103 el('span', {style:{fontSize:'14px',fontWeight:600,minWidth:'40px'}}, angle+'°')
104 )
105 )
106 ),
107 // Color stops
108 el('div', {style:{display:'flex',flexDirection:'column',gap:'8px',marginBottom:'10px'}},
109 stops.map(function(s,i){
110 return el('div', {key:i, style:{display:'flex',gap:'8px',alignItems:'center'}},
111 el('input', {type:'color',value:s.color,style:{width:'36px',height:'36px',border:'none',borderRadius:'6px',cursor:'pointer',padding:'0'},onChange:function(e){updateStop(i,'color',e.target.value);}}),
112 el('input', {type:'text',value:s.color,style:Object.assign({},inputStyle,{width:'90px'}),onChange:function(e){updateStop(i,'color',e.target.value);},placeholder:'#rrggbb'}),
113 el('input', {type:'text',value:s.pos,style:Object.assign({},inputStyle,{width:'64px'}),onChange:function(e){updateStop(i,'pos',e.target.value);},placeholder:'0%'}),
114 el('button', {onClick:function(){removeStop(i);},disabled:stops.length<=2, style:{padding:'6px 10px',borderRadius:'6px',border:'none',background:'#fee2e2',color:'#ef4444',cursor:'pointer',fontWeight:700,fontSize:'16px',lineHeight:1}}, '×')
115 );
116 })
117 ),
118 el('button', {onClick:addStop, style:{padding:'7px 14px',borderRadius:'6px',border:'1.5px dashed '+(a.controlsBorder||'#e5e7eb'),background:'transparent',color:accent,fontWeight:600,fontSize:'13px',cursor:'pointer',width:'100%',fontFamily:'inherit'}}, '+ Add Color Stop')
119 ),
120
121 // Presets
122 a.showPresets && el('div', {style:{marginBottom:'14px'}},
123 el('div', {style:{fontSize:'12px',fontWeight:600,color:a.labelColor||'#374151',marginBottom:'8px',textTransform:'uppercase',letterSpacing:'.05em'}}, 'Presets'),
124 el('div', {style:{display:'flex',flexWrap:'wrap',gap:'8px'}},
125 PRESETS.map(function(p){
126 return el('button', {key:p.label, onClick:function(){setStops(parseStops(p.stops));setType(p.type);setAngle(p.angle);},
127 style:{padding:'0 0 4px',border:'none',background:'none',cursor:'pointer',display:'flex',flexDirection:'column',alignItems:'center',gap:'4px'}},
128 el('div', {style:{width:'48px',height:'32px',borderRadius:'6px',background:buildCSS(p.type,p.angle,parseStops(p.stops)),boxShadow:'0 1px 4px rgba(0,0,0,.15)'}}),
129 el('span', {style:{fontSize:'10px',color:a.labelColor||'#6b7280',fontFamily:'inherit'}}, p.label)
130 );
131 })
132 )
133 ),
134
135 // CSS output
136 a.showCssOutput && el('div', {style:{position:'relative'}},
137 el('div', {style:{background:a.cssBg||'#1e1b4b',borderRadius:'8px',padding:'14px 16px',paddingRight:'100px'}},
138 el('code', {style:{fontSize:'13px',color:a.cssColor||'#c4b5fd',fontFamily:"'SFMono-Regular',Consolas,monospace",wordBreak:'break-all'}},
139 'background: ' + css + ';')
140 ),
141 el('button', {onClick:copyCSS,
142 style:{position:'absolute',right:'12px',top:'50%',transform:'translateY(-50%)',padding:'6px 14px',borderRadius:'6px',border:'none',background:copied?'#10b981':(a.copyBg||accent),color:a.copyColor||'#fff',fontWeight:600,fontSize:'12px',cursor:'pointer',fontFamily:'inherit',whiteSpace:'nowrap',transition:'background .2s'}},
143 copied?'✓ Copied':'Copy CSS')
144 )
145 )
146 );
147 }
148
149 registerBlockType('blockenberg/gradient-generator', {
150 edit: function(props) {
151 var a = props.attributes; var set = props.setAttributes;
152 var blockProps = useBlockProps();
153 var colorSettings = [
154 { value: a.accentColor, onChange: function(v){set({accentColor:v});}, label: 'Accent Color' },
155 { value: a.cardBg, onChange: function(v){set({cardBg:v});}, label: 'Card Background' },
156 { value: a.controlsBg, onChange: function(v){set({controlsBg:v});}, label: 'Controls Background' },
157 { value: a.controlsBorder, onChange: function(v){set({controlsBorder:v});}, label: 'Controls Border' },
158 { value: a.cssBg, onChange: function(v){set({cssBg:v});}, label: 'CSS Block Background' },
159 { value: a.cssColor, onChange: function(v){set({cssColor:v});}, label: 'CSS Code Color' },
160 { value: a.copyBg, onChange: function(v){set({copyBg:v});}, label: 'Copy Button Background' },
161 { value: a.copyColor, onChange: function(v){set({copyColor:v});}, label: 'Copy Button Text' },
162 { value: a.labelColor, onChange: function(v){set({labelColor:v});}, label: 'Label Color' },
163 { value: a.titleColor, onChange: function(v){set({titleColor:v});}, label: 'Title Color' },
164 { value: a.subtitleColor, onChange: function(v){set({subtitleColor:v});}, label: 'Subtitle Color' },
165 { value: a.sectionBg, onChange: function(v){set({sectionBg:v});}, label: 'Section Background' }
166 ];
167 return el(Fragment, null,
168 el(InspectorControls, null,
169 el(PanelBody,{title:'Header',initialOpen:false},
170 el(ToggleControl,{__nextHasNoMarginBottom:true,label:'Show Title', checked:a.showTitle, onChange:function(v){set({showTitle:v});}}),
171 el(ToggleControl,{__nextHasNoMarginBottom:true,label:'Show Subtitle',checked:a.showSubtitle,onChange:function(v){set({showSubtitle:v});}}),
172 el(TextControl,{label:'Title', value:a.title, onChange:function(v){set({title:v});}}),
173 el(TextControl,{label:'Subtitle',value:a.subtitle,onChange:function(v){set({subtitle:v});}})
174 ),
175 el(PanelBody,{title:'Generator Defaults',initialOpen:true},
176 el(TextControl, {label:'Default Color Stops (comma-separated)',value:a.defaultStops, onChange:function(v){set({defaultStops:v});},help:'e.g. #6c3fb5 0%,#06b6d4 100%'}),
177 el(SelectControl,{label:'Default Gradient Type',value:a.defaultType, options:[{label:'Linear',value:'linear'},{label:'Radial',value:'radial'},{label:'Conic',value:'conic'}], onChange:function(v){set({defaultType:v});}}),
178 el(RangeControl, {label:'Default Angle',value:a.defaultAngle,min:0,max:360,step:5,onChange:function(v){set({defaultAngle:v});}}),
179 el(ToggleControl,{__nextHasNoMarginBottom:true,label:'Show Presets', checked:a.showPresets, onChange:function(v){set({showPresets:v});}}),
180 el(ToggleControl,{__nextHasNoMarginBottom:true,label:'Show CSS Output',checked:a.showCssOutput,onChange:function(v){set({showCssOutput:v});}})
181 ),
182
183 el( PanelBody, { title: __( 'Typography', 'blockenberg' ), initialOpen: false },
184 _tc() && el(_tc(), { label:'Title', value:a.typoTitle, onChange:function(v){ set({typoTitle:v}); } }),
185 _tc() && el(_tc(), { label:'Subtitle', value:a.typoBody, onChange:function(v){ set({typoBody:v}); } })
186 ),
187 el(PanelColorSettings,{title:'Colors',initialOpen:false,colorSettings:colorSettings}),
188 el(PanelBody,{title:'Sizing & Layout',initialOpen:false},
189 el(RangeControl,{label:'Preview Height (px)', value:a.previewHeight, min:80,max:400,step:10, onChange:function(v){set({previewHeight:v});}}),
190 el(RangeControl,{label:'Card Border Radius', value:a.cardRadius, min:0, max:40, step:1, onChange:function(v){set({cardRadius:v});}}),
191 el(RangeControl,{label:'Preview Border Radius',value:a.previewRadius,min:0, max:40, step:1, onChange:function(v){set({previewRadius:v});}}),
192 el(RangeControl,{label:'Max Width (px)', value:a.maxWidth, min:340,max:960,step:10,onChange:function(v){set({maxWidth:v});}}),
193 el(RangeControl,{label:'Padding Top (px)', value:a.paddingTop, min:0, max:160,step:4, onChange:function(v){set({paddingTop:v});}}),
194 el(RangeControl,{label:'Padding Bottom (px)', value:a.paddingBottom, min:0, max:160,step:4, onChange:function(v){set({paddingBottom:v});}})
195 )
196 ),
197 el('div', blockProps, el(GradientPreview, {attributes:a, setAttributes:set}))
198 );
199 },
200 save: function(props) {
201 var a = props.attributes;
202 return el('div', wp.blockEditor.useBlockProps.save(), el('div', {className:'bkbg-gg-app','data-opts':JSON.stringify(a)}));
203 }
204 });
205 }() );
206