@elementor/editor-props 0.15.0 → 0.16.0

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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@elementor/editor-props",
3
3
  "description": "This package contains the props model for the Elementor editor",
4
- "version": "0.15.0",
4
+ "version": "0.16.0",
5
5
  "private": false,
6
6
  "author": "Elementor Team",
7
7
  "homepage": "https://elementor.com/",
@@ -30,3 +30,5 @@ export * from './position';
30
30
  export * from './filter-prop-types/filter';
31
31
  export * from './filter-prop-types/blur-filter';
32
32
  export * from './filter-prop-types/brightness-filter';
33
+ export * from './transform-prop-types/transform';
34
+ export * from './transform-prop-types/move-transform';
@@ -0,0 +1,15 @@
1
+ import { z } from '@elementor/schema';
2
+
3
+ import { createPropUtils } from '../../utils/create-prop-utils';
4
+ import { sizePropTypeUtil } from '../size';
5
+
6
+ export const moveTransformPropTypeUtil = createPropUtils(
7
+ 'transform-move',
8
+ z.strictObject( {
9
+ x: sizePropTypeUtil.schema.nullable(),
10
+ y: sizePropTypeUtil.schema.nullable(),
11
+ z: sizePropTypeUtil.schema.nullable(),
12
+ } )
13
+ );
14
+
15
+ export type MoveTransformPropValue = z.infer< typeof moveTransformPropTypeUtil.schema >;
@@ -0,0 +1,13 @@
1
+ import { z } from '@elementor/schema';
2
+
3
+ import { createPropUtils } from '../../utils/create-prop-utils';
4
+ import { sizePropTypeUtil } from '../size';
5
+
6
+ export const scaleTransformPropTypeUtil = createPropUtils(
7
+ 'scale',
8
+ z.strictObject( {
9
+ x: sizePropTypeUtil.schema.nullable(),
10
+ y: sizePropTypeUtil.schema.nullable(),
11
+ z: sizePropTypeUtil.schema.nullable(),
12
+ } )
13
+ );
@@ -0,0 +1,12 @@
1
+ import { z } from '@elementor/schema';
2
+
3
+ import { createPropUtils } from '../../utils/create-prop-utils';
4
+ import { moveTransformPropTypeUtil } from './move-transform';
5
+ import { scaleTransformPropTypeUtil } from './scale-transform';
6
+
7
+ const filterTypes = moveTransformPropTypeUtil.schema.or( scaleTransformPropTypeUtil.schema );
8
+ export const transformPropTypeUtil = createPropUtils( 'transform', z.array( filterTypes ) );
9
+
10
+ export type TransformPropValue = z.infer< typeof transformPropTypeUtil.schema >;
11
+
12
+ export type TransformItemPropValue = z.infer< typeof filterTypes >;
package/src/types.ts CHANGED
@@ -16,17 +16,16 @@ export type DependencyOperator =
16
16
  | 'contains'
17
17
  | 'ncontains';
18
18
 
19
- export type DependencyTerm< THasActualValue extends boolean = false > = {
19
+ export type DependencyTerm = {
20
20
  operator: DependencyOperator;
21
21
  path: string[];
22
22
  value: PropValue;
23
- actualValue: THasActualValue extends true ? PropValue : never;
24
23
  };
25
24
 
26
- export type Dependency< THasActualValue extends boolean = false > = {
25
+ export type Dependency = {
27
26
  effect: DependencyEffect;
28
27
  relation: 'or' | 'and';
29
- terms: ( DependencyTerm< THasActualValue > | Dependency< THasActualValue > )[];
28
+ terms: ( DependencyTerm | Dependency )[];
30
29
  };
31
30
 
32
31
  type BasePropType< TValue > = {
@@ -1,22 +1,22 @@
1
- import { type Dependency, type DependencyTerm, type PropValue } from '../types';
1
+ import { type Dependency, type DependencyTerm, type PropValue, type TransformablePropValue } from '../types';
2
2
 
3
- type ParsedTerm = DependencyTerm< true >;
3
+ type ParsedTerm = DependencyTerm;
4
4
 
5
5
  type Relation = Dependency[ 'relation' ];
6
6
 
7
- export function shouldApplyEffect( { relation, terms }: Dependency< true > ): boolean {
7
+ export function shouldApplyEffect( { relation, terms }: Dependency, values: PropValue ): boolean {
8
8
  if ( ! terms.length ) {
9
9
  return false;
10
10
  }
11
11
 
12
12
  const method = getRelationMethod( relation );
13
13
 
14
- return terms[ method ]( ( term: ParsedTerm | Dependency< true > ) =>
15
- isDependency( term ) ? shouldApplyEffect( term ) : evaluateTerm( term, term.actualValue )
14
+ return terms[ method ]( ( term: ParsedTerm | Dependency ) =>
15
+ isDependency( term ) ? shouldApplyEffect( term, values ) : evaluateTerm( term, getValue( term.path, values ) )
16
16
  );
17
17
  }
18
18
 
19
- export function evaluateTerm( term: DependencyTerm< true >, actualValue: PropValue ) {
19
+ export function evaluateTerm( term: DependencyTerm, actualValue: PropValue ) {
20
20
  const { value: valueToCompare, operator } = term;
21
21
 
22
22
  switch ( operator ) {
@@ -26,31 +26,40 @@ export function evaluateTerm( term: DependencyTerm< true >, actualValue: PropVal
26
26
 
27
27
  case 'gt':
28
28
  case 'lte':
29
- return (
30
- ! isNaN( Number( actualValue ) ) &&
31
- ! isNaN( Number( valueToCompare ) ) &&
32
- Number( actualValue ) > Number( valueToCompare ) === ( 'gt' === operator )
33
- );
29
+ if ( isNaN( Number( actualValue ) ) || isNaN( Number( valueToCompare ) ) ) {
30
+ throw new Error( 'Mathematical comparison requires numeric values.' );
31
+ }
32
+
33
+ return Number( actualValue ) > Number( valueToCompare ) === ( 'gt' === operator );
34
34
 
35
35
  case 'lt':
36
36
  case 'gte':
37
- return (
38
- ! isNaN( Number( actualValue ) ) &&
39
- ! isNaN( Number( valueToCompare ) ) &&
40
- Number( actualValue ) < Number( valueToCompare ) === ( 'lt' === operator )
41
- );
37
+ if ( isNaN( Number( actualValue ) ) || isNaN( Number( valueToCompare ) ) ) {
38
+ throw new Error( 'Mathematical comparison requires numeric values.' );
39
+ }
40
+
41
+ return Number( actualValue ) < Number( valueToCompare ) === ( 'lt' === operator );
42
42
 
43
43
  case 'in':
44
44
  case 'nin':
45
- return Array.isArray( valueToCompare ) && valueToCompare.includes( actualValue ) === ( 'in' === operator );
45
+ if ( ! Array.isArray( valueToCompare ) ) {
46
+ throw new Error( 'The "in" and "nin" operators require an array for comparison.' );
47
+ }
48
+
49
+ return valueToCompare.includes( actualValue ) === ( 'in' === operator );
46
50
 
47
51
  case 'contains':
48
52
  case 'ncontains':
49
- return (
50
- 'string' === typeof actualValue &&
51
- 'string' === typeof valueToCompare &&
52
- ( 'contains' === operator ) === actualValue.includes( valueToCompare )
53
- );
53
+ if (
54
+ ( 'string' !== typeof actualValue || 'string' !== typeof valueToCompare ) &&
55
+ ! Array.isArray( actualValue )
56
+ ) {
57
+ throw new Error(
58
+ 'The "contains" and "ncontains" operators require a string or an array for comparison.'
59
+ );
60
+ }
61
+
62
+ return ( 'contains' === operator ) === actualValue.includes( valueToCompare as never );
54
63
 
55
64
  case 'exists':
56
65
  case 'not_exist':
@@ -76,8 +85,14 @@ function getRelationMethod( relation: Relation ) {
76
85
  }
77
86
  }
78
87
 
79
- function isDependency< THasActualValue extends boolean >(
80
- term: DependencyTerm< THasActualValue > | Dependency< THasActualValue >
81
- ): term is Dependency< THasActualValue > {
88
+ function getValue( path: string[], elementValues: PropValue ): PropValue | null {
89
+ return path.reduce( ( acc, key ) => {
90
+ return 'object' === typeof acc && acc !== null && key in acc
91
+ ? ( ( acc[ key as keyof typeof acc ] as TransformablePropValue< string > )?.value as PropValue )
92
+ : null;
93
+ }, elementValues );
94
+ }
95
+
96
+ function isDependency( term: DependencyTerm | Dependency ): term is Dependency {
82
97
  return 'relation' in term;
83
98
  }