shortcoder.js
88 lines
| 1 | (function( blocks, element ){ |
| 2 | var el = element.createElement; |
| 3 | |
| 4 | function shortcoderBlock( props ){ |
| 5 | |
| 6 | return el( 'div', { |
| 7 | 'id': 'shortcoder_wrap_' + props.instanceId, |
| 8 | 'style': { |
| 9 | 'background-color': '#f8f9f9', |
| 10 | 'padding': '15px', |
| 11 | 'border-radius': '5px' |
| 12 | } |
| 13 | }, |
| 14 | el( window.wp.editor.PlainText, { |
| 15 | 'id': 'shortcoder_box_' + props.instanceId, |
| 16 | 'className': 'input-control', |
| 17 | 'onChange': function(value){ |
| 18 | props.setAttributes({ |
| 19 | text: value |
| 20 | }); |
| 21 | }, |
| 22 | 'placeholder': 'Select/enter shortcode', |
| 23 | 'spellcheck': 'false', |
| 24 | 'value': props.attributes.text, |
| 25 | 'style': { |
| 26 | 'font-family': 'monospace', |
| 27 | 'font-size': '15px' |
| 28 | } |
| 29 | }), |
| 30 | el( 'button', { |
| 31 | 'class': 'button button-primary', |
| 32 | 'onClick': function(){ |
| 33 | if( window.sc_show_insert ){ |
| 34 | var sc_val = document.getElementById('shortcoder_box_' + props.instanceId).value; |
| 35 | window.sc_show_insert( sc_val, props.instanceId ); |
| 36 | } |
| 37 | } |
| 38 | }, 'Select shortcode') |
| 39 | ); |
| 40 | |
| 41 | } |
| 42 | |
| 43 | function icon() { |
| 44 | return el('svg', { |
| 45 | width: 64, |
| 46 | height: 64, |
| 47 | viewBox: '0 0 16.933 16.933' |
| 48 | }, el('g', { |
| 49 | fill: '#00aee9', |
| 50 | transform: 'translate(-22.213 -238.799) scale(.93445)' |
| 51 | }, el('path', { |
| 52 | d: 'M24.549 257.815a.493.493 0 00-.495.494v12.602c0 .274.22.495.495.495h3.541c.274 0 .494-.22.494-.495v-.993a.493.493 0 00-.494-.494h-2.054v-9.627h2.054c.274 0 .494-.22.494-.494v-.994a.493.493 0 00-.494-.494H24.55z' |
| 53 | }), el('rect', { |
| 54 | ry: 0.515, |
| 55 | transform: 'matrix(1 0 -.2671 .96367 0 0)', |
| 56 | y: 265.478, |
| 57 | x: 104.75, |
| 58 | height: 18.217, |
| 59 | width: 2.464 |
| 60 | }), el('path', { |
| 61 | d: 'M37.573 257.815a.493.493 0 00-.494.494v.994c0 .273.22.494.494.494h2.054v9.627h-2.054a.493.493 0 00-.494.494v.993c0 .274.22.495.494.495h3.542c.274 0 .494-.22.494-.495V258.31a.493.493 0 00-.494-.494h-3.542z' |
| 62 | }))); |
| 63 | } |
| 64 | |
| 65 | blocks.registerBlockType( 'shortcoder/shortcoder', { |
| 66 | title: 'Shortcoder', |
| 67 | icon: icon, |
| 68 | category: 'widgets', |
| 69 | attributes: { |
| 70 | text: { |
| 71 | type: 'string', |
| 72 | source: 'text' |
| 73 | }, |
| 74 | }, |
| 75 | supports: { |
| 76 | html: false, |
| 77 | customClassName: false, |
| 78 | className: false |
| 79 | }, |
| 80 | edit: wp.compose.withInstanceId( shortcoderBlock ), |
| 81 | save: function(props) { |
| 82 | return el( element.RawHTML, {}, props.attributes.text); |
| 83 | }, |
| 84 | }); |
| 85 | }( |
| 86 | window.wp.blocks, |
| 87 | window.wp.element |
| 88 | )); |