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

Fix error and improve privacy of sectionRootClientId setting #65000

Merged
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 { unlock } from '../../lock-unlock';
import { sectionRootClientIdKey } from '../../store/private-keys';

function ZoomOutModeInserters() {
const [ isReady, setIsReady ] = useState( false );
Expand All @@ -33,7 +33,7 @@ function ZoomOutModeInserters() {
getHoveredBlockClientId,
isBlockInsertionPointVisible,
} = select( blockEditorStore );
const { sectionRootClientId: root } = unlock( getSettings() );
const { [ sectionRootClientIdKey ]: root } = getSettings();
return {
hasSelection: !! getSelectionStart().clientId,
blockInsertionPoint: getBlockInsertionPoint(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ 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 @@ -225,7 +226,8 @@ 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 { sectionRootClientId } = unlock( getSettings() );
const { [ sectionRootClientIdKey ]: sectionRootClientId } =
getSettings();

_isDropZoneDisabled = clientId !== sectionRootClientId;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ 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 @@ -360,7 +361,8 @@ export default function useBlockDropZone( {
return;
}

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

// 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
2 changes: 2 additions & 0 deletions packages/block-editor/src/private-apis.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import {
reusableBlocksSelectKey,
globalStylesDataKey,
globalStylesLinksDataKey,
sectionRootClientIdKey,
} from './store/private-keys';
import { requiresWrapperOnCopy } from './components/writing-flow/utils';
import { PrivateRichText } from './components/rich-text/';
Expand Down Expand Up @@ -94,4 +95,5 @@ lock( privateApis, {
__unstableBlockStyleVariationOverridesWithConfig,
setBackgroundStyleDefaults,
useBlockBindingsUtils,
sectionRootClientIdKey,
} );
10 changes: 6 additions & 4 deletions packages/block-editor/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ import {
privateRemoveBlocks,
} from './private-actions';
import { STORE_NAME } from './constants';
import { unlock } from '../lock-unlock';

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

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

Expand Down Expand Up @@ -1674,9 +1675,10 @@ export const __unstableSetEditorMode =
// When switching to zoom-out mode, we need to select the parent section
if ( mode === 'zoom-out' ) {
const firstSelectedClientId = select.getBlockSelectionStart();
const { sectionRootClientId } = unlock(
registry.select( STORE_NAME ).getSettings()
);
const { [ sectionRootClientIdKey ]: sectionRootClientId } = registry
.select( STORE_NAME )
.getSettings();

if ( firstSelectedClientId ) {
let sectionClientId;

Expand Down
1 change: 1 addition & 0 deletions packages/block-editor/src/store/private-keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export const globalStylesDataKey = Symbol( 'globalStylesDataKey' );
export const globalStylesLinksDataKey = Symbol( 'globalStylesLinks' );
export const selectBlockPatternsKey = Symbol( 'selectBlockPatternsKey' );
export const reusableBlocksSelectKey = Symbol( 'reusableBlocksSelect' );
export const sectionRootClientIdKey = Symbol( 'sectionRootClientIdKey' );
16 changes: 11 additions & 5 deletions packages/block-editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ import {
getTemporarilyEditingFocusModeToRevert,
} from './private-selectors';

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

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

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

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

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

export default function InserterSidebar() {
const {
Expand All @@ -44,7 +46,8 @@ export default function InserterSidebar() {
const { getActiveComplementaryArea } = select( interfaceStore );
const getBlockSectionRootClientId = () => {
if ( __unstableGetEditorMode() === 'zoom-out' ) {
const { sectionRootClientId } = unlock( getSettings() );
const { [ sectionRootClientIdKey ]: sectionRootClientId } =
getSettings();
if ( sectionRootClientId ) {
return sectionRootClientId;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
import inserterMediaCategories from '../media-categories';
import { mediaUpload } from '../../utils';
import { store as editorStore } from '../../store';
import { lock, unlock } from '../../lock-unlock';
import { unlock } from '../../lock-unlock';
import { useGlobalStylesContext } from '../global-styles-provider';

const EMPTY_BLOCKS_LIST = [];
Expand Down Expand Up @@ -74,7 +74,6 @@ const BLOCK_EDITOR_SETTINGS = [
'postContentAttributes',
'postsPerPage',
'readOnly',
'sectionRootClientId',
'styles',
'titlePlaceholder',
'supportsLayout',
Expand All @@ -90,6 +89,7 @@ const {
globalStylesLinksDataKey,
selectBlockPatternsKey,
reusableBlocksSelectKey,
sectionRootClientIdKey,
} = unlock( privateApis );

/**
Expand Down Expand Up @@ -325,10 +325,9 @@ function useBlockEditorSettings( settings, postType, postId, renderingMode ) {
? [ [ 'core/navigation', {}, [] ] ]
: settings.template,
__experimentalSetIsInserterOpened: setIsInserterOpened,
[ sectionRootClientIdKey ]: sectionRootClientId,
};
lock( blockEditorSettings, {
sectionRootClientId,
} );

return blockEditorSettings;
}, [
allowedBlockTypes,
Expand Down
Loading