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

Navigation: add transformations from a link to other allowed nav blocks #34978

Merged
merged 2 commits into from
Sep 23, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
17 changes: 2 additions & 15 deletions packages/block-library/src/navigation-link/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { _x } from '@wordpress/i18n';
import { customLink as linkIcon } from '@wordpress/icons';
import { InnerBlocks } from '@wordpress/block-editor';
import { addFilter } from '@wordpress/hooks';
import { createBlock } from '@wordpress/blocks';

/**
* Internal dependencies
Expand All @@ -14,6 +13,7 @@ import metadata from './block.json';
import edit from './edit';
import save from './save';
import { enhanceNavigationLinkVariations } from './hooks';
import transforms from './transforms';

const { name } = metadata;

Expand Down Expand Up @@ -85,20 +85,7 @@ export const settings = {
},
},
],
transforms: {
to: [
{
type: 'block',
blocks: [ 'core/navigation-submenu' ],
transform: ( attributes, innerBlocks ) =>
createBlock(
'core/navigation-submenu',
attributes,
innerBlocks
),
},
],
},
transforms,
};

// importing this file includes side effects. This is whitelisted in block-library/package.json under sideEffects
Expand Down
93 changes: 93 additions & 0 deletions packages/block-library/src/navigation-link/transforms.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/**
* WordPress dependencies
*/
import { createBlock } from '@wordpress/blocks';

const transforms = {
from: [
{
type: 'block',
blocks: [ 'core/site-logo' ],
transform: () => {
return createBlock( 'core/navigation-link' );
},
},
{
type: 'block',
blocks: [ 'core/spacer' ],
transform: () => {
return createBlock( 'core/navigation-link' );
},
},
{
type: 'block',
blocks: [ 'core/home-link' ],
transform: () => {
return createBlock( 'core/navigation-link' );
},
},
{
type: 'block',
blocks: [ 'core/social-links' ],
transform: () => {
return createBlock( 'core/navigation-link' );
},
},
{
type: 'block',
blocks: [ 'core/search' ],
transform: () => {
return createBlock( 'core/navigation-link' );
},
},
],
to: [
{
type: 'block',
blocks: [ 'core/navigation-submenu' ],
transform: ( attributes, innerBlocks ) =>
createBlock(
'core/navigation-submenu',
attributes,
innerBlocks
),
},
{
type: 'block',
blocks: [ 'core/spacer' ],
transform: () => {
return createBlock( 'core/spacer' );
},
},
{
type: 'block',
blocks: [ 'core/site-logo' ],
transform: () => {
return createBlock( 'core/site-logo' );
},
},
{
type: 'block',
blocks: [ 'core/home-link' ],
transform: () => {
return createBlock( 'core/home-link' );
},
},
{
type: 'block',
blocks: [ 'core/social-links' ],
transform: () => {
return createBlock( 'core/social-links' );
},
},
{
type: 'block',
blocks: [ 'core/search' ],
transform: () => {
return createBlock( 'core/search' );
},
},
],
};

export default transforms;
3 changes: 3 additions & 0 deletions packages/block-library/src/navigation-submenu/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { addSubmenu } from '@wordpress/icons';
import metadata from './block.json';
import edit from './edit';
import save from './save';
import transforms from './transforms';

const { name } = metadata;

Expand All @@ -23,4 +24,6 @@ export const settings = {
edit,

save,

transforms,
};
52 changes: 52 additions & 0 deletions packages/block-library/src/navigation-submenu/transforms.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* WordPress dependencies
*/
import { createBlock } from '@wordpress/blocks';

const transforms = {
to: [
{
type: 'block',
blocks: [ 'core/navigation-link' ],
transform: ( attributes ) =>
createBlock( 'core/navigation-link', attributes ),
},
{
type: 'block',
blocks: [ 'core/spacer' ],
transform: () => {
return createBlock( 'core/spacer' );
},
},
{
type: 'block',
blocks: [ 'core/site-logo' ],
transform: () => {
return createBlock( 'core/site-logo' );
},
},
{
type: 'block',
blocks: [ 'core/home-link' ],
transform: () => {
return createBlock( 'core/home-link' );
},
},
{
type: 'block',
blocks: [ 'core/social-links' ],
transform: () => {
return createBlock( 'core/social-links' );
},
},
{
type: 'block',
blocks: [ 'core/search' ],
transform: () => {
return createBlock( 'core/search' );
},
},
],
};

export default transforms;