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

Zoom Out Inserter: enable Dynamic Labels #68310

Open
wants to merge 3 commits into
base: trunk
Choose a base branch
from
Open
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
24 changes: 24 additions & 0 deletions docs/reference-guides/data/data-core-block-editor.md
Original file line number Diff line number Diff line change
Expand Up @@ -827,6 +827,18 @@ _Returns_

- `0|-1|null`: Initial position.

### getSelectedTab

Returns the selected tab.

_Parameters_

- _state_ `Object`: Global application state.

_Returns_

- `string`: The selected tab.

### getSelectionEnd

Returns the current selection end block client ID, attribute key and text offset.
Expand Down Expand Up @@ -1721,6 +1733,18 @@ _Parameters_

- _isNavigationMode_ `boolean`: Enable/Disable navigation mode.

### setSelectedTab

Sets the selected tab.

_Parameters_

- _tab_ `string`: The selected tab. Takes `patterns`, `blocks` or `media`.

_Returns_

- `Object`: Action object.

### setTemplateValidity

Action that resets the template validity.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
import { useReducedMotion } from '@wordpress/compose';
import { useSelect } from '@wordpress/data';
import { useState } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import { __, sprintf } from '@wordpress/i18n';

/**
* Internal dependencies
Expand All @@ -34,6 +34,7 @@ export function ZoomOutSeparator( {
blockInsertionPointVisible,
blockInsertionPoint,
blocksBeingDragged,
selectedTab,
} = useSelect( ( select ) => {
const {
getInsertionPoint,
Expand All @@ -42,6 +43,7 @@ export function ZoomOutSeparator( {
isBlockInsertionPointVisible,
getBlockInsertionPoint,
getDraggedBlockClientIds,
getSelectedTab,
} = unlock( select( blockEditorStore ) );

const root = getSectionRootClientId();
Expand All @@ -54,6 +56,7 @@ export function ZoomOutSeparator( {
blockInsertionPoint: getBlockInsertionPoint(),
blockInsertionPointVisible: isBlockInsertionPointVisible(),
blocksBeingDragged: getDraggedBlockClientIds(),
selectedTab: getSelectedTab(),
};
}, [] );

Expand Down Expand Up @@ -162,7 +165,11 @@ export function ZoomOutSeparator( {
delay: 0.125,
} }
>
{ __( 'Drop pattern.' ) }
{ sprintf(
/* translators: %s: tab name (e.g. "pattern" or "block") */
__( 'Drop %s.' ),
selectedTab
) }
</motion.div>
</motion.div>
) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,21 @@ import clsx from 'clsx';
/**
* WordPress dependencies
*/
import { useSelect } from '@wordpress/data';
import { Button } from '@wordpress/components';
import { plus } from '@wordpress/icons';
import { _x } from '@wordpress/i18n';
import { _x, sprintf } from '@wordpress/i18n';

/**
* Internal dependencies
*/
import { store as blockEditorStore } from '../../store';

function ZoomOutModeInserterButton( { onClick } ) {
const selectedTab = useSelect( ( select ) => {
return select( blockEditorStore ).getSelectedTab();
}, [] );

return (
<Button
variant="primary"
Expand All @@ -21,9 +31,10 @@ function ZoomOutModeInserterButton( { onClick } ) {
'block-editor-block-tools__zoom-out-mode-inserter-button'
) }
onClick={ onClick }
label={ _x(
'Add pattern',
'Generic label for pattern inserter button'
label={ sprintf(
/* translators: %s: tab name (e.g. "pattern" or "block") */
_x( 'Add %s', 'Label for zoom out mode inserter button.' ),
selectedTab
) }
/>
);
Expand Down
10 changes: 9 additions & 1 deletion packages/block-editor/src/components/inserter/menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ import {
useCallback,
useMemo,
useRef,
useEffect,
useLayoutEffect,
} from '@wordpress/element';
import { VisuallyHidden, SearchControl, Popover } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { useDebouncedInput, useViewportMatch } from '@wordpress/compose';
import { useSelect } from '@wordpress/data';
import { useDispatch, useSelect } from '@wordpress/data';

/**
* Internal dependencies
Expand Down Expand Up @@ -85,7 +86,14 @@ function InserterMenu(

return 'blocks';
}

const [ selectedTab, setSelectedTab ] = useState( getInitialTab() );
const { setSelectedTab: setInserterSelectedTab } =
useDispatch( blockEditorStore );

useEffect( () => {
setInserterSelectedTab( selectedTab );
}, [ selectedTab, setInserterSelectedTab ] );

const shouldUseZoomOut =
hasSectionRootClientId &&
Expand Down
14 changes: 14 additions & 0 deletions packages/block-editor/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -2142,3 +2142,17 @@ export function unsetBlockEditingMode( clientId = '' ) {
clientId,
};
}

/**
* Sets the selected tab.
*
* @param {string} tab The selected tab. Takes `patterns`, `blocks` or `media`.
*
* @return {Object} Action object.
*/
export function setSelectedTab( tab ) {
return {
type: 'SET_SELECTED_TAB',
tab,
};
}
18 changes: 18 additions & 0 deletions packages/block-editor/src/store/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2110,6 +2110,23 @@ export function insertionPoint( state = null, action ) {
return state;
}

/**
* Reducer setting the selected tab
*
* @param {string} state Current state. Defaults to 'patterns'.
* @param {Object} action Dispatched action.
*
* @return {string} Updated state.
*/
export function selectedTab( state = 'patterns', action ) {
switch ( action.type ) {
case 'SET_SELECTED_TAB':
return action.tab;
}

return state;
}

const combinedReducers = combineReducers( {
blocks,
isDragging,
Expand Down Expand Up @@ -2143,6 +2160,7 @@ const combinedReducers = combineReducers( {
registeredInserterMediaCategories,
hoveredBlockClientId,
zoomLevel,
selectedTab,
} );

/**
Expand Down
11 changes: 11 additions & 0 deletions packages/block-editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -3153,6 +3153,17 @@ export const isGroupable = createRegistrySelector(
}
);

/**
* Returns the selected tab.
*
* @param {Object} state Global application state.
*
* @return {string} The selected tab.
*/
export function getSelectedTab( state ) {
return state.selectedTab;
}

/**
* DO-NOT-USE in production.
* This selector is created for internal/experimental only usage and may be
Expand Down
Loading