tnp-blocks.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. (function (blocks, editor, element, components) {
  2. const el = element.createElement;
  3. const {registerBlockType} = blocks;
  4. const { RichText, InspectorControls, withColors, PanelColorSettings, getColorClassName, AlignmentToolbar, BlockControls } = editor;
  5. const { Fragment } = element;
  6. const { TextControl, RadioControl, Panel, PanelBody, PanelRow, SelectControl, RangeControl } = components;
  7. const colorSamples = [
  8. {
  9. name: 'GREEN SEA',
  10. slug: 'GREENSEA',
  11. color: '#16A085'
  12. },
  13. {
  14. name: 'NEPHRITIS',
  15. slug: 'NEPHRITIS',
  16. color: '#27AE60'
  17. },
  18. {
  19. name: 'BELIZE HOLE',
  20. slug: 'BELIZEHOLE',
  21. color: '#2980B9'
  22. },
  23. {
  24. name: 'WISTERIA',
  25. slug: 'WISTERIA',
  26. color: '#8E44AD'
  27. },
  28. {
  29. name: 'MIDNIGHT BLUE',
  30. slug: 'MIDNIGHTBLUE',
  31. color: '#2C3E50'
  32. },
  33. {
  34. name: 'ORANGE',
  35. slug: 'ORANGE',
  36. color: '#F39C12'
  37. },
  38. {
  39. name: 'ALIZARIN',
  40. slug: 'ALIZARIN',
  41. color: '#E74C3C'
  42. },
  43. {
  44. name: 'WHITE',
  45. slug: 'WHITE',
  46. color: '#FFFFFF'
  47. },
  48. {
  49. name: 'CLOUDS',
  50. slug: 'CLOUDS',
  51. color: '#ECF0F1'
  52. },
  53. {
  54. name: 'ASBESTOS',
  55. slug: 'ASBESTOS',
  56. color: '#7F8C8D'
  57. }
  58. ];
  59. registerBlockType('tnp/minimal', {
  60. title: 'Newsletter subscription form',
  61. icon: 'email',
  62. category: 'common',
  63. keywords: ['newsletter', 'subscription', 'form'],
  64. attributes: {
  65. formtype: {type: 'string', default: 'minimal'},
  66. content: { type: 'array', source: 'children', selector: 'p', default: 'Subscribe to our newsletter!'},
  67. list_ids: { type: 'string' },
  68. rowColor: { type: 'string'},
  69. customRowColor: { type: 'string'},
  70. textColor: { type: 'string'},
  71. customTextColor: { type: 'string'},
  72. buttonColor: { type: 'string'},
  73. customButtonColor: { type: 'string'},
  74. padding: {type: 'integer', default: 20},
  75. alignment: { type: 'string'}
  76. },
  77. edit: withColors('rowColor', 'textColor', 'buttonColor')(function (props) {
  78. function onChangeContent( newContent ) {
  79. props.setAttributes( { content: newContent } );
  80. }
  81. function onChangeAlignment( newAlignment ) {
  82. props.setAttributes( { alignment: newAlignment } );
  83. }
  84. return el( Fragment, {},
  85. el( InspectorControls, {},
  86. // 1st Panel - Form Settings
  87. el( PanelBody, { title: 'Form Settings', initialOpen: true },
  88. /* Form type */
  89. el( RadioControl,
  90. {
  91. label: 'Form type',
  92. options : [
  93. { label: 'Minimal', value: 'minimal' },
  94. { label: 'Full', value: 'full' },
  95. ],
  96. onChange: ( value ) => {
  97. props.setAttributes( { formtype: value } );
  98. },
  99. selected: props.attributes.formtype
  100. }
  101. ),
  102. /* Lists field */
  103. el( PanelRow, {},
  104. el( TextControl,
  105. {
  106. label: 'Lists IDs (comma separated)',
  107. onChange: ( value ) => {
  108. props.setAttributes( { list_ids: value } );
  109. },
  110. value: props.attributes.list_ids
  111. }
  112. )
  113. )
  114. ),
  115. /* Style */
  116. el( PanelColorSettings, {
  117. title: 'Style',
  118. colorSettings: [
  119. {
  120. colors: colorSamples, // here you can pass custom colors
  121. value: props.rowColor.color,
  122. label: 'Background color',
  123. onChange: props.setRowColor,
  124. },
  125. {
  126. colors: colorSamples, // here you can pass custom colors
  127. value: props.textColor.color,
  128. label: 'Text color',
  129. onChange: props.setTextColor,
  130. },
  131. {
  132. colors: colorSamples, // here you can pass custom colors
  133. value: props.buttonColor.color,
  134. label: 'Button color',
  135. onChange: props.setButtonColor,
  136. }
  137. ]
  138. }),
  139. el( RangeControl,
  140. {
  141. label: 'Padding',
  142. min: 0,
  143. max: 100,
  144. onChange: ( value ) => {
  145. props.setAttributes( { padding: value } );
  146. },
  147. value: props.attributes.padding
  148. }
  149. )
  150. ),
  151. el(
  152. "div",
  153. {style: {backgroundColor: props.rowColor.color, color: props.textColor.color, padding: props.attributes.padding, textAlign: props.attributes.alignment}},
  154. el(
  155. BlockControls,
  156. { key: 'controls' },
  157. el(
  158. AlignmentToolbar,
  159. {
  160. value: props.attributes.alignment,
  161. onChange: onChangeAlignment
  162. }
  163. )
  164. ),
  165. el(RichText,
  166. {
  167. tagName: 'p',
  168. format: 'string',
  169. onChange: onChangeContent,
  170. value: props.attributes.content,
  171. // formattingControls: [ 'bold' ]
  172. }),
  173. el('div',
  174. {style: {backgroundColor: 'lightGrey', margin: '20px', padding: '5px',
  175. fontFamily: '-apple-system, BlinkMacSystemFont, Segoe UI, Roboto, Oxygen-Sans, Ubuntu, Cantarell, Helvetica Neue, sans-serif'}},
  176. el('svg',
  177. {
  178. width: 20,
  179. height: 20
  180. },
  181. wp.element.createElement( 'path',
  182. {
  183. d: "M6 14H4V6h2V4H2v12h4M7.1 17h2.1l3.7-14h-2.1M14 4v2h2v8h-2v2h4V4"
  184. }
  185. )
  186. ),
  187. ' Newsletter Form'
  188. ),
  189. ))
  190. }),
  191. save: function (props) {
  192. var rowClass = getColorClassName( 'row-color', props.attributes.rowColor );
  193. var textClass = getColorClassName( 'text-color', props.attributes.textColor );
  194. var buttonClass = getColorClassName( 'button-color', props.attributes.buttonColor );
  195. formtype_attr = "";
  196. if (props.attributes.formtype != "full") {
  197. formtype_attr = " type=\"minimal\"";
  198. }
  199. lists_attr = "";
  200. if (props.attributes.list_ids != "") {
  201. lists_attr = " lists=\"" + props.attributes.list_ids + "\"";
  202. }
  203. button_color_attr = "";
  204. button_color = buttonClass ? undefined : props.attributes.customButtonColor;
  205. if (button_color != "") {
  206. button_color_attr = " button_color=\"" + button_color + "\"";
  207. }
  208. var formStyles = {
  209. backgroundColor: rowClass ? undefined : props.attributes.customRowColor,
  210. color: textClass ? undefined : props.attributes.customTextColor,
  211. padding: props.attributes.padding,
  212. textAlign: props.attributes.alignment
  213. };
  214. return (
  215. el('div', {style: formStyles},
  216. el( RichText.Content, {
  217. tagName: 'p',
  218. value: props.attributes.content
  219. }),
  220. el(
  221. "div",
  222. {},
  223. "[newsletter_form" + formtype_attr + lists_attr + button_color_attr + "]"
  224. )));
  225. }
  226. });
  227. })(
  228. window.wp.blocks,
  229. window.wp.editor,
  230. window.wp.element,
  231. window.wp.components,
  232. );