Advertisement
puggan

gutenberg-box.js

Sep 18th, 2018
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. wp.blocks.registerBlockType(
  2.     'spiro/box',
  3.     {
  4.         title: 'Spiro Box',
  5.         icon: 'tablet',
  6.         category: 'layout',
  7.  
  8.         attributes: {
  9.             headline: {
  10.                 type: 'string',
  11.             },
  12.             color: {
  13.                 type: 'string',
  14.             },
  15.             boxurl: {
  16.                 type: 'string',
  17.             },
  18.             content: {
  19.                 type: 'string',
  20.             }
  21.         },
  22.  
  23.         edit: function(props) {
  24.             return wp.element.createElement(
  25.                 'div',
  26.                 {
  27.                     className: 'box box-' + props.attributes.color
  28.                 },
  29.                 [
  30.                     // Headline
  31.                     wp.element.createElement(
  32.                         'h3',
  33.                         {
  34.                             style:
  35.                                 {
  36.                                     display: 'flex',
  37.                                 },
  38.                         },
  39.                         [
  40.                             wp.element.createElement(
  41.                                 'span',
  42.                                 {},
  43.                                 'Headline: '
  44.                             ),
  45.                             wp.element.createElement(
  46.                                 wp.editor.RichText,
  47.                                 {
  48.                                     key: 'editable',
  49.                                     tagName: 'span',
  50.                                     style: {
  51.                                         display: 'inline-block',
  52.                                         minWidth: '100px',
  53.                                         border: '1px dashed gray'
  54.                                     },
  55.                                     onChange: function(data) {
  56.                                         props.setAttributes({headline: data});
  57.                                     },
  58.                                     value: props.attributes.headline
  59.                                 }
  60.                             ),
  61.                         ]
  62.                     ),
  63.  
  64.                     // Color
  65.                     wp.element.createElement(
  66.                         'div',
  67.                         {
  68.                             style:
  69.                                 {
  70.                                     display: 'flex',
  71.                                 }
  72.                         },
  73.                         [
  74.                             wp.element.createElement(
  75.                                 'span',
  76.                                 {},
  77.                                 'Color:'
  78.                             ),
  79.                             // White
  80.                             wp.element.createElement(
  81.                                 'label',
  82.                                 {},
  83.                                 [
  84.                                     wp.element.createElement(
  85.                                         'input',
  86.                                         {
  87.                                             style: {marginLeft: '2em'},
  88.                                             type: 'radio',
  89.                                             name: props.id + '_color',
  90.                                             onChange: function(data) {
  91.                                                 props.setAttributes({color: 'white'});
  92.                                             },
  93.                                         }
  94.                                     ),
  95.                                     wp.element.createElement('span', {}, 'White'),
  96.                                 ]
  97.                             ),
  98.  
  99.                             // Gold
  100.                             wp.element.createElement(
  101.                                 'label',
  102.                                 {},
  103.                                 [
  104.                                     wp.element.createElement(
  105.                                         'input',
  106.                                         {
  107.                                             style: {marginLeft: '2em'},
  108.                                             type: 'radio',
  109.                                             name: props.id + '_color',
  110.                                             onChange: function(data) {
  111.                                                 props.setAttributes({color: 'gold'});
  112.                                             },
  113.                                         }
  114.                                     ),
  115.                                     wp.element.createElement('span', {}, 'Gold'),
  116.                                 ]
  117.                             ),
  118.  
  119.                             // Blue
  120.                             wp.element.createElement(
  121.                                 'label',
  122.                                 {},
  123.                                 [
  124.                                     wp.element.createElement(
  125.                                         'input',
  126.                                         {
  127.                                             style: {marginLeft: '2em'},
  128.                                             type: 'radio',
  129.                                             name: props.id + '_color',
  130.                                             onChange: function(data) {
  131.                                                 props.setAttributes({color: 'blue'});
  132.                                             },
  133.                                         }
  134.                                     ),
  135.                                     wp.element.createElement('span', {}, 'Blue'),
  136.                                 ]
  137.                             ),
  138.                         ]
  139.                     ),
  140.  
  141.                     // URL
  142.                     wp.element.createElement(
  143.                         'label',
  144.                         {
  145.                             style: {
  146.                                 display: 'flex',
  147.                             }
  148.                         },
  149.                         wp.element.createElement(
  150.                             'span',
  151.                             {},
  152.                             'URL:'
  153.                         ),
  154.                         wp.element.createElement(
  155.                             wp.editor.RichText,
  156.                             {
  157.                                 key: 'editable',
  158.                                 tagName: 'p',
  159.                                 style: {
  160.                                     minWidth: '100px',
  161.                                     border: '1px dashed gray'
  162.                                 },
  163.                                 onChange: function(data) {
  164.                                     props.setAttributes({boxurl: data});
  165.                                     window.console.log('new url: ', data);
  166.                                     window.console.log('url: ', props.attributes.boxurl);
  167.                                 },
  168.                                 value: props.attributes.boxurl
  169.                             }
  170.                         ),
  171.                     ),
  172.  
  173.                     // Text
  174.                     wp.element.createElement(
  175.                         'label',
  176.                         {
  177.                             style:
  178.                                 {
  179.                                     display: 'flex',
  180.                                 }
  181.                         },
  182.                         [
  183.                             'Text:',
  184.                             wp.element.createElement(
  185.                                 wp.editor.RichText,
  186.                                 {
  187.                                     key: 'editable',
  188.                                     tagName: 'p',
  189.                                     style: {
  190.                                         minWidth: '100px',
  191.                                         border: '1px dashed gray'
  192.                                     },
  193.                                     onChange: function(data) {
  194.                                         props.setAttributes({content: data});
  195.                                     },
  196.                                     value: props.attributes.content
  197.                                 }
  198.                             )
  199.                         ]
  200.                     ),
  201.                 ]
  202.             )
  203.                 ;
  204.         },
  205.  
  206.         save: function(props) {
  207.             return wp.element.createElement(
  208.                 'div',
  209.                 {
  210.                     className: 'box box-' + props.attributes.color
  211.                 },
  212.                 wp.element.createElement(
  213.                     'a',
  214.                     {
  215.                         href: props.attributes.boxurl,
  216.                     },
  217.                     [
  218.                         wp.element.createElement(
  219.                             'h3',
  220.                             {},
  221.                             props.attributes.headline
  222.                         ),
  223.                         wp.element.createElement(
  224.                             'p',
  225.                             {},
  226.                             props.attributes.content
  227.                         )
  228.                     ]
  229.                 )
  230.             )
  231.         }
  232.     }
  233. );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement