@elementor/editor-props 0.2.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/CHANGELOG.md +14 -0
- package/README.md +4 -0
- package/dist/index.d.mts +2346 -0
- package/dist/index.d.ts +2346 -0
- package/dist/index.js +225 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +184 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +45 -0
- package/src/index.ts +9 -0
- package/src/prop-types/border-radius.ts +14 -0
- package/src/prop-types/border-width.ts +14 -0
- package/src/prop-types/box-shadow.ts +7 -0
- package/src/prop-types/classes.ts +6 -0
- package/src/prop-types/color.ts +6 -0
- package/src/prop-types/image-attachment-id.ts +6 -0
- package/src/prop-types/image-src.ts +21 -0
- package/src/prop-types/image.ts +14 -0
- package/src/prop-types/index.ts +13 -0
- package/src/prop-types/linked-dimensions.ts +15 -0
- package/src/prop-types/shadow.ts +18 -0
- package/src/prop-types/size.ts +12 -0
- package/src/prop-types/stroke.ts +14 -0
- package/src/prop-types/url.ts +6 -0
- package/src/types.ts +51 -0
- package/src/utils/create-prop-utils.ts +56 -0
- package/src/utils/is-transformable.ts +12 -0
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { z } from '@elementor/schema';
|
|
2
|
+
import { createPropUtils } from '../utils/create-prop-utils';
|
|
3
|
+
|
|
4
|
+
export const sizePropType = createPropUtils(
|
|
5
|
+
'size',
|
|
6
|
+
z.object( {
|
|
7
|
+
unit: z.enum( [ 'px', 'em', 'rem', '%', 'vw', 'vh' ] ),
|
|
8
|
+
size: z.number(),
|
|
9
|
+
} )
|
|
10
|
+
);
|
|
11
|
+
|
|
12
|
+
export type SizePropValue = z.infer< typeof sizePropType.schema >;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { z } from '@elementor/schema';
|
|
2
|
+
import { createPropUtils } from '../utils/create-prop-utils';
|
|
3
|
+
import { colorPropType } from './color';
|
|
4
|
+
import { sizePropType } from './size';
|
|
5
|
+
|
|
6
|
+
export const strokePropType = createPropUtils(
|
|
7
|
+
'stroke',
|
|
8
|
+
z.object( {
|
|
9
|
+
color: colorPropType.schema,
|
|
10
|
+
width: sizePropType.schema,
|
|
11
|
+
} )
|
|
12
|
+
);
|
|
13
|
+
|
|
14
|
+
export type StrokePropValue = z.infer< typeof strokePropType.schema >;
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
type PropTypeKey = string;
|
|
2
|
+
|
|
3
|
+
type BasePropType = {
|
|
4
|
+
default: PropValue;
|
|
5
|
+
settings: Record< string, unknown >;
|
|
6
|
+
meta: Record< string, unknown >;
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
export type PlainPropType = BasePropType & {
|
|
10
|
+
kind: 'plain';
|
|
11
|
+
key: PropTypeKey;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export type ArrayPropType = BasePropType & {
|
|
15
|
+
kind: 'array';
|
|
16
|
+
key: PropTypeKey;
|
|
17
|
+
item_prop_type: PropType | null;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export type ObjectPropType = BasePropType & {
|
|
21
|
+
kind: 'object';
|
|
22
|
+
key: PropTypeKey;
|
|
23
|
+
shape: Record< string, PropType >;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export type TransformablePropType = PlainPropType | ArrayPropType | ObjectPropType;
|
|
27
|
+
|
|
28
|
+
export type UnionPropType = BasePropType & {
|
|
29
|
+
kind: 'union';
|
|
30
|
+
prop_types: Record< string, TransformablePropType >;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export type PropType = TransformablePropType | UnionPropType;
|
|
34
|
+
|
|
35
|
+
type MaybeArray< T > = T | T[];
|
|
36
|
+
|
|
37
|
+
export type TransformablePropValue< Type extends string, Value = unknown > = {
|
|
38
|
+
$$type: Type;
|
|
39
|
+
value: Value;
|
|
40
|
+
disabled?: boolean;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
export type PlainPropValue = MaybeArray< string | number | boolean | object | null | undefined >;
|
|
44
|
+
|
|
45
|
+
export type PropValue = PlainPropValue | TransformablePropValue< string >;
|
|
46
|
+
|
|
47
|
+
export type PropKey = string;
|
|
48
|
+
|
|
49
|
+
export type Props = Record< PropKey, PropValue >;
|
|
50
|
+
|
|
51
|
+
export type PlainProps = Record< PropKey, PlainPropValue >;
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { z, type ZodTypeAny } from '@elementor/schema';
|
|
2
|
+
|
|
3
|
+
type Updater< T > = ( prev?: T ) => T;
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Usage example:
|
|
7
|
+
*
|
|
8
|
+
* ```ts
|
|
9
|
+
* const elementsPropUtils = createPropUtils( 'elements', z.array( z.string() ) );
|
|
10
|
+
*
|
|
11
|
+
* elementsPropUtils.isValid( element.props?.children );
|
|
12
|
+
* elementsPropUtils.create( [ 'a', 'b' ] );
|
|
13
|
+
* elementsPropUtils.create( ( prev = [] ) => [ ...prev, 'c' ], element.props?.children );
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
export function createPropUtils< TKey extends string, TValue extends ZodTypeAny >( key: TKey, valueSchema: TValue ) {
|
|
18
|
+
const schema = z.object( {
|
|
19
|
+
$$type: z.literal( key ),
|
|
20
|
+
value: valueSchema,
|
|
21
|
+
} );
|
|
22
|
+
|
|
23
|
+
type Prop = z.infer< typeof schema >;
|
|
24
|
+
|
|
25
|
+
function isValid( prop: unknown ): prop is Prop {
|
|
26
|
+
return schema.safeParse( prop ).success;
|
|
27
|
+
}
|
|
28
|
+
|
|
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' ] >;
|
|
33
|
+
|
|
34
|
+
if ( ! base ) {
|
|
35
|
+
return {
|
|
36
|
+
$$type: key,
|
|
37
|
+
value: fn(),
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
if ( ! isValid( base ) ) {
|
|
42
|
+
throw new Error( `Cannot create prop based on invalid value: ${ JSON.stringify( base ) }` );
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return {
|
|
46
|
+
$$type: key,
|
|
47
|
+
value: fn( base.value ),
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return {
|
|
52
|
+
isValid,
|
|
53
|
+
create,
|
|
54
|
+
schema,
|
|
55
|
+
};
|
|
56
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { z } from '@elementor/schema';
|
|
2
|
+
|
|
3
|
+
const transformableSchema = z.object( {
|
|
4
|
+
$$type: z.string(),
|
|
5
|
+
value: z.any(),
|
|
6
|
+
} );
|
|
7
|
+
|
|
8
|
+
type TransformablePropValue = z.infer< typeof transformableSchema >;
|
|
9
|
+
|
|
10
|
+
export const isTransformable = ( value: unknown ): value is TransformablePropValue => {
|
|
11
|
+
return transformableSchema.safeParse( value ).success;
|
|
12
|
+
};
|