WordPress/gutenberg

RichText: registerFormatType should allow identification by style rule

Open

#15478 opened on May 7, 2019

View on GitHub
 (2 comments) (1 reaction) (0 assignees)JavaScript (9,607 stars) (3,893 forks)batch import
Good First IssueNeeds Dev[Package] Format library[Package] Rich text[Type] Enhancement

Description

It seems only one formatType at a time can register itself for controlling a bare span element.

It looks like something went wrong implementing this in Gutenberg. The format should have received a class (maybe underline). Registering bare tags is meant for semantic elements, not span elements. Perhaps we need to allow identifying formats by style rules as well, when we want to avoid setting a class and detect existing formats in the content. Switching now would exclude previously applied formatting.

https://github.com/Automattic/wp-calypso/pull/32808#issuecomment-489971816

Currently formatting in the content is identified either by "bare" element name (e.g. strong) or by class (e.g. span.my-custom-format). This can be limiting in cases where you can ID formatting by a style property, so you can also ID formatting that existed before Gutenberg, or wasn't applied by your format type. E.g.: underline and color.

Rules when matching:

  • Should match the tagName first.
  • Should only have one CSS rule.
  • Should match the rule's property, and store that with the value as a format attribute.

In case of underline, the rule is text-decoration. It's just an implementation detail that we're always going to set it to underline. Someone could decide to re-register underline with some style options.

For this reason, I'd also rename core/underline to core/text-decoration.

For color the above matching also makes sense.

Seems like a good first issue to me for anyone wanting to start with rich text. Happy to review it.

Contributor guide