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 :: Allowed Blocks in Nested Blocks Component Wordpress 
Javascript :: Register post meta of sidebar in wordpress 
Javascript :: Template literals in ES6 Syntax Concatenation 
Javascript :: editorGutter.modifiedBackground striped color 
Javascript :: currying in javascript mdn 
Javascript :: hover over class apply to subclass 
Javascript :: Async functions and execution order 
Javascript :: this ....object of. 
Javascript :: Storing Values With Assignment Operators 
Javascript :: visable in viewport 
Javascript :: itreating string js 
Javascript :: antd table access data from object //with dot 
Javascript :: how is react different from normal js 
Javascript :: shipengine connect 
Javascript :: convert an array to other array 
Javascript :: js tabbed data to array 
Javascript :: how to remove all Class(es) from a DOM, and then adds all Elements of an Array 
Javascript :: for loop shothand in js 
Javascript :: react Update a label when rate moves 
Javascript :: settimerout get throw out after refresh browser 
Javascript :: codeigniter 4 tooltip dynamic 
Javascript :: how to check if a string contains a specific word in javascript 
Javascript :: The behavior that Selection.addRange() merges existing Range and the specified Range was removed. 
Javascript :: Render raw html in response with Express 
Javascript :: how to add array of object in class in javascript 
Javascript :: Installation de react native maps bibliothèque 
Javascript :: get random id javascript 
Javascript :: angular 2 on data bound event equivalent 
Javascript :: pure-javascript-listen-to-input-value-change 
Javascript :: Snail array 
ADD CONTENT
Topic
Content
Source link
Name
7+1 =