@websolutespa/bom-mixer-ui 1.7.0 → 1.7.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,19 @@
1
1
  # @websolutespa/bom-mixer-ui
2
2
 
3
+ ## 1.7.2
4
+
5
+ ### Patch Changes
6
+
7
+ - Fixing: MediaImage aspectRatio
8
+
9
+ ## 1.7.1
10
+
11
+ ### Patch Changes
12
+
13
+ - 7898849: Added: Media image strategies Mixer, Oven
14
+ - Updated dependencies [7898849]
15
+ - @websolutespa/bom-mixer-store@1.7.2
16
+
3
17
  ## 1.7.0
4
18
 
5
19
  ### Minor Changes
package/dist/cjs/index.js CHANGED
@@ -11,6 +11,7 @@ var reactDom = require('react-dom');
11
11
  var bomMixerIcons = require('@websolutespa/bom-mixer-icons');
12
12
  var swiperCss = require('!!raw-loader!swiper/css');
13
13
  var react = require('swiper/react');
14
+ var bomMixerStore = require('@websolutespa/bom-mixer-store');
14
15
  var bomMixerModels = require('@websolutespa/bom-mixer-models');
15
16
  var Head = require('next/head');
16
17
  var router = require('next/router');
@@ -4326,15 +4327,31 @@ exports.ImageStrategy = void 0;
4326
4327
  (function (ImageStrategy) {
4327
4328
  ImageStrategy["None"] = "none";
4328
4329
  ImageStrategy["Next"] = "next";
4330
+ ImageStrategy["Mixer"] = "mixer";
4331
+ ImageStrategy["Oven"] = "oven";
4329
4332
  ImageStrategy["Custom"] = "custom";
4330
4333
  })(exports.ImageStrategy || (exports.ImageStrategy = {}));
4331
- const IMAGE_QUALITY = process.env.IMAGE_QUALITY ? parseInt(process.env.IMAGE_QUALITY) : 80;
4334
+ const IMAGE_SIZE_VARIANTS = ['xs', 'sm', 'md', 'lg', 'xl'];
4332
4335
  const IMAGE_SIZES = process.env.IMAGE_SIZES ? process.env.IMAGE_SIZES.split(',').map(x => parseInt(x)) : [640, 828, 1080, 1200, 1920];
4333
- const IMAGE_STRATEGY = process.env.IMAGE_STRATEGY ? process.env.IMAGE_STRATEGY : exports.ImageStrategy.Next;
4336
+ const IMAGE_SIZEMAP = Object.fromEntries(IMAGE_SIZES.map((x, i) => [IMAGE_SIZE_VARIANTS[i], x]));
4337
+ const IMAGE_QUALITY = process.env.IMAGE_QUALITY ? parseInt(process.env.IMAGE_QUALITY) : 80;
4338
+ const IMAGE_STRATEGY = process.env.IMAGE_STRATEGY ? process.env.IMAGE_STRATEGY : exports.ImageStrategy.Mixer;
4339
+ function isMediaSizeVariant(item) {
4340
+ return typeof item === 'string';
4341
+ // return typeof item === 'string' && IMAGE_SIZE_VARIANTS.includes(item);
4342
+ }
4343
+ function isMediaSizeVariants(item) {
4344
+ return bomCore.isObject(item) && Object.entries(item).reduce((p, _ref) => {
4345
+ let [k, v] = _ref;
4346
+ return p && typeof v === 'number';
4347
+ }, true);
4348
+ // return Array.isArray(item) && item.length > 0 && item.reduce((p, c) => p && typeof c === 'number', true);
4349
+ }
4334
4350
  const StyledMediaImage = styled__default.default.img`
4335
4351
  max-width: 100%;
4336
4352
  // height: auto;
4337
4353
  user-select: none;
4354
+ color: transparent;
4338
4355
  `;
4339
4356
  const MediaImage = _a => {
4340
4357
  var {
@@ -4343,53 +4360,133 @@ const MediaImage = _a => {
4343
4360
  loading = 'lazy'
4344
4361
  } = _a,
4345
4362
  props = __rest(_a, ["className", "type", "loading"]);
4346
- const classNames = bomCore.getClassNames(className, 'image', {
4363
+ const theme = React.useContext(styled.ThemeContext);
4364
+ if (!theme) {
4365
+ throw new Error('MediaImage must be used within ThemeProvider');
4366
+ }
4367
+ const classNames = bomCore.getClassNames(className, 'image', props.src && {
4347
4368
  'image-svg': isSVG(props.src)
4348
4369
  });
4349
- const imageProps = getImageProps(props);
4370
+ const imageProps = getImageProps(props, theme.mediaQuery);
4350
4371
  return jsxRuntime.jsx(StyledMediaImage, Object.assign({
4351
4372
  className: classNames,
4352
4373
  loading: loading
4353
- }, imageProps));
4374
+ }, imageProps, {
4375
+ draggable: "false",
4376
+ decoding: "async"
4377
+ }));
4354
4378
  };
4355
- function getImageProps(_a) {
4379
+ function getMediaSizes(mediaQuery, ratio, size) {
4380
+ if (size) {
4381
+ if (typeof size === 'number') {
4382
+ return Object.entries(ratio).map(_ref2 => {
4383
+ let [k, v] = _ref2;
4384
+ return [size, parseInt(String(mediaQuery[k])), v];
4385
+ });
4386
+ }
4387
+ if (isMediaSizeVariant(size)) {
4388
+ const sizeLimit = IMAGE_SIZEMAP[size] || 0;
4389
+ // console.log(size, ratio[size], sizeLimit);
4390
+ return Object.entries(IMAGE_SIZEMAP).filter(_ref3 => {
4391
+ let [k, v] = _ref3;
4392
+ return v <= sizeLimit;
4393
+ }).map(_ref4 => {
4394
+ let [k, v] = _ref4;
4395
+ return [v, parseInt(String(mediaQuery[k])), ratio[k]];
4396
+ });
4397
+ }
4398
+ }
4399
+ size = size || IMAGE_SIZEMAP;
4400
+ return Object.entries(size).map(_ref5 => {
4401
+ let [k, v] = _ref5;
4402
+ return [v, parseInt(String(mediaQuery[k])), ratio[k]];
4403
+ });
4404
+ }
4405
+ function getImageProps(_a, mediaQuery) {
4356
4406
  var {
4357
4407
  src
4358
4408
  } = _a,
4359
4409
  props = __rest(_a, ["src"]);
4360
- if (IMAGE_STRATEGY === exports.ImageStrategy.None || isSVG(src)) {
4410
+ if (IMAGE_STRATEGY === exports.ImageStrategy.None || !src || isSVG(src)) {
4361
4411
  return Object.assign({
4362
4412
  src
4363
4413
  }, props);
4364
4414
  }
4365
- const ratio = props.width && props.height ? parseInt(props.width.toString()) / parseInt(props.height.toString()) : 0;
4366
- const sizes = IMAGE_SIZES;
4415
+ const {
4416
+ width,
4417
+ height,
4418
+ size
4419
+ } = props,
4420
+ otherProps = __rest(props, ["width", "height", "size"]);
4421
+ const ratio = getMediaRatio(props);
4422
+ const sizes = getMediaSizes(mediaQuery, ratio, size);
4423
+ const imageSrc = getImageSrc(src, sizes[0][0], sizes[0][2]);
4424
+ const imageSrcSet = getImageSrcset(src, sizes);
4425
+ const imageSizes = getImageSizes(sizes);
4367
4426
  return Object.assign({
4368
- src: getImageSrc(src, ratio, sizes[0]),
4369
- srcSet: getImageSrcset(src, ratio, sizes),
4370
- sizes: ratio !== 0 ? getImageSizes(sizes) : '100vw'
4371
- }, props);
4427
+ src: imageSrc,
4428
+ srcSet: imageSrcSet,
4429
+ sizes: imageSizes
4430
+ }, otherProps);
4431
+ }
4432
+ function getMediaRatio(props) {
4433
+ let width = undefined;
4434
+ let height = undefined;
4435
+ if (props.width) {
4436
+ width = parseInt(String(props.width));
4437
+ }
4438
+ props.width = width;
4439
+ if (props.height) {
4440
+ height = parseInt(String(props.height));
4441
+ }
4442
+ props.height = height;
4443
+ const defaultRatio = width && height ? width / height : 0;
4444
+ const xs = getRatio(props.aspectRatio, defaultRatio);
4445
+ const sm = getRatio(props.aspectRatioSm, xs);
4446
+ const md = getRatio(props.aspectRatioMd, sm);
4447
+ const lg = getRatio(props.aspectRatioLg, md);
4448
+ const xl = getRatio(props.aspectRatioXl, lg);
4449
+ const ratio = {
4450
+ xs,
4451
+ sm,
4452
+ md,
4453
+ lg,
4454
+ xl
4455
+ };
4456
+ return ratio;
4457
+ }
4458
+ function getRatio(aspectRatio, defaultRatio) {
4459
+ if (defaultRatio === void 0) {
4460
+ defaultRatio = 0;
4461
+ }
4462
+ return typeof aspectRatio === 'string' ? parseFloat(aspectRatio) : aspectRatio || defaultRatio;
4372
4463
  }
4373
- function getImageSrc(src, ratio, width, quality) {
4464
+ const BASE_URLS = {
4465
+ [exports.ImageStrategy.Mixer]: '/api/_image',
4466
+ [exports.ImageStrategy.Next]: '/_next/image',
4467
+ [exports.ImageStrategy.Oven]: bomMixerStore.getRemoteApiUrl('/_image')
4468
+ };
4469
+ const BASE_URL = BASE_URLS[IMAGE_STRATEGY];
4470
+ function getImageSrc(src, width, ratio, quality) {
4374
4471
  if (quality === void 0) {
4375
4472
  quality = IMAGE_QUALITY;
4376
4473
  }
4377
4474
  if (isSVG(src)) {
4378
4475
  return src;
4379
4476
  }
4380
- return ratio !== 0 ? `/_next/image?url=${encodeURIComponent(src)}&w=${width}&h=${width / ratio}&q=${quality}` : `/_next/image?url=${encodeURIComponent(src)}&w=${width}&q=${quality}`;
4477
+ return ratio !== 0 ? `${BASE_URL}?url=${encodeURIComponent(src)}&w=${width}&h=${Math.round(width / ratio)}&q=${quality}` : `${BASE_URL}?url=${encodeURIComponent(src)}&w=${width}&q=${quality}`;
4381
4478
  }
4382
- function getImageSrcset(src, ratio, widths, quality) {
4479
+ function getImageSrcset(src, sizes, quality) {
4383
4480
  if (quality === void 0) {
4384
4481
  quality = IMAGE_QUALITY;
4385
4482
  }
4386
4483
  if (isSVG(src)) {
4387
4484
  return undefined;
4388
4485
  }
4389
- return widths.map(width => getImageSrc(src, ratio, width, quality) + ` ${width}w`).join(', ');
4486
+ return sizes.length > 1 ? sizes.map(size => getImageSrc(src, size[0], size[2], quality) + ` ${size[0]}w`).join(', ') : undefined;
4390
4487
  }
4391
- function getImageSizes(widths) {
4392
- return widths.map((width, i) => `${i < widths.length - 1 ? `(max-width: ${width}px) ` : ''}${width}px`).join(', ');
4488
+ function getImageSizes(sizes) {
4489
+ return sizes.length > 1 ? sizes.map((size, i) => `${i < sizes.length - 1 ? `(max-width: ${sizes[i + 1][1]}px) ` : ''}${size[0]}px`).join(', ') : undefined;
4393
4490
  }
4394
4491
  function isSVG(src) {
4395
4492
  return src.indexOf('.svg') !== -1;
@@ -4440,7 +4537,7 @@ const StyledMedia$1 = styled__default.default.div`
4440
4537
  &>video,
4441
4538
  &>.video {
4442
4539
  object-fit: cover;
4443
- background: var(--color-neutral-200);
4540
+ background: transparent; // var(--color-neutral-200);
4444
4541
  }
4445
4542
 
4446
4543
  &>svg {
@@ -4506,12 +4603,15 @@ const MediaBase = /*#__PURE__*/React.forwardRef((_a, ref) => {
4506
4603
  item,
4507
4604
  onClick,
4508
4605
  className,
4606
+ size,
4607
+ width,
4608
+ height,
4509
4609
  eager,
4510
4610
  alt,
4511
4611
  title,
4512
4612
  as = 'div'
4513
4613
  } = _a,
4514
- props = __rest(_a, ["children", "item", "onClick", "className", "eager", "alt", "title", "as"]);
4614
+ props = __rest(_a, ["children", "item", "onClick", "className", "size", "width", "height", "eager", "alt", "title", "as"]);
4515
4615
  const {
4516
4616
  locale
4517
4617
  } = bomMixerHooks.useLayout();
@@ -4523,6 +4623,14 @@ const MediaBase = /*#__PURE__*/React.forwardRef((_a, ref) => {
4523
4623
  'media--gallery': id
4524
4624
  });
4525
4625
  const mediaChildren = item && !children ? getMediaItems(item, {
4626
+ width,
4627
+ height,
4628
+ size,
4629
+ aspectRatio: props.aspectRatio,
4630
+ aspectRatioSm: props.aspectRatioSm,
4631
+ aspectRatioMd: props.aspectRatioMd,
4632
+ aspectRatioLg: props.aspectRatioLg,
4633
+ aspectRatioXl: props.aspectRatioXl,
4526
4634
  eager,
4527
4635
  galleryId: id,
4528
4636
  alt,
@@ -4574,27 +4682,39 @@ function getMediaItems(itemOrItems, options) {
4574
4682
  if (x.alt) {
4575
4683
  mediaItem.alt = x.alt;
4576
4684
  }
4577
- if (x.height) {
4578
- mediaItem.height = x.height;
4579
- }
4580
4685
  if (x.mimeType) {
4581
4686
  mediaItem.mimeType = x.mimeType;
4582
4687
  }
4583
4688
  if (x.title) {
4584
4689
  mediaItem.title = x.title;
4585
4690
  }
4586
- if (x.width) {
4691
+ if (options.width) {
4692
+ mediaItem.width = parseInt(String(options.width));
4693
+ } else if (x.width) {
4587
4694
  mediaItem.width = x.width;
4588
4695
  }
4696
+ if (options.height) {
4697
+ mediaItem.height = parseInt(String(options.height));
4698
+ } else if (x.height) {
4699
+ mediaItem.height = x.height;
4700
+ }
4701
+ mediaItem.aspectRatio = options.aspectRatio;
4702
+ mediaItem.aspectRatioSm = options.aspectRatioSm;
4703
+ mediaItem.aspectRatioMd = options.aspectRatioMd;
4704
+ mediaItem.aspectRatioLg = options.aspectRatioLg;
4705
+ mediaItem.aspectRatioXl = options.aspectRatioXl;
4589
4706
  return mediaItem;
4590
4707
  });
4591
4708
  const hasGallery = options.galleryId != null && typeof options.open === 'function';
4592
4709
  const getMediaItemProps = media => {
4593
4710
  const title = media.title || options.title || media.alt || options.alt;
4594
4711
  const alt = media.alt || options.alt || media.title || options.title;
4712
+ const src = media.src || media.url || '';
4595
4713
  const props = {
4714
+ src,
4596
4715
  title,
4597
- alt
4716
+ alt,
4717
+ size: options.size
4598
4718
  };
4599
4719
  if (hasGallery) {
4600
4720
  props['data-gallery'] = JSON.stringify({
@@ -4608,11 +4728,16 @@ function getMediaItems(itemOrItems, options) {
4608
4728
  };
4609
4729
  }
4610
4730
  if (bomCore.resolveMediaType(media) === 'image') {
4611
- const aspectRatio = typeof options.aspectRatio === 'string' ? parseFloat(options.aspectRatio) : options.aspectRatio;
4612
- if (aspectRatio != null) {
4613
- props.width = 800;
4614
- props.height = 800 / aspectRatio;
4731
+ let width = undefined;
4732
+ let height = undefined;
4733
+ if (options.width) {
4734
+ width = parseInt(String(options.width));
4735
+ }
4736
+ props.width = width;
4737
+ if (options.height) {
4738
+ height = parseInt(String(options.height));
4615
4739
  }
4740
+ props.height = height;
4616
4741
  if (options.eager) {
4617
4742
  props.loading = 'eager';
4618
4743
  }
@@ -4635,7 +4760,7 @@ function getMediaItems(itemOrItems, options) {
4635
4760
  srcLang: options.locale,
4636
4761
  label: "Video Captions"
4637
4762
  })]
4638
- }), m) : jsxRuntime.jsx(MediaImage, Object.assign({}, media, {
4763
+ }), m) : jsxRuntime.jsx(MediaImage, Object.assign({
4639
4764
  draggable: false
4640
4765
  }, getMediaItemProps(media)), m));
4641
4766
  }
@@ -20457,6 +20582,8 @@ exports.isCSSNumberValue = isCSSNumberValue;
20457
20582
  exports.isChildElement = isChildElement;
20458
20583
  exports.isCssProp = isCssProp;
20459
20584
  exports.isMac = isMac;
20585
+ exports.isMediaSizeVariant = isMediaSizeVariant;
20586
+ exports.isMediaSizeVariants = isMediaSizeVariants;
20460
20587
  exports.isMixerElement = isMixerElement;
20461
20588
  exports.labelVariants = labelVariants;
20462
20589
  exports.mapChildsByType = mapChildsByType;