Skip to content

Commit

Permalink
RichText: Introduce new attributeKey prop
Browse files Browse the repository at this point in the history
  • Loading branch information
gziolo committed Dec 12, 2024
1 parent 0ef8a35 commit 8c8c165
Show file tree
Hide file tree
Showing 19 changed files with 29 additions and 114 deletions.
2 changes: 2 additions & 0 deletions packages/block-editor/src/components/rich-text/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ export function RichTextWrapper(
{
children,
tagName = 'div',
attributeKey,
value: adjustedValue = '',
onChange: adjustedOnChange,
isSelected: originalIsSelected,
Expand Down Expand Up @@ -117,6 +118,7 @@ export function RichTextWrapper(
alternative: 'block.json support key: "splitting"',
} );
}
identifier = identifier || attributeKey;

const instanceId = useInstanceId( RichTextWrapper );
const anchorRef = useRef();
Expand Down
3 changes: 1 addition & 2 deletions packages/block-library/src/button/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,9 +268,9 @@ function ButtonEdit( props ) {
>
<RichText
ref={ mergedRef }
attributeKey="text"
aria-label={ __( 'Button text' ) }
placeholder={ placeholder || __( 'Add text…' ) }
value={ text }
onChange={ ( value ) =>
setAttributes( {
text: removeAnchorTag( value ),
Expand Down Expand Up @@ -298,7 +298,6 @@ function ButtonEdit( props ) {
} }
onReplace={ onReplace }
onMerge={ mergeBlocks }
identifier="text"
/>
</div>
<BlockControls group="block">
Expand Down
9 changes: 1 addition & 8 deletions packages/block-library/src/button/edit.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,11 +241,6 @@ function ButtonEdit( props ) {
);
}

function onChangeText( value ) {
const { setAttributes } = props;
setAttributes( { text: value } );
}

function onChangeBorderRadius( newRadius ) {
const { setAttributes, attributes } = props;
const { style } = attributes;
Expand Down Expand Up @@ -495,15 +490,13 @@ function ButtonEdit( props ) {
) }
<RichText
ref={ onSetRef }
attributeKey="text"
placeholder={ placeholderText }
value={ text }
onChange={ onChangeText }
style={ textStyles }
textAlign={ align }
placeholderTextColor={
style?.color || styles.placeholderTextColor.color
}
identifier="text"
tagName="p"
minWidth={ minWidth } // The minimum Button size.
maxWidth={ isFixedWidth ? minWidth : maxWidth } // The width of the screen.
Expand Down
6 changes: 1 addition & 5 deletions packages/block-library/src/code/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import { RichText, useBlockProps } from '@wordpress/block-editor';
import { createBlock, getDefaultBlockName } from '@wordpress/blocks';

export default function CodeEdit( {
attributes,
setAttributes,
onRemove,
insertBlocksAfter,
mergeBlocks,
Expand All @@ -17,9 +15,7 @@ export default function CodeEdit( {
<pre { ...blockProps }>
<RichText
tagName="code"
identifier="content"
value={ attributes.content }
onChange={ ( content ) => setAttributes( { content } ) }
attributeKey="content"
onRemove={ onRemove }
onMerge={ mergeBlocks }
placeholder={ __( 'Write code…' ) }
Expand Down
13 changes: 2 additions & 11 deletions packages/block-library/src/code/edit.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,7 @@ import { createBlock, getDefaultBlockName } from '@wordpress/blocks';
import styles from './theme.scss';

export function CodeEdit( props ) {
const {
attributes,
setAttributes,
onRemove,
style,
insertBlocksAfter,
mergeBlocks,
} = props;
const { onRemove, style, insertBlocksAfter, mergeBlocks } = props;
const codeStyle = {
...usePreferredColorSchemeStyle(
styles.blockCode,
Expand All @@ -47,11 +40,9 @@ export function CodeEdit( props ) {
<View style={ codeStyle }>
<RichText
tagName="pre"
value={ attributes.content }
identifier="content"
attributeKey="content"
style={ textStyle }
underlineColorAndroid="transparent"
onChange={ ( content ) => setAttributes( { content } ) }
onMerge={ mergeBlocks }
onRemove={ onRemove }
placeholder={ __( 'Write code…' ) }
Expand Down
8 changes: 2 additions & 6 deletions packages/block-library/src/details/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const TEMPLATE = [
];

function DetailsEdit( { attributes, setAttributes, clientId } ) {
const { showContent, summary } = attributes;
const { showContent } = attributes;
const blockProps = useBlockProps();
const innerBlocksProps = useInnerBlocksProps( blockProps, {
template: TEMPLATE,
Expand Down Expand Up @@ -65,15 +65,11 @@ function DetailsEdit( { attributes, setAttributes, clientId } ) {
>
<summary onClick={ ( event ) => event.preventDefault() }>
<RichText
identifier="summary"
attributeKey="summary"
aria-label={ __( 'Write summary' ) }
placeholder={ __( 'Write summary…' ) }
allowedFormats={ [] }
withoutInteractiveFormatting
value={ summary }
onChange={ ( newSummary ) =>
setAttributes( { summary: newSummary } )
}
/>
</summary>
{ innerBlocksProps.children }
Expand Down
7 changes: 2 additions & 5 deletions packages/block-library/src/file/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ function ClipboardToolbarButton( { text, disabled } ) {
function FileEdit( { attributes, isSelected, setAttributes, clientId } ) {
const {
id,
fileName,
href,
textLinkHref,
textLinkTarget,
Expand Down Expand Up @@ -289,9 +288,8 @@ function FileEdit( { attributes, isSelected, setAttributes, clientId } ) {
) }
<div className="wp-block-file__content-wrapper">
<RichText
identifier="fileName"
tagName="a"
value={ fileName }
attributeKey="fileName"
placeholder={ __( 'Write file name…' ) }
withoutInteractiveFormatting
onChange={ ( text ) =>
Expand All @@ -305,16 +303,15 @@ function FileEdit( { attributes, isSelected, setAttributes, clientId } ) {
<div className="wp-block-file__button-richtext-wrapper">
{ /* Using RichText here instead of PlainText so that it can be styled like a button. */ }
<RichText
identifier="downloadButtonText"
tagName="div" // Must be block-level or else cursor disappears.
attributeKey="downloadButtonText"
aria-label={ __( 'Download button text' ) }
className={ clsx(
'wp-block-file__button',
__experimentalGetElementClassName(
'button'
)
) }
value={ downloadButtonText }
withoutInteractiveFormatting
placeholder={ __( 'Add text…' ) }
onChange={ ( text ) =>
Expand Down
3 changes: 1 addition & 2 deletions packages/block-library/src/heading/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,8 @@ function HeadingEdit( {
</BlockControls>
) }
<RichText
identifier="content"
tagName={ tagName }
value={ content }
attributeKey="content"
onChange={ onContentChange }
onMerge={ mergeBlocks }
onReplace={ onReplace }
Expand Down
3 changes: 1 addition & 2 deletions packages/block-library/src/heading/edit.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,8 @@ function HeadingEdit( {
/>
</BlockControls>
<RichText
identifier="content"
tagName={ tagName }
value={ content }
attributeKey="content"
onChange={ onContentChange }
onMerge={ mergeBlocks }
onSplit={ ( value, isOriginal ) => {
Expand Down
7 changes: 2 additions & 5 deletions packages/block-library/src/home-link/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { store as coreStore } from '@wordpress/core-data';

const preventDefault = ( event ) => event.preventDefault();

export default function HomeEdit( { attributes, setAttributes, context } ) {
export default function HomeEdit( { attributes, context } ) {
const homeUrl = useSelect( ( select ) => {
// Site index.
return select( coreStore ).getEntityRecord( 'root', '__unstableBase' )
Expand Down Expand Up @@ -42,12 +42,9 @@ export default function HomeEdit( { attributes, setAttributes, context } ) {
onClick={ preventDefault }
>
<RichText
identifier="label"
attributeKey="label"
className="wp-block-home-link__label"
value={ attributes.label ?? __( 'Home' ) }
onChange={ ( labelValue ) => {
setAttributes( { label: labelValue } );
} }
aria-label={ __( 'Home link text' ) }
placeholder={ __( 'Add home link' ) }
withoutInteractiveFormatting
Expand Down
13 changes: 2 additions & 11 deletions packages/block-library/src/list-item/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,7 @@ export function IndentUI( { clientId } ) {
);
}

export default function ListItemEdit( {
attributes,
setAttributes,
clientId,
mergeBlocks,
} ) {
export default function ListItemEdit( { attributes, clientId, mergeBlocks } ) {
const { placeholder, content } = attributes;
const blockProps = useBlockProps();
const innerBlocksProps = useInnerBlocksProps( blockProps, {
Expand All @@ -88,12 +83,8 @@ export default function ListItemEdit( {
<li { ...innerBlocksProps }>
<RichText
ref={ useMergeRefs( [ useEnterRef, useSpaceRef ] ) }
identifier="content"
tagName="div"
onChange={ ( nextContent ) =>
setAttributes( { content: nextContent } )
}
value={ content }
attributeKey="content"
aria-label={ __( 'List text' ) }
placeholder={ placeholder || __( 'List' ) }
onMerge={ onMerge }
Expand Down
7 changes: 1 addition & 6 deletions packages/block-library/src/list-item/edit.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ const OPACITY = '9e';

export default function ListItemEdit( {
attributes,
setAttributes,
clientId,
style,
mergeBlocks,
Expand Down Expand Up @@ -148,12 +147,8 @@ export default function ListItemEdit( {
>
<RichText
__unstableUseSplitSelection
identifier="content"
tagName="p"
onChange={ ( nextContent ) =>
setAttributes( { content: nextContent } )
}
value={ content }
attributeKey="content"
placeholder={ placeholder || __( 'List' ) }
placeholderTextColor={
defaultPlaceholderTextColorWithOpacity
Expand Down
8 changes: 1 addition & 7 deletions packages/block-library/src/navigation-link/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -505,14 +505,8 @@ export default function NavigationLinkEdit( {
<>
<RichText
ref={ ref }
identifier="label"
attributeKey="label"
className="wp-block-navigation-item__label"
value={ label }
onChange={ ( labelValue ) =>
setAttributes( {
label: labelValue,
} )
}
onMerge={ mergeBlocks }
onReplace={ onReplace }
__unstableOnSplitAtEnd={ () =>
Expand Down
6 changes: 1 addition & 5 deletions packages/block-library/src/navigation-submenu/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -450,12 +450,8 @@ export default function NavigationSubmenuEdit( {
{ /* eslint-enable */ }
<RichText
ref={ ref }
identifier="label"
attributeKey="label"
className="wp-block-navigation-item__label"
value={ label }
onChange={ ( labelValue ) =>
setAttributes( { label: labelValue } )
}
onMerge={ mergeBlocks }
onReplace={ onReplace }
aria-label={ __( 'Navigation link text' ) }
Expand Down
6 changes: 1 addition & 5 deletions packages/block-library/src/paragraph/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,9 @@ function ParagraphBlock( {
/>
) }
<RichText
identifier="content"
tagName="p"
attributeKey="content"
{ ...blockProps }
value={ content }
onChange={ ( newContent ) =>
setAttributes( { content: newContent } )
}
onMerge={ mergeBlocks }
onReplace={ onReplace }
onRemove={ onRemove }
Expand Down
10 changes: 2 additions & 8 deletions packages/block-library/src/paragraph/edit.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ function ParagraphBlock( {
return !! select( blockEditorStore ).getSettings().isRTL;
}, [] );

const { align, content, placeholder } = attributes;
const { align, placeholder } = attributes;

const styles = {
...( style?.baseColors && {
Expand Down Expand Up @@ -62,16 +62,10 @@ function ParagraphBlock( {
/>
</BlockControls>
<RichText
identifier="content"
tagName="p"
value={ content }
attributeKey="content"
deleteEnter
style={ styles }
onChange={ ( nextContent ) => {
setAttributes( {
content: nextContent,
} );
} }
onSplit={ ( value, isOriginal ) => {
let newAttributes;

Expand Down
6 changes: 1 addition & 5 deletions packages/block-library/src/post-author/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,14 +206,10 @@ function PostAuthorEdit( {
<div className="wp-block-post-author__content">
{ ( ! RichText.isEmpty( byline ) || isSelected ) && (
<RichText
identifier="byline"
attributeKey="byline"
className="wp-block-post-author__byline"
aria-label={ __( 'Post author byline text' ) }
placeholder={ __( 'Write byline…' ) }
value={ byline }
onChange={ ( value ) =>
setAttributes( { byline: value } )
}
/>
) }
<p className="wp-block-post-author__name">
Expand Down
10 changes: 3 additions & 7 deletions packages/block-library/src/post-excerpt/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import { useCanEditEntity } from '../utils/hooks';
const ELLIPSIS = '…';

export default function PostExcerptEditor( {
attributes: { textAlign, moreText, showMoreOnNewLine, excerptLength },
attributes: { textAlign, showMoreOnNewLine, excerptLength },
setAttributes,
isSelected,
context: { postId, postType, queryId },
Expand Down Expand Up @@ -129,15 +129,11 @@ export default function PostExcerptEditor( {
}
const readMoreLink = (
<RichText
identifier="moreText"
className="wp-block-post-excerpt__more-link"
tagName="a"
attributeKey="moreText"
className="wp-block-post-excerpt__more-link"
aria-label={ __( '“Read more” link text' ) }
placeholder={ __( 'Add "read more" link text' ) }
value={ moreText }
onChange={ ( newMoreText ) =>
setAttributes( { moreText: newMoreText } )
}
withoutInteractiveFormatting
/>
);
Expand Down
Loading

0 comments on commit 8c8c165

Please sign in to comment.