@elementor/editor-elements 0.5.0 → 0.5.2

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,14 +1,15 @@
1
1
  import { classesPropTypeUtil, type ClassesPropValue } from '@elementor/editor-props';
2
2
  import { type StyleDefinition, type StyleDefinitionsMap } from '@elementor/editor-styles';
3
+ import { __privateRunCommandSync as runCommandSync } from '@elementor/editor-v1-adapters';
3
4
 
5
+ import { ElementNotFoundError } from '../errors';
4
6
  import getContainer from '../sync/get-container';
5
7
  import { type V1Element } from '../sync/types';
6
8
  import { updateElementSettings } from '../sync/update-element-settings';
7
9
  import { type ElementID } from '../types';
8
10
  import { ELEMENT_STYLE_CHANGE_EVENT } from './consts';
9
- import { ElementNotFoundError } from './errors';
10
11
 
11
- export type Mutator = ( styles: StyleDefinitionsMap ) => StyleDefinitionsMap;
12
+ type Mutator = ( styles: StyleDefinitionsMap ) => StyleDefinitionsMap;
12
13
 
13
14
  export function mutateElementStyles( elementId: ElementID, mutator: Mutator ) {
14
15
  const container = getContainer( elementId );
@@ -17,11 +18,18 @@ export function mutateElementStyles( elementId: ElementID, mutator: Mutator ) {
17
18
  throw new ElementNotFoundError( { context: { elementId } } );
18
19
  }
19
20
 
21
+ const oldIds = Object.keys( container.model.get( 'styles' ) ?? {} );
22
+
20
23
  const styles = mutateStyles( container, mutator );
21
24
 
22
- removeNonExistingClasses( container, styles );
25
+ const newIds = Object.keys( styles );
23
26
 
24
- dispatchChangeEvent();
27
+ clearRemovedClasses( container, {
28
+ oldIds,
29
+ newIds,
30
+ } );
31
+
32
+ notifyChanges();
25
33
 
26
34
  return styles;
27
35
  }
@@ -54,12 +62,12 @@ function isStyleEmpty( style: StyleDefinition ) {
54
62
  return style.variants.length === 0;
55
63
  }
56
64
 
57
- function removeNonExistingClasses( container: V1Element, styles: StyleDefinitionsMap ) {
58
- const existingStylesIds = Object.keys( styles );
65
+ function clearRemovedClasses( container: V1Element, { oldIds, newIds }: { oldIds: string[]; newIds: string[] } ) {
66
+ const removedIds = oldIds.filter( ( id ) => ! newIds.includes( id ) );
59
67
  const classesProps = structuredClone( getClassesProps( container ) );
60
68
 
61
69
  classesProps.forEach( ( [ , prop ] ) => {
62
- prop.value = prop.value.filter( ( value ) => existingStylesIds.includes( value ) );
70
+ prop.value = prop.value.filter( ( value ) => ! removedIds.includes( value ) );
63
71
  } );
64
72
 
65
73
  updateElementSettings( {
@@ -77,6 +85,11 @@ function getClassesProps( container: V1Element ) {
77
85
  } );
78
86
  }
79
87
 
88
+ function notifyChanges() {
89
+ dispatchChangeEvent();
90
+ runCommandSync( 'document/save/set-is-modified', { status: true }, { internal: true } );
91
+ }
92
+
80
93
  function dispatchChangeEvent() {
81
94
  window.dispatchEvent( new CustomEvent( ELEMENT_STYLE_CHANGE_EVENT ) );
82
95
  }
@@ -1,8 +1,8 @@
1
- import { type Props } from '@elementor/editor-props';
1
+ import { mergeProps } from '@elementor/editor-props';
2
2
  import { getVariantByMeta, type StyleDefinition, type StyleDefinitionVariant } from '@elementor/editor-styles';
3
3
 
4
+ import { StyleNotFoundError } from '../errors';
4
5
  import { type ElementID } from '../types';
5
- import { StyleNotFoundError } from './errors';
6
6
  import { mutateElementStyles } from './mutate-element-styles';
7
7
 
8
8
  export type UpdateElementStyleArgs = {
@@ -20,31 +20,14 @@ export function updateElementStyle( args: UpdateElementStyleArgs ) {
20
20
  throw new StyleNotFoundError( { context: { styleId: args.styleId } } );
21
21
  }
22
22
 
23
- let variant = getVariantByMeta( style, args.meta );
23
+ const variant = getVariantByMeta( style, args.meta );
24
24
 
25
- if ( ! variant ) {
26
- variant = { meta: args.meta, props: {} };
27
-
28
- style.variants.push( variant );
25
+ if ( variant ) {
26
+ variant.props = mergeProps( variant.props, args.props );
27
+ } else {
28
+ style.variants.push( { meta: args.meta, props: args.props } );
29
29
  }
30
30
 
31
- variant.props = mergeVariantProps( variant, args.props );
32
-
33
31
  return styles;
34
32
  } );
35
33
  }
36
-
37
- function mergeVariantProps( variant: StyleDefinitionVariant, props: Props ) {
38
- const finalProps = { ...variant.props };
39
-
40
- Object.entries( props ).forEach( ( [ key, value ] ) => {
41
- if ( value === null || value === undefined ) {
42
- // eslint-disable-next-line @typescript-eslint/no-dynamic-delete
43
- delete finalProps[ key ];
44
- } else {
45
- finalProps[ key ] = value;
46
- }
47
- } );
48
-
49
- return finalProps;
50
- }
@@ -0,0 +1,22 @@
1
+ import { ElementLabelNotExistsError, ElementTypeNotExistsError } from '../errors';
2
+ import getContainer from '../sync/get-container';
3
+ import { getWidgetsCache } from '../sync/get-widgets-cache';
4
+ import { type ElementID } from '../types';
5
+
6
+ export function getElementLabel( elementId: ElementID ) {
7
+ const container = getContainer( elementId );
8
+
9
+ const type = container?.model.get( 'widgetType' ) || container?.model.get( 'elType' );
10
+
11
+ if ( ! type ) {
12
+ throw new ElementTypeNotExistsError( { context: { elementId } } );
13
+ }
14
+
15
+ const label = getWidgetsCache()?.[ type ]?.title;
16
+
17
+ if ( ! label ) {
18
+ throw new ElementLabelNotExistsError( { context: { elementType: type } } );
19
+ }
20
+
21
+ return label;
22
+ }
package/src/sync/types.ts CHANGED
@@ -1,7 +1,7 @@
1
- import { type PropValue } from '@elementor/editor-props';
1
+ import { type PropsSchema, type PropValue } from '@elementor/editor-props';
2
2
  import { type StyleDefinition, type StyleDefinitionID } from '@elementor/editor-styles';
3
3
 
4
- import { type ControlItem, type PropsSchema } from '../types';
4
+ import { type ControlItem } from '../types';
5
5
 
6
6
  export type ExtendedWindow = Window & {
7
7
  elementor?: {
@@ -14,7 +14,7 @@ export type ExtendedWindow = Window & {
14
14
  atomic_controls?: ControlItem[];
15
15
  atomic_props_schema?: PropsSchema;
16
16
  controls: object;
17
- base_styles?: StyleDefinition[];
17
+ base_styles?: Record< string, StyleDefinition >;
18
18
  title: string;
19
19
  }
20
20
  >;
package/src/types.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { type PropType } from '@elementor/editor-props';
1
+ import { type PropsSchema } from '@elementor/editor-props';
2
2
 
3
3
  export type ElementID = string;
4
4
 
@@ -35,5 +35,3 @@ export type Control = {
35
35
  };
36
36
 
37
37
  export type ControlItem = ControlsSection | Control;
38
-
39
- export type PropsSchema = Record< Control[ 'value' ][ 'bind' ], PropType >;
@@ -1,49 +0,0 @@
1
- import { type Props } from '@elementor/editor-props';
2
- import { getVariantByMeta, type StyleDefinitionID, type StyleDefinitionVariant } from '@elementor/editor-styles';
3
- import { __privateUseListenTo as useListenTo, windowEvent } from '@elementor/editor-v1-adapters';
4
-
5
- import { ELEMENT_STYLE_CHANGE_EVENT } from '../styles/consts';
6
- import { getElementStyles } from '../sync/get-element-styles';
7
- import { type ElementID } from '../types';
8
-
9
- export type UseElementStylePropsArgs< T extends Props > = {
10
- elementID: ElementID;
11
- styleDefID: StyleDefinitionID | null;
12
- meta: StyleDefinitionVariant[ 'meta' ];
13
- propNames: Array< keyof T & string >;
14
- };
15
-
16
- type NullableObjectValues< T extends Props > = {
17
- [ K in keyof T ]: T[ K ] | null;
18
- };
19
-
20
- export function useElementStyleProps< T extends Props >( {
21
- elementID,
22
- styleDefID,
23
- meta,
24
- propNames,
25
- }: UseElementStylePropsArgs< T > ): NullableObjectValues< T > | null {
26
- return useListenTo(
27
- windowEvent( ELEMENT_STYLE_CHANGE_EVENT ),
28
- () => {
29
- if ( ! styleDefID ) {
30
- return null;
31
- }
32
-
33
- const styleDef = getElementStyles( elementID )?.[ styleDefID ];
34
-
35
- if ( ! styleDef ) {
36
- return null;
37
- }
38
-
39
- const variant = getVariantByMeta( styleDef, meta );
40
-
41
- return propNames.reduce< Record< string, unknown > >( ( acc, key ) => {
42
- acc[ key ] = variant?.props[ key ] ?? null;
43
-
44
- return acc;
45
- }, {} ) as NullableObjectValues< T >;
46
- },
47
- [ elementID, styleDefID, JSON.stringify( propNames ), meta ]
48
- );
49
- }
@@ -1,11 +0,0 @@
1
- import { createError } from '@elementor/utils';
2
-
3
- export const ElementNotFoundError = createError< { elementId: string } >( {
4
- code: 'element_not_found',
5
- message: 'Element not found.',
6
- } );
7
-
8
- export const StyleNotFoundError = createError< { styleId: string } >( {
9
- code: 'style_not_found',
10
- message: 'Style not found.',
11
- } );