-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Embed block refactor and tidy #10958
Changes from 1 commit
b8e02f4
ddf0f98
71a2af3
ececfe9
0733d41
f34d9ea
c0eeaf2
c7fdf5c
3d87a41
a6d0d15
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -2,12 +2,13 @@ | |||||||||||||||||||||
* Internal dependencies | ||||||||||||||||||||||
*/ | ||||||||||||||||||||||
import { common, others } from './core-embeds'; | ||||||||||||||||||||||
import { DEFAULT_EMBED_BLOCK, WORDPRESS_EMBED_BLOCK } from './constants'; | ||||||||||||||||||||||
import { DEFAULT_EMBED_BLOCK, WORDPRESS_EMBED_BLOCK, ASPECT_RATIOS } from './constants'; | ||||||||||||||||||||||
|
||||||||||||||||||||||
/** | ||||||||||||||||||||||
* External dependencies | ||||||||||||||||||||||
*/ | ||||||||||||||||||||||
import { includes } from 'lodash'; | ||||||||||||||||||||||
import classnames from 'classnames/dedupe'; | ||||||||||||||||||||||
|
||||||||||||||||||||||
/** | ||||||||||||||||||||||
* WordPress dependencies | ||||||||||||||||||||||
|
@@ -113,3 +114,37 @@ export const createUpgradedEmbedBlock = ( props, attributesFromPreview ) => { | |||||||||||||||||||||
} | ||||||||||||||||||||||
} | ||||||||||||||||||||||
}; | ||||||||||||||||||||||
|
||||||||||||||||||||||
/** | ||||||||||||||||||||||
* Returns class names with any relevant responsive aspect ratio names. | ||||||||||||||||||||||
* | ||||||||||||||||||||||
* @param {string} html The preview HTML that possibly contains an iframe with width and height set. | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nitpick, I think the params are supposed to be lined up like they are here: gutenberg/packages/core-data/src/entities.js Lines 57 to 66 in 55d5aac
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Although it's actually really hard to find a good example of this, and I'm sure I just created some doc blocks where they weren't lined up myself 🤦♂️ |
||||||||||||||||||||||
* @param {string} existingClassNames Any existing class names. | ||||||||||||||||||||||
* @param {boolean} allowResponsive If the responsive class names should be added, or removed. | ||||||||||||||||||||||
* @return {string} Deduped class names. | ||||||||||||||||||||||
*/ | ||||||||||||||||||||||
export function getClassNames( html, existingClassNames, allowResponsive = true ) { | ||||||||||||||||||||||
const previewDocument = document.implementation.createHTMLDocument( '' ); | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suppose one future improvement is that this function is quite expensive when |
||||||||||||||||||||||
previewDocument.body.innerHTML = html; | ||||||||||||||||||||||
const iframe = previewDocument.body.querySelector( 'iframe' ); | ||||||||||||||||||||||
|
||||||||||||||||||||||
// If we have a fixed aspect iframe, and it's a responsive embed block. | ||||||||||||||||||||||
if ( iframe && iframe.height && iframe.width ) { | ||||||||||||||||||||||
const aspectRatio = ( iframe.width / iframe.height ).toFixed( 2 ); | ||||||||||||||||||||||
// Given the actual aspect ratio, find the widest ratio to support it. | ||||||||||||||||||||||
for ( let ratioIndex = 0; ratioIndex < ASPECT_RATIOS.length; ratioIndex++ ) { | ||||||||||||||||||||||
const potentialRatio = ASPECT_RATIOS[ ratioIndex ]; | ||||||||||||||||||||||
if ( aspectRatio >= potentialRatio.ratio ) { | ||||||||||||||||||||||
return classnames( | ||||||||||||||||||||||
existingClassNames, | ||||||||||||||||||||||
{ | ||||||||||||||||||||||
[ potentialRatio.className ]: allowResponsive, | ||||||||||||||||||||||
'wp-has-aspect-ratio': allowResponsive, | ||||||||||||||||||||||
} | ||||||||||||||||||||||
); | ||||||||||||||||||||||
} | ||||||||||||||||||||||
} | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
return existingClassNames; | ||||||||||||||||||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just wondering what the
responsive
variable is, is it whether the type of embed supports responsiveness?On a separate note, it might be nice to extract
! allowResponsive
into a separate variable so that it doesn't have to be repeated.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes,
responsive
is a flag for if the block allows responsive content or not.