Laravel

How to Create a Laraberg Custom block?

Share your learning

Laraberg is a Laravel package to use wordpress gutenberg editor with Laravel Framework. Today we are going to learn how we can create a laraberg custom block.

So, let’s dive into the code to initialize the laraberg.

Initialize the Laraberg

document.addEventListener('DOMContentLoaded', function () {

    (function () {
        if (typeof Laraberg == 'undefined') {
            console.warning('Laraberg not found!');
            return;
        }

        registerPostTitleBlock();

        Laraberg.init('post-editor', {
            mediaUpload: mediaUploaded
        });
})

As you can see, I have used mediaUpload while init the laraberg for this check my media upload related article.

Register a custom block to the Laraberg

After init the laraberg, we need to create the function for the custom block like given below.

function registerPostTitleBlock() {

    (function (blocks, blockEditor, element, components) {

        var el = element.createElement;
        var RichText = blockEditor.RichText;
        var useBlockProps = blockEditor.useBlockProps;
        var InspectorControls = blockEditor.InspectorControls;

        const icon = el("svg", {
            viewBox: "0 0 24 24",
            xmlns: "http://www.w3.org/2000/svg"
        }, el("path", {
            d: "M6.2 5.2v13.4l5.8-4.8 5.8 4.8V5.2z"
        }));

        const block = blocks.registerBlockType('sbt-block/post-title', {
            apiVersion: 2,
            title: 'Post title',
            icon: icon,
            category: 'text',
            attributes: {
                content: {
                    type: 'string',
                    selector: 'h1.post-title',
                    source: 'text',
                }
            },
            supports: {
                customClassName: false,
            },
            edit: function (props) {
                var blockProps = useBlockProps();

                return [
                    el(
                        InspectorControls,
                        { key: 'l8' }
                    ),
                    el(RichText, {
                        tagName: 'h1',
                        key: 'lx4',
                        value: props.attributes.content,
                        preserveWhiteSpace: true,
                        __unstablePastePlainText: true,
                        onChange: content => props.setAttributes({
                            content: content
                        }),
                        placeholder: "Enter title..",
                    })
                ]
            },
            save: function (props) {
                var blockProps = useBlockProps.save();

                return el(RichText.Content,
                    Object.assign(blockProps, {
                        tagName: 'h1',
                        className: 'post-title',
                        value: props.attributes.content
                    }),
                );
            },
        })

    })(Laraberg.wordpress.blocks,
        Laraberg.wordpress.blockEditor,
        Laraberg.wordpress.element,
        Laraberg.wordpress.components);
}

That’s it. It’s simple to create a custom block in laraberg.

Hope you find this article useful.

Satpal

Recent Posts

How to Switch PHP Versions in XAMPP Easily: Managing Multiple PHP Versions on Ubuntu

Today we are going to learn about managing multiple PHP versions on ubuntu with xampp.…

1 year ago

How to Use Coding to Improve Your Website’s SEO Ranking?

Let's understand about how to use coding to improve your website's SEO. In today’s computerized…

1 year ago

Most Important Linux Commands for Web Developers

Let's understand the most important linux commands for web developers. Linux, as an open-source and…

1 year ago

Top 75+ Laravel Interview Questions Asked by Top MNCs

Today we are going to discuss top 75+ Laravel interview questions asked by top MNCs.Laravel,…

1 year ago

Mailtrap Integration for Email Testing with Laravel 10

Today we will discuss about the Mailtrap integration with laravel 10 .Sending and receiving emails…

1 year ago

Firebase Cloud Messaging (FCM) with Ionic 6: Push Notifications

Today we are going to integrate FCM (Firebase Cloud Messaging) push notifications with ionic application.Firebase…

1 year ago