@bigbinary/neeto-themes-frontend 1.2.3 → 1.2.4

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/dist/index.js CHANGED
@@ -8,26 +8,26 @@ import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
8
8
  import { isNotPresent, snakeToCamelCase, filterBy, isPresent, isNot, findBy, findIndexBy, toLabelAndValue, humanize, isNotEmpty } from '@bigbinary/neeto-cist';
9
9
  import Alert from '@bigbinary/neetoui/Alert';
10
10
  import { useTranslation, Trans } from 'react-i18next';
11
- import '@babel/runtime/helpers/asyncToGenerator';
12
- import '@babel/runtime/regenerator';
13
- import '@bigbinary/neetoui/Toastr';
11
+ import _asyncToGenerator from '@babel/runtime/helpers/asyncToGenerator';
12
+ import _regeneratorRuntime from '@babel/runtime/regenerator';
13
+ import Toastr from '@bigbinary/neetoui/Toastr';
14
14
  import { useQuery } from 'react-query';
15
15
  import axios from 'axios';
16
16
  import _objectWithoutProperties from '@babel/runtime/helpers/objectWithoutProperties';
17
17
  import { prop, isEmpty, negate } from 'ramda';
18
18
  import _defineProperty from '@babel/runtime/helpers/defineProperty';
19
- import { Close, LeftArrow, Plus } from '@bigbinary/neeto-icons';
19
+ import { LeftAlign, CenterAlign, Close, LeftArrow, Plus } from '@bigbinary/neeto-icons';
20
20
  import Button from '@bigbinary/neetoui/Button';
21
21
  import Popover from '@bigbinary/neetoui/Popover';
22
22
  import Typography from '@bigbinary/neetoui/Typography';
23
23
  import Form from '@bigbinary/neetoui/formik/Form';
24
24
  import * as yup from 'yup';
25
- import { useFormikContext } from 'formik';
25
+ import { useFormikContext, Field } from 'formik';
26
26
  import { jsxs, jsx, Fragment } from 'react/jsx-runtime';
27
27
  import { ImageUploader } from '@bigbinary/neeto-image-uploader-frontend';
28
28
  import { joinHyphenCase } from '@bigbinary/neeto-commons-frontend/utils';
29
- import ColorPicker from '@bigbinary/neetoui/ColorPicker';
30
29
  import Select from '@bigbinary/neetoui/Select';
30
+ import ColorPicker from '@bigbinary/neetoui/ColorPicker';
31
31
  import { Slider } from 'antd';
32
32
  import Switch from '@bigbinary/neetoui/Switch';
33
33
  import Input from '@bigbinary/neetoui/formik/Input';
@@ -57,6 +57,52 @@ var useThemeStore = create$1(withImmutableActions(function (set) {
57
57
  };
58
58
  }));
59
59
 
60
+ var getImageFromUrl = function getImageFromUrl(source) {
61
+ var image = new Image();
62
+ image.crossOrigin = "anonymous";
63
+ image.src = source;
64
+ return new Promise(function (resolve) {
65
+ image.onload = function () {
66
+ resolve(image);
67
+ };
68
+ });
69
+ };
70
+ var getDominantColorFromUrl = /*#__PURE__*/function () {
71
+ var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(imageUrl) {
72
+ var image, context, c;
73
+ return _regeneratorRuntime.wrap(function _callee$(_context) {
74
+ while (1) switch (_context.prev = _context.next) {
75
+ case 0:
76
+ _context.prev = 0;
77
+ _context.next = 3;
78
+ return getImageFromUrl(imageUrl);
79
+ case 3:
80
+ image = _context.sent;
81
+ context = document.createElement("canvas").getContext("2d");
82
+ context === null || context === void 0 || context.drawImage(image, 0, 0, 1, 1);
83
+ c = context === null || context === void 0 ? void 0 : context.getImageData(0, 0, 1, 1).data;
84
+ if (!c) {
85
+ _context.next = 9;
86
+ break;
87
+ }
88
+ return _context.abrupt("return", "#".concat(((1 << 24) + (c[0] << 16) + (c[1] << 8) + c[2]).toString(16).slice(1)));
89
+ case 9:
90
+ return _context.abrupt("return", null);
91
+ case 12:
92
+ _context.prev = 12;
93
+ _context.t0 = _context["catch"](0);
94
+ Toastr.error(_context.t0);
95
+ return _context.abrupt("return", null);
96
+ case 16:
97
+ case "end":
98
+ return _context.stop();
99
+ }
100
+ }, _callee, null, [[0, 12]]);
101
+ }));
102
+ return function getDominantColorFromUrl(_x) {
103
+ return _ref.apply(this, arguments);
104
+ };
105
+ }();
60
106
  var scrollElementIntoView = function scrollElementIntoView(elementId) {
61
107
  var elementToScroll = document.getElementById(elementId);
62
108
  elementToScroll === null || elementToScroll === void 0 || elementToScroll.scrollIntoView({
@@ -195,6 +241,8 @@ var setTheme = function setTheme(theme, variableNamesMap, themeConfig) {
195
241
  var overlayOpacity = Number(property.value);
196
242
  setVariable(snakeToCamelCase("".concat(property.key, "_overlay_opacity")), Math.abs(overlayOpacity / 100), variableNamesMap);
197
243
  setVariable(snakeToCamelCase("".concat(property.key, "_overlay_color")), overlayOpacity < 0 ? BLACK : WHITE, variableNamesMap);
244
+ } else if (property.kind === "radius") {
245
+ setVariable(attribute, "".concat(property.value, "px"), variableNamesMap);
198
246
  } else {
199
247
  setVariable(attribute, property.value, variableNamesMap);
200
248
  }
@@ -381,6 +429,12 @@ var buildInitialValues = function buildInitialValues(theme, themePropertiesSchem
381
429
  kind: "slider",
382
430
  key: key
383
431
  };
432
+ case "radius":
433
+ return {
434
+ value: defaultValue,
435
+ kind: "radius",
436
+ key: key
437
+ };
384
438
  case "boolean":
385
439
  return {
386
440
  value: String(defaultValue),
@@ -393,6 +447,12 @@ var buildInitialValues = function buildInitialValues(theme, themePropertiesSchem
393
447
  kind: "position_selector",
394
448
  key: key
395
449
  };
450
+ case "alignment_block":
451
+ return {
452
+ value: defaultValue,
453
+ kind: "alignment_block",
454
+ key: key
455
+ };
396
456
  default:
397
457
  return null;
398
458
  }
@@ -410,23 +470,26 @@ var buildImageData = function buildImageData(values, themePropertiesSchema) {
410
470
  }, {});
411
471
  };
412
472
  var isPropertyVisible = function isPropertyVisible(key, formikValues, themePropertiesSchema) {
413
- var _findBy;
414
473
  var property = findBy({
415
474
  key: key
416
475
  }, themePropertiesSchema);
417
476
  if (isNotPresent(property)) return false;
477
+ if (property !== null && property !== void 0 && property.hidden) return false;
418
478
  var conditionalKey = property === null || property === void 0 ? void 0 : property.dependsOn;
419
479
  if (isNotPresent(conditionalKey)) return true;
420
- var dependentProperty = findBy({
480
+ var dependentPropertySchema = findBy({
421
481
  key: conditionalKey
422
482
  }, themePropertiesSchema);
423
- if (dependentProperty.kind === "image") {
483
+ var dependentProperty = findBy({
484
+ key: conditionalKey
485
+ }, formikValues.properties);
486
+ if (dependentPropertySchema.kind === "image") {
424
487
  var _formikValues$snakeTo;
425
488
  return isPresent((_formikValues$snakeTo = formikValues[snakeToCamelCase(conditionalKey)]) === null || _formikValues$snakeTo === void 0 ? void 0 : _formikValues$snakeTo.url);
489
+ } else if (dependentPropertySchema.kind === "boolean") {
490
+ return (dependentProperty === null || dependentProperty === void 0 ? void 0 : dependentProperty.value) !== "false";
426
491
  }
427
- return isPresent((_findBy = findBy({
428
- key: conditionalKey
429
- }, formikValues.properties)) === null || _findBy === void 0 ? void 0 : _findBy.value);
492
+ return isPresent(dependentProperty === null || dependentProperty === void 0 ? void 0 : dependentProperty.value);
430
493
  };
431
494
  var buildLabel = function buildLabel(key, kind) {
432
495
  return t$1("neetoThemes.properties.".concat(snakeToCamelCase(kind), ".").concat(snakeToCamelCase(key)));
@@ -454,6 +517,32 @@ var Card$2 = function Card(_ref) {
454
517
  });
455
518
  };
456
519
 
520
+ var SelectBlock = function SelectBlock(_ref) {
521
+ var label = _ref.label,
522
+ _ref$placeholder = _ref.placeholder,
523
+ placeholder = _ref$placeholder === void 0 ? "" : _ref$placeholder,
524
+ value = _ref.value,
525
+ options = _ref.options,
526
+ onChange = _ref.onChange;
527
+ return /*#__PURE__*/jsxs("div", {
528
+ className: "flex items-center justify-between py-3 first:pt-0 last:pb-0",
529
+ "data-cy": "theme-style-".concat(joinHyphenCase(label)),
530
+ children: [/*#__PURE__*/jsx(Typography, {
531
+ "data-cy": "logo-field-labels",
532
+ style: "body2",
533
+ children: label
534
+ }), /*#__PURE__*/jsx("div", {
535
+ className: "w-40",
536
+ children: /*#__PURE__*/jsx(Select, {
537
+ onChange: onChange,
538
+ options: options,
539
+ placeholder: placeholder,
540
+ value: value
541
+ })
542
+ })]
543
+ });
544
+ };
545
+
457
546
  var DESIGN_SCREENS = {
458
547
  THEMES: "Themes",
459
548
  Customize: "Customize"
@@ -461,6 +550,7 @@ var DESIGN_SCREENS = {
461
550
  var ONE_MEGABYTE = 1;
462
551
 
463
552
  var ImageBlock = function ImageBlock(_ref) {
553
+ var _findBy;
464
554
  var label = _ref.label,
465
555
  name = _ref.name,
466
556
  _ref$imageData = _ref.imageData,
@@ -468,22 +558,73 @@ var ImageBlock = function ImageBlock(_ref) {
468
558
  _ref$uploadConfig = _ref.uploadConfig,
469
559
  uploadConfig = _ref$uploadConfig === void 0 ? {
470
560
  maxImageSize: ONE_MEGABYTE
471
- } : _ref$uploadConfig;
561
+ } : _ref$uploadConfig,
562
+ attribute = _ref.attribute;
563
+ var _useTranslation = useTranslation(),
564
+ t = _useTranslation.t;
472
565
  var _useFormikContext = useFormikContext(),
473
- setFieldValue = _useFormikContext.setFieldValue;
474
- var handleImageChange = function handleImageChange(image) {
475
- if (isEmpty(image.signedId) || isEmpty(image.url)) {
476
- setFieldValue(name, {
477
- signedId: null,
478
- url: image.url
479
- });
480
- return;
481
- }
482
- setFieldValue(name, {
483
- signedId: image.signedId,
484
- url: image.url
485
- });
486
- };
566
+ setFieldValue = _useFormikContext.setFieldValue,
567
+ values = _useFormikContext.values;
568
+ var _useConfigStore = useConfigStore(function (store) {
569
+ return {
570
+ themePropertiesSchema: store["themePropertiesSchema"]
571
+ };
572
+ }, shallow),
573
+ themePropertiesSchema = _useConfigStore.themePropertiesSchema;
574
+ var propertySchema = findBy({
575
+ key: attribute
576
+ }, themePropertiesSchema);
577
+ var enablePositionChanger = (propertySchema === null || propertySchema === void 0 ? void 0 : propertySchema.enablePositionChanger) || false;
578
+ var options = propertySchema === null || propertySchema === void 0 ? void 0 : propertySchema.options;
579
+ var positionKeyInSchema = findBy({
580
+ key: "".concat(name, "_position")
581
+ }, themePropertiesSchema);
582
+ var isPositionChangerVisible = enablePositionChanger && (imageData === null || imageData === void 0 ? void 0 : imageData.url) && isPresent(positionKeyInSchema);
583
+ var positionKeyIndex = findIndexBy({
584
+ key: "".concat(name, "_position")
585
+ }, values.properties);
586
+ var handleImageChange = /*#__PURE__*/function () {
587
+ var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(image) {
588
+ var dominantColor;
589
+ return _regeneratorRuntime.wrap(function _callee$(_context) {
590
+ while (1) switch (_context.prev = _context.next) {
591
+ case 0:
592
+ _context.prev = 0;
593
+ if (!(isEmpty(image.signedId) || isEmpty(image.url))) {
594
+ _context.next = 4;
595
+ break;
596
+ }
597
+ setFieldValue(name, {
598
+ signedId: null,
599
+ url: image.url
600
+ });
601
+ return _context.abrupt("return");
602
+ case 4:
603
+ _context.next = 6;
604
+ return getDominantColorFromUrl(image.url);
605
+ case 6:
606
+ dominantColor = _context.sent;
607
+ setFieldValue(name, {
608
+ signedId: image.signedId,
609
+ url: image.url,
610
+ dominantColor: dominantColor
611
+ });
612
+ _context.next = 13;
613
+ break;
614
+ case 10:
615
+ _context.prev = 10;
616
+ _context.t0 = _context["catch"](0);
617
+ Toastr.error(_context.t0);
618
+ case 13:
619
+ case "end":
620
+ return _context.stop();
621
+ }
622
+ }, _callee, null, [[0, 10]]);
623
+ }));
624
+ return function handleImageChange(_x) {
625
+ return _ref2.apply(this, arguments);
626
+ };
627
+ }();
487
628
  return /*#__PURE__*/jsxs("div", {
488
629
  className: "space-y-3 py-3 first:pt-0 last:pb-0",
489
630
  "data-cy": "theme-style-".concat(joinHyphenCase(label)),
@@ -494,12 +635,25 @@ var ImageBlock = function ImageBlock(_ref) {
494
635
  style: "body2",
495
636
  children: label
496
637
  })
497
- }), /*#__PURE__*/createElement(ImageUploader, {
498
- uploadConfig: uploadConfig,
499
- className: "h-40 w-full",
500
- key: imageData === null || imageData === void 0 ? void 0 : imageData.url,
501
- src: imageData === null || imageData === void 0 ? void 0 : imageData.url,
502
- onUploadComplete: handleImageChange
638
+ }), /*#__PURE__*/jsxs("div", {
639
+ className: "space-y-3 divide-y divide-gray-200",
640
+ children: [/*#__PURE__*/createElement(ImageUploader, {
641
+ uploadConfig: uploadConfig,
642
+ className: "h-40 w-full",
643
+ key: imageData === null || imageData === void 0 ? void 0 : imageData.url,
644
+ src: imageData === null || imageData === void 0 ? void 0 : imageData.url,
645
+ onUploadComplete: handleImageChange
646
+ }), isPositionChangerVisible && /*#__PURE__*/jsx(SelectBlock, {
647
+ label: buildLabel("".concat(name, "Position"), "image"),
648
+ options: options || POSITION_OPTIONS,
649
+ placeholder: t("neetoThemes.common.left"),
650
+ value: toLabelAndValue((_findBy = findBy({
651
+ key: "".concat(name, "_position")
652
+ }, values.properties)) === null || _findBy === void 0 ? void 0 : _findBy.value),
653
+ onChange: function onChange(option) {
654
+ return setFieldValue("properties[".concat(positionKeyIndex, "].value"), option.value);
655
+ }
656
+ })]
503
657
  })]
504
658
  });
505
659
  };
@@ -516,6 +670,7 @@ var Images = function Images(_ref) {
516
670
  children: /*#__PURE__*/jsx("div", {
517
671
  className: "divide-y divide-gray-200",
518
672
  children: /*#__PURE__*/jsx(ImageBlock, {
673
+ attribute: attribute,
519
674
  imageData: imageData,
520
675
  name: key
521
676
  })
@@ -523,6 +678,54 @@ var Images = function Images(_ref) {
523
678
  });
524
679
  };
525
680
 
681
+ var AlignmentBlock = function AlignmentBlock(_ref) {
682
+ var label = _ref.label,
683
+ name = _ref.name;
684
+ return /*#__PURE__*/jsxs("div", {
685
+ className: "flex items-center justify-between gap-2 py-3 first:pt-0 last:pb-0",
686
+ "data-cy": joinHyphenCase(label),
687
+ children: [/*#__PURE__*/jsx(Typography, {
688
+ "data-cy": "style-fields-labels",
689
+ style: "body2",
690
+ children: label
691
+ }), /*#__PURE__*/jsx(Field, {
692
+ name: name,
693
+ children: function children(_ref2) {
694
+ var value = _ref2.field.value,
695
+ setFieldValue = _ref2.form.setFieldValue;
696
+ return /*#__PURE__*/jsxs("div", {
697
+ className: "flex items-center gap-3",
698
+ children: [/*#__PURE__*/jsx(Button, {
699
+ "data-cy": "alignment-left",
700
+ icon: LeftAlign,
701
+ size: "large",
702
+ style: value === "left" ? "primary" : "text",
703
+ tooltipProps: {
704
+ content: "Left align",
705
+ position: "bottom"
706
+ },
707
+ onClick: function onClick() {
708
+ return setFieldValue(name, "left");
709
+ }
710
+ }), /*#__PURE__*/jsx(Button, {
711
+ "data-cy": "alignment-center",
712
+ icon: CenterAlign,
713
+ size: "large",
714
+ style: value === "center" ? "primary" : "text",
715
+ tooltipProps: {
716
+ content: "Center align",
717
+ position: "bottom"
718
+ },
719
+ onClick: function onClick() {
720
+ return setFieldValue(name, "center");
721
+ }
722
+ })]
723
+ });
724
+ }
725
+ })]
726
+ });
727
+ };
728
+
526
729
  var ColorBlock = function ColorBlock(_ref) {
527
730
  var label = _ref.label,
528
731
  _ref$color = _ref.color,
@@ -582,32 +785,6 @@ var FontPickerBlock = function FontPickerBlock(_ref) {
582
785
  });
583
786
  };
584
787
 
585
- var SelectBlock = function SelectBlock(_ref) {
586
- var label = _ref.label,
587
- _ref$placeholder = _ref.placeholder,
588
- placeholder = _ref$placeholder === void 0 ? "" : _ref$placeholder,
589
- value = _ref.value,
590
- options = _ref.options,
591
- onChange = _ref.onChange;
592
- return /*#__PURE__*/jsxs("div", {
593
- className: "flex items-center justify-between py-3 first:pt-0 last:pb-0",
594
- "data-cy": "theme-style-".concat(joinHyphenCase(label)),
595
- children: [/*#__PURE__*/jsx(Typography, {
596
- "data-cy": "logo-field-labels",
597
- style: "body2",
598
- children: label
599
- }), /*#__PURE__*/jsx("div", {
600
- className: "w-40",
601
- children: /*#__PURE__*/jsx(Select, {
602
- onChange: onChange,
603
- options: options,
604
- placeholder: placeholder,
605
- value: value
606
- })
607
- })]
608
- });
609
- };
610
-
611
788
  var SliderBlock = function SliderBlock(_ref) {
612
789
  var label = _ref.label,
613
790
  onChange = _ref.onChange,
@@ -704,7 +881,7 @@ var Properties = function Properties(_ref) {
704
881
  }
705
882
  };
706
883
  useEffect(function () {
707
- onPropertiesChange === null || onPropertiesChange === void 0 || onPropertiesChange(values, setFieldValue);
884
+ onPropertiesChange === null || onPropertiesChange === void 0 || onPropertiesChange(values, handleChange, handleColorChange);
708
885
  setTheme(values);
709
886
  setThemeState({
710
887
  previewingTheme: values
@@ -743,11 +920,26 @@ var Properties = function Properties(_ref) {
743
920
  onChange: handleColorChange(key)
744
921
  }, key);
745
922
  }), filterBy({
746
- kind: "overlay_opacity"
923
+ kind: "radius"
747
924
  }, values.properties).map(function (_ref6) {
748
925
  var key = _ref6.key,
749
926
  value = _ref6.value,
750
927
  kind = _ref6.kind;
928
+ return isPropertyVisible(key, values, themePropertiesSchema) && /*#__PURE__*/jsx(SliderBlock, {
929
+ label: buildLabel(key, kind),
930
+ max: 50,
931
+ min: 0,
932
+ value: Number(value) || 0,
933
+ onChange: function onChange(radius) {
934
+ return handleChange(key, radius, "".concat(radius, "px"));
935
+ }
936
+ }, key);
937
+ }), filterBy({
938
+ kind: "overlay_opacity"
939
+ }, values.properties).map(function (_ref7) {
940
+ var key = _ref7.key,
941
+ value = _ref7.value,
942
+ kind = _ref7.kind;
751
943
  return isPropertyVisible(key, values, themePropertiesSchema) && /*#__PURE__*/jsx(SliderBlock, {
752
944
  label: buildLabel(key, kind),
753
945
  marks: OVERLAY_OPACITY_SLIDER_MARKS,
@@ -760,11 +952,11 @@ var Properties = function Properties(_ref) {
760
952
  }, key);
761
953
  }), filterBy({
762
954
  kind: "position_selector"
763
- }, values.properties).map(function (_ref7) {
955
+ }, values.properties).map(function (_ref8) {
764
956
  var _findBy;
765
- var key = _ref7.key,
766
- kind = _ref7.kind,
767
- value = _ref7.value;
957
+ var key = _ref8.key,
958
+ kind = _ref8.kind,
959
+ value = _ref8.value;
768
960
  var index = findIndexBy({
769
961
  key: key
770
962
  }, values.properties);
@@ -782,10 +974,10 @@ var Properties = function Properties(_ref) {
782
974
  }, key);
783
975
  }), filterBy({
784
976
  kind: "boolean"
785
- }, values.properties).map(function (_ref8) {
786
- var key = _ref8.key,
787
- kind = _ref8.kind,
788
- value = _ref8.value;
977
+ }, values.properties).map(function (_ref9) {
978
+ var key = _ref9.key,
979
+ kind = _ref9.kind,
980
+ value = _ref9.value;
789
981
  var index = findIndexBy({
790
982
  key: key
791
983
  }, values.properties);
@@ -796,6 +988,18 @@ var Properties = function Properties(_ref) {
796
988
  return setFieldValue("properties[".concat(index, "].value"), String(e.target.checked));
797
989
  }
798
990
  }, key);
991
+ }), filterBy({
992
+ kind: "alignment_block"
993
+ }, values.properties).map(function (_ref10) {
994
+ var key = _ref10.key,
995
+ kind = _ref10.kind;
996
+ var index = findIndexBy({
997
+ key: key
998
+ }, values.properties);
999
+ return isPropertyVisible(key, values, themePropertiesSchema) && /*#__PURE__*/jsx(AlignmentBlock, {
1000
+ label: buildLabel(key, kind),
1001
+ name: "properties[".concat(index, "].value")
1002
+ });
799
1003
  })]
800
1004
  })
801
1005
  });