From 730a31e018cbad506fd3024053d1676a36e38181 Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Wed, 28 Aug 2024 12:05:59 +0100 Subject: [PATCH] Add Simple mode --- .../src/components/tool-selector/index.js | 81 ++++++++++++++++--- 1 file changed, 70 insertions(+), 11 deletions(-) diff --git a/packages/block-editor/src/components/tool-selector/index.js b/packages/block-editor/src/components/tool-selector/index.js index 4ec777a911cc73..0a51fbd4325f73 100644 --- a/packages/block-editor/src/components/tool-selector/index.js +++ b/packages/block-editor/src/components/tool-selector/index.js @@ -11,8 +11,8 @@ import { } from '@wordpress/components'; import { __ } from '@wordpress/i18n'; import { useSelect, useDispatch } from '@wordpress/data'; -import { forwardRef } from '@wordpress/element'; -import { Icon, edit as editIcon } from '@wordpress/icons'; +import { forwardRef, useState } from '@wordpress/element'; +import { Icon, edit as editIcon, brush as brushIcon } from '@wordpress/icons'; /** * Internal dependencies @@ -31,11 +31,28 @@ const selectIcon = ( ); function ToolSelector( props, ref ) { - const mode = useSelect( - ( select ) => select( blockEditorStore ).__unstableGetEditorMode(), - [] - ); - const { __unstableSetEditorMode } = useDispatch( blockEditorStore ); + const [ isSimpleMode, setIsSimpleMode ] = useState( false ); + const [ originalTemplateLocks, setOriginalTemplateLocks ] = useState( {} ); + const { mode, blocksWithinMainBlockClientIds, getBlockAttributes } = + useSelect( ( select ) => { + const { + __unstableGetEditorMode, + getBlockOrder, + getBlockAttributes: _getBlockAttributes, + } = select( blockEditorStore ); + + const mainBlockClientId = getBlockOrder()[ 1 ]; + + return { + mode: __unstableGetEditorMode(), + blocksWithinMainBlockClientIds: + getBlockOrder( mainBlockClientId ), + getBlockAttributes: _getBlockAttributes, + }; + }, [] ); + + const { __unstableSetEditorMode, updateBlockAttributes } = + useDispatch( blockEditorStore ); return ( { + if ( newMode === 'simple' ) { + const originalLocks = {}; + blocksWithinMainBlockClientIds.forEach( + ( clientId ) => { + const attributes = + getBlockAttributes( clientId ); + originalLocks[ clientId ] = + attributes.templateLock; + } + ); + setOriginalTemplateLocks( originalLocks ); + updateBlockAttributes( + blocksWithinMainBlockClientIds, + { + templateLock: 'contentOnly', + } + ); + __unstableSetEditorMode( 'edit' ); + setIsSimpleMode( true ); + } else { + // Restore the original templateLock attributes + blocksWithinMainBlockClientIds.forEach( + ( clientId ) => { + updateBlockAttributes( clientId, { + templateLock: + originalTemplateLocks[ + clientId + ], + } ); + } + ); + __unstableSetEditorMode( newMode ); + setIsSimpleMode( false ); + } + } } choices={ [ { value: 'edit', @@ -81,6 +131,15 @@ function ToolSelector( props, ref ) { ), }, + { + value: 'simple', + label: ( + <> + + { __( 'Simple' ) } + + ), + }, ] } />