Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

Block Alignment Toolbar Using ES5 in Wordpress

( function ( blocks, blockEditor, element ) {
    var el = element.createElement;
    var RichText = blockEditor.RichText;
    var AlignmentToolbar = blockEditor.AlignmentToolbar;
    var BlockControls = blockEditor.BlockControls;
    var useBlockProps = blockEditor.useBlockProps;
 
    blocks.registerBlockType( 'softhunt/controls-example', {
        title: 'Controls',
        icon: 'universal-access-alt',
        category: 'design',
 
        attributes: {
            content: {
                type: 'array',
                source: 'children',
                selector: 'p',
            },
            alignment: {
                type: 'string',
                default: 'none',
            },
        },
        example: {
            attributes: {
                content: 'Hello World',
                alignment: 'right',
            },
        },
        edit: function ( props ) {
            var content = props.attributes.content;
            var alignment = props.attributes.alignment;
 
            function onChangeContent( newContent ) {
                props.setAttributes( { content: newContent } );
            }
 
            function onChangeAlignment( newAlignment ) {
                props.setAttributes( {
                    alignment:
                        newAlignment === undefined ? 'none' : newAlignment,
                } );
            }
 
            return el(
                'div',
                useBlockProps(),
                el(
                    BlockControls,
                    { key: 'controls' },
                    el( AlignmentToolbar, {
                        value: alignment,
                        onChange: onChangeAlignment,
                    } )
                ),
                el( RichText, {
                    key: 'richtext',
                    tagName: 'p',
                    style: { textAlign: alignment },
                    onChange: onChangeContent,
                    value: content,
                } )
            );
        },
 
        save: function ( props ) {
            var blockProps = useBlockProps.save();
 
            return el(
                'div',
                blockProps,
                el( RichText.Content, {
                    tagName: 'p',
                    className:
                        'softhunt-gutenberg-examples-align-' +
                        props.attributes.alignment,
                    value: props.attributes.content,
                } )
            );
        },
    } );
} )( window.wp.blocks, window.wp.blockEditor, window.wp.element );
Comment

Block Alignment Toolbar Using ESNext in Wordpress

import { registerBlockType } from '@wordpress/blocks';
 
import {
    useBlockProps,
    RichText,
    AlignmentToolbar,
    BlockControls,
} from '@wordpress/block-editor';
 
registerBlockType( 'softhunt/controls-example', {
    apiVersion: 2,
    title: 'Controls',
    icon: 'universal-access-alt',
    category: 'design',
    attributes: {
        content: {
            type: 'array',
            source: 'children',
            selector: 'p',
        },
        alignment: {
            type: 'string',
            default: 'none',
        },
    },
    example: {
        attributes: {
            content: 'Hello World',
            alignment: 'right',
        },
    },
    edit: ( { attributes, setAttributes } ) => {
        const onChangeContent = ( newContent ) => {
            setAttributes( { content: newContent } );
        };
 
        const onChangeAlignment = ( newAlignment ) => {
            setAttributes( {
                alignment: newAlignment === undefined ? 'none' : newAlignment,
            } );
        };
 
        return (
            <div { ...useBlockProps() }>
                {
                    <BlockControls>
                        <AlignmentToolbar
                            value={ attributes.alignment }
                            onChange={ onChangeAlignment }
                        />
                    </BlockControls>
                }
                <RichText
                    className={ attributes.className }
                    style={ { textAlign: attributes.alignment } }
                    tagName="p"
                    onChange={ onChangeContent }
                    value={ attributes.content }
                />
            </div>
        );
    },
    save: ( props ) => {
        const blockProps = useBlockProps.save();
 
        return (
            <div { ...blockProps }>
                <RichText.Content
                    className={ `softhunt-gutenberg-examples-align-${ props.attributes.alignment }` }
                    tagName="p"
                    value={ props.attributes.content }
                />
            </div>
        );
    },
} );
Comment

PREVIOUS NEXT
Code Example
Javascript :: Paginate array in JavaScript 
Javascript :: adding values for metaboxes in wordpress 
Javascript :: Multiline string in ES6 
Javascript :: how to change sender name in nodemailer 
Javascript :: JS get dropdown setting 
Javascript :: angular 8 input decorator Expected 2 arguments, but got 1. 
Javascript :: google.translate.TranslateElement part of page 
Javascript :: cypress contains regex 
Javascript :: url is not a constructor javascript 
Javascript :: fcus on element 
Javascript :: telegram web app js 
Javascript :: External javascript in React Native 
Javascript :: for loop display numbers 1 to 10 javascript 
Javascript :: Parametro angulara con ruta y recarga de componente 
Javascript :: for (var i = 0; i < 3; i++) { setTimeout(function() { console.log(i); }, 1000 + i); } 
Javascript :: array[-1] not working 
Javascript :: Javacript code that delays, based on Milliseconds 
Javascript :: javascript remove files name starts with 
Javascript :: react Alert when rate changes 
Javascript :: change the input feild name when the div contaoining that field is cloned using jquery 
Javascript :: mutiple if in express handlebars 
Javascript :: https://stackoverflow.com/questions/19380738/mongoose-nested-query-on-model-by-field-of-its-referenced-model 
Javascript :: find minimum length word in a phrase 
Javascript :: como acrescentar item no array js 
Javascript :: how to save js object to clipboard 
Javascript :: React.createElement pass props 
Javascript :: conditionally add property to JSON object javascript es6 
Javascript :: react native parent opcaity not affecting text 
Javascript :: js convert charcode to char 
Javascript :: enzyme to json 
ADD CONTENT
Topic
Content
Source link
Name
6+6 =