Skip to content

Commit

Permalink
Change to modify
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin940726 committed May 10, 2024
1 parent a13071f commit 0147d37
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 38 deletions.
49 changes: 16 additions & 33 deletions packages/block-editor/src/hooks/content-lock-ui.js
Expand Up @@ -20,7 +20,6 @@ import { unlock } from '../lock-unlock';
// also includes artifacts on the store (actions, reducers, and selector).

function ContentLockControlsPure( { clientId, isSelected } ) {
const { getBlockListSettings, getSettings } = useSelect( blockEditorStore );
const { templateLock, isLockedByParent, isEditingAsBlocks } = useSelect(
( select ) => {
const {
Expand All @@ -37,16 +36,11 @@ function ContentLockControlsPure( { clientId, isSelected } ) {
[ clientId ]
);

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

const stopEditingAsBlockCallback = useCallback( () => {
stopEditingAsBlocks( clientId );
Expand All @@ -73,30 +67,19 @@ function ContentLockControlsPure( { clientId, isSelected } ) {
) }
{ showStartEditingAsBlocks && (
<BlockSettingsMenuControls>
{ ( { onClose } ) => (
<MenuItem
onClick={ () => {
__unstableMarkNextChangeAsNotPersistent();
updateBlockAttributes( clientId, {
templateLock: undefined,
} );
updateBlockListSettings( clientId, {
...getBlockListSettings( clientId ),
templateLock: false,
} );
const focusModeToRevert =
getSettings().focusMode;
updateSettings( { focusMode: true } );
__unstableSetTemporarilyEditingAsBlocks(
clientId,
focusModeToRevert
);
onClose();
} }
>
{ __( 'Modify' ) }
</MenuItem>
) }
{ ( { selectedClientIds, onClose } ) =>
selectedClientIds.length === 1 &&
selectedClientIds[ 0 ] === clientId && (
<MenuItem
onClick={ () => {
modifyContentLockBlock( clientId );
onClose();
} }
>
{ __( 'Modify' ) }
</MenuItem>
)
}
</BlockSettingsMenuControls>
) }
</>
Expand Down
19 changes: 19 additions & 0 deletions packages/block-editor/src/store/private-actions.js
Expand Up @@ -391,3 +391,22 @@ export function expandBlock( clientId ) {
clientId,
};
}

export const modifyContentLockBlock =
( clientId ) =>
( { select, dispatch } ) => {
dispatch.__unstableMarkNextChangeAsNotPersistent();
dispatch.updateBlockAttributes( clientId, {
templateLock: undefined,
} );
dispatch.updateBlockListSettings( clientId, {
...select.getBlockListSettings( clientId ),
templateLock: false,
} );
const focusModeToRevert = select.getSettings().focusMode;
dispatch.updateSettings( { focusMode: true } );
dispatch.__unstableSetTemporarilyEditingAsBlocks(
clientId,
focusModeToRevert
);
};
Expand Up @@ -131,7 +131,11 @@ function TemplateLockContentOnlyMenuItems( { clientId, onClose } ) {
);
const blockDisplayInformation =
useBlockDisplayInformation( contentLockingParent );
const { updateBlockAttributes } = useDispatch( blockEditorStore );
// 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 )
);

if ( ! blockDisplayInformation?.title ) {
return null;
Expand All @@ -156,15 +160,14 @@ function TemplateLockContentOnlyMenuItems( { clientId, onClose } ) {
</BlockSettingsMenuFirstItem>
<MenuItem
onClick={ () => {
updateBlockAttributes( contentLockingParent, {
templateLock: undefined,
} );
selectBlock( contentLockingParent );
modifyContentLockBlock( contentLockingParent );
onClose();
} }
>
{ sprintf(
// translators: %s: block's title.
__( 'Unlock "%s"' ),
__( 'Modify "%s"' ),
blockDisplayInformation.title
) }
</MenuItem>
Expand Down

0 comments on commit 0147d37

Please sign in to comment.