diff --git a/web/app/themes/badegg/resources/js/blocks/components/BlockSettings.jsx b/web/app/themes/badegg/resources/js/blocks/components/BlockSettings.jsx index 7ed5aa1..91a1c84 100644 --- a/web/app/themes/badegg/resources/js/blocks/components/BlockSettings.jsx +++ b/web/app/themes/badegg/resources/js/blocks/components/BlockSettings.jsx @@ -6,18 +6,228 @@ import { useSelect } from '@wordpress/data'; * Bundles the used for several blocks * * * @param {object} props - * @param {number} props.imageId The ID of the image to display. - * @param {string} props.size The size of the image to display. Defaults to 'full'. + * @param {number} props.attributes the data + * @param {string} props.setAttributes the state * @returns {*} React JSX */ -export default function BlockSettings(props) { +import { __ } from '@wordpress/i18n'; +import { useState, useEffect } from '@wordpress/element'; +import apiFetch from '@wordpress/api-fetch'; +import { + Panel, + PanelBody, + PanelRow, + SelectControl, + ToggleControl, + RangeControl, + ColorPalette, + Button, + Spinner, +} from '@wordpress/components'; + +import { + InspectorControls, + BlockControls, + AlignmentToolbar, + MediaUpload, + MediaUploadCheck, +} from '@wordpress/block-editor'; + +export default function BlockSettings({ attributes, setAttributes }) { + const [ configOptions, setConfigOptions ] = useState([]); + const [ isLoading, setIsLoading ] = useState(true); + + useEffect( () => { + let isMounted = true; + + apiFetch( { path: '/badegg/v1/blocks/config' } ) + .then( ( data ) => { + if ( isMounted ) { + setConfigOptions( data ); + setIsLoading( false ); + } + } ) + .catch( () => { + if ( isMounted ) { + setConfigOptions( null ); + setIsLoading( false ); + } + } ); + + return () => { + isMounted = false; + }; + }, [] ); + + if ( isLoading ) { + return ( + + + + + + + + ); + } + + if ( ! configOptions ) { + return null; + } + + const { + alignment, + container_width, + padding_top, + padding_bottom, + background_hex, + background_tint, + background_image, + background_opacity, + background_contrast, + background_fixed, + } = attributes; return ( - <> + <> + + setAttributes({alignment: value})} + /> + + + + + setAttributes({ container_width: value }) } + __next40pxDefaultSize={ true } + __nextHasNoMarginBottom={ true } + /> + setAttributes({ padding_top: value }) } + __nextHasNoMarginBottom + /> + setAttributes({ padding_bottom: value }) } + __nextHasNoMarginBottom + /> + + +

+ { __('Colour', 'badegg') } +

+ { + let slug, hex, selected = ''; + if(value) { + selected = configOptions.colours.find( + ( c ) => c.color === value + ); - - ) + hex = value; + } + + if(selected) { + slug = selected.slug; + } + + setAttributes( { + background_colour: slug, + background_hex: hex, + }); + + } } + /> + + { 'background_colour' in attributes && attributes.background_colour && ![0, '0', 'white', 'black'].includes(attributes.background_colour) ? ( + setAttributes({ background_tint: value }) } + __next40pxDefaultSize={ true } + __nextHasNoMarginBottom={ true } + /> + ) : null } + + { background_image != 0 && ( + <> + setAttributes({ background_contrast: value }) } + __nextHasNoMarginBottom + /> + setAttributes({ background_fixed: value }) } + __nextHasNoMarginBottom + /> + setAttributes({ background_opacity: value }) } + min={ 5 } + max={ 100 } + /> + + )} + + + + { + setAttributes({ + background_image: media.id, + background_url: media.url, + }); + }} + allowedTypes={ ['image'] } + value={ background_image } + render={ ({ open }) => ( + + )} + /> + + + { background_image != 0 && ( + + )} + + +
+
+
+ + ); } diff --git a/web/app/themes/badegg/resources/js/blocks/lib/classNames.js b/web/app/themes/badegg/resources/js/blocks/lib/classNames.js index 0dcb496..b9a589a 100644 --- a/web/app/themes/badegg/resources/js/blocks/lib/classNames.js +++ b/web/app/themes/badegg/resources/js/blocks/lib/classNames.js @@ -14,10 +14,8 @@ export function containerClassNames(attributes, bgProps) return classNames; } -export function sectionClassNames(attributes, defaultClasses, extraClasses = []) +export function sectionClassNames(attributes, defaultClasses = '', extraClasses = []) { - defaultClasses = defaultClasses.split(' '); - let classNames = [ 'section', ]; diff --git a/web/app/themes/badegg/resources/views/blocks/article/index.jsx b/web/app/themes/badegg/resources/views/blocks/article/index.jsx index 57e2427..ea6937e 100644 --- a/web/app/themes/badegg/resources/views/blocks/article/index.jsx +++ b/web/app/themes/badegg/resources/views/blocks/article/index.jsx @@ -1,229 +1,31 @@ // block.json's editorScript, loaded only in the block editor +import metadata from './block.json'; import { __ } from '@wordpress/i18n'; import { registerBlockType } from '@wordpress/blocks'; -import { useSelect } from '@wordpress/data'; import { useBlockProps, InnerBlocks, - InspectorControls, - BlockControls, - AlignmentToolbar, - MediaUpload, - MediaUploadCheck, } from '@wordpress/block-editor'; -import { - Panel, - PanelBody, - PanelRow, - SelectControl, - ToggleControl, - RangeControl, - ColorPalette, - Button, -} from '@wordpress/components'; - -// import { Image } from '@10up/block-components'; - -import { useState, useEffect } from '@wordpress/element'; -import metadata from './block.json'; import allowedBlocks from '../../../json/core-block-whitelist.json'; import { containerClassNames, sectionClassNames } from '../../../js/blocks/lib/classNames'; -import AttachmentImage from '../../../js/blocks/components/AttachmentImage'; import BackgroundImage from '../../../js/blocks/components/BackgroundImage'; - -import apiFetch from '@wordpress/api-fetch'; +import BlockSettings from '../../../js/blocks/components/BlockSettings'; registerBlockType(metadata.name, { edit({ attributes, setAttributes }) { const blockProps = useBlockProps(); - const [ isLoading, setIsLoading ] = useState( true ); - - const { - container_width, - alignment, - padding_top, - padding_bottom, - background_colour, - background_hex, - background_tint, - background_image, - background_url, - background_position, - background_opacity, - background_contrast, - background_fixed, - } = attributes; - - const [ - configOptions, setConfigOptions, - ] = useState( [] ); - - useEffect( () => { - apiFetch( { path: '/badegg/v1/blocks/config' } ) - .then( ( data ) => { - setConfigOptions( data ); - setIsLoading( false ); - } ) - .catch( () => { - setConfigOptions( [] ); - setIsLoading( false ); - } ); - }, [] ); - - blockProps.className = sectionClassNames( - attributes, - blockProps.className, - [ - 'wysiwyg' - ]).join(' '); - - // console.log(attributes); + blockProps.className = sectionClassNames(attributes, blockProps.className, [ 'wysiwyg' ]).join(' '); return (
- - setAttributes({alignment: value})} - /> - - - - - setAttributes({ container_width: value }) } - __next40pxDefaultSize={ true } - __nextHasNoMarginBottom={ true } - /> - setAttributes({ padding_top: value }) } - __nextHasNoMarginBottom - /> - setAttributes({ padding_bottom: value }) } - __nextHasNoMarginBottom - /> - - -

- { __('Colour', 'badegg') } -

- { - let slug, hex, selected = ''; + - if(value) { - selected = configOptions.colours.find( - ( c ) => c.color === value - ); - - hex = value; - } - - if(selected) { - slug = selected.slug; - } - - setAttributes( { - background_colour: slug, - background_hex: hex, - }); - - } } - /> - - { 'background_colour' in attributes && attributes.background_colour && ![0, '0', 'white', 'black'].includes(attributes.background_colour) ? ( - setAttributes({ background_tint: value }) } - __next40pxDefaultSize={ true } - __nextHasNoMarginBottom={ true } - /> - ) : null } - - { background_image != 0 && ( - <> - {/* */} - setAttributes({ background_contrast: value }) } - __nextHasNoMarginBottom - /> - setAttributes({ background_fixed: value }) } - __nextHasNoMarginBottom - /> - setAttributes({ background_opacity: value }) } - min={ 5 } - max={ 100 } - /> - - )} - - - - { - setAttributes({ - background_image: media.id, - background_url: media.url, - }); - }} - allowedTypes={ ['image'] } - value={ background_image } - render={ ({ open }) => ( - - )} - /> - - - { background_image != 0 && ( - - )} - - -
-
-
+
+ + +
) }