Skip to content

Commit

Permalink
Fix: Show add pattern label when patterns are being prioritised.
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgefilipecosta committed Apr 25, 2022
1 parent c5b9ca1 commit 5157c12
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 16 deletions.
20 changes: 20 additions & 0 deletions packages/block-editor/src/components/inserter/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const defaultRenderToggle = ( {
blockTitle,
hasSingleBlockType,
toggleProps = {},
prioritizePatterns,
} ) => {
let label;
if ( hasSingleBlockType ) {
Expand All @@ -38,6 +39,8 @@ const defaultRenderToggle = ( {
_x( 'Add %s', 'directly add the only allowed block' ),
blockTitle
);
} else if ( prioritizePatterns ) {
label = __( 'Add pattern' );
} else {
label = _x( 'Add block', 'Generic label for block inserter button' );
}
Expand Down Expand Up @@ -106,6 +109,7 @@ class Inserter extends Component {
toggleProps,
hasItems,
renderToggle = defaultRenderToggle,
prioritizePatterns,
} = this.props;

return renderToggle( {
Expand All @@ -116,6 +120,7 @@ class Inserter extends Component {
hasSingleBlockType,
directInsertBlock,
toggleProps,
prioritizePatterns,
} );
}

Expand All @@ -138,6 +143,7 @@ class Inserter extends Component {
// This prop is experimental to give some time for the quick inserter to mature
// Feel free to make them stable after a few releases.
__experimentalIsQuick: isQuick,
prioritizePatterns,
} = this.props;

if ( isQuick ) {
Expand All @@ -149,6 +155,7 @@ class Inserter extends Component {
rootClientId={ rootClientId }
clientId={ clientId }
isAppender={ isAppender }
prioritizePatterns={ prioritizePatterns }
/>
);
}
Expand Down Expand Up @@ -206,7 +213,11 @@ export default compose( [
hasInserterItems,
__experimentalGetAllowedBlocks,
__experimentalGetDirectInsertBlock,
getBlockIndex,
getBlockCount,
getSettings,
} = select( blockEditorStore );

const { getBlockVariations } = select( blocksStore );

rootClientId =
Expand All @@ -218,6 +229,10 @@ export default compose( [
rootClientId
);

const index = getBlockIndex( clientId );
const blockCount = getBlockCount();
const settings = getSettings();

const hasSingleBlockType =
size( allowedBlocks ) === 1 &&
size(
Expand All @@ -236,6 +251,11 @@ export default compose( [
allowedBlockType,
directInsertBlock,
rootClientId,
prioritizePatterns:
settings.__experimentalPreferPatternsOnRoot &&
! rootClientId &&
index > 0 &&
( index < blockCount || blockCount === 0 ),
};
} ),
withDispatch( ( dispatch, ownProps, { select } ) => {
Expand Down
19 changes: 3 additions & 16 deletions packages/block-editor/src/components/inserter/quick-inserter.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export default function QuickInserter( {
rootClientId,
clientId,
isAppender,
prioritizePatterns,
} ) {
const [ filterValue, setFilterValue ] = useState( '' );
const [ destinationRootClientId, onInsertBlocks ] = useInsertionPoint( {
Expand All @@ -48,27 +49,13 @@ export default function QuickInserter( {
destinationRootClientId
);

const {
setInserterIsOpened,
insertionIndex,
prioritizePatterns,
} = useSelect(
const { setInserterIsOpened, insertionIndex } = useSelect(
( select ) => {
const { getSettings, getBlockIndex, getBlockCount } = select(
blockEditorStore
);
const { getSettings } = select( blockEditorStore );
const settings = getSettings();
const index = getBlockIndex( clientId );
const blockCount = getBlockCount();

return {
setInserterIsOpened: settings.__experimentalSetIsInserterOpened,
prioritizePatterns:
settings.__experimentalPreferPatternsOnRoot &&
! rootClientId &&
index > 0 &&
( index < blockCount || blockCount === 0 ),
insertionIndex: index === -1 ? blockCount : index,
};
},
[ clientId, rootClientId ]
Expand Down

0 comments on commit 5157c12

Please sign in to comment.