@elementor/editor-controls 0.9.0 → 0.10.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/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": "0.9.0",
4
+ "version": "0.10.0",
5
5
  "private": false,
6
6
  "author": "Elementor Team",
7
7
  "homepage": "https://elementor.com/",
@@ -40,7 +40,7 @@
40
40
  "dev": "tsup --config=../../tsup.dev.ts"
41
41
  },
42
42
  "dependencies": {
43
- "@elementor/editor-props": "0.9.0",
43
+ "@elementor/editor-props": "0.9.1",
44
44
  "@elementor/env": "0.3.5",
45
45
  "@elementor/http": "0.1.3",
46
46
  "@elementor/icons": "1.31.0",
@@ -6,7 +6,7 @@ import {
6
6
  backgroundOverlayPropTypeUtil,
7
7
  type PropKey,
8
8
  } from '@elementor/editor-props';
9
- import { Box, CardMedia, Grid, Stack, Tab, TabPanel, Tabs, UnstableColorIndicator, useTabs } from '@elementor/ui';
9
+ import { Box, CardMedia, Grid, Stack, Tab, TabPanel, Tabs, UnstableColorIndicator } from '@elementor/ui';
10
10
  import { useWpMediaAttachment } from '@elementor/wp-media';
11
11
  import { __ } from '@wordpress/i18n';
12
12
 
@@ -20,8 +20,15 @@ import { BackgroundImageOverlayAttachment } from './background-image-overlay/bac
20
20
  import { BackgroundImageOverlayPosition } from './background-image-overlay/background-image-overlay-position';
21
21
  import { BackgroundImageOverlayRepeat } from './background-image-overlay/background-image-overlay-repeat';
22
22
  import { BackgroundImageOverlaySize } from './background-image-overlay/background-image-overlay-size';
23
+ import { type BackgroundImageOverlay } from './types';
24
+ import { useBackgroundTabsHistory } from './use-background-tabs-history';
23
25
 
24
- const initialBackgroundOverlay = ( backgroundPlaceholderImage: string ): BackgroundOverlayItemPropValue => ( {
26
+ export const initialBackgroundColorOverlay: BackgroundOverlayItemPropValue = {
27
+ $$type: 'background-color-overlay',
28
+ value: '#00000033',
29
+ };
30
+
31
+ export const getInitialBackgroundOverlay = (): BackgroundOverlayItemPropValue => ( {
25
32
  $$type: 'background-image-overlay',
26
33
  value: {
27
34
  image: {
@@ -32,7 +39,7 @@ const initialBackgroundOverlay = ( backgroundPlaceholderImage: string ): Backgro
32
39
  value: {
33
40
  url: {
34
41
  $$type: 'url',
35
- value: backgroundPlaceholderImage,
42
+ value: env.background_placeholder_image,
36
43
  },
37
44
  id: null,
38
45
  },
@@ -53,31 +60,6 @@ const backgroundResolutionOptions = [
53
60
  { label: __( 'Full', 'elementor' ), value: 'full' },
54
61
  ];
55
62
 
56
- type OverlayType = 'image' | 'color';
57
-
58
- type ImageSrcAttachment = { id: { $$type: 'image-attachment-id'; value: number }; url: null };
59
-
60
- type ImageSrcUrl = { url: { $$type: 'url'; value: string }; id: null };
61
-
62
- type BackgroundImageOverlay = {
63
- $$type: 'background-image-overlay';
64
- value: {
65
- image: {
66
- $$type: 'image';
67
- value: {
68
- src: {
69
- $$type: 'image-src';
70
- value: ImageSrcAttachment | ImageSrcUrl;
71
- };
72
- size: {
73
- $$type: 'string';
74
- value: 'thumbnail' | 'medium' | 'large' | 'full';
75
- };
76
- };
77
- };
78
- };
79
- };
80
-
81
63
  export const BackgroundOverlayRepeaterControl = createControl( () => {
82
64
  const { propType, value: overlayValues, setValue } = useBoundProp( backgroundOverlayPropTypeUtil );
83
65
 
@@ -91,24 +73,26 @@ export const BackgroundOverlayRepeaterControl = createControl( () => {
91
73
  Icon: ItemIcon,
92
74
  Label: ItemLabel,
93
75
  Content: ItemContent,
94
- initialValues: initialBackgroundOverlay( env.background_placeholder_image ),
76
+ initialValues: getInitialBackgroundOverlay(),
95
77
  } }
96
78
  />
97
79
  </PropProvider>
98
80
  );
99
81
  } );
100
82
 
101
- const ItemContent = ( { bind, value }: { bind: PropKey; value: BackgroundOverlayItemPropValue } ) => {
83
+ export const ItemContent = ( { bind }: { bind: PropKey } ) => {
102
84
  return (
103
85
  <PropKeyProvider bind={ bind }>
104
- <Content value={ value } />
86
+ <Content />
105
87
  </PropKeyProvider>
106
88
  );
107
89
  };
108
90
 
109
- const Content = ( { value }: { value: BackgroundOverlayItemPropValue } ) => {
110
- const activeTab = deriveOverlayType( value.$$type );
111
- const { getTabsProps, getTabProps, getTabPanelProps } = useTabs< OverlayType >( activeTab );
91
+ const Content = () => {
92
+ const { getTabsProps, getTabProps, getTabPanelProps } = useBackgroundTabsHistory( {
93
+ image: getInitialBackgroundOverlay().value,
94
+ color: initialBackgroundColorOverlay.value,
95
+ } );
112
96
 
113
97
  return (
114
98
  <Box sx={ { width: '100%' } }>
@@ -207,18 +191,6 @@ const ImageOverlayContent = () => {
207
191
  );
208
192
  };
209
193
 
210
- const deriveOverlayType = ( type: string ): OverlayType => {
211
- if ( type === 'background-color-overlay' ) {
212
- return 'color';
213
- }
214
-
215
- if ( type === 'background-image-overlay' ) {
216
- return 'image';
217
- }
218
-
219
- throw new Error( `Invalid overlay type: ${ type }` );
220
- };
221
-
222
194
  const useImage = ( image: BackgroundImageOverlay ) => {
223
195
  let imageTitle,
224
196
  imageUrl: string | null = null;
@@ -0,0 +1,22 @@
1
+ type ImageSrcAttachment = { id: { $$type: 'image-attachment-id'; value: number }; url: null };
2
+
3
+ type ImageSrcUrl = { url: { $$type: 'url'; value: string }; id: null };
4
+
5
+ export type BackgroundImageOverlay = {
6
+ $$type: 'background-image-overlay';
7
+ value: {
8
+ image: {
9
+ $$type: 'image';
10
+ value: {
11
+ src: {
12
+ $$type: 'image-src';
13
+ value: ImageSrcAttachment | ImageSrcUrl;
14
+ };
15
+ size: {
16
+ $$type: 'string';
17
+ value: 'thumbnail' | 'medium' | 'large' | 'full';
18
+ };
19
+ };
20
+ };
21
+ };
22
+ };
@@ -0,0 +1,62 @@
1
+ import { useRef } from 'react';
2
+ import {
3
+ backgroundColorOverlayPropTypeUtil,
4
+ backgroundImageOverlayPropTypeUtil,
5
+ type BackgroundOverlayItemPropValue,
6
+ } from '@elementor/editor-props';
7
+ import { useTabs } from '@elementor/ui';
8
+
9
+ import { useBoundProp } from '../../../bound-prop-context';
10
+ import { type BackgroundImageOverlay } from './types';
11
+
12
+ type OverlayType = 'image' | 'color';
13
+
14
+ type InitialBackgroundValues = {
15
+ color: BackgroundOverlayItemPropValue[ 'value' ];
16
+ image: BackgroundImageOverlay[ 'value' ];
17
+ };
18
+
19
+ export const useBackgroundTabsHistory = ( {
20
+ color: initialBackgroundColorOverlay,
21
+ image: initialBackgroundImageOverlay,
22
+ }: InitialBackgroundValues ) => {
23
+ const { value: imageValue, setValue: setImageValue } = useBoundProp( backgroundImageOverlayPropTypeUtil );
24
+ const { value: colorValue, setValue: setColorValue } = useBoundProp( backgroundColorOverlayPropTypeUtil );
25
+
26
+ const { getTabsProps, getTabProps, getTabPanelProps } = useTabs< OverlayType >( colorValue ? 'color' : 'image' );
27
+
28
+ const valuesHistory = useRef< InitialBackgroundValues >( {
29
+ image: initialBackgroundImageOverlay,
30
+ color: initialBackgroundColorOverlay,
31
+ } );
32
+
33
+ const saveToHistory = ( key: keyof InitialBackgroundValues, value: BackgroundOverlayItemPropValue[ 'value' ] ) => {
34
+ if ( value ) {
35
+ valuesHistory.current[ key ] = value;
36
+ }
37
+ };
38
+
39
+ const onTabChange = ( e: React.SyntheticEvent, tabName: OverlayType ) => {
40
+ switch ( tabName ) {
41
+ case 'image':
42
+ setImageValue( valuesHistory.current.image );
43
+
44
+ saveToHistory( 'color', colorValue );
45
+
46
+ break;
47
+
48
+ case 'color':
49
+ setColorValue( valuesHistory.current.color );
50
+
51
+ saveToHistory( 'image', imageValue );
52
+ }
53
+
54
+ return getTabsProps().onChange( e, tabName );
55
+ };
56
+
57
+ return {
58
+ getTabProps,
59
+ getTabPanelProps,
60
+ getTabsProps: () => ( { ...getTabsProps(), onChange: onTabChange } ),
61
+ };
62
+ };