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

Add selector for getting section root clientId #65001

Merged
merged 4 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { useEffect, useState } from '@wordpress/element';
import BlockPopoverInbetween from '../block-popover/inbetween';
import ZoomOutModeInserterButton from './zoom-out-mode-inserter-button';
import { store as blockEditorStore } from '../../store';
import { sectionRootClientIdKey } from '../../store/private-keys';
import { unlock } from '../../lock-unlock';

function ZoomOutModeInserters() {
const [ isReady, setIsReady ] = useState( false );
Expand All @@ -32,8 +32,11 @@ function ZoomOutModeInserters() {
getSelectedBlockClientId,
getHoveredBlockClientId,
isBlockInsertionPointVisible,
} = select( blockEditorStore );
const { [ sectionRootClientIdKey ]: root } = getSettings();
getSectionRootClientId,
} = unlock( select( blockEditorStore ) );

const root = getSectionRootClientId();

return {
hasSelection: !! getSelectionStart().clientId,
blockInsertionPoint: getBlockInsertionPoint(),
Expand Down
6 changes: 2 additions & 4 deletions packages/block-editor/src/components/inner-blocks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import useBlockSync from '../provider/use-block-sync';
import { store as blockEditorStore } from '../../store';
import useBlockDropZone from '../use-block-drop-zone';
import { unlock } from '../../lock-unlock';
import { sectionRootClientIdKey } from '../../store/private-keys';

const EMPTY_OBJECT = {};

Expand Down Expand Up @@ -204,7 +203,7 @@ export function useInnerBlocksProps( props = {}, options = {} ) {
getBlockEditingMode,
getBlockSettings,
isDragging,
getSettings,
getSectionRootClientId,
} = unlock( select( blockEditorStore ) );
let _isDropZoneDisabled;

Expand All @@ -226,8 +225,7 @@ export function useInnerBlocksProps( props = {}, options = {} ) {
// In zoom out mode, we want to disable the drop zone for the sections.
// The inner blocks belonging to the section drop zone is
// already disabled by the blocks themselves being disabled.
const { [ sectionRootClientIdKey ]: sectionRootClientId } =
getSettings();
const sectionRootClientId = getSectionRootClientId();

_isDropZoneDisabled = clientId !== sectionRootClientId;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import {
} from '../../utils/math';
import { store as blockEditorStore } from '../../store';
import { unlock } from '../../lock-unlock';
import { sectionRootClientIdKey } from '../../store/private-keys';

const THRESHOLD_DISTANCE = 30;
const MINIMUM_HEIGHT_FOR_THRESHOLD = 120;
Expand Down Expand Up @@ -314,8 +313,8 @@ export default function useBlockDropZone( {
getAllowedBlocks,
isDragging,
isGroupable,
getSettings,
isZoomOutMode,
getSectionRootClientId,
} = unlock( useSelect( blockEditorStore ) );
const {
showInsertionPoint,
Expand Down Expand Up @@ -361,8 +360,7 @@ export default function useBlockDropZone( {
return;
}

const { [ sectionRootClientIdKey ]: sectionRootClientId } =
getSettings();
const sectionRootClientId = getSectionRootClientId();

// In Zoom Out mode, if the target is not the section root provided by settings then
// do not allow dropping as the drop target is not within the root (that which is
Expand Down Expand Up @@ -494,6 +492,8 @@ export default function useBlockDropZone( {
getBlockNamesByClientId,
getDraggedBlockClientIds,
getBlockType,
getSectionRootClientId,
isZoomOutMode,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This isZoomOutMode dep isn't new, just the getSectionRootClientId above.

getBlocks,
getBlockListSettings,
dropZoneElement,
Expand All @@ -506,8 +506,6 @@ export default function useBlockDropZone( {
isGroupable,
getBlockVariations,
getGroupingBlockName,
getSettings,
isZoomOutMode,
]
),
200
Expand Down
10 changes: 3 additions & 7 deletions packages/block-editor/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ import {
__experimentalUpdateSettings,
privateRemoveBlocks,
} from './private-actions';
import { STORE_NAME } from './constants';

import { sectionRootClientIdKey } from './private-keys';

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

Expand Down Expand Up @@ -1671,13 +1668,12 @@ export const setNavigationMode =
*/
export const __unstableSetEditorMode =
( mode ) =>
( { dispatch, select, registry } ) => {
( { dispatch, select } ) => {
// 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();

const sectionRootClientId = select.getSectionRootClientId();

if ( firstSelectedClientId ) {
let sectionClientId;
Expand Down
5 changes: 5 additions & 0 deletions packages/block-editor/src/store/private-selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { unlock } from '../lock-unlock';
import {
selectBlockPatternsKey,
reusableBlocksSelectKey,
sectionRootClientIdKey,
} from './private-keys';

export { getBlockSettings } from './get-block-settings';
Expand Down Expand Up @@ -543,3 +544,7 @@ export const getBlockStyles = createSelector(
export function isZoomOutMode( state ) {
return state.editorMode === 'zoom-out';
}

export function getSectionRootClientId( state ) {
return state.settings?.[ sectionRootClientIdKey ];
}
Comment on lines +548 to +550
Copy link
Member

Choose a reason for hiding this comment

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

Having a JSDoc block could be helpful in the future.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeh we really need to do that.

13 changes: 4 additions & 9 deletions packages/block-editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,9 @@ import {
getContentLockingParent,
getTemporarilyEditingAsBlocks,
getTemporarilyEditingFocusModeToRevert,
getSectionRootClientId,
} from './private-selectors';

import { sectionRootClientIdKey } from './private-keys';

/**
* A block selection object.
*
Expand Down Expand Up @@ -2061,9 +2060,7 @@ export const getInserterItems = createRegistrySelector( ( select ) =>
let sectionRootClientId;
try {
sectionRootClientId =
getSettings( state )[
sectionRootClientIdKey
];
getSectionRootClientId( state );
} catch ( e ) {}
if (
sectionRootClientId &&
Expand Down Expand Up @@ -2841,8 +2838,7 @@ export function __unstableHasActiveBlockOverlayActive( state, clientId ) {

// In zoom-out mode, the block overlay is always active for section level blocks.
if ( editorMode === 'zoom-out' ) {
const { [ sectionRootClientIdKey ]: sectionRootClientId } =
getSettings( state );
const sectionRootClientId = getSectionRootClientId( state );
if ( sectionRootClientId ) {
const sectionClientIds = getBlockOrder(
state,
Expand Down Expand Up @@ -2935,8 +2931,7 @@ export const getBlockEditingMode = createRegistrySelector(
// sections.
const editorMode = __unstableGetEditorMode( state );
if ( editorMode === 'zoom-out' ) {
const { [ sectionRootClientIdKey ]: sectionRootClientId } =
getSettings( state );
const sectionRootClientId = getSectionRootClientId( state );

if ( clientId === '' /* ROOT_CONTAINER_CLIENT_ID */ ) {
return sectionRootClientId ? 'disabled' : 'contentOnly';
Expand Down
12 changes: 5 additions & 7 deletions packages/editor/src/components/inserter-sidebar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ import { store as interfaceStore } from '@wordpress/interface';
import { unlock } from '../../lock-unlock';
import { store as editorStore } from '../../store';

const { PrivateInserterLibrary, sectionRootClientIdKey } = unlock(
blockEditorPrivateApis
);
const { PrivateInserterLibrary } = unlock( blockEditorPrivateApis );

export default function InserterSidebar() {
const {
Expand All @@ -40,14 +38,14 @@ export default function InserterSidebar() {
getBlockInsertionPoint,
getBlockRootClientId,
__unstableGetEditorMode,
getSettings,
} = select( blockEditorStore );
getSectionRootClientId,
} = unlock( select( blockEditorStore ) );
const { get } = select( preferencesStore );
const { getActiveComplementaryArea } = select( interfaceStore );
const getBlockSectionRootClientId = () => {
if ( __unstableGetEditorMode() === 'zoom-out' ) {
const { [ sectionRootClientIdKey ]: sectionRootClientId } =
getSettings();
const sectionRootClientId = getSectionRootClientId();

if ( sectionRootClientId ) {
return sectionRootClientId;
}
Expand Down
Loading