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

Transform of newly-created text blocks doesn't offer ability to grab their content #63500

Open
jakeparis opened this issue Jul 12, 2024 · 1 comment
Labels
[Type] Bug An existing feature does not function as intended

Comments

@jakeparis
Copy link
Contributor

Description

When you transform from a block like core/heading or core/paragraph which has not yet been saved, the text content of that block is not available to the transform. Instead, it is locked inside an inaccessible private property in the attributes object provided to the transform function:

{
  "clientId": "dc20b9f6-4d79-42f2-bdf1-79d6f11bc4ab",
  "name": "core/heading",
  "isValid": true,
  "attributes": {
    "content": {
        #value: {
            ...
            "text": "Here is my custom header",
        }
    },
    "level": 2
  },
  "innerBlocks": []
}

So if you want to transform a newly created header or paragraph block (those are the two I tested with), you can't do it. You have to first save the page, then transform them.

I tried this with both the transform() property function as well as the __experimentalConvert() property function.

Step-by-step reproduction instructions

  1. Create a transform in the from direction
  2. set blocks: ['*']
  3. set either __experimentalConvert or transform function property.
  4. The first argument to the function is either blocks (first case) or attributes (second case).
  5. Save that block.
  6. In the wordpress editor, create a header with some text
  7. WITHOUT saving the page, attempt to tranform the header to your new block
  8. If you console.log the incoming argument to the tranform function, you can see that you don't have access to the text content of the heading block.

Screenshots, screen recording, code snippet

import {
	registerBlockType,
} from '@wordpress/blocks';

registerBlockType( 'gutentest/jp', {
	"apiVersion": 2,
	
	"name": "gutentest/jp",
	"title": "Gutentest JP",
	edit: function ( props ) {
		return ( <p>Hello</p> );
	},
	save: function ( props ) {
		return null;
	},
	transforms: {
		from: [
			{
				type: 'block',
				blocks: [ '*' ],
				isMultiBlock: true,
				isMatch: () => true,
				transform: function ( attributesArray ) {
					console.debug( attributesArray)
					return createBlock( 'gutenttest/jp');
				},
				// OR (one or the other)
				// __experimentalConvert ( blocks ) {
				// 	console.debug(  blocks )
				// 	return createBlock( 'gutentest/jp' );
				// }
			}
		]
	}
});

Environment info

Wordpress 6.5.5

Please confirm that you have searched existing issues in the repo.

Yes

Please confirm that you have tested with all plugins deactivated except Gutenberg.

Yes

@jakeparis jakeparis added the [Type] Bug An existing feature does not function as intended label Jul 12, 2024
@jakeparis
Copy link
Contributor Author

I've found a work around for this.

import { serialize, parse } from '@wordpress/blocks';

export transform {
  from: [
    {
      ...
      __experimentalConvert ( blocks ) {
        let html = serialize( blocks );
        let reparsedBlocks = parse( html );
        // now the inner html of the blocks is always available
        reparsedBlocks[0].attributes.content.originalHTML;
      ...
    }
  ]
}

This works, but I wouldn't expect to have to do this on my own.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Type] Bug An existing feature does not function as intended
Projects
None yet
Development

No branches or pull requests

1 participant