UNPKG

11.1 kBSource Map (JSON)View Raw
1{"version":3,"sources":["@wordpress/block-library/src/embed/util.js"],"names":["common","others","DEFAULT_EMBED_BLOCK","WORDPRESS_EMBED_BLOCK","ASPECT_RATIOS","includes","classnames","renderToString","createBlock","matchesPatterns","url","patterns","some","pattern","match","findBlock","block","name","isFromWordPress","html","getPhotoHtml","photo","imageUrl","thumbnail_url","photoPreview","title","createUpgradedEmbedBlock","props","attributesFromPreview","preview","attributes","matchingBlock","getClassNames","existingClassNames","allowResponsive","aspectRatioClassNames","ratioIndex","length","aspectRatioToRemove","className","previewDocument","document","implementation","createHTMLDocument","body","innerHTML","iframe","querySelector","height","width","aspectRatio","toFixed","potentialRatio","ratio","fallback","onReplace","link","content"],"mappings":";;;;;AAAA;;;AAGA,SAASA,MAAT,EAAiBC,MAAjB,QAA+B,eAA/B;AACA,SAASC,mBAAT,EAA8BC,qBAA9B,EAAqDC,aAArD,QAA0E,aAA1E;AAEA;;;;AAGA,SAASC,QAAT,QAAyB,QAAzB;AACA,OAAOC,UAAP,MAAuB,mBAAvB;AAEA;;;;AAGA,SAASC,cAAT,QAA+B,oBAA/B;AACA,SAASC,WAAT,QAA4B,mBAA5B;AAEA;;;;;;;;AAOA,OAAO,IAAMC,eAAe,GAAG,SAAlBA,eAAkB,CAAEC,GAAF,EAA0B;AAAA,MAAnBC,QAAmB,uEAAR,EAAQ;AACxD,SAAOA,QAAQ,CAACC,IAAT,CAAe,UAAEC,OAAF,EAAe;AACpC,WAAOH,GAAG,CAACI,KAAJ,CAAWD,OAAX,CAAP;AACA,GAFM,CAAP;AAGA,CAJM;AAMP;;;;;;;;AAOA,OAAO,IAAME,SAAS,GAAG,SAAZA,SAAY,CAAEL,GAAF,EAAW;AAAA,0CACTV,MADS,sBACEC,MADF;;AACnC,2CAAgD;AAA1C,QAAMe,KAAK,WAAX;;AACL,QAAKP,eAAe,CAAEC,GAAF,EAAOM,KAAK,CAACL,QAAb,CAApB,EAA8C;AAC7C,aAAOK,KA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ACA,SAAM,IAAIf,WAAU,GAAG,CAAvB,EAA0BA,WAAU,GAAGhC,aAAa,CAACiC,MAArD,EAA6DD,WAAU,EAAvE,EAA4E;AAC3E,UAAMgB,cAAc,GAAGhD,aAAa,CAAEgC,WAAF,CAApC;;AACA,UAAKc,WAAW,IAAIE,cAAc,CAACC,KAAnC,EAA2C;AAAA;;AAC1C,eAAO/C,UAAU,CAChB2B,kBADgB,kDAGbmB,cAAc,CAACb,SAHF,EAGeL,eAHf,gCAIf,qBAJe,EAIQA,eAJR,gBAAjB;AAOA;AACD;AACD;;AAED,SAAOD,kBAAP;AACA;AAED;;;;;;;;AAOA,OAAO,SAASqB,QAAT,CAAmB5C,GAAnB,EAAwB6C,SAAxB,EAAoC;AAC1C,MAAMC,IAAI,GAAG;AAAG,IAAA,IAAI,EAAG9C;AAAV,KAAkBA,GAAlB,CAAb;AACA6C,EAAAA,SAAS,CACR/C,WAAW,CAAE,gBAAF,EAAoB;AAAEiD,IAAAA,OAAO,EAAElD,cAAc,CAAEiD,IAAF;AAAzB,GAApB,CADH,CAAT;AAGA","sourcesContent":["/**\n * Internal dependencies\n */\nimport { common, others } from './core-embeds';\nimport { DEFAULT_EMBED_BLOCK, WORDPRESS_EMBED_BLOCK, ASPECT_RATIOS } from './constants';\n\n/**\n * External dependencies\n */\nimport { includes } from 'lodash';\nimport classnames from 'classnames/dedupe';\n\n/**\n * WordPress dependencies\n */\nimport { renderToString } from '@wordpress/element';\nimport { createBlock } from '@wordpress/blocks';\n\n/**\n * Returns true if any of the regular expressions match the URL.\n *\n * @param {string} url The URL to test.\n * @param {Array} patterns The list of regular expressions to test agains.\n * @return {boolean} True if any of the regular expressions match the URL.\n */\nexport const matchesPatterns = ( url, patterns = [] ) => {\n\treturn patterns.some( ( pattern ) => {\n\t\treturn url.match( pattern );\n\t} );\n};\n\n/**\n * Finds the block name that should be used for the URL, based on the\n * structure of the URL.\n *\n * @param {string} url The URL to test.\n * @return {string} The name of the block that should be used for this URL, e.g. core-embed/twitter\n */\nexport const findBlock = ( url ) => {\n\tfor ( const block of [ ...common, ...others ] ) {\n\t\tif ( matchesPatterns( url, block.patterns ) ) {\n\t\t\treturn block.name;\n\t\t}\n\t}\n\treturn DEFAULT_EMBED_BLOCK;\n};\n\nexport const isFromWordPress = ( html ) => {\n\treturn includes( html, 'class=\"wp-embedded-content\"' );\n};\n\nexport const getPhotoHtml = ( photo ) => {\n\t// 100% width for the preview so it fits nicely into the document, some \"thumbnails\" are\n\t// actually the full size photo. If thumbnails not found, use full image.\n\tconst imageUrl = ( photo.thumbnail_url ) ? photo.thumbnail_url : photo.url;\n\tconst photoPreview = <p><img src={ imageUrl } alt={ photo.title } width=\"100%\" /></p>;\n\treturn renderToString( photoPreview );\n};\n\n/***\n * Creates a more suitable embed block based on the passed in props\n * and attributes generated from an embed block's preview.\n *\n * We require `attributesFromPreview` to be generated from the latest attributes\n * and preview, and because of the way the react lifecycle operates, we can't\n * guarantee that the attributes contained in the block's props are the latest\n * versions, so we require that these are generated separately.\n * See `getAttributesFromPreview` in the generated embed edit component.\n *\n * @param {Object} props The block's props.\n * @param {Object} attributesFromPreview Attributes generated from the block's most up to date preview.\n * @return {Object|undefined} A more suitable embed block if one exists.\n */\nexport const createUpgradedEmbedBlock = ( props, attributesFromPreview ) => {\n\tconst { preview, name } = props;\n\tconst { url } = props.attributes;\n\n\tif ( ! url ) {\n\t\treturn;\n\t}\n\n\tconst matchingBlock = findBlock( url );\n\n\t// WordPress blocks can work on multiple sites, and so don't have patterns,\n\t// so if we're in a WordPress block, assume the user has chosen it for a WordPress URL.\n\tif ( WORDPRESS_EMBED_BLOCK !== name && DEFAULT_EMBED_BLOCK !== matchingBlock ) {\n\t\t// At this point, we have discovered a more suitable block for this url, so transform it.\n\t\tif ( name !== matchingBlock ) {\n\t\t\treturn createBlock( matchingBlock, { url } );\n\t\t}\n\t}\n\n\tif ( preview ) {\n\t\tconst { html } = preview;\n\n\t\t// We can't match the URL for WordPress embeds, we have to check the HTML instead.\n\t\tif ( isFromWordPress( html ) ) {\n\t\t\t// If this is not the WordPress embed block, transform it into one.\n\t\t\tif ( WORDPRESS_EMBED_BLOCK !== name ) {\n\t\t\t\treturn createBlock(\n\t\t\t\t\tWORDPRESS_EMBED_BLOCK,\n\t\t\t\t\t{\n\t\t\t\t\t\turl,\n\t\t\t\t\t\t// By now we have the preview, but when the new block first renders, it\n\t\t\t\t\t\t// won't have had all the attributes set, and so won't get the correct\n\t\t\t\t\t\t// type and it won't render correctly. So, we pass through the current attributes\n\t\t\t\t\t\t// here so that the initial render works when we switch to the WordPress\n\t\t\t\t\t\t// block. This only affects the WordPress block because it can't be\n\t\t\t\t\t\t// rendered in the usual Sandbox (it has a sandbox of its own) and it\n\t\t\t\t\t\t// relies on the preview to set the correct render type.\n\t\t\t\t\t\t...attributesFromPreview,\n\t\t\t\t\t}\n\t\t\t\t);\n\t\t\t}\n\t\t}\n\t}\n};\n\n/**\n * Returns class names with any relevant responsive aspect ratio names.\n *\n * @param {string} html The preview HTML that possibly contains an iframe with width and height set.\n * @param {string} existingClassNames Any existing class names.\n * @param {boolean} allowResponsive If the responsive class names should be added, or removed.\n * @return {string} Deduped class names.\n */\nexport function getClassNames( html, existingClassNames = '', allowResponsive = true ) {\n\tif ( ! allowResponsive ) {\n\t\t// Remove all of the aspect ratio related class names.\n\t\tconst aspectRatioClassNames = {\n\t\t\t'wp-has-aspect-ratio': false,\n\t\t};\n\t\tfor ( let ratioIndex = 0; ratioIndex < ASPECT_RATIOS.length; ratioIndex++ ) {\n\t\t\tconst aspectRatioToRemove = ASPECT_RATIOS[ ratioIndex ];\n\t\t\taspectRatioClassNames[ aspectRatioToRemove.className ] = false;\n\t\t}\n\t\treturn classnames(\n\t\t\texistingClassNames,\n\t\t\taspectRatioClassNames\n\t\t);\n\t}\n\n\tconst previewDocument = document.implementation.createHTMLDocument( '' );\n\tpreviewDocument.body.innerHTML = html;\n\tconst iframe = previewDocument.body.querySelector( 'iframe' );\n\n\t// If we have a fixed aspect iframe, and it's a responsive embed block.\n\tif ( iframe && iframe.height && iframe.width ) {\n\t\tconst aspectRatio = ( iframe.width / iframe.height ).toFixed( 2 );\n\t\t// Given the actual aspect ratio, find the widest ratio to support it.\n\t\tfor ( let ratioIndex = 0; ratioIndex < ASPECT_RATIOS.length; ratioIndex++ ) {\n\t\t\tconst potentialRatio = ASPECT_RATIOS[ ratioIndex ];\n\t\t\tif ( aspectRatio >= potentialRatio.ratio ) {\n\t\t\t\treturn classnames(\n\t\t\t\t\texistingClassNames,\n\t\t\t\t\t{\n\t\t\t\t\t\t[ potentialRatio.className ]: allowResponsive,\n\t\t\t\t\t\t'wp-has-aspect-ratio': allowResponsive,\n\t\t\t\t\t}\n\t\t\t\t);\n\t\t\t}\n\t\t}\n\t}\n\n\treturn existingClassNames;\n}\n\n/**\n * Fallback behaviour for unembeddable URLs.\n * Creates a paragraph block containing a link to the URL, and calls `onReplace`.\n *\n * @param {string} url The URL that could not be embedded.\n * @param {function} onReplace Function to call with the created fallback block.\n */\nexport function fallback( url, onReplace ) {\n\tconst link = <a href={ url }>{ url }</a>;\n\tonReplace(\n\t\tcreateBlock( 'core/paragraph', { content: renderToString( link ) } )\n\t);\n}\n"]}
\No newline at end of file