diff --git a/packages/e2e-tests/specs/plugins/__snapshots__/meta-attribute-block.test.js.snap b/packages/e2e-tests/specs/plugins/__snapshots__/meta-attribute-block.test.js.snap index 076a8ebbaac6e..1b5c130366402 100644 --- a/packages/e2e-tests/specs/plugins/__snapshots__/meta-attribute-block.test.js.snap +++ b/packages/e2e-tests/specs/plugins/__snapshots__/meta-attribute-block.test.js.snap @@ -1,3 +1,5 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP exports[`Block with a meta attribute Should persist the meta attribute properly 1`] = `""`; + +exports[`Block with a meta attribute Should persist the meta attribute properly in a different post type 1`] = `""`; diff --git a/packages/e2e-tests/specs/plugins/meta-attribute-block.test.js b/packages/e2e-tests/specs/plugins/meta-attribute-block.test.js index 0e87fdeefea61..934e2ac73d69d 100644 --- a/packages/e2e-tests/specs/plugins/meta-attribute-block.test.js +++ b/packages/e2e-tests/specs/plugins/meta-attribute-block.test.js @@ -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' ); + } ); } ); diff --git a/packages/editor/src/hooks/custom-sources-backwards-compatibility.js b/packages/editor/src/hooks/custom-sources-backwards-compatibility.js index 0e6173b99382d..6b56281feade7 100644 --- a/packages/editor/src/hooks/custom-sources-backwards-compatibility.js +++ b/packages/editor/src/hooks/custom-sources-backwards-compatibility.js @@ -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'; @@ -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(