Skip to content

Commit

Permalink
Support passing a context variable to the custom format prepareEditTr…
Browse files Browse the repository at this point in the history
…eeValue (#11630)
  • Loading branch information
youknowriad committed Nov 9, 2018
1 parent aa6f609 commit 89539fd
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 6 deletions.
1 change: 1 addition & 0 deletions packages/block-library/src/heading/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export default function HeadingEdit( {
</PanelBody>
</InspectorControls>
<RichText
identifier="content"
wrapperClassName="wp-block-heading"
tagName={ tagName }
value={ content }
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/list/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@ export const settings = {
] }
/>
<RichText
identifier="values"
multiline="li"
tagName={ tagName }
unstableGetSettings={ this.getEditorSettings }
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/paragraph/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ class ParagraphBlock extends Component {
</PanelColorSettings>
</InspectorControls>
<RichText
identifier="content"
tagName="p"
className={ classnames( 'wp-block-paragraph', className, {
'has-text-color': textColor.color,
Expand Down
9 changes: 7 additions & 2 deletions packages/block-library/src/quote/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,18 @@ import {
import { join, split, create, toHTMLString } from '@wordpress/rich-text';
import { G, Path, SVG } from '@wordpress/components';

const ATTRIBUTE_QUOTE = 'value';
const ATTRIBUTE_CITATION = 'citation';

const blockAttributes = {
value: {
[ ATTRIBUTE_QUOTE ]: {
type: 'string',
source: 'html',
selector: 'blockquote',
multiline: 'p',
default: '',
},
citation: {
[ ATTRIBUTE_CITATION ]: {
type: 'string',
source: 'html',
selector: 'cite',
Expand Down Expand Up @@ -201,6 +204,7 @@ export const settings = {
</BlockControls>
<blockquote className={ className } style={ { textAlign: align } }>
<RichText
identifier={ ATTRIBUTE_QUOTE }
multiline
value={ value }
onChange={
Expand All @@ -222,6 +226,7 @@ export const settings = {
/>
{ ( ! RichText.isEmpty( citation ) || isSelected ) && (
<RichText
identifier={ ATTRIBUTE_CITATION }
value={ citation }
onChange={
( nextCitation ) => setAttributes( {
Expand Down
6 changes: 5 additions & 1 deletion packages/editor/src/components/rich-text/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -959,19 +959,23 @@ const RichTextContainer = compose( [
withBlockEditContext( ( context, ownProps ) => {
// When explicitly set as not selected, do nothing.
if ( ownProps.isSelected === false ) {
return {};
return {
clientId: context.clientId,
};
}
// When explicitly set as selected, use the value stored in the context instead.
if ( ownProps.isSelected === true ) {
return {
isSelected: context.isSelected,
clientId: context.clientId,
};
}

// Ensures that only one RichText component can be focused.
return {
isSelected: context.isSelected && context.focusedElement === ownProps.instanceId,
setFocusedElement: context.setFocusedElement,
clientId: context.clientId,
};
} ),
withSelect( ( select ) => {
Expand Down
15 changes: 12 additions & 3 deletions packages/rich-text/src/register-format-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,23 @@ export function registerFormatType( name, settings ) {
settings.__experimentalGetPropsForEditableTreePreparation
) {
addFilter( 'experimentalRichText', name, ( OriginalComponent ) => {
return withSelect( ( sel ) => ( {
[ `format_${ name }` ]: settings.__experimentalGetPropsForEditableTreePreparation( sel ),
return withSelect( ( sel, { clientId, identifier } ) => ( {
[ `format_${ name }` ]: settings.__experimentalGetPropsForEditableTreePreparation(
sel,
{
richTextIdentifier: identifier,
blockClientId: clientId,
}
),
} ) )( ( props ) => (
<OriginalComponent
{ ...props }
prepareEditableTree={ [
...( props.prepareEditableTree || [] ),
settings.__experimentalCreatePrepareEditableTree( props[ `format_${ name }` ] ),
settings.__experimentalCreatePrepareEditableTree( props[ `format_${ name }` ], {
richTextIdentifier: props.identifier,
blockClientId: props.clientId,
} ),
] }
/>
) );
Expand Down

0 comments on commit 89539fd

Please sign in to comment.