* update locks to attributes instead

* remove lock from support attribute
This commit is contained in:
Seghir Nadir 2021-08-16 11:20:27 +01:00 committed by GitHub
parent 70ec1f8b31
commit 5a0840506e
23 changed files with 136 additions and 80 deletions

View File

@ -16,21 +16,24 @@ export interface FormStepBlockProps {
setAttributes: ( attributes: Record< string, unknown > ) => void; setAttributes: ( attributes: Record< string, unknown > ) => void;
className?: string; className?: string;
children?: React.ReactNode; children?: React.ReactNode;
lock?: { move: boolean; remove: boolean };
} }
/** /**
* Form Step Block for use in the editor. * Form Step Block for use in the editor.
*/ */
export const FormStepBlock = ( { export const FormStepBlock = ( {
attributes: { title = '', description = '', showStepNumber = true }, attributes,
setAttributes, setAttributes,
className = '', className = '',
children, children,
}: FormStepBlockProps ): JSX.Element => { }: FormStepBlockProps ): JSX.Element => {
const { title = '', description = '', showStepNumber = true } = attributes;
const blockProps = useBlockPropsWithLocking( { const blockProps = useBlockPropsWithLocking( {
className: classnames( 'wc-block-components-checkout-step', className, { className: classnames( 'wc-block-components-checkout-step', className, {
'wc-block-components-checkout-step--with-step-number': showStepNumber, 'wc-block-components-checkout-step--with-step-number': showStepNumber,
} ), } ),
attributes,
} ); } );
return ( return (

View File

@ -17,12 +17,11 @@ import {
store as blockEditorStore, store as blockEditorStore,
} from '@wordpress/block-editor'; } from '@wordpress/block-editor';
import { isTextField } from '@wordpress/dom'; import { isTextField } from '@wordpress/dom';
import { store as blocksStore } from '@wordpress/blocks';
import { useSelect, subscribe, select as _select } from '@wordpress/data'; import { useSelect, subscribe, select as _select } from '@wordpress/data';
import { useEffect, useRef } from '@wordpress/element'; import { useEffect, useRef } from '@wordpress/element';
import { MutableRefObject } from 'react'; import { MutableRefObject } from 'react';
import { BACKSPACE, DELETE } from '@wordpress/keycodes'; import { BACKSPACE, DELETE } from '@wordpress/keycodes';
import { hasFilter } from '@wordpress/hooks';
/** /**
* Toggle class on body. * Toggle class on body.
* *
@ -47,6 +46,7 @@ const toggleBodyClass = ( className: string, add = true ) => {
* We use a component so we can react to changes in the store. * We use a component so we can react to changes in the store.
*/ */
export const addClassToBody = (): void => { export const addClassToBody = (): void => {
if ( ! hasFilter( 'blocks.registerBlockType', 'core/lock/addAttribute' ) ) {
subscribe( () => { subscribe( () => {
const blockEditorSelect = _select( blockEditorStore ); const blockEditorSelect = _select( blockEditorStore );
@ -60,22 +60,17 @@ export const addClassToBody = (): void => {
return; return;
} }
const blockSelect = _select( blocksStore );
const selectedBlockType = blockSelect.getBlockType(
selectedBlock.name
);
toggleBodyClass( toggleBodyClass(
'wc-lock-selected-block--remove', 'wc-lock-selected-block--remove',
!! selectedBlockType?.supports?.lock?.remove !! selectedBlock?.attributes?.lock?.remove
); );
toggleBodyClass( toggleBodyClass(
'wc-lock-selected-block--move', 'wc-lock-selected-block--move',
!! selectedBlockType?.supports?.lock?.move !! selectedBlock?.attributes?.lock?.move
); );
} ); } );
}
}; };
/** /**
@ -87,19 +82,22 @@ export const addClassToBody = (): void => {
const useLockBlock = ( { const useLockBlock = ( {
clientId, clientId,
ref, ref,
type, attributes,
}: { }: {
clientId: string; clientId: string;
ref: MutableRefObject< Element >; ref: MutableRefObject< Element | undefined >;
type: string; attributes: Record< string, unknown >;
} ): void => { } ): void => {
const { isSelected, blockType } = useSelect( const lockInCore = hasFilter(
'blocks.registerBlockType',
'core/lock/addAttribute'
);
const { isSelected } = useSelect(
( select ) => { ( select ) => {
return { return {
isSelected: select( blockEditorStore ).isBlockSelected( isSelected: select( blockEditorStore ).isBlockSelected(
clientId clientId
), ),
blockType: select( blocksStore ).getBlockType( type ),
}; };
}, },
[ clientId ] [ clientId ]
@ -108,7 +106,7 @@ const useLockBlock = ( {
const node = ref.current; const node = ref.current;
return useEffect( () => { return useEffect( () => {
if ( ! isSelected || ! node ) { if ( ! isSelected || ! node || lockInCore ) {
return; return;
} }
function onKeyDown( event: KeyboardEvent ) { function onKeyDown( event: KeyboardEvent ) {
@ -120,9 +118,8 @@ const useLockBlock = ( {
if ( target !== node || isTextField( target ) ) { if ( target !== node || isTextField( target ) ) {
return; return;
} }
// Prevent the keyboard event from propogating if it supports locking. // Prevent the keyboard event from propogating if it supports locking.
if ( blockType?.supports?.lock?.remove ) { if ( attributes?.lock?.remove ) {
event.preventDefault(); event.preventDefault();
event.stopPropagation(); event.stopPropagation();
} }
@ -133,20 +130,21 @@ const useLockBlock = ( {
return () => { return () => {
node.removeEventListener( 'keydown', onKeyDown, true ); node.removeEventListener( 'keydown', onKeyDown, true );
}; };
}, [ node, isSelected, blockType ] ); }, [ node, isSelected, lockInCore, attributes ] );
}; };
/** /**
* This hook is a light wrapper to useBlockProps, it wraps that hook plus useLockBlock to pass data between them. * This hook is a light wrapper to useBlockProps, it wraps that hook plus useLockBlock to pass data between them.
*/ */
export const useBlockPropsWithLocking = ( export const useBlockPropsWithLocking = (
props?: Record< string, unknown > = {} props: Record< string, unknown > = {}
): Record< string, unknown > => { ): Record< string, unknown > => {
const ref = useRef< Element >(); const ref = useRef< Element >();
const { attributes } = props;
const blockProps = useBlockProps( { ref, ...props } ); const blockProps = useBlockProps( { ref, ...props } );
useLockBlock( { useLockBlock( {
ref, ref,
type: blockProps[ 'data-type' ], attributes,
clientId: blockProps[ 'data-block' ], clientId: blockProps[ 'data-block' ],
} ); } );
return blockProps; return blockProps;

View File

@ -7,4 +7,11 @@ export default {
type: 'boolean', type: 'boolean',
default: true, default: true,
}, },
lock: {
type: 'object',
default: {
move: true,
remove: true,
},
},
}; };

View File

@ -23,7 +23,7 @@ export const Edit = ( {
}; };
setAttributes: ( attributes: Record< string, unknown > ) => void; setAttributes: ( attributes: Record< string, unknown > ) => void;
} ): JSX.Element => { } ): JSX.Element => {
const blockProps = useBlockPropsWithLocking(); const blockProps = useBlockPropsWithLocking( { attributes } );
const { cartPageId = 0, showReturnToCart = true } = attributes; const { cartPageId = 0, showReturnToCart = true } = attributes;
const { current: savedCartPageId } = useRef( cartPageId ); const { current: savedCartPageId } = useRef( cartPageId );
const currentPostId = useSelect( const currentPostId = useSelect(

View File

@ -23,10 +23,6 @@ registerFeaturePluginBlockType( 'woocommerce/checkout-actions-block', {
multiple: false, multiple: false,
reusable: false, reusable: false,
inserter: false, inserter: false,
lock: {
remove: true,
move: true,
},
}, },
parent: [ 'woocommerce/checkout-fields-block' ], parent: [ 'woocommerce/checkout-fields-block' ],
attributes, attributes,

View File

@ -16,4 +16,11 @@ export default {
'woo-gutenberg-products-block' 'woo-gutenberg-products-block'
), ),
} ), } ),
lock: {
type: 'object',
default: {
move: true,
remove: true,
},
},
}; };

View File

@ -28,10 +28,6 @@ registerFeaturePluginBlockType( 'woocommerce/checkout-billing-address-block', {
multiple: false, multiple: false,
reusable: false, reusable: false,
inserter: false, inserter: false,
lock: {
remove: true,
move: true,
},
}, },
parent: [ 'woocommerce/checkout-fields-block' ], parent: [ 'woocommerce/checkout-fields-block' ],
attributes, attributes,

View File

@ -19,4 +19,11 @@ export default {
'woo-gutenberg-products-block' 'woo-gutenberg-products-block'
), ),
} ), } ),
lock: {
type: 'object',
default: {
remove: true,
move: true,
},
},
}; };

View File

@ -30,10 +30,6 @@ registerFeaturePluginBlockType(
multiple: false, multiple: false,
reusable: false, reusable: false,
inserter: false, inserter: false,
lock: {
remove: true,
move: true,
},
}, },
parent: [ 'woocommerce/checkout-fields-block' ], parent: [ 'woocommerce/checkout-fields-block' ],
attributes, attributes,

View File

@ -10,8 +10,17 @@ import Block from './block';
import './editor.scss'; import './editor.scss';
import { useBlockPropsWithLocking } from '../../hacks'; import { useBlockPropsWithLocking } from '../../hacks';
export const Edit = (): JSX.Element => { export const Edit = ( {
const blockProps = useBlockPropsWithLocking(); attributes,
}: {
attributes: {
lock: {
move: boolean;
remove: boolean;
};
};
} ): JSX.Element => {
const blockProps = useBlockPropsWithLocking( { attributes } );
return ( return (
<div { ...blockProps }> <div { ...blockProps }>

View File

@ -27,13 +27,17 @@ registerFeaturePluginBlockType( 'woocommerce/checkout-express-payment-block', {
multiple: false, multiple: false,
reusable: false, reusable: false,
inserter: false, inserter: false,
},
parent: [ 'woocommerce/checkout-fields-block' ],
attributes: {
lock: { lock: {
type: 'object',
default: {
remove: true, remove: true,
move: true, move: true,
}, },
}, },
parent: [ 'woocommerce/checkout-fields-block' ], },
attributes: {},
apiVersion: 2, apiVersion: 2,
edit: Edit, edit: Edit,
save: Save, save: Save,

View File

@ -11,8 +11,17 @@ import Block from './block';
import './editor.scss'; import './editor.scss';
import { useBlockPropsWithLocking } from '../../hacks'; import { useBlockPropsWithLocking } from '../../hacks';
export const Edit = (): JSX.Element => { export const Edit = ( {
const blockProps = useBlockPropsWithLocking(); attributes,
}: {
attributes: {
lock: {
move: boolean;
remove: boolean;
};
};
} ): JSX.Element => {
const blockProps = useBlockPropsWithLocking( { attributes } );
return ( return (
<div { ...blockProps }> <div { ...blockProps }>
<Disabled> <Disabled>

View File

@ -28,7 +28,15 @@ registerFeaturePluginBlockType( 'woocommerce/checkout-order-note-block', {
reusable: false, reusable: false,
}, },
parent: [ 'woocommerce/checkout-fields-block' ], parent: [ 'woocommerce/checkout-fields-block' ],
attributes: {}, attributes: {
lock: {
type: 'object',
default: {
move: true,
remove: true,
},
},
},
apiVersion: 2, apiVersion: 2,
edit: Edit, edit: Edit,
save: Save, save: Save,

View File

@ -8,4 +8,11 @@ export default {
type: 'boolean', type: 'boolean',
default: getSetting( 'displayCartPricesIncludingTax', false ), default: getSetting( 'displayCartPricesIncludingTax', false ),
}, },
lock: {
type: 'object',
default: {
move: true,
remove: true,
},
},
}; };

View File

@ -18,10 +18,14 @@ export const Edit = ( {
}: { }: {
attributes: { attributes: {
showRateAfterTaxName: boolean; showRateAfterTaxName: boolean;
lock: {
move: boolean;
remove: boolean;
};
}; };
setAttributes: ( attributes: Record< string, unknown > ) => void; setAttributes: ( attributes: Record< string, unknown > ) => void;
} ): JSX.Element => { } ): JSX.Element => {
const blockProps = useBlockPropsWithLocking(); const blockProps = useBlockPropsWithLocking( { attributes } );
const taxesEnabled = getSetting( 'taxesEnabled' ) as boolean; const taxesEnabled = getSetting( 'taxesEnabled' ) as boolean;
const displayItemizedTaxes = getSetting( const displayItemizedTaxes = getSetting(
'displayItemizedTaxes', 'displayItemizedTaxes',

View File

@ -23,10 +23,6 @@ registerFeaturePluginBlockType( 'woocommerce/checkout-order-summary-block', {
multiple: false, multiple: false,
reusable: false, reusable: false,
inserter: false, inserter: false,
lock: {
remove: true,
move: true,
},
}, },
parent: [ 'woocommerce/checkout-totals-block' ], parent: [ 'woocommerce/checkout-totals-block' ],
attributes, attributes,

View File

@ -16,4 +16,11 @@ export default {
'woo-gutenberg-products-block' 'woo-gutenberg-products-block'
), ),
} ), } ),
lock: {
type: 'object',
default: {
move: true,
remove: true,
},
},
}; };

View File

@ -28,10 +28,6 @@ registerFeaturePluginBlockType( 'woocommerce/checkout-payment-block', {
multiple: false, multiple: false,
reusable: false, reusable: false,
inserter: false, inserter: false,
lock: {
remove: true,
move: true,
},
}, },
parent: [ 'woocommerce/checkout-fields-block' ], parent: [ 'woocommerce/checkout-fields-block' ],
attributes, attributes,

View File

@ -16,4 +16,11 @@ export default {
'woo-gutenberg-products-block' 'woo-gutenberg-products-block'
), ),
} ), } ),
lock: {
type: 'object',
default: {
move: true,
remove: true,
},
},
}; };

View File

@ -28,10 +28,6 @@ registerFeaturePluginBlockType( 'woocommerce/checkout-shipping-address-block', {
multiple: false, multiple: false,
reusable: false, reusable: false,
inserter: false, inserter: false,
lock: {
remove: true,
move: true,
},
}, },
parent: [ 'woocommerce/checkout-fields-block' ], parent: [ 'woocommerce/checkout-fields-block' ],
attributes, attributes,

View File

@ -20,4 +20,11 @@ export default {
type: 'boolean', type: 'boolean',
default: false, default: false,
}, },
lock: {
type: 'object',
default: {
move: true,
remove: true,
},
},
}; };

View File

@ -28,10 +28,6 @@ registerFeaturePluginBlockType( 'woocommerce/checkout-shipping-methods-block', {
multiple: false, multiple: false,
reusable: false, reusable: false,
inserter: false, inserter: false,
lock: {
remove: true,
move: true,
},
}, },
parent: [ 'woocommerce/checkout-fields-block' ], parent: [ 'woocommerce/checkout-fields-block' ],
attributes, attributes,

View File

@ -27,12 +27,12 @@
body.wc-lock-selected-block--move { body.wc-lock-selected-block--move {
.block-editor-block-mover__move-button-container, .block-editor-block-mover__move-button-container,
.block-editor-block-mover, .block-editor-block-mover {
.block-editor-block-settings-menu {
display: none; display: none;
} }
} }
body.wc-lock-selected-block--remove { body.wc-lock-selected-block--remove {
.block-editor-block-settings-menu__popover { .block-editor-block-settings-menu__popover {
.components-menu-group:last-child { .components-menu-group:last-child {