UNPKG

@wordpress/block-editor

Version:
8 lines (7 loc) 3.12 kB
{ "version": 3, "sources": ["../../../src/components/media-placeholder/utils.js"], "sourcesContent": ["/**\n * Computes the accept attribute for file inputs based on allowed types\n * and server-supported MIME types.\n *\n * This ensures users can only select file types that the server can handle,\n * preventing upload failures (e.g., HEIC files when server lacks support).\n *\n * @param {Array} allowedTypes - List of allowed media types (e.g., ['image', 'video']).\n * @param {Object} allowedMimeTypes - Map of allowed MIME types from server settings.\n * @param {string} accept - Optional explicit accept attribute to use.\n *\n * @return {string|undefined} Computed accept attribute value, or undefined if no restrictions apply.\n */\nexport function getComputedAcceptAttribute(\n\tallowedTypes,\n\tallowedMimeTypes,\n\taccept\n) {\n\t// If accept prop is explicitly provided, use it as is.\n\tif ( accept ) {\n\t\treturn accept;\n\t}\n\n\t// If allowedMimeTypes is not available, fall back to wildcard.\n\tif (\n\t\t! allowedMimeTypes ||\n\t\ttypeof allowedMimeTypes !== 'object' ||\n\t\tObject.keys( allowedMimeTypes ).length === 0\n\t) {\n\t\tif ( allowedTypes && allowedTypes.length > 0 ) {\n\t\t\treturn allowedTypes.map( ( type ) => `${ type }/*` ).join( ',' );\n\t\t}\n\t\treturn undefined;\n\t}\n\n\t// If no allowedTypes specified, we can't filter, so return undefined.\n\tif ( ! allowedTypes || allowedTypes.length === 0 ) {\n\t\treturn undefined;\n\t}\n\n\t// Build a list of specific MIME types based on allowedTypes.\n\tconst acceptedMimeTypes = [];\n\n\tfor ( const [ , mimeType ] of Object.entries( allowedMimeTypes ) ) {\n\t\t// Check if this MIME type matches any of the allowedTypes.\n\t\tconst isAllowed = allowedTypes.some( ( allowedType ) => {\n\t\t\t// Support both 'image' and 'image/jpeg' formats.\n\t\t\tif ( allowedType.includes( '/' ) ) {\n\t\t\t\treturn mimeType === allowedType;\n\t\t\t}\n\t\t\treturn mimeType.startsWith( `${ allowedType }/` );\n\t\t} );\n\n\t\tif ( isAllowed ) {\n\t\t\tacceptedMimeTypes.push( mimeType );\n\t\t}\n\t}\n\n\t// If we found specific MIME types, use them. Otherwise fall back to wildcard.\n\tif ( acceptedMimeTypes.length > 0 ) {\n\t\treturn acceptedMimeTypes.join( ',' );\n\t}\n\n\treturn allowedTypes.map( ( type ) => `${ type }/*` ).join( ',' );\n}\n"], "mappings": ";AAaO,SAAS,2BACf,cACA,kBACA,QACC;AAED,MAAK,QAAS;AACb,WAAO;AAAA,EACR;AAGA,MACC,CAAE,oBACF,OAAO,qBAAqB,YAC5B,OAAO,KAAM,gBAAiB,EAAE,WAAW,GAC1C;AACD,QAAK,gBAAgB,aAAa,SAAS,GAAI;AAC9C,aAAO,aAAa,IAAK,CAAE,SAAU,GAAI,IAAK,IAAK,EAAE,KAAM,GAAI;AAAA,IAChE;AACA,WAAO;AAAA,EACR;AAGA,MAAK,CAAE,gBAAgB,aAAa,WAAW,GAAI;AAClD,WAAO;AAAA,EACR;AAGA,QAAM,oBAAoB,CAAC;AAE3B,aAAY,CAAE,EAAE,QAAS,KAAK,OAAO,QAAS,gBAAiB,GAAI;AAElE,UAAM,YAAY,aAAa,KAAM,CAAE,gBAAiB;AAEvD,UAAK,YAAY,SAAU,GAAI,GAAI;AAClC,eAAO,aAAa;AAAA,MACrB;AACA,aAAO,SAAS,WAAY,GAAI,WAAY,GAAI;AAAA,IACjD,CAAE;AAEF,QAAK,WAAY;AAChB,wBAAkB,KAAM,QAAS;AAAA,IAClC;AAAA,EACD;AAGA,MAAK,kBAAkB,SAAS,GAAI;AACnC,WAAO,kBAAkB,KAAM,GAAI;AAAA,EACpC;AAEA,SAAO,aAAa,IAAK,CAAE,SAAU,GAAI,IAAK,IAAK,EAAE,KAAM,GAAI;AAChE;", "names": [] }