| 1 |
(function () { |
| 2 |
const { registerBlockType } = wp.blocks; |
| 3 |
const { createElement: el, Fragment } = wp.element; |
| 4 |
const { InspectorControls, PanelColorSettings, FontSizePicker, ColorPalette } = wp.blockEditor; |
| 5 |
const { __ } = wp.i18n; |
| 6 |
const { PanelBody, RangeControl, TextControl, SelectControl, Icon, TabPanel, __experimentalBoxControl } = wp.components; |
| 7 |
|
| 8 |
registerBlockType('wpdirectorykit/wdk-block-share', { |
| 9 |
title: 'Share Page Button', |
| 10 |
icon: el('svg', { width: 24, height: 24, viewBox: '0 0 24 24', fill: 'none', xmlns: 'http://www.w3.org/2000/svg' }, |
| 11 |
el('path', { |
| 12 |
d: 'M18 8a3 3 0 0 0-2.23 1.01l-6.06-3.34a3 3 0 1 0-.65 1.31l6 3.3a3 3 0 0 0 0 2.44l-6 3.3A3 3 0 1 0 9.71 17.03l6.06-3.34A3 3 0 1 0 18 8z', |
| 13 |
fill: 'currentColor' |
| 14 |
}) |
| 15 |
), |
| 16 |
category: 'wdk-blocks', |
| 17 |
attributes: { |
| 18 |
linkText: { |
| 19 |
type: 'string', |
| 20 |
default: 'Share this page', |
| 21 |
}, |
| 22 |
specialClass: { |
| 23 |
type: 'string', |
| 24 |
default: 'default-class', |
| 25 |
}, |
| 26 |
padding: { |
| 27 |
type: 'object', |
| 28 |
default: { top: '0px', right: '0px', bottom: '0px', left: '0px' }, |
| 29 |
}, |
| 30 |
margin: { |
| 31 |
type: 'object', |
| 32 |
default: { top: '0px', right: '0px', bottom: '0px', left: '0px' }, |
| 33 |
}, |
| 34 |
borderWidth: { |
| 35 |
type: 'number', |
| 36 |
default: '0', |
| 37 |
}, |
| 38 |
fontSize: { |
| 39 |
type: 'number', |
| 40 |
default: 16, |
| 41 |
}, |
| 42 |
textColor: { |
| 43 |
type: 'string', |
| 44 |
default: '#000000', |
| 45 |
}, |
| 46 |
backgroundColor: { |
| 47 |
type: 'string', |
| 48 |
default: '#ffffff', |
| 49 |
}, |
| 50 |
}, |
| 51 |
example: { |
| 52 |
attributes: { |
| 53 |
linkText: 'Share this зфпу', |
| 54 |
specialClass: 'example-class', |
| 55 |
textColor: '#ffffff', |
| 56 |
backgroundColor: '#0073aa', |
| 57 |
}, |
| 58 |
}, |
| 59 |
edit: function (props) { |
| 60 |
const { |
| 61 |
attributes: { |
| 62 |
linkText, specialClass, padding, margin, fontSize, textColor, backgroundColor, |
| 63 |
borderWidth, borderStyle, borderColor |
| 64 |
}, |
| 65 |
setAttributes, |
| 66 |
} = props; |
| 67 |
|
| 68 |
return el( |
| 69 |
Fragment, {}, |
| 70 |
|
| 71 |
el( |
| 72 |
InspectorControls, {}, |
| 73 |
el( |
| 74 |
TabPanel, { |
| 75 |
tabs: [ |
| 76 |
{ |
| 77 |
name: 'general', |
| 78 |
title: el(Fragment, {}, el(Icon, { icon: 'admin-generic' }), ' General'), |
| 79 |
className: 'general-tab', |
| 80 |
}, |
| 81 |
{ |
| 82 |
name: 'style', |
| 83 |
title: el(Fragment, {}, el(Icon, { icon: 'art' }), ' Style'), |
| 84 |
className: 'style-tab', |
| 85 |
}, |
| 86 |
], |
| 87 |
}, |
| 88 |
(tab) => { |
| 89 |
if (tab.name === 'general') { |
| 90 |
return el( |
| 91 |
Fragment, {}, |
| 92 |
el( |
| 93 |
PanelBody, { title: 'Block Settings', initialOpen: true }, |
| 94 |
el(TextControl, { |
| 95 |
label: 'Button Text', |
| 96 |
value: linkText, |
| 97 |
onChange: (value) => setAttributes({ linkText: value }) |
| 98 |
}), |
| 99 |
el(TextControl, { |
| 100 |
label: 'Special Class', |
| 101 |
value: specialClass, |
| 102 |
onChange: (value) => setAttributes({ specialClass: value }) |
| 103 |
}), |
| 104 |
) |
| 105 |
); |
| 106 |
} |
| 107 |
|
| 108 |
if (tab.name === 'style') { |
| 109 |
return el( |
| 110 |
Fragment, {}, |
| 111 |
el( |
| 112 |
PanelBody, { title: 'Border Settings', initialOpen: true }, |
| 113 |
el(RangeControl, { |
| 114 |
label: 'Border Width', |
| 115 |
values: borderWidth, |
| 116 |
onChange: (newWidth) => setAttributes({ borderWidth: newWidth }), |
| 117 |
}), |
| 118 |
el(SelectControl, { |
| 119 |
label: 'Border Style', |
| 120 |
value: borderStyle, |
| 121 |
options: [ |
| 122 |
{ label: 'None', value: 'none' }, |
| 123 |
{ label: 'Solid', value: 'solid' }, |
| 124 |
{ label: 'Dashed', value: 'dashed' }, |
| 125 |
{ label: 'Dotted', value: 'dotted' }, |
| 126 |
], |
| 127 |
onChange: (newStyle) => setAttributes({ borderStyle: newStyle }), |
| 128 |
}), |
| 129 |
el(ColorPalette, { |
| 130 |
label: 'Border Color', |
| 131 |
value: borderColor, |
| 132 |
onChange: (newColor) => setAttributes({ borderColor: newColor }), |
| 133 |
}) |
| 134 |
), |
| 135 |
el( |
| 136 |
PanelBody, { title: 'Space Control', initialOpen: true }, |
| 137 |
el(__experimentalBoxControl, { |
| 138 |
label: 'Padding', |
| 139 |
values: padding, |
| 140 |
onChange: (newPadding) => setAttributes({ padding: newPadding }), |
| 141 |
}), |
| 142 |
el(__experimentalBoxControl, { |
| 143 |
label: 'Margin', |
| 144 |
values: margin, |
| 145 |
onChange: (newMargin) => setAttributes({ margin: newMargin }), |
| 146 |
}), |
| 147 |
), |
| 148 |
el( |
| 149 |
PanelBody, { title: 'Font', initialOpen: true }, |
| 150 |
el(FontSizePicker, { |
| 151 |
fontSizes: [ |
| 152 |
{ name: 'small', size: 12 }, |
| 153 |
{ name: 'medium', size: 16 }, |
| 154 |
{ name: 'large', size: 20 }, |
| 155 |
{ name: 'extra large', size: 24 } |
| 156 |
], |
| 157 |
value: fontSize, |
| 158 |
onChange: (value) => setAttributes({ fontSize: value }) |
| 159 |
}), |
| 160 |
), |
| 161 |
el(PanelColorSettings, { |
| 162 |
title: 'Color Settings', |
| 163 |
colorSettings: [ |
| 164 |
{ |
| 165 |
value: textColor, |
| 166 |
onChange: (color) => setAttributes({ textColor: color }), |
| 167 |
label: 'Text Color', |
| 168 |
}, |
| 169 |
{ |
| 170 |
value: backgroundColor, |
| 171 |
onChange: (color) => setAttributes({ backgroundColor: color }), |
| 172 |
label: 'Background Color', |
| 173 |
} |
| 174 |
] |
| 175 |
}) |
| 176 |
); |
| 177 |
} |
| 178 |
} |
| 179 |
) |
| 180 |
), |
| 181 |
el( |
| 182 |
'a', |
| 183 |
{ |
| 184 |
className: `wdk-block-share ${specialClass}`, |
| 185 |
style: { |
| 186 |
padding: `${padding.top} ${padding.right} ${padding.bottom} ${padding.left}`, |
| 187 |
margin: `${margin.top} ${margin.right} ${margin.bottom} ${margin.left}`, |
| 188 |
fontSize: `${fontSize}`, |
| 189 |
color: textColor, |
| 190 |
backgroundColor, |
| 191 |
borderWidth, |
| 192 |
borderStyle, |
| 193 |
borderColor, |
| 194 |
textDecoration: 'none', |
| 195 |
display: 'inline-block' |
| 196 |
}, |
| 197 |
href: '', |
| 198 |
}, |
| 199 |
linkText |
| 200 |
) |
| 201 |
); |
| 202 |
}, |
| 203 |
save: function (props) { |
| 204 |
const { |
| 205 |
attributes: { |
| 206 |
linkText, specialClass, padding, margin, fontSize, textColor, backgroundColor, |
| 207 |
borderWidth, borderStyle, borderColor |
| 208 |
}, |
| 209 |
} = props; |
| 210 |
|
| 211 |
return el( |
| 212 |
'a', |
| 213 |
{ |
| 214 |
className: `wdk-block-share ${specialClass}`, |
| 215 |
style: { |
| 216 |
padding: `${padding.top} ${padding.right} ${padding.bottom} ${padding.left}`, |
| 217 |
margin: `${margin.top} ${margin.right} ${margin.bottom} ${margin.left}`, |
| 218 |
fontSize: `${fontSize}`, |
| 219 |
color: textColor, |
| 220 |
backgroundColor, |
| 221 |
borderWidth, |
| 222 |
borderStyle, |
| 223 |
borderColor, |
| 224 |
textDecoration: 'none', |
| 225 |
display: 'inline-block' |
| 226 |
}, |
| 227 |
href: '#', |
| 228 |
}, |
| 229 |
linkText |
| 230 |
); |
| 231 |
} |
| 232 |
}); |
| 233 |
})(); |
| 234 |
|