Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Block API: Add support for transforms prioritization #5701

Merged
merged 1 commit into from
Mar 21, 2018

Conversation

aduth
Copy link
Member

@aduth aduth commented Mar 20, 2018

Fixes #5620

This pull request seeks to add a prioritization mechanism to block transforms, addressing two current issues with reliance on block registration order (paragraph and shortcode transforms as fallback). From a block implementer perspective, this simply adds a new property priority to the transform object, which behaves nearly identical to hooks prioritization (the similarity is not coincidental, as it uses hooks under the hood as the basis). At a code level, it introduces two new functions getBlockTransforms and findTransform which respectively return/normalize transforms and return a predicate-matching transform considering priority.

There may be room for further optimization here (e.g. sorting at registration time, caching transforms) but after a few failed attempts, I landed on this as a more direct/simple implementation.

Testing instructions:

Verify that there are no regressions in transform behavior. Notably, this impacts:

  • Block transforms
  • Paste transforms
  • Shortcode transforms
  • Use-once validation
  • File drop

@aduth aduth added the [Feature] Block API API that allows to express the block paradigm. label Mar 20, 2018
@aduth aduth requested review from mcsf and ellatrix March 20, 2018 00:38
Copy link
Contributor

@mcsf mcsf left a comment

Choose a reason for hiding this comment

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

Nice. I find the reuse of hooks clever. Outside this PR, it would be interesting to have some perf tests that we could incorporate into the test suite to track improvements and regressions. Right now, we only have anecdotal evidence to judge the impact of something like these changes (no apparent regression on my end).

* @return {Array} Block transforms for direction.
*/
export function getBlockTransforms( direction, blockName ) {
// When retrieving transforms for all block types, recurse into self.
Copy link
Contributor

Choose a reason for hiding this comment

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

recurse into self

:-O

ἓν τὸ πᾶν!

(#)

const transformation = findTransform( transformsFrom, ( transform ) => (
transform.type === 'shortcode' &&
some( castArray( transform.tag ), ( tag ) => shortcode.regexp( tag ).test( HTML ) )
) );
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice simplification, along with the reduction of the while into if + recursion.

// yielding non-deterministic results. A proper solution could be to
// let the editor (or site owners) determine a default block handler of
// unknown shortcodes — see `setUnknownTypeHandlerName`.
shortcode,
Copy link
Contributor

Choose a reason for hiding this comment

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

\o/

@@ -1,7 +1,8 @@
// Set up `wp.*` aliases. Handled by Webpack outside of the test build.
global.wp = {
shortcode: {
next: () => {},
next() {},
regexp() {},
Copy link
Contributor

Choose a reason for hiding this comment

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

This needs proper stubbing for tests.

@aduth aduth added this to the 2.5 milestone Mar 21, 2018
@aduth
Copy link
Member Author

aduth commented Mar 21, 2018

Thanks for the review @mcsf .

This needs proper stubbing for tests.

Just slightly more accurate stubbing; at least returning a RegExp. This is resolved in the rebased 777f7b6. Of course, the fact that the stubbed RegExp is enough to allow tests to pass is a good signal we have poor test coverage for this functionality.

Longer-term, we'd want to extract wp.shortcode as a reusable module that we can reference from npm.

@aduth
Copy link
Member Author

aduth commented Mar 21, 2018

Re: Performance, in fact I might suspect a net performance improvement from the changes here, merely by dropping all the _.get calls.

@aduth aduth merged commit b3b0dfe into master Mar 21, 2018
@aduth aduth deleted the try/transform-priority branch March 21, 2018 19:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Block API API that allows to express the block paradigm.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants