@elementor/editor-props 4.4.0-1087 → 4.4.0-1089

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,89 @@
1
+ export const RESPONSIVE_BREAKPOINT_KEYS = [
2
+ 'mobile',
3
+ 'mobile_extra',
4
+ 'tablet',
5
+ 'tablet_extra',
6
+ 'laptop',
7
+ 'desktop',
8
+ 'widescreen',
9
+ ] as const;
10
+
11
+ export type ResponsiveBreakpointKey = ( typeof RESPONSIVE_BREAKPOINT_KEYS )[ number ];
12
+
13
+ export type ActiveBreakpoints = string[] | Record< string, unknown > | null | undefined;
14
+
15
+ export function responsiveFallbackChain( breakpoint: string ): string[] {
16
+ const keys = [ ...RESPONSIVE_BREAKPOINT_KEYS ];
17
+ const index = keys.indexOf( breakpoint as ResponsiveBreakpointKey );
18
+ const desktopIndex = keys.indexOf( 'desktop' );
19
+
20
+ if ( -1 === index || -1 === desktopIndex ) {
21
+ return [ 'desktop' ];
22
+ }
23
+
24
+ if ( index > desktopIndex ) {
25
+ return [ breakpoint, 'desktop' ];
26
+ }
27
+
28
+ return keys.slice( index, desktopIndex + 1 );
29
+ }
30
+
31
+ function isBreakpointActive( key: string, activeBreakpoints: ActiveBreakpoints ): boolean {
32
+ if ( 'desktop' === key || ! activeBreakpoints ) {
33
+ return true;
34
+ }
35
+
36
+ if ( Array.isArray( activeBreakpoints ) ) {
37
+ return activeBreakpoints.includes( key );
38
+ }
39
+
40
+ return Object.prototype.hasOwnProperty.call( activeBreakpoints, key );
41
+ }
42
+
43
+ function unwrapResponsiveEntry( entry: unknown ): unknown {
44
+ if ( null === entry || undefined === entry || '' === entry ) {
45
+ return null;
46
+ }
47
+
48
+ if (
49
+ typeof entry === 'object' &&
50
+ ! Array.isArray( entry ) &&
51
+ entry !== null &&
52
+ '$$type' in entry &&
53
+ 'value' in entry
54
+ ) {
55
+ return ( entry as { value: unknown } ).value;
56
+ }
57
+
58
+ return entry;
59
+ }
60
+
61
+ export function resolveResponsiveValue(
62
+ value: unknown,
63
+ breakpoint: string,
64
+ activeBreakpoints?: ActiveBreakpoints
65
+ ): unknown {
66
+ if ( value === null || value === undefined ) {
67
+ return null;
68
+ }
69
+
70
+ if ( typeof value !== 'object' || Array.isArray( value ) ) {
71
+ return value;
72
+ }
73
+
74
+ const map = value as Record< string, unknown >;
75
+
76
+ for ( const key of responsiveFallbackChain( breakpoint ) ) {
77
+ if ( ! isBreakpointActive( key, activeBreakpoints ) ) {
78
+ continue;
79
+ }
80
+
81
+ const resolved = unwrapResponsiveEntry( map[ key ] );
82
+
83
+ if ( resolved !== null && resolved !== undefined ) {
84
+ return resolved;
85
+ }
86
+ }
87
+
88
+ return unwrapResponsiveEntry( map.desktop );
89
+ }