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

Sidebar Tabs: Use editor settings to override display #46321

Merged
merged 3 commits into from
Dec 6, 2022
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 @@ -168,6 +168,12 @@ public function get_item_schema() {
'context' => array( 'mobile' ),
),

'__experimentalBlockInspectorTabs' => array(
'description' => __( 'Block inspector tab display overrides.', 'gutenberg' ),
'type' => 'object',
'context' => array( 'post-editor', 'site-editor', 'widgets-editor' ),
),

'alignWide' => array(
'description' => __( 'Enable/Disable Wide/Full Alignments.', 'gutenberg' ),
'type' => 'boolean',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,7 @@ const BlockInspector = ( { showNoBlockSelectedMessage = true } ) => {
}, [] );

const availableTabs = useInspectorControlsTabs( blockType?.name );
const showTabs =
window?.__experimentalEnableBlockInspectorTabs &&
availableTabs.length > 1;
const showTabs = availableTabs.length > 1;
aaronrobertshaw marked this conversation as resolved.
Show resolved Hide resolved

if ( count > 1 ) {
return (
Expand Down Expand Up @@ -243,9 +241,7 @@ const BlockInspector = ( { showNoBlockSelectedMessage = true } ) => {

const BlockInspectorSingleBlock = ( { clientId, blockName } ) => {
const availableTabs = useInspectorControlsTabs( blockName );
const showTabs =
window?.__experimentalEnableBlockInspectorTabs &&
availableTabs.length > 1;
const showTabs = availableTabs.length > 1;
aaronrobertshaw marked this conversation as resolved.
Show resolved Hide resolved

const hasBlockStyles = useSelect(
( select ) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* WordPress dependencies
*/
import { __experimentalUseSlotFills as useSlotFills } from '@wordpress/components';
import { useSelect } from '@wordpress/data';

/**
* Internal dependencies
Expand All @@ -10,6 +11,27 @@ import InspectorControlsGroups from '../inspector-controls/groups';
import useIsListViewTabDisabled from './use-is-list-view-tab-disabled';
import { InspectorAdvancedControls } from '../inspector-controls';
import { TAB_LIST_VIEW, TAB_SETTINGS, TAB_STYLES } from './utils';
import { store as blockEditorStore } from '../../store';

function getShowTabs( blockName, tabSettings = {} ) {
// Don't allow settings to force the display of tabs if the block inspector
// tabs experiment hasn't been opted into.
if ( ! window?.__experimentalEnableBlockInspectorTabs ) {
return false;
}

// Block specific setting takes precedence over generic default.
if ( tabSettings[ blockName ] !== undefined ) {
return tabSettings[ blockName ];
}

// Use generic default if set over the Gutenberg experiment option.
if ( tabSettings.default !== undefined ) {
return tabSettings.default;
}

return true;
}

export default function useInspectorControlsTabs( blockName ) {
const tabs = [];
Expand Down Expand Up @@ -54,5 +76,12 @@ export default function useInspectorControlsTabs( blockName ) {
tabs.push( TAB_SETTINGS );
}

return tabs;
const tabSettings = useSelect( ( select ) => {
return select( blockEditorStore ).getSettings()
.__experimentalBlockInspectorTabs;
}, [] );

const showTabs = getShowTabs( blockName, tabSettings );

return showTabs ? tabs : [];
aaronrobertshaw marked this conversation as resolved.
Show resolved Hide resolved
}
23 changes: 23 additions & 0 deletions packages/block-library/src/navigation-link/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -344,3 +344,26 @@ function register_block_core_navigation_link() {
);
}
add_action( 'init', 'register_block_core_navigation_link' );

/**
* Disables the display of tabs for the Navigation Link block.
aaronrobertshaw marked this conversation as resolved.
Show resolved Hide resolved
*
* @param array $settings Default editor settings.
* @return array Filtered editor settings.
*/
function gutenberg_disable_tabs_for_navigation_link_block( $settings ) {
$current_tab_settings = _wp_array_get(
$settings,
array( '__experimentalBlockInspectorTabs' ),
array()
);

$settings['__experimentalBlockInspectorTabs'] = array_merge(
$current_tab_settings,
array( 'core/navigation-link' => false )
aaronrobertshaw marked this conversation as resolved.
Show resolved Hide resolved
);

return $settings;
}

add_filter( 'block_editor_settings_all', 'gutenberg_disable_tabs_for_navigation_link_block' );
23 changes: 23 additions & 0 deletions packages/block-library/src/navigation-submenu/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -289,3 +289,26 @@ function register_block_core_navigation_submenu() {
);
}
add_action( 'init', 'register_block_core_navigation_submenu' );

/**
* Disables the display of tabs for the Navigation Submenu block.
*
* @param array $settings Default editor settings.
* @return array Filtered editor settings.
*/
function gutenberg_disable_tabs_for_navigation_submenu_block( $settings ) {
aaronrobertshaw marked this conversation as resolved.
Show resolved Hide resolved
$current_tab_settings = _wp_array_get(
$settings,
array( '__experimentalBlockInspectorTabs' ),
array()
);

$settings['__experimentalBlockInspectorTabs'] = array_merge(
$current_tab_settings,
array( 'core/navigation-submenu' => false )
);

return $settings;
}

add_filter( 'block_editor_settings_all', 'gutenberg_disable_tabs_for_navigation_submenu_block' );
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ function useBlockEditorSettings( settings, hasTemplate ) {
Object.entries( settings ).filter( ( [ key ] ) =>
[
'__experimentalBlockDirectory',
'__experimentalBlockInspectorTabs',
'__experimentalDiscussionSettings',
'__experimentalFeatures',
'__experimentalPreferredStyleVariations',
Expand Down