@elementor/editor-props 0.2.0 → 0.4.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.
@@ -0,0 +1,7 @@
1
+ import { z } from '@elementor/schema';
2
+
3
+ import { createPropUtils } from '../utils/create-prop-utils';
4
+
5
+ export const numberPropTypeUtil = createPropUtils( 'number', z.number() );
6
+
7
+ export type NumberPropValue = z.infer< typeof numberPropTypeUtil.schema >;
@@ -1,18 +1,18 @@
1
1
  import { z } from '@elementor/schema';
2
+
2
3
  import { createPropUtils } from '../utils/create-prop-utils';
3
- import { sizePropType } from './size';
4
- import { colorPropType } from './color';
4
+ import { unknownChildrenSchema } from './utils';
5
5
 
6
- export const shadowPropType = createPropUtils(
6
+ export const shadowPropTypeUtil = createPropUtils(
7
7
  'shadow',
8
- z.object( {
9
- position: z.nullable( z.literal( 'inset' ) ),
10
- hOffset: sizePropType.schema,
11
- vOffset: sizePropType.schema,
12
- blur: sizePropType.schema,
13
- spread: sizePropType.schema,
14
- color: colorPropType.schema,
8
+ z.strictObject( {
9
+ position: unknownChildrenSchema,
10
+ hOffset: unknownChildrenSchema,
11
+ vOffset: unknownChildrenSchema,
12
+ blur: unknownChildrenSchema,
13
+ spread: unknownChildrenSchema,
14
+ color: unknownChildrenSchema,
15
15
  } )
16
16
  );
17
17
 
18
- export type ShadowPropValue = z.infer< typeof shadowPropType.schema >;
18
+ export type ShadowPropValue = z.infer< typeof shadowPropTypeUtil.schema >;
@@ -1,12 +1,13 @@
1
1
  import { z } from '@elementor/schema';
2
+
2
3
  import { createPropUtils } from '../utils/create-prop-utils';
3
4
 
4
- export const sizePropType = createPropUtils(
5
+ export const sizePropTypeUtil = createPropUtils(
5
6
  'size',
6
- z.object( {
7
+ z.strictObject( {
7
8
  unit: z.enum( [ 'px', 'em', 'rem', '%', 'vw', 'vh' ] ),
8
9
  size: z.number(),
9
10
  } )
10
11
  );
11
12
 
12
- export type SizePropValue = z.infer< typeof sizePropType.schema >;
13
+ export type SizePropValue = z.infer< typeof sizePropTypeUtil.schema >;
@@ -0,0 +1,7 @@
1
+ import { z } from '@elementor/schema';
2
+
3
+ import { createPropUtils } from '../utils/create-prop-utils';
4
+
5
+ export const stringPropTypeUtil = createPropUtils( 'string', z.string().nullable() );
6
+
7
+ export type StringPropValue = z.infer< typeof stringPropTypeUtil.schema >;
@@ -1,14 +1,14 @@
1
1
  import { z } from '@elementor/schema';
2
+
2
3
  import { createPropUtils } from '../utils/create-prop-utils';
3
- import { colorPropType } from './color';
4
- import { sizePropType } from './size';
4
+ import { unknownChildrenSchema } from './utils';
5
5
 
6
- export const strokePropType = createPropUtils(
6
+ export const strokePropTypeUtil = createPropUtils(
7
7
  'stroke',
8
- z.object( {
9
- color: colorPropType.schema,
10
- width: sizePropType.schema,
8
+ z.strictObject( {
9
+ color: unknownChildrenSchema,
10
+ width: unknownChildrenSchema,
11
11
  } )
12
12
  );
13
13
 
14
- export type StrokePropValue = z.infer< typeof strokePropType.schema >;
14
+ export type StrokePropValue = z.infer< typeof strokePropTypeUtil.schema >;
@@ -1,6 +1,7 @@
1
1
  import { z } from '@elementor/schema';
2
+
2
3
  import { createPropUtils } from '../utils/create-prop-utils';
3
4
 
4
- export const urlPropType = createPropUtils( 'url', z.string() );
5
+ export const urlPropTypeUtil = createPropUtils( 'url', z.string().url().nullable() );
5
6
 
6
- export type UrlPropValue = z.infer< typeof urlPropType.schema >;
7
+ export type UrlPropValue = z.infer< typeof urlPropTypeUtil.schema >;
@@ -0,0 +1,3 @@
1
+ import { z } from '@elementor/schema';
2
+
3
+ export const unknownChildrenSchema = z.any().nullable();
@@ -1,7 +1,14 @@
1
- import { z, type ZodTypeAny } from '@elementor/schema';
1
+ import { z, type ZodType, type ZodTypeAny } from '@elementor/schema';
2
+
3
+ import { type PropValue } from '../types';
2
4
 
3
5
  type Updater< T > = ( prev?: T ) => T;
4
6
 
7
+ export type CreateOptions = {
8
+ base?: unknown;
9
+ disabled?: boolean;
10
+ };
11
+
5
12
  /**
6
13
  * Usage example:
7
14
  *
@@ -10,32 +17,46 @@ type Updater< T > = ( prev?: T ) => T;
10
17
  *
11
18
  * elementsPropUtils.isValid( element.props?.children );
12
19
  * elementsPropUtils.create( [ 'a', 'b' ] );
13
- * elementsPropUtils.create( ( prev = [] ) => [ ...prev, 'c' ], element.props?.children );
20
+ * elementsPropUtils.create( ( prev = [] ) => [ ...prev, 'c' ], { base: element.props?.children } );
21
+ * elementsPropUtils.create( ( prev = [] ) => [ ...prev, 'c' ], { disabled: true } );
22
+ * elementsPropUtils.extract( element.props?.children );
23
+ *
14
24
  * ```
15
25
  */
16
26
 
27
+ export type PropTypeUtil< TKey extends string, TValue extends PropValue > = ReturnType<
28
+ typeof createPropUtils< TKey, ZodType< TValue > >
29
+ >;
30
+
17
31
  export function createPropUtils< TKey extends string, TValue extends ZodTypeAny >( key: TKey, valueSchema: TValue ) {
18
- const schema = z.object( {
32
+ const schema = z.strictObject( {
19
33
  $$type: z.literal( key ),
20
34
  value: valueSchema,
35
+ disabled: z.boolean().optional(),
21
36
  } );
22
37
 
23
38
  type Prop = z.infer< typeof schema >;
24
39
 
40
+ type TPropValue = z.infer< typeof valueSchema >;
41
+
25
42
  function isValid( prop: unknown ): prop is Prop {
26
43
  return schema.safeParse( prop ).success;
27
44
  }
28
45
 
29
- function create( value: Prop[ 'value' ] ): Prop;
30
- function create( value: Updater< Prop[ 'value' ] >, base: unknown ): Prop;
31
- function create( value: Prop[ 'value' ] | Updater< Prop[ 'value' ] >, base?: unknown ): Prop {
32
- const fn = ( typeof value === 'function' ? value : () => value ) as Updater< Prop[ 'value' ] >;
46
+ function create( value: TPropValue ): Prop;
47
+ function create( value: TPropValue, createOptions?: CreateOptions ): Prop;
48
+ function create( value: Updater< TPropValue >, createOptions: CreateOptions ): Prop;
49
+ function create( value: TPropValue | Updater< TPropValue >, createOptions?: CreateOptions ): Prop {
50
+ const fn = ( typeof value === 'function' ? value : () => value ) as Updater< TPropValue >;
51
+
52
+ const { base, disabled } = createOptions || {};
33
53
 
34
54
  if ( ! base ) {
35
55
  return {
36
56
  $$type: key,
37
57
  value: fn(),
38
- };
58
+ ...( disabled && { disabled } ),
59
+ } as Prop;
39
60
  }
40
61
 
41
62
  if ( ! isValid( base ) ) {
@@ -45,12 +66,23 @@ export function createPropUtils< TKey extends string, TValue extends ZodTypeAny
45
66
  return {
46
67
  $$type: key,
47
68
  value: fn( base.value ),
48
- };
69
+ ...( disabled && { disabled } ),
70
+ } as Prop;
71
+ }
72
+
73
+ function extract( prop: unknown ): TPropValue | null {
74
+ if ( ! isValid( prop ) ) {
75
+ return null;
76
+ }
77
+
78
+ return prop.value;
49
79
  }
50
80
 
51
81
  return {
82
+ extract,
52
83
  isValid,
53
84
  create,
54
- schema,
85
+ // this type fails in build due to zod issue that does not recognize the schema as a ZodType.
86
+ schema: schema as ZodType< Prop >,
55
87
  };
56
88
  }