0

Currently, I have a code to edit the paragraph block and add a button to convert it into having a drop cap.

const { __ } = wp.i18n;

const enableToolbarButtonOnBlocks = ["core/paragraph"];

const { createHigherOrderComponent } = wp.compose;
const { Fragment } = wp.element;
const { BlockControls } = wp.blockEditor;
const { ToolbarGroup, ToolbarButton } = wp.components;

const setToolbarButtonAttribute = (settings, name) => {
    if (!enableToolbarButtonOnBlocks.includes(name)) {
        return settings;
    }

    return Object.assign({}, settings, {
        attributes: Object.assign({}, settings.attributes, {
            dropCapAttribute: { type: "string" },
        }),
    });
};
wp.hooks.addFilter(
    "blocks.registerBlockType",
    "custom-attributes/set-toolbar-button-attribute",
    setToolbarButtonAttribute
);

const withToolbarButton = createHigherOrderComponent((BlockEdit) => {
    return (props) => {
        if (!enableToolbarButtonOnBlocks.includes(props.name)) {
            return <BlockEdit {...props} />;
        }
        const { attributes, setAttributes } = props;
        const { dropCapAttribute } = attributes;
        
        return (
            <Fragment>
                <BlockControls group="block">
                    <ToolbarGroup>
                        <ToolbarButton
                            icon="editor-paragraph"
                            label={__("drop cap", "core-block-custom-attributes")}
                            isActive={dropCapAttribute === "true"}
                            onClick={() => {
                                if (dropCapAttribute === "true") {
                                    setAttributes({ dropCapAttribute: false });
                                } else {
                                    setAttributes({ dropCapAttribute: "true" });
                                }
                            }}
                        />
                    </ToolbarGroup>
                </BlockControls>
                <BlockEdit {...props} />
            </Fragment>
        );
    };
}, "withToolbarButton");

wp.hooks.addFilter(
    "editor.BlockEdit",
    "custom-attributes/with-toolbar-button",
    withToolbarButton
);

const withToolbarButtonProp = createHigherOrderComponent((BlockListBlock) => {
    return (props) => <BlockListBlock {...props} />;
}, "withToolbarButtonProp");

wp.hooks.addFilter(
    "editor.BlockListBlock",
    "custom-attributes/with-toolbar-button-prop",
    withToolbarButtonProp
);

const saveToolbarButtonAttribute = (extraProps, blockType, attributes) => {
    if (enableToolbarButtonOnBlocks.includes(blockType.name)) {
        const { dropCapAttribute } = attributes;
        if (dropCapAttribute && "true" === dropCapAttribute) {
            return {
                ...extraProps,
                "data-dropcap": "true",
            };
        }
    }

    return extraProps;
};
wp.hooks.addFilter(
    "blocks.getSaveContent.extraProps",
    "custom-attributes/save-toolbar-button-attribute",
    saveToolbarButtonAttribute
);

I have this code in the folder: myplugin/src/attributes/dropcap, I also have styles.scss in the same folder with in myplugin I have other blocks so I can run npm run start in the folder and it will build them in the build folder.

My question how can I get wp-scripts to include these attributes within my build process?

I'm currently adding the code to add the new block by wp_register_script and adding it with wp_enqueue_script.

Do I add the folder to the register_block_type?

3
  • 1
    WordPress already has built in dropcap support, if you declare in your theme.json that the paragraph block supports it all of your code can be eliminated
    – Tom J Nowell
    Commented Oct 18, 2023 at 17:07
  • For reference: developer.wordpress.org/block-editor/reference-guides/… Commented Oct 18, 2023 at 17:48
  • While WordPress might support it, we need to do it in a different way as we are using WP as a headless CMS and have to send different elements in a specific way.
    – Omar
    Commented Oct 18, 2023 at 18:39

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.