diff --git a/packages/rich-text/src/create.js b/packages/rich-text/src/create.js index 82348bbd4fa8a..66943ed200536 100644 --- a/packages/rich-text/src/create.js +++ b/packages/rich-text/src/create.js @@ -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 }; } diff --git a/packages/rich-text/src/register-format-type.js b/packages/rich-text/src/register-format-type.js index 262812a22b248..3354ca44a5a8c 100644 --- a/packages/rich-text/src/register-format-type.js +++ b/packages/rich-text/src/register-format-type.js @@ -139,7 +139,7 @@ export function registerFormatType( name, settings ) { } ); if ( - settings.__experimentalGetPropsForEditableTreePreparation + settings.__experimentalCreatePrepareEditableTree ) { addFilter( 'experimentalRichText', name, ( OriginalComponent ) => { let Component = OriginalComponent; @@ -193,8 +193,10 @@ 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, { @@ -202,8 +204,8 @@ export function registerFormatType( name, settings ) { blockClientId: clientId, } ), - } ) ), - ]; + } ) ) ); + } if ( settings.__experimentalGetPropsForEditableTreeChangeHandler ) { hocs.push( withDispatch( ( disp, { clientId, identifier } ) => { diff --git a/packages/rich-text/src/test/helpers/index.js b/packages/rich-text/src/test/helpers/index.js index e12757ad03530..859e7e3acefc2 100644 --- a/packages/rich-text/src/test/helpers/index.js +++ b/packages/rich-text/src/test/helpers/index.js @@ -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', + 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', + value: { + formats: [ [ { + type: 'my-plugin/link', + attributes: {}, + unregisteredAttributes: {}, + } ] ], + text: 'a', + }, + }, ]; diff --git a/packages/rich-text/src/test/to-html-string.js b/packages/rich-text/src/test/to-html-string.js index cdb491b21dedf..bd8e846cb13ef 100644 --- a/packages/rich-text/src/test/to-html-string.js +++ b/packages/rich-text/src/test/to-html-string.js @@ -35,7 +35,12 @@ describe( 'toHTMLString', () => { formatType, html, value, + noToHTMLString, } ) => { + if ( noToHTMLString ) { + return; + } + it( description, () => { if ( formatName ) { registerFormatType( formatName, formatType );