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

Introduce section container selection when assembling patterns (zoom out mode) #59249

Merged
merged 53 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from 47 commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
42cedd0
Zoom Out: Only enable the zoom-out mode within the main group
youknowriad Feb 21, 2024
5b8a035
Hide inner blocks if block has active overlay
getdave Feb 21, 2024
20b540e
Account for assembling pages by using post content block as section c…
getdave Feb 24, 2024
d191f7a
use maybedisable as a name for the disabling component
draganescu Feb 24, 2024
627ad0d
Refactor components to hooks
ajlende Feb 24, 2024
83d6680
rebase fix missed isZoomedOutView changes in trunk
draganescu Mar 15, 2024
ccffdf4
don't spread the client ids
scruffian Mar 27, 2024
5869fb8
create a new selector to find the root section block name
scruffian Mar 27, 2024
1e1db1f
correct some variable names, remove testing code
draganescu Mar 28, 2024
4c49ed7
remove useless call to function in the editor package
draganescu Mar 28, 2024
8b924fa
remove the useEffect in the editor component of edit site, use the ac…
draganescu Mar 28, 2024
ada6ff7
WIP Refactor hook to store action
ajlende Mar 28, 2024
53b2ef1
Fix copy/paste mistake
ajlende Mar 28, 2024
8906c33
combine state and modes in the reducer correctly into one iterable
draganescu Mar 29, 2024
d68bb3a
enable selection for no root section block
draganescu Mar 29, 2024
bb01ba4
enable section selection for sections when there is no root section c…
draganescu Mar 29, 2024
61b9078
Use registry.batch instead
ajlende Apr 1, 2024
a828c9c
Replace bulk actions with registry.batch
ajlende Apr 1, 2024
212d638
make section root selector and action private
draganescu Apr 1, 2024
4d8c43c
dispatch set block mode action
draganescu Apr 2, 2024
207126a
privatize the section root block client id selector
ajlende Apr 3, 2024
79d8af4
Remove unused reducers
ajlende Apr 2, 2024
120aa93
Refactor to group by if we have a sectionsContainerClientId
ajlende Apr 2, 2024
206e17d
Add wrapper to prevent running a bunch of loops in non-zoom out modes
ajlende Apr 2, 2024
90a4634
Move SET_EDITOR_MODE back to the end so prevMode works
ajlende Apr 3, 2024
c7a2cc0
Add comments for two main sections
ajlende Apr 3, 2024
9f3d560
Use constant instead of random empty string
ajlende Apr 3, 2024
b289f6d
Revert remaining block-list changes
ajlende Apr 3, 2024
cab518b
Refactor to use select
ajlende Apr 3, 2024
de412a9
Refactor to use ROOT_CONTAINER_CLIENT_ID instead of empty string
ajlende Apr 3, 2024
5162d0c
Move zoom-out logic to selector instead of action
ajlende Apr 3, 2024
37c477c
Remove registry.batch
ajlende Apr 3, 2024
80e67b5
Update JSDoc for ROOT_CONTAINER_CLIENT_ID defaults
ajlende Apr 3, 2024
3a59a10
Revert pattern edit.js
ajlende Apr 3, 2024
ebed90c
Fix selectors in selectors
ajlende Apr 3, 2024
2528ce9
Fix selection when entering zoom-out mode
ajlende Apr 4, 2024
c5816f2
Fix case wher getBlock returns null
ajlende Apr 4, 2024
fb41e16
Optimize selector some more
ajlende Apr 5, 2024
b1ccce7
Fix missing imports
ajlende Apr 5, 2024
5d0a81a
Revert ROOT_CONTAINER_CLIENT_ID rename
ajlende Apr 9, 2024
9e5640a
Revert list-view changes
ajlende Apr 9, 2024
dfc8f1e
Try moving post type logic into useBlockEditorSettings
ajlende Apr 9, 2024
d632363
Try make sectionRootClientId a private setting
ajlende Apr 9, 2024
4febcf8
Remove unused code
ajlende Apr 9, 2024
e5bafcc
Fix selected block in zoom out mode
ajlende Apr 9, 2024
29399fd
Rename _settings to blockEditorSettings
ajlende Apr 9, 2024
3af6938
Remove destructuing
ajlende Apr 9, 2024
a9529ac
make templates with no root also work
draganescu Apr 10, 2024
cb04e38
Check block list for active block overlay
ajlende Apr 10, 2024
6c2b78f
Pass rendering mode instead of type
ajlende Apr 10, 2024
640823f
Fix an issue where some selectors pass null as a default value instea…
ajlende Apr 10, 2024
fd8cf0a
Try disabling all non-children of the sectionRoot
ajlende Apr 10, 2024
b96f5a6
don't put overlay on all blocks in zoom out, just root and section ch…
draganescu Apr 11, 2024
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
28 changes: 22 additions & 6 deletions packages/block-editor/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ import {
__experimentalUpdateSettings,
privateRemoveBlocks,
} from './private-actions';
import { STORE_NAME } from './constants';
import { unlock } from '../lock-unlock';

/** @typedef {import('../components/use-on-block-drop/types').WPDropOperation} WPDropOperation */

Expand Down Expand Up @@ -1432,16 +1434,30 @@ export const setNavigationMode =
*/
export const __unstableSetEditorMode =
( mode ) =>
( { dispatch, select } ) => {
// When switching to zoom-out mode, we need to select the root block
( { dispatch, select, registry } ) => {
// When switching to zoom-out mode, we need to select the parent section
if ( mode === 'zoom-out' ) {
const firstSelectedClientId = select.getBlockSelectionStart();
if ( firstSelectedClientId ) {
dispatch.selectBlock(
select.getBlockHierarchyRootClientId(
const { sectionRootClientId } = unlock(
registry.select( STORE_NAME ).getSettings()
);
if ( sectionRootClientId ) {
const sectionClientIds =
select.getBlockOrder( sectionRootClientId );
if ( sectionClientIds ) {
const parents = select.getBlockParents(
firstSelectedClientId
)
);
const firstSectionClientId = parents.find( ( parent ) =>
sectionClientIds.includes( parent )
);
dispatch.selectBlock( firstSectionClientId );
}
} else if ( firstSelectedClientId ) {
const rootClientId = select.getBlockHierarchyRootClientId(
firstSelectedClientId
);
dispatch.selectBlock( rootClientId );
}
}

Expand Down
26 changes: 21 additions & 5 deletions packages/block-editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -2814,11 +2814,7 @@ export function __unstableHasActiveBlockOverlayActive( state, clientId ) {
const editorMode = __unstableGetEditorMode( state );

// In zoom-out mode, the block overlay is always active for top level blocks.
if (
editorMode === 'zoom-out' &&
clientId &&
! getBlockRootClientId( state, clientId )
) {
if ( editorMode === 'zoom-out' && clientId ) {
return true;
draganescu marked this conversation as resolved.
Show resolved Hide resolved
}

Expand Down Expand Up @@ -2891,6 +2887,26 @@ export function __unstableIsWithinBlockOverlay( state, clientId ) {
export const getBlockEditingMode = createRegistrySelector(
( select ) =>
( state, clientId = '' ) => {
// In zoom-out mode, override the behavior set by
// __unstableSetBlockEditingMode to only allow editing the top-level
// sections.
const editorMode = __unstableGetEditorMode( state );
if ( editorMode === 'zoom-out' ) {
if ( clientId === '' /* ROOT_CONTAINER_CLIENT_ID */ ) {
return 'disabled';
}

const { sectionRootClientId } = unlock( getSettings( state ) );
if ( clientId === sectionRootClientId ) {
return 'contentOnly';
}

const sectionsClientIds = getBlockOrder( state );
if ( sectionsClientIds.includes( clientId ) ) {
return 'default';
draganescu marked this conversation as resolved.
Show resolved Hide resolved
}
}

const blockEditingMode = state.blockEditingModes.get( clientId );
if ( blockEditingMode ) {
return blockEditingMode;
Expand Down
8 changes: 4 additions & 4 deletions packages/editor/src/components/provider/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,11 @@ export const ExperimentalEditorProvider = withRegistryProvider(
},
[]
);
const { id, type } = rootLevelPost;
const blockEditorSettings = useBlockEditorSettings(
editorSettings,
type,
id
rootLevelPost.type,
rootLevelPost.id,
post.type
);
const [ blocks, onInput, onChange ] = useBlockEditorProps(
post,
Expand Down Expand Up @@ -262,7 +262,7 @@ export const ExperimentalEditorProvider = withRegistryProvider(
{ mode === 'template-locked' && (
<DisableNonPageContentBlocks />
) }
{ type === 'wp_navigation' && (
{ rootLevelPost.type === 'wp_navigation' && (
<NavigationBlockEditingMode />
) }
<BlockRemovalWarnings />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,18 @@ import { __ } from '@wordpress/i18n';
import { store as preferencesStore } from '@wordpress/preferences';
import { useViewportMatch } from '@wordpress/compose';
import { store as blocksStore } from '@wordpress/blocks';
import { privateApis } from '@wordpress/block-editor';
import {
privateApis,
store as blockEditorStore,
} from '@wordpress/block-editor';

/**
* Internal dependencies
*/
import inserterMediaCategories from '../media-categories';
import { mediaUpload } from '../../utils';
import { store as editorStore } from '../../store';
import { unlock } from '../../lock-unlock';
import { lock, unlock } from '../../lock-unlock';

const EMPTY_BLOCKS_LIST = [];

Expand Down Expand Up @@ -85,13 +88,14 @@ const BLOCK_EDITOR_SETTINGS = [
/**
* React hook used to compute the block editor settings to use for the post editor.
*
* @param {Object} settings EditorProvider settings prop.
* @param {string} postType Editor root level post type.
* @param {string} postId Editor root level post ID.
* @param {Object} settings EditorProvider settings prop.
* @param {string} postType Editor root level post type.
* @param {string} postId Editor root level post ID.
* @param {string} contextPostType The post type of the edited post.
*
* @return {Object} Block Editor Settings.
*/
function useBlockEditorSettings( settings, postType, postId ) {
function useBlockEditorSettings( settings, postType, postId, contextPostType ) {
ajlende marked this conversation as resolved.
Show resolved Hide resolved
draganescu marked this conversation as resolved.
Show resolved Hide resolved
const isLargeViewport = useViewportMatch( 'medium' );
const {
allowRightClickOverrides,
Expand All @@ -108,6 +112,7 @@ function useBlockEditorSettings( settings, postType, postId ) {
pageForPosts,
userPatternCategories,
restBlockPatternCategories,
sectionRootClientId,
} = useSelect(
( select ) => {
const {
Expand All @@ -119,10 +124,31 @@ function useBlockEditorSettings( settings, postType, postId ) {
} = select( coreStore );
const { get } = select( preferencesStore );
const { getBlockTypes } = select( blocksStore );
const { getBlocksByName, getBlockAttributes } =
select( blockEditorStore );
const siteSettings = canUser( 'read', 'settings' )
? getEntityRecord( 'root', 'site' )
: undefined;

function getSectionRootBlock() {
if ( ! contextPostType ) {
return null;
}

if ( [ 'post', 'page' ].includes( contextPostType ) ) {
Copy link
Contributor

@ajlende ajlende Apr 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copied from #60609 (comment).

These are not the only content types so I'd prefer if remove this. I'd rather do the following algorithm instead:

1- If mode = "template-locked": look for post-content block
2- If mode is not "template-locked', look for the "main" group block
3- fallback to top level. (undefined section container I guess)

return (
getBlocksByName( 'core/post-content' )?.[ 0 ] ?? null
);
}

return (
getBlocksByName( 'core/group' ).find(
( clientId ) =>
getBlockAttributes( clientId )?.tagName === 'main'
) ?? null
);
}

return {
allowRightClickOverrides: get(
'core',
Expand All @@ -146,9 +172,10 @@ function useBlockEditorSettings( settings, postType, postId ) {
pageForPosts: siteSettings?.page_for_posts,
userPatternCategories: getUserPatternCategories(),
restBlockPatternCategories: getBlockPatternCategories(),
sectionRootClientId: getSectionRootBlock(),
};
},
[ postType, postId, isLargeViewport ]
[ postType, postId, isLargeViewport, contextPostType ]
);

const settingsBlockPatterns =
Expand Down Expand Up @@ -230,8 +257,8 @@ function useBlockEditorSettings( settings, postType, postId ) {

const forceDisableFocusMode = settings.focusMode === false;

return useMemo(
() => ( {
return useMemo( () => {
const blockEditorSettings = {
...Object.fromEntries(
Object.entries( settings ).filter( ( [ key ] ) =>
BLOCK_EDITOR_SETTINGS.includes( key )
Expand Down Expand Up @@ -278,30 +305,34 @@ function useBlockEditorSettings( settings, postType, postId ) {
? [ [ 'core/navigation', {}, [] ] ]
: settings.template,
__experimentalSetIsInserterOpened: setIsInserterOpened,
} ),
[
allowedBlockTypes,
allowRightClickOverrides,
focusMode,
forceDisableFocusMode,
hasFixedToolbar,
isDistractionFree,
keepCaretInsideBlock,
settings,
hasUploadPermissions,
userPatternCategories,
blockPatterns,
blockPatternCategories,
canUseUnfilteredHTML,
undo,
createPageEntity,
userCanCreatePages,
pageOnFront,
pageForPosts,
postType,
setIsInserterOpened,
]
);
};
lock( blockEditorSettings, {
sectionRootClientId,
} );
draganescu marked this conversation as resolved.
Show resolved Hide resolved
return blockEditorSettings;
}, [
allowedBlockTypes,
allowRightClickOverrides,
focusMode,
forceDisableFocusMode,
hasFixedToolbar,
isDistractionFree,
keepCaretInsideBlock,
settings,
hasUploadPermissions,
userPatternCategories,
blockPatterns,
blockPatternCategories,
canUseUnfilteredHTML,
undo,
createPageEntity,
userCanCreatePages,
pageOnFront,
pageForPosts,
postType,
setIsInserterOpened,
sectionRootClientId,
] );
}

export default useBlockEditorSettings;
Loading