@shopgate/engage
Version:
Shopgate's ENGAGE library.
27 lines • 3.24 kB
JavaScript
import{CONFIGURATION_COLLECTION_CREATE_EXTERNAL_IMAGE_URL}from'@shopgate/engage/core/constants';import{configuration}from'@shopgate/engage/core/collections';import{getProductImageSettings}from"../../product/helpers";import{getImageFormat}from"./getImageFormat";/**
* Regex to detect the "fill" query parameter
*/var fillParamRegex=/(fill|fillc)=(\w+)%2C(\d)($|&)/i;/**
* Appends image service parameters to an image service url
* @param {string} baseUrl The base url
* @param {Object} params Additional params for the url
* @returns {string}
*/var buildUrl=function buildUrl(baseUrl){var params=arguments.length>1&&arguments[1]!==undefined?arguments[1]:{};var parsedUrl=new URL(baseUrl);/**
* To improve caching of images, we need to create urls with a consistent order of query params.
* Otherwise it can happen that different urls create equal images, just because of different
* ordered query params.
* To reach this goal we utilize the "append" method of the URLSearchParams object and apply
* the params in the order given by the "params" object. Since "append" could create duplicate
* query params, each param needs to be removed from the URLSearchParams before it's added again.
*/Object.keys(params).forEach(function(key){parsedUrl.searchParams["delete"](key);return parsedUrl.searchParams.append(key,params[key]);});/**
* The "fill" parameter contains a ",". Since this would be url encoded by toString(), and the
* service will not recognize it anymore, we need to replace the encoding with a ",".
*/return parsedUrl.toString().replace(fillParamRegex,'$1=$2,$3$4');};/**
* Returns the actual url to the image, by adding url parameters with the dimensions for img-cdn
* @param {string} src Source to the image.
* @param {Object} dimension Dimension of the requested image.
* @param {number} dimension.width Width in pixels.
* @param {number} dimension.height Height in pixels.
* @returns {string}
*/export var getFullImageSource=function getFullImageSource(src){var _ref=arguments.length>1&&arguments[1]!==undefined?arguments[1]:{},width=_ref.width,height=_ref.height;if(src&&src.includes('images.shopgate.services/v2/images')){var _getProductImageSetti2=getProductImageSettings(),fillColor=_getProductImageSetti2.fillColor,quality=_getProductImageSetti2.quality;var format=getImageFormat();return buildUrl(src,{format:format,width:width,height:height,quality:quality,fill:fillColor.replace('#','')});}if(src&&src.startsWith('https://img-cdn.shopgate.com')&&!src.includes('?')){return buildUrl(src,{w:width,h:height,q:70,zd:'resize',fillc:'FFFFFF'});}// Check if an extension registered an external image url handler within the config collection.
var createUrlFn=configuration.get(CONFIGURATION_COLLECTION_CREATE_EXTERNAL_IMAGE_URL);if(typeof createUrlFn==='function'){var _getProductImageSetti4=getProductImageSettings(),_fillColor=_getProductImageSetti4.fillColor,_quality=_getProductImageSetti4.quality;var _format=getImageFormat();// Invoke the handler with all relevant parameters.
var externalUrl=createUrlFn(src,{width:width,height:height,fillColor:_fillColor,quality:_quality,format:_format});if(!!externalUrl&&typeof externalUrl==='string'){return externalUrl;}}return src;};