modern-idoc 0.6.0 → 0.6.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/dist/index.cjs CHANGED
@@ -10,6 +10,38 @@ function normalizeAudio(audio) {
10
10
  }
11
11
  }
12
12
 
13
+ function isNone(value) {
14
+ return value === null || value === void 0 || value === "none";
15
+ }
16
+ function round(number, digits = 0, base = 10 ** digits) {
17
+ return Math.round(base * number) / base + 0;
18
+ }
19
+ function clearUndef(obj, deep = false) {
20
+ if (typeof obj !== "object" || !obj) {
21
+ return obj;
22
+ }
23
+ if (Array.isArray(obj)) {
24
+ if (deep) {
25
+ return obj.map((v) => clearUndef(v, deep));
26
+ } else {
27
+ return obj;
28
+ }
29
+ }
30
+ const newObj = {};
31
+ for (const key in obj) {
32
+ const value = obj[key];
33
+ if (value === void 0 || value === null) {
34
+ continue;
35
+ }
36
+ if (deep) {
37
+ newObj[key] = clearUndef(value, deep);
38
+ } else {
39
+ newObj[key] = value;
40
+ }
41
+ }
42
+ return newObj;
43
+ }
44
+
13
45
  function parseColor(color) {
14
46
  let input;
15
47
  if (typeof color === "number") {
@@ -24,9 +56,6 @@ function parseColor(color) {
24
56
  }
25
57
  return colord.colord(input);
26
58
  }
27
- function round(number, digits = 0, base = 10 ** digits) {
28
- return Math.round(base * number) / base + 0;
29
- }
30
59
  function roundRgba(rgba) {
31
60
  return {
32
61
  r: round(rgba.r),
@@ -40,6 +69,9 @@ function format(number) {
40
69
  return hex.length < 2 ? `0${hex}` : hex;
41
70
  }
42
71
  const defaultColor = "#000000FF";
72
+ function isColor(value) {
73
+ return parseColor(value).isValid();
74
+ }
43
75
  function normalizeColor(color, orFail = false) {
44
76
  const parsed = parseColor(color);
45
77
  if (!parsed.isValid()) {
@@ -574,16 +606,11 @@ GradientParser.stringify = /* @__PURE__ */ function() {
574
606
  }();
575
607
  const stringifyGradient = GradientParser.stringify.bind(GradientParser);
576
608
 
577
- function parseLinearGradientNode(node) {
578
- let angle = 0;
579
- switch (node.orientation?.type) {
580
- case "angular":
581
- angle = Number(node.orientation.value);
582
- break;
583
- }
584
- const stops = node.colorStops.map((stop) => {
609
+ function parseColorStopNodeList(colorStops) {
610
+ const count = colorStops.length - 1;
611
+ return colorStops.map((stop, index) => {
585
612
  const value = stop.value;
586
- let offset = 0;
613
+ let offset = round(index / count, 3);
587
614
  let color = "#00000000";
588
615
  switch (stop.type) {
589
616
  case "rgb":
@@ -605,7 +632,7 @@ function parseLinearGradientNode(node) {
605
632
  color = normalizeColor(stop.value);
606
633
  break;
607
634
  case "hex":
608
- color = normalizeColor(stop.value);
635
+ color = normalizeColor(`#${stop.value}`);
609
636
  break;
610
637
  }
611
638
  switch (stop.length?.type) {
@@ -615,14 +642,22 @@ function parseLinearGradientNode(node) {
615
642
  }
616
643
  return { offset, color };
617
644
  });
645
+ }
646
+ function parseLinearGradientNode(node) {
647
+ let angle = 0;
648
+ switch (node.orientation?.type) {
649
+ case "angular":
650
+ angle = Number(node.orientation.value);
651
+ break;
652
+ }
618
653
  return {
619
654
  type: "linear-gradient",
620
655
  angle,
621
- stops
656
+ stops: parseColorStopNodeList(node.colorStops)
622
657
  };
623
658
  }
624
- function parseRadialGradientNode(ast) {
625
- ast.orientation?.map((item) => {
659
+ function parseRadialGradientNode(node) {
660
+ node.orientation?.map((item) => {
626
661
  switch (item?.type) {
627
662
  case "shape":
628
663
  case "default-radial":
@@ -631,45 +666,14 @@ function parseRadialGradientNode(ast) {
631
666
  return null;
632
667
  }
633
668
  });
634
- const stops = ast.colorStops.map((stop) => {
635
- const value = stop.value;
636
- let offset = 0;
637
- let color = "#00000000";
638
- switch (stop.type) {
639
- case "rgb":
640
- color = normalizeColor({
641
- r: Number(value[0] ?? 0),
642
- g: Number(value[1] ?? 0),
643
- b: Number(value[2] ?? 0)
644
- });
645
- break;
646
- case "rgba":
647
- color = normalizeColor({
648
- r: Number(value[0] ?? 0),
649
- g: Number(value[1] ?? 0),
650
- b: Number(value[2] ?? 0),
651
- a: Number(value[3] ?? 0)
652
- });
653
- break;
654
- case "literal":
655
- color = normalizeColor(stop.value);
656
- break;
657
- case "hex":
658
- color = normalizeColor(stop.value);
659
- break;
660
- }
661
- switch (stop.length?.type) {
662
- case "%":
663
- offset = Number(stop.length.value) / 100;
664
- break;
665
- }
666
- return { offset, color };
667
- });
668
669
  return {
669
670
  type: "radial-gradient",
670
- stops
671
+ stops: parseColorStopNodeList(node.colorStops)
671
672
  };
672
673
  }
674
+ function isGradient(cssText) {
675
+ return cssText.startsWith("linear-gradient") || cssText.startsWith("radial-gradient");
676
+ }
673
677
  function normalizeGradient(cssText) {
674
678
  return parseGradient(cssText).map((node) => {
675
679
  switch (node?.type) {
@@ -687,53 +691,92 @@ function normalizeGradient(cssText) {
687
691
  }).filter(Boolean);
688
692
  }
689
693
 
690
- function isNone(value) {
691
- return value === null || value === void 0 || value === "none";
694
+ function normalizeColorFill(fill) {
695
+ let obj;
696
+ if (typeof fill === "string") {
697
+ obj = { color: fill };
698
+ } else {
699
+ obj = fill;
700
+ }
701
+ return {
702
+ color: normalizeColor(obj.color)
703
+ };
692
704
  }
693
- function clearUndef(obj, deep = false) {
694
- if (typeof obj !== "object" || !obj) {
695
- return obj;
705
+
706
+ function normalizeGradientFill(fill) {
707
+ let obj;
708
+ if (typeof fill === "string") {
709
+ obj = { image: fill };
710
+ } else {
711
+ obj = fill;
696
712
  }
697
- if (Array.isArray(obj)) {
698
- if (deep) {
699
- return obj.map((v) => clearUndef(v, deep));
700
- } else {
701
- return obj;
702
- }
713
+ const res = normalizeGradient(obj.image)[0];
714
+ switch (res?.type) {
715
+ case "radial-gradient":
716
+ return {
717
+ radialGradient: res
718
+ };
719
+ case "linear-gradient":
720
+ return {
721
+ linearGradient: res
722
+ };
703
723
  }
704
- const newObj = {};
705
- for (const key in obj) {
706
- const value = obj[key];
707
- if (value === void 0 || value === null) {
708
- continue;
709
- }
710
- if (deep) {
711
- newObj[key] = clearUndef(value, deep);
712
- } else {
713
- newObj[key] = value;
714
- }
724
+ return {};
725
+ }
726
+
727
+ function normalizeImageFill(fill) {
728
+ return fill;
729
+ }
730
+
731
+ function normalizePresetFill(fill) {
732
+ let obj;
733
+ if (typeof fill === "string") {
734
+ obj = { preset: fill };
735
+ } else {
736
+ obj = fill;
715
737
  }
716
- return newObj;
738
+ return {
739
+ preset: obj.preset,
740
+ foregroundColor: isNone(obj.foregroundColor) ? void 0 : normalizeColor(obj.foregroundColor),
741
+ backgroundColor: isNone(obj.backgroundColor) ? void 0 : normalizeColor(obj.backgroundColor)
742
+ };
717
743
  }
718
744
 
719
745
  function normalizeFill(fill) {
720
746
  if (typeof fill === "string") {
721
- return { color: normalizeColor(fill) };
747
+ if (isColor(fill)) {
748
+ return normalizeColorFill({ color: fill });
749
+ } else if (isGradient(fill)) {
750
+ return normalizeGradientFill({ image: fill });
751
+ } else {
752
+ return normalizeImageFill({ image: fill });
753
+ }
722
754
  } else {
723
- return {
724
- ...fill,
725
- color: isNone(fill.color) ? void 0 : normalizeColor(fill.color)
726
- };
755
+ if (!isNone(fill.color)) {
756
+ return normalizeColorFill(fill);
757
+ } else if (!isNone(fill.image)) {
758
+ if (isGradient(fill.image)) {
759
+ return normalizeGradientFill(fill);
760
+ } else {
761
+ return normalizeImageFill(fill);
762
+ }
763
+ } else if (isNone(fill.preset)) {
764
+ return normalizePresetFill(fill);
765
+ }
727
766
  }
767
+ throw new Error("Unknown fill property object");
728
768
  }
729
769
 
730
770
  function normalizeBackground(background) {
731
771
  if (typeof background === "string") {
732
- return { src: background };
772
+ return {
773
+ ...normalizeFill(background),
774
+ fillWithShape: false
775
+ };
733
776
  } else {
734
777
  return {
735
- ...background,
736
- ...normalizeFill(background)
778
+ ...normalizeFill(background),
779
+ fillWithShape: Boolean(background.fillWithShape)
737
780
  };
738
781
  }
739
782
  }
@@ -785,35 +828,15 @@ function normalizeEffect(effect) {
785
828
 
786
829
  function normalizeForeground(foreground) {
787
830
  if (typeof foreground === "string") {
788
- return { src: foreground };
789
- } else {
790
831
  return {
791
- ...foreground,
792
- ...normalizeFill(foreground)
832
+ ...normalizeFill(foreground),
833
+ fillWithShape: false
793
834
  };
794
- }
795
- }
796
-
797
- function normalizeGeometry(geometry) {
798
- if (typeof geometry === "string") {
799
- return {
800
- paths: [
801
- { data: geometry }
802
- ]
803
- };
804
- } else if (Array.isArray(geometry)) {
835
+ } else {
805
836
  return {
806
- paths: geometry.map((data) => {
807
- if (typeof data === "string") {
808
- return {
809
- data
810
- };
811
- }
812
- return data;
813
- })
837
+ ...normalizeFill(foreground),
838
+ fillWithShape: Boolean(foreground.fillWithShape)
814
839
  };
815
- } else {
816
- return geometry;
817
840
  }
818
841
  }
819
842
 
@@ -848,6 +871,29 @@ function getDefaultShadowStyle() {
848
871
  };
849
872
  }
850
873
 
874
+ function normalizeShape(shape) {
875
+ if (typeof shape === "string") {
876
+ return {
877
+ paths: [
878
+ { data: shape }
879
+ ]
880
+ };
881
+ } else if (Array.isArray(shape)) {
882
+ return {
883
+ paths: shape.map((data) => {
884
+ if (typeof data === "string") {
885
+ return {
886
+ data
887
+ };
888
+ }
889
+ return data;
890
+ })
891
+ };
892
+ } else {
893
+ return shape;
894
+ }
895
+ }
896
+
851
897
  function getDefaultLayoutStyle() {
852
898
  return {
853
899
  // box
@@ -1100,7 +1146,7 @@ function normalizeElement(element) {
1100
1146
  style: isNone(element.style) ? void 0 : normalizeStyle(element.style),
1101
1147
  text: isNone(element.text) ? void 0 : normalizeText(element.text),
1102
1148
  background: isNone(element.background) ? void 0 : normalizeBackground(element.background),
1103
- geometry: isNone(element.geometry) ? void 0 : normalizeGeometry(element.geometry),
1149
+ shape: isNone(element.shape) ? void 0 : normalizeShape(element.shape),
1104
1150
  fill: isNone(element.fill) ? void 0 : normalizeFill(element.fill),
1105
1151
  outline: isNone(element.outline) ? void 0 : normalizeOutline(element.outline),
1106
1152
  foreground: isNone(element.foreground) ? void 0 : normalizeForeground(element.foreground),
@@ -1130,21 +1176,27 @@ exports.getDefaultTextInlineStyle = getDefaultTextInlineStyle;
1130
1176
  exports.getDefaultTextLineStyle = getDefaultTextLineStyle;
1131
1177
  exports.getDefaultTextStyle = getDefaultTextStyle;
1132
1178
  exports.getDefaultTransformStyle = getDefaultTransformStyle;
1179
+ exports.isColor = isColor;
1180
+ exports.isGradient = isGradient;
1133
1181
  exports.isNone = isNone;
1134
1182
  exports.normalizeAudio = normalizeAudio;
1135
1183
  exports.normalizeBackground = normalizeBackground;
1136
1184
  exports.normalizeColor = normalizeColor;
1185
+ exports.normalizeColorFill = normalizeColorFill;
1137
1186
  exports.normalizeDocument = normalizeDocument;
1138
1187
  exports.normalizeEffect = normalizeEffect;
1139
1188
  exports.normalizeElement = normalizeElement;
1140
1189
  exports.normalizeFill = normalizeFill;
1141
1190
  exports.normalizeForeground = normalizeForeground;
1142
- exports.normalizeGeometry = normalizeGeometry;
1143
1191
  exports.normalizeGradient = normalizeGradient;
1192
+ exports.normalizeGradientFill = normalizeGradientFill;
1193
+ exports.normalizeImageFill = normalizeImageFill;
1144
1194
  exports.normalizeInnerShadow = normalizeInnerShadow;
1145
1195
  exports.normalizeOuterShadow = normalizeOuterShadow;
1146
1196
  exports.normalizeOutline = normalizeOutline;
1197
+ exports.normalizePresetFill = normalizePresetFill;
1147
1198
  exports.normalizeShadow = normalizeShadow;
1199
+ exports.normalizeShape = normalizeShape;
1148
1200
  exports.normalizeSoftEdge = normalizeSoftEdge;
1149
1201
  exports.normalizeStyle = normalizeStyle;
1150
1202
  exports.normalizeText = normalizeText;
@@ -1152,4 +1204,5 @@ exports.normalizeTextContent = normalizeTextContent;
1152
1204
  exports.normalizeVideo = normalizeVideo;
1153
1205
  exports.parseColor = parseColor;
1154
1206
  exports.parseGradient = parseGradient;
1207
+ exports.round = round;
1155
1208
  exports.stringifyGradient = stringifyGradient;