Skip to content

Commit

Permalink
Remove unneeded block.json name-only imports (#53677)
Browse files Browse the repository at this point in the history
  • Loading branch information
jsnajdr authored Aug 16, 2023
1 parent 550ca6e commit 3ac37ea
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 87 deletions.
9 changes: 2 additions & 7 deletions packages/block-library/src/buttons/transforms.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@
import { createBlock } from '@wordpress/blocks';
import { __unstableCreateElement as createElement } from '@wordpress/rich-text';

/**
* Internal dependencies
*/
import { name } from './block.json';

const transforms = {
from: [
{
Expand All @@ -18,7 +13,7 @@ const transforms = {
transform: ( buttons ) =>
// Creates the buttons block.
createBlock(
name,
'core/buttons',
{},
// Loop the selected buttons.
buttons.map( ( attributes ) =>
Expand All @@ -34,7 +29,7 @@ const transforms = {
transform: ( buttons ) =>
// Creates the buttons block.
createBlock(
name,
'core/buttons',
{},
// Loop the selected buttons.
buttons.map( ( attributes ) => {
Expand Down
15 changes: 8 additions & 7 deletions packages/block-library/src/footnotes/format.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import { createBlock, store as blocksStore } from '@wordpress/blocks';
/**
* Internal dependencies
*/
import { name } from './block.json';
import { unlock } from '../lock-unlock';

const { usesContextKey } = unlock( privateApis );
Expand Down Expand Up @@ -53,9 +52,11 @@ export const format = {
getBlockRootClientId,
getBlockName,
getBlockParentsByBlockName,
} = useSelect( blockEditorStore );
const footnotesBlockType = useSelect( ( select ) =>
select( blocksStore ).getBlockType( name )
} = registry.select( blockEditorStore );
const hasFootnotesBlockType = useSelect(
( select ) =>
!! select( blocksStore ).getBlockType( 'core/footnotes' ),
[]
);
/*
* This useSelect exists because we need to use its return value
Expand All @@ -76,7 +77,7 @@ export const format = {
const { selectionChange, insertBlock } =
useDispatch( blockEditorStore );

if ( ! footnotesBlockType ) {
if ( ! hasFootnotesBlockType ) {
return null;
}

Expand Down Expand Up @@ -136,7 +137,7 @@ export const format = {
const queue = [ ...blocks ];
while ( queue.length ) {
const block = queue.shift();
if ( block.name === name ) {
if ( block.name === 'core/footnotes' ) {
fnBlock = block;
break;
}
Expand All @@ -157,7 +158,7 @@ export const format = {
rootClientId = getBlockRootClientId( rootClientId );
}

fnBlock = createBlock( name );
fnBlock = createBlock( 'core/footnotes' );

insertBlock( fnBlock, undefined, rootClientId );
}
Expand Down
14 changes: 8 additions & 6 deletions packages/block-library/src/heading/transforms.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { createBlock, getBlockAttributes } from '@wordpress/blocks';
* Internal dependencies
*/
import { getLevelFromHeadingNodeName } from './shared';
import { name } from './block.json';

const transforms = {
from: [
Expand All @@ -17,7 +16,7 @@ const transforms = {
blocks: [ 'core/paragraph' ],
transform: ( attributes ) =>
attributes.map( ( { content, anchor, align: textAlign } ) =>
createBlock( name, {
createBlock( 'core/heading', {
content,
anchor,
textAlign,
Expand All @@ -42,7 +41,10 @@ const transforms = {
};
},
transform( node ) {
const attributes = getBlockAttributes( name, node.outerHTML );
const attributes = getBlockAttributes(
'core/heading',
node.outerHTML
);
const { textAlign } = node.style || {};

attributes.level = getLevelFromHeadingNodeName( node.nodeName );
Expand All @@ -55,14 +57,14 @@ const transforms = {
attributes.align = textAlign;
}

return createBlock( name, attributes );
return createBlock( 'core/heading', attributes );
},
},
...[ 1, 2, 3, 4, 5, 6 ].map( ( level ) => ( {
type: 'prefix',
prefix: Array( level + 1 ).join( '#' ),
transform( content ) {
return createBlock( name, {
return createBlock( 'core/heading', {
level,
content,
} );
Expand All @@ -72,7 +74,7 @@ const transforms = {
type: 'enter',
regExp: new RegExp( `^/(h|H)${ level }$` ),
transform( content ) {
return createBlock( name, {
return createBlock( 'core/heading', {
level,
content,
} );
Expand Down
4 changes: 1 addition & 3 deletions packages/block-library/src/list-item/hooks/use-merge.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import { store as blockEditorStore } from '@wordpress/block-editor';
*/
import useOutdentListItem from './use-outdent-list-item';

import { name as listItemName } from '../block.json';

export default function useMerge( clientId, onMerge ) {
const registry = useRegistry();
const {
Expand Down Expand Up @@ -38,7 +36,7 @@ export default function useMerge( clientId, onMerge ) {
const listId = getBlockRootClientId( id );
const parentListItemId = getBlockRootClientId( listId );
if ( ! parentListItemId ) return;
if ( getBlockName( parentListItemId ) !== listItemName ) return;
if ( getBlockName( parentListItemId ) !== 'core/list-item' ) return;
return parentListItemId;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@ import { useSelect, useDispatch, useRegistry } from '@wordpress/data';
import { store as blockEditorStore } from '@wordpress/block-editor';
import { cloneBlock } from '@wordpress/blocks';

/**
* Internal dependencies
*/
import { name as listItemName } from '../block.json';

export default function useOutdentListItem( clientId ) {
const registry = useRegistry();
const { canOutdent } = useSelect(
Expand All @@ -21,7 +16,7 @@ export default function useOutdentListItem( clientId ) {
getBlockRootClientId( clientId )
);
const grandParentName = getBlockName( grandParentId );
const isListItem = grandParentName === listItemName;
const isListItem = grandParentName === 'core/list-item';

return {
canOutdent: isListItem,
Expand Down Expand Up @@ -49,7 +44,7 @@ export default function useOutdentListItem( clientId ) {
const listId = getBlockRootClientId( id );
const parentListItemId = getBlockRootClientId( listId );
if ( ! parentListItemId ) return;
if ( getBlockName( parentListItemId ) !== listItemName ) return;
if ( getBlockName( parentListItemId ) !== 'core/list-item' ) return;
return parentListItemId;
}

Expand All @@ -65,7 +60,7 @@ export default function useOutdentListItem( clientId ) {
const firstClientId = clientIds[ 0 ];

// Can't outdent if it's not a list item.
if ( getBlockName( firstClientId ) !== listItemName ) return;
if ( getBlockName( firstClientId ) !== 'core/list-item' ) return;

const parentListItemId = getParentListItemId( firstClientId );

Expand Down
37 changes: 12 additions & 25 deletions packages/block-library/src/list-item/utils.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,27 @@
/**
* WordPress dependencies
*/
import { createBlock, switchToBlockType } from '@wordpress/blocks';

/**
* Internal dependencies
*/
import { name as listItemName } from './block.json';
import { name as listName } from '../list/block.json';
import { name as paragraphName } from '../paragraph/block.json';

export function createListItem( listItemAttributes, listAttributes, children ) {
return createBlock(
listItemName,
listItemAttributes,
! children?.length
? []
: [ createBlock( listName, listAttributes, children ) ]
);
}
import { switchToBlockType } from '@wordpress/blocks';

function convertBlockToList( block ) {
const list = switchToBlockType( block, listName );
if ( list ) return list;
const paragraph = switchToBlockType( block, paragraphName );
if ( paragraph ) return switchToBlockType( paragraph, listName );
return null;
const list = switchToBlockType( block, 'core/list' );
if ( list ) {
return list;
}
const paragraph = switchToBlockType( block, 'core/paragraph' );
if ( ! paragraph ) {
return null;
}
return switchToBlockType( paragraph, 'core/list' );
}

export function convertToListItems( blocks ) {
const listItems = [];

for ( let block of blocks ) {
if ( block.name === listItemName ) {
if ( block.name === 'core/list-item' ) {
listItems.push( block );
} else if ( block.name === listName ) {
} else if ( block.name === 'core/list' ) {
listItems.push( ...block.innerBlocks );
} else if ( ( block = convertBlockToList( block ) ) ) {
for ( const { innerBlocks } of block ) {
Expand Down
3 changes: 1 addition & 2 deletions packages/block-library/src/navigation-link/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ import { useMergeRefs } from '@wordpress/compose';
/**
* Internal dependencies
*/
import { name } from './block.json';
import { LinkUI } from './link-ui';
import { updateAttributes } from './update-attributes';
import { getColors } from '../navigation/edit/utils';
Expand Down Expand Up @@ -206,7 +205,7 @@ export default function NavigationLinkEdit( {
innerBlocks: getBlocks( clientId ),
isAtMaxNesting:
getBlockParentsByBlockName( clientId, [
name,
'core/navigation-link',
'core/navigation-submenu',
] ).length >= maxNestingLevel,
isTopLevelLink:
Expand Down
26 changes: 12 additions & 14 deletions packages/block-library/src/navigation-submenu/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ import { useMergeRefs, usePrevious } from '@wordpress/compose';
* Internal dependencies
*/
import { ItemSubmenuIcon } from './icons';
import { name } from './block.json';
import { LinkUI } from '../navigation-link/link-ui';
import { updateAttributes } from '../navigation-link/update-attributes';
import {
Expand Down Expand Up @@ -155,8 +154,7 @@ export default function NavigationSubmenuEdit( {
const postsPermissions = useResourcePermissions( 'posts' );

const {
isAtMaxNesting,
isTopLevelItem,
parentCount,
isParentOfSelectedBlock,
isImmediateParentOfSelectedBlock,
hasChildren,
Expand Down Expand Up @@ -191,11 +189,10 @@ export default function NavigationSubmenuEdit( {
}

return {
isAtMaxNesting:
getBlockParentsByBlockName( clientId, name ).length >=
maxNestingLevel,
isTopLevelItem:
getBlockParentsByBlockName( clientId, name ).length === 0,
parentCount: getBlockParentsByBlockName(
clientId,
'core/navigation-submenu'
).length,
isParentOfSelectedBlock: hasSelectedInnerBlock(
clientId,
true
Expand Down Expand Up @@ -278,7 +275,7 @@ export default function NavigationSubmenuEdit( {
customTextColor,
backgroundColor,
customBackgroundColor,
} = getColors( context, ! isTopLevelItem );
} = getColors( context, parentCount > 0 );

function onKeyDown( event ) {
if ( isKeyboardEvent.primary( event, 'k' ) ) {
Expand Down Expand Up @@ -310,11 +307,12 @@ export default function NavigationSubmenuEdit( {
// Always use overlay colors for submenus.
const innerBlocksColors = getColors( context, true );

const allowedBlocks = isAtMaxNesting
? ALLOWED_BLOCKS.filter(
( blockName ) => blockName !== 'core/navigation-submenu'
)
: ALLOWED_BLOCKS;
const allowedBlocks =
parentCount >= maxNestingLevel
? ALLOWED_BLOCKS.filter(
( blockName ) => blockName !== 'core/navigation-submenu'
)
: ALLOWED_BLOCKS;

const navigationChildBlockProps =
getNavigationChildBlockProps( innerBlocksColors );
Expand Down
29 changes: 14 additions & 15 deletions packages/block-library/src/query/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@ import { store as blockEditorStore } from '@wordpress/block-editor';
import { decodeEntities } from '@wordpress/html-entities';
import { cloneBlock, store as blocksStore } from '@wordpress/blocks';

/**
* Internal dependencies
*/
import { name as queryLoopName } from './block.json';

/** @typedef {import('@wordpress/blocks').WPBlockVariation} WPBlockVariation */

/**
Expand Down Expand Up @@ -175,7 +170,7 @@ export function useAllowedControls( attributes ) {
return useSelect(
( select ) =>
select( blocksStore ).getActiveBlockVariation(
queryLoopName,
'core/query',
attributes
)?.allowedControls,

Expand Down Expand Up @@ -249,25 +244,29 @@ export function useBlockNameForPatterns( clientId, attributes ) {
const activeVariationName = useSelect(
( select ) =>
select( blocksStore ).getActiveBlockVariation(
queryLoopName,
'core/query',
attributes
)?.name,
[ attributes ]
);
const blockName = `${ queryLoopName }/${ activeVariationName }`;
const activeVariationPatterns = useSelect(
const blockName = `core/query/${ activeVariationName }`;
const hasActiveVariationPatterns = useSelect(
( select ) => {
if ( ! activeVariationName ) {
return;
return false;
}
const { getBlockRootClientId, getPatternsByBlockTypes } =
select( blockEditorStore );
const rootClientId = getBlockRootClientId( clientId );
return getPatternsByBlockTypes( blockName, rootClientId );
const activePatterns = getPatternsByBlockTypes(
blockName,
rootClientId
);
return activePatterns.length > 0;
},
[ clientId, activeVariationName ]
[ clientId, activeVariationName, blockName ]
);
return activeVariationPatterns?.length ? blockName : queryLoopName;
return hasActiveVariationPatterns ? blockName : 'core/query';
}

/**
Expand Down Expand Up @@ -300,10 +299,10 @@ export function useScopedBlockVariations( attributes ) {
select( blocksStore );
return {
activeVariationName: getActiveBlockVariation(
queryLoopName,
'core/query',
attributes
)?.name,
blockVariations: getBlockVariations( queryLoopName, 'block' ),
blockVariations: getBlockVariations( 'core/query', 'block' ),
};
},
[ attributes ]
Expand Down

0 comments on commit 3ac37ea

Please sign in to comment.