Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Using Blocks Outside The Editor

Using Blocks Outside The Editor

Tom J Nowell

June 22, 2019
Tweet

More Decks by Tom J Nowell

Other Decks in Technology

Transcript

  1. @tarendai https://tomjn.com
    Using Blocks Outside
    The Editor

    View Slide

  2. @tarendai https://tomjn.com

    View Slide

  3. @tarendai https://tomjn.com
    Frontenberg
    https://frontenberg.tomjn.com

    View Slide

  4. @tarendai https://tomjn.com

    View Slide

  5. @tarendai https://tomjn.com
    Frontenberg
    Is not a frontend editor!

    View Slide

  6. @tarendai https://tomjn.com
    Blocks are the
    Interface

    View Slide

  7. @tarendai https://tomjn.com

    View Slide

  8. @tarendai https://tomjn.com
    wp.blocks.registerBlockType( 'tomjn/helloworld', {
    title: 'Hello World',
    icon: 'universal-access-alt',
    category: 'layout',
    edit() {
    return el("p", "Hello World, step 1 (from the editor).");
    },
    save() {
    return el("p", "Hello World, step 1 (from the frontend)");
    },
    } );

    View Slide

  9. @tarendai https://tomjn.com

    View Slide

  10. @tarendai https://tomjn.com

    View Slide

  11. @tarendai https://tomjn.com
    Building a Block
    Editor Component

    View Slide

  12. @tarendai https://tomjn.com
    function Editor({ startingContent, inputName }) {
    return

    ....
    ;
    }

    View Slide

  13. @tarendai https://tomjn.com
    function Editor({ startingContent, inputName }) {
    const [ blocks, update ] = React.useState( parse(startingContent) );
    return










    ;
    }

    View Slide

  14. @tarendai https://tomjn.com
    function Editor({ startingContent, inputName }) {
    const [ blocks, update ] = React.useState( parse(startingContent) );
    return










    ;
    }

    View Slide

  15. @tarendai https://tomjn.com
    function Editor({ startingContent, inputName }) {
    const [ blocks, update ] = React.useState( parse(startingContent) );
    return










    ;
    }

    View Slide

  16. @tarendai https://tomjn.com
    function Editor({ startingContent, inputName }) {
    const [ blocks, update ] = React.useState( parse(startingContent) );
    return










    ;
    }

    View Slide

  17. @tarendai https://tomjn.com
    function Editor({ startingContent, inputName }) {
    const [ blocks, update ] = React.useState( parse(startingContent) );
    return










    ;
    }

    View Slide

  18. @tarendai https://tomjn.com
    function Editor({ startingContent, inputName }) {
    const [ blocks, update ] = React.useState( parse(startingContent) );
    return










    ;
    }

    View Slide

  19. @tarendai https://tomjn.com

    View Slide

  20. @tarendai https://tomjn.com
    Building a Generic
    JS Function

    View Slide

  21. @tarendai https://tomjn.com
    tomjn_wp_editor(
    'The ID of the tag',
    'The desired input name',
    'Starting content'
    );

    View Slide

  22. @tarendai https://tomjn.com

    View Slide

  23. @tarendai https://tomjn.com
    window.tomjn_wp_editor = function( id, inputName, content ) {
    const element = document.createElement( 'div' );
    element.setAttribute( 'class',
    'tomjn_editor_container block-editor gutenberg__editor' +
    'block-editor__container'
    );
    element.setAttribute( 'id', id );
    document.getElementById( id ).replaceWith(element);
    ReactDOM.render(
    ,
    element
    );
    }

    View Slide

  24. @tarendai https://tomjn.com
    window.tomjn_wp_editor = function( id, inputName, content ) {
    const element = document.createElement( 'div' );
    element.setAttribute( 'class',
    'tomjn_editor_container block-editor gutenberg__editor' +
    'block-editor__container'
    );
    element.setAttribute( 'id', id );
    document.getElementById( id ).replaceWith(element);
    ...
    }

    View Slide

  25. @tarendai https://tomjn.com
    ...
    ReactDOM.render(
    ,
    element
    );
    }

    View Slide

  26. @tarendai https://tomjn.com
    Commenting

    View Slide

  27. @tarendai https://tomjn.com
    var allowedBlocks = [
    'core/paragraph',
    'core/image',
    'core/html',
    'core/freeform'
    ];
    wp.blocks.getBlockTypes().forEach( function( blockType ) {
    if ( allowedBlocks.indexOf( blockType.name ) === -1 ) {
    wp.blocks.unregisterBlockType( blockType.name );
    }
    });

    View Slide

  28. @tarendai https://tomjn.com
    Building a Generic
    PHP function

    View Slide

  29. @tarendai https://tomjn.com

    View Slide

  30. @tarendai https://tomjn.com
    wp_editor( $content, $editor_id, [] );
    ?>

    View Slide

  31. @tarendai https://tomjn.com
    wp_block_editor($content, $name);
    ?>

    View Slide

  32. @tarendai https://tomjn.com
    function wp_block_editor( $content, $name ) {
    ?>

    <br/>document.addEventListener('DOMContentLoaded', function(event) {<br/>tomjn_wp_editor(<br/>'<?php echo esc_js( $name ); ?>',<br/>'<?php echo esc_js( $name ); ?>',<br/>'<?php echo esc_js( $content ); ?>'<br/>);<br/>});<br/>
    }

    View Slide

  33. @tarendai https://tomjn.com
    Supporting PHP Code

    View Slide

  34. @tarendai https://tomjn.com
    1. setup the core data layer
    2. enqueue styles and scripts

    View Slide

  35. @tarendai https://tomjn.com
    $wp_scripts->registered['wp-data']->extra['after'] = array();
    wp_add_inline_script(
    'wp-data',
    implode( "\n", [
    '( function() {',
    ' var userId = ' . get_current_user_ID() . ';',
    ' var storageKey = "WP_DATA_USER_" + userId;',
    ' var persistence = wp.data.plugins.persistence',
    ' wp.data.use( persistence, { storageKey: storageKey } );',
    ' persistence.__unstableMigrate({ storageKey: storageKey });',
    ' wp.data.use( wp.data.plugins.controls );',
    '} )();',
    ])
    );

    View Slide

  36. @tarendai https://tomjn.com
    wp_enqueue_script('wp-editor');
    wp_enqueue_script('wp-core-data');
    wp_enqueue_script('wp-data');
    wp_enqueue_script('wp-block-library');
    wp_enqueue_script('wp-format-library');
    wp_enqueue_style('media');
    wp_enqueue_style('l10n');
    wp_enqueue_style('buttons');
    wp_enqueue_style('wp-editor');

    View Slide

  37. @tarendai https://tomjn.com
    I'm Tom J Nowell
    @tarendai
    https://tomjn.com
    WordPress VIP

    View Slide

  38. @tarendai https://tomjn.com
    https://wpvip.com/careers

    View Slide

  39. @tarendai https://tomjn.com

    View Slide

  40. @tarendai https://tomjn.com

    View Slide