@wordpress/block-editor 15.6.1-next.36001005c.0 → 15.6.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/build/components/background-image-control/index.js +2 -2
- package/build/components/background-image-control/index.js.map +2 -2
- package/build/components/block-list/block.js +3 -3
- package/build/components/block-list/block.js.map +2 -2
- package/build/components/block-list/index.js +2 -2
- package/build/components/block-list/index.js.map +1 -1
- package/build/components/block-quick-navigation/index.js +0 -1
- package/build/components/block-quick-navigation/index.js.map +2 -2
- package/build/components/global-styles/border-panel.js +1 -2
- package/build/components/global-styles/border-panel.js.map +2 -2
- package/build/components/global-styles/color-panel.js +1 -2
- package/build/components/global-styles/color-panel.js.map +2 -2
- package/build/components/global-styles/dimensions-panel.js +2 -3
- package/build/components/global-styles/dimensions-panel.js.map +2 -2
- package/build/components/global-styles/filters-panel.js +1 -2
- package/build/components/global-styles/filters-panel.js.map +2 -2
- package/build/components/global-styles/get-block-css-selector.js +78 -0
- package/build/components/global-styles/get-block-css-selector.js.map +7 -0
- package/build/components/global-styles/hooks.js +95 -23
- package/build/components/global-styles/hooks.js.map +2 -2
- package/build/components/global-styles/index.js +14 -0
- package/build/components/global-styles/index.js.map +2 -2
- package/build/components/global-styles/typography-panel.js +19 -3
- package/build/components/global-styles/typography-panel.js.map +2 -2
- package/build/components/global-styles/typography-utils.js +49 -2
- package/build/components/global-styles/typography-utils.js.map +2 -2
- package/build/components/global-styles/use-global-styles-output.js +998 -0
- package/build/components/global-styles/use-global-styles-output.js.map +7 -0
- package/build/components/global-styles/utils.js +377 -0
- package/build/components/global-styles/utils.js.map +2 -2
- package/build/components/rich-text/index.js +8 -7
- package/build/components/rich-text/index.js.map +2 -2
- package/build/hooks/block-bindings.js +111 -170
- package/build/hooks/block-bindings.js.map +2 -2
- package/build/hooks/block-style-variation.js +10 -6
- package/build/hooks/block-style-variation.js.map +2 -2
- package/build/hooks/custom-class-name.js +1 -1
- package/build/hooks/custom-class-name.js.map +1 -1
- package/build/hooks/duotone.js +3 -3
- package/build/hooks/duotone.js.map +2 -2
- package/build/hooks/fit-text.js +31 -18
- package/build/hooks/fit-text.js.map +2 -2
- package/build/hooks/font-size.js +6 -5
- package/build/hooks/font-size.js.map +2 -2
- package/build/hooks/metadata.js +48 -2
- package/build/hooks/metadata.js.map +2 -2
- package/build/hooks/typography.js +11 -4
- package/build/hooks/typography.js.map +3 -3
- package/build/hooks/use-typography-props.js +2 -2
- package/build/hooks/use-typography-props.js.map +2 -2
- package/build/store/private-selectors.js +3 -3
- package/build/store/private-selectors.js.map +2 -2
- package/build/store/selectors.js +38 -13
- package/build/store/selectors.js.map +2 -2
- package/build/store/utils.js +2 -1
- package/build/store/utils.js.map +2 -2
- package/build/utils/fit-text-utils.js +4 -4
- package/build/utils/fit-text-utils.js.map +2 -2
- package/build-module/components/background-image-control/index.js +1 -1
- package/build-module/components/background-image-control/index.js.map +2 -2
- package/build-module/components/block-list/block.js +3 -3
- package/build-module/components/block-list/block.js.map +2 -2
- package/build-module/components/block-list/index.js +2 -2
- package/build-module/components/block-list/index.js.map +1 -1
- package/build-module/components/block-quick-navigation/index.js +0 -1
- package/build-module/components/block-quick-navigation/index.js.map +2 -2
- package/build-module/components/global-styles/border-panel.js +1 -2
- package/build-module/components/global-styles/border-panel.js.map +2 -2
- package/build-module/components/global-styles/color-panel.js +1 -2
- package/build-module/components/global-styles/color-panel.js.map +2 -2
- package/build-module/components/global-styles/dimensions-panel.js +1 -2
- package/build-module/components/global-styles/dimensions-panel.js.map +2 -2
- package/build-module/components/global-styles/filters-panel.js +1 -2
- package/build-module/components/global-styles/filters-panel.js.map +2 -2
- package/build-module/components/global-styles/get-block-css-selector.js +54 -0
- package/build-module/components/global-styles/get-block-css-selector.js.map +7 -0
- package/build-module/components/global-styles/hooks.js +95 -27
- package/build-module/components/global-styles/hooks.js.map +2 -2
- package/build-module/components/global-styles/index.js +14 -0
- package/build-module/components/global-styles/index.js.map +2 -2
- package/build-module/components/global-styles/typography-panel.js +19 -3
- package/build-module/components/global-styles/typography-panel.js.map +2 -2
- package/build-module/components/global-styles/typography-utils.js +49 -1
- package/build-module/components/global-styles/typography-utils.js.map +2 -2
- package/build-module/components/global-styles/use-global-styles-output.js +979 -0
- package/build-module/components/global-styles/use-global-styles-output.js.map +7 -0
- package/build-module/components/global-styles/utils.js +364 -0
- package/build-module/components/global-styles/utils.js.map +2 -2
- package/build-module/components/rich-text/index.js +8 -7
- package/build-module/components/rich-text/index.js.map +2 -2
- package/build-module/hooks/block-bindings.js +112 -172
- package/build-module/hooks/block-bindings.js.map +2 -2
- package/build-module/hooks/block-style-variation.js +12 -4
- package/build-module/hooks/block-style-variation.js.map +2 -2
- package/build-module/hooks/custom-class-name.js +1 -1
- package/build-module/hooks/custom-class-name.js.map +1 -1
- package/build-module/hooks/duotone.js +3 -3
- package/build-module/hooks/duotone.js.map +2 -2
- package/build-module/hooks/fit-text.js +32 -19
- package/build-module/hooks/fit-text.js.map +2 -2
- package/build-module/hooks/font-size.js +5 -4
- package/build-module/hooks/font-size.js.map +2 -2
- package/build-module/hooks/metadata.js +46 -1
- package/build-module/hooks/metadata.js.map +2 -2
- package/build-module/hooks/typography.js +11 -4
- package/build-module/hooks/typography.js.map +3 -3
- package/build-module/hooks/use-typography-props.js +1 -1
- package/build-module/hooks/use-typography-props.js.map +2 -2
- package/build-module/store/private-selectors.js +2 -2
- package/build-module/store/private-selectors.js.map +2 -2
- package/build-module/store/selectors.js +39 -14
- package/build-module/store/selectors.js.map +2 -2
- package/build-module/store/utils.js +3 -2
- package/build-module/store/utils.js.map +2 -2
- package/build-module/utils/fit-text-utils.js +4 -4
- package/build-module/utils/fit-text-utils.js.map +2 -2
- package/build-style/style-rtl.css +6 -10
- package/build-style/style.css +6 -10
- package/package.json +35 -36
- package/src/components/background-image-control/index.js +1 -1
- package/src/components/block-card/style.scss +1 -1
- package/src/components/block-list/block.js +1 -1
- package/src/components/block-list/index.js +2 -2
- package/src/components/block-navigation/style.scss +1 -1
- package/src/components/block-quick-navigation/index.js +0 -1
- package/src/components/block-switcher/style.scss +1 -1
- package/src/components/color-palette/test/__snapshots__/control.js.snap +1 -1
- package/src/components/global-styles/border-panel.js +1 -2
- package/src/components/global-styles/color-panel.js +1 -2
- package/src/components/global-styles/color-panel.native.js +1 -1
- package/src/components/global-styles/dimensions-panel.js +1 -2
- package/src/components/global-styles/filters-panel.js +1 -2
- package/src/components/global-styles/get-block-css-selector.js +114 -0
- package/src/components/global-styles/hooks.js +108 -29
- package/src/components/global-styles/index.js +8 -0
- package/src/components/global-styles/test/typography-utils.js +806 -0
- package/src/components/global-styles/test/use-global-styles-output.js +1131 -0
- package/src/components/global-styles/test/utils.js +442 -1
- package/src/components/global-styles/typography-panel.js +27 -3
- package/src/components/global-styles/typography-utils.js +133 -0
- package/src/components/global-styles/use-global-styles-output.js +1487 -0
- package/src/components/global-styles/utils.js +537 -0
- package/src/components/inserter/style.scss +2 -2
- package/src/components/multi-selection-inspector/style.scss +1 -1
- package/src/components/rich-text/index.js +8 -14
- package/src/hooks/block-bindings.js +79 -153
- package/src/hooks/block-style-variation.js +12 -4
- package/src/hooks/custom-class-name.js +1 -1
- package/src/hooks/duotone.js +3 -3
- package/src/hooks/fit-text.js +37 -28
- package/src/hooks/font-size.js +8 -4
- package/src/hooks/metadata.js +89 -0
- package/src/hooks/test/metadata.js +316 -0
- package/src/hooks/typography.js +15 -4
- package/src/hooks/use-typography-props.js +1 -1
- package/src/store/private-selectors.js +2 -2
- package/src/store/selectors.js +59 -21
- package/src/store/test/selectors.js +1 -1
- package/src/store/utils.js +2 -1
- package/src/style.scss +0 -1
- package/src/utils/fit-text-utils.js +4 -16
- package/tsconfig.json +0 -1
- package/src/components/block-quick-navigation/style.scss +0 -5
|
@@ -0,0 +1,979 @@
|
|
|
1
|
+
import {
|
|
2
|
+
__EXPERIMENTAL_STYLE_PROPERTY as STYLE_PROPERTY,
|
|
3
|
+
__EXPERIMENTAL_ELEMENTS as ELEMENTS,
|
|
4
|
+
getBlockSupport,
|
|
5
|
+
getBlockTypes,
|
|
6
|
+
store as blocksStore
|
|
7
|
+
} from "@wordpress/blocks";
|
|
8
|
+
import { useSelect } from "@wordpress/data";
|
|
9
|
+
import { useContext, useMemo } from "@wordpress/element";
|
|
10
|
+
import { getCSSRules, getCSSValueFromRawStyle } from "@wordpress/style-engine";
|
|
11
|
+
import { privateApis as componentsPrivateApis } from "@wordpress/components";
|
|
12
|
+
import {
|
|
13
|
+
PRESET_METADATA,
|
|
14
|
+
ROOT_BLOCK_SELECTOR,
|
|
15
|
+
ROOT_CSS_PROPERTIES_SELECTOR,
|
|
16
|
+
scopeSelector,
|
|
17
|
+
scopeFeatureSelectors,
|
|
18
|
+
appendToSelector,
|
|
19
|
+
getBlockStyleVariationSelector,
|
|
20
|
+
getResolvedValue
|
|
21
|
+
} from "./utils";
|
|
22
|
+
import { getBlockCSSSelector } from "./get-block-css-selector";
|
|
23
|
+
import { getTypographyFontSizeValue } from "./typography-utils";
|
|
24
|
+
import { GlobalStylesContext } from "./context";
|
|
25
|
+
import { useGlobalSetting } from "./hooks";
|
|
26
|
+
import { getDuotoneFilter } from "../duotone/utils";
|
|
27
|
+
import { getGapCSSValue } from "../../hooks/gap";
|
|
28
|
+
import { setBackgroundStyleDefaults } from "../../hooks/background";
|
|
29
|
+
import { store as blockEditorStore } from "../../store";
|
|
30
|
+
import { LAYOUT_DEFINITIONS } from "../../layouts/definitions";
|
|
31
|
+
import { getValueFromObjectPath, setImmutably } from "../../utils/object";
|
|
32
|
+
import { unlock } from "../../lock-unlock";
|
|
33
|
+
const ELEMENT_CLASS_NAMES = {
|
|
34
|
+
button: "wp-element-button",
|
|
35
|
+
caption: "wp-element-caption"
|
|
36
|
+
};
|
|
37
|
+
const BLOCK_SUPPORT_FEATURE_LEVEL_SELECTORS = {
|
|
38
|
+
__experimentalBorder: "border",
|
|
39
|
+
color: "color",
|
|
40
|
+
spacing: "spacing",
|
|
41
|
+
typography: "typography"
|
|
42
|
+
};
|
|
43
|
+
const { kebabCase } = unlock(componentsPrivateApis);
|
|
44
|
+
function getPresetsDeclarations(blockPresets = {}, mergedSettings) {
|
|
45
|
+
return PRESET_METADATA.reduce(
|
|
46
|
+
(declarations, { path, valueKey, valueFunc, cssVarInfix }) => {
|
|
47
|
+
const presetByOrigin = getValueFromObjectPath(
|
|
48
|
+
blockPresets,
|
|
49
|
+
path,
|
|
50
|
+
[]
|
|
51
|
+
);
|
|
52
|
+
["default", "theme", "custom"].forEach((origin) => {
|
|
53
|
+
if (presetByOrigin[origin]) {
|
|
54
|
+
presetByOrigin[origin].forEach((value) => {
|
|
55
|
+
if (valueKey && !valueFunc) {
|
|
56
|
+
declarations.push(
|
|
57
|
+
`--wp--preset--${cssVarInfix}--${kebabCase(
|
|
58
|
+
value.slug
|
|
59
|
+
)}: ${value[valueKey]}`
|
|
60
|
+
);
|
|
61
|
+
} else if (valueFunc && typeof valueFunc === "function") {
|
|
62
|
+
declarations.push(
|
|
63
|
+
`--wp--preset--${cssVarInfix}--${kebabCase(
|
|
64
|
+
value.slug
|
|
65
|
+
)}: ${valueFunc(value, mergedSettings)}`
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
return declarations;
|
|
72
|
+
},
|
|
73
|
+
[]
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
function getPresetsClasses(blockSelector = "*", blockPresets = {}) {
|
|
77
|
+
return PRESET_METADATA.reduce(
|
|
78
|
+
(declarations, { path, cssVarInfix, classes }) => {
|
|
79
|
+
if (!classes) {
|
|
80
|
+
return declarations;
|
|
81
|
+
}
|
|
82
|
+
const presetByOrigin = getValueFromObjectPath(
|
|
83
|
+
blockPresets,
|
|
84
|
+
path,
|
|
85
|
+
[]
|
|
86
|
+
);
|
|
87
|
+
["default", "theme", "custom"].forEach((origin) => {
|
|
88
|
+
if (presetByOrigin[origin]) {
|
|
89
|
+
presetByOrigin[origin].forEach(({ slug }) => {
|
|
90
|
+
classes.forEach(({ classSuffix, propertyName }) => {
|
|
91
|
+
const classSelectorToUse = `.has-${kebabCase(
|
|
92
|
+
slug
|
|
93
|
+
)}-${classSuffix}`;
|
|
94
|
+
const selectorToUse = blockSelector.split(",").map(
|
|
95
|
+
(selector) => `${selector}${classSelectorToUse}`
|
|
96
|
+
).join(",");
|
|
97
|
+
const value = `var(--wp--preset--${cssVarInfix}--${kebabCase(
|
|
98
|
+
slug
|
|
99
|
+
)})`;
|
|
100
|
+
declarations += `${selectorToUse}{${propertyName}: ${value} !important;}`;
|
|
101
|
+
});
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
return declarations;
|
|
106
|
+
},
|
|
107
|
+
""
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
function getPresetsSvgFilters(blockPresets = {}) {
|
|
111
|
+
return PRESET_METADATA.filter(
|
|
112
|
+
// Duotone are the only type of filters for now.
|
|
113
|
+
(metadata) => metadata.path.at(-1) === "duotone"
|
|
114
|
+
).flatMap((metadata) => {
|
|
115
|
+
const presetByOrigin = getValueFromObjectPath(
|
|
116
|
+
blockPresets,
|
|
117
|
+
metadata.path,
|
|
118
|
+
{}
|
|
119
|
+
);
|
|
120
|
+
return ["default", "theme"].filter((origin) => presetByOrigin[origin]).flatMap(
|
|
121
|
+
(origin) => presetByOrigin[origin].map(
|
|
122
|
+
(preset) => getDuotoneFilter(
|
|
123
|
+
`wp-duotone-${preset.slug}`,
|
|
124
|
+
preset.colors
|
|
125
|
+
)
|
|
126
|
+
)
|
|
127
|
+
).join("");
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
function flattenTree(input = {}, prefix, token) {
|
|
131
|
+
let result = [];
|
|
132
|
+
Object.keys(input).forEach((key) => {
|
|
133
|
+
const newKey = prefix + kebabCase(key.replace("/", "-"));
|
|
134
|
+
const newLeaf = input[key];
|
|
135
|
+
if (newLeaf instanceof Object) {
|
|
136
|
+
const newPrefix = newKey + token;
|
|
137
|
+
result = [...result, ...flattenTree(newLeaf, newPrefix, token)];
|
|
138
|
+
} else {
|
|
139
|
+
result.push(`${newKey}: ${newLeaf}`);
|
|
140
|
+
}
|
|
141
|
+
});
|
|
142
|
+
return result;
|
|
143
|
+
}
|
|
144
|
+
function concatFeatureVariationSelectorString(featureSelector, styleVariationSelector) {
|
|
145
|
+
const featureSelectors = featureSelector.split(",");
|
|
146
|
+
const combinedSelectors = [];
|
|
147
|
+
featureSelectors.forEach((selector) => {
|
|
148
|
+
combinedSelectors.push(
|
|
149
|
+
`${styleVariationSelector.trim()}${selector.trim()}`
|
|
150
|
+
);
|
|
151
|
+
});
|
|
152
|
+
return combinedSelectors.join(", ");
|
|
153
|
+
}
|
|
154
|
+
const getFeatureDeclarations = (selectors, styles) => {
|
|
155
|
+
const declarations = {};
|
|
156
|
+
Object.entries(selectors).forEach(([feature, selector]) => {
|
|
157
|
+
if (feature === "root" || !styles?.[feature]) {
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
const isShorthand = typeof selector === "string";
|
|
161
|
+
if (!isShorthand) {
|
|
162
|
+
Object.entries(selector).forEach(
|
|
163
|
+
([subfeature, subfeatureSelector]) => {
|
|
164
|
+
if (subfeature === "root" || !styles?.[feature][subfeature]) {
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
const subfeatureStyles = {
|
|
168
|
+
[feature]: {
|
|
169
|
+
[subfeature]: styles[feature][subfeature]
|
|
170
|
+
}
|
|
171
|
+
};
|
|
172
|
+
const newDeclarations = getStylesDeclarations(subfeatureStyles);
|
|
173
|
+
declarations[subfeatureSelector] = [
|
|
174
|
+
...declarations[subfeatureSelector] || [],
|
|
175
|
+
...newDeclarations
|
|
176
|
+
];
|
|
177
|
+
delete styles[feature][subfeature];
|
|
178
|
+
}
|
|
179
|
+
);
|
|
180
|
+
}
|
|
181
|
+
if (isShorthand || selector.root) {
|
|
182
|
+
const featureSelector = isShorthand ? selector : selector.root;
|
|
183
|
+
const featureStyles = { [feature]: styles[feature] };
|
|
184
|
+
const newDeclarations = getStylesDeclarations(featureStyles);
|
|
185
|
+
declarations[featureSelector] = [
|
|
186
|
+
...declarations[featureSelector] || [],
|
|
187
|
+
...newDeclarations
|
|
188
|
+
];
|
|
189
|
+
delete styles[feature];
|
|
190
|
+
}
|
|
191
|
+
});
|
|
192
|
+
return declarations;
|
|
193
|
+
};
|
|
194
|
+
function getStylesDeclarations(blockStyles = {}, selector = "", useRootPaddingAlign, tree = {}, disableRootPadding = false) {
|
|
195
|
+
const isRoot = ROOT_BLOCK_SELECTOR === selector;
|
|
196
|
+
const output = Object.entries(STYLE_PROPERTY).reduce(
|
|
197
|
+
(declarations, [key, { value, properties, useEngine, rootOnly }]) => {
|
|
198
|
+
if (rootOnly && !isRoot) {
|
|
199
|
+
return declarations;
|
|
200
|
+
}
|
|
201
|
+
const pathToValue = value;
|
|
202
|
+
if (pathToValue[0] === "elements" || useEngine) {
|
|
203
|
+
return declarations;
|
|
204
|
+
}
|
|
205
|
+
const styleValue = getValueFromObjectPath(
|
|
206
|
+
blockStyles,
|
|
207
|
+
pathToValue
|
|
208
|
+
);
|
|
209
|
+
if (key === "--wp--style--root--padding" && (typeof styleValue === "string" || !useRootPaddingAlign)) {
|
|
210
|
+
return declarations;
|
|
211
|
+
}
|
|
212
|
+
if (properties && typeof styleValue !== "string") {
|
|
213
|
+
Object.entries(properties).forEach((entry) => {
|
|
214
|
+
const [name, prop] = entry;
|
|
215
|
+
if (!getValueFromObjectPath(styleValue, [prop], false)) {
|
|
216
|
+
return;
|
|
217
|
+
}
|
|
218
|
+
const cssProperty = name.startsWith("--") ? name : kebabCase(name);
|
|
219
|
+
declarations.push(
|
|
220
|
+
`${cssProperty}: ${getCSSValueFromRawStyle(
|
|
221
|
+
getValueFromObjectPath(styleValue, [prop])
|
|
222
|
+
)}`
|
|
223
|
+
);
|
|
224
|
+
});
|
|
225
|
+
} else if (getValueFromObjectPath(blockStyles, pathToValue, false)) {
|
|
226
|
+
const cssProperty = key.startsWith("--") ? key : kebabCase(key);
|
|
227
|
+
declarations.push(
|
|
228
|
+
`${cssProperty}: ${getCSSValueFromRawStyle(
|
|
229
|
+
getValueFromObjectPath(blockStyles, pathToValue)
|
|
230
|
+
)}`
|
|
231
|
+
);
|
|
232
|
+
}
|
|
233
|
+
return declarations;
|
|
234
|
+
},
|
|
235
|
+
[]
|
|
236
|
+
);
|
|
237
|
+
if (!!blockStyles.background) {
|
|
238
|
+
if (blockStyles.background?.backgroundImage) {
|
|
239
|
+
blockStyles.background.backgroundImage = getResolvedValue(
|
|
240
|
+
blockStyles.background.backgroundImage,
|
|
241
|
+
tree
|
|
242
|
+
);
|
|
243
|
+
}
|
|
244
|
+
if (!isRoot && !!blockStyles.background?.backgroundImage?.id) {
|
|
245
|
+
blockStyles = {
|
|
246
|
+
...blockStyles,
|
|
247
|
+
background: {
|
|
248
|
+
...blockStyles.background,
|
|
249
|
+
...setBackgroundStyleDefaults(blockStyles.background)
|
|
250
|
+
}
|
|
251
|
+
};
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
const extraRules = getCSSRules(blockStyles);
|
|
255
|
+
extraRules.forEach((rule) => {
|
|
256
|
+
if (isRoot && (useRootPaddingAlign || disableRootPadding) && rule.key.startsWith("padding")) {
|
|
257
|
+
return;
|
|
258
|
+
}
|
|
259
|
+
const cssProperty = rule.key.startsWith("--") ? rule.key : kebabCase(rule.key);
|
|
260
|
+
let ruleValue = getResolvedValue(rule.value, tree, null);
|
|
261
|
+
if (cssProperty === "font-size") {
|
|
262
|
+
ruleValue = getTypographyFontSizeValue(
|
|
263
|
+
{ size: ruleValue },
|
|
264
|
+
tree?.settings
|
|
265
|
+
);
|
|
266
|
+
}
|
|
267
|
+
if (cssProperty === "aspect-ratio") {
|
|
268
|
+
output.push("min-height: unset");
|
|
269
|
+
}
|
|
270
|
+
output.push(`${cssProperty}: ${ruleValue}`);
|
|
271
|
+
});
|
|
272
|
+
return output;
|
|
273
|
+
}
|
|
274
|
+
function getLayoutStyles({
|
|
275
|
+
layoutDefinitions = LAYOUT_DEFINITIONS,
|
|
276
|
+
style,
|
|
277
|
+
selector,
|
|
278
|
+
hasBlockGapSupport,
|
|
279
|
+
hasFallbackGapSupport,
|
|
280
|
+
fallbackGapValue
|
|
281
|
+
}) {
|
|
282
|
+
let ruleset = "";
|
|
283
|
+
let gapValue = hasBlockGapSupport ? getGapCSSValue(style?.spacing?.blockGap) : "";
|
|
284
|
+
if (hasFallbackGapSupport) {
|
|
285
|
+
if (selector === ROOT_BLOCK_SELECTOR) {
|
|
286
|
+
gapValue = !gapValue ? "0.5em" : gapValue;
|
|
287
|
+
} else if (!hasBlockGapSupport && fallbackGapValue) {
|
|
288
|
+
gapValue = fallbackGapValue;
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
if (gapValue && layoutDefinitions) {
|
|
292
|
+
Object.values(layoutDefinitions).forEach(
|
|
293
|
+
({ className, name, spacingStyles }) => {
|
|
294
|
+
if (!hasBlockGapSupport && "flex" !== name && "grid" !== name) {
|
|
295
|
+
return;
|
|
296
|
+
}
|
|
297
|
+
if (spacingStyles?.length) {
|
|
298
|
+
spacingStyles.forEach((spacingStyle) => {
|
|
299
|
+
const declarations = [];
|
|
300
|
+
if (spacingStyle.rules) {
|
|
301
|
+
Object.entries(spacingStyle.rules).forEach(
|
|
302
|
+
([cssProperty, cssValue]) => {
|
|
303
|
+
declarations.push(
|
|
304
|
+
`${cssProperty}: ${cssValue ? cssValue : gapValue}`
|
|
305
|
+
);
|
|
306
|
+
}
|
|
307
|
+
);
|
|
308
|
+
}
|
|
309
|
+
if (declarations.length) {
|
|
310
|
+
let combinedSelector = "";
|
|
311
|
+
if (!hasBlockGapSupport) {
|
|
312
|
+
combinedSelector = selector === ROOT_BLOCK_SELECTOR ? `:where(.${className}${spacingStyle?.selector || ""})` : `:where(${selector}.${className}${spacingStyle?.selector || ""})`;
|
|
313
|
+
} else {
|
|
314
|
+
combinedSelector = selector === ROOT_BLOCK_SELECTOR ? `:root :where(.${className})${spacingStyle?.selector || ""}` : `:root :where(${selector}-${className})${spacingStyle?.selector || ""}`;
|
|
315
|
+
}
|
|
316
|
+
ruleset += `${combinedSelector} { ${declarations.join(
|
|
317
|
+
"; "
|
|
318
|
+
)}; }`;
|
|
319
|
+
}
|
|
320
|
+
});
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
);
|
|
324
|
+
if (selector === ROOT_BLOCK_SELECTOR && hasBlockGapSupport) {
|
|
325
|
+
ruleset += `${ROOT_CSS_PROPERTIES_SELECTOR} { --wp--style--block-gap: ${gapValue}; }`;
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
if (selector === ROOT_BLOCK_SELECTOR && layoutDefinitions) {
|
|
329
|
+
const validDisplayModes = ["block", "flex", "grid"];
|
|
330
|
+
Object.values(layoutDefinitions).forEach(
|
|
331
|
+
({ className, displayMode, baseStyles }) => {
|
|
332
|
+
if (displayMode && validDisplayModes.includes(displayMode)) {
|
|
333
|
+
ruleset += `${selector} .${className} { display:${displayMode}; }`;
|
|
334
|
+
}
|
|
335
|
+
if (baseStyles?.length) {
|
|
336
|
+
baseStyles.forEach((baseStyle) => {
|
|
337
|
+
const declarations = [];
|
|
338
|
+
if (baseStyle.rules) {
|
|
339
|
+
Object.entries(baseStyle.rules).forEach(
|
|
340
|
+
([cssProperty, cssValue]) => {
|
|
341
|
+
declarations.push(
|
|
342
|
+
`${cssProperty}: ${cssValue}`
|
|
343
|
+
);
|
|
344
|
+
}
|
|
345
|
+
);
|
|
346
|
+
}
|
|
347
|
+
if (declarations.length) {
|
|
348
|
+
const combinedSelector = `.${className}${baseStyle?.selector || ""}`;
|
|
349
|
+
ruleset += `${combinedSelector} { ${declarations.join(
|
|
350
|
+
"; "
|
|
351
|
+
)}; }`;
|
|
352
|
+
}
|
|
353
|
+
});
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
);
|
|
357
|
+
}
|
|
358
|
+
return ruleset;
|
|
359
|
+
}
|
|
360
|
+
const STYLE_KEYS = [
|
|
361
|
+
"border",
|
|
362
|
+
"color",
|
|
363
|
+
"dimensions",
|
|
364
|
+
"spacing",
|
|
365
|
+
"typography",
|
|
366
|
+
"filter",
|
|
367
|
+
"outline",
|
|
368
|
+
"shadow",
|
|
369
|
+
"background"
|
|
370
|
+
];
|
|
371
|
+
function pickStyleKeys(treeToPickFrom) {
|
|
372
|
+
if (!treeToPickFrom) {
|
|
373
|
+
return {};
|
|
374
|
+
}
|
|
375
|
+
const entries = Object.entries(treeToPickFrom);
|
|
376
|
+
const pickedEntries = entries.filter(
|
|
377
|
+
([key]) => STYLE_KEYS.includes(key)
|
|
378
|
+
);
|
|
379
|
+
const clonedEntries = pickedEntries.map(([key, style]) => [
|
|
380
|
+
key,
|
|
381
|
+
JSON.parse(JSON.stringify(style))
|
|
382
|
+
]);
|
|
383
|
+
return Object.fromEntries(clonedEntries);
|
|
384
|
+
}
|
|
385
|
+
const getNodesWithStyles = (tree, blockSelectors) => {
|
|
386
|
+
const nodes = [];
|
|
387
|
+
if (!tree?.styles) {
|
|
388
|
+
return nodes;
|
|
389
|
+
}
|
|
390
|
+
const styles = pickStyleKeys(tree.styles);
|
|
391
|
+
if (styles) {
|
|
392
|
+
nodes.push({
|
|
393
|
+
styles,
|
|
394
|
+
selector: ROOT_BLOCK_SELECTOR,
|
|
395
|
+
// Root selector (body) styles should not be wrapped in `:root where()` to keep
|
|
396
|
+
// specificity at (0,0,1) and maintain backwards compatibility.
|
|
397
|
+
skipSelectorWrapper: true
|
|
398
|
+
});
|
|
399
|
+
}
|
|
400
|
+
Object.entries(ELEMENTS).forEach(([name, selector]) => {
|
|
401
|
+
if (tree.styles?.elements?.[name]) {
|
|
402
|
+
nodes.push({
|
|
403
|
+
styles: tree.styles?.elements?.[name],
|
|
404
|
+
selector,
|
|
405
|
+
// Top level elements that don't use a class name should not receive the
|
|
406
|
+
// `:root :where()` wrapper to maintain backwards compatibility.
|
|
407
|
+
skipSelectorWrapper: !ELEMENT_CLASS_NAMES[name]
|
|
408
|
+
});
|
|
409
|
+
}
|
|
410
|
+
});
|
|
411
|
+
Object.entries(tree.styles?.blocks ?? {}).forEach(
|
|
412
|
+
([blockName, node]) => {
|
|
413
|
+
const blockStyles = pickStyleKeys(node);
|
|
414
|
+
if (node?.variations) {
|
|
415
|
+
const variations = {};
|
|
416
|
+
Object.entries(node.variations).forEach(
|
|
417
|
+
([variationName, variation]) => {
|
|
418
|
+
variations[variationName] = pickStyleKeys(variation);
|
|
419
|
+
if (variation?.css) {
|
|
420
|
+
variations[variationName].css = variation.css;
|
|
421
|
+
}
|
|
422
|
+
const variationSelector = blockSelectors[blockName]?.styleVariationSelectors?.[variationName];
|
|
423
|
+
Object.entries(variation?.elements ?? {}).forEach(
|
|
424
|
+
([element, elementStyles]) => {
|
|
425
|
+
if (elementStyles && ELEMENTS[element]) {
|
|
426
|
+
nodes.push({
|
|
427
|
+
styles: elementStyles,
|
|
428
|
+
selector: scopeSelector(
|
|
429
|
+
variationSelector,
|
|
430
|
+
ELEMENTS[element]
|
|
431
|
+
)
|
|
432
|
+
});
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
);
|
|
436
|
+
Object.entries(variation?.blocks ?? {}).forEach(
|
|
437
|
+
([
|
|
438
|
+
variationBlockName,
|
|
439
|
+
variationBlockStyles
|
|
440
|
+
]) => {
|
|
441
|
+
const variationBlockSelector = scopeSelector(
|
|
442
|
+
variationSelector,
|
|
443
|
+
blockSelectors[variationBlockName]?.selector
|
|
444
|
+
);
|
|
445
|
+
const variationDuotoneSelector = scopeSelector(
|
|
446
|
+
variationSelector,
|
|
447
|
+
blockSelectors[variationBlockName]?.duotoneSelector
|
|
448
|
+
);
|
|
449
|
+
const variationFeatureSelectors = scopeFeatureSelectors(
|
|
450
|
+
variationSelector,
|
|
451
|
+
blockSelectors[variationBlockName]?.featureSelectors
|
|
452
|
+
);
|
|
453
|
+
const variationBlockStyleNodes = pickStyleKeys(variationBlockStyles);
|
|
454
|
+
if (variationBlockStyles?.css) {
|
|
455
|
+
variationBlockStyleNodes.css = variationBlockStyles.css;
|
|
456
|
+
}
|
|
457
|
+
nodes.push({
|
|
458
|
+
selector: variationBlockSelector,
|
|
459
|
+
duotoneSelector: variationDuotoneSelector,
|
|
460
|
+
featureSelectors: variationFeatureSelectors,
|
|
461
|
+
fallbackGapValue: blockSelectors[variationBlockName]?.fallbackGapValue,
|
|
462
|
+
hasLayoutSupport: blockSelectors[variationBlockName]?.hasLayoutSupport,
|
|
463
|
+
styles: variationBlockStyleNodes
|
|
464
|
+
});
|
|
465
|
+
Object.entries(
|
|
466
|
+
variationBlockStyles.elements ?? {}
|
|
467
|
+
).forEach(
|
|
468
|
+
([
|
|
469
|
+
variationBlockElement,
|
|
470
|
+
variationBlockElementStyles
|
|
471
|
+
]) => {
|
|
472
|
+
if (variationBlockElementStyles && ELEMENTS[variationBlockElement]) {
|
|
473
|
+
nodes.push({
|
|
474
|
+
styles: variationBlockElementStyles,
|
|
475
|
+
selector: scopeSelector(
|
|
476
|
+
variationBlockSelector,
|
|
477
|
+
ELEMENTS[variationBlockElement]
|
|
478
|
+
)
|
|
479
|
+
});
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
);
|
|
483
|
+
}
|
|
484
|
+
);
|
|
485
|
+
}
|
|
486
|
+
);
|
|
487
|
+
blockStyles.variations = variations;
|
|
488
|
+
}
|
|
489
|
+
if (blockSelectors?.[blockName]?.selector) {
|
|
490
|
+
nodes.push({
|
|
491
|
+
duotoneSelector: blockSelectors[blockName].duotoneSelector,
|
|
492
|
+
fallbackGapValue: blockSelectors[blockName].fallbackGapValue,
|
|
493
|
+
hasLayoutSupport: blockSelectors[blockName].hasLayoutSupport,
|
|
494
|
+
selector: blockSelectors[blockName].selector,
|
|
495
|
+
styles: blockStyles,
|
|
496
|
+
featureSelectors: blockSelectors[blockName].featureSelectors,
|
|
497
|
+
styleVariationSelectors: blockSelectors[blockName].styleVariationSelectors
|
|
498
|
+
});
|
|
499
|
+
}
|
|
500
|
+
Object.entries(node?.elements ?? {}).forEach(
|
|
501
|
+
([elementName, value]) => {
|
|
502
|
+
if (value && blockSelectors?.[blockName] && ELEMENTS[elementName]) {
|
|
503
|
+
nodes.push({
|
|
504
|
+
styles: value,
|
|
505
|
+
selector: blockSelectors[blockName]?.selector.split(",").map((sel) => {
|
|
506
|
+
const elementSelectors = ELEMENTS[elementName].split(",");
|
|
507
|
+
return elementSelectors.map(
|
|
508
|
+
(elementSelector) => sel + " " + elementSelector
|
|
509
|
+
);
|
|
510
|
+
}).join(",")
|
|
511
|
+
});
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
);
|
|
515
|
+
}
|
|
516
|
+
);
|
|
517
|
+
return nodes;
|
|
518
|
+
};
|
|
519
|
+
const getNodesWithSettings = (tree, blockSelectors) => {
|
|
520
|
+
const nodes = [];
|
|
521
|
+
if (!tree?.settings) {
|
|
522
|
+
return nodes;
|
|
523
|
+
}
|
|
524
|
+
const pickPresets = (treeToPickFrom) => {
|
|
525
|
+
let presets2 = {};
|
|
526
|
+
PRESET_METADATA.forEach(({ path }) => {
|
|
527
|
+
const value = getValueFromObjectPath(treeToPickFrom, path, false);
|
|
528
|
+
if (value !== false) {
|
|
529
|
+
presets2 = setImmutably(presets2, path, value);
|
|
530
|
+
}
|
|
531
|
+
});
|
|
532
|
+
return presets2;
|
|
533
|
+
};
|
|
534
|
+
const presets = pickPresets(tree.settings);
|
|
535
|
+
const custom = tree.settings?.custom;
|
|
536
|
+
if (Object.keys(presets).length > 0 || custom) {
|
|
537
|
+
nodes.push({
|
|
538
|
+
presets,
|
|
539
|
+
custom,
|
|
540
|
+
selector: ROOT_CSS_PROPERTIES_SELECTOR
|
|
541
|
+
});
|
|
542
|
+
}
|
|
543
|
+
Object.entries(tree.settings?.blocks ?? {}).forEach(
|
|
544
|
+
([blockName, node]) => {
|
|
545
|
+
const blockPresets = pickPresets(node);
|
|
546
|
+
const blockCustom = node.custom;
|
|
547
|
+
if (Object.keys(blockPresets).length > 0 || blockCustom) {
|
|
548
|
+
nodes.push({
|
|
549
|
+
presets: blockPresets,
|
|
550
|
+
custom: blockCustom,
|
|
551
|
+
selector: blockSelectors[blockName]?.selector
|
|
552
|
+
});
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
);
|
|
556
|
+
return nodes;
|
|
557
|
+
};
|
|
558
|
+
const toCustomProperties = (tree, blockSelectors) => {
|
|
559
|
+
const settings = getNodesWithSettings(tree, blockSelectors);
|
|
560
|
+
let ruleset = "";
|
|
561
|
+
settings.forEach(({ presets, custom, selector }) => {
|
|
562
|
+
const declarations = getPresetsDeclarations(presets, tree?.settings);
|
|
563
|
+
const customProps = flattenTree(custom, "--wp--custom--", "--");
|
|
564
|
+
if (customProps.length > 0) {
|
|
565
|
+
declarations.push(...customProps);
|
|
566
|
+
}
|
|
567
|
+
if (declarations.length > 0) {
|
|
568
|
+
ruleset += `${selector}{${declarations.join(";")};}`;
|
|
569
|
+
}
|
|
570
|
+
});
|
|
571
|
+
return ruleset;
|
|
572
|
+
};
|
|
573
|
+
const toStyles = (tree, blockSelectors, hasBlockGapSupport, hasFallbackGapSupport, disableLayoutStyles = false, disableRootPadding = false, styleOptions = void 0) => {
|
|
574
|
+
const options = {
|
|
575
|
+
blockGap: true,
|
|
576
|
+
blockStyles: true,
|
|
577
|
+
layoutStyles: true,
|
|
578
|
+
marginReset: true,
|
|
579
|
+
presets: true,
|
|
580
|
+
rootPadding: true,
|
|
581
|
+
variationStyles: false,
|
|
582
|
+
...styleOptions
|
|
583
|
+
};
|
|
584
|
+
const nodesWithStyles = getNodesWithStyles(tree, blockSelectors);
|
|
585
|
+
const nodesWithSettings = getNodesWithSettings(tree, blockSelectors);
|
|
586
|
+
const useRootPaddingAlign = tree?.settings?.useRootPaddingAwareAlignments;
|
|
587
|
+
const { contentSize, wideSize } = tree?.settings?.layout || {};
|
|
588
|
+
const hasBodyStyles = options.marginReset || options.rootPadding || options.layoutStyles;
|
|
589
|
+
let ruleset = "";
|
|
590
|
+
if (options.presets && (contentSize || wideSize)) {
|
|
591
|
+
ruleset += `${ROOT_CSS_PROPERTIES_SELECTOR} {`;
|
|
592
|
+
ruleset = contentSize ? ruleset + ` --wp--style--global--content-size: ${contentSize};` : ruleset;
|
|
593
|
+
ruleset = wideSize ? ruleset + ` --wp--style--global--wide-size: ${wideSize};` : ruleset;
|
|
594
|
+
ruleset += "}";
|
|
595
|
+
}
|
|
596
|
+
if (hasBodyStyles) {
|
|
597
|
+
ruleset += ":where(body) {margin: 0;";
|
|
598
|
+
if (options.rootPadding && useRootPaddingAlign) {
|
|
599
|
+
ruleset += `padding-right: 0; padding-left: 0; padding-top: var(--wp--style--root--padding-top); padding-bottom: var(--wp--style--root--padding-bottom) }
|
|
600
|
+
.has-global-padding { padding-right: var(--wp--style--root--padding-right); padding-left: var(--wp--style--root--padding-left); }
|
|
601
|
+
.has-global-padding > .alignfull { margin-right: calc(var(--wp--style--root--padding-right) * -1); margin-left: calc(var(--wp--style--root--padding-left) * -1); }
|
|
602
|
+
.has-global-padding :where(:not(.alignfull.is-layout-flow) > .has-global-padding:not(.wp-block-block, .alignfull)) { padding-right: 0; padding-left: 0; }
|
|
603
|
+
.has-global-padding :where(:not(.alignfull.is-layout-flow) > .has-global-padding:not(.wp-block-block, .alignfull)) > .alignfull { margin-left: 0; margin-right: 0;
|
|
604
|
+
`;
|
|
605
|
+
}
|
|
606
|
+
ruleset += "}";
|
|
607
|
+
}
|
|
608
|
+
if (options.blockStyles) {
|
|
609
|
+
nodesWithStyles.forEach(
|
|
610
|
+
({
|
|
611
|
+
selector,
|
|
612
|
+
duotoneSelector,
|
|
613
|
+
styles,
|
|
614
|
+
fallbackGapValue,
|
|
615
|
+
hasLayoutSupport,
|
|
616
|
+
featureSelectors,
|
|
617
|
+
styleVariationSelectors,
|
|
618
|
+
skipSelectorWrapper
|
|
619
|
+
}) => {
|
|
620
|
+
if (featureSelectors) {
|
|
621
|
+
const featureDeclarations = getFeatureDeclarations(
|
|
622
|
+
featureSelectors,
|
|
623
|
+
styles
|
|
624
|
+
);
|
|
625
|
+
Object.entries(featureDeclarations).forEach(
|
|
626
|
+
([cssSelector, declarations]) => {
|
|
627
|
+
if (declarations.length) {
|
|
628
|
+
const rules = declarations.join(";");
|
|
629
|
+
ruleset += `:root :where(${cssSelector}){${rules};}`;
|
|
630
|
+
}
|
|
631
|
+
}
|
|
632
|
+
);
|
|
633
|
+
}
|
|
634
|
+
if (duotoneSelector) {
|
|
635
|
+
const duotoneStyles = {};
|
|
636
|
+
if (styles?.filter) {
|
|
637
|
+
duotoneStyles.filter = styles.filter;
|
|
638
|
+
delete styles.filter;
|
|
639
|
+
}
|
|
640
|
+
const duotoneDeclarations = getStylesDeclarations(duotoneStyles);
|
|
641
|
+
if (duotoneDeclarations.length) {
|
|
642
|
+
ruleset += `${duotoneSelector}{${duotoneDeclarations.join(
|
|
643
|
+
";"
|
|
644
|
+
)};}`;
|
|
645
|
+
}
|
|
646
|
+
}
|
|
647
|
+
if (!disableLayoutStyles && (ROOT_BLOCK_SELECTOR === selector || hasLayoutSupport)) {
|
|
648
|
+
ruleset += getLayoutStyles({
|
|
649
|
+
style: styles,
|
|
650
|
+
selector,
|
|
651
|
+
hasBlockGapSupport,
|
|
652
|
+
hasFallbackGapSupport,
|
|
653
|
+
fallbackGapValue
|
|
654
|
+
});
|
|
655
|
+
}
|
|
656
|
+
const styleDeclarations = getStylesDeclarations(
|
|
657
|
+
styles,
|
|
658
|
+
selector,
|
|
659
|
+
useRootPaddingAlign,
|
|
660
|
+
tree,
|
|
661
|
+
disableRootPadding
|
|
662
|
+
);
|
|
663
|
+
if (styleDeclarations?.length) {
|
|
664
|
+
const generalSelector = skipSelectorWrapper ? selector : `:root :where(${selector})`;
|
|
665
|
+
ruleset += `${generalSelector}{${styleDeclarations.join(
|
|
666
|
+
";"
|
|
667
|
+
)};}`;
|
|
668
|
+
}
|
|
669
|
+
if (styles?.css) {
|
|
670
|
+
ruleset += processCSSNesting(
|
|
671
|
+
styles.css,
|
|
672
|
+
`:root :where(${selector})`
|
|
673
|
+
);
|
|
674
|
+
}
|
|
675
|
+
if (options.variationStyles && styleVariationSelectors) {
|
|
676
|
+
Object.entries(styleVariationSelectors).forEach(
|
|
677
|
+
([styleVariationName, styleVariationSelector]) => {
|
|
678
|
+
const styleVariations = styles?.variations?.[styleVariationName];
|
|
679
|
+
if (styleVariations) {
|
|
680
|
+
if (featureSelectors) {
|
|
681
|
+
const featureDeclarations = getFeatureDeclarations(
|
|
682
|
+
featureSelectors,
|
|
683
|
+
styleVariations
|
|
684
|
+
);
|
|
685
|
+
Object.entries(
|
|
686
|
+
featureDeclarations
|
|
687
|
+
).forEach(
|
|
688
|
+
([baseSelector, declarations]) => {
|
|
689
|
+
if (declarations.length) {
|
|
690
|
+
const cssSelector = concatFeatureVariationSelectorString(
|
|
691
|
+
baseSelector,
|
|
692
|
+
styleVariationSelector
|
|
693
|
+
);
|
|
694
|
+
const rules = declarations.join(";");
|
|
695
|
+
ruleset += `:root :where(${cssSelector}){${rules};}`;
|
|
696
|
+
}
|
|
697
|
+
}
|
|
698
|
+
);
|
|
699
|
+
}
|
|
700
|
+
const styleVariationDeclarations = getStylesDeclarations(
|
|
701
|
+
styleVariations,
|
|
702
|
+
styleVariationSelector,
|
|
703
|
+
useRootPaddingAlign,
|
|
704
|
+
tree
|
|
705
|
+
);
|
|
706
|
+
if (styleVariationDeclarations.length) {
|
|
707
|
+
ruleset += `:root :where(${styleVariationSelector}){${styleVariationDeclarations.join(
|
|
708
|
+
";"
|
|
709
|
+
)};}`;
|
|
710
|
+
}
|
|
711
|
+
if (styleVariations?.css) {
|
|
712
|
+
ruleset += processCSSNesting(
|
|
713
|
+
styleVariations.css,
|
|
714
|
+
`:root :where(${styleVariationSelector})`
|
|
715
|
+
);
|
|
716
|
+
}
|
|
717
|
+
}
|
|
718
|
+
}
|
|
719
|
+
);
|
|
720
|
+
}
|
|
721
|
+
const pseudoSelectorStyles = Object.entries(styles).filter(
|
|
722
|
+
([key]) => key.startsWith(":")
|
|
723
|
+
);
|
|
724
|
+
if (pseudoSelectorStyles?.length) {
|
|
725
|
+
pseudoSelectorStyles.forEach(
|
|
726
|
+
([pseudoKey, pseudoStyle]) => {
|
|
727
|
+
const pseudoDeclarations = getStylesDeclarations(pseudoStyle);
|
|
728
|
+
if (!pseudoDeclarations?.length) {
|
|
729
|
+
return;
|
|
730
|
+
}
|
|
731
|
+
const _selector = selector.split(",").map((sel) => sel + pseudoKey).join(",");
|
|
732
|
+
const pseudoRule = `:root :where(${_selector}){${pseudoDeclarations.join(
|
|
733
|
+
";"
|
|
734
|
+
)};}`;
|
|
735
|
+
ruleset += pseudoRule;
|
|
736
|
+
}
|
|
737
|
+
);
|
|
738
|
+
}
|
|
739
|
+
}
|
|
740
|
+
);
|
|
741
|
+
}
|
|
742
|
+
if (options.layoutStyles) {
|
|
743
|
+
ruleset = ruleset + ".wp-site-blocks > .alignleft { float: left; margin-right: 2em; }";
|
|
744
|
+
ruleset = ruleset + ".wp-site-blocks > .alignright { float: right; margin-left: 2em; }";
|
|
745
|
+
ruleset = ruleset + ".wp-site-blocks > .aligncenter { justify-content: center; margin-left: auto; margin-right: auto; }";
|
|
746
|
+
}
|
|
747
|
+
if (options.blockGap && hasBlockGapSupport) {
|
|
748
|
+
const gapValue = getGapCSSValue(tree?.styles?.spacing?.blockGap) || "0.5em";
|
|
749
|
+
ruleset = ruleset + `:root :where(.wp-site-blocks) > * { margin-block-start: ${gapValue}; margin-block-end: 0; }`;
|
|
750
|
+
ruleset = ruleset + ":root :where(.wp-site-blocks) > :first-child { margin-block-start: 0; }";
|
|
751
|
+
ruleset = ruleset + ":root :where(.wp-site-blocks) > :last-child { margin-block-end: 0; }";
|
|
752
|
+
}
|
|
753
|
+
if (options.presets) {
|
|
754
|
+
nodesWithSettings.forEach(({ selector, presets }) => {
|
|
755
|
+
if (ROOT_BLOCK_SELECTOR === selector || ROOT_CSS_PROPERTIES_SELECTOR === selector) {
|
|
756
|
+
selector = "";
|
|
757
|
+
}
|
|
758
|
+
const classes = getPresetsClasses(selector, presets);
|
|
759
|
+
if (classes.length > 0) {
|
|
760
|
+
ruleset += classes;
|
|
761
|
+
}
|
|
762
|
+
});
|
|
763
|
+
}
|
|
764
|
+
return ruleset;
|
|
765
|
+
};
|
|
766
|
+
function toSvgFilters(tree, blockSelectors) {
|
|
767
|
+
const nodesWithSettings = getNodesWithSettings(tree, blockSelectors);
|
|
768
|
+
return nodesWithSettings.flatMap(({ presets }) => {
|
|
769
|
+
return getPresetsSvgFilters(presets);
|
|
770
|
+
});
|
|
771
|
+
}
|
|
772
|
+
const getSelectorsConfig = (blockType, rootSelector) => {
|
|
773
|
+
if (blockType?.selectors && Object.keys(blockType.selectors).length > 0) {
|
|
774
|
+
return blockType.selectors;
|
|
775
|
+
}
|
|
776
|
+
const config = { root: rootSelector };
|
|
777
|
+
Object.entries(BLOCK_SUPPORT_FEATURE_LEVEL_SELECTORS).forEach(
|
|
778
|
+
([featureKey, featureName]) => {
|
|
779
|
+
const featureSelector = getBlockCSSSelector(
|
|
780
|
+
blockType,
|
|
781
|
+
featureKey
|
|
782
|
+
);
|
|
783
|
+
if (featureSelector) {
|
|
784
|
+
config[featureName] = featureSelector;
|
|
785
|
+
}
|
|
786
|
+
}
|
|
787
|
+
);
|
|
788
|
+
return config;
|
|
789
|
+
};
|
|
790
|
+
const getBlockSelectors = (blockTypes, getBlockStyles, variationInstanceId) => {
|
|
791
|
+
const result = {};
|
|
792
|
+
blockTypes.forEach((blockType) => {
|
|
793
|
+
const name = blockType.name;
|
|
794
|
+
const selector = getBlockCSSSelector(blockType);
|
|
795
|
+
let duotoneSelector = getBlockCSSSelector(
|
|
796
|
+
blockType,
|
|
797
|
+
"filter.duotone"
|
|
798
|
+
);
|
|
799
|
+
if (!duotoneSelector) {
|
|
800
|
+
const rootSelector = getBlockCSSSelector(blockType);
|
|
801
|
+
const duotoneSupport = getBlockSupport(
|
|
802
|
+
blockType,
|
|
803
|
+
"color.__experimentalDuotone",
|
|
804
|
+
false
|
|
805
|
+
);
|
|
806
|
+
duotoneSelector = duotoneSupport && scopeSelector(rootSelector, duotoneSupport);
|
|
807
|
+
}
|
|
808
|
+
const hasLayoutSupport = !!blockType?.supports?.layout || !!blockType?.supports?.__experimentalLayout;
|
|
809
|
+
const fallbackGapValue = blockType?.supports?.spacing?.blockGap?.__experimentalDefault;
|
|
810
|
+
const blockStyleVariations = getBlockStyles(name);
|
|
811
|
+
const styleVariationSelectors = {};
|
|
812
|
+
blockStyleVariations?.forEach((variation) => {
|
|
813
|
+
const variationSuffix = variationInstanceId ? `-${variationInstanceId}` : "";
|
|
814
|
+
const variationName = `${variation.name}${variationSuffix}`;
|
|
815
|
+
const styleVariationSelector = getBlockStyleVariationSelector(
|
|
816
|
+
variationName,
|
|
817
|
+
selector
|
|
818
|
+
);
|
|
819
|
+
styleVariationSelectors[variationName] = styleVariationSelector;
|
|
820
|
+
});
|
|
821
|
+
const featureSelectors = getSelectorsConfig(blockType, selector);
|
|
822
|
+
result[name] = {
|
|
823
|
+
duotoneSelector,
|
|
824
|
+
fallbackGapValue,
|
|
825
|
+
featureSelectors: Object.keys(featureSelectors).length ? featureSelectors : void 0,
|
|
826
|
+
hasLayoutSupport,
|
|
827
|
+
name,
|
|
828
|
+
selector,
|
|
829
|
+
styleVariationSelectors: blockStyleVariations?.length ? styleVariationSelectors : void 0
|
|
830
|
+
};
|
|
831
|
+
});
|
|
832
|
+
return result;
|
|
833
|
+
};
|
|
834
|
+
function updateConfigWithSeparator(config) {
|
|
835
|
+
const needsSeparatorStyleUpdate = config.styles?.blocks?.["core/separator"] && config.styles?.blocks?.["core/separator"].color?.background && !config.styles?.blocks?.["core/separator"].color?.text && !config.styles?.blocks?.["core/separator"].border?.color;
|
|
836
|
+
if (needsSeparatorStyleUpdate) {
|
|
837
|
+
return {
|
|
838
|
+
...config,
|
|
839
|
+
styles: {
|
|
840
|
+
...config.styles,
|
|
841
|
+
blocks: {
|
|
842
|
+
...config.styles.blocks,
|
|
843
|
+
"core/separator": {
|
|
844
|
+
...config.styles.blocks["core/separator"],
|
|
845
|
+
color: {
|
|
846
|
+
...config.styles.blocks["core/separator"].color,
|
|
847
|
+
text: config.styles?.blocks["core/separator"].color.background
|
|
848
|
+
}
|
|
849
|
+
}
|
|
850
|
+
}
|
|
851
|
+
}
|
|
852
|
+
};
|
|
853
|
+
}
|
|
854
|
+
return config;
|
|
855
|
+
}
|
|
856
|
+
function processCSSNesting(css, blockSelector) {
|
|
857
|
+
let processedCSS = "";
|
|
858
|
+
if (!css || css.trim() === "") {
|
|
859
|
+
return processedCSS;
|
|
860
|
+
}
|
|
861
|
+
const parts = css.split("&");
|
|
862
|
+
parts.forEach((part) => {
|
|
863
|
+
if (!part || part.trim() === "") {
|
|
864
|
+
return;
|
|
865
|
+
}
|
|
866
|
+
const isRootCss = !part.includes("{");
|
|
867
|
+
if (isRootCss) {
|
|
868
|
+
processedCSS += `:root :where(${blockSelector}){${part.trim()}}`;
|
|
869
|
+
} else {
|
|
870
|
+
const splitPart = part.replace("}", "").split("{");
|
|
871
|
+
if (splitPart.length !== 2) {
|
|
872
|
+
return;
|
|
873
|
+
}
|
|
874
|
+
const [nestedSelector, cssValue] = splitPart;
|
|
875
|
+
const matches = nestedSelector.match(/([>+~\s]*::[a-zA-Z-]+)/);
|
|
876
|
+
const pseudoPart = matches ? matches[1] : "";
|
|
877
|
+
const withoutPseudoElement = matches ? nestedSelector.replace(pseudoPart, "").trim() : nestedSelector.trim();
|
|
878
|
+
let combinedSelector;
|
|
879
|
+
if (withoutPseudoElement === "") {
|
|
880
|
+
combinedSelector = blockSelector;
|
|
881
|
+
} else {
|
|
882
|
+
combinedSelector = nestedSelector.startsWith(" ") ? scopeSelector(blockSelector, withoutPseudoElement) : appendToSelector(blockSelector, withoutPseudoElement);
|
|
883
|
+
}
|
|
884
|
+
processedCSS += `:root :where(${combinedSelector})${pseudoPart}{${cssValue.trim()}}`;
|
|
885
|
+
}
|
|
886
|
+
});
|
|
887
|
+
return processedCSS;
|
|
888
|
+
}
|
|
889
|
+
function useGlobalStylesOutputWithConfig(mergedConfig = {}, disableRootPadding) {
|
|
890
|
+
const [blockGap] = useGlobalSetting("spacing.blockGap");
|
|
891
|
+
const hasBlockGapSupport = blockGap !== null;
|
|
892
|
+
const hasFallbackGapSupport = !hasBlockGapSupport;
|
|
893
|
+
const disableLayoutStyles = useSelect((select) => {
|
|
894
|
+
const { getSettings } = select(blockEditorStore);
|
|
895
|
+
return !!getSettings().disableLayoutStyles;
|
|
896
|
+
});
|
|
897
|
+
const { getBlockStyles } = useSelect(blocksStore);
|
|
898
|
+
return useMemo(() => {
|
|
899
|
+
if (!mergedConfig?.styles || !mergedConfig?.settings) {
|
|
900
|
+
return [];
|
|
901
|
+
}
|
|
902
|
+
const updatedConfig = updateConfigWithSeparator(mergedConfig);
|
|
903
|
+
const blockSelectors = getBlockSelectors(
|
|
904
|
+
getBlockTypes(),
|
|
905
|
+
getBlockStyles
|
|
906
|
+
);
|
|
907
|
+
const customProperties = toCustomProperties(
|
|
908
|
+
updatedConfig,
|
|
909
|
+
blockSelectors
|
|
910
|
+
);
|
|
911
|
+
const globalStyles = toStyles(
|
|
912
|
+
updatedConfig,
|
|
913
|
+
blockSelectors,
|
|
914
|
+
hasBlockGapSupport,
|
|
915
|
+
hasFallbackGapSupport,
|
|
916
|
+
disableLayoutStyles,
|
|
917
|
+
disableRootPadding
|
|
918
|
+
);
|
|
919
|
+
const svgs = toSvgFilters(updatedConfig, blockSelectors);
|
|
920
|
+
const styles = [
|
|
921
|
+
{
|
|
922
|
+
css: customProperties,
|
|
923
|
+
isGlobalStyles: true
|
|
924
|
+
},
|
|
925
|
+
{
|
|
926
|
+
css: globalStyles,
|
|
927
|
+
isGlobalStyles: true
|
|
928
|
+
},
|
|
929
|
+
// Load custom CSS in own stylesheet so that any invalid CSS entered in the input won't break all the global styles in the editor.
|
|
930
|
+
{
|
|
931
|
+
css: updatedConfig.styles.css ?? "",
|
|
932
|
+
isGlobalStyles: true
|
|
933
|
+
},
|
|
934
|
+
{
|
|
935
|
+
assets: svgs,
|
|
936
|
+
__unstableType: "svg",
|
|
937
|
+
isGlobalStyles: true
|
|
938
|
+
}
|
|
939
|
+
];
|
|
940
|
+
getBlockTypes().forEach((blockType) => {
|
|
941
|
+
if (updatedConfig.styles.blocks[blockType.name]?.css) {
|
|
942
|
+
const selector = blockSelectors[blockType.name].selector;
|
|
943
|
+
styles.push({
|
|
944
|
+
css: processCSSNesting(
|
|
945
|
+
updatedConfig.styles.blocks[blockType.name]?.css,
|
|
946
|
+
selector
|
|
947
|
+
),
|
|
948
|
+
isGlobalStyles: true
|
|
949
|
+
});
|
|
950
|
+
}
|
|
951
|
+
});
|
|
952
|
+
return [styles, updatedConfig.settings];
|
|
953
|
+
}, [
|
|
954
|
+
hasBlockGapSupport,
|
|
955
|
+
hasFallbackGapSupport,
|
|
956
|
+
mergedConfig,
|
|
957
|
+
disableLayoutStyles,
|
|
958
|
+
disableRootPadding,
|
|
959
|
+
getBlockStyles
|
|
960
|
+
]);
|
|
961
|
+
}
|
|
962
|
+
function useGlobalStylesOutput(disableRootPadding = false) {
|
|
963
|
+
const { merged: mergedConfig } = useContext(GlobalStylesContext);
|
|
964
|
+
return useGlobalStylesOutputWithConfig(mergedConfig, disableRootPadding);
|
|
965
|
+
}
|
|
966
|
+
export {
|
|
967
|
+
getBlockSelectors,
|
|
968
|
+
getLayoutStyles,
|
|
969
|
+
getNodesWithSettings,
|
|
970
|
+
getNodesWithStyles,
|
|
971
|
+
getStylesDeclarations,
|
|
972
|
+
processCSSNesting,
|
|
973
|
+
toCustomProperties,
|
|
974
|
+
toStyles,
|
|
975
|
+
toSvgFilters,
|
|
976
|
+
useGlobalStylesOutput,
|
|
977
|
+
useGlobalStylesOutputWithConfig
|
|
978
|
+
};
|
|
979
|
+
//# sourceMappingURL=use-global-styles-output.js.map
|