@elementor/editor-interactions 4.0.0-537 → 4.0.0-539

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,65 @@
1
+ import { type ElementInteractions } from '@elementor/editor-elements';
2
+ import {
3
+ isTransformable,
4
+ numberPropTypeUtil,
5
+ type PropValue,
6
+ type SizePropValue,
7
+ type TransformablePropValue,
8
+ } from '@elementor/editor-props';
9
+
10
+ import { type TimeUnit } from '../../types';
11
+ import { convertTimeUnit } from '../../utils/time-conversion';
12
+
13
+ type Normalizer = ( value: TransformablePropValue< string, unknown > ) => PropValue;
14
+
15
+ const FIELD_NORMALIZERS: Record< string, Normalizer > = {
16
+ size: ( value ) => {
17
+ const sizeProp = value as SizePropValue;
18
+ const { size, unit } = sizeProp.value;
19
+ const numberPropValue = convertTimeUnit( size as number, unit as TimeUnit, 'ms' );
20
+
21
+ return numberPropTypeUtil.create( numberPropValue );
22
+ },
23
+ };
24
+
25
+ export const normalizeInteractions = ( interactions?: ElementInteractions ): ElementInteractions | undefined => {
26
+ if ( ! interactions ) {
27
+ return;
28
+ }
29
+
30
+ if ( interactions.items.length === 0 ) {
31
+ return interactions;
32
+ }
33
+
34
+ return {
35
+ ...interactions,
36
+ items: interactions.items.map( normalizeNode ),
37
+ } as ElementInteractions;
38
+ };
39
+
40
+ const normalizeNode = ( node: PropValue ): PropValue => {
41
+ if ( Array.isArray( node ) ) {
42
+ return node.map( normalizeNode );
43
+ }
44
+
45
+ if ( node !== null && typeof node === 'object' ) {
46
+ if ( isTransformable( node ) && node.value !== undefined ) {
47
+ const normalizer = FIELD_NORMALIZERS[ node.$$type ];
48
+
49
+ if ( normalizer ) {
50
+ return normalizer( node as TransformablePropValue< string > );
51
+ }
52
+ }
53
+
54
+ const typedNode = node as Record< string, unknown >;
55
+ const out: Record< string, PropValue > = {};
56
+
57
+ for ( const k in typedNode ) {
58
+ out[ k ] = normalizeNode( typedNode[ k ] as PropValue );
59
+ }
60
+
61
+ return out;
62
+ }
63
+
64
+ return node;
65
+ };
package/src/types.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { type Unit } from '@elementor/editor-controls';
1
2
  import type {
2
3
  AnimationPresetPropValue,
3
4
  BooleanPropValue,
@@ -75,3 +76,7 @@ export type InteractionsProvider = {
75
76
  };
76
77
 
77
78
  export type InteractionItemValue = InteractionItemPropValue[ 'value' ];
79
+
80
+ export type TimeUnit = Extract< Unit, 'ms' | 's' >;
81
+
82
+ export type TimeValue = `${ number }${ TimeUnit }` | number;
@@ -1,16 +1,21 @@
1
- import type {
2
- AnimationPresetPropValue,
3
- BooleanPropValue,
4
- ConfigPropValue,
5
- ElementInteractions,
6
- ExcludedBreakpointsPropValue,
7
- InteractionBreakpointsPropValue,
8
- InteractionItemPropValue,
9
- InteractionItemValue,
10
- NumberPropValue,
11
- StringPropValue,
12
- TimingConfigPropValue,
1
+ import { sizePropTypeUtil, type SizePropValue } from '@elementor/editor-props';
2
+
3
+ import { DEFAULT_TIME_UNIT, TIME_UNITS } from '../configs/time-constants';
4
+ import {
5
+ type AnimationPresetPropValue,
6
+ type BooleanPropValue,
7
+ type ConfigPropValue,
8
+ type ElementInteractions,
9
+ type ExcludedBreakpointsPropValue,
10
+ type InteractionBreakpointsPropValue,
11
+ type InteractionItemPropValue,
12
+ type InteractionItemValue,
13
+ type NumberPropValue,
14
+ type StringPropValue,
15
+ type TimeValue,
16
+ type TimingConfigPropValue,
13
17
  } from '../types';
18
+ import { formatSizeValue, parseSizeValue } from '../utils/size-transform-utils';
14
19
  import { generateTempInteractionId } from './temp-id-utils';
15
20
 
16
21
  export const createString = ( value: string ): StringPropValue => ( {
@@ -23,11 +28,11 @@ export const createNumber = ( value: number ): NumberPropValue => ( {
23
28
  value,
24
29
  } );
25
30
 
26
- export const createTimingConfig = ( duration: number, delay: number ): TimingConfigPropValue => ( {
31
+ export const createTimingConfig = ( duration: TimeValue, delay: TimeValue ): TimingConfigPropValue => ( {
27
32
  $$type: 'timing-config',
28
33
  value: {
29
- duration: createNumber( duration ),
30
- delay: createNumber( delay ),
34
+ duration: sizePropTypeUtil.create( parseSizeValue( duration, TIME_UNITS, undefined, DEFAULT_TIME_UNIT ) ),
35
+ delay: sizePropTypeUtil.create( parseSizeValue( delay, TIME_UNITS, undefined, DEFAULT_TIME_UNIT ) ),
31
36
  },
32
37
  } );
33
38
 
@@ -94,8 +99,8 @@ export const createAnimationPreset = ( {
94
99
  effect: string;
95
100
  type: string;
96
101
  direction?: string;
97
- duration: number;
98
- delay: number;
102
+ duration: TimeValue;
103
+ delay: TimeValue;
99
104
  replay: boolean;
100
105
  easing?: string;
101
106
  relativeTo?: string;
@@ -137,8 +142,8 @@ export const createInteractionItem = ( {
137
142
  effect: string;
138
143
  type: string;
139
144
  direction?: string;
140
- duration: number;
141
- delay: number;
145
+ duration: TimeValue;
146
+ delay: TimeValue;
142
147
  interactionId?: string;
143
148
  replay: boolean;
144
149
  easing?: string;
@@ -192,6 +197,10 @@ export const extractString = ( prop: StringPropValue | undefined, fallback = ''
192
197
  return prop?.value ?? fallback;
193
198
  };
194
199
 
200
+ export const extractSize = ( prop: SizePropValue ): TimeValue => {
201
+ return formatSizeValue( prop?.value );
202
+ };
203
+
195
204
  export const extractNumber = ( prop: NumberPropValue | undefined, fallback = 0 ): number => {
196
205
  return prop?.value ?? fallback;
197
206
  };
@@ -0,0 +1,67 @@
1
+ import { type SizePropValue } from '@elementor/editor-props';
2
+
3
+ import { type TimeUnit, type TimeValue } from '../types';
4
+
5
+ type SizeValue = SizePropValue[ 'value' ];
6
+ type SizeUnit = SizeValue[ 'unit' ];
7
+
8
+ const SIZE_REGEX = /^(?:(-?\d*\.?\d+)([a-z%]+)|([a-z%]+))$/i;
9
+
10
+ export const parseSizeValue = (
11
+ value: TimeValue,
12
+ allowedUnits: SizeUnit[],
13
+ defaultValue?: TimeValue,
14
+ defaultUnit?: TimeUnit
15
+ ): SizeValue => {
16
+ if ( typeof value === 'number' ) {
17
+ return {
18
+ size: value,
19
+ unit: defaultUnit as TimeUnit,
20
+ };
21
+ }
22
+
23
+ const sizeValue = tryParse( value, allowedUnits, defaultUnit );
24
+
25
+ if ( sizeValue ) {
26
+ return sizeValue;
27
+ }
28
+
29
+ if ( defaultValue ) {
30
+ const fallbackSize = tryParse( defaultValue, allowedUnits, defaultUnit );
31
+
32
+ if ( fallbackSize ) {
33
+ return fallbackSize;
34
+ }
35
+ }
36
+
37
+ return createSizeValue( null, defaultUnit );
38
+ };
39
+
40
+ const tryParse = ( value: TimeValue, allowedUnits: SizeUnit[], defaultUnit?: TimeUnit ): SizeValue | null => {
41
+ if ( typeof value === 'number' ) {
42
+ return createSizeValue( value, defaultUnit );
43
+ }
44
+
45
+ const match = value && value.match( SIZE_REGEX );
46
+
47
+ if ( ! match ) {
48
+ return null;
49
+ }
50
+
51
+ const size = match[ 1 ] ? parseFloat( match[ 1 ] ) : null;
52
+ const unit = ( match[ 2 ] || match[ 3 ] ) as SizeUnit;
53
+
54
+ if ( ! allowedUnits.includes( unit ) ) {
55
+ return null;
56
+ }
57
+
58
+ return createSizeValue( size, unit );
59
+ };
60
+
61
+ export const formatSizeValue = ( { size, unit }: SizeValue ): TimeValue => {
62
+ return `${ size ?? '' }${ unit }` as TimeValue;
63
+ };
64
+
65
+ const createSizeValue = ( size: number | null, unit?: SizeUnit ): SizeValue => {
66
+ return { size, unit } as SizeValue;
67
+ };
@@ -0,0 +1,10 @@
1
+ import { type TimeUnit } from '../types';
2
+
3
+ const UNIT_TO_MS: Record< TimeUnit, number > = {
4
+ ms: 1,
5
+ s: 1000,
6
+ };
7
+
8
+ export const convertTimeUnit = ( value: number, from: TimeUnit, to: TimeUnit ): number => {
9
+ return ( value * UNIT_TO_MS[ from ] ) / UNIT_TO_MS[ to ];
10
+ };