| | import percentageFactory from 'percentage-regex'; |
| |
|
| | const percentageRegex = percentageFactory( { exact: true } ); |
| | const isPercentage = ( val ) => percentageRegex.test( val ); |
| |
|
| | const embedsConfig = { |
| | default: { |
| | sizingFunction: function defaultEmbedSizingFunction( embed, availableWidth ) { |
| | const { aspectRatio } = embed; |
| | let { width, height } = embed; |
| |
|
| | if ( ! isNaN( aspectRatio ) ) { |
| | |
| | |
| | width = availableWidth; |
| | height = availableWidth / aspectRatio; |
| | } |
| | if ( isPercentage( width ) ) { |
| | |
| | width = availableWidth * ( parseInt( width, 10 ) / 100 ); |
| | } |
| | if ( isPercentage( height ) ) { |
| | |
| | height = width * ( parseInt( height, 10 ) / 100 ); |
| | } |
| | return { |
| | width: `${ width | 0 }px`, |
| | height: `${ height | 0 }px`, |
| | paddingRight: '1px', |
| | }; |
| | }, |
| | }, |
| | spotify: { |
| | sizingFunction: function spotifyEmbedSizingFunction( embed, availableWidth ) { |
| | let height; |
| |
|
| | |
| | |
| | if ( embed.aspectRatio ) { |
| | height = Math.min( availableWidth / embed.aspectRatio, availableWidth + 80 ); |
| | } else { |
| | height = availableWidth + 80; |
| | } |
| |
|
| | return { |
| | width: availableWidth + 'px', |
| | height: height + 'px', |
| | }; |
| | }, |
| | urlRegex: /\/\/embed.spotify.com/, |
| | }, |
| | soundcloud: { |
| | sizingFunction: function soundcloudEmbedSizingFunction( embed, availableWidth ) { |
| | const aspectRatio = embed.aspectRatio || 1; |
| | let height = '100%'; |
| |
|
| | if ( embed.iframe.indexOf( 'visual=true' ) > -1 ) { |
| | height = Math.floor( availableWidth / aspectRatio ) + 'px'; |
| | } |
| |
|
| | return { |
| | width: availableWidth + 'px', |
| | height: height, |
| | }; |
| | }, |
| | urlRegex: /\/\/w\.soundcloud\.com\/player/, |
| | }, |
| | }; |
| |
|
| | function extractUrlFromIframe( iframeHtml ) { |
| | const urlRegex = new RegExp( 'src="([^"]+)"' ); |
| | const res = urlRegex.exec( iframeHtml ); |
| |
|
| | return res.length > 1 ? res[ 1 ] : null; |
| | } |
| |
|
| | function resolveEmbedConfig( embed ) { |
| | let embedType; |
| |
|
| | |
| | if ( embedsConfig.hasOwnProperty( embed.type ) ) { |
| | return embedsConfig[ embed.type ]; |
| | } |
| |
|
| | |
| | const url = extractUrlFromIframe( embed.iframe ); |
| |
|
| | if ( url ) { |
| | for ( embedType in embedsConfig ) { |
| | if ( embedsConfig.hasOwnProperty( embedType ) && embedsConfig[ embedType ].urlRegex ) { |
| | if ( url.match( embedsConfig[ embedType ].urlRegex ) ) { |
| | return embedsConfig[ embedType ]; |
| | } |
| | } |
| | } |
| | } |
| |
|
| | return embedsConfig.default; |
| | } |
| |
|
| | const exported = { |
| | getEmbedSizingFunction: function getEmbedSizingFunction( embed ) { |
| | const embedConfig = resolveEmbedConfig( embed ); |
| |
|
| | return embedConfig.sizingFunction.bind( embedConfig, embed ); |
| | }, |
| | }; |
| |
|
| | export default exported; |
| |
|
| | export const { getEmbedSizingFunction } = exported; |
| |
|