Advertisement
Guest User

Untitled

a guest
Sep 13th, 2019
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. wp.hooks.addFilter(
  2.     'blocks.registerBlockType',
  3.     'myPlugin/quoteButtonAttributes',
  4.     settings => {
  5.         if(settings.name === 'core/pullquote') {
  6.             settings.attributes = {
  7.                 ...settings.attributes,
  8.                 myPluginShowButton: {
  9.                     type: 'boolean',
  10.                     default: false,
  11.                 },
  12.                 myPluginButtonContent: {},
  13.             };
  14.         }
  15.         return settings;
  16.     }
  17. );
  18.  
  19. wp.hooks.addFilter(
  20.     'editor.BlockEdit',
  21.     'myPlugin/quoteButtonEdit',
  22.     wp.compose.createHigherOrderComponent(
  23.         BlockEdit => props => {
  24.             if(props.name === 'core/pullquote') {
  25.                 const {attributes, setAttributes} = props;
  26.                 const {myPluginShowButton, myPluginButtonContent} = attributes;
  27.                 return (
  28.                     <wp.element.Fragment>
  29.                         <BlockEdit {...props} />
  30.                         {myPluginShowButton && (
  31.                             <button>
  32.                                 <wp.blockEditor.RichText
  33.                                     tagName='div'
  34.                                     placeholder='Click here'
  35.                                     value={myPluginButtonContent}
  36.                                     onChange={value => setAttributes({myPluginButtonContent: value})}
  37.                                 />
  38.                             </button>
  39.                         )}
  40.                         <wp.blockEditor.InspectorControls>
  41.                             <wp.components.PanelBody title='Button options'>
  42.                                 <wp.components.ToggleControl
  43.                                     label='Show button'
  44.                                     checked={myPluginShowButton}
  45.                                     onChange={() => setAttributes({myPluginShowButton: !myPluginShowButton})}
  46.                                 />
  47.                             </wp.components.PanelBody>
  48.                         </wp.blockEditor.InspectorControls>
  49.                     </wp.element.Fragment>
  50.                 );
  51.             }
  52.             return <BlockEdit {...props} />;
  53.         },
  54.         'withMyPluginQuoteButtonEdit'
  55.     )
  56. );
  57.  
  58. wp.hooks.addFilter(
  59.     'blocks.getSaveElement',
  60.     'myPlugin/quoteButtonSave',
  61.     (element, block, attributes) => {
  62.         if(block.name === 'core/pullquote') {
  63.             const {myPluginShowButton, myPluginButtonContent} = attributes;
  64.             if(myPluginShowButton && myPluginButtonContent) {
  65.                 return (
  66.                     <wp.element.Fragment>
  67.                         {element}
  68.                         <button>
  69.                             <wp.blockEditor.RichText.Content value={myPluginButtonContent} />
  70.                         </button>
  71.                     </wp.element.Fragment>
  72.                 );
  73.             }
  74.         }
  75.         return element;
  76.     }
  77. );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement