@elementor/editor-interactions 4.1.0-742 → 4.1.0-744

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,6 +1,6 @@
1
1
  {
2
2
  "name": "@elementor/editor-interactions",
3
- "version": "4.1.0-742",
3
+ "version": "4.1.0-744",
4
4
  "private": false,
5
5
  "author": "Elementor Team",
6
6
  "homepage": "https://elementor.com/",
@@ -39,19 +39,19 @@
39
39
  "dev": "tsup --config=../../tsup.dev.ts"
40
40
  },
41
41
  "dependencies": {
42
- "@elementor/editor-controls": "4.1.0-742",
43
- "@elementor/editor-elements": "4.1.0-742",
44
- "@elementor/editor-mcp": "4.1.0-742",
45
- "@elementor/editor-props": "4.1.0-742",
46
- "@elementor/editor-responsive": "4.1.0-742",
47
- "@elementor/editor-ui": "4.1.0-742",
48
- "@elementor/editor-v1-adapters": "4.1.0-742",
42
+ "@elementor/editor-controls": "4.1.0-744",
43
+ "@elementor/editor-elements": "4.1.0-744",
44
+ "@elementor/editor-mcp": "4.1.0-744",
45
+ "@elementor/editor-props": "4.1.0-744",
46
+ "@elementor/editor-responsive": "4.1.0-744",
47
+ "@elementor/editor-ui": "4.1.0-744",
48
+ "@elementor/editor-v1-adapters": "4.1.0-744",
49
49
  "@elementor/icons": "^1.68.0",
50
- "@elementor/schema": "4.1.0-742",
51
- "@elementor/session": "4.1.0-742",
50
+ "@elementor/schema": "4.1.0-744",
51
+ "@elementor/session": "4.1.0-744",
52
52
  "@elementor/ui": "1.36.17",
53
- "@elementor/utils": "4.1.0-742",
54
- "@elementor/events": "4.1.0-742",
53
+ "@elementor/utils": "4.1.0-744",
54
+ "@elementor/events": "4.1.0-744",
55
55
  "@wordpress/i18n": "^5.13.0"
56
56
  },
57
57
  "peerDependencies": {
@@ -1,10 +1,14 @@
1
1
  import * as React from 'react';
2
- import { createContext, type ReactNode, useContext, useEffect } from 'react';
2
+ import { createContext, type ReactNode, useContext, useEffect, useMemo } from 'react';
3
3
  import {
4
4
  type ElementInteractions,
5
+ getElementInteractions,
6
+ getElementLabel,
5
7
  playElementInteractions,
6
8
  updateElementInteractions,
7
9
  } from '@elementor/editor-elements';
10
+ import { undoable } from '@elementor/editor-v1-adapters';
11
+ import { __ } from '@wordpress/i18n';
8
12
 
9
13
  import { useElementInteractions } from '../hooks/use-element-interactions';
10
14
 
@@ -15,6 +19,11 @@ type InteractionsContextValue = {
15
19
  playInteractions: ( interactionId: string ) => void;
16
20
  };
17
21
 
22
+ type UndoablePayload = {
23
+ interactions: ElementInteractions | undefined;
24
+ operationType: 'apply' | 'delete';
25
+ };
26
+
18
27
  const InteractionsContext = createContext< InteractionsContextValue | null >( null );
19
28
 
20
29
  const DEFAULT_INTERACTIONS: ElementInteractions = {
@@ -32,13 +41,47 @@ export const InteractionsProvider = ( { children, elementId }: { children: React
32
41
  const interactions: ElementInteractions =
33
42
  ( rawInteractions as unknown as ElementInteractions ) ?? DEFAULT_INTERACTIONS;
34
43
 
44
+ const undoableSetInteractions = useMemo(
45
+ () =>
46
+ undoable(
47
+ {
48
+ do: ( { interactions: newInteractions }: UndoablePayload ) => {
49
+ const previous = getElementInteractions( elementId );
50
+
51
+ updateElementInteractions( { elementId, interactions: newInteractions } );
52
+
53
+ return previous;
54
+ },
55
+ undo: ( _: UndoablePayload, previous ) => {
56
+ updateElementInteractions( {
57
+ elementId,
58
+ interactions: previous?.items?.length ? previous : undefined,
59
+ } );
60
+ },
61
+ },
62
+ {
63
+ title: getElementLabel( elementId ),
64
+ subtitle: ( { operationType }: UndoablePayload ) =>
65
+ operationType === 'apply'
66
+ ? __( 'Interaction Applied', 'elementor' )
67
+ : __( 'Interaction Deleted', 'elementor' ),
68
+ }
69
+ ),
70
+ [ elementId ]
71
+ );
72
+
35
73
  const setInteractions = ( value: ElementInteractions | undefined ) => {
36
74
  const normalizedValue = value && value.items?.length === 0 ? undefined : value;
75
+ const prevItemCount = interactions.items?.length ?? 0;
76
+ const newItemCount = normalizedValue?.items?.length ?? 0;
37
77
 
38
- updateElementInteractions( {
39
- elementId,
40
- interactions: normalizedValue,
41
- } );
78
+ if ( newItemCount > prevItemCount ) {
79
+ undoableSetInteractions( { interactions: normalizedValue, operationType: 'apply' } );
80
+ } else if ( newItemCount < prevItemCount ) {
81
+ undoableSetInteractions( { interactions: normalizedValue, operationType: 'delete' } );
82
+ } else {
83
+ updateElementInteractions( { elementId, interactions: normalizedValue } );
84
+ }
42
85
  };
43
86
 
44
87
  const playInteractions = ( interactionId: string ) => {