Skip to content

Commit

Permalink
Make the new action private
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgefilipecosta committed Jan 11, 2024
1 parent 6883e1b commit 9f989b3
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 57 deletions.
58 changes: 34 additions & 24 deletions packages/block-editor/src/components/block-list/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ import {
useMergeRefs,
useDebounce,
} from '@wordpress/compose';
import { createContext, useMemo, useCallback, useEffect } from '@wordpress/element';
import {
createContext,
useMemo,
useCallback,
useEffect,
} from '@wordpress/element';

/**
* Internal dependencies
Expand All @@ -34,6 +39,7 @@ import {
DEFAULT_BLOCK_EDIT_CONTEXT,
} from '../block-edit/context';
import { useTypingObserver } from '../observe-typing';
import { unlock } from '../../lock-unlock';

export const IntersectionObserver = createContext();
const pendingBlockVisibilityUpdatesPerRegistry = new WeakMap();
Expand Down Expand Up @@ -114,9 +120,7 @@ function Root( { className, ...settings } ) {
}

function StopEditingAsBlocksOnOutsideSelect( { clientId } ) {
const {
__unstableStopEditingAsBlocks,
} = useDispatch( blockEditorStore );
const { stopEditingAsBlocks } = unlock( useDispatch( blockEditorStore ) );
const isBlockOrDescendantSelected = useSelect(
( select ) => {
const { isBlockSelected, hasSelectedInnerBlock } =
Expand All @@ -130,9 +134,9 @@ function StopEditingAsBlocksOnOutsideSelect( { clientId } ) {
);
useEffect( () => {
if ( ! isBlockOrDescendantSelected ) {
__unstableStopEditingAsBlocks( clientId );
stopEditingAsBlocks( clientId );
}
}, [ isBlockOrDescendantSelected, clientId, __unstableStopEditingAsBlocks ] );
}, [ isBlockOrDescendantSelected, clientId, stopEditingAsBlocks ] );
return null;
}

Expand All @@ -151,23 +155,25 @@ function Items( {
__experimentalAppenderTagName,
layout = defaultLayout,
} ) {
const { order, selectedBlocks, visibleBlocks, temporarilyEditingAsBlocks } = useSelect(
( select ) => {
const {
getBlockOrder,
getSelectedBlockClientIds,
__unstableGetVisibleBlocks,
__unstableGetTemporarilyEditingAsBlocks,
} = select( blockEditorStore );
return {
order: getBlockOrder( rootClientId ),
selectedBlocks: getSelectedBlockClientIds(),
visibleBlocks: __unstableGetVisibleBlocks(),
temporarilyEditingAsBlocks: __unstableGetTemporarilyEditingAsBlocks(),
};
},
[ rootClientId ]
);
const { order, selectedBlocks, visibleBlocks, temporarilyEditingAsBlocks } =
useSelect(
( select ) => {
const {
getBlockOrder,
getSelectedBlockClientIds,
__unstableGetVisibleBlocks,
__unstableGetTemporarilyEditingAsBlocks,
} = select( blockEditorStore );
return {
order: getBlockOrder( rootClientId ),
selectedBlocks: getSelectedBlockClientIds(),
visibleBlocks: __unstableGetVisibleBlocks(),
temporarilyEditingAsBlocks:
__unstableGetTemporarilyEditingAsBlocks(),
};
},
[ rootClientId ]
);

return (
<LayoutProvider value={ layout }>
Expand All @@ -188,7 +194,11 @@ function Items( {
</AsyncModeProvider>
) ) }
{ order.length < 1 && placeholder }
{ !! temporarilyEditingAsBlocks && <StopEditingAsBlocksOnOutsideSelect clientId={ temporarilyEditingAsBlocks } /> }
{ !! temporarilyEditingAsBlocks && (
<StopEditingAsBlocksOnOutsideSelect
clientId={ temporarilyEditingAsBlocks }
/>
) }
<BlockListAppender
tagName={ __experimentalAppenderTagName }
rootClientId={ rootClientId }
Expand Down
15 changes: 8 additions & 7 deletions packages/block-editor/src/hooks/content-lock-ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { useCallback } from '@wordpress/element';
*/
import { store as blockEditorStore } from '../store';
import { BlockControls, BlockSettingsMenuControls } from '../components';
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
Expand Down Expand Up @@ -41,17 +42,19 @@ function ContentLockControlsPure( { clientId, isSelected } ) {
const {
updateSettings,
updateBlockListSettings,
__unstableStopEditingAsBlocks,
__unstableSetTemporarilyEditingAsBlocks,
} = useDispatch( blockEditorStore );
const { stopEditingAsBlocks } = unlock(
useDispatch( blockEditorStore )
);
const isContentLocked =
! isLockedByParent && templateLock === 'contentOnly';
const { __unstableMarkNextChangeAsNotPersistent, updateBlockAttributes } =
useDispatch( blockEditorStore );

const stopEditingAsBlock = useCallback( () => {
__unstableStopEditingAsBlocks( clientId );
}, [ clientId, __unstableStopEditingAsBlocks ] );
const stopEditingAsBlockCallback = useCallback( () => {
stopEditingAsBlocks( clientId );
}, [ clientId, stopEditingAsBlocks ] );

if ( ! isContentLocked && ! isEditingAsBlocks ) {
return null;
Expand All @@ -67,9 +70,7 @@ function ContentLockControlsPure( { clientId, isSelected } ) {
<>
<BlockControls group="other">
<ToolbarButton
onClick={ () => {
stopEditingAsBlock();
} }
onClick={ stopEditingAsBlockCallback }
>
{ __( 'Done' ) }
</ToolbarButton>
Expand Down
26 changes: 0 additions & 26 deletions packages/block-editor/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -1699,32 +1699,6 @@ export function __unstableSetTemporarilyEditingAsBlocks(
};
}

/**
* Action that stops temporarily editing as blocks.
*
* DO-NOT-USE in production.
* This action is created for internal/experimental only usage and may be
* removed anytime without any warning, causing breakage on any plugin or theme invoking it.
*
* @param {string} clientId The block's clientId.
*/
export function __unstableStopEditingAsBlocks( clientId ) {
return ( { select, dispatch } ) => {
const focusModeToRevert =
select.__unstableGetTemporarilyEditingFocusModeToRevert();
dispatch.__unstableMarkNextChangeAsNotPersistent();
dispatch.updateBlockAttributes( clientId, {
templateLock: 'contentOnly',
} );
dispatch.updateBlockListSettings( clientId, {
...select.getBlockListSettings( clientId ),
templateLock: 'contentOnly',
} );
dispatch.updateSettings( { focusMode: focusModeToRevert } );
dispatch.__unstableSetTemporarilyEditingAsBlocks();
};
}

/**
* Interface for inserter media requests.
*
Expand Down
22 changes: 22 additions & 0 deletions packages/block-editor/src/store/private-actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -338,3 +338,25 @@ export function setLastFocus( lastFocus = null ) {
lastFocus,
};
}

/**
* Action that stops temporarily editing as blocks.
*
* @param {string} clientId The block's clientId.
*/
export function stopEditingAsBlocks( clientId ) {
return ( { select, dispatch } ) => {
const focusModeToRevert =
select.__unstableGetTemporarilyEditingFocusModeToRevert();
dispatch.__unstableMarkNextChangeAsNotPersistent();
dispatch.updateBlockAttributes( clientId, {
templateLock: 'contentOnly',
} );
dispatch.updateBlockListSettings( clientId, {
...select.getBlockListSettings( clientId ),
templateLock: 'contentOnly',
} );
dispatch.updateSettings( { focusMode: focusModeToRevert } );
dispatch.__unstableSetTemporarilyEditingAsBlocks();
};
}

0 comments on commit 9f989b3

Please sign in to comment.