integrating attributes
This commit is contained in:
@@ -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
|
||||||
|
|||||||
@@ -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>
|
||||||
|
|||||||
@@ -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')
|
|
||||||
|
|||||||
@@ -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')
|
|
||||||
|
|||||||
Reference in New Issue
Block a user