Skip to content

Commit

Permalink
Code block: should use RichText (native) (#56724)
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix authored Dec 5, 2023
1 parent 88da557 commit 310f789
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 15 deletions.
24 changes: 11 additions & 13 deletions packages/block-library/src/code/edit.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { View } from 'react-native';
/**
* WordPress dependencies
*/
import { PlainText } from '@wordpress/block-editor';
import { RichText } from '@wordpress/block-editor';
import { __ } from '@wordpress/i18n';
import { usePreferredColorSchemeStyle } from '@wordpress/compose';
import { createBlock, getDefaultBlockName } from '@wordpress/blocks';
Expand All @@ -20,14 +20,11 @@ import { createBlock, getDefaultBlockName } from '@wordpress/blocks';
*/
import styles from './theme.scss';

// Note: styling is applied directly to the (nested) PlainText component. Web-side components
// apply it to the container 'div' but we don't have a proper proposal for cascading styling yet.
export function CodeEdit( props ) {
const {
attributes,
setAttributes,
onFocus,
onBlur,
onRemove,
style,
insertBlocksAfter,
mergeBlocks,
Expand All @@ -37,30 +34,31 @@ export function CodeEdit( props ) {
styles.blockCode,
styles.blockCodeDark
),
...( style?.fontSize && { fontSize: style.fontSize } ),
};

const textStyle = style?.fontSize ? { fontSize: style.fontSize } : {};

const placeholderStyle = usePreferredColorSchemeStyle(
styles.placeholder,
styles.placeholderDark
);

return (
<View>
<PlainText
<View style={ codeStyle }>
<RichText
tagName="pre"
value={ attributes.content }
identifier="content"
style={ codeStyle }
multiline={ true }
style={ textStyle }
underlineColorAndroid="transparent"
onChange={ ( content ) => setAttributes( { content } ) }
onMerge={ mergeBlocks }
onRemove={ onRemove }
placeholder={ __( 'Write code…' ) }
aria-label={ __( 'Code' ) }
isSelected={ props.isSelected }
onFocus={ onFocus }
onBlur={ onBlur }
placeholderTextColor={ placeholderStyle.color }
preserveWhiteSpace
__unstablePastePlainText
__unstableOnSplitAtDoubleLineEnd={ () =>
insertBlocksAfter( createBlock( getDefaultBlockName() ) )
}
Expand Down
4 changes: 2 additions & 2 deletions packages/block-library/src/code/test/edit.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ describe( 'Code', () => {
const screen = await initializeEditor( {
initialHtml,
} );
const { getByDisplayValue } = screen;
const { findByPlaceholderText } = screen;

// Get block
const codeBlock = await getBlock( screen, 'Code' );
expect( codeBlock ).toBeVisible();
fireEvent.press( codeBlock );

// Get initial text
const codeBlockText = getByDisplayValue( 'Sample text' );
const codeBlockText = await findByPlaceholderText( 'Write code…' );
expect( codeBlockText ).toBeVisible();

expect( getEditorHtml() ).toMatchSnapshot();
Expand Down

1 comment on commit 310f789

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected in 310f789.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/7103097930
📝 Reported issues:

Please sign in to comment.