@elementor/editor-interactions 3.35.0-453 → 3.35.0-454

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,44 +1,52 @@
1
1
  import * as React from 'react';
2
- import { useEffect, useMemo, useState } from 'react';
2
+ import { useCallback, useEffect, useMemo, useRef } from 'react';
3
3
  import { Repeater } from '@elementor/editor-controls';
4
4
  import { InfoCircleFilledIcon, PlayerPlayIcon } from '@elementor/icons';
5
5
  import { Alert, AlertTitle, Box, IconButton, Tooltip } from '@elementor/ui';
6
6
  import { __ } from '@wordpress/i18n';
7
7
 
8
8
  import type { ElementInteractions, InteractionItemPropValue, InteractionItemValue } from '../types';
9
- import { buildAnimationIdString, buildDisplayLabel, createDefaultInteractionItem } from '../utils/prop-value-utils';
9
+ import { buildDisplayLabel, createDefaultInteractionItem, extractString } from '../utils/prop-value-utils';
10
10
  import { InteractionDetails } from './interaction-details';
11
-
12
11
  export const MAX_NUMBER_OF_INTERACTIONS = 5;
13
12
 
14
13
  export type InteractionListProps = {
15
14
  onSelectInteractions: ( interactions: ElementInteractions ) => void;
16
15
  interactions: ElementInteractions;
17
- onPlayInteraction: ( animationId: string ) => void;
16
+ onPlayInteraction: ( interactionId: string ) => void;
18
17
  triggerCreateOnShowEmpty?: boolean;
19
18
  };
20
19
 
21
20
  export function InteractionsList( props: InteractionListProps ) {
22
21
  const { interactions, onSelectInteractions, onPlayInteraction, triggerCreateOnShowEmpty } = props;
23
22
 
24
- const [ interactionsState, setInteractionsState ] = useState< ElementInteractions >( interactions );
23
+ const hasInitializedRef = useRef( false );
24
+
25
+ const handleUpdateInteractions = useCallback(
26
+ ( newInteractions: ElementInteractions ) => {
27
+ onSelectInteractions( newInteractions );
28
+ },
29
+ [ onSelectInteractions ]
30
+ );
25
31
 
26
32
  useEffect( () => {
27
- if ( JSON.stringify( interactions ) !== JSON.stringify( interactionsState ) ) {
28
- onSelectInteractions( interactionsState );
33
+ if (
34
+ triggerCreateOnShowEmpty &&
35
+ ! hasInitializedRef.current &&
36
+ ( ! interactions.items || interactions.items?.length === 0 )
37
+ ) {
38
+ hasInitializedRef.current = true;
39
+ const newState: ElementInteractions = {
40
+ version: 1,
41
+ items: [ createDefaultInteractionItem() ],
42
+ };
43
+ handleUpdateInteractions( newState );
29
44
  }
30
- }, [ interactions, interactionsState, onSelectInteractions ] );
45
+ }, [ triggerCreateOnShowEmpty, interactions.items, handleUpdateInteractions ] );
31
46
 
32
47
  const isMaxNumberOfInteractionsReached = useMemo( () => {
33
- return interactionsState.items?.length >= MAX_NUMBER_OF_INTERACTIONS;
34
- }, [ interactionsState.items ] );
35
-
36
- if ( triggerCreateOnShowEmpty && ( ! interactionsState.items || interactionsState.items?.length === 0 ) ) {
37
- setInteractionsState( {
38
- version: 1,
39
- items: [ createDefaultInteractionItem() ],
40
- } );
41
- }
48
+ return interactions.items?.length >= MAX_NUMBER_OF_INTERACTIONS;
49
+ }, [ interactions.items?.length ] );
42
50
 
43
51
  const infotipContent = isMaxNumberOfInteractionsReached ? (
44
52
  <Alert color="secondary" icon={ <InfoCircleFilledIcon /> } size="small">
@@ -52,18 +60,38 @@ export function InteractionsList( props: InteractionListProps ) {
52
60
  </Alert>
53
61
  ) : undefined;
54
62
 
63
+ const handleRepeaterChange = useCallback(
64
+ ( newItems: ElementInteractions[ 'items' ] ) => {
65
+ handleUpdateInteractions( {
66
+ ...interactions,
67
+ items: newItems,
68
+ } );
69
+ },
70
+ [ interactions, handleUpdateInteractions ]
71
+ );
72
+
73
+ const handleInteractionChange = useCallback(
74
+ ( index: number, newInteractionValue: InteractionItemValue ) => {
75
+ const newItems = structuredClone( interactions.items );
76
+ newItems[ index ] = {
77
+ $$type: 'interaction-item',
78
+ value: newInteractionValue,
79
+ };
80
+ handleUpdateInteractions( {
81
+ ...interactions,
82
+ items: newItems,
83
+ } );
84
+ },
85
+ [ interactions, handleUpdateInteractions ]
86
+ );
87
+
55
88
  return (
56
89
  <Repeater
57
90
  openOnAdd
58
91
  openItem={ triggerCreateOnShowEmpty ? 0 : undefined }
59
92
  label={ __( 'Interactions', 'elementor' ) }
60
- values={ interactionsState.items }
61
- setValues={ ( newValue: ElementInteractions[ 'items' ] ) => {
62
- setInteractionsState( {
63
- ...interactionsState,
64
- items: newValue,
65
- } );
66
- } }
93
+ values={ interactions.items }
94
+ setValues={ handleRepeaterChange }
67
95
  showDuplicate={ false }
68
96
  showToggle={ false }
69
97
  isSortable={ false }
@@ -78,13 +106,9 @@ export function InteractionsList( props: InteractionListProps ) {
78
106
  key={ index }
79
107
  interaction={ value.value }
80
108
  onChange={ ( newInteractionValue: InteractionItemValue ) => {
81
- const newItems = structuredClone( interactionsState.items );
82
- newItems[ index ] = {
83
- $$type: 'interaction-item',
84
- value: newInteractionValue,
85
- };
86
- setInteractionsState( { ...interactionsState, items: newItems } );
109
+ handleInteractionChange( index, newInteractionValue );
87
110
  } }
111
+ onPlayInteraction={ onPlayInteraction }
88
112
  />
89
113
  ),
90
114
  actions: ( value: InteractionItemPropValue ) => (
@@ -92,7 +116,7 @@ export function InteractionsList( props: InteractionListProps ) {
92
116
  <IconButton
93
117
  aria-label={ __( 'Play interaction', 'elementor' ) }
94
118
  size="tiny"
95
- onClick={ () => onPlayInteraction( buildAnimationIdString( value.value ) ) }
119
+ onClick={ () => onPlayInteraction( extractString( value.value.interaction_id ) ) }
96
120
  >
97
121
  <PlayerPlayIcon fontSize="tiny" />
98
122
  </IconButton>
@@ -10,7 +10,7 @@ import {
10
10
  type InteractionsContextValue = {
11
11
  interactions: ElementInteractions;
12
12
  setInteractions: ( value: ElementInteractions | undefined ) => void;
13
- playInteractions: ( animationId: string ) => void;
13
+ playInteractions: ( interactionId: string ) => void;
14
14
  };
15
15
 
16
16
  const InteractionsContext = createContext< InteractionsContextValue | null >( null );
@@ -31,14 +31,16 @@ export const InteractionsProvider = ( { children, elementId }: { children: React
31
31
  ( rawInteractions as unknown as ElementInteractions ) ?? DEFAULT_INTERACTIONS;
32
32
 
33
33
  const setInteractions = ( value: ElementInteractions | undefined ) => {
34
+ const normalizedValue = value && value.items?.length === 0 ? undefined : value;
35
+
34
36
  updateElementInteractions( {
35
37
  elementId,
36
- interactions: value as unknown as ElementInteractions,
38
+ interactions: normalizedValue,
37
39
  } );
38
40
  };
39
41
 
40
- const playInteractions = ( animationId: string ) => {
41
- playElementInteractions( elementId, animationId );
42
+ const playInteractions = ( interactionId: string ) => {
43
+ playElementInteractions( elementId, interactionId );
42
44
  };
43
45
 
44
46
  const contextValue: InteractionsContextValue = {
@@ -2,6 +2,8 @@ import { type ElementID, getAllDescendants, getContainer, type V1Element } from
2
2
  import { registerDataHook } from '@elementor/editor-v1-adapters';
3
3
 
4
4
  import type { ElementInteractions } from '../types';
5
+ import { createString } from '../utils/prop-value-utils';
6
+ import { generateTempInteractionId } from '../utils/temp-id-utils';
5
7
 
6
8
  export function initCleanInteractionIdsOnDuplicate() {
7
9
  registerDataHook( 'after', 'document/elements/duplicate', ( _args, result: V1Element | V1Element[] ) => {
@@ -41,8 +43,8 @@ function cleanInteractionIds( elementId: ElementID ) {
41
43
  const updatedInteractions = structuredClone( interactions ) as ElementInteractions;
42
44
 
43
45
  updatedInteractions?.items?.forEach( ( interaction ) => {
44
- if ( interaction.$$type === 'interaction-item' && interaction.value?.interaction_id ) {
45
- delete interaction.value.interaction_id;
46
+ if ( interaction.$$type === 'interaction-item' && interaction.value ) {
47
+ interaction.value.interaction_id = createString( generateTempInteractionId() );
46
48
  }
47
49
  } );
48
50
 
@@ -9,6 +9,7 @@ import type {
9
9
  StringPropValue,
10
10
  TimingConfigPropValue,
11
11
  } from '../types';
12
+ import { generateTempInteractionId } from './temp-id-utils';
12
13
 
13
14
  export const createString = ( value: string ): StringPropValue => ( {
14
15
  $$type: 'string',
@@ -104,6 +105,7 @@ export const createDefaultInteractionItem = (): InteractionItemPropValue => {
104
105
  duration: 300,
105
106
  delay: 0,
106
107
  replay: false,
108
+ interactionId: generateTempInteractionId(),
107
109
  } );
108
110
  };
109
111
 
@@ -0,0 +1,10 @@
1
+ const TEMP_ID_PREFIX = 'temp-';
2
+ const TEMP_ID_REGEX = /^temp-[a-z0-9]+$/i;
3
+
4
+ export function generateTempInteractionId(): string {
5
+ return `${ TEMP_ID_PREFIX }${ Math.random().toString( 36 ).substring( 2, 11 ) }`;
6
+ }
7
+
8
+ export function isTempId( id: string | undefined ): boolean {
9
+ return !! id && TEMP_ID_REGEX.test( id );
10
+ }