@elementor/editor-components 4.0.0-662 → 4.0.0-664
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/dist/index.d.mts +9 -4
- package/dist/index.d.ts +9 -4
- package/dist/index.js +1267 -843
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1026 -603
- package/dist/index.mjs.map +1 -1
- package/package.json +23 -23
- package/src/components/detach-instance-confirmation-dialog.tsx +50 -0
- package/src/components/instance-editing-panel/detach-action.tsx +76 -0
- package/src/components/instance-editing-panel/instance-editing-panel.tsx +12 -5
- package/src/components/instance-editing-panel/use-instance-panel-data.ts +2 -2
- package/src/consts.ts +1 -0
- package/src/create-component-type.ts +87 -23
- package/src/index.ts +1 -0
- package/src/init.ts +6 -1
- package/src/types.ts +1 -1
- package/src/utils/detach-component-instance/detach-component-instance.ts +172 -0
- package/src/utils/detach-component-instance/index.ts +1 -0
- package/src/utils/detach-component-instance/regenerate-local-style-ids.ts +53 -0
- package/src/utils/detach-component-instance/resolve-detached-instance.ts +94 -0
- package/src/utils/detach-component-instance/resolve-overridable-settings.ts +121 -0
- package/src/utils/is-component-instance.ts +1 -1
- package/src/utils/tracking.ts +2 -1
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { generateElementId, type V1ElementData, type V1ElementSettingsProps } from '@elementor/editor-elements';
|
|
2
|
+
import { classesPropTypeUtil, type ClassesPropValue, type PropValue } from '@elementor/editor-props';
|
|
3
|
+
import { type StyleDefinition, type StyleDefinitionID } from '@elementor/editor-styles';
|
|
4
|
+
|
|
5
|
+
// Ts version for atomic-widgets/assets/js/editor/utils/regenerate-local-style-ids.js
|
|
6
|
+
export function regenerateLocalStyleIds( element: V1ElementData ): {
|
|
7
|
+
styles: Record< StyleDefinitionID, StyleDefinition > | undefined;
|
|
8
|
+
settings: V1ElementSettingsProps | undefined;
|
|
9
|
+
} {
|
|
10
|
+
const originalStyles = element.styles;
|
|
11
|
+
|
|
12
|
+
if ( ! originalStyles || Object.keys( originalStyles ).length === 0 ) {
|
|
13
|
+
return { styles: undefined, settings: undefined };
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const newStyles: Record< string, StyleDefinition > = {};
|
|
17
|
+
const styleIdMapping: Record< string, string > = {};
|
|
18
|
+
|
|
19
|
+
for ( const [ originalStyleId, style ] of Object.entries( originalStyles ) ) {
|
|
20
|
+
const newStyleId = generateLocalStyleId( element.id );
|
|
21
|
+
|
|
22
|
+
newStyles[ newStyleId ] = { ...style, id: newStyleId };
|
|
23
|
+
styleIdMapping[ originalStyleId ] = newStyleId;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const settings = element.settings;
|
|
27
|
+
if ( ! settings || Object.keys( settings ).length === 0 ) {
|
|
28
|
+
return { styles: newStyles, settings: undefined };
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const updatedSettings = { ...settings };
|
|
32
|
+
|
|
33
|
+
for ( const [ propKey, propValue ] of Object.entries( updatedSettings ) ) {
|
|
34
|
+
if ( isClassesProp( propValue ) && propValue.value.length > 0 ) {
|
|
35
|
+
const updatedClasses = propValue.value.map( ( classId ) => styleIdMapping[ classId ] ?? classId );
|
|
36
|
+
|
|
37
|
+
updatedSettings[ propKey ] = classesPropTypeUtil.create( updatedClasses );
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return {
|
|
42
|
+
styles: newStyles,
|
|
43
|
+
settings: updatedSettings,
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function isClassesProp( prop: PropValue ): prop is ClassesPropValue {
|
|
48
|
+
return classesPropTypeUtil.isValid( prop );
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function generateLocalStyleId( elementId: string ): string {
|
|
52
|
+
return `e-${ elementId }-${ generateElementId() }`;
|
|
53
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { generateElementId, type V1ElementData } from '@elementor/editor-elements';
|
|
2
|
+
import { type PropValue } from '@elementor/editor-props';
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
type ComponentInstanceOverrideProp,
|
|
6
|
+
componentInstanceOverridePropTypeUtil,
|
|
7
|
+
} from '../../prop-types/component-instance-override-prop-type';
|
|
8
|
+
import { type ComponentInstanceOverride } from '../../prop-types/component-instance-overrides-prop-type';
|
|
9
|
+
import { componentOverridablePropTypeUtil } from '../../prop-types/component-overridable-prop-type';
|
|
10
|
+
import { regenerateLocalStyleIds } from './regenerate-local-style-ids';
|
|
11
|
+
import { resolveOverridableSettings } from './resolve-overridable-settings';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Creates detached element from component instance
|
|
15
|
+
* by applying overrides, reverting overridables, and regenerating IDs.
|
|
16
|
+
* This is used when detaching a component instance from its origin component.
|
|
17
|
+
*
|
|
18
|
+
* The function goes through all nested elements recursively and:
|
|
19
|
+
* - Regenerates element IDs and local style IDs
|
|
20
|
+
* - Resolves overridable settings - applies all instance overrides to element settings,
|
|
21
|
+
* or replaces overridable with origin_value when no matching override exists.
|
|
22
|
+
*
|
|
23
|
+
* @param element - The component's root element
|
|
24
|
+
* @param overrides - Array of overrides from the component instance
|
|
25
|
+
* @return A new element data with all overrides applied and new IDs
|
|
26
|
+
*/
|
|
27
|
+
export function resolveDetachedInstance(
|
|
28
|
+
element: V1ElementData,
|
|
29
|
+
overrides: ComponentInstanceOverride[]
|
|
30
|
+
): V1ElementData {
|
|
31
|
+
const overrideMap = createOverrideMap( overrides );
|
|
32
|
+
|
|
33
|
+
return resolveElementRecursive( structuredClone( element ), overrideMap );
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function resolveElementRecursive(
|
|
37
|
+
element: V1ElementData,
|
|
38
|
+
overrideMap: Map< string, ComponentInstanceOverrideProp >
|
|
39
|
+
): V1ElementData {
|
|
40
|
+
element.id = generateElementId();
|
|
41
|
+
|
|
42
|
+
if ( element.styles ) {
|
|
43
|
+
const { styles, settings } = regenerateLocalStyleIds( element );
|
|
44
|
+
|
|
45
|
+
element.styles = styles;
|
|
46
|
+
if ( settings ) {
|
|
47
|
+
element.settings = { ...element.settings, ...settings };
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if ( element.settings ) {
|
|
52
|
+
element.settings = resolveOverridableSettings( element, overrideMap );
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
if ( element.elements?.length ) {
|
|
56
|
+
element.elements = element.elements.map( ( child ) => resolveElementRecursive( child, overrideMap ) );
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return element;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function createOverrideMap( overrides: ComponentInstanceOverride[] ): Map< string, ComponentInstanceOverrideProp > {
|
|
63
|
+
const map = new Map< string, ComponentInstanceOverrideProp >();
|
|
64
|
+
|
|
65
|
+
overrides.forEach( ( item ) => {
|
|
66
|
+
let override: ComponentInstanceOverrideProp | null = null;
|
|
67
|
+
|
|
68
|
+
if ( componentInstanceOverridePropTypeUtil.isValid( item ) ) {
|
|
69
|
+
override = item;
|
|
70
|
+
} else if ( componentOverridablePropTypeUtil.isValid( item ) ) {
|
|
71
|
+
override = getOverridableOverride( item );
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
if ( override ) {
|
|
75
|
+
const overrideKey = override.value.override_key;
|
|
76
|
+
map.set( overrideKey, override );
|
|
77
|
+
}
|
|
78
|
+
} );
|
|
79
|
+
|
|
80
|
+
return map;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export function getOverridableOverride( propValue: PropValue ): ComponentInstanceOverrideProp | null {
|
|
84
|
+
if ( ! componentOverridablePropTypeUtil.isValid( propValue ) ) {
|
|
85
|
+
return null;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const originValue = componentOverridablePropTypeUtil.extract( propValue )?.origin_value;
|
|
89
|
+
if ( ! componentInstanceOverridePropTypeUtil.isValid( originValue ) ) {
|
|
90
|
+
return null;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
return originValue;
|
|
94
|
+
}
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { type V1ElementData, type V1ElementSettingsProps } from '@elementor/editor-elements';
|
|
2
|
+
import { type PropValue } from '@elementor/editor-props';
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
type ComponentInstanceOverrideProp,
|
|
6
|
+
componentInstanceOverridePropTypeUtil,
|
|
7
|
+
} from '../../prop-types/component-instance-override-prop-type';
|
|
8
|
+
import {
|
|
9
|
+
type ComponentInstanceProp,
|
|
10
|
+
componentInstancePropTypeUtil,
|
|
11
|
+
} from '../../prop-types/component-instance-prop-type';
|
|
12
|
+
import { componentOverridablePropTypeUtil } from '../../prop-types/component-overridable-prop-type';
|
|
13
|
+
import { isComponentInstance } from '../is-component-instance';
|
|
14
|
+
|
|
15
|
+
export function resolveOverridableSettings(
|
|
16
|
+
element: V1ElementData,
|
|
17
|
+
overrideMap: Map< string, ComponentInstanceOverrideProp >
|
|
18
|
+
): V1ElementSettingsProps {
|
|
19
|
+
if ( isComponentInstance( { widgetType: element.widgetType, elType: element.elType } ) ) {
|
|
20
|
+
return resolveOverridableSettingsForComponentInstance( element, overrideMap );
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
return resolveOverridableSettingsForElement( element, overrideMap );
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function resolveOverridableSettingsForElement(
|
|
27
|
+
element: V1ElementData,
|
|
28
|
+
overrideMap: Map< string, ComponentInstanceOverrideProp >
|
|
29
|
+
): V1ElementSettingsProps {
|
|
30
|
+
const updatedSettings = element.settings ? { ...element.settings } : {};
|
|
31
|
+
|
|
32
|
+
for ( const [ settingKey, settingValue ] of Object.entries( element.settings ?? {} ) ) {
|
|
33
|
+
updatedSettings[ settingKey ] = resolvePropValue( settingValue, overrideMap );
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return updatedSettings;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function resolveOverridableSettingsForComponentInstance(
|
|
40
|
+
element: V1ElementData,
|
|
41
|
+
overrideMap: Map< string, ComponentInstanceOverrideProp >
|
|
42
|
+
): V1ElementSettingsProps {
|
|
43
|
+
const componentInstance = element.settings?.component_instance as ComponentInstanceProp | undefined;
|
|
44
|
+
|
|
45
|
+
if ( ! componentInstancePropTypeUtil.isValid( componentInstance ) ) {
|
|
46
|
+
return element.settings ?? {};
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const instanceOverrides = componentInstance.value.overrides?.value;
|
|
50
|
+
|
|
51
|
+
if ( ! instanceOverrides?.length ) {
|
|
52
|
+
return element.settings ?? {};
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const updatedOverrides = instanceOverrides.map( ( item ) =>
|
|
56
|
+
resolvePropValue( item, overrideMap, { isOverridableOverride: true } )
|
|
57
|
+
);
|
|
58
|
+
|
|
59
|
+
return {
|
|
60
|
+
...element.settings,
|
|
61
|
+
component_instance: {
|
|
62
|
+
...componentInstance,
|
|
63
|
+
value: {
|
|
64
|
+
...componentInstance.value,
|
|
65
|
+
overrides: {
|
|
66
|
+
...componentInstance.value.overrides,
|
|
67
|
+
value: updatedOverrides,
|
|
68
|
+
},
|
|
69
|
+
},
|
|
70
|
+
},
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function resolvePropValue(
|
|
75
|
+
propValue: PropValue,
|
|
76
|
+
overrideMap: Map< string, ComponentInstanceOverrideProp >,
|
|
77
|
+
options?: { isOverridableOverride?: boolean }
|
|
78
|
+
): PropValue | null {
|
|
79
|
+
const { isOverridableOverride = false } = options ?? {};
|
|
80
|
+
|
|
81
|
+
// if it's not an overridable, return the prop value as is
|
|
82
|
+
if ( ! componentOverridablePropTypeUtil.isValid( propValue ) ) {
|
|
83
|
+
return propValue;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const overridableKey = propValue.value.override_key;
|
|
87
|
+
const matchingOverride = overrideMap.get( overridableKey );
|
|
88
|
+
|
|
89
|
+
const originValue = componentOverridablePropTypeUtil.extract( propValue )?.origin_value;
|
|
90
|
+
|
|
91
|
+
// if no matching override, return the overridable's origin value
|
|
92
|
+
if ( ! matchingOverride ) {
|
|
93
|
+
return originValue;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
if ( isOverridableOverride ) {
|
|
97
|
+
return resolveOverridableOverride( matchingOverride, originValue );
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// for regular props, when there's a matching override, return the matching override value
|
|
101
|
+
const matchingOverrideValue = componentInstanceOverridePropTypeUtil.extract( matchingOverride )
|
|
102
|
+
?.override_value as PropValue | null;
|
|
103
|
+
return matchingOverrideValue;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function resolveOverridableOverride(
|
|
107
|
+
matchingOverride: ComponentInstanceOverrideProp,
|
|
108
|
+
originValue: PropValue
|
|
109
|
+
): ComponentInstanceOverrideProp | null {
|
|
110
|
+
if ( ! originValue || ! componentInstanceOverridePropTypeUtil.isValid( originValue ) ) {
|
|
111
|
+
return null;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// for overridable overrides, we should create a new override with the matching override value
|
|
115
|
+
// but keep the origin value's override key and schema source, so they'll match the inner component.
|
|
116
|
+
return componentInstanceOverridePropTypeUtil.create( {
|
|
117
|
+
override_value: matchingOverride.value.override_value,
|
|
118
|
+
override_key: originValue.value.override_key,
|
|
119
|
+
schema_source: originValue.value.schema_source,
|
|
120
|
+
} );
|
|
121
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { type V1ElementModelProps } from '@elementor/editor-elements';
|
|
2
2
|
|
|
3
|
-
import { COMPONENT_WIDGET_TYPE } from '../
|
|
3
|
+
import { COMPONENT_WIDGET_TYPE } from '../consts';
|
|
4
4
|
|
|
5
5
|
export function isComponentInstance( elementModel: Partial< V1ElementModelProps > ) {
|
|
6
6
|
return [ elementModel.widgetType, elementModel.elType ].includes( COMPONENT_WIDGET_TYPE );
|
package/src/utils/tracking.ts
CHANGED