@elementor/editor-props 0.16.0 → 3.32.0-20

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.
@@ -1,22 +1,32 @@
1
- import { type Dependency, type DependencyTerm, type PropValue, type TransformablePropValue } from '../types';
1
+ import {
2
+ type Dependency,
3
+ type DependencyTerm,
4
+ type PropKey,
5
+ type PropValue,
6
+ type TransformablePropValue,
7
+ } from '../types';
8
+ import { isTransformable } from './is-transformable';
2
9
 
3
10
  type ParsedTerm = DependencyTerm;
4
11
 
5
12
  type Relation = Dependency[ 'relation' ];
6
13
 
7
- export function shouldApplyEffect( { relation, terms }: Dependency, values: PropValue ): boolean {
8
- if ( ! terms.length ) {
9
- return false;
14
+ export function isDependencyMet( dependency: Dependency | undefined, values: PropValue ): boolean {
15
+ if ( ! dependency?.terms.length ) {
16
+ return true;
10
17
  }
11
18
 
19
+ const { relation, terms } = dependency;
12
20
  const method = getRelationMethod( relation );
13
21
 
14
22
  return terms[ method ]( ( term: ParsedTerm | Dependency ) =>
15
- isDependency( term ) ? shouldApplyEffect( term, values ) : evaluateTerm( term, getValue( term.path, values ) )
23
+ isDependency( term )
24
+ ? isDependencyMet( term, values )
25
+ : evaluateTerm( term, extractValue( term.path, values )?.value )
16
26
  );
17
27
  }
18
28
 
19
- export function evaluateTerm( term: DependencyTerm, actualValue: PropValue ) {
29
+ export function evaluateTerm( term: DependencyTerm, actualValue: unknown ) {
20
30
  const { value: valueToCompare, operator } = term;
21
31
 
22
32
  switch ( operator ) {
@@ -26,27 +36,26 @@ export function evaluateTerm( term: DependencyTerm, actualValue: PropValue ) {
26
36
 
27
37
  case 'gt':
28
38
  case 'lte':
29
- if ( isNaN( Number( actualValue ) ) || isNaN( Number( valueToCompare ) ) ) {
30
- throw new Error( 'Mathematical comparison requires numeric values.' );
39
+ if ( ! isNumber( actualValue ) || ! isNumber( valueToCompare ) ) {
40
+ return false;
31
41
  }
32
42
 
33
43
  return Number( actualValue ) > Number( valueToCompare ) === ( 'gt' === operator );
34
44
 
35
45
  case 'lt':
36
46
  case 'gte':
37
- if ( isNaN( Number( actualValue ) ) || isNaN( Number( valueToCompare ) ) ) {
38
- throw new Error( 'Mathematical comparison requires numeric values.' );
47
+ if ( ! isNumber( actualValue ) || ! isNumber( valueToCompare ) ) {
48
+ return false;
39
49
  }
40
50
 
41
51
  return Number( actualValue ) < Number( valueToCompare ) === ( 'lt' === operator );
42
-
43
52
  case 'in':
44
53
  case 'nin':
45
54
  if ( ! Array.isArray( valueToCompare ) ) {
46
- throw new Error( 'The "in" and "nin" operators require an array for comparison.' );
55
+ return false;
47
56
  }
48
57
 
49
- return valueToCompare.includes( actualValue ) === ( 'in' === operator );
58
+ return valueToCompare.includes( actualValue as never ) === ( 'in' === operator );
50
59
 
51
60
  case 'contains':
52
61
  case 'ncontains':
@@ -54,9 +63,7 @@ export function evaluateTerm( term: DependencyTerm, actualValue: PropValue ) {
54
63
  ( 'string' !== typeof actualValue || 'string' !== typeof valueToCompare ) &&
55
64
  ! Array.isArray( actualValue )
56
65
  ) {
57
- throw new Error(
58
- 'The "contains" and "ncontains" operators require a string or an array for comparison.'
59
- );
66
+ return false;
60
67
  }
61
68
 
62
69
  return ( 'contains' === operator ) === actualValue.includes( valueToCompare as never );
@@ -68,10 +75,14 @@ export function evaluateTerm( term: DependencyTerm, actualValue: PropValue ) {
68
75
  return ( 'exists' === operator ) === evaluation;
69
76
 
70
77
  default:
71
- return false;
78
+ return true;
72
79
  }
73
80
  }
74
81
 
82
+ function isNumber( value: unknown ): value is number {
83
+ return typeof value === 'number' && ! isNaN( value );
84
+ }
85
+
75
86
  function getRelationMethod( relation: Relation ) {
76
87
  switch ( relation ) {
77
88
  case 'or':
@@ -85,14 +96,14 @@ function getRelationMethod( relation: Relation ) {
85
96
  }
86
97
  }
87
98
 
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 );
99
+ export function extractValue( path: string[], elementValues: PropValue ): TransformablePropValue< PropKey > | null {
100
+ return path.reduce( ( acc, key, index ) => {
101
+ const value = acc?.[ key as keyof typeof acc ] as PropValue | null;
102
+
103
+ return index !== path.length - 1 && isTransformable( value ) ? value.value ?? null : value;
104
+ }, elementValues ) as TransformablePropValue< PropKey >;
94
105
  }
95
106
 
96
- function isDependency( term: DependencyTerm | Dependency ): term is Dependency {
107
+ export function isDependency( term: DependencyTerm | Dependency ): term is Dependency {
97
108
  return 'relation' in term;
98
109
  }
@@ -1,8 +0,0 @@
1
- import { type z } from '@elementor/schema';
2
-
3
- import { createPropUtils } from '../../utils/create-prop-utils';
4
- import { unknownChildrenSchema } from '../utils';
5
-
6
- export const blurFilterPropTypeUtil = createPropUtils( 'blur', unknownChildrenSchema );
7
-
8
- export type BlurFilterPropValue = z.infer< typeof blurFilterPropTypeUtil.schema >;
@@ -1,8 +0,0 @@
1
- import { type z } from '@elementor/schema';
2
-
3
- import { createPropUtils } from '../../utils/create-prop-utils';
4
- import { unknownChildrenSchema } from '../utils';
5
-
6
- export const brightnessFilterPropTypeUtil = createPropUtils( 'brightness', unknownChildrenSchema );
7
-
8
- export type BrightnessFilterPropValue = z.infer< typeof brightnessFilterPropTypeUtil.schema >;