Skip to content

Commit

Permalink
RichText: Fix prepareEditableTree (#14284)
Browse files Browse the repository at this point in the history
* Fix use of __experimentalCreatePrepareEditableTree without __experimentalCreateOnChangeEditableValue

* Add unit tests
  • Loading branch information
ellatrix authored and youknowriad committed Mar 20, 2019
1 parent ba3bc52 commit 65e07c4
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 5 deletions.
7 changes: 7 additions & 0 deletions packages/rich-text/src/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ function toFormat( { type, attributes } ) {
return attributes ? { type, attributes } : { type };
}

if (
formatType.__experimentalCreatePrepareEditableTree &&
! formatType.__experimentalCreateOnChangeEditableValue
) {
return null;
}

if ( ! attributes ) {
return { type: formatType.name };
}
Expand Down
12 changes: 7 additions & 5 deletions packages/rich-text/src/register-format-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ export function registerFormatType( name, settings ) {
} );

if (
settings.__experimentalGetPropsForEditableTreePreparation
settings.__experimentalCreatePrepareEditableTree
) {
addFilter( 'experimentalRichText', name, ( OriginalComponent ) => {
let Component = OriginalComponent;
Expand Down Expand Up @@ -197,17 +197,19 @@ export function registerFormatType( name, settings ) {
};
}

const hocs = [
withSelect( ( sel, { clientId, identifier } ) => ( {
const hocs = [];

if ( settings.__experimentalGetPropsForEditableTreePreparation ) {
hocs.push( withSelect( ( sel, { clientId, identifier } ) => ( {
[ `format_${ name }` ]: settings.__experimentalGetPropsForEditableTreePreparation(
sel,
{
richTextIdentifier: identifier,
blockClientId: clientId,
}
),
} ) ),
];
} ) ) );
}

if ( settings.__experimentalGetPropsForEditableTreeChangeHandler ) {
hocs.push( withDispatch( ( disp, { clientId, identifier } ) => {
Expand Down
38 changes: 38 additions & 0 deletions packages/rich-text/src/test/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -650,4 +650,42 @@ export const specWithRegistration = [
text: 'a',
},
},
{
description: 'should not create format if editable tree only',
formatName: 'my-plugin/link',
formatType: {
title: 'Custom Link',
tagName: 'a',
className: 'custom-format',
edit() {},
__experimentalCreatePrepareEditableTree() {},
},
html: '<a class="custom-format">a</a>',
value: {
formats: [ , ],
text: 'a',
},
noToHTMLString: true,
},
{
description: 'should create format if editable tree only but changes need to be recorded',
formatName: 'my-plugin/link',
formatType: {
title: 'Custom Link',
tagName: 'a',
className: 'custom-format',
edit() {},
__experimentalCreatePrepareEditableTree() {},
__experimentalCreateOnChangeEditableValue() {},
},
html: '<a class="custom-format">a</a>',
value: {
formats: [ [ {
type: 'my-plugin/link',
attributes: {},
unregisteredAttributes: {},
} ] ],
text: 'a',
},
},
];
5 changes: 5 additions & 0 deletions packages/rich-text/src/test/to-html-string.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,12 @@ describe( 'toHTMLString', () => {
formatType,
html,
value,
noToHTMLString,
} ) => {
if ( noToHTMLString ) {
return;
}

it( description, () => {
if ( formatName ) {
registerFormatType( formatName, formatType );
Expand Down

0 comments on commit 65e07c4

Please sign in to comment.