@elementor/editor-variables 0.15.0 → 0.16.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.
@@ -1,32 +1,16 @@
1
1
  import { styleTransformersRegistry } from '@elementor/editor-canvas';
2
- import { injectIntoRepeaterItemIcon, injectIntoRepeaterItemLabel } from '@elementor/editor-controls';
3
2
  import { controlActionsMenu, registerControlReplacement } from '@elementor/editor-editing-panel';
4
- import { backgroundColorOverlayPropTypeUtil, type PropValue, shadowPropTypeUtil } from '@elementor/editor-props';
5
3
 
6
- import {
7
- BackgroundRepeaterColorIndicator,
8
- BackgroundRepeaterLabel,
9
- BoxShadowRepeaterColorIndicator,
10
- } from './components/variables-repeater-item-slot';
11
4
  import { ColorVariableControl } from './controls/color-variable-control';
12
5
  import { usePropColorVariableAction } from './hooks/use-prop-color-variable-action';
13
6
  import { colorVariablePropTypeUtil } from './prop-types/color-variable-prop-type';
7
+ import { registerRepeaterInjections } from './repeater-injections';
14
8
  import { variableTransformer } from './transformers/variable-transformer';
15
9
  import { hasAssignedColorVariable } from './utils';
16
10
 
17
11
  const { registerPopoverAction } = controlActionsMenu;
18
12
 
19
- const conditions = {
20
- backgroundOverlay: ( { value: prop }: { value: PropValue } ) => {
21
- return hasAssignedColorVariable( backgroundColorOverlayPropTypeUtil.extract( prop )?.color );
22
- },
23
-
24
- boxShadow: ( { value: prop }: { value: PropValue } ) => {
25
- return hasAssignedColorVariable( shadowPropTypeUtil.extract( prop )?.color );
26
- },
27
- };
28
-
29
- function registerControlsAndActions() {
13
+ export function initColorVariables() {
30
14
  registerControlReplacement( {
31
15
  component: ColorVariableControl,
32
16
  condition: ( { value } ) => hasAssignedColorVariable( value ),
@@ -36,37 +20,8 @@ function registerControlsAndActions() {
36
20
  id: 'color-variables',
37
21
  useProps: usePropColorVariableAction,
38
22
  } );
39
- }
40
-
41
- function registerRepeaterItemIcons() {
42
- injectIntoRepeaterItemIcon( {
43
- id: 'color-variables-background-icon',
44
- component: BackgroundRepeaterColorIndicator,
45
- condition: conditions.backgroundOverlay,
46
- } );
47
-
48
- injectIntoRepeaterItemIcon( {
49
- id: 'color-variables-icon',
50
- component: BoxShadowRepeaterColorIndicator,
51
- condition: conditions.boxShadow,
52
- } );
53
- }
54
23
 
55
- function registerRepeaterItemLabels() {
56
- injectIntoRepeaterItemLabel( {
57
- id: 'color-variables-label',
58
- component: BackgroundRepeaterLabel,
59
- condition: conditions.backgroundOverlay,
60
- } );
61
- }
62
-
63
- function registerStyleTransformers() {
64
24
  styleTransformersRegistry.register( colorVariablePropTypeUtil.key, variableTransformer );
65
- }
66
25
 
67
- export function initColorVariables() {
68
- registerControlsAndActions();
69
- registerRepeaterItemIcons();
70
- registerRepeaterItemLabels();
71
- registerStyleTransformers();
26
+ registerRepeaterInjections();
72
27
  }
@@ -0,0 +1,35 @@
1
+ import { injectIntoRepeaterItemIcon, injectIntoRepeaterItemLabel } from '@elementor/editor-controls';
2
+ import { backgroundColorOverlayPropTypeUtil, type PropValue, shadowPropTypeUtil } from '@elementor/editor-props';
3
+
4
+ import {
5
+ BackgroundRepeaterColorIndicator,
6
+ BackgroundRepeaterLabel,
7
+ BoxShadowRepeaterColorIndicator,
8
+ } from './components/variables-repeater-item-slot';
9
+ import { hasAssignedColorVariable } from './utils';
10
+
11
+ export function registerRepeaterInjections() {
12
+ injectIntoRepeaterItemIcon( {
13
+ id: 'color-variables-background-icon',
14
+ component: BackgroundRepeaterColorIndicator,
15
+ condition: ( { value: prop }: { value: PropValue } ) => {
16
+ return hasAssignedColorVariable( backgroundColorOverlayPropTypeUtil.extract( prop )?.color );
17
+ },
18
+ } );
19
+
20
+ injectIntoRepeaterItemIcon( {
21
+ id: 'color-variables-icon',
22
+ component: BoxShadowRepeaterColorIndicator,
23
+ condition: ( { value: prop }: { value: PropValue } ) => {
24
+ return hasAssignedColorVariable( shadowPropTypeUtil.extract( prop )?.color );
25
+ },
26
+ } );
27
+
28
+ injectIntoRepeaterItemLabel( {
29
+ id: 'color-variables-label',
30
+ component: BackgroundRepeaterLabel,
31
+ condition: ( { value: prop }: { value: PropValue } ) => {
32
+ return hasAssignedColorVariable( backgroundColorOverlayPropTypeUtil.extract( prop )?.color );
33
+ },
34
+ } );
35
+ }
@@ -0,0 +1,42 @@
1
+ import { __ } from '@wordpress/i18n';
2
+
3
+ export const VARIABLE_LABEL_MAX_LENGTH = 50;
4
+
5
+ export const validateLabel = ( name: string ): string => {
6
+ if ( ! name.trim() ) {
7
+ return __( 'Missing variable name.', 'elementor' );
8
+ }
9
+
10
+ const allowedChars = /^[a-zA-Z0-9_-]+$/;
11
+ if ( ! allowedChars.test( name ) ) {
12
+ return __( 'Names can only use letters, numbers, dashes (-) and underscores (_).', 'elementor' );
13
+ }
14
+
15
+ const hasAlphanumeric = /[a-zA-Z0-9]/;
16
+ if ( ! hasAlphanumeric.test( name ) ) {
17
+ return __( 'Names have to include at least one non-special character.', 'elementor' );
18
+ }
19
+
20
+ if ( VARIABLE_LABEL_MAX_LENGTH < name.length ) {
21
+ return __( 'Variable names can contain up to 50 characters.', 'elementor' );
22
+ }
23
+
24
+ return '';
25
+ };
26
+
27
+ export const labelHint = ( name: string ): string => {
28
+ const hintThreshold = VARIABLE_LABEL_MAX_LENGTH * 0.8 - 1;
29
+ if ( hintThreshold < name.length ) {
30
+ return __( 'Variable names can contain up to 50 characters.', 'elementor' );
31
+ }
32
+
33
+ return '';
34
+ };
35
+
36
+ export const validateValue = ( value: string ): string => {
37
+ if ( ! value.trim() ) {
38
+ return __( 'Missing variable value.', 'elementor' );
39
+ }
40
+
41
+ return '';
42
+ };