@elementor/editor-styles-repository 0.8.1 → 0.8.4
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/.turbo/turbo-build.log +10 -10
- package/CHANGELOG.md +30 -0
- package/dist/index.d.mts +47 -17
- package/dist/index.d.ts +47 -17
- package/dist/index.js +111 -51
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +110 -49
- package/dist/index.mjs.map +1 -1
- package/package.json +7 -5
- package/src/__tests__/{elements-styles-provider.test.ts → document-elements-styles-provider.test.ts} +33 -21
- package/src/__tests__/element-base-styles-provider.test.ts +3 -3
- package/src/errors.ts +5 -0
- package/src/hooks/use-create-actions-by-provider.ts +1 -1
- package/src/index.ts +10 -10
- package/src/init.ts +3 -3
- package/src/{elements-styles-provider.ts → providers/document-elements-styles-provider.ts} +29 -16
- package/src/providers/element-base-styles-provider.ts +22 -0
- package/src/types.ts +33 -0
- package/src/utils/__tests__/create-styles-repository.test.ts +17 -93
- package/src/utils/__tests__/validate-style-label.test.ts +37 -0
- package/src/utils/create-styles-provider.ts +51 -0
- package/src/utils/create-styles-repository.ts +3 -58
- package/src/utils/is-elements-styles-provider.ts +5 -0
- package/src/utils/validate-style-label.ts +38 -0
- package/src/element-base-styles-provider.ts +0 -26
package/.turbo/turbo-build.log
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
|
|
2
|
-
> @elementor/editor-styles-repository@0.8.
|
|
2
|
+
> @elementor/editor-styles-repository@0.8.4 build
|
|
3
3
|
> tsup --config=../../tsup.build.ts
|
|
4
4
|
|
|
5
5
|
[34mCLI[39m Building entry: src/index.ts
|
|
@@ -10,13 +10,13 @@
|
|
|
10
10
|
[34mCLI[39m Cleaning output folder
|
|
11
11
|
[34mESM[39m Build start
|
|
12
12
|
[34mCJS[39m Build start
|
|
13
|
-
[32mESM[39m [1mdist/index.mjs [22m[
|
|
14
|
-
[32mESM[39m [1mdist/index.mjs.map [22m[
|
|
15
|
-
[32mESM[39m ⚡️ Build success in
|
|
16
|
-
[32mCJS[39m [1mdist/index.js [22m[
|
|
17
|
-
[32mCJS[39m [1mdist/index.js.map [22m[
|
|
18
|
-
[32mCJS[39m ⚡️ Build success in
|
|
13
|
+
[32mESM[39m [1mdist/index.mjs [22m[32m6.33 KB[39m
|
|
14
|
+
[32mESM[39m [1mdist/index.mjs.map [22m[32m12.77 KB[39m
|
|
15
|
+
[32mESM[39m ⚡️ Build success in 120ms
|
|
16
|
+
[32mCJS[39m [1mdist/index.js [22m[32m8.15 KB[39m
|
|
17
|
+
[32mCJS[39m [1mdist/index.js.map [22m[32m13.61 KB[39m
|
|
18
|
+
[32mCJS[39m ⚡️ Build success in 124ms
|
|
19
19
|
[34mDTS[39m Build start
|
|
20
|
-
[32mDTS[39m ⚡️ Build success in
|
|
21
|
-
[32mDTS[39m [1mdist/index.d.mts [22m[
|
|
22
|
-
[32mDTS[39m [1mdist/index.d.ts [22m[
|
|
20
|
+
[32mDTS[39m ⚡️ Build success in 8869ms
|
|
21
|
+
[32mDTS[39m [1mdist/index.d.mts [22m[32m3.19 KB[39m
|
|
22
|
+
[32mDTS[39m [1mdist/index.d.ts [22m[32m3.19 KB[39m
|
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,35 @@
|
|
|
1
1
|
# @elementor/editor-styles-repository
|
|
2
2
|
|
|
3
|
+
## 0.8.4
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 42c42ee: Remove side-effects
|
|
8
|
+
- 5fa575c: Fix when switching documents it removes all the previous document styles
|
|
9
|
+
- 64574e3: Refactor styles provider as a preparation for header/footer styles not being rendered
|
|
10
|
+
- Updated dependencies [5fa575c]
|
|
11
|
+
- Updated dependencies [fe0ab45]
|
|
12
|
+
- Updated dependencies [fd5251c]
|
|
13
|
+
- @elementor/editor-elements@0.8.1
|
|
14
|
+
- @elementor/editor-props@0.12.0
|
|
15
|
+
- @elementor/editor-styles@0.6.6
|
|
16
|
+
|
|
17
|
+
## 0.8.3
|
|
18
|
+
|
|
19
|
+
### Patch Changes
|
|
20
|
+
|
|
21
|
+
- 02fd724: Remove styles inheritance indicator if all inherited values are from a base style provider
|
|
22
|
+
- Updated dependencies [f644084]
|
|
23
|
+
- @elementor/editor-elements@0.8.0
|
|
24
|
+
- @elementor/editor-styles@0.6.5
|
|
25
|
+
|
|
26
|
+
## 0.8.2
|
|
27
|
+
|
|
28
|
+
### Patch Changes
|
|
29
|
+
|
|
30
|
+
- Updated dependencies [548209b]
|
|
31
|
+
- @elementor/editor-elements@0.7.1
|
|
32
|
+
|
|
3
33
|
## 0.8.1
|
|
4
34
|
|
|
5
35
|
### Patch Changes
|
package/dist/index.d.mts
CHANGED
|
@@ -3,32 +3,30 @@ import * as _elementor_editor_styles from '@elementor/editor-styles';
|
|
|
3
3
|
import { StyleDefinition, StyleDefinitionID, StyleDefinitionVariant } from '@elementor/editor-styles';
|
|
4
4
|
|
|
5
5
|
type MakeOptional<T, K extends keyof T> = Omit<T, K> & Partial<T>;
|
|
6
|
+
type Meta = Record<string, unknown>;
|
|
6
7
|
type UpdateActionPayload = MakeOptional<StyleDefinition, 'label' | 'variants' | 'type'>;
|
|
7
8
|
type UpdatePropsActionPayload = {
|
|
8
9
|
id: StyleDefinitionID;
|
|
9
10
|
meta: StyleDefinitionVariant['meta'];
|
|
10
11
|
props: Props;
|
|
11
12
|
};
|
|
12
|
-
type Meta = Record<string, unknown>;
|
|
13
13
|
type StylesProvider = {
|
|
14
|
-
|
|
14
|
+
getKey: () => string;
|
|
15
15
|
priority: number;
|
|
16
|
+
limit: number;
|
|
17
|
+
subscribe: (callback: () => void) => () => void;
|
|
18
|
+
labels: {
|
|
19
|
+
singular: string | null;
|
|
20
|
+
plural: string | null;
|
|
21
|
+
};
|
|
16
22
|
actions: {
|
|
17
|
-
|
|
18
|
-
|
|
23
|
+
all: (meta?: Meta) => StyleDefinition[];
|
|
24
|
+
get: (id: StyleDefinitionID, meta?: Meta) => StyleDefinition | null;
|
|
19
25
|
create?: (label: StyleDefinition['label']) => StyleDefinitionID;
|
|
20
26
|
delete?: (id: StyleDefinitionID) => void;
|
|
21
|
-
setOrder?: (order: StyleDefinitionID[]) => void;
|
|
22
27
|
update?: (data: UpdateActionPayload) => void;
|
|
23
28
|
updateProps?: (args: UpdatePropsActionPayload, meta?: Meta) => void;
|
|
24
29
|
};
|
|
25
|
-
subscribe: (callback: () => void) => () => void;
|
|
26
|
-
labels?: {
|
|
27
|
-
singular: string;
|
|
28
|
-
plural: string;
|
|
29
|
-
};
|
|
30
|
-
reservedLabel?: string;
|
|
31
|
-
limit?: number;
|
|
32
30
|
};
|
|
33
31
|
|
|
34
32
|
declare const stylesRepository: {
|
|
@@ -37,8 +35,6 @@ declare const stylesRepository: {
|
|
|
37
35
|
subscribe: (cb: () => void) => () => void;
|
|
38
36
|
getProviders: () => StylesProvider[];
|
|
39
37
|
getProviderByKey: (key: string) => StylesProvider | undefined;
|
|
40
|
-
isLabelExist: (newLabel: string) => boolean;
|
|
41
|
-
isLabelValid: (newLabel: string) => boolean;
|
|
42
38
|
};
|
|
43
39
|
|
|
44
40
|
declare function useProviders(): StylesProvider[];
|
|
@@ -47,7 +43,41 @@ type CreateAction = Required<StylesProvider['actions']>['create'];
|
|
|
47
43
|
type CreateTuple = [StylesProvider, CreateAction];
|
|
48
44
|
declare function useCreateActionsByProvider(): CreateTuple[];
|
|
49
45
|
|
|
50
|
-
declare
|
|
51
|
-
|
|
46
|
+
declare function validateStyleLabel(label: string): {
|
|
47
|
+
isValid: boolean;
|
|
48
|
+
error: null;
|
|
49
|
+
} | {
|
|
50
|
+
isValid: boolean;
|
|
51
|
+
error: string;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
type CreateStylesProviderOptions = {
|
|
55
|
+
key: string | (() => string);
|
|
56
|
+
priority?: number;
|
|
57
|
+
limit?: number;
|
|
58
|
+
subscribe?: (callback: () => void) => () => void;
|
|
59
|
+
labels?: {
|
|
60
|
+
singular: string;
|
|
61
|
+
plural: string;
|
|
62
|
+
};
|
|
63
|
+
actions: {
|
|
64
|
+
all: StylesProvider['actions']['all'];
|
|
65
|
+
get: StylesProvider['actions']['get'];
|
|
66
|
+
create?: StylesProvider['actions']['create'];
|
|
67
|
+
delete?: StylesProvider['actions']['delete'];
|
|
68
|
+
update?: StylesProvider['actions']['update'];
|
|
69
|
+
updateProps?: StylesProvider['actions']['updateProps'];
|
|
70
|
+
};
|
|
71
|
+
};
|
|
72
|
+
declare function createStylesProvider({ key, priority, limit, subscribe, labels, actions, }: CreateStylesProviderOptions): StylesProvider;
|
|
73
|
+
|
|
74
|
+
declare function isElementsStylesProvider(key: string): boolean;
|
|
75
|
+
|
|
76
|
+
declare const ELEMENTS_BASE_STYLES_PROVIDER_KEY = "element-base-styles";
|
|
77
|
+
|
|
78
|
+
declare const ELEMENTS_STYLES_PROVIDER_KEY_PREFIX = "document-elements-";
|
|
79
|
+
declare const ELEMENTS_STYLES_RESERVED_LABEL = "local";
|
|
80
|
+
|
|
81
|
+
declare function init(): void;
|
|
52
82
|
|
|
53
|
-
export {
|
|
83
|
+
export { type CreateStylesProviderOptions, ELEMENTS_BASE_STYLES_PROVIDER_KEY, ELEMENTS_STYLES_PROVIDER_KEY_PREFIX, ELEMENTS_STYLES_RESERVED_LABEL, type Meta, type StylesProvider, type UpdateActionPayload, type UpdatePropsActionPayload, createStylesProvider, init, isElementsStylesProvider, stylesRepository, useCreateActionsByProvider, useProviders, validateStyleLabel };
|
package/dist/index.d.ts
CHANGED
|
@@ -3,32 +3,30 @@ import * as _elementor_editor_styles from '@elementor/editor-styles';
|
|
|
3
3
|
import { StyleDefinition, StyleDefinitionID, StyleDefinitionVariant } from '@elementor/editor-styles';
|
|
4
4
|
|
|
5
5
|
type MakeOptional<T, K extends keyof T> = Omit<T, K> & Partial<T>;
|
|
6
|
+
type Meta = Record<string, unknown>;
|
|
6
7
|
type UpdateActionPayload = MakeOptional<StyleDefinition, 'label' | 'variants' | 'type'>;
|
|
7
8
|
type UpdatePropsActionPayload = {
|
|
8
9
|
id: StyleDefinitionID;
|
|
9
10
|
meta: StyleDefinitionVariant['meta'];
|
|
10
11
|
props: Props;
|
|
11
12
|
};
|
|
12
|
-
type Meta = Record<string, unknown>;
|
|
13
13
|
type StylesProvider = {
|
|
14
|
-
|
|
14
|
+
getKey: () => string;
|
|
15
15
|
priority: number;
|
|
16
|
+
limit: number;
|
|
17
|
+
subscribe: (callback: () => void) => () => void;
|
|
18
|
+
labels: {
|
|
19
|
+
singular: string | null;
|
|
20
|
+
plural: string | null;
|
|
21
|
+
};
|
|
16
22
|
actions: {
|
|
17
|
-
|
|
18
|
-
|
|
23
|
+
all: (meta?: Meta) => StyleDefinition[];
|
|
24
|
+
get: (id: StyleDefinitionID, meta?: Meta) => StyleDefinition | null;
|
|
19
25
|
create?: (label: StyleDefinition['label']) => StyleDefinitionID;
|
|
20
26
|
delete?: (id: StyleDefinitionID) => void;
|
|
21
|
-
setOrder?: (order: StyleDefinitionID[]) => void;
|
|
22
27
|
update?: (data: UpdateActionPayload) => void;
|
|
23
28
|
updateProps?: (args: UpdatePropsActionPayload, meta?: Meta) => void;
|
|
24
29
|
};
|
|
25
|
-
subscribe: (callback: () => void) => () => void;
|
|
26
|
-
labels?: {
|
|
27
|
-
singular: string;
|
|
28
|
-
plural: string;
|
|
29
|
-
};
|
|
30
|
-
reservedLabel?: string;
|
|
31
|
-
limit?: number;
|
|
32
30
|
};
|
|
33
31
|
|
|
34
32
|
declare const stylesRepository: {
|
|
@@ -37,8 +35,6 @@ declare const stylesRepository: {
|
|
|
37
35
|
subscribe: (cb: () => void) => () => void;
|
|
38
36
|
getProviders: () => StylesProvider[];
|
|
39
37
|
getProviderByKey: (key: string) => StylesProvider | undefined;
|
|
40
|
-
isLabelExist: (newLabel: string) => boolean;
|
|
41
|
-
isLabelValid: (newLabel: string) => boolean;
|
|
42
38
|
};
|
|
43
39
|
|
|
44
40
|
declare function useProviders(): StylesProvider[];
|
|
@@ -47,7 +43,41 @@ type CreateAction = Required<StylesProvider['actions']>['create'];
|
|
|
47
43
|
type CreateTuple = [StylesProvider, CreateAction];
|
|
48
44
|
declare function useCreateActionsByProvider(): CreateTuple[];
|
|
49
45
|
|
|
50
|
-
declare
|
|
51
|
-
|
|
46
|
+
declare function validateStyleLabel(label: string): {
|
|
47
|
+
isValid: boolean;
|
|
48
|
+
error: null;
|
|
49
|
+
} | {
|
|
50
|
+
isValid: boolean;
|
|
51
|
+
error: string;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
type CreateStylesProviderOptions = {
|
|
55
|
+
key: string | (() => string);
|
|
56
|
+
priority?: number;
|
|
57
|
+
limit?: number;
|
|
58
|
+
subscribe?: (callback: () => void) => () => void;
|
|
59
|
+
labels?: {
|
|
60
|
+
singular: string;
|
|
61
|
+
plural: string;
|
|
62
|
+
};
|
|
63
|
+
actions: {
|
|
64
|
+
all: StylesProvider['actions']['all'];
|
|
65
|
+
get: StylesProvider['actions']['get'];
|
|
66
|
+
create?: StylesProvider['actions']['create'];
|
|
67
|
+
delete?: StylesProvider['actions']['delete'];
|
|
68
|
+
update?: StylesProvider['actions']['update'];
|
|
69
|
+
updateProps?: StylesProvider['actions']['updateProps'];
|
|
70
|
+
};
|
|
71
|
+
};
|
|
72
|
+
declare function createStylesProvider({ key, priority, limit, subscribe, labels, actions, }: CreateStylesProviderOptions): StylesProvider;
|
|
73
|
+
|
|
74
|
+
declare function isElementsStylesProvider(key: string): boolean;
|
|
75
|
+
|
|
76
|
+
declare const ELEMENTS_BASE_STYLES_PROVIDER_KEY = "element-base-styles";
|
|
77
|
+
|
|
78
|
+
declare const ELEMENTS_STYLES_PROVIDER_KEY_PREFIX = "document-elements-";
|
|
79
|
+
declare const ELEMENTS_STYLES_RESERVED_LABEL = "local";
|
|
80
|
+
|
|
81
|
+
declare function init(): void;
|
|
52
82
|
|
|
53
|
-
export {
|
|
83
|
+
export { type CreateStylesProviderOptions, ELEMENTS_BASE_STYLES_PROVIDER_KEY, ELEMENTS_STYLES_PROVIDER_KEY_PREFIX, ELEMENTS_STYLES_RESERVED_LABEL, type Meta, type StylesProvider, type UpdateActionPayload, type UpdatePropsActionPayload, createStylesProvider, init, isElementsStylesProvider, stylesRepository, useCreateActionsByProvider, useProviders, validateStyleLabel };
|
package/dist/index.js
CHANGED
|
@@ -20,16 +20,20 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
20
20
|
// src/index.ts
|
|
21
21
|
var index_exports = {};
|
|
22
22
|
__export(index_exports, {
|
|
23
|
-
|
|
24
|
-
|
|
23
|
+
ELEMENTS_BASE_STYLES_PROVIDER_KEY: () => ELEMENTS_BASE_STYLES_PROVIDER_KEY,
|
|
24
|
+
ELEMENTS_STYLES_PROVIDER_KEY_PREFIX: () => ELEMENTS_STYLES_PROVIDER_KEY_PREFIX,
|
|
25
|
+
ELEMENTS_STYLES_RESERVED_LABEL: () => ELEMENTS_STYLES_RESERVED_LABEL,
|
|
26
|
+
createStylesProvider: () => createStylesProvider,
|
|
27
|
+
init: () => init,
|
|
28
|
+
isElementsStylesProvider: () => isElementsStylesProvider,
|
|
25
29
|
stylesRepository: () => stylesRepository,
|
|
26
30
|
useCreateActionsByProvider: () => useCreateActionsByProvider,
|
|
27
|
-
useProviders: () => useProviders
|
|
31
|
+
useProviders: () => useProviders,
|
|
32
|
+
validateStyleLabel: () => validateStyleLabel
|
|
28
33
|
});
|
|
29
34
|
module.exports = __toCommonJS(index_exports);
|
|
30
35
|
|
|
31
36
|
// src/utils/create-styles-repository.ts
|
|
32
|
-
var VALID_SELECTOR_REGEX = /^[a-zA-Z0-9_-]+$/;
|
|
33
37
|
var createStylesRepository = () => {
|
|
34
38
|
const providers = [];
|
|
35
39
|
const getProviders = () => {
|
|
@@ -39,7 +43,7 @@ var createStylesRepository = () => {
|
|
|
39
43
|
providers.push(provider);
|
|
40
44
|
};
|
|
41
45
|
const all = (meta = {}) => {
|
|
42
|
-
return getProviders().flatMap((provider) => provider.actions.
|
|
46
|
+
return getProviders().flatMap((provider) => provider.actions.all(meta));
|
|
43
47
|
};
|
|
44
48
|
const subscribe = (cb) => {
|
|
45
49
|
const unsubscribes = providers.map((provider) => {
|
|
@@ -50,28 +54,14 @@ var createStylesRepository = () => {
|
|
|
50
54
|
};
|
|
51
55
|
};
|
|
52
56
|
const getProviderByKey = (key) => {
|
|
53
|
-
return providers.find((provider) => provider.
|
|
57
|
+
return providers.find((provider) => provider.getKey() === key);
|
|
54
58
|
};
|
|
55
|
-
const isLabelExist = (newLabel) => {
|
|
56
|
-
const classes = all();
|
|
57
|
-
const reservedLabels = providers.map(({ reservedLabel }) => reservedLabel).filter(Boolean);
|
|
58
|
-
if (reservedLabels.includes(newLabel)) {
|
|
59
|
-
return true;
|
|
60
|
-
}
|
|
61
|
-
if (!classes?.length) {
|
|
62
|
-
return false;
|
|
63
|
-
}
|
|
64
|
-
return classes.some(({ label }) => label.toLowerCase() === newLabel.toLowerCase());
|
|
65
|
-
};
|
|
66
|
-
const isLabelValid = (newLabel) => VALID_SELECTOR_REGEX.test(newLabel);
|
|
67
59
|
return {
|
|
68
60
|
all,
|
|
69
61
|
register,
|
|
70
62
|
subscribe,
|
|
71
63
|
getProviders,
|
|
72
|
-
getProviderByKey
|
|
73
|
-
isLabelExist,
|
|
74
|
-
isLabelValid
|
|
64
|
+
getProviderByKey
|
|
75
65
|
};
|
|
76
66
|
};
|
|
77
67
|
|
|
@@ -99,7 +89,11 @@ function useCreateActionsByProvider() {
|
|
|
99
89
|
}, []);
|
|
100
90
|
}
|
|
101
91
|
|
|
102
|
-
// src/
|
|
92
|
+
// src/utils/validate-style-label.ts
|
|
93
|
+
var import_schema = require("@elementor/schema");
|
|
94
|
+
var import_i18n = require("@wordpress/i18n");
|
|
95
|
+
|
|
96
|
+
// src/providers/document-elements-styles-provider.ts
|
|
103
97
|
var import_editor_elements = require("@elementor/editor-elements");
|
|
104
98
|
var import_editor_v1_adapters = require("@elementor/editor-v1-adapters");
|
|
105
99
|
|
|
@@ -109,22 +103,65 @@ var InvalidElementsStyleProviderMetaError = (0, import_utils.createError)({
|
|
|
109
103
|
code: "invalid_elements_style_provider_meta",
|
|
110
104
|
message: "Invalid elements style provider meta."
|
|
111
105
|
});
|
|
106
|
+
var ActiveDocumentMustExistError = (0, import_utils.createError)({
|
|
107
|
+
code: "active_document_must_exist",
|
|
108
|
+
message: "Active document must exist."
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
// src/utils/create-styles-provider.ts
|
|
112
|
+
var DEFAULT_LIMIT = 1e4;
|
|
113
|
+
var DEFAULT_PRIORITY = 10;
|
|
114
|
+
function createStylesProvider({
|
|
115
|
+
key,
|
|
116
|
+
priority = DEFAULT_PRIORITY,
|
|
117
|
+
limit = DEFAULT_LIMIT,
|
|
118
|
+
subscribe = () => () => {
|
|
119
|
+
},
|
|
120
|
+
labels,
|
|
121
|
+
actions
|
|
122
|
+
}) {
|
|
123
|
+
return {
|
|
124
|
+
getKey: typeof key === "string" ? () => key : key,
|
|
125
|
+
priority,
|
|
126
|
+
limit,
|
|
127
|
+
subscribe,
|
|
128
|
+
labels: {
|
|
129
|
+
singular: labels?.singular ?? null,
|
|
130
|
+
plural: labels?.plural ?? null
|
|
131
|
+
},
|
|
132
|
+
actions: {
|
|
133
|
+
all: actions.all,
|
|
134
|
+
get: actions.get,
|
|
135
|
+
create: actions.create,
|
|
136
|
+
delete: actions.delete,
|
|
137
|
+
update: actions.update,
|
|
138
|
+
updateProps: actions.updateProps
|
|
139
|
+
}
|
|
140
|
+
};
|
|
141
|
+
}
|
|
112
142
|
|
|
113
|
-
// src/elements-styles-provider.ts
|
|
114
|
-
var
|
|
115
|
-
var
|
|
116
|
-
var
|
|
117
|
-
key:
|
|
143
|
+
// src/providers/document-elements-styles-provider.ts
|
|
144
|
+
var ELEMENTS_STYLES_PROVIDER_KEY_PREFIX = "document-elements-";
|
|
145
|
+
var ELEMENTS_STYLES_RESERVED_LABEL = "local";
|
|
146
|
+
var documentElementsStylesProvider = createStylesProvider({
|
|
147
|
+
key: () => {
|
|
148
|
+
const documentId = (0, import_editor_elements.getCurrentDocumentId)();
|
|
149
|
+
if (!documentId) {
|
|
150
|
+
throw new ActiveDocumentMustExistError();
|
|
151
|
+
}
|
|
152
|
+
return `${ELEMENTS_STYLES_PROVIDER_KEY_PREFIX}${documentId}`;
|
|
153
|
+
},
|
|
118
154
|
priority: 50,
|
|
155
|
+
subscribe: (cb) => (0, import_editor_v1_adapters.__privateListenTo)(import_editor_elements.styleRerenderEvents, cb),
|
|
119
156
|
actions: {
|
|
120
|
-
|
|
157
|
+
all: (meta = {}) => {
|
|
121
158
|
let elements = (0, import_editor_elements.getElements)();
|
|
122
159
|
if (isValidElementsMeta(meta)) {
|
|
123
160
|
elements = elements.filter((element) => element.id === meta.elementId);
|
|
124
161
|
}
|
|
125
162
|
return elements.flatMap((element) => Object.values(element.model.get("styles") ?? {}));
|
|
126
163
|
},
|
|
127
|
-
|
|
164
|
+
get: (id, meta = {}) => {
|
|
128
165
|
if (!isValidElementsMeta(meta)) {
|
|
129
166
|
throw new InvalidElementsStyleProviderMetaError({ context: { meta } });
|
|
130
167
|
}
|
|
@@ -142,51 +179,74 @@ var elementsStylesProvider = {
|
|
|
142
179
|
props: args.props
|
|
143
180
|
});
|
|
144
181
|
}
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
reservedLabel: LOCAL_STYLES_RESERVED_LABEL
|
|
148
|
-
};
|
|
182
|
+
}
|
|
183
|
+
});
|
|
149
184
|
function isValidElementsMeta(meta) {
|
|
150
185
|
return "elementId" in meta && typeof meta.elementId === "string" && !!meta.elementId;
|
|
151
186
|
}
|
|
152
187
|
|
|
153
|
-
// src/
|
|
188
|
+
// src/utils/validate-style-label.ts
|
|
189
|
+
var VALID_LABEL_REGEX = /^[a-zA-Z0-9_-]+$/;
|
|
190
|
+
var schema = import_schema.z.string().min(1, (0, import_i18n.__)("Cannot be empty", "elementor")).max(50, (0, import_i18n.__)("Cannot be longer than 50 characters", "elementor")).regex(VALID_LABEL_REGEX, (0, import_i18n.__)("Invalid format", "elementor"));
|
|
191
|
+
function validateStyleLabel(label) {
|
|
192
|
+
const existingLabels = /* @__PURE__ */ new Set([
|
|
193
|
+
ELEMENTS_STYLES_RESERVED_LABEL,
|
|
194
|
+
...stylesRepository.all().map((styleDef) => styleDef.label.toLowerCase())
|
|
195
|
+
]);
|
|
196
|
+
const result = schema.refine((value) => !existingLabels.has(value), {
|
|
197
|
+
message: (0, import_i18n.__)("Name exists", "elementor")
|
|
198
|
+
}).safeParse(label.toLowerCase());
|
|
199
|
+
if (result.success) {
|
|
200
|
+
return {
|
|
201
|
+
isValid: true,
|
|
202
|
+
error: null
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
return {
|
|
206
|
+
isValid: false,
|
|
207
|
+
error: result.error.format()._errors[0]
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
// src/utils/is-elements-styles-provider.ts
|
|
212
|
+
function isElementsStylesProvider(key) {
|
|
213
|
+
return new RegExp(`^${ELEMENTS_STYLES_PROVIDER_KEY_PREFIX}\\d+$`).test(key);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
// src/providers/element-base-styles-provider.ts
|
|
154
217
|
var import_editor_elements2 = require("@elementor/editor-elements");
|
|
155
218
|
var ELEMENTS_BASE_STYLES_PROVIDER_KEY = "element-base-styles";
|
|
156
|
-
var elementBaseStylesProvider = {
|
|
219
|
+
var elementBaseStylesProvider = createStylesProvider({
|
|
157
220
|
key: ELEMENTS_BASE_STYLES_PROVIDER_KEY,
|
|
158
|
-
priority: 10,
|
|
159
221
|
actions: {
|
|
160
|
-
|
|
222
|
+
all() {
|
|
161
223
|
const widgetsCache = (0, import_editor_elements2.getWidgetsCache)();
|
|
162
224
|
return Object.values(widgetsCache ?? {}).flatMap(
|
|
163
225
|
(widget) => Object.values(widget.base_styles ?? {})
|
|
164
226
|
);
|
|
165
227
|
},
|
|
166
|
-
|
|
167
|
-
return this.
|
|
228
|
+
get(id) {
|
|
229
|
+
return this.all().find((style) => style.id === id) ?? null;
|
|
168
230
|
}
|
|
169
|
-
},
|
|
170
|
-
subscribe: () => {
|
|
171
|
-
return () => {
|
|
172
|
-
};
|
|
173
231
|
}
|
|
174
|
-
};
|
|
232
|
+
});
|
|
175
233
|
|
|
176
234
|
// src/init.ts
|
|
177
235
|
function init() {
|
|
178
|
-
stylesRepository.register(
|
|
236
|
+
stylesRepository.register(documentElementsStylesProvider);
|
|
179
237
|
stylesRepository.register(elementBaseStylesProvider);
|
|
180
238
|
}
|
|
181
|
-
|
|
182
|
-
// src/index.ts
|
|
183
|
-
init();
|
|
184
239
|
// Annotate the CommonJS export names for ESM import in node:
|
|
185
240
|
0 && (module.exports = {
|
|
186
|
-
|
|
187
|
-
|
|
241
|
+
ELEMENTS_BASE_STYLES_PROVIDER_KEY,
|
|
242
|
+
ELEMENTS_STYLES_PROVIDER_KEY_PREFIX,
|
|
243
|
+
ELEMENTS_STYLES_RESERVED_LABEL,
|
|
244
|
+
createStylesProvider,
|
|
245
|
+
init,
|
|
246
|
+
isElementsStylesProvider,
|
|
188
247
|
stylesRepository,
|
|
189
248
|
useCreateActionsByProvider,
|
|
190
|
-
useProviders
|
|
249
|
+
useProviders,
|
|
250
|
+
validateStyleLabel
|
|
191
251
|
});
|
|
192
252
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/utils/create-styles-repository.ts","../src/styles-repository.ts","../src/hooks/use-providers.ts","../src/hooks/use-create-actions-by-provider.ts","../src/elements-styles-provider.ts","../src/errors.ts","../src/element-base-styles-provider.ts","../src/init.ts"],"sourcesContent":["export {\n\ttype StylesProvider,\n\ttype UpdateActionPayload,\n\ttype Meta,\n\ttype UpdatePropsActionPayload,\n} from './utils/create-styles-repository';\n\nexport { stylesRepository } from './styles-repository';\nexport { useProviders } from './hooks/use-providers';\nexport { useCreateActionsByProvider } from './hooks/use-create-actions-by-provider';\n\nexport { ELEMENTS_STYLES_PROVIDER_KEY, LOCAL_STYLES_RESERVED_LABEL } from './elements-styles-provider';\n\nimport { init } from './init';\n\ninit();\n","import { type Props } from '@elementor/editor-props';\nimport { type StyleDefinition, type StyleDefinitionID, type StyleDefinitionVariant } from '@elementor/editor-styles';\n\ntype MakeOptional< T, K extends keyof T > = Omit< T, K > & Partial< T >;\n\nexport type UpdateActionPayload = MakeOptional< StyleDefinition, 'label' | 'variants' | 'type' >;\n\nexport type UpdatePropsActionPayload = {\n\tid: StyleDefinitionID;\n\tmeta: StyleDefinitionVariant[ 'meta' ];\n\tprops: Props;\n};\n\nexport type Meta = Record< string, unknown >;\n\nexport type StylesProvider = {\n\tkey: string;\n\tpriority: number;\n\tactions: {\n\t\tget: ( meta?: Meta ) => StyleDefinition[];\n\t\tgetById: ( id: StyleDefinitionID, meta?: Meta ) => StyleDefinition | null;\n\t\tcreate?: ( label: StyleDefinition[ 'label' ] ) => StyleDefinitionID;\n\t\tdelete?: ( id: StyleDefinitionID ) => void;\n\t\tsetOrder?: ( order: StyleDefinitionID[] ) => void;\n\t\tupdate?: ( data: UpdateActionPayload ) => void;\n\t\tupdateProps?: ( args: UpdatePropsActionPayload, meta?: Meta ) => void;\n\t};\n\tsubscribe: ( callback: () => void ) => () => void;\n\tlabels?: {\n\t\tsingular: string;\n\t\tplural: string;\n\t};\n\treservedLabel?: string;\n\tlimit?: number;\n};\n\nconst VALID_SELECTOR_REGEX = /^[a-zA-Z0-9_-]+$/;\n\nexport const createStylesRepository = () => {\n\tconst providers: StylesProvider[] = [];\n\n\tconst getProviders = () => {\n\t\treturn providers.slice( 0 ).sort( ( a, b ) => ( a.priority > b.priority ? -1 : 1 ) );\n\t};\n\n\tconst register = ( provider: StylesProvider ) => {\n\t\tproviders.push( provider );\n\t};\n\n\tconst all = ( meta: Meta = {} ) => {\n\t\treturn getProviders().flatMap( ( provider ) => provider.actions.get( meta ) );\n\t};\n\n\tconst subscribe = ( cb: () => void ) => {\n\t\tconst unsubscribes = providers.map( ( provider ) => {\n\t\t\treturn provider.subscribe( cb );\n\t\t} );\n\n\t\treturn () => {\n\t\t\tunsubscribes.forEach( ( unsubscribe ) => unsubscribe() );\n\t\t};\n\t};\n\n\tconst getProviderByKey = ( key: string ) => {\n\t\treturn providers.find( ( provider ) => provider.key === key );\n\t};\n\n\tconst isLabelExist = ( newLabel: string ) => {\n\t\tconst classes = all();\n\t\tconst reservedLabels = providers.map( ( { reservedLabel } ) => reservedLabel ).filter( Boolean );\n\n\t\tif ( reservedLabels.includes( newLabel ) ) {\n\t\t\treturn true;\n\t\t}\n\n\t\tif ( ! classes?.length ) {\n\t\t\treturn false;\n\t\t}\n\n\t\treturn classes.some( ( { label } ) => label.toLowerCase() === newLabel.toLowerCase() );\n\t};\n\n\tconst isLabelValid = ( newLabel: string ) => VALID_SELECTOR_REGEX.test( newLabel );\n\n\treturn {\n\t\tall,\n\t\tregister,\n\t\tsubscribe,\n\t\tgetProviders,\n\t\tgetProviderByKey,\n\t\tisLabelExist,\n\t\tisLabelValid,\n\t};\n};\n","import { createStylesRepository } from './utils/create-styles-repository';\n\nexport const stylesRepository = createStylesRepository();\n","import { useEffect, useReducer } from 'react';\n\nimport { stylesRepository } from '../styles-repository';\n\nexport function useProviders() {\n\tconst [ , rerender ] = useReducer( ( prev ) => ! prev, false );\n\n\tuseEffect( () => stylesRepository.subscribe( rerender ), [] );\n\n\treturn stylesRepository.getProviders();\n}\n","import { useMemo } from 'react';\n\nimport { stylesRepository } from '../styles-repository';\nimport { type StylesProvider } from '../utils/create-styles-repository';\n\ntype CreateAction = Required< StylesProvider[ 'actions' ] >[ 'create' ];\ntype CreateTuple = [ StylesProvider, CreateAction ];\n\nexport function useCreateActionsByProvider() {\n\treturn useMemo( () => {\n\t\treturn stylesRepository\n\t\t\t.getProviders()\n\t\t\t.map< CreateTuple | null >( ( provider ) => {\n\t\t\t\tif ( ! provider.actions.create ) {\n\t\t\t\t\treturn null;\n\t\t\t\t}\n\n\t\t\t\treturn [ provider, provider.actions.create ];\n\t\t\t} )\n\t\t\t.filter( ( item ) => !! item );\n\t}, [] );\n}\n","import { getElements, getElementStyles, styleRerenderEvents, updateElementStyle } from '@elementor/editor-elements';\nimport { __privateListenTo as listenTo } from '@elementor/editor-v1-adapters';\n\nimport { InvalidElementsStyleProviderMetaError } from './errors';\nimport { type StylesProvider } from './utils/create-styles-repository';\n\nexport const ELEMENTS_STYLES_PROVIDER_KEY = 'elements';\nexport const LOCAL_STYLES_RESERVED_LABEL = 'local';\n\nexport const elementsStylesProvider = {\n\tkey: ELEMENTS_STYLES_PROVIDER_KEY,\n\tpriority: 50,\n\tactions: {\n\t\tget: ( meta = {} ) => {\n\t\t\tlet elements = getElements();\n\n\t\t\tif ( isValidElementsMeta( meta ) ) {\n\t\t\t\telements = elements.filter( ( element ) => element.id === meta.elementId );\n\t\t\t}\n\n\t\t\treturn elements.flatMap( ( element ) => Object.values( element.model.get( 'styles' ) ?? {} ) );\n\t\t},\n\n\t\tgetById: ( id, meta = {} ) => {\n\t\t\tif ( ! isValidElementsMeta( meta ) ) {\n\t\t\t\tthrow new InvalidElementsStyleProviderMetaError( { context: { meta } } );\n\t\t\t}\n\n\t\t\tconst styles = getElementStyles( meta.elementId ) ?? {};\n\n\t\t\treturn styles[ id ] ?? null;\n\t\t},\n\n\t\tupdateProps: ( args, meta = {} ) => {\n\t\t\tif ( ! isValidElementsMeta( meta ) ) {\n\t\t\t\tthrow new InvalidElementsStyleProviderMetaError( { context: { meta } } );\n\t\t\t}\n\n\t\t\tupdateElementStyle( {\n\t\t\t\telementId: meta.elementId,\n\t\t\t\tstyleId: args.id,\n\t\t\t\tmeta: args.meta,\n\t\t\t\tprops: args.props,\n\t\t\t} );\n\t\t},\n\t},\n\tsubscribe: ( cb ) => listenTo( styleRerenderEvents, cb ),\n\treservedLabel: LOCAL_STYLES_RESERVED_LABEL,\n} satisfies StylesProvider;\n\ntype ElementsMeta = {\n\telementId: string;\n};\n\nfunction isValidElementsMeta( meta: Record< string, unknown > ): meta is ElementsMeta {\n\treturn 'elementId' in meta && typeof meta.elementId === 'string' && !! meta.elementId;\n}\n","import { createError } from '@elementor/utils';\n\nexport const InvalidElementsStyleProviderMetaError = createError< { meta: Record< string, unknown > } >( {\n\tcode: 'invalid_elements_style_provider_meta',\n\tmessage: 'Invalid elements style provider meta.',\n} );\n","import { getWidgetsCache } from '@elementor/editor-elements';\n\nimport { type StylesProvider } from './utils/create-styles-repository';\n\nconst ELEMENTS_BASE_STYLES_PROVIDER_KEY = 'element-base-styles';\n\nexport const elementBaseStylesProvider: StylesProvider = {\n\tkey: ELEMENTS_BASE_STYLES_PROVIDER_KEY,\n\tpriority: 10,\n\tactions: {\n\t\tget() {\n\t\t\tconst widgetsCache = getWidgetsCache();\n\n\t\t\treturn Object.values( widgetsCache ?? {} ).flatMap( ( widget ) =>\n\t\t\t\tObject.values( widget.base_styles ?? {} )\n\t\t\t);\n\t\t},\n\n\t\tgetById( id ) {\n\t\t\treturn this.get().find( ( style ) => style.id === id ) ?? null;\n\t\t},\n\t},\n\tsubscribe: () => {\n\t\treturn () => {};\n\t},\n};\n","import { elementBaseStylesProvider } from './element-base-styles-provider';\nimport { elementsStylesProvider } from './elements-styles-provider';\nimport { stylesRepository } from './styles-repository';\n\nexport function init() {\n\tstylesRepository.register( elementsStylesProvider );\n\tstylesRepository.register( elementBaseStylesProvider );\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACoCA,IAAM,uBAAuB;AAEtB,IAAM,yBAAyB,MAAM;AAC3C,QAAM,YAA8B,CAAC;AAErC,QAAM,eAAe,MAAM;AAC1B,WAAO,UAAU,MAAO,CAAE,EAAE,KAAM,CAAE,GAAG,MAAS,EAAE,WAAW,EAAE,WAAW,KAAK,CAAI;AAA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C,cAAM,IAAI,sCAAuC,EAAE,SAAS,EAAE,KAAK,EAAE,CAAE;AAAA,MACxE;AAEA,qDAAoB;AAAA,QACnB,WAAW,KAAK;AAAA,QAChB,SAAS,KAAK;AAAA,QACd,MAAM,KAAK;AAAA,QACX,OAAO,KAAK;AAAA,MACb,CAAE;AAAA,IACH;AAAA,EACD;AAAA,EACA,WAAW,CAAE,WAAQ,0BAAAC,mBAAU,4CAAqB,EAAG;AAAA,EACvD,eAAe;AAChB;AAMA,SAAS,oBAAqB,MAAwD;AACrF,SAAO,eAAe,QAAQ,OAAO,KAAK,cAAc,YAAY,CAAC,CAAE,KAAK;AAC7E;;;AExDA,IAAAC,0BAAgC;AAIhC,IAAM,oCAAoC;AAEnC,IAAM,4BAA4C;AAAA,EACxD,KAAK;AAAA,EACL,UAAU;AAAA,EACV,SAAS;AAAA,IACR,MAAM;AACL,YAAM,mBAAe,yCAAgB;AAErC,aAAO,OAAO,OAAQ,gBAAgB,CAAC,CAAE,EAAE;AAAA,QAAS,CAAE,WACrD,OAAO,OAAQ,OAAO,eAAe,CAAC,CAAE;AAAA,MACzC;AAAA,IACD;AAAA,IAEA,QAAS,IAAK;AACb,aAAO,KAAK,IAAI,EAAE,KAAM,CAAE,UAAW,MAAM,OAAO,EAAG,KAAK;AAAA,IAC3D;AAAA,EACD;AAAA,EACA,WAAW,MAAM;AAChB,WAAO,MAAM;AAAA,IAAC;AAAA,EACf;AACD;;;ACrBO,SAAS,OAAO;AACtB,mBAAiB,SAAU,sBAAuB;AAClD,mBAAiB,SAAU,yBAA0B;AACtD;;;ARQA,KAAK;","names":["import_react","listenTo","import_editor_elements"]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/utils/create-styles-repository.ts","../src/styles-repository.ts","../src/hooks/use-providers.ts","../src/hooks/use-create-actions-by-provider.ts","../src/utils/validate-style-label.ts","../src/providers/document-elements-styles-provider.ts","../src/errors.ts","../src/utils/create-styles-provider.ts","../src/utils/is-elements-styles-provider.ts","../src/providers/element-base-styles-provider.ts","../src/init.ts"],"sourcesContent":["export * from './types';\n\nexport { stylesRepository } from './styles-repository';\nexport { useProviders } from './hooks/use-providers';\nexport { useCreateActionsByProvider } from './hooks/use-create-actions-by-provider';\nexport { validateStyleLabel } from './utils/validate-style-label';\nexport { createStylesProvider, type CreateStylesProviderOptions } from './utils/create-styles-provider';\nexport { isElementsStylesProvider } from './utils/is-elements-styles-provider';\n\nexport { ELEMENTS_BASE_STYLES_PROVIDER_KEY } from './providers/element-base-styles-provider';\nexport {\n\tELEMENTS_STYLES_PROVIDER_KEY_PREFIX,\n\tELEMENTS_STYLES_RESERVED_LABEL,\n} from './providers/document-elements-styles-provider';\n\nexport { init } from './init';\n","import { type Meta, type StylesProvider } from '../types';\n\nexport const createStylesRepository = () => {\n\tconst providers: StylesProvider[] = [];\n\n\tconst getProviders = () => {\n\t\treturn providers.slice( 0 ).sort( ( a, b ) => ( a.priority > b.priority ? -1 : 1 ) );\n\t};\n\n\tconst register = ( provider: StylesProvider ) => {\n\t\tproviders.push( provider );\n\t};\n\n\tconst all = ( meta: Meta = {} ) => {\n\t\treturn getProviders().flatMap( ( provider ) => provider.actions.all( meta ) );\n\t};\n\n\tconst subscribe = ( cb: () => void ) => {\n\t\tconst unsubscribes = providers.map( ( provider ) => {\n\t\t\treturn provider.subscribe( cb );\n\t\t} );\n\n\t\treturn () => {\n\t\t\tunsubscribes.forEach( ( unsubscribe ) => unsubscribe() );\n\t\t};\n\t};\n\n\tconst getProviderByKey = ( key: string ) => {\n\t\treturn providers.find( ( provider ) => provider.getKey() === key );\n\t};\n\n\treturn {\n\t\tall,\n\t\tregister,\n\t\tsubscribe,\n\t\tgetProviders,\n\t\tgetProviderByKey,\n\t};\n};\n","import { createStylesRepository } from './utils/create-styles-repository';\n\nexport const stylesRepository = createStylesRepository();\n","import { useEffect, useReducer } from 'react';\n\nimport { stylesRepository } from '../styles-repository';\n\nexport function useProviders() {\n\tconst [ , rerender ] = useReducer( ( prev ) => ! prev, false );\n\n\tuseEffect( () => stylesRepository.subscribe( rerender ), [] );\n\n\treturn stylesRepository.getProviders();\n}\n","import { useMemo } from 'react';\n\nimport { stylesRepository } from '../styles-repository';\nimport { type StylesProvider } from '../types';\n\ntype CreateAction = Required< StylesProvider[ 'actions' ] >[ 'create' ];\ntype CreateTuple = [ StylesProvider, CreateAction ];\n\nexport function useCreateActionsByProvider() {\n\treturn useMemo( () => {\n\t\treturn stylesRepository\n\t\t\t.getProviders()\n\t\t\t.map< CreateTuple | null >( ( provider ) => {\n\t\t\t\tif ( ! provider.actions.create ) {\n\t\t\t\t\treturn null;\n\t\t\t\t}\n\n\t\t\t\treturn [ provider, provider.actions.create ];\n\t\t\t} )\n\t\t\t.filter( ( item ) => !! item );\n\t}, [] );\n}\n","import { z } from '@elementor/schema';\nimport { __ } from '@wordpress/i18n';\n\nimport { ELEMENTS_STYLES_RESERVED_LABEL } from '../providers/document-elements-styles-provider';\nimport { stylesRepository } from '../styles-repository';\n\nconst VALID_LABEL_REGEX = /^[a-zA-Z0-9_-]+$/;\n\nconst schema = z\n\t.string()\n\t.min( 1, __( 'Cannot be empty', 'elementor' ) )\n\t.max( 50, __( 'Cannot be longer than 50 characters', 'elementor' ) )\n\t.regex( VALID_LABEL_REGEX, __( 'Invalid format', 'elementor' ) );\n\nexport function validateStyleLabel( label: string ) {\n\tconst existingLabels = new Set( [\n\t\tELEMENTS_STYLES_RESERVED_LABEL,\n\t\t...stylesRepository.all().map( ( styleDef ) => styleDef.label.toLowerCase() ),\n\t] );\n\n\tconst result = schema\n\t\t.refine( ( value ) => ! existingLabels.has( value ), {\n\t\t\tmessage: __( 'Name exists', 'elementor' ),\n\t\t} )\n\t\t.safeParse( label.toLowerCase() );\n\n\tif ( result.success ) {\n\t\treturn {\n\t\t\tisValid: true,\n\t\t\terror: null,\n\t\t};\n\t}\n\n\treturn {\n\t\tisValid: false,\n\t\terror: result.error.format()._errors[ 0 ],\n\t};\n}\n","import {\n\tgetCurrentDocumentId,\n\tgetElements,\n\tgetElementStyles,\n\tstyleRerenderEvents,\n\tupdateElementStyle,\n} from '@elementor/editor-elements';\nimport { __privateListenTo as listenTo } from '@elementor/editor-v1-adapters';\n\nimport { ActiveDocumentMustExistError, InvalidElementsStyleProviderMetaError } from '../errors';\nimport { createStylesProvider } from '../utils/create-styles-provider';\n\nexport const ELEMENTS_STYLES_PROVIDER_KEY_PREFIX = 'document-elements-';\nexport const ELEMENTS_STYLES_RESERVED_LABEL = 'local';\n\ntype ElementsMeta = {\n\telementId: string;\n};\n\nexport const documentElementsStylesProvider = createStylesProvider( {\n\tkey: () => {\n\t\tconst documentId = getCurrentDocumentId();\n\n\t\tif ( ! documentId ) {\n\t\t\tthrow new ActiveDocumentMustExistError();\n\t\t}\n\n\t\treturn `${ ELEMENTS_STYLES_PROVIDER_KEY_PREFIX }${ documentId }`;\n\t},\n\tpriority: 50,\n\tsubscribe: ( cb ) => listenTo( styleRerenderEvents, cb ),\n\tactions: {\n\t\tall: ( meta = {} ) => {\n\t\t\tlet elements = getElements();\n\n\t\t\tif ( isValidElementsMeta( meta ) ) {\n\t\t\t\telements = elements.filter( ( element ) => element.id === meta.elementId );\n\t\t\t}\n\n\t\t\treturn elements.flatMap( ( element ) => Object.values( element.model.get( 'styles' ) ?? {} ) );\n\t\t},\n\n\t\tget: ( id, meta = {} ) => {\n\t\t\tif ( ! isValidElementsMeta( meta ) ) {\n\t\t\t\tthrow new InvalidElementsStyleProviderMetaError( { context: { meta } } );\n\t\t\t}\n\n\t\t\tconst styles = getElementStyles( meta.elementId ) ?? {};\n\n\t\t\treturn styles[ id ] ?? null;\n\t\t},\n\n\t\tupdateProps: ( args, meta = {} ) => {\n\t\t\tif ( ! isValidElementsMeta( meta ) ) {\n\t\t\t\tthrow new InvalidElementsStyleProviderMetaError( { context: { meta } } );\n\t\t\t}\n\n\t\t\tupdateElementStyle( {\n\t\t\t\telementId: meta.elementId,\n\t\t\t\tstyleId: args.id,\n\t\t\t\tmeta: args.meta,\n\t\t\t\tprops: args.props,\n\t\t\t} );\n\t\t},\n\t},\n} );\n\nfunction isValidElementsMeta( meta: Record< string, unknown > ): meta is ElementsMeta {\n\treturn 'elementId' in meta && typeof meta.elementId === 'string' && !! meta.elementId;\n}\n","import { createError } from '@elementor/utils';\n\nexport const InvalidElementsStyleProviderMetaError = createError< { meta: Record< string, unknown > } >( {\n\tcode: 'invalid_elements_style_provider_meta',\n\tmessage: 'Invalid elements style provider meta.',\n} );\n\nexport const ActiveDocumentMustExistError = createError( {\n\tcode: 'active_document_must_exist',\n\tmessage: 'Active document must exist.',\n} );\n","import { type StylesProvider } from '../types';\n\nexport type CreateStylesProviderOptions = {\n\tkey: string | ( () => string );\n\tpriority?: number;\n\tlimit?: number;\n\tsubscribe?: ( callback: () => void ) => () => void;\n\tlabels?: {\n\t\tsingular: string;\n\t\tplural: string;\n\t};\n\tactions: {\n\t\tall: StylesProvider[ 'actions' ][ 'all' ];\n\t\tget: StylesProvider[ 'actions' ][ 'get' ];\n\t\tcreate?: StylesProvider[ 'actions' ][ 'create' ];\n\t\tdelete?: StylesProvider[ 'actions' ][ 'delete' ];\n\t\tupdate?: StylesProvider[ 'actions' ][ 'update' ];\n\t\tupdateProps?: StylesProvider[ 'actions' ][ 'updateProps' ];\n\t};\n};\n\nconst DEFAULT_LIMIT = 10000;\nconst DEFAULT_PRIORITY = 10;\n\nexport function createStylesProvider( {\n\tkey,\n\tpriority = DEFAULT_PRIORITY,\n\tlimit = DEFAULT_LIMIT,\n\tsubscribe = () => () => {},\n\tlabels,\n\tactions,\n}: CreateStylesProviderOptions ): StylesProvider {\n\treturn {\n\t\tgetKey: typeof key === 'string' ? () => key : key,\n\t\tpriority,\n\t\tlimit,\n\t\tsubscribe,\n\t\tlabels: {\n\t\t\tsingular: labels?.singular ?? null,\n\t\t\tplural: labels?.plural ?? null,\n\t\t},\n\t\tactions: {\n\t\t\tall: actions.all,\n\t\t\tget: actions.get,\n\t\t\tcreate: actions.create,\n\t\t\tdelete: actions.delete,\n\t\t\tupdate: actions.update,\n\t\t\tupdateProps: actions.updateProps,\n\t\t},\n\t};\n}\n","import { ELEMENTS_STYLES_PROVIDER_KEY_PREFIX } from '../providers/document-elements-styles-provider';\n\nexport function isElementsStylesProvider( key: string ) {\n\treturn new RegExp( `^${ ELEMENTS_STYLES_PROVIDER_KEY_PREFIX }\\\\d+$` ).test( key );\n}\n","import { getWidgetsCache } from '@elementor/editor-elements';\n\nimport { createStylesProvider } from '../utils/create-styles-provider';\n\nexport const ELEMENTS_BASE_STYLES_PROVIDER_KEY = 'element-base-styles';\n\nexport const elementBaseStylesProvider = createStylesProvider( {\n\tkey: ELEMENTS_BASE_STYLES_PROVIDER_KEY,\n\tactions: {\n\t\tall() {\n\t\t\tconst widgetsCache = getWidgetsCache();\n\n\t\t\treturn Object.values( widgetsCache ?? {} ).flatMap( ( widget ) =>\n\t\t\t\tObject.values( widget.base_styles ?? {} )\n\t\t\t);\n\t\t},\n\n\t\tget( id ) {\n\t\t\treturn this.all().find( ( style ) => style.id === id ) ?? null;\n\t\t},\n\t},\n} );\n","import { documentElementsStylesProvider } from './providers/document-elements-styles-provider';\nimport { elementBaseStylesProvider } from './providers/element-base-styles-provider';\nimport { stylesRepository } from './styles-repository';\n\nexport function init() {\n\tstylesRepository.register( documentElementsStylesProvider );\n\tstylesRepository.register( elementBaseStylesProvider );\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACEO,IAAM,yBAAyB,MAAM;AAC3C,QAAM,YAA8B,CAAC;AAErC,QAAM,eAAe,MAAM;AAC1B,WAAO,UAAU,MAAO,CAAE,EAAE,KAAM,CAAE,GAAG,MAAS,EAAE,WAAW,EAAE,WAAW,KAAK,CAAI;AAAA,EACpF;AAEA,QAAM,WAAW,CAAE,aAA8B;AAChD,cAAU,KAAM,QAAS;AAAA,EAC1B;AAE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