Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to prevent editing blocks using useBlockEditingMode() #50643

Merged
merged 17 commits into from
May 24, 2023
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions packages/block-editor/src/components/block-breadcrumb/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { chevronRightSmall, Icon } from '@wordpress/icons';
*/
import BlockTitle from '../block-title';
import { store as blockEditorStore } from '../../store';
import { unlock } from '../../lock-unlock';

/**
* Block breadcrumb component, displaying the hierarchy of the current block selection as a breadcrumb.
Expand All @@ -22,11 +23,18 @@ import { store as blockEditorStore } from '../../store';
function BlockBreadcrumb( { rootLabelText } ) {
const { selectBlock, clearSelectedBlock } = useDispatch( blockEditorStore );
const { clientId, parents, hasSelection } = useSelect( ( select ) => {
const { getSelectionStart, getSelectedBlockClientId, getBlockParents } =
select( blockEditorStore );
const {
getSelectionStart,
getSelectedBlockClientId,
getBlockParents,
getBlockEditingMode,
} = unlock( select( blockEditorStore ) );
const selectedBlockClientId = getSelectedBlockClientId();
return {
parents: getBlockParents( selectedBlockClientId ),
parents: getBlockParents( selectedBlockClientId ).filter(
( parentClientId ) =>
getBlockEditingMode( parentClientId ) !== 'disabled'
),
Comment on lines +34 to +37
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

clientId: selectedBlockClientId,
hasSelection: !! getSelectionStart().clientId,
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/**
* WordPress dependencies
*/
import { useSelect, useDispatch } from '@wordpress/data';
import { useContext, useEffect } from '@wordpress/element';

/**
* Internal dependencies
*/
import { store as blockEditorStore } from '../../store';
import { unlock } from '../../lock-unlock';
import { BlockListBlockContext } from '../block-list/block-list-block-context';

/**
* @typedef {'disabled'|'contentOnly'|'default'} BlockEditingMode
*/

/**
* Allows a block to restrict the user interface that is displayed for editing
* that block and its inner blocks.
*
* @example
* ```js
* function MyBlock( { attributes, setAttributes } ) {
* useBlockEditingMode( 'disabled' );
* return <div { ...useBlockProps() }></div>;
* }
* ```
*
* `mode` can be one of three options:
*
* - `'disabled'`: Prevents editing the block entirely, i.e. it cannot be
* selected.
* - `'contentOnly'`: Hides all non-content UI, e.g. auxiliary controls in the
* toolbar, the block movers, block settings.
* - `'default'`: Allows editing the block as normal.
*
* The mode is inherited by all of the block's inner blocks, unless they have
* their own mode.
*
* If called outside of a block context, the mode is applied to all blocks.
*
* @param {?BlockEditingMode} mode The editing mode to apply. If undefined, the
* current editing mode is not changed.
*
* @return {BlockEditingMode} The current editing mode.
*/
export function useBlockEditingMode( mode ) {
ramonjd marked this conversation as resolved.
Show resolved Hide resolved
const { clientId = '' } = useContext( BlockListBlockContext ) ?? {};
const blockEditingMode = useSelect(
( select ) =>
unlock( select( blockEditorStore ) ).getBlockEditingMode(
clientId
),
[ clientId ]
);
const { setBlockEditingMode, unsetBlockEditingMode } = unlock(
useDispatch( blockEditorStore )
);
useEffect( () => {
if ( mode ) {
setBlockEditingMode( clientId, mode );
}
return () => {
if ( mode ) {
unsetBlockEditingMode( clientId );
}
};
}, [ clientId, mode ] );
return blockEditingMode;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* WordPress dependencies
*/
import { createContext } from '@wordpress/element';

export const BlockListBlockContext = createContext( null );
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pulling this out to its own file so that it's available for use on both web and mobile (React native). Hopefully that fixes mobile tests.

38 changes: 10 additions & 28 deletions packages/block-editor/src/components/block-list/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,13 @@ import classnames from 'classnames';
/**
* WordPress dependencies
*/
import {
createContext,
useMemo,
useCallback,
RawHTML,
} from '@wordpress/element';
import { useMemo, useCallback, RawHTML } from '@wordpress/element';
import {
getBlockType,
getSaveContent,
isUnmodifiedDefaultBlock,
serializeRawBlock,
switchToBlockType,
store as blocksStore,
getDefaultBlockName,
isUnmodifiedBlock,
} from '@wordpress/blocks';
Expand All @@ -43,7 +37,8 @@ import BlockHtml from './block-html';
import { useBlockProps } from './use-block-props';
import { store as blockEditorStore } from '../../store';
import { useLayout } from './layout';
export const BlockListBlockContext = createContext();
import { unlock } from '../../lock-unlock';
import { BlockListBlockContext } from './block-list-block-context';

/**
* Merges wrapper props with special handling for classNames and styles.
Expand Down Expand Up @@ -99,35 +94,23 @@ function BlockListBlock( {
} ) {
const {
themeSupportsLayout,
hasContentLockedParent,
isContentBlock,
isContentLocking,
isTemporarilyEditingAsBlocks,
blockEditingMode,
} = useSelect(
( select ) => {
const {
getSettings,
__unstableGetContentLockingParent,
getTemplateLock,
__unstableGetTemporarilyEditingAsBlocks,
} = select( blockEditorStore );
const _hasContentLockedParent =
!! __unstableGetContentLockingParent( clientId );
getBlockEditingMode,
} = unlock( select( blockEditorStore ) );
return {
themeSupportsLayout: getSettings().supportsLayout,
isContentBlock:
select( blocksStore ).__experimentalHasContentRoleAttribute(
name
),
hasContentLockedParent: _hasContentLockedParent,
isContentLocking:
getTemplateLock( clientId ) === 'contentOnly' &&
! _hasContentLockedParent,
isTemporarilyEditingAsBlocks:
__unstableGetTemporarilyEditingAsBlocks() === clientId,
blockEditingMode: getBlockEditingMode( clientId ),
};
},
[ name, clientId ]
[ clientId ]
);
const { removeBlock } = useDispatch( blockEditorStore );
const onRemove = useCallback( () => removeBlock( clientId ), [ clientId ] );
Expand Down Expand Up @@ -160,7 +143,7 @@ function BlockListBlock( {

const blockType = getBlockType( name );

if ( hasContentLockedParent && ! isContentBlock ) {
if ( blockEditingMode === 'disabled' ) {
wrapperProps = {
...wrapperProps,
tabIndex: -1,
Expand Down Expand Up @@ -234,10 +217,9 @@ function BlockListBlock( {
clientId,
className: classnames(
{
'is-content-locked': isContentLocking,
'is-editing-disabled': blockEditingMode === 'disabled',
'is-content-locked-temporarily-editing-as-blocks':
isTemporarilyEditingAsBlocks,
'is-content-block': hasContentLockedParent && isContentBlock,
},
dataAlign && themeSupportsLayout && `align${ dataAlign }`,
className
Expand Down
10 changes: 5 additions & 5 deletions packages/block-editor/src/components/block-list/content.scss
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,13 @@
padding: 0;
}

.is-content-locked {
.block-editor-block-list__block {
.block-editor-block-list__layout,
.block-editor-block-list__block {
pointer-events: initial;

&.is-editing-disabled {
pointer-events: none;
noisysocks marked this conversation as resolved.
Show resolved Hide resolved
}
.is-content-block {
pointer-events: initial;
}
}

.block-editor-block-list__layout .block-editor-block-list__block {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import warning from '@wordpress/warning';
* Internal dependencies
*/
import useMovingAnimation from '../../use-moving-animation';
import { BlockListBlockContext } from '../block';
import { BlockListBlockContext } from '../block-list-block-context';
import { useFocusFirstElement } from './use-focus-first-element';
import { useIsHovered } from './use-is-hovered';
import { useBlockEditContext } from '../../block-edit/context';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { isRTL } from '@wordpress/i18n';
*/
import { store as blockEditorStore } from '../../store';
import { InsertionPointOpenRef } from '../block-tools/insertion-point';
import { unlock } from '../../lock-unlock';

export function useInBetweenInserter() {
const openRef = useContext( InsertionPointOpenRef );
Expand All @@ -29,7 +30,8 @@ export function useInBetweenInserter() {
getSelectedBlockClientIds,
getTemplateLock,
__unstableIsWithinBlockOverlay,
} = useSelect( blockEditorStore );
getBlockEditingMode,
} = unlock( useSelect( blockEditorStore ) );
const { showInsertionPoint, hideInsertionPoint } =
useDispatch( blockEditorStore );

Expand Down Expand Up @@ -74,8 +76,12 @@ export function useInBetweenInserter() {
rootClientId = blockElement.getAttribute( 'data-block' );
}

// Don't set the insertion point if the template is locked.
if ( getTemplateLock( rootClientId ) ) {
// Don't show the in-between inserter if the container block is
// locked or disabled.
noisysocks marked this conversation as resolved.
Show resolved Hide resolved
if (
getTemplateLock( rootClientId ) ||
getBlockEditingMode( rootClientId ) === 'disabled'
) {
return;
}

Expand Down
21 changes: 10 additions & 11 deletions packages/block-editor/src/components/block-toolbar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import BlockEditVisuallyButton from '../block-edit-visually-button';
import { useShowMoversGestures } from './utils';
import { store as blockEditorStore } from '../../store';
import __unstableBlockNameContext from './block-name-context';
import { unlock } from '../../lock-unlock';

const BlockToolbar = ( { hideDragHandle } ) => {
const {
Expand All @@ -42,7 +43,7 @@ const BlockToolbar = ( { hideDragHandle } ) => {
isDistractionFree,
isValid,
isVisual,
isContentLocked,
blockEditingMode,
} = useSelect( ( select ) => {
const {
getBlockName,
Expand All @@ -51,8 +52,8 @@ const BlockToolbar = ( { hideDragHandle } ) => {
isBlockValid,
getBlockRootClientId,
getSettings,
__unstableGetContentLockingParent,
} = select( blockEditorStore );
getBlockEditingMode,
} = unlock( select( blockEditorStore ) );
const selectedBlockClientIds = getSelectedBlockClientIds();
const selectedBlockClientId = selectedBlockClientIds[ 0 ];
const blockRootClientId = getBlockRootClientId( selectedBlockClientId );
Expand All @@ -73,9 +74,7 @@ const BlockToolbar = ( { hideDragHandle } ) => {
isVisual: selectedBlockClientIds.every(
( id ) => getBlockMode( id ) === 'visual'
),
isContentLocked: !! __unstableGetContentLockingParent(
selectedBlockClientId
),
blockEditingMode: getBlockEditingMode( selectedBlockClientId ),
};
}, [] );

Expand Down Expand Up @@ -125,12 +124,12 @@ const BlockToolbar = ( { hideDragHandle } ) => {

return (
<div className={ classes }>
{ ! isMultiToolbar && isLargeViewport && ! isContentLocked && (
<BlockParentSelector />
) }
{ ! isMultiToolbar &&
isLargeViewport &&
blockEditingMode === 'default' && <BlockParentSelector /> }
<div ref={ nodeRef } { ...showMoversGestures }>
{ ( shouldShowVisualToolbar || isMultiToolbar ) &&
! isContentLocked && (
blockEditingMode === 'default' && (
<ToolbarGroup className="block-editor-block-toolbar__block-controls">
<BlockSwitcher clientIds={ blockClientIds } />
{ ! isMultiToolbar && (
Expand Down Expand Up @@ -175,7 +174,7 @@ const BlockToolbar = ( { hideDragHandle } ) => {
</>
) }
<BlockEditVisuallyButton clientIds={ blockClientIds } />
{ ! isContentLocked && (
{ blockEditingMode === 'default' && (
<BlockSettingsMenu clientIds={ blockClientIds } />
) }
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import NavigableToolbar from '../navigable-toolbar';
import BlockToolbar from '../block-toolbar';
import { store as blockEditorStore } from '../../store';
import BlockIcon from '../block-icon';
import { unlock } from '../../lock-unlock';

function BlockContextualToolbar( { focusOnMount, isFixed, ...props } ) {
// When the toolbar is fixed it can be collapsed
Expand All @@ -38,8 +39,8 @@ function BlockContextualToolbar( { focusOnMount, isFixed, ...props } ) {
getBlockName,
getBlockParents,
getSelectedBlockClientIds,
__unstableGetContentLockingParent,
} = select( blockEditorStore );
getBlockEditingMode,
} = unlock( select( blockEditorStore ) );
const { getBlockType } = select( blocksStore );
const selectedBlockClientIds = getSelectedBlockClientIds();
const _selectedBlockClientId = selectedBlockClientIds[ 0 ];
Expand All @@ -62,9 +63,7 @@ function BlockContextualToolbar( { focusOnMount, isFixed, ...props } ) {
true
) &&
selectedBlockClientIds.length <= 1 &&
! __unstableGetContentLockingParent(
_selectedBlockClientId
),
getBlockEditingMode( _selectedBlockClientId ) === 'default',
};
}, [] );

Expand Down
Loading