Skip to content

Support Prepare RichText tree#11595

Merged
youknowriad merged 13 commits intomasterfrom
try/prepare-editable
Nov 9, 2018
Merged

Support Prepare RichText tree#11595
youknowriad merged 13 commits intomasterfrom
try/prepare-editable

Conversation

@ellatrix
Copy link
Copy Markdown
Member

@ellatrix ellatrix commented Nov 7, 2018

Description

To test: type "Gutenberg" in a RichText field. The word will be highlighted if it's the first occurrence, and if the "Block" sidebar is open. Now remove a letter, and highlighting will be gone. Add it back to add highlighting and go to the "Document" sidebar. Highlighting will be gone. It depends on the sidebar state.

Example plugin code:

wp.richText.registerFormatType( 'plugin/annotation', {
	title: 'invisible',
	tagName: 'mark',
	className: 'annotation',
	__experimentalGetPropsForEditableTreePreparation( select ) {
		return {
			isEnabled: select( 'core/edit-post' ).getActiveGeneralSidebarName() === 'edit-post/block',
		};
	},
	__experimentalCreatePrepareEditableTree( props ) {
		return ( formats, text ) => {
			if ( ! props.isEnabled ) {
				return formats;
			}

			const search = 'Gutenberg';
			const index = text.indexOf( search );

			if ( index === -1 ) {
				return formats;
			}

			const start = index;
			const end = index + search.length;

			const newValue = wp.richText.applyFormat( { text, formats }, { type: 'plugin/annotation' }, start, end );

			return newValue.formats;
		};
	},
} );

How has this been tested?

Screenshots

Types of changes

Checklist:

  • My code is tested.
  • My code follows the WordPress code style.
  • My code follows the accessibility standards.
  • My code has proper inline documentation.

function prepareFormats( value ) {
return getFormatTypes().reduce( ( accumlator, { prepareEditableTree } ) => {
if ( prepareEditableTree ) {
return prepareEditableTree( accumlator, value.text );
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Any reason not to pass the entire value?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes. We don't want to alter the text at this stage. Otherwise the internal value is wrong. This should be purely for applying formatting on top of text.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

We could also pass the entire value and only use the formats from what is returned. Also maybe warn if the length is different.

@youknowriad
Copy link
Copy Markdown
Contributor

This feels like a great start, can we try to polish the PR for merge.

Also, I'm wondering if we should keep prepareEditableTree as experimental API for some time. Thoughts? Like just rename it __experimentalPrepareEditableTree?

@youknowriad youknowriad added this to the 4.3 milestone Nov 8, 2018
@youknowriad youknowriad mentioned this pull request Nov 8, 2018
4 tasks
@ellatrix ellatrix force-pushed the try/prepare-editable branch from 98bb0f7 to 88740cd Compare November 8, 2018 14:52
<br
data-mce-bogus="1"
/>
<li>
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Are these changes here normal?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes, it was actually wrong before. We're expecting a padded multiline element.

} ) )( ( props ) => (
<OriginalComponent
{ ...props }
prepareEditableTree={ [
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We are generating a new array here on each render, I wonder if this is the source of the rerendering happening in all RichText. I wonder if we could memoize it somehow in withSelect by using settings.__experimentalGetPropsForEditableTreePreparation( sel ) as the memoization key, as we don't want to generate a new array if the the returned value didn't change.

Anyway, it doesn't feel unsolvable, so for the sake of iterations, I'm fine leaving this optimisation for a follow-up.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Could it also be creating a new object in withSelect every time? But sure, sounds resolvable.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Should we create an issue for this one?

Copy link
Copy Markdown
Contributor

@youknowriad youknowriad left a comment

Choose a reason for hiding this comment

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

LGTM 👍

Do you want to give it another look @aduth?

@ellatrix
Copy link
Copy Markdown
Member Author

ellatrix commented Nov 8, 2018

Ideally this is followed up with some tests and a Christmas light investigation.

) {
addFilter( 'experimentalRichText', name, ( OriginalComponent ) => {
return withSelect( ( sel ) => ( {
[ `format_${ name }` ]: settings.__experimentalGetPropsForEditableTreePreparation( sel ),
Copy link
Copy Markdown
Member

@gziolo gziolo Nov 8, 2018

Choose a reason for hiding this comment

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

I think you can simplify it to:

return withSelect( ( select, ownProps ) => ( {
	prepareEditableTree: [
		...ownProps.prepareEditableTree,
		settings.__experimentalCreatePrepareEditableTree( settings.__experimentalGetPropsForEditableTreePreparation( select ) ),
	],
} ) )( OriginalComponent );

Updated: I thought settings.__experimentalGetPropsForEditableTreePreparation returns a boolean value.

It still has this issue that it will create a new reference each time props change.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

☝️ this way you will have only one wrapping HOC instead of two.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I tried this, it doesn't behave exactly the same.

@youknowriad youknowriad merged commit 4741104 into master Nov 9, 2018
@youknowriad youknowriad deleted the try/prepare-editable branch November 9, 2018 08:01
@youknowriad youknowriad changed the title Proof of Concept for e.g. Spellchecker Support Prepare RichText tree Nov 9, 2018
<p></p>
<!-- /wp:paragraph -->"
`;
exports[`Quote can be converted to paragraphs and renders a void paragraph if both the cite and quote are void 1`] = `""`;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why did this change?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why did this change?

Why did this change?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I have no idea tbh. But now this is true, where it wasn't before: "Quote can be converted to paragraphs and renders a void paragraph if both the cite and quote are void".

// date. In the future we could always let it flow back in the live DOM
// if there are no performance issues.
this.onChange( transformed, record === transformed );
this.onChange( transformed );
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This worries me that our standard set of operations for a single input have suddenly become more non-performant. Am I wrong to be worried?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

The only thing that changes the difference between the created DOM tree by us will be applied to the live DOM. With most input, there will be no changes.

@aduth
Copy link
Copy Markdown
Member

aduth commented Nov 9, 2018

Ideally this is followed up with some tests and a Christmas light investigation.

Before 4.3 I'd hope.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants