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

Fix: Transforms: Audio and Video to file transform bug; Add: getTextElements function: #7830

Closed
wants to merge 1 commit into from

Conversation

jorgefilipecosta
Copy link
Member

The audio and video to file transform contain a bug where if formats were used (e.g: bold, italic, ...) the caption was not correctly parsed.
Here we add a new function getTextElements that returns an array of all the text nodes present in an elements hierarchy, and we make use of that function to solve the bug in the transform.

Fixes: #7626

How has this been tested?

Add a an audio block, add a caption with formats e.g: bold, italic etc.. Transform to file block and verify things work as expected.
Repeat the steps for the video block.

@jorgefilipecosta jorgefilipecosta added [Type] Bug An existing feature does not function as intended [Feature] Block Transforms Block transforms from one block to another labels Jul 9, 2018
@jorgefilipecosta jorgefilipecosta self-assigned this Jul 9, 2018
@sarahmonster
Copy link
Member

Could we also add this fix to the image block? It seems to be suffering the same problem:

2018-07-30 14 14 13

@jorgefilipecosta jorgefilipecosta force-pushed the fix/caption-conversion-audio-file branch 2 times, most recently from 0e10341 to 102daac Compare August 1, 2018 16:18
@jorgefilipecosta
Copy link
Member Author

Hi @sarahmonster thank you for catching the problem in images the fix was added in this PR.

@sarahmonster
Copy link
Member

Tested and this looks much improved! One small thing though... when you convert it to a file, the formatting is lost.

If it's trivial to implement, it would be ideal if we could preserve the caption formatting on conversion. Otherwise, let's just ship this sooner—this is a big improvement. 👍

@jorgefilipecosta
Copy link
Member Author

Hi @sarahmonster thank you for your tests 👍 The current implementation of the file block does not support formatting in that area. To solve that we would need to change the attributes specification to allow formatting and add UI controls that allow the users to format. I think adding formats there may make sense but I'm not certain if there was any reason/decision to not have formats there, maybe it is something we can discuss in another issue.

Copy link
Member

@sarahmonster sarahmonster left a comment

Choose a reason for hiding this comment

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

Right, I should have known that. 🙈 Let's go with it as-is then. This is pretty edge case anyway, but it may be worth considering adding the text formatting (at some point, as a separate PR) as per #3785.

This looks good from a design/fixes-the-bug sort of perspective; may warrant a quick 👀 from someone who likes JavaScript more than I do. 👍

@jorgefilipecosta jorgefilipecosta requested a review from a team August 3, 2018 10:43
Copy link
Member

@tofumatt tofumatt left a comment

Choose a reason for hiding this comment

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

The code seems fine but it'd be nice if you could fix up the comments a bit.

Also, unit tests for getTextElements seem like they'd be straightforward? If possible that'd be cool 😄

if ( isArray( element ) ) {
return flatMap( element, ( child ) => getTextElements( child ) );
}
if ( element && element.props && element.props.children ) {
Copy link
Member

Choose a reason for hiding this comment

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

Isn't there some lodash helper for this kind of deep-check? get() or something?

I personally don't like it and prefer just using "raw" JS, but we use it elsewhere so we might as well here 🤷‍♂️

Copy link
Member

Choose a reason for hiding this comment

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

@@ -133,3 +133,24 @@ export function switchChildrenNodeName( children, nodeName ) {
return createElement( nodeName, { key: index, ...props }, childrenProp );
} );
}

/**
* Function that returns an array of all the text elements
Copy link
Member

Choose a reason for hiding this comment

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

I don't understand this comment. I think "descendants" should be "descendant". Could you rephrase this maybe?

@@ -96,9 +97,10 @@ export const settings = {
type: 'block',
blocks: [ 'core/audio' ],
transform: ( attributes ) => {
const captionProcessed = getTextElements( attributes.caption );
Copy link
Member

Choose a reason for hiding this comment

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

caption is a children value. It's not expected to be strictly compatible with element. The implementation of getTextElements in @wordpress/element and its usage with a children value contradicts this.

See #7934

…h custom formats; Add: getTextElements function

The audio and video to file transform contain a bug where if formats were used (e.g: bold, italic, ...)  the caption was not correctly parsed.
Here we add a new function getTextElements that returns an array of all the text nodes present in an elements hieraraquy, and we make use of that function to solve the bug in the transform.
@jorgefilipecosta jorgefilipecosta force-pushed the fix/caption-conversion-audio-file branch from 102daac to 27d845a Compare August 16, 2018 19:51
@jorgefilipecosta
Copy link
Member Author

jorgefilipecosta commented Oct 16, 2018

Closing as with the last changes to the formatting API this PR is now not relevant.

@youknowriad youknowriad deleted the fix/caption-conversion-audio-file branch October 16, 2018 21:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Block Transforms Block transforms from one block to another [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants