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

Remove alignments from the root level of the site editor #30079

Merged
merged 1 commit into from
Mar 23, 2021
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 @@ -3,7 +3,6 @@
*/
import { __ } from '@wordpress/i18n';
import { DropdownMenu, ToolbarGroup } from '@wordpress/components';
import { useSelect } from '@wordpress/data';
import {
positionCenter,
positionLeft,
Expand All @@ -15,8 +14,7 @@ import {
/**
* Internal dependencies
*/
import { useLayout } from '../block-list/layout';
import { store as blockEditorStore } from '../../store';
import useAvailableAlignments from './use-available-alignments';

const BLOCK_ALIGNMENTS_CONTROLS = {
left: {
Expand All @@ -41,9 +39,7 @@ const BLOCK_ALIGNMENTS_CONTROLS = {
},
};

const DEFAULT_CONTROLS = [ 'left', 'center', 'right', 'wide', 'full' ];
const DEFAULT_CONTROL = 'center';
const WIDE_CONTROLS = [ 'wide', 'full' ];

const POPOVER_PROPS = {
isAlternate: true,
Expand All @@ -52,33 +48,12 @@ const POPOVER_PROPS = {
function BlockAlignmentUI( {
value,
onChange,
controls = DEFAULT_CONTROLS,
controls,
isToolbar,
isCollapsed = true,
isToolbarButton = true,
} ) {
const { wideControlsEnabled = false } = useSelect( ( select ) => {
const { getSettings } = select( blockEditorStore );
const settings = getSettings();
return {
wideControlsEnabled: settings.alignWide,
};
}, [] );
const layout = useLayout();
const supportsAlignments = layout.type === 'default';

if ( ! supportsAlignments ) {
return null;
}
const { alignments: availableAlignments = DEFAULT_CONTROLS } = layout;
const enabledControls = controls.filter(
( control ) =>
( layout.alignments || // Ignore the global wideAlignment check if the layout explicitely defines alignments.
wideControlsEnabled ||
! WIDE_CONTROLS.includes( control ) ) &&
availableAlignments.includes( control )
);

const enabledControls = useAvailableAlignments( controls );
if ( enabledControls.length === 0 ) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* WordPress dependencies
*/
import { useSelect } from '@wordpress/data';

/**
* Internal dependencies
*/
import { useLayout } from '../block-list/layout';
import { store as blockEditorStore } from '../../store';

const DEFAULT_CONTROLS = [ 'left', 'center', 'right', 'wide', 'full' ];
const WIDE_CONTROLS = [ 'wide', 'full' ];

export default function useAvailableAlignments( controls = DEFAULT_CONTROLS ) {
const { wideControlsEnabled = false } = useSelect( ( select ) => {
const { getSettings } = select( blockEditorStore );
const settings = getSettings();
return {
wideControlsEnabled: settings.alignWide,
};
}, [] );
const layout = useLayout();
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we can do a destructuring here

const { type, alignments: availableAlignments = DEFAULT_CONTROLS } = useLayout();

const supportsAlignments = layout.type === 'default';

if ( ! supportsAlignments ) {
return [];
}
const { alignments: availableAlignments = DEFAULT_CONTROLS } = layout;
const enabledControls = controls.filter(
( control ) =>
( layout.alignments || // Ignore the global wideAlignment check if the layout explicitely defines alignments.
youknowriad marked this conversation as resolved.
Show resolved Hide resolved
wideControlsEnabled ||
! WIDE_CONTROLS.includes( control ) ) &&
availableAlignments.includes( control )
);

return enabledControls;
}
8 changes: 6 additions & 2 deletions packages/block-editor/src/hooks/align.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { useSelect } from '@wordpress/data';
*/
import { BlockControls, BlockAlignmentControl } from '../components';
import { store as blockEditorStore } from '../store';
import useAvailableAlignments from '../components/block-alignment-control/use-available-alignments';

/**
* An array which includes all possible valid alignments,
Expand Down Expand Up @@ -116,14 +117,17 @@ export function addAttribute( settings ) {
export const withToolbarControls = createHigherOrderComponent(
( BlockEdit ) => ( props ) => {
const { name: blockName } = props;
// Compute valid alignments without taking into account,
// Compute the block allowed alignments without taking into account,
// if the theme supports wide alignments or not
// and without checking the layout for availble alignments.
// BlockAlignmentToolbar takes both of these into account.
const validAlignments = getValidAlignments(
const blockAllowedAlignments = getValidAlignments(
getBlockSupport( blockName, 'align' ),
hasBlockSupport( blockName, 'alignWide', true )
);
const validAlignments = useAvailableAlignments(
blockAllowedAlignments
);

const updateAlignment = ( nextAlign ) => {
if ( ! nextAlign ) {
Expand Down
9 changes: 8 additions & 1 deletion packages/edit-site/src/components/block-editor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,14 @@ export default function BlockEditor( { setIsInserterOpen } ) {
>
<DropZoneProvider>
<WritingFlow>
<BlockList className="edit-site-block-editor__block-list" />
<BlockList
className="edit-site-block-editor__block-list"
__experimentalLayout={ {
type: 'default',
// At the root level of the site editor, no alignments should be allowed.
alignments: [],
} }
/>
</WritingFlow>
</DropZoneProvider>
</Iframe>
Expand Down