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

Fix Modify content-locked menu item not showing if the block is not selected #61605

Merged
merged 1 commit into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
import { BlockLockMenuItem, useBlockLock } from '../block-lock';
import { store as blockEditorStore } from '../../store';
import BlockModeToggle from '../block-settings-menu/block-mode-toggle';

import { ModifyContentLockMenuItem } from '../content-lock';
import { BlockRenameControl, useBlockRename } from '../block-rename';

const { Fill, Slot } = createSlotFill( 'BlockSettingsMenuControls' );
Expand Down Expand Up @@ -108,6 +108,12 @@ const BlockSettingsMenuControlsSlot = ( { fillProps, clientIds = null } ) => {
{ __( 'Move to' ) }
</MenuItem>
) }
{ selectedClientIds.length === 1 && (
<ModifyContentLockMenuItem
clientId={ selectedClientIds[ 0 ] }
onClose={ fillProps?.onClose }
/>
) }
{ fillProps?.count === 1 && ! isContentOnly && (
<BlockModeToggle
clientId={ fillProps?.firstBlockClientId }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { ModifyContentLockMenuItem } from './modify-content-lock-menu-item';
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
* WordPress dependencies
*/
import { MenuItem } from '@wordpress/components';
import { useDispatch, useSelect } from '@wordpress/data';
import { _x } from '@wordpress/i18n';

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

// The implementation of content locking is mainly in this file, although the mechanism
// to stop temporarily editing as blocks when an outside block is selected is on component StopEditingAsBlocksOnOutsideSelect
// at block-editor/src/components/block-list/index.js.
// Besides the components on this file and the file referenced above the implementation
// also includes artifacts on the store (actions, reducers, and selector).

export function ModifyContentLockMenuItem( { clientId, onClose } ) {
const { templateLock, isLockedByParent, isEditingAsBlocks } = useSelect(
( select ) => {
const {
getContentLockingParent,
getTemplateLock,
getTemporarilyEditingAsBlocks,
} = unlock( select( blockEditorStore ) );
return {
templateLock: getTemplateLock( clientId ),
isLockedByParent: !! getContentLockingParent( clientId ),
isEditingAsBlocks: getTemporarilyEditingAsBlocks() === clientId,
};
},
[ clientId ]
);
const blockEditorActions = useDispatch( blockEditorStore );
const isContentLocked =
! isLockedByParent && templateLock === 'contentOnly';
if ( ! isContentLocked && ! isEditingAsBlocks ) {
return null;
}

const { modifyContentLockBlock } = unlock( blockEditorActions );
const showStartEditingAsBlocks = ! isEditingAsBlocks && isContentLocked;

return (
showStartEditingAsBlocks && (
<MenuItem
onClick={ () => {
modifyContentLockBlock( clientId );
onClose();
} }
>
{ _x( 'Modify', 'Unlock content locked blocks' ) }
</MenuItem>
)
);
}
47 changes: 11 additions & 36 deletions packages/block-editor/src/hooks/content-lock-ui.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* WordPress dependencies
*/
import { ToolbarButton, MenuItem } from '@wordpress/components';
import { ToolbarButton } from '@wordpress/components';
import { useDispatch, useSelect } from '@wordpress/data';
import { __ } from '@wordpress/i18n';
import { useCallback } from '@wordpress/element';
Expand All @@ -10,7 +10,7 @@ import { useCallback } from '@wordpress/element';
* Internal dependencies
*/
import { store as blockEditorStore } from '../store';
import { BlockControls, BlockSettingsMenuControls } from '../components';
import { BlockControls } from '../components';
import { unlock } from '../lock-unlock';

// The implementation of content locking is mainly in this file, although the mechanism
Expand All @@ -19,7 +19,7 @@ import { unlock } from '../lock-unlock';
// Besides the components on this file and the file referenced above the implementation
// also includes artifacts on the store (actions, reducers, and selector).

function ContentLockControlsPure( { clientId, isSelected } ) {
function ContentLockControlsPure( { clientId } ) {
const { templateLock, isLockedByParent, isEditingAsBlocks } = useSelect(
( select ) => {
const {
Expand All @@ -36,9 +36,7 @@ function ContentLockControlsPure( { clientId, isSelected } ) {
[ clientId ]
);

const { stopEditingAsBlocks, modifyContentLockBlock } = unlock(
useDispatch( blockEditorStore )
);
const { stopEditingAsBlocks } = unlock( useDispatch( blockEditorStore ) );
const isContentLocked =
! isLockedByParent && templateLock === 'contentOnly';

Expand All @@ -51,38 +49,15 @@ function ContentLockControlsPure( { clientId, isSelected } ) {
}

const showStopEditingAsBlocks = isEditingAsBlocks && ! isContentLocked;
const showStartEditingAsBlocks =
! isEditingAsBlocks && isContentLocked && isSelected;

return (
<>
{ showStopEditingAsBlocks && (
<>
<BlockControls group="other">
<ToolbarButton onClick={ stopEditingAsBlockCallback }>
{ __( 'Done' ) }
</ToolbarButton>
</BlockControls>
</>
) }
{ showStartEditingAsBlocks && (
<BlockSettingsMenuControls>
{ ( { selectedClientIds, onClose } ) =>
selectedClientIds.length === 1 &&
selectedClientIds[ 0 ] === clientId && (
<MenuItem
onClick={ () => {
modifyContentLockBlock( clientId );
onClose();
} }
>
{ __( 'Modify' ) }
</MenuItem>
)
}
</BlockSettingsMenuControls>
) }
</>
showStopEditingAsBlocks && (
<BlockControls group="other">
<ToolbarButton onClick={ stopEditingAsBlockCallback }>
{ __( 'Done' ) }
</ToolbarButton>
</BlockControls>
)
);
}

Expand Down
1 change: 1 addition & 0 deletions packages/block-editor/src/store/private-actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,7 @@ export function expandBlock( clientId ) {
export const modifyContentLockBlock =
( clientId ) =>
( { select, dispatch } ) => {
dispatch.selectBlock( clientId );
dispatch.__unstableMarkNextChangeAsNotPersistent();
dispatch.updateBlockAttributes( clientId, {
templateLock: undefined,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
import { store as coreStore } from '@wordpress/core-data';
import { __experimentalText as Text, MenuItem } from '@wordpress/components';
import { useSelect, useDispatch } from '@wordpress/data';
import { __ } from '@wordpress/i18n';
import { __, _x } from '@wordpress/i18n';

/**
* Internal dependencies
Expand Down Expand Up @@ -137,27 +137,23 @@ function TemplateLockContentOnlyMenuItems( { clientId, onClose } ) {
);
const blockDisplayInformation =
useBlockDisplayInformation( contentLockingParent );
// Disable reason: We're using a hook here so it has to be on top-level.
// eslint-disable-next-line @wordpress/no-unused-vars-before-return
const { modifyContentLockBlock, selectBlock } = unlock(
useDispatch( blockEditorStore )
);

const blockEditorActions = useDispatch( blockEditorStore );
if ( ! blockDisplayInformation?.title ) {
return null;
}

const { modifyContentLockBlock } = unlock( blockEditorActions );

return (
<>
<BlockSettingsMenuFirstItem>
<MenuItem
onClick={ () => {
selectBlock( contentLockingParent );
modifyContentLockBlock( contentLockingParent );
onClose();
} }
>
{ __( 'Unlock' ) }
{ _x( 'Unlock', 'Unlock content locked blocks' ) }
</MenuItem>
</BlockSettingsMenuFirstItem>
<Text
Expand Down
Loading