@elementor/editor-controls 4.1.0-758 → 4.1.0-759
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/dist/index.d.mts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +92 -63
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +67 -38
- package/dist/index.mjs.map +1 -1
- package/package.json +15 -15
- package/src/controls/size-control/hooks/use-size-value.ts +18 -8
- package/src/controls/size-control/hooks/use-unit-sync.ts +28 -0
- package/src/controls/size-control/unstable-size-control.tsx +1 -1
- package/src/controls/size-control/utils/resolve-bound-prop-value.ts +31 -27
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@elementor/editor-controls",
|
|
3
3
|
"description": "This package contains the controls model and utils for the Elementor editor",
|
|
4
|
-
"version": "4.1.0-
|
|
4
|
+
"version": "4.1.0-759",
|
|
5
5
|
"private": false,
|
|
6
6
|
"author": "Elementor Team",
|
|
7
7
|
"homepage": "https://elementor.com/",
|
|
@@ -40,22 +40,22 @@
|
|
|
40
40
|
"dev": "tsup --config=../../tsup.dev.ts"
|
|
41
41
|
},
|
|
42
42
|
"dependencies": {
|
|
43
|
-
"@elementor/editor-current-user": "4.1.0-
|
|
44
|
-
"@elementor/editor-elements": "4.1.0-
|
|
45
|
-
"@elementor/editor-props": "4.1.0-
|
|
46
|
-
"@elementor/editor-responsive": "4.1.0-
|
|
47
|
-
"@elementor/editor-ui": "4.1.0-
|
|
48
|
-
"@elementor/editor-v1-adapters": "4.1.0-
|
|
49
|
-
"@elementor/env": "4.1.0-
|
|
50
|
-
"@elementor/http-client": "4.1.0-
|
|
43
|
+
"@elementor/editor-current-user": "4.1.0-759",
|
|
44
|
+
"@elementor/editor-elements": "4.1.0-759",
|
|
45
|
+
"@elementor/editor-props": "4.1.0-759",
|
|
46
|
+
"@elementor/editor-responsive": "4.1.0-759",
|
|
47
|
+
"@elementor/editor-ui": "4.1.0-759",
|
|
48
|
+
"@elementor/editor-v1-adapters": "4.1.0-759",
|
|
49
|
+
"@elementor/env": "4.1.0-759",
|
|
50
|
+
"@elementor/http-client": "4.1.0-759",
|
|
51
51
|
"@elementor/icons": "^1.68.0",
|
|
52
|
-
"@elementor/locations": "4.1.0-
|
|
53
|
-
"@elementor/events": "4.1.0-
|
|
54
|
-
"@elementor/query": "4.1.0-
|
|
55
|
-
"@elementor/session": "4.1.0-
|
|
52
|
+
"@elementor/locations": "4.1.0-759",
|
|
53
|
+
"@elementor/events": "4.1.0-759",
|
|
54
|
+
"@elementor/query": "4.1.0-759",
|
|
55
|
+
"@elementor/session": "4.1.0-759",
|
|
56
56
|
"@elementor/ui": "1.36.17",
|
|
57
|
-
"@elementor/utils": "4.1.0-
|
|
58
|
-
"@elementor/wp-media": "4.1.0-
|
|
57
|
+
"@elementor/utils": "4.1.0-759",
|
|
58
|
+
"@elementor/wp-media": "4.1.0-759",
|
|
59
59
|
"@wordpress/i18n": "^5.13.0",
|
|
60
60
|
"@monaco-editor/react": "^4.7.0",
|
|
61
61
|
"dayjs": "^1.11.18",
|
|
@@ -5,6 +5,7 @@ import { useSyncExternalState } from '../../../hooks/use-sync-external-state';
|
|
|
5
5
|
import { type SetSizeValue, type SizeUnit } from '../types';
|
|
6
6
|
import { isExtendedUnit } from '../utils/is-extended-unit';
|
|
7
7
|
import { createDefaultSizeValue, resolveSizeOnUnitChange, resolveSizeValue } from '../utils/resolve-size-value';
|
|
8
|
+
import { useUnitSync } from './use-unit-sync';
|
|
8
9
|
|
|
9
10
|
type SizeValue = SizePropValue[ 'value' ];
|
|
10
11
|
|
|
@@ -38,8 +39,21 @@ export const useSizeValue = < T extends SizeValue, U extends SizeUnit >( {
|
|
|
38
39
|
fallback: () => createDefaultSizeValue( units, defaultUnit ),
|
|
39
40
|
} );
|
|
40
41
|
|
|
42
|
+
const [ unit, setUnit ] = useUnitSync( {
|
|
43
|
+
unit: sizeValue?.unit,
|
|
44
|
+
setUnit: ( newUnit ) => {
|
|
45
|
+
setSizeValue( {
|
|
46
|
+
unit: newUnit,
|
|
47
|
+
size: resolveSizeOnUnitChange( sizeValue.size, newUnit ),
|
|
48
|
+
} as T );
|
|
49
|
+
},
|
|
50
|
+
persistWhen: () => {
|
|
51
|
+
return Boolean( sizeValue.size ) || sizeValue.size !== '';
|
|
52
|
+
},
|
|
53
|
+
} );
|
|
54
|
+
|
|
41
55
|
const setSize = ( newSize: string, isInputValid = true ) => {
|
|
42
|
-
if ( isExtendedUnit(
|
|
56
|
+
if ( isExtendedUnit( unit ) ) {
|
|
43
57
|
return;
|
|
44
58
|
}
|
|
45
59
|
|
|
@@ -47,21 +61,17 @@ export const useSizeValue = < T extends SizeValue, U extends SizeUnit >( {
|
|
|
47
61
|
const parsed = Number( trimmed );
|
|
48
62
|
|
|
49
63
|
const newState = {
|
|
50
|
-
|
|
64
|
+
unit,
|
|
51
65
|
size: trimmed && ! isNaN( parsed ) ? parsed : '',
|
|
52
|
-
};
|
|
66
|
+
} as T;
|
|
53
67
|
|
|
54
68
|
setSizeValue( newState, undefined, { validation: () => isInputValid } );
|
|
55
69
|
};
|
|
56
70
|
|
|
57
|
-
const setUnit = ( unit: SizeValue[ 'unit' ] ) => {
|
|
58
|
-
setSizeValue( { unit, size: resolveSizeOnUnitChange( sizeValue.size, unit ) } as T );
|
|
59
|
-
};
|
|
60
|
-
|
|
61
71
|
return {
|
|
62
72
|
size: sizeValue.size,
|
|
63
|
-
unit: sizeValue.unit,
|
|
64
73
|
setSize,
|
|
74
|
+
unit,
|
|
65
75
|
setUnit,
|
|
66
76
|
};
|
|
67
77
|
};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { useEffect, useState } from 'react';
|
|
2
|
+
|
|
3
|
+
type UseUnitSyncProps< U > = {
|
|
4
|
+
unit: U;
|
|
5
|
+
setUnit: ( unit: U ) => void;
|
|
6
|
+
persistWhen: () => boolean;
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
export const useUnitSync = < U >( { unit, setUnit, persistWhen }: UseUnitSyncProps< U > ) => {
|
|
10
|
+
const [ state, setState ] = useState( unit );
|
|
11
|
+
|
|
12
|
+
useEffect( () => {
|
|
13
|
+
if ( unit !== state ) {
|
|
14
|
+
setState( unit );
|
|
15
|
+
}
|
|
16
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
17
|
+
}, [ unit ] );
|
|
18
|
+
|
|
19
|
+
const setInternalValue = ( newUnit: U ) => {
|
|
20
|
+
setState( newUnit );
|
|
21
|
+
|
|
22
|
+
if ( persistWhen() ) {
|
|
23
|
+
setUnit( newUnit );
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
return [ state, setInternalValue ] as const;
|
|
28
|
+
};
|
|
@@ -13,7 +13,7 @@ import { getSizeUnits } from './utils/settings/get-size-units';
|
|
|
13
13
|
import { shouldNullifyValue } from './utils/should-nullify-value';
|
|
14
14
|
|
|
15
15
|
type Props = {
|
|
16
|
-
placeholder?: string;
|
|
16
|
+
placeholder?: string | SizePropValue[ 'value' ];
|
|
17
17
|
variant?: SizeVariant;
|
|
18
18
|
anchorRef?: RefObject< HTMLDivElement | null >;
|
|
19
19
|
startIcon?: React.ReactNode;
|
|
@@ -1,55 +1,59 @@
|
|
|
1
|
-
import { sizePropTypeUtil, type SizePropValue } from '@elementor/editor-props';
|
|
1
|
+
import { type PropValue, sizePropTypeUtil, type SizePropValue } from '@elementor/editor-props';
|
|
2
2
|
|
|
3
|
-
type SizeValue = SizePropValue[ 'value' ]
|
|
3
|
+
type SizeValue = SizePropValue[ 'value' ];
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
5
|
+
type SizeValueResolver< T extends SizeValue > = {
|
|
6
|
+
candidate: PropValue;
|
|
7
|
+
resolve: ( v: T ) => T;
|
|
8
8
|
};
|
|
9
9
|
|
|
10
10
|
export const resolveBoundPropValue = < T extends SizeValue >(
|
|
11
11
|
value?: T | null,
|
|
12
12
|
boundPropPlaceholder?: T | null,
|
|
13
|
-
propPlaceholder?: string
|
|
14
|
-
)
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
13
|
+
propPlaceholder?: string | SizeValue
|
|
14
|
+
) => {
|
|
15
|
+
const sizeValue = pickFirstValid< T >( [
|
|
16
|
+
{ candidate: value, resolve: ( v ) => v },
|
|
17
|
+
{ candidate: propPlaceholder, resolve: toUnitPlaceholder },
|
|
18
|
+
{ candidate: boundPropPlaceholder, resolve: toUnitPlaceholder },
|
|
19
|
+
] );
|
|
20
|
+
|
|
21
|
+
const placeholderSource = propPlaceholder ?? boundPropPlaceholder;
|
|
22
22
|
|
|
23
23
|
return {
|
|
24
24
|
sizeValue,
|
|
25
|
-
placeholder: resolvePlaceholder(
|
|
25
|
+
placeholder: resolvePlaceholder( placeholderSource ),
|
|
26
26
|
};
|
|
27
27
|
};
|
|
28
28
|
|
|
29
|
-
const
|
|
30
|
-
|
|
29
|
+
const toUnitPlaceholder = < T extends SizeValue >( v: T ): T => ( { ...v, size: '' } );
|
|
30
|
+
|
|
31
|
+
const pickFirstValid = < T extends SizeValue >( candidates: SizeValueResolver< T >[] ): T | null => {
|
|
32
|
+
const found = candidates.find( ( { candidate } ) => validateSizeValue( candidate ) );
|
|
33
|
+
|
|
34
|
+
return found ? found.resolve( found.candidate as T ) : null;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
const validateSizeValue = ( value: PropValue ): value is SizeValue => {
|
|
38
|
+
if ( ! value || typeof value !== 'object' ) {
|
|
31
39
|
return false;
|
|
32
40
|
}
|
|
33
41
|
|
|
34
|
-
const sizePropValue = sizePropTypeUtil.create( value );
|
|
42
|
+
const sizePropValue = sizePropTypeUtil.create( value as SizeValue );
|
|
35
43
|
|
|
36
44
|
return sizePropTypeUtil.isValid( sizePropValue );
|
|
37
45
|
};
|
|
38
46
|
|
|
39
|
-
const resolvePlaceholder = (
|
|
40
|
-
if (
|
|
41
|
-
return
|
|
47
|
+
const resolvePlaceholder = ( placeholder?: string | null | SizeValue ): string | undefined => {
|
|
48
|
+
if ( typeof placeholder === 'string' ) {
|
|
49
|
+
return placeholder;
|
|
42
50
|
}
|
|
43
51
|
|
|
44
|
-
const size =
|
|
52
|
+
const size = placeholder?.size;
|
|
45
53
|
|
|
46
54
|
if ( size === undefined ) {
|
|
47
55
|
return undefined;
|
|
48
56
|
}
|
|
49
57
|
|
|
50
|
-
|
|
51
|
-
return size.toString();
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
return size;
|
|
58
|
+
return typeof size === 'number' ? size.toString() : size;
|
|
55
59
|
};
|