@elementor/editor-props 0.13.0 → 0.15.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/src/index.ts CHANGED
@@ -7,6 +7,7 @@ export * from './prop-types';
7
7
 
8
8
  // utils
9
9
  export { mergeProps } from './utils/merge-props';
10
- export { createPropUtils } from './utils/create-prop-utils';
10
+ export { createPropUtils, createArrayPropUtils } from './utils/create-prop-utils';
11
+ export { shouldApplyEffect, evaluateTerm } from './utils/prop-dependency-utils';
11
12
  export { isTransformable } from './utils/is-transformable';
12
13
  export { filterEmptyValues, isEmpty } from './utils/filter-empty-values';
@@ -0,0 +1,8 @@
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 >;
@@ -0,0 +1,8 @@
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 >;
@@ -0,0 +1,12 @@
1
+ import { z } from '@elementor/schema';
2
+
3
+ import { createPropUtils } from '../../utils/create-prop-utils';
4
+ import { blurFilterPropTypeUtil } from './blur-filter';
5
+ import { brightnessFilterPropTypeUtil } from './brightness-filter';
6
+
7
+ const filterTypes = blurFilterPropTypeUtil.schema.or( brightnessFilterPropTypeUtil.schema );
8
+ export const filterPropTypeUtil = createPropUtils( 'filter', z.array( filterTypes ) );
9
+
10
+ export type FilterPropValue = z.infer< typeof filterPropTypeUtil.schema >;
11
+
12
+ export type FilterItemPropValue = z.infer< typeof filterTypes >;
@@ -25,3 +25,8 @@ export * from './background-prop-types/background-image-size-scale';
25
25
  export * from './boolean';
26
26
  export * from './color-stop';
27
27
  export * from './gradient-color-stop';
28
+ export * from './key-value';
29
+ export * from './position';
30
+ export * from './filter-prop-types/filter';
31
+ export * from './filter-prop-types/blur-filter';
32
+ export * from './filter-prop-types/brightness-filter';
@@ -0,0 +1,14 @@
1
+ import { z } from '@elementor/schema';
2
+
3
+ import { createPropUtils } from '../utils/create-prop-utils';
4
+ import { unknownChildrenSchema } from './utils';
5
+
6
+ export const keyValuePropTypeUtil = createPropUtils(
7
+ 'key-value',
8
+ z.strictObject( {
9
+ key: unknownChildrenSchema,
10
+ value: unknownChildrenSchema,
11
+ } )
12
+ );
13
+
14
+ export type KeyValuePropValue = z.infer< typeof keyValuePropTypeUtil.schema >;
@@ -0,0 +1,14 @@
1
+ import { z } from '@elementor/schema';
2
+
3
+ import { createPropUtils } from '../utils/create-prop-utils';
4
+ import { sizePropTypeUtil } from './size';
5
+
6
+ export const positionPropTypeUtil = createPropUtils(
7
+ 'object-position',
8
+ z.strictObject( {
9
+ x: sizePropTypeUtil.schema.nullable(),
10
+ y: sizePropTypeUtil.schema.nullable(),
11
+ } )
12
+ );
13
+
14
+ export type PositionPropTypeValue = z.infer< typeof positionPropTypeUtil.schema >;
package/src/types.ts CHANGED
@@ -1,9 +1,39 @@
1
1
  export type PropTypeKey = string;
2
2
 
3
+ export type DependencyEffect = 'disable' | 'hide';
4
+
5
+ export type DependencyOperator =
6
+ | 'lt'
7
+ | 'lte'
8
+ | 'eq'
9
+ | 'ne'
10
+ | 'gte'
11
+ | 'gt'
12
+ | 'exists'
13
+ | 'not_exist'
14
+ | 'in'
15
+ | 'nin'
16
+ | 'contains'
17
+ | 'ncontains';
18
+
19
+ export type DependencyTerm< THasActualValue extends boolean = false > = {
20
+ operator: DependencyOperator;
21
+ path: string[];
22
+ value: PropValue;
23
+ actualValue: THasActualValue extends true ? PropValue : never;
24
+ };
25
+
26
+ export type Dependency< THasActualValue extends boolean = false > = {
27
+ effect: DependencyEffect;
28
+ relation: 'or' | 'and';
29
+ terms: ( DependencyTerm< THasActualValue > | Dependency< THasActualValue > )[];
30
+ };
31
+
3
32
  type BasePropType< TValue > = {
4
33
  default?: TValue | null;
5
34
  settings: Record< string, unknown >;
6
35
  meta: Record< string, unknown >;
36
+ dependencies?: Dependency[];
7
37
  };
8
38
 
9
39
  export type PlainPropType = BasePropType< PlainPropValue > & {
@@ -87,3 +87,10 @@ export function createPropUtils< TKey extends string, TValue extends PropValue >
87
87
  key: key as TKey,
88
88
  };
89
89
  }
90
+
91
+ export function createArrayPropUtils< TKey extends string, TValue extends PropValue >(
92
+ key: TKey,
93
+ valueSchema: ZodType< TValue >
94
+ ) {
95
+ return createPropUtils( `${ key }-array`, z.array( valueSchema ) );
96
+ }
@@ -0,0 +1,83 @@
1
+ import { type Dependency, type DependencyTerm, type PropValue } from '../types';
2
+
3
+ type ParsedTerm = DependencyTerm< true >;
4
+
5
+ type Relation = Dependency[ 'relation' ];
6
+
7
+ export function shouldApplyEffect( { relation, terms }: Dependency< true > ): boolean {
8
+ if ( ! terms.length ) {
9
+ return false;
10
+ }
11
+
12
+ const method = getRelationMethod( relation );
13
+
14
+ return terms[ method ]( ( term: ParsedTerm | Dependency< true > ) =>
15
+ isDependency( term ) ? shouldApplyEffect( term ) : evaluateTerm( term, term.actualValue )
16
+ );
17
+ }
18
+
19
+ export function evaluateTerm( term: DependencyTerm< true >, actualValue: PropValue ) {
20
+ const { value: valueToCompare, operator } = term;
21
+
22
+ switch ( operator ) {
23
+ case 'eq':
24
+ case 'ne':
25
+ return ( actualValue === valueToCompare ) === ( 'eq' === operator );
26
+
27
+ case 'gt':
28
+ case 'lte':
29
+ return (
30
+ ! isNaN( Number( actualValue ) ) &&
31
+ ! isNaN( Number( valueToCompare ) ) &&
32
+ Number( actualValue ) > Number( valueToCompare ) === ( 'gt' === operator )
33
+ );
34
+
35
+ case 'lt':
36
+ case 'gte':
37
+ return (
38
+ ! isNaN( Number( actualValue ) ) &&
39
+ ! isNaN( Number( valueToCompare ) ) &&
40
+ Number( actualValue ) < Number( valueToCompare ) === ( 'lt' === operator )
41
+ );
42
+
43
+ case 'in':
44
+ case 'nin':
45
+ return Array.isArray( valueToCompare ) && valueToCompare.includes( actualValue ) === ( 'in' === operator );
46
+
47
+ case 'contains':
48
+ case 'ncontains':
49
+ return (
50
+ 'string' === typeof actualValue &&
51
+ 'string' === typeof valueToCompare &&
52
+ ( 'contains' === operator ) === actualValue.includes( valueToCompare )
53
+ );
54
+
55
+ case 'exists':
56
+ case 'not_exist':
57
+ const evaluation = !! actualValue || 0 === actualValue || false === actualValue;
58
+
59
+ return ( 'exists' === operator ) === evaluation;
60
+
61
+ default:
62
+ return false;
63
+ }
64
+ }
65
+
66
+ function getRelationMethod( relation: Relation ) {
67
+ switch ( relation ) {
68
+ case 'or':
69
+ return 'some';
70
+
71
+ case 'and':
72
+ return 'every';
73
+
74
+ default:
75
+ throw new Error( `Relation not supported ${ relation }` );
76
+ }
77
+ }
78
+
79
+ function isDependency< THasActualValue extends boolean >(
80
+ term: DependencyTerm< THasActualValue > | Dependency< THasActualValue >
81
+ ): term is Dependency< THasActualValue > {
82
+ return 'relation' in term;
83
+ }