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

[RNMobile] Cover block: Avoid exception when adding media after setting the overlay color #54825

Merged
merged 2 commits into from
Sep 26, 2023
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
11 changes: 9 additions & 2 deletions packages/block-library/src/cover/edit.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,15 @@ const Cover = ( {

const onSelectMedia = ( media ) => {
setDidUploadFail( false );
const onSelect = attributesFromMedia( setAttributes, dimRatio );
onSelect( media );

const mediaAttributes = attributesFromMedia( media );
setAttributes( {
...mediaAttributes,
focalPoint: undefined,
useFeaturedImage: undefined,
dimRatio: dimRatio === 100 ? 50 : dimRatio,
isDark: undefined,
} );
};

const onMediaPressed = () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,11 @@ exports[`when an image is attached updates background opacity 1`] = `
<!-- /wp:paragraph --></div></div>
<!-- /wp:cover -->"
`;

exports[`when no media is attached and overlay color is set adds image 1`] = `
"<!-- wp:cover {"url":"https://test.files.wordpress.com/local-image-1.mp4","id":2000,"dimRatio":50,"overlayColor":"cyan-bluish-gray","isUserOverlayColor":true} -->
<div class="wp-block-cover is-light"><span aria-hidden="true" class="wp-block-cover__background has-cyan-bluish-gray-background-color has-background-dim"></span><img class="wp-block-cover__image-background wp-image-2000" src="https://test.files.wordpress.com/local-image-1.mp4" data-object-fit="cover"/><div class="wp-block-cover__inner-container"><!-- wp:paragraph {"align":"center","placeholder":"Write title…"} -->
<p class="has-text-align-center"></p>
<!-- /wp:paragraph --></div></div>
<!-- /wp:cover -->"
`;
38 changes: 38 additions & 0 deletions packages/block-library/src/cover/test/edit.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import {
within,
getBlock,
openBlockSettings,
setupMediaPicker,
setupPicker,
} from 'test/helpers';

/**
Expand Down Expand Up @@ -68,6 +70,13 @@ const COLOR_GRAY = '#abb8c3';
const GRADIENT_GREEN =
'linear-gradient(135deg,rgb(122,220,180) 0%,rgb(0,208,130) 100%)';

const MEDIA_OPTIONS = [
'Choose from device',
'Take a Photo',
'Take a Video',
'WordPress Media Library',
];

// Simplified tree to render Cover edit within slot.
const CoverEdit = ( props ) => (
<SlotFillProvider>
Expand Down Expand Up @@ -127,6 +136,35 @@ describe( 'when no media is attached', () => {
} );
} );

describe( 'when no media is attached and overlay color is set', () => {
it( 'adds image', async () => {
const media = {
type: 'image',
id: 2000,
url: 'https://test.files.wordpress.com/local-image-1.mp4',
};
const { mediaPickerCallback } = setupMediaPicker();
const screen = await initializeEditor( {
initialHtml: COVER_BLOCK_SOLID_COLOR_HTML,
} );
const { getByText } = screen;
const { selectOption } = setupPicker( screen, MEDIA_OPTIONS );

// Get block
const coverBlock = await getBlock( screen, 'Cover' );
fireEvent.press( coverBlock );

// Open block settings
await openBlockSettings( screen );

fireEvent.press( getByText( 'Add image or video' ) );
selectOption( 'WordPress Media Library' );
await mediaPickerCallback( media );

expect( getEditorHtml() ).toMatchSnapshot();
} );
} );

describe( 'when an image is attached', () => {
it( 'edits the image', async () => {
const screen = render(
Expand Down
Loading