@elementor/editor-elements 0.4.1 → 0.5.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.
@@ -0,0 +1,50 @@
1
+ import { type Props } from '@elementor/editor-props';
2
+ import { getVariantByMeta, type StyleDefinition, type StyleDefinitionVariant } from '@elementor/editor-styles';
3
+
4
+ import { type ElementID } from '../types';
5
+ import { StyleNotFoundError } from './errors';
6
+ import { mutateElementStyles } from './mutate-element-styles';
7
+
8
+ export type UpdateElementStyleArgs = {
9
+ elementId: ElementID;
10
+ styleId: StyleDefinition[ 'id' ];
11
+ meta: StyleDefinitionVariant[ 'meta' ];
12
+ props: StyleDefinitionVariant[ 'props' ];
13
+ };
14
+
15
+ export function updateElementStyle( args: UpdateElementStyleArgs ) {
16
+ mutateElementStyles( args.elementId, ( styles ) => {
17
+ const style = styles[ args.styleId ];
18
+
19
+ if ( ! style ) {
20
+ throw new StyleNotFoundError( { context: { styleId: args.styleId } } );
21
+ }
22
+
23
+ let variant = getVariantByMeta( style, args.meta );
24
+
25
+ if ( ! variant ) {
26
+ variant = { meta: args.meta, props: {} };
27
+
28
+ style.variants.push( variant );
29
+ }
30
+
31
+ variant.props = mergeVariantProps( variant, args.props );
32
+
33
+ return styles;
34
+ } );
35
+ }
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
+ }
package/src/sync/types.ts CHANGED
@@ -14,6 +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
18
  title: string;
18
19
  }
19
20
  >;
@@ -31,7 +32,7 @@ export type ExtendedWindow = Window & {
31
32
  export type V1Element = {
32
33
  id: string;
33
34
  model: V1Model< V1ElementModelProps >;
34
- settings?: V1Model< V1ElementSettingsProps >;
35
+ settings: V1Model< V1ElementSettingsProps >;
35
36
  children?: V1Element[];
36
37
  view?: {
37
38
  el: HTMLElement;
@@ -50,4 +51,6 @@ export type V1ElementSettingsProps = Record< string, PropValue >;
50
51
 
51
52
  type V1Model< T > = {
52
53
  get: < K extends keyof T >( key: K ) => T[ K ];
54
+ set: < K extends keyof T >( key: K, value: T[ K ] ) => void;
55
+ toJSON: () => T;
53
56
  };
@@ -0,0 +1,26 @@
1
+ import { type Props } from '@elementor/editor-props';
2
+ import { __privateRunCommandSync as runCommandSync } from '@elementor/editor-v1-adapters';
3
+
4
+ import { type ElementID } from '../types';
5
+ import getContainer from './get-container';
6
+
7
+ export type UpdateElementSettingsArgs = {
8
+ id: ElementID;
9
+ props: Props;
10
+ withHistory?: boolean;
11
+ };
12
+
13
+ export const updateElementSettings = ( { id, props, withHistory = true }: UpdateElementSettingsArgs ) => {
14
+ const container = getContainer( id );
15
+
16
+ const args = {
17
+ container,
18
+ settings: { ...props },
19
+ };
20
+
21
+ if ( withHistory ) {
22
+ runCommandSync( 'document/elements/settings', args );
23
+ } else {
24
+ runCommandSync( 'document/elements/set-settings', args, { internal: true } );
25
+ }
26
+ };
@@ -1,16 +0,0 @@
1
- import { type Props } from '@elementor/editor-props';
2
- import { __privateRunCommand as runCommand } from '@elementor/editor-v1-adapters';
3
-
4
- import { type ElementID } from '../types';
5
- import getContainer from './get-container';
6
-
7
- export const updateSettings = ( { id, props }: { id: ElementID; props: Props } ) => {
8
- const container = getContainer( id );
9
-
10
- runCommand( 'document/elements/settings', {
11
- container,
12
- settings: {
13
- ...props,
14
- },
15
- } );
16
- };
@@ -1,28 +0,0 @@
1
- import { type PropKey, type Props } from '@elementor/editor-props';
2
- import { type StyleDefinitionID, type StyleVariant } from '@elementor/editor-styles';
3
- import { __privateRunCommand as runCommand } from '@elementor/editor-v1-adapters';
4
-
5
- import { type ElementID } from '../types';
6
- import getContainer from './get-container';
7
-
8
- export type UpdateStyleProps = {
9
- elementID: ElementID;
10
- styleDefID: StyleDefinitionID | null;
11
- meta: StyleVariant[ 'meta' ];
12
- props: Props;
13
- bind: PropKey;
14
- label?: string;
15
- };
16
-
17
- export const updateStyle = async ( { elementID, styleDefID, meta, props, bind, label }: UpdateStyleProps ) => {
18
- const container = getContainer( elementID );
19
-
20
- await runCommand( 'document/atomic-widgets/styles', {
21
- container,
22
- styleDefID,
23
- bind,
24
- meta,
25
- props,
26
- label,
27
- } );
28
- };