Skip to content

Commit

Permalink
Move updating blocks to action creator
Browse files Browse the repository at this point in the history
  • Loading branch information
getdave committed Sep 3, 2024
1 parent 072a8e8 commit 8ef39a7
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 70 deletions.
72 changes: 8 additions & 64 deletions packages/block-editor/src/components/tool-selector/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,13 @@ import {
} from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { useSelect, useDispatch } from '@wordpress/data';
import { forwardRef, useState } from '@wordpress/element';
import { forwardRef } from '@wordpress/element';
import { Icon, edit as editIcon, brush as brushIcon } from '@wordpress/icons';

/**
* Internal dependencies
*/
import { store as blockEditorStore } from '../../store';
import { sectionRootClientIdKey } from '../../store/private-keys';

const selectIcon = (
<SVG
Expand All @@ -32,34 +31,15 @@ const selectIcon = (
);

function ToolSelector( props, ref ) {
const [ originalTemplateLocks, setOriginalTemplateLocks ] = useState( {} );
const { mode, sectionBlocksClientIds, getBlockAttributes } = useSelect(
( select ) => {
const {
__unstableGetEditorMode,
getBlockOrder,
getBlockAttributes: _getBlockAttributes,
getSettings,
} = select( blockEditorStore );
const { mode } = useSelect( ( select ) => {
const { __unstableGetEditorMode } = select( blockEditorStore );

const { [ sectionRootClientIdKey ]: sectionRootClientId } =
getSettings();
return {
mode: __unstableGetEditorMode(),
};
}, [] );

return {
mode: __unstableGetEditorMode(),
sectionBlocksClientIds: getBlockOrder( sectionRootClientId ),
getBlockAttributes: _getBlockAttributes,
};
},
[]
);

const {
__unstableSetEditorMode,
updateBlockAttributes,
setBlockEditingMode,
unsetBlockEditingMode,
} = useDispatch( blockEditorStore );
const { __unstableSetEditorMode } = useDispatch( blockEditorStore );

const modeIcons = {
edit: editIcon,
Expand Down Expand Up @@ -94,42 +74,6 @@ function ToolSelector( props, ref ) {
<MenuItemsChoice
value={ mode }
onSelect={ ( newMode ) => {
if ( newMode === 'simple' ) {
const originalLocks = {};
sectionBlocksClientIds.forEach(
( clientId ) => {
const attributes =
getBlockAttributes( clientId );
originalLocks[ clientId ] =
attributes.templateLock;

setBlockEditingMode(
clientId,
'contentOnly'
);
}
);
setOriginalTemplateLocks( originalLocks );
updateBlockAttributes(
sectionBlocksClientIds,
{
templateLock: 'contentOnly',
}
);
} else {
// Restore the original templateLock attributes
sectionBlocksClientIds.forEach(
( clientId ) => {
updateBlockAttributes( clientId, {
templateLock:
originalTemplateLocks[
clientId
],
} );
unsetBlockEditingMode( clientId );
}
);
}
__unstableSetEditorMode( newMode );
} }
choices={ [
Expand Down
31 changes: 25 additions & 6 deletions packages/block-editor/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -1672,20 +1672,20 @@ export const setNavigationMode =
export const __unstableSetEditorMode =
( mode ) =>
( { dispatch, select, registry } ) => {
const { [ sectionRootClientIdKey ]: sectionRootClientId } = registry
.select( STORE_NAME )
.getSettings();

const sectionClientIds = select.getBlockOrder( sectionRootClientId );

// When switching to zoom-out mode, we need to select the parent section
if ( mode === 'zoom-out' ) {
const firstSelectedClientId = select.getBlockSelectionStart();
const { [ sectionRootClientIdKey ]: sectionRootClientId } = registry
.select( STORE_NAME )
.getSettings();

if ( firstSelectedClientId ) {
let sectionClientId;

if ( sectionRootClientId ) {
const sectionClientIds =
select.getBlockOrder( sectionRootClientId );

// If the selected block is a section block, use it.
if ( sectionClientIds?.includes( firstSelectedClientId ) ) {
sectionClientId = firstSelectedClientId;
Expand All @@ -1712,6 +1712,25 @@ export const __unstableSetEditorMode =
}
}

// Insert the provided logic here
if ( mode === 'simple' ) {
dispatch.updateBlockAttributes( sectionClientIds, {
templateLock: 'contentOnly',
} );

sectionClientIds.forEach( ( clientId ) => {
dispatch.setBlockEditingMode( clientId, 'contentOnly' );
} );
} else {
dispatch.updateBlockAttributes( sectionClientIds, {
templateLock: null,
} );

sectionClientIds.forEach( ( clientId ) => {
dispatch.unsetBlockEditingMode( clientId );
} );
}

dispatch( { type: 'SET_EDITOR_MODE', mode } );

switch ( mode ) {
Expand Down

0 comments on commit 8ef39a7

Please sign in to comment.