Skip to content

Commit

Permalink
[RNMobile][Embed block] Block settings (#33654)
Browse files Browse the repository at this point in the history
  • Loading branch information
ceyhun authored Aug 13, 2021
1 parent 4aa7c70 commit b77066b
Show file tree
Hide file tree
Showing 13 changed files with 79 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import useBlockTypeImpressions from './hooks/use-block-type-impressions';

const NON_BLOCK_CATEGORIES = [ 'reusable' ];

const ALLOWED_EMBED_VARIATIONS = [ 'core/embed' ];

function BlockTypesTab( { onSelect, rootClientId, listProps } ) {
const clipboardBlock = useClipboardBlock( rootClientId );

Expand All @@ -22,7 +24,14 @@ function BlockTypesTab( { onSelect, rootClientId, listProps } ) {

const allItems = getInserterItems( rootClientId );
const blockItems = allItems.filter(
( { category } ) => ! NON_BLOCK_CATEGORIES.includes( category )
( { id, category } ) =>
! NON_BLOCK_CATEGORIES.includes( category ) &&
// We don't want to show all possible embed variations
// as different blocks in the inserter. We'll only show a
// few popular ones.
( category !== 'embed' ||
( category === 'embed' &&
ALLOWED_EMBED_VARIATIONS.includes( id ) ) )
);

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ describe( 'BlockTypesTab component', () => {
selectMock.getInserterItems.mockReturnValue( items );

const blockItems = items.filter(
( { category } ) => category !== 'reusable'
( { id, category } ) =>
category !== 'reusable' && id !== 'core-embed/a-paragraph-embed'
);
const component = shallow(
<BlockTypesTab
Expand Down
11 changes: 0 additions & 11 deletions packages/block-library/src/embed/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,6 @@ import { useBlockProps } from '@wordpress/block-editor';
import { store as coreStore } from '@wordpress/core-data';
import { View } from '@wordpress/primitives';

function getResponsiveHelp( checked ) {
return checked
? __(
'This embed will preserve its aspect ratio when the browser is resized.'
)
: __(
'This embed may not preserve its aspect ratio when the browser is resized.'
);
}

const EmbedEdit = ( props ) => {
const {
attributes: {
Expand Down Expand Up @@ -242,7 +232,6 @@ const EmbedEdit = ( props ) => {
themeSupportsResponsive={ themeSupportsResponsive }
blockSupportsResponsive={ responsive }
allowResponsive={ allowResponsive }
getResponsiveHelp={ getResponsiveHelp }
toggleResponsive={ toggleResponsive }
switchBackToURLInput={ () => setIsEditingURL( true ) }
/>
Expand Down
45 changes: 43 additions & 2 deletions packages/block-library/src/embed/edit.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
import {
createUpgradedEmbedBlock,
getClassNames,
getAttributesFromPreview,
getEmbedInfoByProvider,
} from './util';
Expand Down Expand Up @@ -62,12 +63,18 @@ const EmbedEdit = ( props ) => {
isSelected && wasBlockJustInserted && ! url
);

const { preview, fetching, cannotEmbed } = useSelect(
const {
preview,
fetching,
themeSupportsResponsive,
cannotEmbed,
} = useSelect(
( select ) => {
const {
getEmbedPreview,
isPreviewEmbedFallback,
isRequestingEmbedPreview,
getThemeSupports,
} = select( coreStore );
if ( ! url ) {
return { fetching: false, cannotEmbed: false };
Expand Down Expand Up @@ -97,6 +104,9 @@ const EmbedEdit = ( props ) => {
return {
preview: validPreview ? embedPreview : undefined,
fetching: isFetching,
themeSupportsResponsive: getThemeSupports()[
'responsive-embeds'
],
cannotEmbed: ! validPreview || previewIsFallback,
};
},
Expand All @@ -120,6 +130,21 @@ const EmbedEdit = ( props ) => {
};
};

const toggleResponsive = () => {
const { allowResponsive, className } = attributes;
const { html } = preview;
const newAllowResponsive = ! allowResponsive;

setAttributes( {
allowResponsive: newAllowResponsive,
className: getClassNames(
html,
className,
responsive && newAllowResponsive
),
} );
};

useEffect( () => {
if ( ! preview?.html || ! cannotEmbed || fetching ) {
return;
Expand Down Expand Up @@ -167,7 +192,19 @@ const EmbedEdit = ( props ) => {
}

const showEmbedPlaceholder = ! preview || cannotEmbed;
const { type, className: classFromPreview } = getMergedAttributes();

// Even though we set attributes that get derived from the preview,
// we don't access them directly because for the initial render,
// the `setAttributes` call will not have taken effect. If we're
// rendering responsive content, setting the responsive classes
// after the preview has been rendered can result in unwanted
// clipping or scrollbars. The `getAttributesFromPreview` function
// that `getMergedAttributes` uses is memoized so that we're not
const {
type,
allowResponsive,
className: classFromPreview,
} = getMergedAttributes();
const className = classnames( classFromPreview, props.className );

return (
Expand All @@ -189,6 +226,10 @@ const EmbedEdit = ( props ) => {
<>
<EmbedControls
showEditButton={ preview && ! cannotEmbed }
themeSupportsResponsive={ themeSupportsResponsive }
blockSupportsResponsive={ responsive }
allowResponsive={ allowResponsive }
toggleResponsive={ toggleResponsive }
switchBackToURLInput={ () => setIsEditingURL( true ) }
/>
<View { ...blockProps }>
Expand Down
11 changes: 10 additions & 1 deletion packages/block-library/src/embed/embed-controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,21 @@ import {
import { BlockControls, InspectorControls } from '@wordpress/block-editor';
import { edit } from '@wordpress/icons';

function getResponsiveHelp( checked ) {
return checked
? __(
'This embed will preserve its aspect ratio when the browser is resized.'
)
: __(
'This embed may not preserve its aspect ratio when the browser is resized.'
);
}

const EmbedControls = ( {
blockSupportsResponsive,
showEditButton,
themeSupportsResponsive,
allowResponsive,
getResponsiveHelp,
toggleResponsive,
switchBackToURLInput,
} ) => (
Expand Down
23 changes: 0 additions & 23 deletions packages/block-library/src/embed/embed-controls.native.js

This file was deleted.

7 changes: 2 additions & 5 deletions packages/block-library/src/embed/embed-no-preview.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { useSelect } from '@wordpress/data';
import { store as editorStore } from '@wordpress/editor';
import { BottomSheet, Icon, TextControl } from '@wordpress/components';
import { help } from '@wordpress/icons';
import { BlockIcon } from '@wordpress/block-editor';

/**
* Internal dependencies
Expand All @@ -38,10 +39,6 @@ const EmbedNoPreview = ( { label, icon, isSelected, onPress } ) => {
styles.embed__label,
styles[ 'embed__label--dark' ]
);
const embedIconStyle = usePreferredColorSchemeStyle(
styles.embed__icon,
styles[ 'embed__icon--dark' ]
);
const descriptionStyle = usePreferredColorSchemeStyle(
styles.embed__description,
styles[ 'embed__description--dark' ]
Expand Down Expand Up @@ -118,7 +115,7 @@ const EmbedNoPreview = ( { label, icon, isSelected, onPress } ) => {
onPress={ onPressContainer }
>
<View style={ containerStyle }>
<Icon icon={ icon } fill={ embedIconStyle.fill } />
<BlockIcon icon={ icon } />
<Text style={ labelStyle }>{ label }</Text>
<Text style={ descriptionStyle }>
{ __( 'Embed previews not yet available' ) }
Expand Down
7 changes: 2 additions & 5 deletions packages/block-library/src/embed/embed-placeholder.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { View, Text, TouchableWithoutFeedback } from 'react-native';
import { __ } from '@wordpress/i18n';
import { usePreferredColorSchemeStyle } from '@wordpress/compose';
import { Icon } from '@wordpress/components';
import { BlockIcon } from '@wordpress/block-editor';

/**
* Internal dependencies
Expand Down Expand Up @@ -37,10 +38,6 @@ const EmbedPlaceholder = ( {
styles.embed__action,
styles[ 'embed__action--dark' ]
);
const embedIconStyle = usePreferredColorSchemeStyle(
styles.embed__icon,
styles[ 'embed__icon--dark' ]
);
const embedIconErrorStyle = styles[ 'embed__icon--error' ];

return (
Expand Down Expand Up @@ -73,7 +70,7 @@ const EmbedPlaceholder = ( {
</>
) : (
<>
<Icon icon={ icon } fill={ embedIconStyle.fill } />
<BlockIcon icon={ icon } />
<Text style={ labelStyle }>{ label }</Text>
<Text style={ actionStyle }>
{ __( 'ADD LINK' ) }
Expand Down
8 changes: 0 additions & 8 deletions packages/block-library/src/embed/styles.native.scss
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,6 @@
background-color: $background-dark-secondary;
}

.embed__icon {
fill: $light-secondary;
}

.embed__icon--dark {
fill: $dark-secondary;
}

.embed__icon--error {
margin-bottom: 6;
fill: $alert-red;
Expand Down
3 changes: 0 additions & 3 deletions packages/block-library/src/embed/variations.native.js

This file was deleted.

10 changes: 8 additions & 2 deletions packages/components/src/toggle-control/index.native.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* External dependencies
*/
import { isFunction } from 'lodash';

/**
* WordPress dependencies
*/
Expand All @@ -11,15 +16,16 @@ const ToggleControl = memo(
( { label, checked, help, instanceId, className, onChange, ...props } ) => {
const id = `inspector-toggle-control-${ instanceId }`;

const helpLabel = help && isFunction( help ) ? help( checked ) : help;

return (
<SwitchCell
label={ label }
id={ id }
help={ help }
help={ helpLabel }
className={ className }
value={ checked }
onValueChange={ onChange }
aria-describedby={ !! help ? id + '__help' : undefined }
{ ...props }
/>
);
Expand Down
1 change: 1 addition & 0 deletions packages/react-native-editor/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ For each user feature we should also add a importance categorization label to i
-->

## Unreleased
- [**] Embed block: Add "Resize for smaller devices" setting. [#33654]

## 1.59.1
- [*] Global styles - Add color to the block styles filter list [#34000]
Expand Down
2 changes: 1 addition & 1 deletion packages/react-native-editor/src/api-fetch-setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import apiFetch from '@wordpress/api-fetch';

// Please add only wp.org API paths here!
const SUPPORTED_ENDPOINTS = [
/wp\/v2\/(media|categories|blocks)\/?\d*?.*/i,
/wp\/v2\/(media|categories|blocks|themes)\/?\d*?.*/i,
/wp\/v2\/search\?.*/i,
/oembed\/1\.0\/proxy\?.*/i,
];
Expand Down

0 comments on commit b77066b

Please sign in to comment.