2022-03-29 13:48:59 +00:00
|
|
|
/* eslint-disable jsdoc/check-alignment */
|
2021-11-19 11:47:48 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2023-04-14 07:48:55 +00:00
|
|
|
import {
|
|
|
|
useBlockProps,
|
|
|
|
InnerBlocks,
|
|
|
|
InspectorControls,
|
|
|
|
} from '@wordpress/block-editor';
|
2021-12-07 14:07:53 +00:00
|
|
|
import { EditorProvider } from '@woocommerce/base-context';
|
|
|
|
import type { TemplateArray } from '@wordpress/blocks';
|
2022-10-28 15:30:51 +00:00
|
|
|
import { useEffect } from '@wordpress/element';
|
2023-04-25 16:54:35 +00:00
|
|
|
import type { FocusEvent, ReactElement } from 'react';
|
2023-04-14 07:48:55 +00:00
|
|
|
import { __ } from '@wordpress/i18n';
|
|
|
|
import {
|
|
|
|
PanelBody,
|
|
|
|
// eslint-disable-next-line @wordpress/no-unsafe-wp-apis
|
|
|
|
__experimentalUnitControl as UnitControl,
|
|
|
|
} from '@wordpress/components';
|
2021-11-19 11:47:48 +00:00
|
|
|
|
2021-12-07 14:07:53 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2022-12-22 14:35:24 +00:00
|
|
|
import { useForcedLayout } from '../../cart-checkout-shared';
|
2022-03-29 13:48:59 +00:00
|
|
|
import { MiniCartInnerBlocksStyle } from './inner-blocks-style';
|
2021-12-20 07:57:55 +00:00
|
|
|
import './editor.scss';
|
2023-04-14 07:48:55 +00:00
|
|
|
import { attributes as defaultAttributes } from './attributes';
|
2021-12-07 14:07:53 +00:00
|
|
|
|
|
|
|
// Array of allowed block names.
|
|
|
|
const ALLOWED_BLOCKS = [
|
|
|
|
'woocommerce/filled-mini-cart-contents-block',
|
|
|
|
'woocommerce/empty-mini-cart-contents-block',
|
|
|
|
];
|
2023-04-25 16:54:35 +00:00
|
|
|
const MIN_WIDTH = 300;
|
2021-12-14 00:46:10 +00:00
|
|
|
|
2021-12-07 14:07:53 +00:00
|
|
|
interface Props {
|
|
|
|
clientId: string;
|
2023-04-14 07:48:55 +00:00
|
|
|
attributes: Record< string, unknown >;
|
|
|
|
setAttributes: ( attributes: Record< string, unknown > ) => void;
|
2021-12-07 14:07:53 +00:00
|
|
|
}
|
|
|
|
|
2023-04-14 07:48:55 +00:00
|
|
|
const Edit = ( {
|
|
|
|
clientId,
|
|
|
|
attributes,
|
|
|
|
setAttributes,
|
|
|
|
}: Props ): ReactElement => {
|
|
|
|
const { currentView, width } = attributes;
|
|
|
|
|
2023-07-14 14:12:40 +00:00
|
|
|
const blockProps = useBlockProps();
|
2021-11-19 11:47:48 +00:00
|
|
|
|
2021-12-07 14:07:53 +00:00
|
|
|
const defaultTemplate = [
|
|
|
|
[ 'woocommerce/filled-mini-cart-contents-block', {}, [] ],
|
|
|
|
[ 'woocommerce/empty-mini-cart-contents-block', {}, [] ],
|
|
|
|
] as TemplateArray;
|
|
|
|
|
|
|
|
useForcedLayout( {
|
|
|
|
clientId,
|
|
|
|
registeredBlocks: ALLOWED_BLOCKS,
|
|
|
|
defaultTemplate,
|
|
|
|
} );
|
|
|
|
|
2022-10-28 15:30:51 +00:00
|
|
|
/**
|
|
|
|
* This is a workaround for the Site Editor to set the correct
|
2023-05-11 12:52:03 +00:00
|
|
|
* background color of the Mini-Cart Contents block base on
|
2022-10-28 15:30:51 +00:00
|
|
|
* the main background color set by the theme.
|
|
|
|
*/
|
|
|
|
useEffect( () => {
|
|
|
|
const canvasEl = document.querySelector(
|
|
|
|
'.edit-site-visual-editor__editor-canvas'
|
|
|
|
);
|
|
|
|
if ( ! ( canvasEl instanceof HTMLIFrameElement ) ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const canvas =
|
|
|
|
canvasEl.contentDocument || canvasEl.contentWindow?.document;
|
|
|
|
if ( ! canvas ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ( canvas.getElementById( 'mini-cart-contents-background-color' ) ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const styles = canvas.querySelectorAll( 'style' );
|
|
|
|
const [ cssRule ] = Array.from( styles )
|
|
|
|
.map( ( style ) => Array.from( style.sheet?.cssRules || [] ) )
|
|
|
|
.flatMap( ( style ) => style )
|
|
|
|
.filter( Boolean )
|
|
|
|
.filter(
|
|
|
|
( rule ) =>
|
|
|
|
rule.selectorText === '.editor-styles-wrapper' &&
|
|
|
|
rule.style.backgroundColor
|
|
|
|
);
|
|
|
|
if ( ! cssRule ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const backgroundColor = cssRule.style.backgroundColor;
|
|
|
|
if ( ! backgroundColor ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const style = document.createElement( 'style' );
|
|
|
|
style.id = 'mini-cart-contents-background-color';
|
|
|
|
style.appendChild(
|
|
|
|
document.createTextNode(
|
|
|
|
`:where(.wp-block-woocommerce-mini-cart-contents) {
|
|
|
|
background-color: ${ backgroundColor };
|
|
|
|
}`
|
|
|
|
)
|
|
|
|
);
|
|
|
|
const body = canvas.querySelector( '.editor-styles-wrapper' );
|
|
|
|
if ( ! body ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
body.appendChild( style );
|
|
|
|
}, [] );
|
|
|
|
|
2021-11-19 11:47:48 +00:00
|
|
|
return (
|
2023-04-14 08:44:09 +00:00
|
|
|
<>
|
2024-05-29 14:50:09 +00:00
|
|
|
<InspectorControls key="inspector">
|
|
|
|
<PanelBody
|
|
|
|
title={ __( 'Dimensions', 'woocommerce' ) }
|
|
|
|
initialOpen
|
|
|
|
>
|
|
|
|
<UnitControl
|
|
|
|
onChange={ ( value ) => {
|
|
|
|
setAttributes( { width: value } );
|
|
|
|
} }
|
|
|
|
onBlur={ ( e: FocusEvent< HTMLInputElement > ) => {
|
|
|
|
if ( e.target.value === '' ) {
|
|
|
|
setAttributes( {
|
|
|
|
width: defaultAttributes.width.default,
|
|
|
|
} );
|
|
|
|
} else if ( Number( e.target.value ) < MIN_WIDTH ) {
|
|
|
|
setAttributes( {
|
|
|
|
width: MIN_WIDTH + 'px',
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
} }
|
|
|
|
value={ width }
|
|
|
|
units={ [
|
|
|
|
{
|
|
|
|
value: 'px',
|
|
|
|
label: 'px',
|
|
|
|
default: defaultAttributes.width.default,
|
|
|
|
},
|
|
|
|
] }
|
|
|
|
/>
|
|
|
|
</PanelBody>
|
|
|
|
</InspectorControls>
|
|
|
|
|
2023-04-14 08:44:09 +00:00
|
|
|
<div
|
|
|
|
className="wc-block-components-drawer__screen-overlay"
|
|
|
|
aria-hidden="true"
|
|
|
|
></div>
|
|
|
|
<div className="wc-block-editor-mini-cart-contents__wrapper">
|
|
|
|
<div { ...blockProps }>
|
|
|
|
<EditorProvider currentView={ currentView }>
|
|
|
|
<InnerBlocks
|
|
|
|
allowedBlocks={ ALLOWED_BLOCKS }
|
|
|
|
template={ defaultTemplate }
|
|
|
|
templateLock={ false }
|
|
|
|
/>
|
|
|
|
</EditorProvider>
|
|
|
|
<MiniCartInnerBlocksStyle style={ blockProps.style } />
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</>
|
2021-11-19 11:47:48 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Edit;
|
2021-12-07 14:07:53 +00:00
|
|
|
|
|
|
|
export const Save = (): JSX.Element => {
|
|
|
|
return (
|
|
|
|
<div { ...useBlockProps.save() }>
|
|
|
|
<InnerBlocks.Content />
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|