UNPKG

@wordpress/block-library

Version:
8 lines (7 loc) 3.96 kB
{ "version": 3, "sources": ["../../src/image/utils.js"], "sourcesContent": ["/**\n * Internal dependencies\n */\nimport { NEW_TAB_REL, ALLOWED_MEDIA_TYPES } from './constants';\n\n/**\n * Evaluates a CSS aspect-ratio property value as a number.\n *\n * Degenerate or invalid ratios behave as 'auto'. And 'auto' ratios return NaN.\n *\n * @see https://drafts.csswg.org/css-sizing-4/#aspect-ratio\n *\n * @param {string} value CSS aspect-ratio property value.\n * @return {number} Numerical aspect ratio or NaN if invalid.\n */\nexport function evalAspectRatio( value ) {\n\tconst [ width, height = 1 ] = value.split( '/' ).map( Number );\n\tconst aspectRatio = width / height;\n\treturn aspectRatio === Infinity || aspectRatio === 0 ? NaN : aspectRatio;\n}\n\nexport function removeNewTabRel( currentRel ) {\n\tlet newRel = currentRel;\n\n\tif ( currentRel !== undefined && newRel ) {\n\t\tNEW_TAB_REL.forEach( ( relVal ) => {\n\t\t\tconst regExp = new RegExp( '\\\\b' + relVal + '\\\\b', 'gi' );\n\t\t\tnewRel = newRel.replace( regExp, '' );\n\t\t} );\n\n\t\t// Only trim if NEW_TAB_REL values was replaced.\n\t\tif ( newRel !== currentRel ) {\n\t\t\tnewRel = newRel.trim();\n\t\t}\n\n\t\tif ( ! newRel ) {\n\t\t\tnewRel = undefined;\n\t\t}\n\t}\n\n\treturn newRel;\n}\n\n/**\n * Helper to get the link target settings to be stored.\n *\n * @param {boolean} value The new link target value.\n * @param {Object} attributes Block attributes.\n * @param {Object} attributes.rel Image block's rel attribute.\n *\n * @return {Object} Updated link target settings.\n */\nexport function getUpdatedLinkTargetSettings( value, { rel } ) {\n\tconst linkTarget = value ? '_blank' : undefined;\n\n\tlet updatedRel;\n\tif ( ! linkTarget && ! rel ) {\n\t\tupdatedRel = undefined;\n\t} else {\n\t\tupdatedRel = removeNewTabRel( rel );\n\t}\n\n\treturn {\n\t\tlinkTarget,\n\t\trel: updatedRel,\n\t};\n}\n\n/**\n * Determines new Image block attributes size selection.\n *\n * @param {Object} image Media file object for gallery image.\n * @param {string} size Selected size slug to apply.\n */\nexport function getImageSizeAttributes( image, size ) {\n\tconst url = image?.media_details?.sizes?.[ size ]?.source_url;\n\n\tif ( url ) {\n\t\treturn { url, width: undefined, height: undefined, sizeSlug: size };\n\t}\n\n\treturn {};\n}\n\n/**\n * Checks if the file has a valid file type.\n *\n * @param {File} file - The file to check.\n * @return {boolean} - Returns true if the file has a valid file type, otherwise false.\n */\nexport function isValidFileType( file ) {\n\treturn ALLOWED_MEDIA_TYPES.some(\n\t\t( mediaType ) => file.type.indexOf( mediaType ) === 0\n\t);\n}\n\nexport function mediaPosition( { x, y } = { x: 0.5, y: 0.5 } ) {\n\treturn `${ Math.round( x * 100 ) }% ${ Math.round( y * 100 ) }%`;\n}\n"], "mappings": ";AAGA,SAAS,aAAa,2BAA2B;AAY1C,SAAS,gBAAiB,OAAQ;AACxC,QAAM,CAAE,OAAO,SAAS,CAAE,IAAI,MAAM,MAAO,GAAI,EAAE,IAAK,MAAO;AAC7D,QAAM,cAAc,QAAQ;AAC5B,SAAO,gBAAgB,YAAY,gBAAgB,IAAI,MAAM;AAC9D;AAEO,SAAS,gBAAiB,YAAa;AAC7C,MAAI,SAAS;AAEb,MAAK,eAAe,UAAa,QAAS;AACzC,gBAAY,QAAS,CAAE,WAAY;AAClC,YAAM,SAAS,IAAI,OAAQ,QAAQ,SAAS,OAAO,IAAK;AACxD,eAAS,OAAO,QAAS,QAAQ,EAAG;AAAA,IACrC,CAAE;AAGF,QAAK,WAAW,YAAa;AAC5B,eAAS,OAAO,KAAK;AAAA,IACtB;AAEA,QAAK,CAAE,QAAS;AACf,eAAS;AAAA,IACV;AAAA,EACD;AAEA,SAAO;AACR;AAWO,SAAS,6BAA8B,OAAO,EAAE,IAAI,GAAI;AAC9D,QAAM,aAAa,QAAQ,WAAW;AAEtC,MAAI;AACJ,MAAK,CAAE,cAAc,CAAE,KAAM;AAC5B,iBAAa;AAAA,EAC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