@elementor/editor-props 3.35.0-340 → 3.35.0-342

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.mjs CHANGED
@@ -175,7 +175,7 @@ import { z as z15 } from "@elementor/schema";
175
175
  var sizePropTypeUtil = createPropUtils(
176
176
  "size",
177
177
  z15.strictObject({
178
- unit: z15.enum(["px", "em", "rem", "%", "vw", "vh"]),
178
+ unit: z15.enum(["px", "em", "rem", "%", "vw", "vh", "ch"]),
179
179
  size: z15.number()
180
180
  }).or(
181
181
  z15.strictObject({
@@ -527,63 +527,55 @@ var backdropFilterPropTypeUtil = createPropUtils(
527
527
 
528
528
  // src/utils/adjust-llm-prop-value-schema.ts
529
529
  var ensureNotNull = (v, fallback) => v === null ? fallback : v;
530
- var adjustLlmPropValueSchema = (value, forceKey) => {
530
+ var defaultOptions = {
531
+ transformers: {}
532
+ };
533
+ var adjustLlmPropValueSchema = (value, { transformers = {}, forceKey = void 0 } = defaultOptions) => {
531
534
  const clone = structuredClone(value);
532
- if (typeof clone === "string") {
533
- return stringPropTypeUtil.create(clone);
535
+ if (typeof clone !== "object" || clone === null) {
536
+ return null;
534
537
  }
535
- if (typeof clone === "number") {
536
- return numberPropTypeUtil.create(clone);
538
+ if (Array.isArray(clone)) {
539
+ return clone.map((item) => adjustLlmPropValueSchema(item, { forceKey, transformers }));
537
540
  }
538
- if (clone && typeof clone === "object") {
539
- if (Array.isArray(clone)) {
540
- return clone.map((item) => adjustLlmPropValueSchema(item, forceKey));
541
- }
542
- const transformablePropValue = clone;
543
- if (forceKey) {
544
- transformablePropValue.$$type = forceKey;
545
- }
546
- if (!transformablePropValue.$$type) {
547
- throw new Error("Missing $$type in property: " + JSON.stringify(transformablePropValue));
548
- }
549
- if (!("value" in transformablePropValue)) {
550
- throw new Error("Missing value property in PropValue: " + JSON.stringify(transformablePropValue));
551
- }
552
- switch (transformablePropValue.$$type) {
553
- case "size": {
554
- const { value: rawSizePropValue } = transformablePropValue;
555
- const unit = typeof rawSizePropValue.unit === "string" ? rawSizePropValue.unit : ensureNotNull(stringPropTypeUtil.extract(rawSizePropValue.unit), "px");
556
- const size = typeof rawSizePropValue.size === "string" || typeof rawSizePropValue.size === "number" ? rawSizePropValue.size : ensureNotNull(
557
- stringPropTypeUtil.extract(rawSizePropValue.size),
558
- numberPropTypeUtil.extract(rawSizePropValue.size)
559
- );
560
- return {
561
- $$type: "size",
562
- value: {
563
- unit,
564
- size
565
- }
566
- };
567
- }
541
+ const transformablePropValue = clone;
542
+ if (forceKey) {
543
+ transformablePropValue.$$type = forceKey;
544
+ }
545
+ switch (transformablePropValue.$$type) {
546
+ case "size": {
547
+ const { value: rawSizePropValue } = transformablePropValue;
548
+ const unit = typeof rawSizePropValue.unit === "string" ? rawSizePropValue.unit : ensureNotNull(stringPropTypeUtil.extract(rawSizePropValue.unit), "px");
549
+ const size = typeof rawSizePropValue.size === "string" || typeof rawSizePropValue.size === "number" ? rawSizePropValue.size : ensureNotNull(
550
+ stringPropTypeUtil.extract(rawSizePropValue.size),
551
+ numberPropTypeUtil.extract(rawSizePropValue.size)
552
+ );
553
+ return {
554
+ $$type: "size",
555
+ value: {
556
+ unit,
557
+ size
558
+ }
559
+ };
568
560
  }
569
- if (typeof transformablePropValue.value === "object") {
570
- if (Array.isArray(transformablePropValue.value)) {
571
- transformablePropValue.value = adjustLlmPropValueSchema(
572
- transformablePropValue.value,
573
- void 0
574
- );
575
- } else {
576
- const { value: objectValue } = transformablePropValue;
577
- const clonedObject = clone;
578
- clonedObject.value = {};
579
- Object.keys(objectValue).forEach((key) => {
580
- const childProp = objectValue[key];
581
- clonedObject.value[key] = adjustLlmPropValueSchema(
582
- childProp,
583
- void 0
584
- );
585
- });
561
+ default:
562
+ const transformer = transformers?.[transformablePropValue.$$type];
563
+ if (transformer) {
564
+ return transformer(transformablePropValue.value);
586
565
  }
566
+ }
567
+ if (typeof transformablePropValue.value === "object") {
568
+ if (Array.isArray(transformablePropValue.value)) {
569
+ transformablePropValue.value = adjustLlmPropValueSchema(transformablePropValue.value, {
570
+ transformers
571
+ });
572
+ } else {
573
+ const { value: objectValue } = transformablePropValue;
574
+ const clonedObject = clone;
575
+ clonedObject.value = {};
576
+ Object.entries(objectValue).forEach(([key, childProp]) => {
577
+ clonedObject.value[key] = adjustLlmPropValueSchema(childProp, { transformers });
578
+ });
587
579
  }
588
580
  }
589
581
  return clone;
@@ -705,9 +697,10 @@ function propTypeToJsonSchema(propType) {
705
697
  if (description) {
706
698
  schema.description = description;
707
699
  }
700
+ if (propType.initial_value !== null && propType.initial_value !== void 0) {
701
+ schema.examples = [propType.initial_value];
702
+ }
708
703
  switch (propType.kind) {
709
- case "plain":
710
- return convertPlainPropType(propType, schema);
711
704
  case "union":
712
705
  return convertUnionPropType(propType, schema);
713
706
  case "object":
@@ -717,45 +710,53 @@ function propTypeToJsonSchema(propType) {
717
710
  default:
718
711
  return convertPlainPropType(propType, schema);
719
712
  }
720
- return schema;
721
713
  }
722
714
  function convertPlainPropType(propType, baseSchema) {
723
715
  const schema = { ...baseSchema };
724
- const key = propType.key.toLowerCase();
725
- switch (key) {
716
+ if (!Object.hasOwn(propType, "kind")) {
717
+ throw new Error(`PropType kind is undefined for propType with key: ${propType.key ?? "[unknown key]"}`);
718
+ }
719
+ const enumValues = propType.settings?.enum || [];
720
+ switch (propType.kind) {
721
+ case "string":
726
722
  case "number":
727
- schema.type = "number";
728
- break;
729
723
  case "boolean":
730
- schema.type = "boolean";
731
- break;
724
+ return {
725
+ ...schema,
726
+ type: "object",
727
+ properties: {
728
+ $$type: {
729
+ type: "string",
730
+ const: propType.key ?? propType.kind
731
+ },
732
+ value: {
733
+ type: propType.kind,
734
+ ...enumValues.length > 0 ? { enum: enumValues } : {}
735
+ }
736
+ },
737
+ required: ["$$type", "value"]
738
+ };
732
739
  default:
733
- schema.type = "string";
734
- }
735
- if (Array.isArray(propType.settings?.enum)) {
736
- schema.enum = propType.settings.enum;
740
+ return {
741
+ ...schema,
742
+ type: "object",
743
+ $$type: propType.kind,
744
+ value: {
745
+ type: propType.kind
746
+ }
747
+ };
737
748
  }
738
- return schema;
739
749
  }
740
750
  function convertUnionPropType(propType, baseSchema) {
741
751
  const schema = structuredClone(baseSchema);
742
752
  const propTypes = propType.prop_types || {};
743
753
  const schemas = [];
744
754
  for (const [typeKey, subPropType] of Object.entries(propTypes)) {
755
+ if (typeKey === "dynamic" || typeKey === "overridable") {
756
+ continue;
757
+ }
745
758
  const subSchema = convertPropTypeToJsonSchema(subPropType);
746
- schemas.push({
747
- type: "object",
748
- required: ["$$type", "value"],
749
- properties: {
750
- $$type: {
751
- type: "string",
752
- const: typeKey,
753
- description: subPropType.meta?.description,
754
- $comment: `Discriminator for union type variant: ${typeKey}`
755
- },
756
- value: subSchema
757
- }
758
- });
759
+ schemas.push(subSchema);
759
760
  }
760
761
  if (schemas.length > 0) {
761
762
  schema.anyOf = schemas;
@@ -769,28 +770,58 @@ function convertUnionPropType(propType, baseSchema) {
769
770
  function convertObjectPropType(propType, baseSchema) {
770
771
  const schema = structuredClone(baseSchema);
771
772
  schema.type = "object";
772
- schema.properties = {};
773
- const required = [];
773
+ const internalStructure = {
774
+ properties: {
775
+ $$type: {
776
+ type: "string",
777
+ const: propType.key
778
+ },
779
+ value: {
780
+ type: "object",
781
+ properties: {},
782
+ additionalProperties: false
783
+ }
784
+ }
785
+ };
786
+ const required = ["$$type", "value"];
787
+ const valueRequired = [];
774
788
  const shape = propType.shape || {};
775
789
  for (const [key, subPropType] of Object.entries(shape)) {
776
- const propSchema = convertPropTypeToJsonSchema(subPropType);
790
+ const propSchema = propTypeToJsonSchema(subPropType);
777
791
  if (subPropType.settings?.required === true) {
778
- required.push(key);
792
+ valueRequired.push(key);
793
+ }
794
+ if (internalStructure.properties.value.properties) {
795
+ internalStructure.properties.value.properties[key] = propSchema;
779
796
  }
780
- schema.properties[key] = propSchema;
781
797
  }
782
- if (required.length > 0) {
783
- schema.required = required;
798
+ schema.required = required;
799
+ if (valueRequired.length > 0) {
800
+ internalStructure.properties.value.required = valueRequired;
784
801
  }
785
- return schema;
802
+ return {
803
+ ...schema,
804
+ ...internalStructure
805
+ };
786
806
  }
787
807
  function convertArrayPropType(propType, baseSchema) {
788
808
  const schema = structuredClone(baseSchema);
789
- schema.type = "array";
809
+ schema.type = "object";
810
+ let items;
790
811
  const itemPropType = propType.item_prop_type;
791
812
  if (itemPropType) {
792
- schema.items = convertPropTypeToJsonSchema(itemPropType);
813
+ items = convertPropTypeToJsonSchema(itemPropType);
793
814
  }
815
+ schema.properties = {
816
+ $$type: {
817
+ type: "string",
818
+ const: propType.key
819
+ },
820
+ value: {
821
+ type: "array",
822
+ ...items ? { items } : {}
823
+ }
824
+ };
794
825
  return schema;
795
826
  }
796
827
  function convertPropTypeToJsonSchema(propType) {
@@ -804,6 +835,73 @@ function configurableKeys(schema) {
804
835
  return Object.keys(schema).filter(isPropKeyConfigurable);
805
836
  }
806
837
 
838
+ // src/utils/validate-prop-value.ts
839
+ import { validate } from "jsonschema";
840
+ function processValidationError(error) {
841
+ const detailed = {
842
+ path: error.path,
843
+ message: error.message,
844
+ schema: error.schema,
845
+ instance: error.instance,
846
+ name: error.name
847
+ };
848
+ if (error.name === "anyOf" && error.schema && typeof error.schema === "object" && "anyOf" in error.schema) {
849
+ const anyOfSchema = error.schema;
850
+ const variants = (anyOfSchema.anyOf || []).map((variantSchema, idx) => {
851
+ const variantResult = validate(error.instance, variantSchema);
852
+ let discriminator = `variant-${idx}`;
853
+ if (variantSchema && typeof variantSchema === "object" && "properties" in variantSchema && variantSchema.properties && typeof variantSchema.properties === "object" && "$$type" in variantSchema.properties) {
854
+ const typeProperty = variantSchema.properties.$$type;
855
+ if (typeProperty && typeof typeProperty === "object" && "const" in typeProperty && typeof typeProperty.const === "string") {
856
+ discriminator = typeProperty.const;
857
+ }
858
+ }
859
+ return {
860
+ discriminator,
861
+ errors: variantResult.errors.map(processValidationError)
862
+ };
863
+ });
864
+ detailed.variants = variants;
865
+ }
866
+ return detailed;
867
+ }
868
+ function formatDetailedErrors(errors, indent = "") {
869
+ const lines = [];
870
+ for (const error of errors) {
871
+ const pathStr = error.path.length > 0 ? error.path.join(".") : "root";
872
+ lines.push(`${indent}Error at ${pathStr}: ${error.message}`);
873
+ if (error.variants && error.variants.length > 0) {
874
+ lines.push(`${indent} Tried ${error.variants.length} variant(s):`);
875
+ for (const variant of error.variants) {
876
+ lines.push(`${indent} - ${variant.discriminator}:`);
877
+ if (variant.errors.length === 0) {
878
+ lines.push(`${indent} (no errors - this variant matched!)`);
879
+ } else {
880
+ for (const nestedError of variant.errors) {
881
+ const nestedPathStr = nestedError.path.length > 0 ? nestedError.path.join(".") : "root";
882
+ lines.push(`${indent} ${nestedPathStr}: ${nestedError.message}`);
883
+ if (nestedError.variants && nestedError.variants.length > 0) {
884
+ lines.push(formatDetailedErrors([nestedError], `${indent} `));
885
+ }
886
+ }
887
+ }
888
+ }
889
+ }
890
+ }
891
+ return lines.join("\n");
892
+ }
893
+ var validatePropValue = (schema, value) => {
894
+ const jsonSchema = propTypeToJsonSchema(schema);
895
+ const result = validate(value, jsonSchema);
896
+ const detailedErrors = result.errors.map(processValidationError);
897
+ return {
898
+ valid: result.valid,
899
+ errors: result.errors,
900
+ errorMessages: formatDetailedErrors(detailedErrors),
901
+ jsonSchema: JSON.stringify(jsonSchema)
902
+ };
903
+ };
904
+
807
905
  // src/utils/is-transformable.ts
808
906
  import { z as z47 } from "@elementor/schema";
809
907
  var transformableSchema = z47.object({
@@ -999,7 +1097,8 @@ var Schema = {
999
1097
  adjustLlmPropValueSchema,
1000
1098
  isPropKeyConfigurable,
1001
1099
  nonConfigurablePropKeys,
1002
- configurableKeys
1100
+ configurableKeys,
1101
+ validatePropValue
1003
1102
  };
1004
1103
  export {
1005
1104
  CLASSES_PROP_KEY,