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

Editor: Fix custom sources backwards compatibility for the pages post type. #17820

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Block with a meta attribute Should persist the meta attribute properly 1`] = `"<!-- wp:test/test-meta-attribute-block /-->"`;

exports[`Block with a meta attribute Should persist the meta attribute properly in a different post type 1`] = `"<!-- wp:test/test-meta-attribute-block /-->"`;
21 changes: 21 additions & 0 deletions packages/e2e-tests/specs/plugins/meta-attribute-block.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,25 @@ describe( 'Block with a meta attribute', () => {
expect( await inputValue.jsonValue() ).toBe( 'Meta Value' );
} ) );
} );

it( 'Should persist the meta attribute properly in a different post type', async () => {
await createNewPost( { postType: 'page' } );
await insertBlock( 'Test Meta Attribute Block' );
await page.keyboard.type( 'Value' );

// Regression Test: Previously the caret would wrongly reset to the end
// of any input for meta-sourced attributes, due to syncing behavior of
// meta attribute updates.
//
// See: https://github.com/WordPress/gutenberg/issues/15739
await pressKeyTimes( 'ArrowLeft', 5 );
await page.keyboard.type( 'Meta ' );

await saveDraft();
await page.reload();

expect( await getEditedPostContent() ).toMatchSnapshot();
const persistedValue = await page.evaluate( () => document.querySelector( '.my-meta-input' ).value );
expect( persistedValue ).toBe( 'Meta Value' );
} );
} );
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* WordPress dependencies
*/
import { getBlockType } from '@wordpress/blocks';
import { useSelect } from '@wordpress/data';
import { useEntityProp } from '@wordpress/core-data';
import { useMemo, useCallback } from '@wordpress/element';
import { createHigherOrderComponent } from '@wordpress/compose';
Expand All @@ -15,7 +16,9 @@ function useMetaAttributeSource( name, _attributes, _setAttributes ) {

if ( Object.values( attributeTypes ).some( ( type ) => type.source === 'meta' ) ) {
// eslint-disable-next-line react-hooks/rules-of-hooks
const [ meta, setMeta ] = useEntityProp( 'postType', 'post', 'meta' );
const type = useSelect( ( select ) => select( 'core/editor' ).getCurrentPostType(), [] );
// eslint-disable-next-line react-hooks/rules-of-hooks
const [ meta, setMeta ] = useEntityProp( 'postType', type, 'meta' );

// eslint-disable-next-line react-hooks/rules-of-hooks
attributes = useMemo(
Expand Down