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

Block Switcher does not always show all Text block types #1767

Closed
hedgefield opened this issue Jul 6, 2017 · 3 comments
Closed

Block Switcher does not always show all Text block types #1767

hedgefield opened this issue Jul 6, 2017 · 3 comments

Comments

@hedgefield
Copy link

It confused me that the options you get in the Text block switcher are contextually limited. It makes sense that you cannot convert text into an image (or whatever non-text blocks there may be), but for example, if I have a Text block I get the option to turn it into a Quote/List/Formatted/Heading, but when it is a Heading, I can only change it to Text or Quote. Why is that?

@westonruter
Copy link
Member

Each block can have a transforms property that defines how the block converts from and into other block types. For example:

  • list: https://github.com/WordPress/gutenberg/blob/master/blocks/library/list/index.js#L79-L134
  • preformatted:
    transforms: {
    from: [
    {
    type: 'block',
    blocks: [ 'core/text' ],
    transform: ( attributes ) =>
    createBlock( 'core/preformatted', attributes ),
    },
    ],
    to: [
    {
    type: 'block',
    blocks: [ 'core/text' ],
    transform: ( attributes ) =>
    createBlock( 'core/text', attributes ),
    },
    ],
    },
  • heading:
    transforms: {
    from: [
    {
    type: 'block',
    blocks: [ 'core/text' ],
    transform: ( { content, ...attrs } ) => {
    const isMultiParagraph = Array.isArray( content ) && isObject( content[ 0 ] ) && content[ 0 ].type === 'p';
    if ( isMultiParagraph ) {
    const headingContent = isObject( content[ 0 ] ) && content[ 0 ].type === 'p'
    ? content[ 0 ].props.children
    : content[ 0 ];
    const heading = createBlock( 'core/heading', {
    content: headingContent,
    } );
    const blocks = [ heading ];
    const remainingContent = content.slice( 1 );
    if ( remainingContent.length ) {
    const text = createBlock( 'core/text', {
    ...attrs,
    content: remainingContent,
    } );
    blocks.push( text );
    }
    return blocks;
    }
    return createBlock( 'core/heading', {
    content,
    } );
    },
    },
    {
    type: 'raw',
    matcher: ( node ) => /H\d/.test( node.nodeName ),
    attributes: {
    content: children( 'h1,h2,h3,h4,h5,h6' ),
    nodeName: prop( 'h1,h2,h3,h4,h5,h6', 'nodeName' ),
    },
    },
    ],
    to: [
    {
    type: 'block',
    blocks: [ 'core/text' ],
    transform: ( { content } ) => {
    return createBlock( 'core/text', {
    content,
    } );
    },
    },
    ],
    },

So if a block doesn't have a transforms definition to convert to the desired block, then the option won't be available, as I understand.

@hedgefield
Copy link
Author

Gotcha, so if someone feels compelled to add a transform they can append those files with the 'conversion formula' so to speak?

@westonruter
Copy link
Member

Yes, that's right. I'm pretty sure a plugin could add their own transforms to, say, the Text block by amending the object returned by wp.blocks.getBlockType('core/text').transforms.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants