admin管理员组文章数量:1134247
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 } = wppose;
const { Fragment } = wp.element;
const { BlockControls } = wp.blockEditor;
const { ToolbarGroup, ToolbarButton } = wpponents;
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?
本文标签: pluginsEditing a block to add a button to have a drop cap
版权声明:本文标题:plugins - Editing a block to add a button to have a drop cap 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736772572a1952161.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论