@elementor/editor-elements 0.5.0 → 0.5.1
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/CHANGELOG.md +11 -0
- package/dist/index.d.mts +3 -12
- package/dist/index.d.ts +3 -12
- package/dist/index.js +29 -59
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +35 -65
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -3
- package/src/hooks/use-element-styles.ts +3 -5
- package/src/index.ts +1 -2
- package/src/styles/consts.ts +8 -0
- package/src/styles/mutate-element-styles.ts +11 -4
- package/src/styles/update-element-style.ts +6 -23
- package/src/hooks/use-element-style-props.ts +0 -49
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,16 @@
|
|
|
1
1
|
# @elementor/editor-elements
|
|
2
2
|
|
|
3
|
+
## 0.5.1
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- d61b1bc: Added a new event to update the elements styles repository + organized all events in a central location
|
|
8
|
+
- b34f498: Fix global class styles not updating
|
|
9
|
+
- Updated dependencies [a8b60c9]
|
|
10
|
+
- Updated dependencies [b34f498]
|
|
11
|
+
- @elementor/editor-v1-adapters@0.9.1
|
|
12
|
+
- @elementor/editor-styles@0.5.4
|
|
13
|
+
|
|
3
14
|
## 0.5.0
|
|
4
15
|
|
|
5
16
|
### Minor Changes
|
package/dist/index.d.mts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { PropType, PropValue, Props } from '@elementor/editor-props';
|
|
2
2
|
import * as _elementor_editor_styles from '@elementor/editor-styles';
|
|
3
3
|
import { StyleDefinitionID, StyleDefinition, StyleDefinitionVariant } from '@elementor/editor-styles';
|
|
4
|
+
import * as _elementor_editor_v1_adapters from '@elementor/editor-v1-adapters';
|
|
4
5
|
|
|
5
6
|
type ElementID = string;
|
|
6
7
|
type Element = {
|
|
@@ -61,17 +62,6 @@ declare function useElementsDomRef(): HTMLElement[];
|
|
|
61
62
|
|
|
62
63
|
declare const useElementSetting: <TValue>(elementId: ElementID, settingKey: string) => TValue | null;
|
|
63
64
|
|
|
64
|
-
type UseElementStylePropsArgs<T extends Props> = {
|
|
65
|
-
elementID: ElementID;
|
|
66
|
-
styleDefID: StyleDefinitionID | null;
|
|
67
|
-
meta: StyleDefinitionVariant['meta'];
|
|
68
|
-
propNames: Array<keyof T & string>;
|
|
69
|
-
};
|
|
70
|
-
type NullableObjectValues<T extends Props> = {
|
|
71
|
-
[K in keyof T]: T[K] | null;
|
|
72
|
-
};
|
|
73
|
-
declare function useElementStyleProps<T extends Props>({ elementID, styleDefID, meta, propNames, }: UseElementStylePropsArgs<T>): NullableObjectValues<T> | null;
|
|
74
|
-
|
|
75
65
|
declare function useElementStyles(elementId: ElementID): Record<string, _elementor_editor_styles.StyleDefinition>;
|
|
76
66
|
|
|
77
67
|
declare function useElementType(type?: string): ElementType | null;
|
|
@@ -112,6 +102,7 @@ type UpdateElementSettingsArgs = {
|
|
|
112
102
|
declare const updateElementSettings: ({ id, props, withHistory }: UpdateElementSettingsArgs) => void;
|
|
113
103
|
|
|
114
104
|
declare const ELEMENT_STYLE_CHANGE_EVENT = "elementor/editor-v2/editor-elements/style";
|
|
105
|
+
declare const styleRerenderEvents: (_elementor_editor_v1_adapters.CommandEventDescriptor | _elementor_editor_v1_adapters.WindowEventDescriptor)[];
|
|
115
106
|
|
|
116
107
|
type CreateElementStyleArgs = {
|
|
117
108
|
elementId: ElementID;
|
|
@@ -130,4 +121,4 @@ type UpdateElementStyleArgs = {
|
|
|
130
121
|
};
|
|
131
122
|
declare function updateElementStyle(args: UpdateElementStyleArgs): void;
|
|
132
123
|
|
|
133
|
-
export { type Control, type ControlItem, type ControlsSection, type CreateElementStyleArgs, ELEMENT_STYLE_CHANGE_EVENT, type Element, type ElementID, type ElementType, type PropsSchema, type UpdateElementSettingsArgs, type UpdateElementStyleArgs, type V1Element, type V1ElementModelProps, type V1ElementSettingsProps, createElementStyle, getElementSetting, getElementStyles, getElements, getSelectedElements, getWidgetsCache, isElementInContainer, updateElementSettings, updateElementStyle, useElementSetting,
|
|
124
|
+
export { type Control, type ControlItem, type ControlsSection, type CreateElementStyleArgs, ELEMENT_STYLE_CHANGE_EVENT, type Element, type ElementID, type ElementType, type PropsSchema, type UpdateElementSettingsArgs, type UpdateElementStyleArgs, type V1Element, type V1ElementModelProps, type V1ElementSettingsProps, createElementStyle, getElementSetting, getElementStyles, getElements, getSelectedElements, getWidgetsCache, isElementInContainer, styleRerenderEvents, updateElementSettings, updateElementStyle, useElementSetting, useElementStyles, useElementType, useElementsDomRef, useParentElement, useSelectedElement };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { PropType, PropValue, Props } from '@elementor/editor-props';
|
|
2
2
|
import * as _elementor_editor_styles from '@elementor/editor-styles';
|
|
3
3
|
import { StyleDefinitionID, StyleDefinition, StyleDefinitionVariant } from '@elementor/editor-styles';
|
|
4
|
+
import * as _elementor_editor_v1_adapters from '@elementor/editor-v1-adapters';
|
|
4
5
|
|
|
5
6
|
type ElementID = string;
|
|
6
7
|
type Element = {
|
|
@@ -61,17 +62,6 @@ declare function useElementsDomRef(): HTMLElement[];
|
|
|
61
62
|
|
|
62
63
|
declare const useElementSetting: <TValue>(elementId: ElementID, settingKey: string) => TValue | null;
|
|
63
64
|
|
|
64
|
-
type UseElementStylePropsArgs<T extends Props> = {
|
|
65
|
-
elementID: ElementID;
|
|
66
|
-
styleDefID: StyleDefinitionID | null;
|
|
67
|
-
meta: StyleDefinitionVariant['meta'];
|
|
68
|
-
propNames: Array<keyof T & string>;
|
|
69
|
-
};
|
|
70
|
-
type NullableObjectValues<T extends Props> = {
|
|
71
|
-
[K in keyof T]: T[K] | null;
|
|
72
|
-
};
|
|
73
|
-
declare function useElementStyleProps<T extends Props>({ elementID, styleDefID, meta, propNames, }: UseElementStylePropsArgs<T>): NullableObjectValues<T> | null;
|
|
74
|
-
|
|
75
65
|
declare function useElementStyles(elementId: ElementID): Record<string, _elementor_editor_styles.StyleDefinition>;
|
|
76
66
|
|
|
77
67
|
declare function useElementType(type?: string): ElementType | null;
|
|
@@ -112,6 +102,7 @@ type UpdateElementSettingsArgs = {
|
|
|
112
102
|
declare const updateElementSettings: ({ id, props, withHistory }: UpdateElementSettingsArgs) => void;
|
|
113
103
|
|
|
114
104
|
declare const ELEMENT_STYLE_CHANGE_EVENT = "elementor/editor-v2/editor-elements/style";
|
|
105
|
+
declare const styleRerenderEvents: (_elementor_editor_v1_adapters.CommandEventDescriptor | _elementor_editor_v1_adapters.WindowEventDescriptor)[];
|
|
115
106
|
|
|
116
107
|
type CreateElementStyleArgs = {
|
|
117
108
|
elementId: ElementID;
|
|
@@ -130,4 +121,4 @@ type UpdateElementStyleArgs = {
|
|
|
130
121
|
};
|
|
131
122
|
declare function updateElementStyle(args: UpdateElementStyleArgs): void;
|
|
132
123
|
|
|
133
|
-
export { type Control, type ControlItem, type ControlsSection, type CreateElementStyleArgs, ELEMENT_STYLE_CHANGE_EVENT, type Element, type ElementID, type ElementType, type PropsSchema, type UpdateElementSettingsArgs, type UpdateElementStyleArgs, type V1Element, type V1ElementModelProps, type V1ElementSettingsProps, createElementStyle, getElementSetting, getElementStyles, getElements, getSelectedElements, getWidgetsCache, isElementInContainer, updateElementSettings, updateElementStyle, useElementSetting,
|
|
124
|
+
export { type Control, type ControlItem, type ControlsSection, type CreateElementStyleArgs, ELEMENT_STYLE_CHANGE_EVENT, type Element, type ElementID, type ElementType, type PropsSchema, type UpdateElementSettingsArgs, type UpdateElementStyleArgs, type V1Element, type V1ElementModelProps, type V1ElementSettingsProps, createElementStyle, getElementSetting, getElementStyles, getElements, getSelectedElements, getWidgetsCache, isElementInContainer, styleRerenderEvents, updateElementSettings, updateElementStyle, useElementSetting, useElementStyles, useElementType, useElementsDomRef, useParentElement, useSelectedElement };
|
package/dist/index.js
CHANGED
|
@@ -28,10 +28,10 @@ __export(index_exports, {
|
|
|
28
28
|
getSelectedElements: () => getSelectedElements,
|
|
29
29
|
getWidgetsCache: () => getWidgetsCache,
|
|
30
30
|
isElementInContainer: () => isElementInContainer,
|
|
31
|
+
styleRerenderEvents: () => styleRerenderEvents,
|
|
31
32
|
updateElementSettings: () => updateElementSettings,
|
|
32
33
|
updateElementStyle: () => updateElementStyle,
|
|
33
34
|
useElementSetting: () => useElementSetting,
|
|
34
|
-
useElementStyleProps: () => useElementStyleProps,
|
|
35
35
|
useElementStyles: () => useElementStyles,
|
|
36
36
|
useElementType: () => useElementType,
|
|
37
37
|
useElementsDomRef: () => useElementsDomRef,
|
|
@@ -96,12 +96,17 @@ var useElementSetting = (elementId, settingKey) => {
|
|
|
96
96
|
);
|
|
97
97
|
};
|
|
98
98
|
|
|
99
|
-
// src/hooks/use-element-
|
|
100
|
-
var
|
|
101
|
-
var import_editor_v1_adapters3 = require("@elementor/editor-v1-adapters");
|
|
99
|
+
// src/hooks/use-element-styles.ts
|
|
100
|
+
var import_editor_v1_adapters4 = require("@elementor/editor-v1-adapters");
|
|
102
101
|
|
|
103
102
|
// src/styles/consts.ts
|
|
103
|
+
var import_editor_v1_adapters3 = require("@elementor/editor-v1-adapters");
|
|
104
104
|
var ELEMENT_STYLE_CHANGE_EVENT = "elementor/editor-v2/editor-elements/style";
|
|
105
|
+
var styleRerenderEvents = [
|
|
106
|
+
(0, import_editor_v1_adapters3.commandEndEvent)("document/elements/create"),
|
|
107
|
+
(0, import_editor_v1_adapters3.commandEndEvent)("editor/documents/attach-preview"),
|
|
108
|
+
(0, import_editor_v1_adapters3.windowEvent)(ELEMENT_STYLE_CHANGE_EVENT)
|
|
109
|
+
];
|
|
105
110
|
|
|
106
111
|
// src/sync/get-element-styles.ts
|
|
107
112
|
var getElementStyles = (elementID) => {
|
|
@@ -109,39 +114,9 @@ var getElementStyles = (elementID) => {
|
|
|
109
114
|
return container?.model.get("styles") || null;
|
|
110
115
|
};
|
|
111
116
|
|
|
112
|
-
// src/hooks/use-element-style-props.ts
|
|
113
|
-
function useElementStyleProps({
|
|
114
|
-
elementID,
|
|
115
|
-
styleDefID,
|
|
116
|
-
meta,
|
|
117
|
-
propNames
|
|
118
|
-
}) {
|
|
119
|
-
return (0, import_editor_v1_adapters3.__privateUseListenTo)(
|
|
120
|
-
(0, import_editor_v1_adapters3.windowEvent)(ELEMENT_STYLE_CHANGE_EVENT),
|
|
121
|
-
() => {
|
|
122
|
-
if (!styleDefID) {
|
|
123
|
-
return null;
|
|
124
|
-
}
|
|
125
|
-
const styleDef = getElementStyles(elementID)?.[styleDefID];
|
|
126
|
-
if (!styleDef) {
|
|
127
|
-
return null;
|
|
128
|
-
}
|
|
129
|
-
const variant = (0, import_editor_styles.getVariantByMeta)(styleDef, meta);
|
|
130
|
-
return propNames.reduce((acc, key) => {
|
|
131
|
-
acc[key] = variant?.props[key] ?? null;
|
|
132
|
-
return acc;
|
|
133
|
-
}, {});
|
|
134
|
-
},
|
|
135
|
-
[elementID, styleDefID, JSON.stringify(propNames), meta]
|
|
136
|
-
);
|
|
137
|
-
}
|
|
138
|
-
|
|
139
117
|
// src/hooks/use-element-styles.ts
|
|
140
|
-
var import_editor_v1_adapters4 = require("@elementor/editor-v1-adapters");
|
|
141
118
|
function useElementStyles(elementId) {
|
|
142
|
-
return (0, import_editor_v1_adapters4.__privateUseListenTo)(
|
|
143
|
-
elementId
|
|
144
|
-
]);
|
|
119
|
+
return (0, import_editor_v1_adapters4.__privateUseListenTo)(styleRerenderEvents, () => getElementStyles(elementId) ?? {}, [elementId]);
|
|
145
120
|
}
|
|
146
121
|
|
|
147
122
|
// src/hooks/use-element-type.ts
|
|
@@ -271,7 +246,7 @@ var updateElementSettings = ({ id, props, withHistory = true }) => {
|
|
|
271
246
|
|
|
272
247
|
// src/styles/create-element-style.ts
|
|
273
248
|
var import_editor_props2 = require("@elementor/editor-props");
|
|
274
|
-
var
|
|
249
|
+
var import_editor_styles = require("@elementor/editor-styles");
|
|
275
250
|
|
|
276
251
|
// src/styles/mutate-element-styles.ts
|
|
277
252
|
var import_editor_props = require("@elementor/editor-props");
|
|
@@ -293,8 +268,13 @@ function mutateElementStyles(elementId, mutator) {
|
|
|
293
268
|
if (!container) {
|
|
294
269
|
throw new ElementNotFoundError({ context: { elementId } });
|
|
295
270
|
}
|
|
271
|
+
const oldIds = Object.keys(container.model.get("styles") ?? {});
|
|
296
272
|
const styles = mutateStyles(container, mutator);
|
|
297
|
-
|
|
273
|
+
const newIds = Object.keys(styles);
|
|
274
|
+
clearRemovedClasses(container, {
|
|
275
|
+
oldIds,
|
|
276
|
+
newIds
|
|
277
|
+
});
|
|
298
278
|
dispatchChangeEvent();
|
|
299
279
|
return styles;
|
|
300
280
|
}
|
|
@@ -316,11 +296,11 @@ function removeEmptyVariants(style) {
|
|
|
316
296
|
function isStyleEmpty(style) {
|
|
317
297
|
return style.variants.length === 0;
|
|
318
298
|
}
|
|
319
|
-
function
|
|
320
|
-
const
|
|
299
|
+
function clearRemovedClasses(container, { oldIds, newIds }) {
|
|
300
|
+
const removedIds = oldIds.filter((id) => !newIds.includes(id));
|
|
321
301
|
const classesProps = structuredClone(getClassesProps(container));
|
|
322
302
|
classesProps.forEach(([, prop]) => {
|
|
323
|
-
prop.value = prop.value.filter((value) =>
|
|
303
|
+
prop.value = prop.value.filter((value) => !removedIds.includes(value));
|
|
324
304
|
});
|
|
325
305
|
updateElementSettings({
|
|
326
306
|
id: container.id,
|
|
@@ -341,7 +321,7 @@ function dispatchChangeEvent() {
|
|
|
341
321
|
// src/styles/create-element-style.ts
|
|
342
322
|
function createElementStyle({ elementId, classesProp, label, meta, props }) {
|
|
343
323
|
mutateElementStyles(elementId, (styles) => {
|
|
344
|
-
const id = (0,
|
|
324
|
+
const id = (0, import_editor_styles.generateId)(`e-${elementId}-`, Object.keys(styles));
|
|
345
325
|
styles[id] = {
|
|
346
326
|
id,
|
|
347
327
|
label,
|
|
@@ -370,32 +350,22 @@ function addStyleToClassesProp(elementId, classesProp, styleId) {
|
|
|
370
350
|
}
|
|
371
351
|
|
|
372
352
|
// src/styles/update-element-style.ts
|
|
373
|
-
var
|
|
353
|
+
var import_editor_props3 = require("@elementor/editor-props");
|
|
354
|
+
var import_editor_styles2 = require("@elementor/editor-styles");
|
|
374
355
|
function updateElementStyle(args) {
|
|
375
356
|
mutateElementStyles(args.elementId, (styles) => {
|
|
376
357
|
const style = styles[args.styleId];
|
|
377
358
|
if (!style) {
|
|
378
359
|
throw new StyleNotFoundError({ context: { styleId: args.styleId } });
|
|
379
360
|
}
|
|
380
|
-
|
|
381
|
-
if (
|
|
382
|
-
variant =
|
|
383
|
-
style.variants.push(variant);
|
|
384
|
-
}
|
|
385
|
-
variant.props = mergeVariantProps(variant, args.props);
|
|
386
|
-
return styles;
|
|
387
|
-
});
|
|
388
|
-
}
|
|
389
|
-
function mergeVariantProps(variant, props) {
|
|
390
|
-
const finalProps = { ...variant.props };
|
|
391
|
-
Object.entries(props).forEach(([key, value]) => {
|
|
392
|
-
if (value === null || value === void 0) {
|
|
393
|
-
delete finalProps[key];
|
|
361
|
+
const variant = (0, import_editor_styles2.getVariantByMeta)(style, args.meta);
|
|
362
|
+
if (variant) {
|
|
363
|
+
variant.props = (0, import_editor_props3.mergeProps)(variant.props, args.props);
|
|
394
364
|
} else {
|
|
395
|
-
|
|
365
|
+
style.variants.push({ meta: args.meta, props: args.props });
|
|
396
366
|
}
|
|
367
|
+
return styles;
|
|
397
368
|
});
|
|
398
|
-
return finalProps;
|
|
399
369
|
}
|
|
400
370
|
// Annotate the CommonJS export names for ESM import in node:
|
|
401
371
|
0 && (module.exports = {
|
|
@@ -407,10 +377,10 @@ function mergeVariantProps(variant, props) {
|
|
|
407
377
|
getSelectedElements,
|
|
408
378
|
getWidgetsCache,
|
|
409
379
|
isElementInContainer,
|
|
380
|
+
styleRerenderEvents,
|
|
410
381
|
updateElementSettings,
|
|
411
382
|
updateElementStyle,
|
|
412
383
|
useElementSetting,
|
|
413
|
-
useElementStyleProps,
|
|
414
384
|
useElementStyles,
|
|
415
385
|
useElementType,
|
|
416
386
|
useElementsDomRef,
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/hooks/use-elements-dom-ref.ts","../src/sync/get-current-document-container.ts","../src/hooks/use-element-setting.ts","../src/sync/get-container.ts","../src/sync/get-element-setting.ts","../src/hooks/use-element-style-props.ts","../src/styles/consts.ts","../src/sync/get-element-styles.ts","../src/hooks/use-element-styles.ts","../src/hooks/use-element-type.ts","../src/sync/get-widgets-cache.ts","../src/hooks/use-selected-element.ts","../src/sync/get-selected-elements.ts","../src/hooks/use-parent-element.ts","../src/sync/get-elements.ts","../src/sync/is-element-in-container.ts","../src/sync/update-element-settings.ts","../src/styles/create-element-style.ts","../src/styles/mutate-element-styles.ts","../src/styles/errors.ts","../src/styles/update-element-style.ts"],"sourcesContent":["// types\nexport * from './types';\nexport type { V1Element, V1ElementModelProps, V1ElementSettingsProps } from './sync/types';\n\n// hooks\nexport { useElementsDomRef } from './hooks/use-elements-dom-ref';\nexport { useElementSetting } from './hooks/use-element-setting';\nexport { useElementStyleProps } from './hooks/use-element-style-props';\nexport { useElementStyles } from './hooks/use-element-styles';\nexport { useElementType } from './hooks/use-element-type';\nexport { useSelectedElement } from './hooks/use-selected-element';\nexport { useParentElement } from './hooks/use-parent-element';\n\n// utils\nexport { getElementSetting } from './sync/get-element-setting';\nexport { getElementStyles } from './sync/get-element-styles';\nexport { getElements } from './sync/get-elements';\nexport { getSelectedElements } from './sync/get-selected-elements';\nexport { getWidgetsCache } from './sync/get-widgets-cache';\nexport { isElementInContainer } from './sync/is-element-in-container';\nexport { updateElementSettings, type UpdateElementSettingsArgs } from './sync/update-element-settings';\n\nexport { ELEMENT_STYLE_CHANGE_EVENT } from './styles/consts';\nexport { createElementStyle, type CreateElementStyleArgs } from './styles/create-element-style';\nexport { updateElementStyle, type UpdateElementStyleArgs } from './styles/update-element-style';\n","import { __privateUseListenTo as useListenTo, windowEvent } from '@elementor/editor-v1-adapters';\n\nimport getCurrentDocumentContainer from '../sync/get-current-document-container';\n\nconst ELEMENTS_DATA_ATTR = 'data-atomic';\n\nexport function useElementsDomRef(): HTMLElement[] {\n\treturn useListenTo(\n\t\t[\n\t\t\twindowEvent( 'elementor/preview/atomic-widget/render' ),\n\t\t\twindowEvent( 'elementor/preview/atomic-widget/destroy' ),\n\t\t\twindowEvent( 'elementor/editor/element-rendered' ),\n\t\t\twindowEvent( 'elementor/editor/element-destroyed' ),\n\t\t],\n\t\t() => getElementsDom()\n\t);\n}\n\nfunction getElementsDom(): HTMLElement[] {\n\tconst root = getCurrentDocumentContainer();\n\n\tif ( ! root?.view ) {\n\t\treturn [];\n\t}\n\n\treturn [ ...root.view.el.querySelectorAll< HTMLElement >( `[${ ELEMENTS_DATA_ATTR }]` ) ];\n}\n","import { type ExtendedWindow } from './types';\n\nexport default function getCurrentDocumentContainer() {\n\tconst extendedWindow = window as unknown as ExtendedWindow;\n\n\treturn extendedWindow.elementor?.documents?.getCurrent?.()?.container ?? null;\n}\n","import { __privateUseListenTo as useListenTo, commandEndEvent } from '@elementor/editor-v1-adapters';\n\nimport { getElementSetting } from '../sync/get-element-setting';\nimport { type ElementID } from '../types';\n\nexport const useElementSetting = < TValue >( elementId: ElementID, settingKey: string ) => {\n\treturn useListenTo(\n\t\tcommandEndEvent( 'document/elements/set-settings' ),\n\t\t() => getElementSetting< TValue >( elementId, settingKey ),\n\t\t[ elementId, settingKey ]\n\t);\n};\n","import { type ExtendedWindow } from './types';\n\nexport default function getContainer( id: string ) {\n\tconst extendedWindow = window as unknown as ExtendedWindow;\n\tconst container = extendedWindow.elementor?.getContainer?.( id );\n\n\treturn container ?? null;\n}\n","import { type ElementID } from '../types';\nimport getContainer from './get-container';\n\nexport const getElementSetting = < TValue >( elementId: ElementID, settingKey: string ): TValue | null => {\n\tconst container = getContainer( elementId );\n\tconst value = container?.settings?.get( settingKey ) as TValue;\n\n\treturn value ?? null;\n};\n","import { type Props } from '@elementor/editor-props';\nimport { getVariantByMeta, type StyleDefinitionID, type StyleDefinitionVariant } from '@elementor/editor-styles';\nimport { __privateUseListenTo as useListenTo, windowEvent } from '@elementor/editor-v1-adapters';\n\nimport { ELEMENT_STYLE_CHANGE_EVENT } from '../styles/consts';\nimport { getElementStyles } from '../sync/get-element-styles';\nimport { type ElementID } from '../types';\n\nexport type UseElementStylePropsArgs< T extends Props > = {\n\telementID: ElementID;\n\tstyleDefID: StyleDefinitionID | null;\n\tmeta: StyleDefinitionVariant[ 'meta' ];\n\tpropNames: Array< keyof T & string >;\n};\n\ntype NullableObjectValues< T extends Props > = {\n\t[ K in keyof T ]: T[ K ] | null;\n};\n\nexport function useElementStyleProps< T extends Props >( {\n\telementID,\n\tstyleDefID,\n\tmeta,\n\tpropNames,\n}: UseElementStylePropsArgs< T > ): NullableObjectValues< T > | null {\n\treturn useListenTo(\n\t\twindowEvent( ELEMENT_STYLE_CHANGE_EVENT ),\n\t\t() => {\n\t\t\tif ( ! styleDefID ) {\n\t\t\t\treturn null;\n\t\t\t}\n\n\t\t\tconst styleDef = getElementStyles( elementID )?.[ styleDefID ];\n\n\t\t\tif ( ! styleDef ) {\n\t\t\t\treturn null;\n\t\t\t}\n\n\t\t\tconst variant = getVariantByMeta( styleDef, meta );\n\n\t\t\treturn propNames.reduce< Record< string, unknown > >( ( acc, key ) => {\n\t\t\t\tacc[ key ] = variant?.props[ key ] ?? null;\n\n\t\t\t\treturn acc;\n\t\t\t}, {} ) as NullableObjectValues< T >;\n\t\t},\n\t\t[ elementID, styleDefID, JSON.stringify( propNames ), meta ]\n\t);\n}\n","export const ELEMENT_STYLE_CHANGE_EVENT = 'elementor/editor-v2/editor-elements/style';\n","import { type StyleDefinition } from '@elementor/editor-styles';\n\nimport { type ElementID } from '../types';\nimport getContainer from './get-container';\n\nexport const getElementStyles = ( elementID: ElementID ): Record< string, StyleDefinition > | null => {\n\tconst container = getContainer( elementID );\n\n\treturn container?.model.get( 'styles' ) || null;\n};\n","import { __privateUseListenTo as useListenTo, windowEvent } from '@elementor/editor-v1-adapters';\n\nimport { ELEMENT_STYLE_CHANGE_EVENT } from '../styles/consts';\nimport { getElementStyles } from '../sync/get-element-styles';\nimport { type ElementID } from '../types';\n\nexport function useElementStyles( elementId: ElementID ) {\n\treturn useListenTo( windowEvent( ELEMENT_STYLE_CHANGE_EVENT ), () => getElementStyles( elementId ) ?? {}, [\n\t\telementId,\n\t] );\n}\n","import { __privateUseListenTo as useListenTo, commandEndEvent } from '@elementor/editor-v1-adapters';\n\nimport { getWidgetsCache } from '../sync/get-widgets-cache';\nimport { type ElementType } from '../types';\n\nexport function useElementType( type?: string ) {\n\treturn useListenTo(\n\t\tcommandEndEvent( 'editor/documents/load' ),\n\t\t(): ElementType | null => {\n\t\t\tif ( ! type ) {\n\t\t\t\treturn null;\n\t\t\t}\n\n\t\t\tconst widgetsCache = getWidgetsCache();\n\t\t\tconst elementType = widgetsCache?.[ type ];\n\n\t\t\tif ( ! elementType?.atomic_controls ) {\n\t\t\t\treturn null;\n\t\t\t}\n\n\t\t\tif ( ! elementType?.atomic_props_schema ) {\n\t\t\t\treturn null;\n\t\t\t}\n\n\t\t\treturn {\n\t\t\t\tkey: type,\n\t\t\t\tcontrols: elementType.atomic_controls,\n\t\t\t\tpropsSchema: elementType.atomic_props_schema,\n\t\t\t\ttitle: elementType.title,\n\t\t\t};\n\t\t},\n\t\t[ type ]\n\t);\n}\n","import { type ExtendedWindow } from './types';\n\nexport function getWidgetsCache() {\n\tconst extendedWindow = window as unknown as ExtendedWindow;\n\n\treturn extendedWindow?.elementor?.widgetsCache || null;\n}\n","import { __privateUseListenTo as useListenTo, commandEndEvent } from '@elementor/editor-v1-adapters';\n\nimport { getSelectedElements } from '../sync/get-selected-elements';\nimport { useElementType } from './use-element-type';\n\nexport function useSelectedElement() {\n\tconst elements = useListenTo(\n\t\t[ commandEndEvent( 'document/elements/select' ), commandEndEvent( 'document/elements/deselect' ) ],\n\t\tgetSelectedElements\n\t);\n\n\tconst [ element ] = elements;\n\n\tconst elementType = useElementType( element?.type );\n\n\tif ( elements.length !== 1 || ! elementType ) {\n\t\treturn { element: null, elementType: null };\n\t}\n\n\treturn { element, elementType };\n}\n","import { type Element } from '../types';\nimport { type ExtendedWindow } from './types';\n\nexport function getSelectedElements(): Element[] {\n\tconst extendedWindow = window as unknown as ExtendedWindow;\n\n\tconst selectedElements = extendedWindow.elementor?.selection?.getElements?.() ?? [];\n\n\treturn selectedElements.reduce< Element[] >( ( acc, el ) => {\n\t\tconst type = el.model.get( 'widgetType' ) || el.model.get( 'elType' );\n\n\t\tif ( type ) {\n\t\t\tacc.push( {\n\t\t\t\tid: el.model.get( 'id' ),\n\t\t\t\ttype,\n\t\t\t} );\n\t\t}\n\n\t\treturn acc;\n\t}, [] );\n}\n","import { __privateUseListenTo as useListenTo, commandEndEvent } from '@elementor/editor-v1-adapters';\n\nimport { type ExtendedWindow } from '../sync/types';\n\nexport function useParentElement( elementId: string | null ) {\n\treturn useListenTo(\n\t\t[ commandEndEvent( 'document/elements/create' ) ],\n\t\t() => {\n\t\t\tif ( ! elementId ) {\n\t\t\t\treturn null;\n\t\t\t}\n\n\t\t\tconst extendedWindow = window as unknown as ExtendedWindow;\n\t\t\tconst element = extendedWindow?.elementor?.getContainer?.( elementId );\n\t\t\tif ( ! element ) {\n\t\t\t\treturn null;\n\t\t\t}\n\n\t\t\treturn element.parent;\n\t\t},\n\t\t[ elementId ]\n\t);\n}\n","import { type ElementID } from '../types';\nimport getContainer from './get-container';\nimport getCurrentDocumentContainer from './get-current-document-container';\nimport { type V1Element } from './types';\n\nexport function getElements( root?: ElementID ): V1Element[] {\n\tconst container = root ? getContainer( root ) : getCurrentDocumentContainer();\n\n\tif ( ! container ) {\n\t\treturn [];\n\t}\n\n\tconst children = container.children?.flatMap( ( child ) => getElements( child.id ) ) ?? [];\n\n\treturn [ container, ...children ];\n}\n","import { type Element } from '../types';\nimport { type V1Element } from './types';\n\nexport const isElementInContainer = ( element: Element, container: V1Element ): boolean => {\n\tif ( container.model.get( 'id' ) === element.id && container.model.get( 'widgetType' ) === element.type ) {\n\t\treturn true;\n\t}\n\n\tif ( container.children && container.children.length > 0 ) {\n\t\treturn container.children.some( ( child ) => isElementInContainer( element, child ) );\n\t}\n\n\treturn false;\n};\n","import { type Props } from '@elementor/editor-props';\nimport { __privateRunCommandSync as runCommandSync } from '@elementor/editor-v1-adapters';\n\nimport { type ElementID } from '../types';\nimport getContainer from './get-container';\n\nexport type UpdateElementSettingsArgs = {\n\tid: ElementID;\n\tprops: Props;\n\twithHistory?: boolean;\n};\n\nexport const updateElementSettings = ( { id, props, withHistory = true }: UpdateElementSettingsArgs ) => {\n\tconst container = getContainer( id );\n\n\tconst args = {\n\t\tcontainer,\n\t\tsettings: { ...props },\n\t};\n\n\tif ( withHistory ) {\n\t\trunCommandSync( 'document/elements/settings', args );\n\t} else {\n\t\trunCommandSync( 'document/elements/set-settings', args, { internal: true } );\n\t}\n};\n","import { classesPropTypeUtil } from '@elementor/editor-props';\nimport { generateId, type StyleDefinition, type StyleDefinitionVariant } from '@elementor/editor-styles';\n\nimport { getElementSetting } from '../sync/get-element-setting';\nimport { updateElementSettings } from '../sync/update-element-settings';\nimport { type ElementID } from '../types';\nimport { mutateElementStyles } from './mutate-element-styles';\n\nexport type CreateElementStyleArgs = {\n\telementId: ElementID;\n\tclassesProp: string;\n\tlabel: string;\n\tmeta: StyleDefinitionVariant[ 'meta' ];\n\tprops: StyleDefinitionVariant[ 'props' ];\n};\n\nexport function createElementStyle( { elementId, classesProp, label, meta, props }: CreateElementStyleArgs ) {\n\tmutateElementStyles( elementId, ( styles ) => {\n\t\tconst id = generateId( `e-${ elementId }-`, Object.keys( styles ) );\n\n\t\tstyles[ id ] = {\n\t\t\tid,\n\t\t\tlabel,\n\t\t\ttype: 'class',\n\t\t\tvariants: [ { meta, props } ],\n\t\t} satisfies StyleDefinition;\n\n\t\taddStyleToClassesProp( elementId, classesProp, id );\n\n\t\treturn styles;\n\t} );\n}\n\nfunction addStyleToClassesProp( elementId: ElementID, classesProp: string, styleId: string ) {\n\tconst base = getElementSetting( elementId, classesProp );\n\n\tconst classesPropValue = classesPropTypeUtil.create(\n\t\t( prev ) => {\n\t\t\treturn [ ...( prev ?? [] ), styleId ];\n\t\t},\n\t\t{ base }\n\t);\n\n\tupdateElementSettings( {\n\t\tid: elementId,\n\t\tprops: {\n\t\t\t[ classesProp ]: classesPropValue,\n\t\t},\n\t\twithHistory: false,\n\t} );\n}\n","import { classesPropTypeUtil, type ClassesPropValue } from '@elementor/editor-props';\nimport { type StyleDefinition, type StyleDefinitionsMap } from '@elementor/editor-styles';\n\nimport getContainer from '../sync/get-container';\nimport { type V1Element } from '../sync/types';\nimport { updateElementSettings } from '../sync/update-element-settings';\nimport { type ElementID } from '../types';\nimport { ELEMENT_STYLE_CHANGE_EVENT } from './consts';\nimport { ElementNotFoundError } from './errors';\n\nexport type Mutator = ( styles: StyleDefinitionsMap ) => StyleDefinitionsMap;\n\nexport function mutateElementStyles( elementId: ElementID, mutator: Mutator ) {\n\tconst container = getContainer( elementId );\n\n\tif ( ! container ) {\n\t\tthrow new ElementNotFoundError( { context: { elementId } } );\n\t}\n\n\tconst styles = mutateStyles( container, mutator );\n\n\tremoveNonExistingClasses( container, styles );\n\n\tdispatchChangeEvent();\n\n\treturn styles;\n}\n\nfunction mutateStyles( container: V1Element, mutator: Mutator ) {\n\tconst styles: StyleDefinitionsMap = structuredClone( container.model.get( 'styles' ) ) ?? {};\n\n\tconst entries = Object.entries( mutator( styles ) )\n\t\t.map( ( [ styleId, style ] ) => {\n\t\t\tstyle.variants = removeEmptyVariants( style );\n\n\t\t\treturn [ styleId, style ] as const;\n\t\t} )\n\t\t.filter( ( [ , style ] ) => {\n\t\t\treturn ! isStyleEmpty( style );\n\t\t} );\n\n\tconst mutatedStyles = Object.fromEntries( entries );\n\n\tcontainer.model.set( 'styles', mutatedStyles );\n\n\treturn mutatedStyles;\n}\n\nfunction removeEmptyVariants( style: StyleDefinition ) {\n\treturn style.variants.filter( ( { props } ) => Object.keys( props ).length > 0 );\n}\n\nfunction isStyleEmpty( style: StyleDefinition ) {\n\treturn style.variants.length === 0;\n}\n\nfunction removeNonExistingClasses( container: V1Element, styles: StyleDefinitionsMap ) {\n\tconst existingStylesIds = Object.keys( styles );\n\tconst classesProps = structuredClone( getClassesProps( container ) );\n\n\tclassesProps.forEach( ( [ , prop ] ) => {\n\t\tprop.value = prop.value.filter( ( value ) => existingStylesIds.includes( value ) );\n\t} );\n\n\tupdateElementSettings( {\n\t\tid: container.id,\n\t\tprops: Object.fromEntries( classesProps ),\n\t\twithHistory: false,\n\t} );\n}\n\nfunction getClassesProps( container: V1Element ) {\n\treturn Object.entries( container.settings.toJSON() ).filter( ( prop ): prop is [ string, ClassesPropValue ] => {\n\t\tconst [ , value ] = prop;\n\n\t\treturn classesPropTypeUtil.isValid( value );\n\t} );\n}\n\nfunction dispatchChangeEvent() {\n\twindow.dispatchEvent( new CustomEvent( ELEMENT_STYLE_CHANGE_EVENT ) );\n}\n","import { createError } from '@elementor/utils';\n\nexport const ElementNotFoundError = createError< { elementId: string } >( {\n\tcode: 'element_not_found',\n\tmessage: 'Element not found.',\n} );\n\nexport const StyleNotFoundError = createError< { styleId: string } >( {\n\tcode: 'style_not_found',\n\tmessage: 'Style not found.',\n} );\n","import { type Props } from '@elementor/editor-props';\nimport { getVariantByMeta, type StyleDefinition, type StyleDefinitionVariant } from '@elementor/editor-styles';\n\nimport { type ElementID } from '../types';\nimport { StyleNotFoundError } from './errors';\nimport { mutateElementStyles } from './mutate-element-styles';\n\nexport type UpdateElementStyleArgs = {\n\telementId: ElementID;\n\tstyleId: StyleDefinition[ 'id' ];\n\tmeta: StyleDefinitionVariant[ 'meta' ];\n\tprops: StyleDefinitionVariant[ 'props' ];\n};\n\nexport function updateElementStyle( args: UpdateElementStyleArgs ) {\n\tmutateElementStyles( args.elementId, ( styles ) => {\n\t\tconst style = styles[ args.styleId ];\n\n\t\tif ( ! style ) {\n\t\t\tthrow new StyleNotFoundError( { context: { styleId: args.styleId } } );\n\t\t}\n\n\t\tlet variant = getVariantByMeta( style, args.meta );\n\n\t\tif ( ! variant ) {\n\t\t\tvariant = { meta: args.meta, props: {} };\n\n\t\t\tstyle.variants.push( variant );\n\t\t}\n\n\t\tvariant.props = mergeVariantProps( variant, args.props );\n\n\t\treturn styles;\n\t} );\n}\n\nfunction mergeVariantProps( variant: StyleDefinitionVariant, props: Props ) {\n\tconst finalProps = { ...variant.props };\n\n\tObject.entries( props ).forEach( ( [ key, value ] ) => {\n\t\tif ( value === null || value === undefined ) {\n\t\t\t// eslint-disable-next-line @typescript-eslint/no-dynamic-delete\n\t\t\tdelete finalProps[ key ];\n\t\t} else {\n\t\t\tfinalProps[ key ] = value;\n\t\t}\n\t} );\n\n\treturn finalProps;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,gCAAiE;;;ACElD,SAAR,8BAA+C;AACrD,QAAM,iBAAiB;AAEvB,SAAO,eAAe,WAAW,WAAW,aAAa,GAAG,aAAa;AAC1E;;;ADFA,IAAM,qBAAqB;AAEpB,SAAS,oBAAmC;AAClD,aAAO,0BAAAA;AAAA,IACN;AAAA,UACC,uCAAa,wCAAyC;AAAA,UACtD,uCAAa,yCAA0C;AAAA,UACvD,uCAAa,mCAAoC;AAAA,UACjD,uCAAa,oCAAqC;AAAA,IACnD;AAAA,IACA,MAAM,eAAe;AAAA,EACtB;AACD;AAEA,SAAS,iBAAgC;AACxC,QAAM,OAAO,4BAA4B;AAEzC,MAAK,CAAE,MAAM,MAAO;AACnB,WAAO,CAAC;AAAA,EACT;AAEA,SAAO,CAAE,GAAG,KAAK,KAAK,GAAG,iBAAiC,IAAK,kBAAmB,GAAI,CAAE;AACz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S,sBAAuB,WAAsB,aAAqB,SAAkB;AAC5F,QAAM,OAAO,kBAAmB,WAAW,WAAY;AAEvD,QAAM,mBAAmB,yCAAoB;AAAA,IAC5C,CAAE,SAAU;AACX,aAAO,CAAE,GAAK,QAAQ,CAAC,GAAK,OAAQ;AAAA,IACrC;AAAA,IACA,EAAE,KAAK;AAAA,EACR;AAEA,wBAAuB;AAAA,IACtB,IAAI;AAAA,IACJ,OAAO;AAAA,MACN,CAAE,WAAY,GAAG;AAAA,IAClB;AAAA,IACA,aAAa;AAAA,EACd,CAAE;AACH;;;AGjDA,IAAAC,wBAAoF;AAa7E,SAAS,mBAAoB,MAA+B;AAClE,sBAAqB,KAAK,WAAW,CAAE,WAAY;AAClD,UAAM,QAAQ,OAAQ,KAAK,OAAQ;AAEnC,QAAK,CAAE,OAAQ;AACd,YAAM,IAAI,mBAAoB,EAAE,SAAS,EAAE,SAAS,KAAK,QAAQ,EAAE,CAAE;AAAA,IACtE;AAEA,QAAI,cAAU,wCAAkB,OAAO,KAAK,IAAK;AAEjD,QAAK,CAAE,SAAU;AAChB,gBAAU,EAAE,MAAM,KAAK,MAAM,OAAO,CAAC,EAAE;AAEvC,YAAM,SAAS,KAAM,OAAQ;AAAA,IAC9B;AAEA,YAAQ,QAAQ,kBAAmB,SAAS,KAAK,KAAM;AAEvD,WAAO;AAAA,EACR,CAAE;AACH;AAEA,SAAS,kBAAmB,SAAiC,OAAe;AAC3E,QAAM,aAAa,EAAE,GAAG,QAAQ,MAAM;AAEtC,SAAO,QAAS,KAAM,EAAE,QAAS,CAAE,CAAE,KAAK,KAAM,MAAO;AACtD,QAAK,UAAU,QAAQ,UAAU,QAAY;AAE5C,aAAO,WAAY,GAAI;AAAA,IACxB,OAAO;AACN,iBAAY,GAAI,IAAI;AAAA,IACrB;AAAA,EACD,CAAE;AAEF,SAAO;AACR;","names":["useListenTo","import_editor_v1_adapters","useListenTo","import_editor_v1_adapters","useListenTo","import_editor_v1_adapters","useListenTo","import_editor_v1_adapters","useListenTo","import_editor_v1_adapters","useListenTo","import_editor_v1_adapters","useListenTo","import_editor_v1_adapters","runCommandSync","import_editor_props","import_editor_styles","import_editor_styles"]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/hooks/use-elements-dom-ref.ts","../src/sync/get-current-document-container.ts","../src/hooks/use-element-setting.ts","../src/sync/get-container.ts","../src/sync/get-element-setting.ts","../src/hooks/use-element-styles.ts","../src/styles/consts.ts","../src/sync/get-element-styles.ts","../src/hooks/use-element-type.ts","../src/sync/get-widgets-cache.ts","../src/hooks/use-selected-element.ts","../src/sync/get-selected-elements.ts","../src/hooks/use-parent-element.ts","../src/sync/get-elements.ts","../src/sync/is-element-in-container.ts","../src/sync/update-element-settings.ts","../src/styles/create-element-style.ts","../src/styles/mutate-element-styles.ts","../src/styles/errors.ts","../src/styles/update-element-style.ts"],"sourcesContent":["// types\nexport * from './types';\nexport type { V1Element, V1ElementModelProps, V1ElementSettingsProps } from './sync/types';\n\n// hooks\nexport { useElementsDomRef } from './hooks/use-elements-dom-ref';\nexport { useElementSetting } from './hooks/use-element-setting';\nexport { useElementStyles } from './hooks/use-element-styles';\nexport { useElementType } from './hooks/use-element-type';\nexport { useSelectedElement } from './hooks/use-selected-element';\nexport { useParentElement } from './hooks/use-parent-element';\n\n// utils\nexport { getElementSetting } from './sync/get-element-setting';\nexport { getElementStyles } from './sync/get-element-styles';\nexport { getElements } from './sync/get-elements';\nexport { getSelectedElements } from './sync/get-selected-elements';\nexport { getWidgetsCache } from './sync/get-widgets-cache';\nexport { isElementInContainer } from './sync/is-element-in-container';\nexport { updateElementSettings, type UpdateElementSettingsArgs } from './sync/update-element-settings';\n\nexport { ELEMENT_STYLE_CHANGE_EVENT, styleRerenderEvents } from './styles/consts';\nexport { createElementStyle, type CreateElementStyleArgs } from './styles/create-element-style';\nexport { updateElementStyle, type UpdateElementStyleArgs } from './styles/update-element-style';\n","import { __privateUseListenTo as useListenTo, windowEvent } from '@elementor/editor-v1-adapters';\n\nimport getCurrentDocumentContainer from '../sync/get-current-document-container';\n\nconst ELEMENTS_DATA_ATTR = 'data-atomic';\n\nexport function useElementsDomRef(): HTMLElement[] {\n\treturn useListenTo(\n\t\t[\n\t\t\twindowEvent( 'elementor/preview/atomic-widget/render' ),\n\t\t\twindowEvent( 'elementor/preview/atomic-widget/destroy' ),\n\t\t\twindowEvent( 'elementor/editor/element-rendered' ),\n\t\t\twindowEvent( 'elementor/editor/element-destroyed' ),\n\t\t],\n\t\t() => getElementsDom()\n\t);\n}\n\nfunction getElementsDom(): HTMLElement[] {\n\tconst root = getCurrentDocumentContainer();\n\n\tif ( ! root?.view ) {\n\t\treturn [];\n\t}\n\n\treturn [ ...root.view.el.querySelectorAll< HTMLElement >( `[${ ELEMENTS_DATA_ATTR }]` ) ];\n}\n","import { type ExtendedWindow } from './types';\n\nexport default function getCurrentDocumentContainer() {\n\tconst extendedWindow = window as unknown as ExtendedWindow;\n\n\treturn extendedWindow.elementor?.documents?.getCurrent?.()?.container ?? null;\n}\n","import { __privateUseListenTo as useListenTo, commandEndEvent } from '@elementor/editor-v1-adapters';\n\nimport { getElementSetting } from '../sync/get-element-setting';\nimport { type ElementID } from '../types';\n\nexport const useElementSetting = < TValue >( elementId: ElementID, settingKey: string ) => {\n\treturn useListenTo(\n\t\tcommandEndEvent( 'document/elements/set-settings' ),\n\t\t() => getElementSetting< TValue >( elementId, settingKey ),\n\t\t[ elementId, settingKey ]\n\t);\n};\n","import { type ExtendedWindow } from './types';\n\nexport default function getContainer( id: string ) {\n\tconst extendedWindow = window as unknown as ExtendedWindow;\n\tconst container = extendedWindow.elementor?.getContainer?.( id );\n\n\treturn container ?? null;\n}\n","import { type ElementID } from '../types';\nimport getContainer from './get-container';\n\nexport const getElementSetting = < TValue >( elementId: ElementID, settingKey: string ): TValue | null => {\n\tconst container = getContainer( elementId );\n\tconst value = container?.settings?.get( settingKey ) as TValue;\n\n\treturn value ?? null;\n};\n","import { __privateUseListenTo as useListenTo } from '@elementor/editor-v1-adapters';\n\nimport { styleRerenderEvents } from '../styles/consts';\nimport { getElementStyles } from '../sync/get-element-styles';\nimport { type ElementID } from '../types';\n\nexport function useElementStyles( elementId: ElementID ) {\n\treturn useListenTo( styleRerenderEvents, () => getElementStyles( elementId ) ?? {}, [ elementId ] );\n}\n","import { commandEndEvent, windowEvent } from '@elementor/editor-v1-adapters';\n\nexport const ELEMENT_STYLE_CHANGE_EVENT = 'elementor/editor-v2/editor-elements/style';\n\nexport const styleRerenderEvents = [\n\tcommandEndEvent( 'document/elements/create' ),\n\tcommandEndEvent( 'editor/documents/attach-preview' ),\n\twindowEvent( ELEMENT_STYLE_CHANGE_EVENT ),\n];\n","import { type StyleDefinition } from '@elementor/editor-styles';\n\nimport { type ElementID } from '../types';\nimport getContainer from './get-container';\n\nexport const getElementStyles = ( elementID: ElementID ): Record< string, StyleDefinition > | null => {\n\tconst container = getContainer( elementID );\n\n\treturn container?.model.get( 'styles' ) || null;\n};\n","import { __privateUseListenTo as useListenTo, commandEndEvent } from '@elementor/editor-v1-adapters';\n\nimport { getWidgetsCache } from '../sync/get-widgets-cache';\nimport { type ElementType } from '../types';\n\nexport function useElementType( type?: string ) {\n\treturn useListenTo(\n\t\tcommandEndEvent( 'editor/documents/load' ),\n\t\t(): ElementType | null => {\n\t\t\tif ( ! type ) {\n\t\t\t\treturn null;\n\t\t\t}\n\n\t\t\tconst widgetsCache = getWidgetsCache();\n\t\t\tconst elementType = widgetsCache?.[ type ];\n\n\t\t\tif ( ! elementType?.atomic_controls ) {\n\t\t\t\treturn null;\n\t\t\t}\n\n\t\t\tif ( ! elementType?.atomic_props_schema ) {\n\t\t\t\treturn null;\n\t\t\t}\n\n\t\t\treturn {\n\t\t\t\tkey: type,\n\t\t\t\tcontrols: elementType.atomic_controls,\n\t\t\t\tpropsSchema: elementType.atomic_props_schema,\n\t\t\t\ttitle: elementType.title,\n\t\t\t};\n\t\t},\n\t\t[ type ]\n\t);\n}\n","import { type ExtendedWindow } from './types';\n\nexport function getWidgetsCache() {\n\tconst extendedWindow = window as unknown as ExtendedWindow;\n\n\treturn extendedWindow?.elementor?.widgetsCache || null;\n}\n","import { __privateUseListenTo as useListenTo, commandEndEvent } from '@elementor/editor-v1-adapters';\n\nimport { getSelectedElements } from '../sync/get-selected-elements';\nimport { useElementType } from './use-element-type';\n\nexport function useSelectedElement() {\n\tconst elements = useListenTo(\n\t\t[ commandEndEvent( 'document/elements/select' ), commandEndEvent( 'document/elements/deselect' ) ],\n\t\tgetSelectedElements\n\t);\n\n\tconst [ element ] = elements;\n\n\tconst elementType = useElementType( element?.type );\n\n\tif ( elements.length !== 1 || ! elementType ) {\n\t\treturn { element: null, elementType: null };\n\t}\n\n\treturn { element, elementType };\n}\n","import { type Element } from '../types';\nimport { type ExtendedWindow } from './types';\n\nexport function getSelectedElements(): Element[] {\n\tconst extendedWindow = window as unknown as ExtendedWindow;\n\n\tconst selectedElements = extendedWindow.elementor?.selection?.getElements?.() ?? [];\n\n\treturn selectedElements.reduce< Element[] >( ( acc, el ) => {\n\t\tconst type = el.model.get( 'widgetType' ) || el.model.get( 'elType' );\n\n\t\tif ( type ) {\n\t\t\tacc.push( {\n\t\t\t\tid: el.model.get( 'id' ),\n\t\t\t\ttype,\n\t\t\t} );\n\t\t}\n\n\t\treturn acc;\n\t}, [] );\n}\n","import { __privateUseListenTo as useListenTo, commandEndEvent } from '@elementor/editor-v1-adapters';\n\nimport { type ExtendedWindow } from '../sync/types';\n\nexport function useParentElement( elementId: string | null ) {\n\treturn useListenTo(\n\t\t[ commandEndEvent( 'document/elements/create' ) ],\n\t\t() => {\n\t\t\tif ( ! elementId ) {\n\t\t\t\treturn null;\n\t\t\t}\n\n\t\t\tconst extendedWindow = window as unknown as ExtendedWindow;\n\t\t\tconst element = extendedWindow?.elementor?.getContainer?.( elementId );\n\t\t\tif ( ! element ) {\n\t\t\t\treturn null;\n\t\t\t}\n\n\t\t\treturn element.parent;\n\t\t},\n\t\t[ elementId ]\n\t);\n}\n","import { type ElementID } from '../types';\nimport getContainer from './get-container';\nimport getCurrentDocumentContainer from './get-current-document-container';\nimport { type V1Element } from './types';\n\nexport function getElements( root?: ElementID ): V1Element[] {\n\tconst container = root ? getContainer( root ) : getCurrentDocumentContainer();\n\n\tif ( ! container ) {\n\t\treturn [];\n\t}\n\n\tconst children = container.children?.flatMap( ( child ) => getElements( child.id ) ) ?? [];\n\n\treturn [ container, ...children ];\n}\n","import { type Element } from '../types';\nimport { type V1Element } from './types';\n\nexport const isElementInContainer = ( element: Element, container: V1Element ): boolean => {\n\tif ( container.model.get( 'id' ) === element.id && container.model.get( 'widgetType' ) === element.type ) {\n\t\treturn true;\n\t}\n\n\tif ( container.children && container.children.length > 0 ) {\n\t\treturn container.children.some( ( child ) => isElementInContainer( element, child ) );\n\t}\n\n\treturn false;\n};\n","import { type Props } from '@elementor/editor-props';\nimport { __privateRunCommandSync as runCommandSync } from '@elementor/editor-v1-adapters';\n\nimport { type ElementID } from '../types';\nimport getContainer from './get-container';\n\nexport type UpdateElementSettingsArgs = {\n\tid: ElementID;\n\tprops: Props;\n\twithHistory?: boolean;\n};\n\nexport const updateElementSettings = ( { id, props, withHistory = true }: UpdateElementSettingsArgs ) => {\n\tconst container = getContainer( id );\n\n\tconst args = {\n\t\tcontainer,\n\t\tsettings: { ...props },\n\t};\n\n\tif ( withHistory ) {\n\t\trunCommandSync( 'document/elements/settings', args );\n\t} else {\n\t\trunCommandSync( 'document/elements/set-settings', args, { internal: true } );\n\t}\n};\n","import { classesPropTypeUtil } from '@elementor/editor-props';\nimport { generateId, type StyleDefinition, type StyleDefinitionVariant } from '@elementor/editor-styles';\n\nimport { getElementSetting } from '../sync/get-element-setting';\nimport { updateElementSettings } from '../sync/update-element-settings';\nimport { type ElementID } from '../types';\nimport { mutateElementStyles } from './mutate-element-styles';\n\nexport type CreateElementStyleArgs = {\n\telementId: ElementID;\n\tclassesProp: string;\n\tlabel: string;\n\tmeta: StyleDefinitionVariant[ 'meta' ];\n\tprops: StyleDefinitionVariant[ 'props' ];\n};\n\nexport function createElementStyle( { elementId, classesProp, label, meta, props }: CreateElementStyleArgs ) {\n\tmutateElementStyles( elementId, ( styles ) => {\n\t\tconst id = generateId( `e-${ elementId }-`, Object.keys( styles ) );\n\n\t\tstyles[ id ] = {\n\t\t\tid,\n\t\t\tlabel,\n\t\t\ttype: 'class',\n\t\t\tvariants: [ { meta, props } ],\n\t\t} satisfies StyleDefinition;\n\n\t\taddStyleToClassesProp( elementId, classesProp, id );\n\n\t\treturn styles;\n\t} );\n}\n\nfunction addStyleToClassesProp( elementId: ElementID, classesProp: string, styleId: string ) {\n\tconst base = getElementSetting( elementId, classesProp );\n\n\tconst classesPropValue = classesPropTypeUtil.create(\n\t\t( prev ) => {\n\t\t\treturn [ ...( prev ?? [] ), styleId ];\n\t\t},\n\t\t{ base }\n\t);\n\n\tupdateElementSettings( {\n\t\tid: elementId,\n\t\tprops: {\n\t\t\t[ classesProp ]: classesPropValue,\n\t\t},\n\t\twithHistory: false,\n\t} );\n}\n","import { classesPropTypeUtil, type ClassesPropValue } from '@elementor/editor-props';\nimport { type StyleDefinition, type StyleDefinitionsMap } from '@elementor/editor-styles';\n\nimport getContainer from '../sync/get-container';\nimport { type V1Element } from '../sync/types';\nimport { updateElementSettings } from '../sync/update-element-settings';\nimport { type ElementID } from '../types';\nimport { ELEMENT_STYLE_CHANGE_EVENT } from './consts';\nimport { ElementNotFoundError } from './errors';\n\nexport type Mutator = ( styles: StyleDefinitionsMap ) => StyleDefinitionsMap;\n\nexport function mutateElementStyles( elementId: ElementID, mutator: Mutator ) {\n\tconst container = getContainer( elementId );\n\n\tif ( ! container ) {\n\t\tthrow new ElementNotFoundError( { context: { elementId } } );\n\t}\n\n\tconst oldIds = Object.keys( container.model.get( 'styles' ) ?? {} );\n\n\tconst styles = mutateStyles( container, mutator );\n\n\tconst newIds = Object.keys( styles );\n\n\tclearRemovedClasses( container, {\n\t\toldIds,\n\t\tnewIds,\n\t} );\n\n\tdispatchChangeEvent();\n\n\treturn styles;\n}\n\nfunction mutateStyles( container: V1Element, mutator: Mutator ) {\n\tconst styles: StyleDefinitionsMap = structuredClone( container.model.get( 'styles' ) ) ?? {};\n\n\tconst entries = Object.entries( mutator( styles ) )\n\t\t.map( ( [ styleId, style ] ) => {\n\t\t\tstyle.variants = removeEmptyVariants( style );\n\n\t\t\treturn [ styleId, style ] as const;\n\t\t} )\n\t\t.filter( ( [ , style ] ) => {\n\t\t\treturn ! isStyleEmpty( style );\n\t\t} );\n\n\tconst mutatedStyles = Object.fromEntries( entries );\n\n\tcontainer.model.set( 'styles', mutatedStyles );\n\n\treturn mutatedStyles;\n}\n\nfunction removeEmptyVariants( style: StyleDefinition ) {\n\treturn style.variants.filter( ( { props } ) => Object.keys( props ).length > 0 );\n}\n\nfunction isStyleEmpty( style: StyleDefinition ) {\n\treturn style.variants.length === 0;\n}\n\nfunction clearRemovedClasses( container: V1Element, { oldIds, newIds }: { oldIds: string[]; newIds: string[] } ) {\n\tconst removedIds = oldIds.filter( ( id ) => ! newIds.includes( id ) );\n\tconst classesProps = structuredClone( getClassesProps( container ) );\n\n\tclassesProps.forEach( ( [ , prop ] ) => {\n\t\tprop.value = prop.value.filter( ( value ) => ! removedIds.includes( value ) );\n\t} );\n\n\tupdateElementSettings( {\n\t\tid: container.id,\n\t\tprops: Object.fromEntries( classesProps ),\n\t\twithHistory: false,\n\t} );\n}\n\nfunction getClassesProps( container: V1Element ) {\n\treturn Object.entries( container.settings.toJSON() ).filter( ( prop ): prop is [ string, ClassesPropValue ] => {\n\t\tconst [ , value ] = prop;\n\n\t\treturn classesPropTypeUtil.isValid( value );\n\t} );\n}\n\nfunction dispatchChangeEvent() {\n\twindow.dispatchEvent( new CustomEvent( ELEMENT_STYLE_CHANGE_EVENT ) );\n}\n","import { createError } from '@elementor/utils';\n\nexport const ElementNotFoundError = createError< { elementId: string } >( {\n\tcode: 'element_not_found',\n\tmessage: 'Element not found.',\n} );\n\nexport const StyleNotFoundError = createError< { styleId: string } >( {\n\tcode: 'style_not_found',\n\tmessage: 'Style not found.',\n} );\n","import { mergeProps } from '@elementor/editor-props';\nimport { getVariantByMeta, type StyleDefinition, type StyleDefinitionVariant } from '@elementor/editor-styles';\n\nimport { type ElementID } from '../types';\nimport { StyleNotFoundError } from './errors';\nimport { mutateElementStyles } from './mutate-element-styles';\n\nexport type UpdateElementStyleArgs = {\n\telementId: ElementID;\n\tstyleId: StyleDefinition[ 'id' ];\n\tmeta: StyleDefinitionVariant[ 'meta' ];\n\tprops: StyleDefinitionVariant[ 'props' ];\n};\n\nexport function updateElementStyle( args: UpdateElementStyleArgs ) {\n\tmutateElementStyles( args.elementId, ( styles ) => {\n\t\tconst style = styles[ args.styleId ];\n\n\t\tif ( ! style ) {\n\t\t\tthrow new StyleNotFoundError( { context: { styleId: args.styleId } } );\n\t\t}\n\n\t\tconst variant = getVariantByMeta( style, args.meta );\n\n\t\tif ( variant ) {\n\t\t\tvariant.props = mergeProps( variant.props, args.props );\n\t\t} else {\n\t\t\tstyle.variants.push( { meta: args.meta, props: args.props } );\n\t\t}\n\n\t\treturn styles;\n\t} );\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,gCAAiE;;;ACElD,SAAR,8BAA+C;AACrD,QAAM,iBAAiB;AAEvB,SAAO,eAAe,WAAW,WAAW,aAAa,GAAG,aAAa;AAC1E;;;ADFA,IAAM,qBAAqB;AAEpB,SAAS,oBAAmC;AAClD,aAAO,0BAAAA;AAAA,IACN;AAAA,UACC,uCAAa,wCAAyC;AAAA,UACtD,uCAAa,yCAA0C;AAAA,UACvD,uCAAa,mCAAoC;AAAA,UACjD,uCAAa,oCAAqC;AAAA,IACnD;AAAA,IACA,MAAM,eAAe;AAAA,EACtB;AACD;AAEA,SAAS,iB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AAAA,IAC5C,CAAE,SAAU;AACX,aAAO,CAAE,GAAK,QAAQ,CAAC,GAAK,OAAQ;AAAA,IACrC;AAAA,IACA,EAAE,KAAK;AAAA,EACR;AAEA,wBAAuB;AAAA,IACtB,IAAI;AAAA,IACJ,OAAO;AAAA,MACN,CAAE,WAAY,GAAG;AAAA,IAClB;AAAA,IACA,aAAa;AAAA,EACd,CAAE;AACH;;;AGlDA,IAAAC,uBAA2B;AAC3B,IAAAC,wBAAoF;AAa7E,SAAS,mBAAoB,MAA+B;AAClE,sBAAqB,KAAK,WAAW,CAAE,WAAY;AAClD,UAAM,QAAQ,OAAQ,KAAK,OAAQ;AAEnC,QAAK,CAAE,OAAQ;AACd,YAAM,IAAI,mBAAoB,EAAE,SAAS,EAAE,SAAS,KAAK,QAAQ,EAAE,CAAE;AAAA,IACtE;AAEA,UAAM,cAAU,wCAAkB,OAAO,KAAK,IAAK;AAEnD,QAAK,SAAU;AACd,cAAQ,YAAQ,iCAAY,QAAQ,OAAO,KAAK,KAAM;AAAA,IACvD,OAAO;AACN,YAAM,SAAS,KAAM,EAAE,MAAM,KAAK,MAAM,OAAO,KAAK,MAAM,CAAE;AAAA,IAC7D;AAEA,WAAO;AAAA,EACR,CAAE;AACH;","names":["useListenTo","import_editor_v1_adapters","useListenTo","import_editor_v1_adapters","import_editor_v1_adapters","useListenTo","import_editor_v1_adapters","useListenTo","import_editor_v1_adapters","useListenTo","import_editor_v1_adapters","useListenTo","import_editor_v1_adapters","runCommandSync","import_editor_props","import_editor_props","import_editor_styles"]}
|
package/dist/index.mjs
CHANGED
|
@@ -54,12 +54,17 @@ var useElementSetting = (elementId, settingKey) => {
|
|
|
54
54
|
);
|
|
55
55
|
};
|
|
56
56
|
|
|
57
|
-
// src/hooks/use-element-
|
|
58
|
-
import {
|
|
59
|
-
import { __privateUseListenTo as useListenTo3, windowEvent as windowEvent2 } from "@elementor/editor-v1-adapters";
|
|
57
|
+
// src/hooks/use-element-styles.ts
|
|
58
|
+
import { __privateUseListenTo as useListenTo3 } from "@elementor/editor-v1-adapters";
|
|
60
59
|
|
|
61
60
|
// src/styles/consts.ts
|
|
61
|
+
import { commandEndEvent as commandEndEvent2, windowEvent as windowEvent2 } from "@elementor/editor-v1-adapters";
|
|
62
62
|
var ELEMENT_STYLE_CHANGE_EVENT = "elementor/editor-v2/editor-elements/style";
|
|
63
|
+
var styleRerenderEvents = [
|
|
64
|
+
commandEndEvent2("document/elements/create"),
|
|
65
|
+
commandEndEvent2("editor/documents/attach-preview"),
|
|
66
|
+
windowEvent2(ELEMENT_STYLE_CHANGE_EVENT)
|
|
67
|
+
];
|
|
63
68
|
|
|
64
69
|
// src/sync/get-element-styles.ts
|
|
65
70
|
var getElementStyles = (elementID) => {
|
|
@@ -67,43 +72,13 @@ var getElementStyles = (elementID) => {
|
|
|
67
72
|
return container?.model.get("styles") || null;
|
|
68
73
|
};
|
|
69
74
|
|
|
70
|
-
// src/hooks/use-element-style-props.ts
|
|
71
|
-
function useElementStyleProps({
|
|
72
|
-
elementID,
|
|
73
|
-
styleDefID,
|
|
74
|
-
meta,
|
|
75
|
-
propNames
|
|
76
|
-
}) {
|
|
77
|
-
return useListenTo3(
|
|
78
|
-
windowEvent2(ELEMENT_STYLE_CHANGE_EVENT),
|
|
79
|
-
() => {
|
|
80
|
-
if (!styleDefID) {
|
|
81
|
-
return null;
|
|
82
|
-
}
|
|
83
|
-
const styleDef = getElementStyles(elementID)?.[styleDefID];
|
|
84
|
-
if (!styleDef) {
|
|
85
|
-
return null;
|
|
86
|
-
}
|
|
87
|
-
const variant = getVariantByMeta(styleDef, meta);
|
|
88
|
-
return propNames.reduce((acc, key) => {
|
|
89
|
-
acc[key] = variant?.props[key] ?? null;
|
|
90
|
-
return acc;
|
|
91
|
-
}, {});
|
|
92
|
-
},
|
|
93
|
-
[elementID, styleDefID, JSON.stringify(propNames), meta]
|
|
94
|
-
);
|
|
95
|
-
}
|
|
96
|
-
|
|
97
75
|
// src/hooks/use-element-styles.ts
|
|
98
|
-
import { __privateUseListenTo as useListenTo4, windowEvent as windowEvent3 } from "@elementor/editor-v1-adapters";
|
|
99
76
|
function useElementStyles(elementId) {
|
|
100
|
-
return
|
|
101
|
-
elementId
|
|
102
|
-
]);
|
|
77
|
+
return useListenTo3(styleRerenderEvents, () => getElementStyles(elementId) ?? {}, [elementId]);
|
|
103
78
|
}
|
|
104
79
|
|
|
105
80
|
// src/hooks/use-element-type.ts
|
|
106
|
-
import { __privateUseListenTo as
|
|
81
|
+
import { __privateUseListenTo as useListenTo4, commandEndEvent as commandEndEvent3 } from "@elementor/editor-v1-adapters";
|
|
107
82
|
|
|
108
83
|
// src/sync/get-widgets-cache.ts
|
|
109
84
|
function getWidgetsCache() {
|
|
@@ -113,8 +88,8 @@ function getWidgetsCache() {
|
|
|
113
88
|
|
|
114
89
|
// src/hooks/use-element-type.ts
|
|
115
90
|
function useElementType(type) {
|
|
116
|
-
return
|
|
117
|
-
|
|
91
|
+
return useListenTo4(
|
|
92
|
+
commandEndEvent3("editor/documents/load"),
|
|
118
93
|
() => {
|
|
119
94
|
if (!type) {
|
|
120
95
|
return null;
|
|
@@ -139,7 +114,7 @@ function useElementType(type) {
|
|
|
139
114
|
}
|
|
140
115
|
|
|
141
116
|
// src/hooks/use-selected-element.ts
|
|
142
|
-
import { __privateUseListenTo as
|
|
117
|
+
import { __privateUseListenTo as useListenTo5, commandEndEvent as commandEndEvent4 } from "@elementor/editor-v1-adapters";
|
|
143
118
|
|
|
144
119
|
// src/sync/get-selected-elements.ts
|
|
145
120
|
function getSelectedElements() {
|
|
@@ -159,8 +134,8 @@ function getSelectedElements() {
|
|
|
159
134
|
|
|
160
135
|
// src/hooks/use-selected-element.ts
|
|
161
136
|
function useSelectedElement() {
|
|
162
|
-
const elements =
|
|
163
|
-
[
|
|
137
|
+
const elements = useListenTo5(
|
|
138
|
+
[commandEndEvent4("document/elements/select"), commandEndEvent4("document/elements/deselect")],
|
|
164
139
|
getSelectedElements
|
|
165
140
|
);
|
|
166
141
|
const [element] = elements;
|
|
@@ -172,10 +147,10 @@ function useSelectedElement() {
|
|
|
172
147
|
}
|
|
173
148
|
|
|
174
149
|
// src/hooks/use-parent-element.ts
|
|
175
|
-
import { __privateUseListenTo as
|
|
150
|
+
import { __privateUseListenTo as useListenTo6, commandEndEvent as commandEndEvent5 } from "@elementor/editor-v1-adapters";
|
|
176
151
|
function useParentElement(elementId) {
|
|
177
|
-
return
|
|
178
|
-
[
|
|
152
|
+
return useListenTo6(
|
|
153
|
+
[commandEndEvent5("document/elements/create")],
|
|
179
154
|
() => {
|
|
180
155
|
if (!elementId) {
|
|
181
156
|
return null;
|
|
@@ -251,8 +226,13 @@ function mutateElementStyles(elementId, mutator) {
|
|
|
251
226
|
if (!container) {
|
|
252
227
|
throw new ElementNotFoundError({ context: { elementId } });
|
|
253
228
|
}
|
|
229
|
+
const oldIds = Object.keys(container.model.get("styles") ?? {});
|
|
254
230
|
const styles = mutateStyles(container, mutator);
|
|
255
|
-
|
|
231
|
+
const newIds = Object.keys(styles);
|
|
232
|
+
clearRemovedClasses(container, {
|
|
233
|
+
oldIds,
|
|
234
|
+
newIds
|
|
235
|
+
});
|
|
256
236
|
dispatchChangeEvent();
|
|
257
237
|
return styles;
|
|
258
238
|
}
|
|
@@ -274,11 +254,11 @@ function removeEmptyVariants(style) {
|
|
|
274
254
|
function isStyleEmpty(style) {
|
|
275
255
|
return style.variants.length === 0;
|
|
276
256
|
}
|
|
277
|
-
function
|
|
278
|
-
const
|
|
257
|
+
function clearRemovedClasses(container, { oldIds, newIds }) {
|
|
258
|
+
const removedIds = oldIds.filter((id) => !newIds.includes(id));
|
|
279
259
|
const classesProps = structuredClone(getClassesProps(container));
|
|
280
260
|
classesProps.forEach(([, prop]) => {
|
|
281
|
-
prop.value = prop.value.filter((value) =>
|
|
261
|
+
prop.value = prop.value.filter((value) => !removedIds.includes(value));
|
|
282
262
|
});
|
|
283
263
|
updateElementSettings({
|
|
284
264
|
id: container.id,
|
|
@@ -328,32 +308,22 @@ function addStyleToClassesProp(elementId, classesProp, styleId) {
|
|
|
328
308
|
}
|
|
329
309
|
|
|
330
310
|
// src/styles/update-element-style.ts
|
|
331
|
-
import {
|
|
311
|
+
import { mergeProps } from "@elementor/editor-props";
|
|
312
|
+
import { getVariantByMeta } from "@elementor/editor-styles";
|
|
332
313
|
function updateElementStyle(args) {
|
|
333
314
|
mutateElementStyles(args.elementId, (styles) => {
|
|
334
315
|
const style = styles[args.styleId];
|
|
335
316
|
if (!style) {
|
|
336
317
|
throw new StyleNotFoundError({ context: { styleId: args.styleId } });
|
|
337
318
|
}
|
|
338
|
-
|
|
339
|
-
if (
|
|
340
|
-
variant =
|
|
341
|
-
style.variants.push(variant);
|
|
342
|
-
}
|
|
343
|
-
variant.props = mergeVariantProps(variant, args.props);
|
|
344
|
-
return styles;
|
|
345
|
-
});
|
|
346
|
-
}
|
|
347
|
-
function mergeVariantProps(variant, props) {
|
|
348
|
-
const finalProps = { ...variant.props };
|
|
349
|
-
Object.entries(props).forEach(([key, value]) => {
|
|
350
|
-
if (value === null || value === void 0) {
|
|
351
|
-
delete finalProps[key];
|
|
319
|
+
const variant = getVariantByMeta(style, args.meta);
|
|
320
|
+
if (variant) {
|
|
321
|
+
variant.props = mergeProps(variant.props, args.props);
|
|
352
322
|
} else {
|
|
353
|
-
|
|
323
|
+
style.variants.push({ meta: args.meta, props: args.props });
|
|
354
324
|
}
|
|
325
|
+
return styles;
|
|
355
326
|
});
|
|
356
|
-
return finalProps;
|
|
357
327
|
}
|
|
358
328
|
export {
|
|
359
329
|
ELEMENT_STYLE_CHANGE_EVENT,
|
|
@@ -364,10 +334,10 @@ export {
|
|
|
364
334
|
getSelectedElements,
|
|
365
335
|
getWidgetsCache,
|
|
366
336
|
isElementInContainer,
|
|
337
|
+
styleRerenderEvents,
|
|
367
338
|
updateElementSettings,
|
|
368
339
|
updateElementStyle,
|
|
369
340
|
useElementSetting,
|
|
370
|
-
useElementStyleProps,
|
|
371
341
|
useElementStyles,
|
|
372
342
|
useElementType,
|
|
373
343
|
useElementsDomRef,
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/hooks/use-elements-dom-ref.ts","../src/sync/get-current-document-container.ts","../src/hooks/use-element-setting.ts","../src/sync/get-container.ts","../src/sync/get-element-setting.ts","../src/hooks/use-element-style-props.ts","../src/styles/consts.ts","../src/sync/get-element-styles.ts","../src/hooks/use-element-styles.ts","../src/hooks/use-element-type.ts","../src/sync/get-widgets-cache.ts","../src/hooks/use-selected-element.ts","../src/sync/get-selected-elements.ts","../src/hooks/use-parent-element.ts","../src/sync/get-elements.ts","../src/sync/is-element-in-container.ts","../src/sync/update-element-settings.ts","../src/styles/create-element-style.ts","../src/styles/mutate-element-styles.ts","../src/styles/errors.ts","../src/styles/update-element-style.ts"],"sourcesContent":["import { __privateUseListenTo as useListenTo, windowEvent } from '@elementor/editor-v1-adapters';\n\nimport getCurrentDocumentContainer from '../sync/get-current-document-container';\n\nconst ELEMENTS_DATA_ATTR = 'data-atomic';\n\nexport function useElementsDomRef(): HTMLElement[] {\n\treturn useListenTo(\n\t\t[\n\t\t\twindowEvent( 'elementor/preview/atomic-widget/render' ),\n\t\t\twindowEvent( 'elementor/preview/atomic-widget/destroy' ),\n\t\t\twindowEvent( 'elementor/editor/element-rendered' ),\n\t\t\twindowEvent( 'elementor/editor/element-destroyed' ),\n\t\t],\n\t\t() => getElementsDom()\n\t);\n}\n\nfunction getElementsDom(): HTMLElement[] {\n\tconst root = getCurrentDocumentContainer();\n\n\tif ( ! root?.view ) {\n\t\treturn [];\n\t}\n\n\treturn [ ...root.view.el.querySelectorAll< HTMLElement >( `[${ ELEMENTS_DATA_ATTR }]` ) ];\n}\n","import { type ExtendedWindow } from './types';\n\nexport default function getCurrentDocumentContainer() {\n\tconst extendedWindow = window as unknown as ExtendedWindow;\n\n\treturn extendedWindow.elementor?.documents?.getCurrent?.()?.container ?? null;\n}\n","import { __privateUseListenTo as useListenTo, commandEndEvent } from '@elementor/editor-v1-adapters';\n\nimport { getElementSetting } from '../sync/get-element-setting';\nimport { type ElementID } from '../types';\n\nexport const useElementSetting = < TValue >( elementId: ElementID, settingKey: string ) => {\n\treturn useListenTo(\n\t\tcommandEndEvent( 'document/elements/set-settings' ),\n\t\t() => getElementSetting< TValue >( elementId, settingKey ),\n\t\t[ elementId, settingKey ]\n\t);\n};\n","import { type ExtendedWindow } from './types';\n\nexport default function getContainer( id: string ) {\n\tconst extendedWindow = window as unknown as ExtendedWindow;\n\tconst container = extendedWindow.elementor?.getContainer?.( id );\n\n\treturn container ?? null;\n}\n","import { type ElementID } from '../types';\nimport getContainer from './get-container';\n\nexport const getElementSetting = < TValue >( elementId: ElementID, settingKey: string ): TValue | null => {\n\tconst container = getContainer( elementId );\n\tconst value = container?.settings?.get( settingKey ) as TValue;\n\n\treturn value ?? null;\n};\n","import { type Props } from '@elementor/editor-props';\nimport { getVariantByMeta, type StyleDefinitionID, type StyleDefinitionVariant } from '@elementor/editor-styles';\nimport { __privateUseListenTo as useListenTo, windowEvent } from '@elementor/editor-v1-adapters';\n\nimport { ELEMENT_STYLE_CHANGE_EVENT } from '../styles/consts';\nimport { getElementStyles } from '../sync/get-element-styles';\nimport { type ElementID } from '../types';\n\nexport type UseElementStylePropsArgs< T extends Props > = {\n\telementID: ElementID;\n\tstyleDefID: StyleDefinitionID | null;\n\tmeta: StyleDefinitionVariant[ 'meta' ];\n\tpropNames: Array< keyof T & string >;\n};\n\ntype NullableObjectValues< T extends Props > = {\n\t[ K in keyof T ]: T[ K ] | null;\n};\n\nexport function useElementStyleProps< T extends Props >( {\n\telementID,\n\tstyleDefID,\n\tmeta,\n\tpropNames,\n}: UseElementStylePropsArgs< T > ): NullableObjectValues< T > | null {\n\treturn useListenTo(\n\t\twindowEvent( ELEMENT_STYLE_CHANGE_EVENT ),\n\t\t() => {\n\t\t\tif ( ! styleDefID ) {\n\t\t\t\treturn null;\n\t\t\t}\n\n\t\t\tconst styleDef = getElementStyles( elementID )?.[ styleDefID ];\n\n\t\t\tif ( ! styleDef ) {\n\t\t\t\treturn null;\n\t\t\t}\n\n\t\t\tconst variant = getVariantByMeta( styleDef, meta );\n\n\t\t\treturn propNames.reduce< Record< string, unknown > >( ( acc, key ) => {\n\t\t\t\tacc[ key ] = variant?.props[ key ] ?? null;\n\n\t\t\t\treturn acc;\n\t\t\t}, {} ) as NullableObjectValues< T >;\n\t\t},\n\t\t[ elementID, styleDefID, JSON.stringify( propNames ), meta ]\n\t);\n}\n","export const ELEMENT_STYLE_CHANGE_EVENT = 'elementor/editor-v2/editor-elements/style';\n","import { type StyleDefinition } from '@elementor/editor-styles';\n\nimport { type ElementID } from '../types';\nimport getContainer from './get-container';\n\nexport const getElementStyles = ( elementID: ElementID ): Record< string, StyleDefinition > | null => {\n\tconst container = getContainer( elementID );\n\n\treturn container?.model.get( 'styles' ) || null;\n};\n","import { __privateUseListenTo as useListenTo, windowEvent } from '@elementor/editor-v1-adapters';\n\nimport { ELEMENT_STYLE_CHANGE_EVENT } from '../styles/consts';\nimport { getElementStyles } from '../sync/get-element-styles';\nimport { type ElementID } from '../types';\n\nexport function useElementStyles( elementId: ElementID ) {\n\treturn useListenTo( windowEvent( ELEMENT_STYLE_CHANGE_EVENT ), () => getElementStyles( elementId ) ?? {}, [\n\t\telementId,\n\t] );\n}\n","import { __privateUseListenTo as useListenTo, commandEndEvent } from '@elementor/editor-v1-adapters';\n\nimport { getWidgetsCache } from '../sync/get-widgets-cache';\nimport { type ElementType } from '../types';\n\nexport function useElementType( type?: string ) {\n\treturn useListenTo(\n\t\tcommandEndEvent( 'editor/documents/load' ),\n\t\t(): ElementType | null => {\n\t\t\tif ( ! type ) {\n\t\t\t\treturn null;\n\t\t\t}\n\n\t\t\tconst widgetsCache = getWidgetsCache();\n\t\t\tconst elementType = widgetsCache?.[ type ];\n\n\t\t\tif ( ! elementType?.atomic_controls ) {\n\t\t\t\treturn null;\n\t\t\t}\n\n\t\t\tif ( ! elementType?.atomic_props_schema ) {\n\t\t\t\treturn null;\n\t\t\t}\n\n\t\t\treturn {\n\t\t\t\tkey: type,\n\t\t\t\tcontrols: elementType.atomic_controls,\n\t\t\t\tpropsSchema: elementType.atomic_props_schema,\n\t\t\t\ttitle: elementType.title,\n\t\t\t};\n\t\t},\n\t\t[ type ]\n\t);\n}\n","import { type ExtendedWindow } from './types';\n\nexport function getWidgetsCache() {\n\tconst extendedWindow = window as unknown as ExtendedWindow;\n\n\treturn extendedWindow?.elementor?.widgetsCache || null;\n}\n","import { __privateUseListenTo as useListenTo, commandEndEvent } from '@elementor/editor-v1-adapters';\n\nimport { getSelectedElements } from '../sync/get-selected-elements';\nimport { useElementType } from './use-element-type';\n\nexport function useSelectedElement() {\n\tconst elements = useListenTo(\n\t\t[ commandEndEvent( 'document/elements/select' ), commandEndEvent( 'document/elements/deselect' ) ],\n\t\tgetSelectedElements\n\t);\n\n\tconst [ element ] = elements;\n\n\tconst elementType = useElementType( element?.type );\n\n\tif ( elements.length !== 1 || ! elementType ) {\n\t\treturn { element: null, elementType: null };\n\t}\n\n\treturn { element, elementType };\n}\n","import { type Element } from '../types';\nimport { type ExtendedWindow } from './types';\n\nexport function getSelectedElements(): Element[] {\n\tconst extendedWindow = window as unknown as ExtendedWindow;\n\n\tconst selectedElements = extendedWindow.elementor?.selection?.getElements?.() ?? [];\n\n\treturn selectedElements.reduce< Element[] >( ( acc, el ) => {\n\t\tconst type = el.model.get( 'widgetType' ) || el.model.get( 'elType' );\n\n\t\tif ( type ) {\n\t\t\tacc.push( {\n\t\t\t\tid: el.model.get( 'id' ),\n\t\t\t\ttype,\n\t\t\t} );\n\t\t}\n\n\t\treturn acc;\n\t}, [] );\n}\n","import { __privateUseListenTo as useListenTo, commandEndEvent } from '@elementor/editor-v1-adapters';\n\nimport { type ExtendedWindow } from '../sync/types';\n\nexport function useParentElement( elementId: string | null ) {\n\treturn useListenTo(\n\t\t[ commandEndEvent( 'document/elements/create' ) ],\n\t\t() => {\n\t\t\tif ( ! elementId ) {\n\t\t\t\treturn null;\n\t\t\t}\n\n\t\t\tconst extendedWindow = window as unknown as ExtendedWindow;\n\t\t\tconst element = extendedWindow?.elementor?.getContainer?.( elementId );\n\t\t\tif ( ! element ) {\n\t\t\t\treturn null;\n\t\t\t}\n\n\t\t\treturn element.parent;\n\t\t},\n\t\t[ elementId ]\n\t);\n}\n","import { type ElementID } from '../types';\nimport getContainer from './get-container';\nimport getCurrentDocumentContainer from './get-current-document-container';\nimport { type V1Element } from './types';\n\nexport function getElements( root?: ElementID ): V1Element[] {\n\tconst container = root ? getContainer( root ) : getCurrentDocumentContainer();\n\n\tif ( ! container ) {\n\t\treturn [];\n\t}\n\n\tconst children = container.children?.flatMap( ( child ) => getElements( child.id ) ) ?? [];\n\n\treturn [ container, ...children ];\n}\n","import { type Element } from '../types';\nimport { type V1Element } from './types';\n\nexport const isElementInContainer = ( element: Element, container: V1Element ): boolean => {\n\tif ( container.model.get( 'id' ) === element.id && container.model.get( 'widgetType' ) === element.type ) {\n\t\treturn true;\n\t}\n\n\tif ( container.children && container.children.length > 0 ) {\n\t\treturn container.children.some( ( child ) => isElementInContainer( element, child ) );\n\t}\n\n\treturn false;\n};\n","import { type Props } from '@elementor/editor-props';\nimport { __privateRunCommandSync as runCommandSync } from '@elementor/editor-v1-adapters';\n\nimport { type ElementID } from '../types';\nimport getContainer from './get-container';\n\nexport type UpdateElementSettingsArgs = {\n\tid: ElementID;\n\tprops: Props;\n\twithHistory?: boolean;\n};\n\nexport const updateElementSettings = ( { id, props, withHistory = true }: UpdateElementSettingsArgs ) => {\n\tconst container = getContainer( id );\n\n\tconst args = {\n\t\tcontainer,\n\t\tsettings: { ...props },\n\t};\n\n\tif ( withHistory ) {\n\t\trunCommandSync( 'document/elements/settings', args );\n\t} else {\n\t\trunCommandSync( 'document/elements/set-settings', args, { internal: true } );\n\t}\n};\n","import { classesPropTypeUtil } from '@elementor/editor-props';\nimport { generateId, type StyleDefinition, type StyleDefinitionVariant } from '@elementor/editor-styles';\n\nimport { getElementSetting } from '../sync/get-element-setting';\nimport { updateElementSettings } from '../sync/update-element-settings';\nimport { type ElementID } from '../types';\nimport { mutateElementStyles } from './mutate-element-styles';\n\nexport type CreateElementStyleArgs = {\n\telementId: ElementID;\n\tclassesProp: string;\n\tlabel: string;\n\tmeta: StyleDefinitionVariant[ 'meta' ];\n\tprops: StyleDefinitionVariant[ 'props' ];\n};\n\nexport function createElementStyle( { elementId, classesProp, label, meta, props }: CreateElementStyleArgs ) {\n\tmutateElementStyles( elementId, ( styles ) => {\n\t\tconst id = generateId( `e-${ elementId }-`, Object.keys( styles ) );\n\n\t\tstyles[ id ] = {\n\t\t\tid,\n\t\t\tlabel,\n\t\t\ttype: 'class',\n\t\t\tvariants: [ { meta, props } ],\n\t\t} satisfies StyleDefinition;\n\n\t\taddStyleToClassesProp( elementId, classesProp, id );\n\n\t\treturn styles;\n\t} );\n}\n\nfunction addStyleToClassesProp( elementId: ElementID, classesProp: string, styleId: string ) {\n\tconst base = getElementSetting( elementId, classesProp );\n\n\tconst classesPropValue = classesPropTypeUtil.create(\n\t\t( prev ) => {\n\t\t\treturn [ ...( prev ?? [] ), styleId ];\n\t\t},\n\t\t{ base }\n\t);\n\n\tupdateElementSettings( {\n\t\tid: elementId,\n\t\tprops: {\n\t\t\t[ classesProp ]: classesPropValue,\n\t\t},\n\t\twithHistory: false,\n\t} );\n}\n","import { classesPropTypeUtil, type ClassesPropValue } from '@elementor/editor-props';\nimport { type StyleDefinition, type StyleDefinitionsMap } from '@elementor/editor-styles';\n\nimport getContainer from '../sync/get-container';\nimport { type V1Element } from '../sync/types';\nimport { updateElementSettings } from '../sync/update-element-settings';\nimport { type ElementID } from '../types';\nimport { ELEMENT_STYLE_CHANGE_EVENT } from './consts';\nimport { ElementNotFoundError } from './errors';\n\nexport type Mutator = ( styles: StyleDefinitionsMap ) => StyleDefinitionsMap;\n\nexport function mutateElementStyles( elementId: ElementID, mutator: Mutator ) {\n\tconst container = getContainer( elementId );\n\n\tif ( ! container ) {\n\t\tthrow new ElementNotFoundError( { context: { elementId } } );\n\t}\n\n\tconst styles = mutateStyles( container, mutator );\n\n\tremoveNonExistingClasses( container, styles );\n\n\tdispatchChangeEvent();\n\n\treturn styles;\n}\n\nfunction mutateStyles( container: V1Element, mutator: Mutator ) {\n\tconst styles: StyleDefinitionsMap = structuredClone( container.model.get( 'styles' ) ) ?? {};\n\n\tconst entries = Object.entries( mutator( styles ) )\n\t\t.map( ( [ styleId, style ] ) => {\n\t\t\tstyle.variants = removeEmptyVariants( style );\n\n\t\t\treturn [ styleId, style ] as const;\n\t\t} )\n\t\t.filter( ( [ , style ] ) => {\n\t\t\treturn ! isStyleEmpty( style );\n\t\t} );\n\n\tconst mutatedStyles = Object.fromEntries( entries );\n\n\tcontainer.model.set( 'styles', mutatedStyles );\n\n\treturn mutatedStyles;\n}\n\nfunction removeEmptyVariants( style: StyleDefinition ) {\n\treturn style.variants.filter( ( { props } ) => Object.keys( props ).length > 0 );\n}\n\nfunction isStyleEmpty( style: StyleDefinition ) {\n\treturn style.variants.length === 0;\n}\n\nfunction removeNonExistingClasses( container: V1Element, styles: StyleDefinitionsMap ) {\n\tconst existingStylesIds = Object.keys( styles );\n\tconst classesProps = structuredClone( getClassesProps( container ) );\n\n\tclassesProps.forEach( ( [ , prop ] ) => {\n\t\tprop.value = prop.value.filter( ( value ) => existingStylesIds.includes( value ) );\n\t} );\n\n\tupdateElementSettings( {\n\t\tid: container.id,\n\t\tprops: Object.fromEntries( classesProps ),\n\t\twithHistory: false,\n\t} );\n}\n\nfunction getClassesProps( container: V1Element ) {\n\treturn Object.entries( container.settings.toJSON() ).filter( ( prop ): prop is [ string, ClassesPropValue ] => {\n\t\tconst [ , value ] = prop;\n\n\t\treturn classesPropTypeUtil.isValid( value );\n\t} );\n}\n\nfunction dispatchChangeEvent() {\n\twindow.dispatchEvent( new CustomEvent( ELEMENT_STYLE_CHANGE_EVENT ) );\n}\n","import { createError } from '@elementor/utils';\n\nexport const ElementNotFoundError = createError< { elementId: string } >( {\n\tcode: 'element_not_found',\n\tmessage: 'Element not found.',\n} );\n\nexport const StyleNotFoundError = createError< { styleId: string } >( {\n\tcode: 'style_not_found',\n\tmessage: 'Style not found.',\n} );\n","import { type Props } from '@elementor/editor-props';\nimport { getVariantByMeta, type StyleDefinition, type StyleDefinitionVariant } from '@elementor/editor-styles';\n\nimport { type ElementID } from '../types';\nimport { StyleNotFoundError } from './errors';\nimport { mutateElementStyles } from './mutate-element-styles';\n\nexport type UpdateElementStyleArgs = {\n\telementId: ElementID;\n\tstyleId: StyleDefinition[ 'id' ];\n\tmeta: StyleDefinitionVariant[ 'meta' ];\n\tprops: StyleDefinitionVariant[ 'props' ];\n};\n\nexport function updateElementStyle( args: UpdateElementStyleArgs ) {\n\tmutateElementStyles( args.elementId, ( styles ) => {\n\t\tconst style = styles[ args.styleId ];\n\n\t\tif ( ! style ) {\n\t\t\tthrow new StyleNotFoundError( { context: { styleId: args.styleId } } );\n\t\t}\n\n\t\tlet variant = getVariantByMeta( style, args.meta );\n\n\t\tif ( ! variant ) {\n\t\t\tvariant = { meta: args.meta, props: {} };\n\n\t\t\tstyle.variants.push( variant );\n\t\t}\n\n\t\tvariant.props = mergeVariantProps( variant, args.props );\n\n\t\treturn styles;\n\t} );\n}\n\nfunction mergeVariantProps( variant: StyleDefinitionVariant, props: Props ) {\n\tconst finalProps = { ...variant.props };\n\n\tObject.entries( props ).forEach( ( [ key, value ] ) => {\n\t\tif ( value === null || value === undefined ) {\n\t\t\t// eslint-disable-next-line @typescript-eslint/no-dynamic-delete\n\t\t\tdelete finalProps[ key ];\n\t\t} else {\n\t\t\tfinalProps[ key ] = value;\n\t\t}\n\t} );\n\n\treturn finalProps;\n}\n"],"mappings":";AAAA,SAAS,wBAAwB,aAAa,mBAAmB;;;ACElD,SAAR,8BAA+C;AACrD,QAAM,iBAAiB;AAEvB,SAAO,eAAe,WAAW,WAAW,aAAa,GAAG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MAAM,OAAO,CAAC,EAAE;AAEvC,YAAM,SAAS,KAAM,OAAQ;AAAA,IAC9B;AAEA,YAAQ,QAAQ,kBAAmB,SAAS,KAAK,KAAM;AAEvD,WAAO;AAAA,EACR,CAAE;AACH;AAEA,SAAS,kBAAmB,SAAiC,OAAe;AAC3E,QAAM,aAAa,EAAE,GAAG,QAAQ,MAAM;AAEtC,SAAO,QAAS,KAAM,EAAE,QAAS,CAAE,CAAE,KAAK,KAAM,MAAO;AACtD,QAAK,UAAU,QAAQ,UAAU,QAAY;AAE5C,aAAO,WAAY,GAAI;AAAA,IACxB,OAAO;AACN,iBAAY,GAAI,IAAI;AAAA,IACrB;AAAA,EACD,CAAE;AAEF,SAAO;AACR;","names":["useListenTo","useListenTo","useListenTo","windowEvent","useListenTo","windowEvent","useListenTo","windowEvent","useListenTo","windowEvent","useListenTo","commandEndEvent","useListenTo","commandEndEvent","useListenTo","commandEndEvent","useListenTo","commandEndEvent","useListenTo","commandEndEvent","classesPropTypeUtil","classesPropTypeUtil","getVariantByMeta","getVariantByMeta"]}
|
|
1
|
+
{"version":3,"sources":["../src/hooks/use-elements-dom-ref.ts","../src/sync/get-current-document-container.ts","../src/hooks/use-element-setting.ts","../src/sync/get-container.ts","../src/sync/get-element-setting.ts","../src/hooks/use-element-styles.ts","../src/styles/consts.ts","../src/sync/get-element-styles.ts","../src/hooks/use-element-type.ts","../src/sync/get-widgets-cache.ts","../src/hooks/use-selected-element.ts","../src/sync/get-selected-elements.ts","../src/hooks/use-parent-element.ts","../src/sync/get-elements.ts","../src/sync/is-element-in-container.ts","../src/sync/update-element-settings.ts","../src/styles/create-element-style.ts","../src/styles/mutate-element-styles.ts","../src/styles/errors.ts","../src/styles/update-element-style.ts"],"sourcesContent":["import { __privateUseListenTo as useListenTo, windowEvent } from '@elementor/editor-v1-adapters';\n\nimport getCurrentDocumentContainer from '../sync/get-current-document-container';\n\nconst ELEMENTS_DATA_ATTR = 'data-atomic';\n\nexport function useElementsDomRef(): HTMLElement[] {\n\treturn useListenTo(\n\t\t[\n\t\t\twindowEvent( 'elementor/preview/atomic-widget/render' ),\n\t\t\twindowEvent( 'elementor/preview/atomic-widget/destroy' ),\n\t\t\twindowEvent( 'elementor/editor/element-rendered' ),\n\t\t\twindowEvent( 'elementor/editor/element-destroyed' ),\n\t\t],\n\t\t() => getElementsDom()\n\t);\n}\n\nfunction getElementsDom(): HTMLElement[] {\n\tconst root = getCurrentDocumentContainer();\n\n\tif ( ! root?.view ) {\n\t\treturn [];\n\t}\n\n\treturn [ ...root.view.el.querySelectorAll< HTMLElement >( `[${ ELEMENTS_DATA_ATTR }]` ) ];\n}\n","import { type ExtendedWindow } from './types';\n\nexport default function getCurrentDocumentContainer() {\n\tconst extendedWindow = window as unknown as ExtendedWindow;\n\n\treturn extendedWindow.elementor?.documents?.getCurrent?.()?.container ?? null;\n}\n","import { __privateUseListenTo as useListenTo, commandEndEvent } from '@elementor/editor-v1-adapters';\n\nimport { getElementSetting } from '../sync/get-element-setting';\nimport { type ElementID } from '../types';\n\nexport const useElementSetting = < TValue >( elementId: ElementID, settingKey: string ) => {\n\treturn useListenTo(\n\t\tcommandEndEvent( 'document/elements/set-settings' ),\n\t\t() => getElementSetting< TValue >( elementId, settingKey ),\n\t\t[ elementId, settingKey ]\n\t);\n};\n","import { type ExtendedWindow } from './types';\n\nexport default function getContainer( id: string ) {\n\tconst extendedWindow = window as unknown as ExtendedWindow;\n\tconst container = extendedWindow.elementor?.getContainer?.( id );\n\n\treturn container ?? null;\n}\n","import { type ElementID } from '../types';\nimport getContainer from './get-container';\n\nexport const getElementSetting = < TValue >( elementId: ElementID, settingKey: string ): TValue | null => {\n\tconst container = getContainer( elementId );\n\tconst value = container?.settings?.get( settingKey ) as TValue;\n\n\treturn value ?? null;\n};\n","import { __privateUseListenTo as useListenTo } from '@elementor/editor-v1-adapters';\n\nimport { styleRerenderEvents } from '../styles/consts';\nimport { getElementStyles } from '../sync/get-element-styles';\nimport { type ElementID } from '../types';\n\nexport function useElementStyles( elementId: ElementID ) {\n\treturn useListenTo( styleRerenderEvents, () => getElementStyles( elementId ) ?? {}, [ elementId ] );\n}\n","import { commandEndEvent, windowEvent } from '@elementor/editor-v1-adapters';\n\nexport const ELEMENT_STYLE_CHANGE_EVENT = 'elementor/editor-v2/editor-elements/style';\n\nexport const styleRerenderEvents = [\n\tcommandEndEvent( 'document/elements/create' ),\n\tcommandEndEvent( 'editor/documents/attach-preview' ),\n\twindowEvent( ELEMENT_STYLE_CHANGE_EVENT ),\n];\n","import { type StyleDefinition } from '@elementor/editor-styles';\n\nimport { type ElementID } from '../types';\nimport getContainer from './get-container';\n\nexport const getElementStyles = ( elementID: ElementID ): Record< string, StyleDefinition > | null => {\n\tconst container = getContainer( elementID );\n\n\treturn container?.model.get( 'styles' ) || null;\n};\n","import { __privateUseListenTo as useListenTo, commandEndEvent } from '@elementor/editor-v1-adapters';\n\nimport { getWidgetsCache } from '../sync/get-widgets-cache';\nimport { type ElementType } from '../types';\n\nexport function useElementType( type?: string ) {\n\treturn useListenTo(\n\t\tcommandEndEvent( 'editor/documents/load' ),\n\t\t(): ElementType | null => {\n\t\t\tif ( ! type ) {\n\t\t\t\treturn null;\n\t\t\t}\n\n\t\t\tconst widgetsCache = getWidgetsCache();\n\t\t\tconst elementType = widgetsCache?.[ type ];\n\n\t\t\tif ( ! elementType?.atomic_controls ) {\n\t\t\t\treturn null;\n\t\t\t}\n\n\t\t\tif ( ! elementType?.atomic_props_schema ) {\n\t\t\t\treturn null;\n\t\t\t}\n\n\t\t\treturn {\n\t\t\t\tkey: type,\n\t\t\t\tcontrols: elementType.atomic_controls,\n\t\t\t\tpropsSchema: elementType.atomic_props_schema,\n\t\t\t\ttitle: elementType.title,\n\t\t\t};\n\t\t},\n\t\t[ type ]\n\t);\n}\n","import { type ExtendedWindow } from './types';\n\nexport function getWidgetsCache() {\n\tconst extendedWindow = window as unknown as ExtendedWindow;\n\n\treturn extendedWindow?.elementor?.widgetsCache || null;\n}\n","import { __privateUseListenTo as useListenTo, commandEndEvent } from '@elementor/editor-v1-adapters';\n\nimport { getSelectedElements } from '../sync/get-selected-elements';\nimport { useElementType } from './use-element-type';\n\nexport function useSelectedElement() {\n\tconst elements = useListenTo(\n\t\t[ commandEndEvent( 'document/elements/select' ), commandEndEvent( 'document/elements/deselect' ) ],\n\t\tgetSelectedElements\n\t);\n\n\tconst [ element ] = elements;\n\n\tconst elementType = useElementType( element?.type );\n\n\tif ( elements.length !== 1 || ! elementType ) {\n\t\treturn { element: null, elementType: null };\n\t}\n\n\treturn { element, elementType };\n}\n","import { type Element } from '../types';\nimport { type ExtendedWindow } from './types';\n\nexport function getSelectedElements(): Element[] {\n\tconst extendedWindow = window as unknown as ExtendedWindow;\n\n\tconst selectedElements = extendedWindow.elementor?.selection?.getElements?.() ?? [];\n\n\treturn selectedElements.reduce< Element[] >( ( acc, el ) => {\n\t\tconst type = el.model.get( 'widgetType' ) || el.model.get( 'elType' );\n\n\t\tif ( type ) {\n\t\t\tacc.push( {\n\t\t\t\tid: el.model.get( 'id' ),\n\t\t\t\ttype,\n\t\t\t} );\n\t\t}\n\n\t\treturn acc;\n\t}, [] );\n}\n","import { __privateUseListenTo as useListenTo, commandEndEvent } from '@elementor/editor-v1-adapters';\n\nimport { type ExtendedWindow } from '../sync/types';\n\nexport function useParentElement( elementId: string | null ) {\n\treturn useListenTo(\n\t\t[ commandEndEvent( 'document/elements/create' ) ],\n\t\t() => {\n\t\t\tif ( ! elementId ) {\n\t\t\t\treturn null;\n\t\t\t}\n\n\t\t\tconst extendedWindow = window as unknown as ExtendedWindow;\n\t\t\tconst element = extendedWindow?.elementor?.getContainer?.( elementId );\n\t\t\tif ( ! element ) {\n\t\t\t\treturn null;\n\t\t\t}\n\n\t\t\treturn element.parent;\n\t\t},\n\t\t[ elementId ]\n\t);\n}\n","import { type ElementID } from '../types';\nimport getContainer from './get-container';\nimport getCurrentDocumentContainer from './get-current-document-container';\nimport { type V1Element } from './types';\n\nexport function getElements( root?: ElementID ): V1Element[] {\n\tconst container = root ? getContainer( root ) : getCurrentDocumentContainer();\n\n\tif ( ! container ) {\n\t\treturn [];\n\t}\n\n\tconst children = container.children?.flatMap( ( child ) => getElements( child.id ) ) ?? [];\n\n\treturn [ container, ...children ];\n}\n","import { type Element } from '../types';\nimport { type V1Element } from './types';\n\nexport const isElementInContainer = ( element: Element, container: V1Element ): boolean => {\n\tif ( container.model.get( 'id' ) === element.id && container.model.get( 'widgetType' ) === element.type ) {\n\t\treturn true;\n\t}\n\n\tif ( container.children && container.children.length > 0 ) {\n\t\treturn container.children.some( ( child ) => isElementInContainer( element, child ) );\n\t}\n\n\treturn false;\n};\n","import { type Props } from '@elementor/editor-props';\nimport { __privateRunCommandSync as runCommandSync } from '@elementor/editor-v1-adapters';\n\nimport { type ElementID } from '../types';\nimport getContainer from './get-container';\n\nexport type UpdateElementSettingsArgs = {\n\tid: ElementID;\n\tprops: Props;\n\twithHistory?: boolean;\n};\n\nexport const updateElementSettings = ( { id, props, withHistory = true }: UpdateElementSettingsArgs ) => {\n\tconst container = getContainer( id );\n\n\tconst args = {\n\t\tcontainer,\n\t\tsettings: { ...props },\n\t};\n\n\tif ( withHistory ) {\n\t\trunCommandSync( 'document/elements/settings', args );\n\t} else {\n\t\trunCommandSync( 'document/elements/set-settings', args, { internal: true } );\n\t}\n};\n","import { classesPropTypeUtil } from '@elementor/editor-props';\nimport { generateId, type StyleDefinition, type StyleDefinitionVariant } from '@elementor/editor-styles';\n\nimport { getElementSetting } from '../sync/get-element-setting';\nimport { updateElementSettings } from '../sync/update-element-settings';\nimport { type ElementID } from '../types';\nimport { mutateElementStyles } from './mutate-element-styles';\n\nexport type CreateElementStyleArgs = {\n\telementId: ElementID;\n\tclassesProp: string;\n\tlabel: string;\n\tmeta: StyleDefinitionVariant[ 'meta' ];\n\tprops: StyleDefinitionVariant[ 'props' ];\n};\n\nexport function createElementStyle( { elementId, classesProp, label, meta, props }: CreateElementStyleArgs ) {\n\tmutateElementStyles( elementId, ( styles ) => {\n\t\tconst id = generateId( `e-${ elementId }-`, Object.keys( styles ) );\n\n\t\tstyles[ id ] = {\n\t\t\tid,\n\t\t\tlabel,\n\t\t\ttype: 'class',\n\t\t\tvariants: [ { meta, props } ],\n\t\t} satisfies StyleDefinition;\n\n\t\taddStyleToClassesProp( elementId, classesProp, id );\n\n\t\treturn styles;\n\t} );\n}\n\nfunction addStyleToClassesProp( elementId: ElementID, classesProp: string, styleId: string ) {\n\tconst base = getElementSetting( elementId, classesProp );\n\n\tconst classesPropValue = classesPropTypeUtil.create(\n\t\t( prev ) => {\n\t\t\treturn [ ...( prev ?? [] ), styleId ];\n\t\t},\n\t\t{ base }\n\t);\n\n\tupdateElementSettings( {\n\t\tid: elementId,\n\t\tprops: {\n\t\t\t[ classesProp ]: classesPropValue,\n\t\t},\n\t\twithHistory: false,\n\t} );\n}\n","import { classesPropTypeUtil, type ClassesPropValue } from '@elementor/editor-props';\nimport { type StyleDefinition, type StyleDefinitionsMap } from '@elementor/editor-styles';\n\nimport getContainer from '../sync/get-container';\nimport { type V1Element } from '../sync/types';\nimport { updateElementSettings } from '../sync/update-element-settings';\nimport { type ElementID } from '../types';\nimport { ELEMENT_STYLE_CHANGE_EVENT } from './consts';\nimport { ElementNotFoundError } from './errors';\n\nexport type Mutator = ( styles: StyleDefinitionsMap ) => StyleDefinitionsMap;\n\nexport function mutateElementStyles( elementId: ElementID, mutator: Mutator ) {\n\tconst container = getContainer( elementId );\n\n\tif ( ! container ) {\n\t\tthrow new ElementNotFoundError( { context: { elementId } } );\n\t}\n\n\tconst oldIds = Object.keys( container.model.get( 'styles' ) ?? {} );\n\n\tconst styles = mutateStyles( container, mutator );\n\n\tconst newIds = Object.keys( styles );\n\n\tclearRemovedClasses( container, {\n\t\toldIds,\n\t\tnewIds,\n\t} );\n\n\tdispatchChangeEvent();\n\n\treturn styles;\n}\n\nfunction mutateStyles( container: V1Element, mutator: Mutator ) {\n\tconst styles: StyleDefinitionsMap = structuredClone( container.model.get( 'styles' ) ) ?? {};\n\n\tconst entries = Object.entries( mutator( styles ) )\n\t\t.map( ( [ styleId, style ] ) => {\n\t\t\tstyle.variants = removeEmptyVariants( style );\n\n\t\t\treturn [ styleId, style ] as const;\n\t\t} )\n\t\t.filter( ( [ , style ] ) => {\n\t\t\treturn ! isStyleEmpty( style );\n\t\t} );\n\n\tconst mutatedStyles = Object.fromEntries( entries );\n\n\tcontainer.model.set( 'styles', mutatedStyles );\n\n\treturn mutatedStyles;\n}\n\nfunction removeEmptyVariants( style: StyleDefinition ) {\n\treturn style.variants.filter( ( { props } ) => Object.keys( props ).length > 0 );\n}\n\nfunction isStyleEmpty( style: StyleDefinition ) {\n\treturn style.variants.length === 0;\n}\n\nfunction clearRemovedClasses( container: V1Element, { oldIds, newIds }: { oldIds: string[]; newIds: string[] } ) {\n\tconst removedIds = oldIds.filter( ( id ) => ! newIds.includes( id ) );\n\tconst classesProps = structuredClone( getClassesProps( container ) );\n\n\tclassesProps.forEach( ( [ , prop ] ) => {\n\t\tprop.value = prop.value.filter( ( value ) => ! removedIds.includes( value ) );\n\t} );\n\n\tupdateElementSettings( {\n\t\tid: container.id,\n\t\tprops: Object.fromEntries( classesProps ),\n\t\twithHistory: false,\n\t} );\n}\n\nfunction getClassesProps( container: V1Element ) {\n\treturn Object.entries( container.settings.toJSON() ).filter( ( prop ): prop is [ string, ClassesPropValue ] => {\n\t\tconst [ , value ] = prop;\n\n\t\treturn classesPropTypeUtil.isValid( value );\n\t} );\n}\n\nfunction dispatchChangeEvent() {\n\twindow.dispatchEvent( new CustomEvent( ELEMENT_STYLE_CHANGE_EVENT ) );\n}\n","import { createError } from '@elementor/utils';\n\nexport const ElementNotFoundError = createError< { elementId: string } >( {\n\tcode: 'element_not_found',\n\tmessage: 'Element not found.',\n} );\n\nexport const StyleNotFoundError = createError< { styleId: string } >( {\n\tcode: 'style_not_found',\n\tmessage: 'Style not found.',\n} );\n","import { mergeProps } from '@elementor/editor-props';\nimport { getVariantByMeta, type StyleDefinition, type StyleDefinitionVariant } from '@elementor/editor-styles';\n\nimport { type ElementID } from '../types';\nimport { StyleNotFoundError } from './errors';\nimport { mutateElementStyles } from './mutate-element-styles';\n\nexport type UpdateElementStyleArgs = {\n\telementId: ElementID;\n\tstyleId: StyleDefinition[ 'id' ];\n\tmeta: StyleDefinitionVariant[ 'meta' ];\n\tprops: StyleDefinitionVariant[ 'props' ];\n};\n\nexport function updateElementStyle( args: UpdateElementStyleArgs ) {\n\tmutateElementStyles( args.elementId, ( styles ) => {\n\t\tconst style = styles[ args.styleId ];\n\n\t\tif ( ! style ) {\n\t\t\tthrow new StyleNotFoundError( { context: { styleId: args.styleId } } );\n\t\t}\n\n\t\tconst variant = getVariantByMeta( style, args.meta );\n\n\t\tif ( variant ) {\n\t\t\tvariant.props = mergeProps( variant.props, args.props );\n\t\t} else {\n\t\t\tstyle.variants.push( { meta: args.meta, props: args.props } );\n\t\t}\n\n\t\treturn styles;\n\t} );\n}\n"],"mappings":";AAAA,SAAS,wBAAwB,aAAa,mBAAmB;;;ACElD,SAAR,8BAA+C;AACrD,QAAM,iBAAiB;AAEvB,SAAO,eAAe,WAAW,WAAW,aAAa,GAAG,aAAa;AAC1E;;;ADFA,IAAM,qBAAqB;AAEpB,SAAS,oBAAmC;AAClD,SAAO;AAAA,IACN;AAAA,MACC,YAAa,wCAAyC;AAAA,MACtD,YAAa,yCAA0C;AAAA,MACvD,YAAa,mCAAoC;AAAA,MACjD,YAAa,oCAAqC;AAAA,IACnD;AAAA,IACA,MAAM,eAAe;AAAA,EACtB;AACD;AAEA,SAAS,iBAAgC;AACxC,QAAM,OAAO,4BAA4B;AAEzC,MAAK,CAAE,MAAM,MAAO;AACnB,WAAO,CAAC;AAAA,EACT;AAEA,SAAO,CAAE,GAAG,KAAK,KAAK,GAAG,iBAAiC,IAAK,kBAAmB,GAAI,CAAE;AACzF;;;AE1BA,SAAS,wBAAwBA,cAAa,uBAAuB;;;ACEtD,SAAR,aAA+B,IAAa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,IAAI;AAAA,IACJ,OAAO;AAAA,MACN,CAAE,WAAY,GAAG;AAAA,IAClB;AAAA,IACA,aAAa;AAAA,EACd,CAAE;AACH;;;AGlDA,SAAS,kBAAkB;AAC3B,SAAS,wBAA2E;AAa7E,SAAS,mBAAoB,MAA+B;AAClE,sBAAqB,KAAK,WAAW,CAAE,WAAY;AAClD,UAAM,QAAQ,OAAQ,KAAK,OAAQ;AAEnC,QAAK,CAAE,OAAQ;AACd,YAAM,IAAI,mBAAoB,EAAE,SAAS,EAAE,SAAS,KAAK,QAAQ,EAAE,CAAE;AAAA,IACtE;AAEA,UAAM,UAAU,iBAAkB,OAAO,KAAK,IAAK;AAEnD,QAAK,SAAU;AACd,cAAQ,QAAQ,WAAY,QAAQ,OAAO,KAAK,KAAM;AAAA,IACvD,OAAO;AACN,YAAM,SAAS,KAAM,EAAE,MAAM,KAAK,MAAM,OAAO,KAAK,MAAM,CAAE;AAAA,IAC7D;AAEA,WAAO;AAAA,EACR,CAAE;AACH;","names":["useListenTo","useListenTo","useListenTo","commandEndEvent","windowEvent","useListenTo","useListenTo","commandEndEvent","useListenTo","commandEndEvent","useListenTo","commandEndEvent","useListenTo","commandEndEvent","useListenTo","commandEndEvent","classesPropTypeUtil","classesPropTypeUtil"]}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@elementor/editor-elements",
|
|
3
3
|
"description": "This package contains the elements model for the Elementor editor",
|
|
4
|
-
"version": "0.5.
|
|
4
|
+
"version": "0.5.1",
|
|
5
5
|
"private": false,
|
|
6
6
|
"author": "Elementor Team",
|
|
7
7
|
"homepage": "https://elementor.com/",
|
|
@@ -41,8 +41,8 @@
|
|
|
41
41
|
},
|
|
42
42
|
"dependencies": {
|
|
43
43
|
"@elementor/editor-props": "0.8.0",
|
|
44
|
-
"@elementor/editor-styles": "0.5.
|
|
45
|
-
"@elementor/editor-v1-adapters": "0.9.
|
|
44
|
+
"@elementor/editor-styles": "0.5.4",
|
|
45
|
+
"@elementor/editor-v1-adapters": "0.9.1",
|
|
46
46
|
"@elementor/utils": "0.3.0"
|
|
47
47
|
},
|
|
48
48
|
"peerDependencies": {
|
|
@@ -1,11 +1,9 @@
|
|
|
1
|
-
import { __privateUseListenTo as useListenTo
|
|
1
|
+
import { __privateUseListenTo as useListenTo } from '@elementor/editor-v1-adapters';
|
|
2
2
|
|
|
3
|
-
import {
|
|
3
|
+
import { styleRerenderEvents } from '../styles/consts';
|
|
4
4
|
import { getElementStyles } from '../sync/get-element-styles';
|
|
5
5
|
import { type ElementID } from '../types';
|
|
6
6
|
|
|
7
7
|
export function useElementStyles( elementId: ElementID ) {
|
|
8
|
-
return useListenTo(
|
|
9
|
-
elementId,
|
|
10
|
-
] );
|
|
8
|
+
return useListenTo( styleRerenderEvents, () => getElementStyles( elementId ) ?? {}, [ elementId ] );
|
|
11
9
|
}
|
package/src/index.ts
CHANGED
|
@@ -5,7 +5,6 @@ export type { V1Element, V1ElementModelProps, V1ElementSettingsProps } from './s
|
|
|
5
5
|
// hooks
|
|
6
6
|
export { useElementsDomRef } from './hooks/use-elements-dom-ref';
|
|
7
7
|
export { useElementSetting } from './hooks/use-element-setting';
|
|
8
|
-
export { useElementStyleProps } from './hooks/use-element-style-props';
|
|
9
8
|
export { useElementStyles } from './hooks/use-element-styles';
|
|
10
9
|
export { useElementType } from './hooks/use-element-type';
|
|
11
10
|
export { useSelectedElement } from './hooks/use-selected-element';
|
|
@@ -20,6 +19,6 @@ export { getWidgetsCache } from './sync/get-widgets-cache';
|
|
|
20
19
|
export { isElementInContainer } from './sync/is-element-in-container';
|
|
21
20
|
export { updateElementSettings, type UpdateElementSettingsArgs } from './sync/update-element-settings';
|
|
22
21
|
|
|
23
|
-
export { ELEMENT_STYLE_CHANGE_EVENT } from './styles/consts';
|
|
22
|
+
export { ELEMENT_STYLE_CHANGE_EVENT, styleRerenderEvents } from './styles/consts';
|
|
24
23
|
export { createElementStyle, type CreateElementStyleArgs } from './styles/create-element-style';
|
|
25
24
|
export { updateElementStyle, type UpdateElementStyleArgs } from './styles/update-element-style';
|
package/src/styles/consts.ts
CHANGED
|
@@ -1 +1,9 @@
|
|
|
1
|
+
import { commandEndEvent, windowEvent } from '@elementor/editor-v1-adapters';
|
|
2
|
+
|
|
1
3
|
export const ELEMENT_STYLE_CHANGE_EVENT = 'elementor/editor-v2/editor-elements/style';
|
|
4
|
+
|
|
5
|
+
export const styleRerenderEvents = [
|
|
6
|
+
commandEndEvent( 'document/elements/create' ),
|
|
7
|
+
commandEndEvent( 'editor/documents/attach-preview' ),
|
|
8
|
+
windowEvent( ELEMENT_STYLE_CHANGE_EVENT ),
|
|
9
|
+
];
|
|
@@ -17,9 +17,16 @@ export function mutateElementStyles( elementId: ElementID, mutator: Mutator ) {
|
|
|
17
17
|
throw new ElementNotFoundError( { context: { elementId } } );
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
+
const oldIds = Object.keys( container.model.get( 'styles' ) ?? {} );
|
|
21
|
+
|
|
20
22
|
const styles = mutateStyles( container, mutator );
|
|
21
23
|
|
|
22
|
-
|
|
24
|
+
const newIds = Object.keys( styles );
|
|
25
|
+
|
|
26
|
+
clearRemovedClasses( container, {
|
|
27
|
+
oldIds,
|
|
28
|
+
newIds,
|
|
29
|
+
} );
|
|
23
30
|
|
|
24
31
|
dispatchChangeEvent();
|
|
25
32
|
|
|
@@ -54,12 +61,12 @@ function isStyleEmpty( style: StyleDefinition ) {
|
|
|
54
61
|
return style.variants.length === 0;
|
|
55
62
|
}
|
|
56
63
|
|
|
57
|
-
function
|
|
58
|
-
const
|
|
64
|
+
function clearRemovedClasses( container: V1Element, { oldIds, newIds }: { oldIds: string[]; newIds: string[] } ) {
|
|
65
|
+
const removedIds = oldIds.filter( ( id ) => ! newIds.includes( id ) );
|
|
59
66
|
const classesProps = structuredClone( getClassesProps( container ) );
|
|
60
67
|
|
|
61
68
|
classesProps.forEach( ( [ , prop ] ) => {
|
|
62
|
-
prop.value = prop.value.filter( ( value ) =>
|
|
69
|
+
prop.value = prop.value.filter( ( value ) => ! removedIds.includes( value ) );
|
|
63
70
|
} );
|
|
64
71
|
|
|
65
72
|
updateElementSettings( {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { mergeProps } from '@elementor/editor-props';
|
|
2
2
|
import { getVariantByMeta, type StyleDefinition, type StyleDefinitionVariant } from '@elementor/editor-styles';
|
|
3
3
|
|
|
4
4
|
import { type ElementID } from '../types';
|
|
@@ -20,31 +20,14 @@ export function updateElementStyle( args: UpdateElementStyleArgs ) {
|
|
|
20
20
|
throw new StyleNotFoundError( { context: { styleId: args.styleId } } );
|
|
21
21
|
}
|
|
22
22
|
|
|
23
|
-
|
|
23
|
+
const variant = getVariantByMeta( style, args.meta );
|
|
24
24
|
|
|
25
|
-
if (
|
|
26
|
-
variant =
|
|
27
|
-
|
|
28
|
-
style.variants.push(
|
|
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
|
-
}
|
|
@@ -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
|
-
}
|