integrating attributes

This commit is contained in:
2025-12-18 10:57:50 +00:00
parent 5a3d5f148a
commit 007bc6cbac
4 changed files with 97 additions and 7 deletions

View File

@@ -8,9 +8,25 @@
"foreground": "#f58762" "foreground": "#f58762"
}, },
"description": "A wrapper to contain core blocks", "description": "A wrapper to contain core blocks",
"attributes": {
"container_width": {
"type": "string",
"default": 0
},
"alignment": {
"type": "string"
},
"padding_top": {
"type": "boolean",
"default": true
},
"padding_bottom": {
"type": "boolean",
"default": true
}
},
"supports": { "supports": {
"html": true, "html": true,
"align": ["wide", "full"],
"color": { "color": {
"background": true, "background": true,
"text": false "text": false

View File

@@ -1,17 +1,93 @@
// block.json's editorScript, loaded only in the block editor // block.json's editorScript, loaded only in the block editor
import { __ } from '@wordpress/i18n';
import { registerBlockType } from '@wordpress/blocks'; import { registerBlockType } from '@wordpress/blocks';
import { InnerBlocks, useBlockProps } from '@wordpress/block-editor';
import {
useBlockProps,
InnerBlocks,
InspectorControls,
BlockControls,
AlignmentToolbar,
} from '@wordpress/block-editor';
import {
Panel,
PanelBody,
PanelRow,
SelectControl,
ToggleControl,
} from '@wordpress/components';
import { useState, useEffect } from '@wordpress/element';
import apiFetch from '@wordpress/api-fetch';
import metadata from './block.json'; import metadata from './block.json';
import allowedBlocks from '../../../json/core-block-whitelist.json'; import allowedBlocks from '../../../json/core-block-whitelist.json';
registerBlockType(metadata.name, { registerBlockType(metadata.name, {
edit({ attributes, setAttributes }) { edit({ attributes, setAttributes }) {
const blockProps = useBlockProps(); const blockProps = useBlockProps();
const [ isLoading, setIsLoading ] = useState( true );
const {
container_width,
alignment,
padding_top,
padding_bottom,
} = attributes;
const [
containerWidthOptions, setContainerWidthOptions,
] = useState( [] );
useEffect( () => {
apiFetch( { path: '/badegg/v1/blocks/container_width' } )
.then( ( data ) => {
setContainerWidthOptions( data );
setIsLoading( false );
} )
.catch( () => {
setContainerWidthOptions( [] );
setIsLoading( false );
} );
}, [] );
console.log(attributes);
return ( return (
<div { ...blockProps }> <div { ...blockProps }>
<div className="container"> <BlockControls>
<AlignmentToolbar
value={ alignment }
onChange={(value) => setAttributes({alignment: value})}
/>
</BlockControls>
<InspectorControls>
<Panel>
<PanelBody title={ __("Settings", "badegg") }>
<SelectControl
label={ __("Container Width", "badegg") }
value={ container_width }
options={ containerWidthOptions }
onChange={ (value) => setAttributes({ container_width: value }) }
__next40pxDefaultSize={ true }
__nextHasNoMarginBottom={ true }
/>
<ToggleControl
label={ __('Top Padding', 'badegg') }
checked={ padding_top }
onChange={(value) => setAttributes({ padding_top: value }) }
__nextHasNoMarginBottom
/>
<ToggleControl
label={ __('Bottom Padding', 'badegg') }
checked={ padding_bottom }
onChange={(value) => setAttributes({ padding_bottom: value }) }
__nextHasNoMarginBottom
/>
</PanelBody>
</Panel>
</InspectorControls>
<div className={`container container-${ attributes.container_width } align-${ attributes.alignment }`}>
<InnerBlocks <InnerBlocks
allowedBlocks={ allowedBlocks } allowedBlocks={ allowedBlocks }
defaultBlock={ defaultBlock={
@@ -27,10 +103,10 @@ registerBlockType(metadata.name, {
</div> </div>
); );
}, },
save() { save({ attributes }) {
return ( return (
<div { ...useBlockProps.save() }> <div { ...useBlockProps.save() }>
<div className="container"> <div className={`container container-${attributes.container_width} align-${ attributes.alignment }`}>
<InnerBlocks.Content /> <InnerBlocks.Content />
</div> </div>
</div> </div>

View File

@@ -1,2 +1 @@
// block.json's script, loaded in block editor and front end // block.json's script, loaded in block editor and front end
console.log('loaded: resources/views/blocks/article/script.js')

View File

@@ -1,2 +1 @@
// block.json's viewScript, applied on front end only // block.json's viewScript, applied on front end only
console.log('loaded: resources/views/blocks/article/view.js')