2021-09-21 10:18:27 +00:00
|
|
|
/**
|
|
|
|
* HACKS
|
|
|
|
*
|
|
|
|
* This file contains functionality to "lock" blocks i.e. to prevent blocks being moved or deleted. This needs to be
|
|
|
|
* kept in place until native support for locking is available in WordPress (estimated WordPress 5.9).
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @todo Remove custom block locking (requires native WordPress support)
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import {
|
|
|
|
useBlockProps,
|
|
|
|
store as blockEditorStore,
|
|
|
|
} from '@wordpress/block-editor';
|
|
|
|
import { isTextField } from '@wordpress/dom';
|
2021-09-24 13:44:05 +00:00
|
|
|
import { subscribe, select as _select } from '@wordpress/data';
|
2021-09-21 10:18:27 +00:00
|
|
|
import { useEffect, useRef } from '@wordpress/element';
|
|
|
|
import { MutableRefObject } from 'react';
|
|
|
|
import { BACKSPACE, DELETE } from '@wordpress/keycodes';
|
|
|
|
import { hasFilter } from '@wordpress/hooks';
|
2021-09-24 13:44:05 +00:00
|
|
|
import { getBlockType } from '@wordpress/blocks';
|
2021-09-21 10:18:27 +00:00
|
|
|
/**
|
|
|
|
* Toggle class on body.
|
|
|
|
*
|
|
|
|
* @param {string} className CSS Class name.
|
|
|
|
* @param {boolean} add True to add, false to remove.
|
|
|
|
*/
|
|
|
|
const toggleBodyClass = ( className: string, add = true ) => {
|
|
|
|
if ( add ) {
|
|
|
|
window.document.body.classList.add( className );
|
|
|
|
} else {
|
|
|
|
window.document.body.classList.remove( className );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* addClassToBody
|
|
|
|
*
|
|
|
|
* This components watches the current selected block and adds a class name to the body if that block is locked. If the
|
|
|
|
* current block is not locked, it removes the class name. The appended body class is used to hide UI elements to prevent
|
|
|
|
* the block from being deleted.
|
|
|
|
*
|
|
|
|
* We use a component so we can react to changes in the store.
|
|
|
|
*/
|
|
|
|
export const addClassToBody = (): void => {
|
|
|
|
if ( ! hasFilter( 'blocks.registerBlockType', 'core/lock/addAttribute' ) ) {
|
|
|
|
subscribe( () => {
|
|
|
|
const blockEditorSelect = _select( blockEditorStore );
|
|
|
|
|
|
|
|
if ( ! blockEditorSelect ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const selectedBlock = blockEditorSelect.getSelectedBlock();
|
|
|
|
|
|
|
|
if ( ! selectedBlock ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
toggleBodyClass(
|
|
|
|
'wc-lock-selected-block--remove',
|
|
|
|
!! selectedBlock?.attributes?.lock?.remove
|
|
|
|
);
|
|
|
|
|
|
|
|
toggleBodyClass(
|
|
|
|
'wc-lock-selected-block--move',
|
|
|
|
!! selectedBlock?.attributes?.lock?.move
|
|
|
|
);
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-09-24 13:44:05 +00:00
|
|
|
const isBlockLocked = ( clientId: string ): boolean => {
|
|
|
|
if ( ! clientId ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const { getBlock } = _select( blockEditorStore );
|
|
|
|
const block = getBlock( clientId );
|
|
|
|
// If lock.remove is defined at the block instance (not using the default value)
|
|
|
|
// Then we use it.
|
|
|
|
if ( typeof block?.attributes?.lock?.remove === 'boolean' ) {
|
|
|
|
return block.attributes.lock.remove;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we don't have lock on the block instance, we check the type
|
|
|
|
const blockType = getBlockType( block.name );
|
|
|
|
if ( typeof blockType?.attributes?.lock?.default?.remove === 'boolean' ) {
|
|
|
|
return blockType?.attributes?.lock?.default?.remove;
|
|
|
|
}
|
|
|
|
// If nothing is defined, return false
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2021-09-21 10:18:27 +00:00
|
|
|
/**
|
2021-09-24 13:44:05 +00:00
|
|
|
* This is a hook we use in conjunction with useBlockProps. Its goal is to check if of the block's children is locked and being deleted.
|
|
|
|
* It will stop the keydown event from propagating to stop it from being deleted via the keyboard.
|
2021-09-21 10:18:27 +00:00
|
|
|
*
|
|
|
|
*/
|
2021-09-24 13:44:05 +00:00
|
|
|
const useLockedChildren = ( {
|
2021-09-21 10:18:27 +00:00
|
|
|
ref,
|
|
|
|
}: {
|
2021-09-24 13:44:05 +00:00
|
|
|
ref: MutableRefObject< HTMLElement | undefined >;
|
2021-09-21 10:18:27 +00:00
|
|
|
} ): void => {
|
|
|
|
const lockInCore = hasFilter(
|
|
|
|
'blocks.registerBlockType',
|
|
|
|
'core/lock/addAttribute'
|
|
|
|
);
|
|
|
|
|
|
|
|
const node = ref.current;
|
|
|
|
return useEffect( () => {
|
2021-09-24 13:44:05 +00:00
|
|
|
if ( ! node || lockInCore ) {
|
2021-09-21 10:18:27 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
function onKeyDown( event: KeyboardEvent ) {
|
|
|
|
const { keyCode, target } = event;
|
2021-09-24 13:44:05 +00:00
|
|
|
|
|
|
|
if ( ! ( target instanceof HTMLElement ) ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// We're not trying to delete something here.
|
2021-09-21 10:18:27 +00:00
|
|
|
if ( keyCode !== BACKSPACE && keyCode !== DELETE ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-09-24 13:44:05 +00:00
|
|
|
// We're in a field, so we should let text be deleted.
|
|
|
|
if ( isTextField( target ) ) {
|
2021-09-21 10:18:27 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-09-24 13:44:05 +00:00
|
|
|
|
|
|
|
// Typecast to fix issue with isTextField.
|
|
|
|
const targetNode = target as HTMLElement;
|
|
|
|
|
|
|
|
// Our target isn't a block.
|
|
|
|
if ( targetNode.dataset.block === undefined ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const clientId = targetNode.dataset.block;
|
|
|
|
const isLocked = isBlockLocked( clientId );
|
2021-09-21 10:18:27 +00:00
|
|
|
// Prevent the keyboard event from propogating if it supports locking.
|
2021-09-24 13:44:05 +00:00
|
|
|
if ( isLocked ) {
|
2021-09-21 10:18:27 +00:00
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
2021-09-24 13:44:05 +00:00
|
|
|
event.stopImmediatePropagation();
|
2021-09-21 10:18:27 +00:00
|
|
|
}
|
|
|
|
}
|
2021-09-24 13:44:05 +00:00
|
|
|
node.addEventListener( 'keydown', onKeyDown, {
|
|
|
|
capture: true,
|
|
|
|
passive: false,
|
|
|
|
} );
|
2021-09-21 10:18:27 +00:00
|
|
|
|
|
|
|
return () => {
|
2021-09-24 13:44:05 +00:00
|
|
|
node.removeEventListener( 'keydown', onKeyDown, {
|
|
|
|
capture: true,
|
|
|
|
} );
|
2021-09-21 10:18:27 +00:00
|
|
|
};
|
2021-09-24 13:44:05 +00:00
|
|
|
}, [ node, lockInCore ] );
|
2021-09-21 10:18:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This hook is a light wrapper to useBlockProps, it wraps that hook plus useLockBlock to pass data between them.
|
|
|
|
*/
|
|
|
|
export const useBlockPropsWithLocking = (
|
|
|
|
props: Record< string, unknown > = {}
|
|
|
|
): Record< string, unknown > => {
|
2021-09-24 13:44:05 +00:00
|
|
|
const ref = useRef< HTMLElement >();
|
2021-09-21 10:18:27 +00:00
|
|
|
const blockProps = useBlockProps( { ref, ...props } );
|
2021-09-24 13:44:05 +00:00
|
|
|
useLockedChildren( {
|
2021-09-21 10:18:27 +00:00
|
|
|
ref,
|
|
|
|
} );
|
|
|
|
return blockProps;
|
|
|
|
};
|