@wordpress/block-editor 15.6.1-next.36001005c.0 → 15.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +2 -0
- 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-toolbar/switch-section-style.js +2 -5
- package/build/components/block-toolbar/switch-section-style.js.map +2 -2
- package/build/components/global-styles/hooks.js +0 -107
- package/build/components/global-styles/hooks.js.map +3 -3
- package/build/components/global-styles/index.js +0 -15
- package/build/components/global-styles/index.js.map +2 -2
- package/build/components/global-styles/typography-panel.js +18 -1
- package/build/components/global-styles/typography-panel.js.map +2 -2
- package/build/components/global-styles/utils.js +0 -19
- package/build/components/global-styles/utils.js.map +3 -3
- package/build/components/inserter/media-tab/media-tab.js +33 -1
- package/build/components/inserter/media-tab/media-tab.js.map +3 -3
- package/build/components/inserter/tips.js +0 -2
- package/build/components/inserter/tips.js.map +2 -2
- package/build/components/media-placeholder/index.js +31 -1
- package/build/components/media-placeholder/index.js.map +3 -3
- package/build/components/media-replace-flow/index.js +29 -1
- package/build/components/media-replace-flow/index.js.map +3 -3
- package/build/components/{global-styles/context.js → media-upload-modal/index.js} +8 -23
- package/build/components/media-upload-modal/index.js.map +7 -0
- package/build/components/rich-text/index.js +8 -7
- package/build/components/rich-text/index.js.map +2 -2
- package/build/hooks/allowed-blocks.js +3 -8
- package/build/hooks/allowed-blocks.js.map +3 -3
- package/build/hooks/block-bindings.js +111 -170
- package/build/hooks/block-bindings.js.map +2 -2
- package/build/hooks/block-style-variation.js +4 -13
- 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/fit-text.js +33 -20
- package/build/hooks/fit-text.js.map +2 -2
- package/build/hooks/font-size.js +4 -3
- 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/private-apis.js +3 -1
- package/build/private-apis.js.map +3 -3
- 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/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-toolbar/switch-section-style.js +2 -5
- package/build-module/components/block-toolbar/switch-section-style.js.map +2 -2
- package/build-module/components/global-styles/hooks.js +1 -99
- package/build-module/components/global-styles/hooks.js.map +2 -2
- package/build-module/components/global-styles/index.js +1 -15
- package/build-module/components/global-styles/index.js.map +2 -2
- package/build-module/components/global-styles/typography-panel.js +18 -1
- package/build-module/components/global-styles/typography-panel.js.map +2 -2
- package/build-module/components/global-styles/utils.js +0 -8
- package/build-module/components/global-styles/utils.js.map +2 -2
- package/build-module/components/inserter/media-tab/media-tab.js +34 -2
- package/build-module/components/inserter/media-tab/media-tab.js.map +2 -2
- package/build-module/components/inserter/tips.js +0 -2
- package/build-module/components/inserter/tips.js.map +2 -2
- package/build-module/components/media-placeholder/index.js +31 -1
- package/build-module/components/media-placeholder/index.js.map +2 -2
- package/build-module/components/media-replace-flow/index.js +29 -1
- package/build-module/components/media-replace-flow/index.js.map +2 -2
- package/build-module/components/media-upload-modal/index.js +7 -0
- package/build-module/components/media-upload-modal/index.js.map +7 -0
- 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/allowed-blocks.js +3 -8
- package/build-module/hooks/allowed-blocks.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 +5 -14
- 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/fit-text.js +34 -21
- package/build-module/hooks/fit-text.js.map +2 -2
- package/build-module/hooks/font-size.js +4 -3
- 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/private-apis.js +3 -1
- package/build-module/private-apis.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/package.json +36 -36
- package/src/components/block-list/block.js +1 -1
- package/src/components/block-list/index.js +2 -2
- package/src/components/block-toolbar/switch-section-style.js +2 -5
- package/src/components/global-styles/hooks.js +1 -121
- package/src/components/global-styles/index.js +1 -9
- package/src/components/global-styles/typography-panel.js +26 -1
- package/src/components/global-styles/utils.js +0 -31
- package/src/components/inserter/media-tab/media-tab.js +44 -2
- package/src/components/inserter/tips.js +0 -2
- package/src/components/media-placeholder/index.js +41 -1
- package/src/components/media-replace-flow/index.js +39 -1
- package/src/components/media-upload-modal/index.js +18 -0
- package/src/components/rich-text/index.js +8 -14
- package/src/hooks/allowed-blocks.js +3 -11
- package/src/hooks/block-bindings.js +79 -153
- package/src/hooks/block-style-variation.js +5 -17
- package/src/hooks/custom-class-name.js +1 -1
- package/src/hooks/fit-text.js +39 -30
- package/src/hooks/font-size.js +7 -3
- package/src/hooks/metadata.js +89 -0
- package/src/hooks/test/metadata.js +316 -0
- package/src/hooks/typography.js +15 -4
- package/src/private-apis.js +2 -0
- 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/utils/fit-text-utils.js +4 -16
- package/tsconfig.json +1 -0
- package/build/components/global-styles/context.js.map +0 -7
- package/build/components/global-styles/get-global-styles-changes.js +0 -216
- package/build/components/global-styles/get-global-styles-changes.js.map +0 -7
- package/build-module/components/global-styles/context.js +0 -17
- package/build-module/components/global-styles/context.js.map +0 -7
- package/build-module/components/global-styles/get-global-styles-changes.js +0 -182
- package/build-module/components/global-styles/get-global-styles-changes.js.map +0 -7
- package/src/components/global-styles/README.md +0 -190
- package/src/components/global-styles/context.js +0 -16
- package/src/components/global-styles/get-global-styles-changes.js +0 -252
- package/src/components/global-styles/test/get-global-styles-changes.js +0 -290
- package/src/components/global-styles/test/utils.js +0 -58
|
@@ -1,182 +0,0 @@
|
|
|
1
|
-
import memoize from "memize";
|
|
2
|
-
import { __, _n, sprintf } from "@wordpress/i18n";
|
|
3
|
-
import { getBlockTypes } from "@wordpress/blocks";
|
|
4
|
-
const globalStylesChangesCache = /* @__PURE__ */ new Map();
|
|
5
|
-
const EMPTY_ARRAY = [];
|
|
6
|
-
const translationMap = {
|
|
7
|
-
caption: __("Caption"),
|
|
8
|
-
link: __("Link"),
|
|
9
|
-
button: __("Button"),
|
|
10
|
-
heading: __("Heading"),
|
|
11
|
-
h1: __("H1"),
|
|
12
|
-
h2: __("H2"),
|
|
13
|
-
h3: __("H3"),
|
|
14
|
-
h4: __("H4"),
|
|
15
|
-
h5: __("H5"),
|
|
16
|
-
h6: __("H6"),
|
|
17
|
-
"settings.color": __("Color"),
|
|
18
|
-
"settings.typography": __("Typography"),
|
|
19
|
-
"settings.shadow": __("Shadow"),
|
|
20
|
-
"settings.layout": __("Layout"),
|
|
21
|
-
"styles.color": __("Colors"),
|
|
22
|
-
"styles.spacing": __("Spacing"),
|
|
23
|
-
"styles.background": __("Background"),
|
|
24
|
-
"styles.typography": __("Typography")
|
|
25
|
-
};
|
|
26
|
-
const getBlockNames = memoize(
|
|
27
|
-
() => getBlockTypes().reduce((accumulator, { name, title }) => {
|
|
28
|
-
accumulator[name] = title;
|
|
29
|
-
return accumulator;
|
|
30
|
-
}, {})
|
|
31
|
-
);
|
|
32
|
-
const isObject = (obj) => obj !== null && typeof obj === "object";
|
|
33
|
-
function getTranslation(key) {
|
|
34
|
-
if (translationMap[key]) {
|
|
35
|
-
return translationMap[key];
|
|
36
|
-
}
|
|
37
|
-
const keyArray = key.split(".");
|
|
38
|
-
if (keyArray?.[0] === "blocks") {
|
|
39
|
-
const blockName = getBlockNames()?.[keyArray[1]];
|
|
40
|
-
return blockName || keyArray[1];
|
|
41
|
-
}
|
|
42
|
-
if (keyArray?.[0] === "elements") {
|
|
43
|
-
return translationMap[keyArray[1]] || keyArray[1];
|
|
44
|
-
}
|
|
45
|
-
return void 0;
|
|
46
|
-
}
|
|
47
|
-
function deepCompare(changedObject, originalObject, parentPath = "") {
|
|
48
|
-
if (!isObject(changedObject) && !isObject(originalObject)) {
|
|
49
|
-
return changedObject !== originalObject ? parentPath.split(".").slice(0, 2).join(".") : void 0;
|
|
50
|
-
}
|
|
51
|
-
changedObject = isObject(changedObject) ? changedObject : {};
|
|
52
|
-
originalObject = isObject(originalObject) ? originalObject : {};
|
|
53
|
-
const allKeys = /* @__PURE__ */ new Set([
|
|
54
|
-
...Object.keys(changedObject),
|
|
55
|
-
...Object.keys(originalObject)
|
|
56
|
-
]);
|
|
57
|
-
let diffs = [];
|
|
58
|
-
for (const key of allKeys) {
|
|
59
|
-
const path = parentPath ? parentPath + "." + key : key;
|
|
60
|
-
const changedPath = deepCompare(
|
|
61
|
-
changedObject[key],
|
|
62
|
-
originalObject[key],
|
|
63
|
-
path
|
|
64
|
-
);
|
|
65
|
-
if (changedPath) {
|
|
66
|
-
diffs = diffs.concat(changedPath);
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
return diffs;
|
|
70
|
-
}
|
|
71
|
-
function getGlobalStylesChangelist(next, previous) {
|
|
72
|
-
const cacheKey = JSON.stringify({ next, previous });
|
|
73
|
-
if (globalStylesChangesCache.has(cacheKey)) {
|
|
74
|
-
return globalStylesChangesCache.get(cacheKey);
|
|
75
|
-
}
|
|
76
|
-
const changedValueTree = deepCompare(
|
|
77
|
-
{
|
|
78
|
-
styles: {
|
|
79
|
-
background: next?.styles?.background,
|
|
80
|
-
color: next?.styles?.color,
|
|
81
|
-
typography: next?.styles?.typography,
|
|
82
|
-
spacing: next?.styles?.spacing
|
|
83
|
-
},
|
|
84
|
-
blocks: next?.styles?.blocks,
|
|
85
|
-
elements: next?.styles?.elements,
|
|
86
|
-
settings: next?.settings
|
|
87
|
-
},
|
|
88
|
-
{
|
|
89
|
-
styles: {
|
|
90
|
-
background: previous?.styles?.background,
|
|
91
|
-
color: previous?.styles?.color,
|
|
92
|
-
typography: previous?.styles?.typography,
|
|
93
|
-
spacing: previous?.styles?.spacing
|
|
94
|
-
},
|
|
95
|
-
blocks: previous?.styles?.blocks,
|
|
96
|
-
elements: previous?.styles?.elements,
|
|
97
|
-
settings: previous?.settings
|
|
98
|
-
}
|
|
99
|
-
);
|
|
100
|
-
if (!changedValueTree.length) {
|
|
101
|
-
globalStylesChangesCache.set(cacheKey, EMPTY_ARRAY);
|
|
102
|
-
return EMPTY_ARRAY;
|
|
103
|
-
}
|
|
104
|
-
const result = [...new Set(changedValueTree)].reduce((acc, curr) => {
|
|
105
|
-
const translation = getTranslation(curr);
|
|
106
|
-
if (translation) {
|
|
107
|
-
acc.push([curr.split(".")[0], translation]);
|
|
108
|
-
}
|
|
109
|
-
return acc;
|
|
110
|
-
}, []);
|
|
111
|
-
globalStylesChangesCache.set(cacheKey, result);
|
|
112
|
-
return result;
|
|
113
|
-
}
|
|
114
|
-
function getGlobalStylesChanges(next, previous, options = {}) {
|
|
115
|
-
let changeList = getGlobalStylesChangelist(next, previous);
|
|
116
|
-
const changesLength = changeList.length;
|
|
117
|
-
const { maxResults } = options;
|
|
118
|
-
if (changesLength) {
|
|
119
|
-
if (!!maxResults && changesLength > maxResults) {
|
|
120
|
-
changeList = changeList.slice(0, maxResults);
|
|
121
|
-
}
|
|
122
|
-
return Object.entries(
|
|
123
|
-
changeList.reduce((acc, curr) => {
|
|
124
|
-
const group = acc[curr[0]] || [];
|
|
125
|
-
if (!group.includes(curr[1])) {
|
|
126
|
-
acc[curr[0]] = [...group, curr[1]];
|
|
127
|
-
}
|
|
128
|
-
return acc;
|
|
129
|
-
}, {})
|
|
130
|
-
).map(([key, changeValues]) => {
|
|
131
|
-
const changeValuesLength = changeValues.length;
|
|
132
|
-
const joinedChangesValue = changeValues.join(
|
|
133
|
-
/* translators: Used between list items, there is a space after the comma. */
|
|
134
|
-
__(", ")
|
|
135
|
-
// eslint-disable-line @wordpress/i18n-no-flanking-whitespace
|
|
136
|
-
);
|
|
137
|
-
switch (key) {
|
|
138
|
-
case "blocks": {
|
|
139
|
-
return sprintf(
|
|
140
|
-
// translators: %s: a list of block names separated by a comma.
|
|
141
|
-
_n("%s block.", "%s blocks.", changeValuesLength),
|
|
142
|
-
joinedChangesValue
|
|
143
|
-
);
|
|
144
|
-
}
|
|
145
|
-
case "elements": {
|
|
146
|
-
return sprintf(
|
|
147
|
-
// translators: %s: a list of element names separated by a comma.
|
|
148
|
-
_n("%s element.", "%s elements.", changeValuesLength),
|
|
149
|
-
joinedChangesValue
|
|
150
|
-
);
|
|
151
|
-
}
|
|
152
|
-
case "settings": {
|
|
153
|
-
return sprintf(
|
|
154
|
-
// translators: %s: a list of theme.json setting labels separated by a comma.
|
|
155
|
-
__("%s settings."),
|
|
156
|
-
joinedChangesValue
|
|
157
|
-
);
|
|
158
|
-
}
|
|
159
|
-
case "styles": {
|
|
160
|
-
return sprintf(
|
|
161
|
-
// translators: %s: a list of theme.json top-level styles labels separated by a comma.
|
|
162
|
-
__("%s styles."),
|
|
163
|
-
joinedChangesValue
|
|
164
|
-
);
|
|
165
|
-
}
|
|
166
|
-
default: {
|
|
167
|
-
return sprintf(
|
|
168
|
-
// translators: %s: a list of global styles changes separated by a comma.
|
|
169
|
-
__("%s."),
|
|
170
|
-
joinedChangesValue
|
|
171
|
-
);
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
});
|
|
175
|
-
}
|
|
176
|
-
return EMPTY_ARRAY;
|
|
177
|
-
}
|
|
178
|
-
export {
|
|
179
|
-
getGlobalStylesChanges as default,
|
|
180
|
-
getGlobalStylesChangelist
|
|
181
|
-
};
|
|
182
|
-
//# sourceMappingURL=get-global-styles-changes.js.map
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"version": 3,
|
|
3
|
-
"sources": ["../../../src/components/global-styles/get-global-styles-changes.js"],
|
|
4
|
-
"sourcesContent": ["/**\n * External dependencies\n */\nimport memoize from 'memize';\n\n/**\n * WordPress dependencies\n */\nimport { __, _n, sprintf } from '@wordpress/i18n';\nimport { getBlockTypes } from '@wordpress/blocks';\n\nconst globalStylesChangesCache = new Map();\nconst EMPTY_ARRAY = [];\nconst translationMap = {\n\tcaption: __( 'Caption' ),\n\tlink: __( 'Link' ),\n\tbutton: __( 'Button' ),\n\theading: __( 'Heading' ),\n\th1: __( 'H1' ),\n\th2: __( 'H2' ),\n\th3: __( 'H3' ),\n\th4: __( 'H4' ),\n\th5: __( 'H5' ),\n\th6: __( 'H6' ),\n\t'settings.color': __( 'Color' ),\n\t'settings.typography': __( 'Typography' ),\n\t'settings.shadow': __( 'Shadow' ),\n\t'settings.layout': __( 'Layout' ),\n\t'styles.color': __( 'Colors' ),\n\t'styles.spacing': __( 'Spacing' ),\n\t'styles.background': __( 'Background' ),\n\t'styles.typography': __( 'Typography' ),\n};\nconst getBlockNames = memoize( () =>\n\tgetBlockTypes().reduce( ( accumulator, { name, title } ) => {\n\t\taccumulator[ name ] = title;\n\t\treturn accumulator;\n\t}, {} )\n);\nconst isObject = ( obj ) => obj !== null && typeof obj === 'object';\n\n/**\n * Get the translation for a given global styles key.\n * @param {string} key A key representing a path to a global style property or setting.\n * @return {string|undefined} A translated key or undefined if no translation exists.\n */\nfunction getTranslation( key ) {\n\tif ( translationMap[ key ] ) {\n\t\treturn translationMap[ key ];\n\t}\n\n\tconst keyArray = key.split( '.' );\n\n\tif ( keyArray?.[ 0 ] === 'blocks' ) {\n\t\tconst blockName = getBlockNames()?.[ keyArray[ 1 ] ];\n\t\treturn blockName || keyArray[ 1 ];\n\t}\n\n\tif ( keyArray?.[ 0 ] === 'elements' ) {\n\t\treturn translationMap[ keyArray[ 1 ] ] || keyArray[ 1 ];\n\t}\n\n\treturn undefined;\n}\n\n/**\n * A deep comparison of two objects, optimized for comparing global styles.\n * @param {Object} changedObject The changed object to compare.\n * @param {Object} originalObject The original object to compare against.\n * @param {string} parentPath A key/value pair object of block names and their rendered titles.\n * @return {string[]} An array of paths whose values have changed.\n */\nfunction deepCompare( changedObject, originalObject, parentPath = '' ) {\n\t// We have two non-object values to compare.\n\tif ( ! isObject( changedObject ) && ! isObject( originalObject ) ) {\n\t\t/*\n\t\t * Only return a path if the value has changed.\n\t\t * And then only the path name up to 2 levels deep.\n\t\t */\n\t\treturn changedObject !== originalObject\n\t\t\t? parentPath.split( '.' ).slice( 0, 2 ).join( '.' )\n\t\t\t: undefined;\n\t}\n\n\t// Enable comparison when an object doesn't have a corresponding property to compare.\n\tchangedObject = isObject( changedObject ) ? changedObject : {};\n\toriginalObject = isObject( originalObject ) ? originalObject : {};\n\n\tconst allKeys = new Set( [\n\t\t...Object.keys( changedObject ),\n\t\t...Object.keys( originalObject ),\n\t] );\n\n\tlet diffs = [];\n\tfor ( const key of allKeys ) {\n\t\tconst path = parentPath ? parentPath + '.' + key : key;\n\t\tconst changedPath = deepCompare(\n\t\t\tchangedObject[ key ],\n\t\t\toriginalObject[ key ],\n\t\t\tpath\n\t\t);\n\t\tif ( changedPath ) {\n\t\t\tdiffs = diffs.concat( changedPath );\n\t\t}\n\t}\n\treturn diffs;\n}\n\n/**\n * Returns an array of translated summarized global styles changes.\n * Results are cached using a Map() key of `JSON.stringify( { next, previous } )`.\n *\n * @param {Object} next The changed object to compare.\n * @param {Object} previous The original object to compare against.\n * @return {Array[]} A 2-dimensional array of tuples: [ \"group\", \"translated change\" ].\n */\nexport function getGlobalStylesChangelist( next, previous ) {\n\tconst cacheKey = JSON.stringify( { next, previous } );\n\n\tif ( globalStylesChangesCache.has( cacheKey ) ) {\n\t\treturn globalStylesChangesCache.get( cacheKey );\n\t}\n\n\t/*\n\t * Compare the two changesets with normalized keys.\n\t * The order of these keys determines the order in which\n\t * they'll appear in the results.\n\t */\n\tconst changedValueTree = deepCompare(\n\t\t{\n\t\t\tstyles: {\n\t\t\t\tbackground: next?.styles?.background,\n\t\t\t\tcolor: next?.styles?.color,\n\t\t\t\ttypography: next?.styles?.typography,\n\t\t\t\tspacing: next?.styles?.spacing,\n\t\t\t},\n\t\t\tblocks: next?.styles?.blocks,\n\t\t\telements: next?.styles?.elements,\n\t\t\tsettings: next?.settings,\n\t\t},\n\t\t{\n\t\t\tstyles: {\n\t\t\t\tbackground: previous?.styles?.background,\n\t\t\t\tcolor: previous?.styles?.color,\n\t\t\t\ttypography: previous?.styles?.typography,\n\t\t\t\tspacing: previous?.styles?.spacing,\n\t\t\t},\n\t\t\tblocks: previous?.styles?.blocks,\n\t\t\telements: previous?.styles?.elements,\n\t\t\tsettings: previous?.settings,\n\t\t}\n\t);\n\n\tif ( ! changedValueTree.length ) {\n\t\tglobalStylesChangesCache.set( cacheKey, EMPTY_ARRAY );\n\t\treturn EMPTY_ARRAY;\n\t}\n\n\t// Remove duplicate results.\n\tconst result = [ ...new Set( changedValueTree ) ]\n\t\t/*\n\t\t * Translate the keys.\n\t\t * Remove empty translations.\n\t\t */\n\t\t.reduce( ( acc, curr ) => {\n\t\t\tconst translation = getTranslation( curr );\n\t\t\tif ( translation ) {\n\t\t\t\tacc.push( [ curr.split( '.' )[ 0 ], translation ] );\n\t\t\t}\n\t\t\treturn acc;\n\t\t}, [] );\n\n\tglobalStylesChangesCache.set( cacheKey, result );\n\n\treturn result;\n}\n\n/**\n * From a getGlobalStylesChangelist() result, returns an array of translated global styles changes, grouped by type.\n * The types are 'blocks', 'elements', 'settings', and 'styles'.\n *\n * @param {Object} next The changed object to compare.\n * @param {Object} previous The original object to compare against.\n * @param {{maxResults:number}} options Options. maxResults: results to return before truncating.\n * @return {string[]} An array of translated changes.\n */\nexport default function getGlobalStylesChanges( next, previous, options = {} ) {\n\tlet changeList = getGlobalStylesChangelist( next, previous );\n\tconst changesLength = changeList.length;\n\tconst { maxResults } = options;\n\n\tif ( changesLength ) {\n\t\t// Truncate to `n` results if necessary.\n\t\tif ( !! maxResults && changesLength > maxResults ) {\n\t\t\tchangeList = changeList.slice( 0, maxResults );\n\t\t}\n\t\treturn Object.entries(\n\t\t\tchangeList.reduce( ( acc, curr ) => {\n\t\t\t\tconst group = acc[ curr[ 0 ] ] || [];\n\t\t\t\tif ( ! group.includes( curr[ 1 ] ) ) {\n\t\t\t\t\tacc[ curr[ 0 ] ] = [ ...group, curr[ 1 ] ];\n\t\t\t\t}\n\t\t\t\treturn acc;\n\t\t\t}, {} )\n\t\t).map( ( [ key, changeValues ] ) => {\n\t\t\tconst changeValuesLength = changeValues.length;\n\t\t\tconst joinedChangesValue = changeValues.join(\n\t\t\t\t/* translators: Used between list items, there is a space after the comma. */\n\t\t\t\t__( ', ' ) // eslint-disable-line @wordpress/i18n-no-flanking-whitespace\n\t\t\t);\n\t\t\tswitch ( key ) {\n\t\t\t\tcase 'blocks': {\n\t\t\t\t\treturn sprintf(\n\t\t\t\t\t\t// translators: %s: a list of block names separated by a comma.\n\t\t\t\t\t\t_n( '%s block.', '%s blocks.', changeValuesLength ),\n\t\t\t\t\t\tjoinedChangesValue\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t\tcase 'elements': {\n\t\t\t\t\treturn sprintf(\n\t\t\t\t\t\t// translators: %s: a list of element names separated by a comma.\n\t\t\t\t\t\t_n( '%s element.', '%s elements.', changeValuesLength ),\n\t\t\t\t\t\tjoinedChangesValue\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t\tcase 'settings': {\n\t\t\t\t\treturn sprintf(\n\t\t\t\t\t\t// translators: %s: a list of theme.json setting labels separated by a comma.\n\t\t\t\t\t\t__( '%s settings.' ),\n\t\t\t\t\t\tjoinedChangesValue\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t\tcase 'styles': {\n\t\t\t\t\treturn sprintf(\n\t\t\t\t\t\t// translators: %s: a list of theme.json top-level styles labels separated by a comma.\n\t\t\t\t\t\t__( '%s styles.' ),\n\t\t\t\t\t\tjoinedChangesValue\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t\tdefault: {\n\t\t\t\t\treturn sprintf(\n\t\t\t\t\t\t// translators: %s: a list of global styles changes separated by a comma.\n\t\t\t\t\t\t__( '%s.' ),\n\t\t\t\t\t\tjoinedChangesValue\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t}\n\t\t} );\n\t}\n\n\treturn EMPTY_ARRAY;\n}\n"],
|
|
5
|
-
"mappings": "AAGA,OAAO,aAAa;AAKpB,SAAS,IAAI,IAAI,eAAe;AAChC,SAAS,qBAAqB;AAE9B,MAAM,2BAA2B,oBAAI,IAAI;AACzC,MAAM,cAAc,CAAC;AACrB,MAAM,iBAAiB;AAAA,EACtB,SAAS,GAAI,SAAU;AAAA,EACvB,MAAM,GAAI,MAAO;AAAA,EACjB,QAAQ,GAAI,QAAS;AAAA,EACrB,SAAS,GAAI,SAAU;AAAA,EACvB,IAAI,GAAI,IAAK;AAAA,EACb,IAAI,GAAI,IAAK;AAAA,EACb,IAAI,GAAI,IAAK;AAAA,EACb,IAAI,GAAI,IAAK;AAAA,EACb,IAAI,GAAI,IAAK;AAAA,EACb,IAAI,GAAI,IAAK;AAAA,EACb,kBAAkB,GAAI,OAAQ;A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GAAI,cAAe;AAAA,YACnB;AAAA,UACD;AAAA,QACD;AAAA,QACA,KAAK,UAAU;AACd,iBAAO;AAAA;AAAA,YAEN,GAAI,YAAa;AAAA,YACjB;AAAA,UACD;AAAA,QACD;AAAA,QACA,SAAS;AACR,iBAAO;AAAA;AAAA,YAEN,GAAI,KAAM;AAAA,YACV;AAAA,UACD;AAAA,QACD;AAAA,MACD;AAAA,IACD,CAAE;AAAA,EACH;AAEA,SAAO;AACR;",
|
|
6
|
-
"names": []
|
|
7
|
-
}
|
|
@@ -1,190 +0,0 @@
|
|
|
1
|
-
# Global Styles
|
|
2
|
-
|
|
3
|
-
This folder contains all the necessary APIs to manipulate the global styles data. It can be potentially extracted to its own package.
|
|
4
|
-
|
|
5
|
-
# Available public APIs
|
|
6
|
-
|
|
7
|
-
## useGlobalStylesReset
|
|
8
|
-
|
|
9
|
-
A React hook used to retrieve whether the Global Styles have been edited and a callback to reset to the default theme values.
|
|
10
|
-
|
|
11
|
-
```js
|
|
12
|
-
import { useGlobalStylesReset } from '@wordpress/block-editor';
|
|
13
|
-
|
|
14
|
-
function MyComponent() {
|
|
15
|
-
const [ canReset, reset ] = useGlobalStylesReset();
|
|
16
|
-
|
|
17
|
-
return canReset ? <Button onClick={ () => reset() }>Reset</Button> : null;
|
|
18
|
-
}
|
|
19
|
-
```
|
|
20
|
-
|
|
21
|
-
## useGlobalStylesOutput
|
|
22
|
-
|
|
23
|
-
A React hook used to retrieve the styles array and settings to provide for block editor instances based on the current global styles.
|
|
24
|
-
|
|
25
|
-
```js
|
|
26
|
-
import {
|
|
27
|
-
useGlobalStylesOutput,
|
|
28
|
-
BlockEditorProvider,
|
|
29
|
-
BlockList,
|
|
30
|
-
} from '@wordpress/block-editor';
|
|
31
|
-
|
|
32
|
-
function MyComponent() {
|
|
33
|
-
const [ styles, settings ] = useGlobalStylesOutput();
|
|
34
|
-
|
|
35
|
-
return (
|
|
36
|
-
<BlockEditorProvider
|
|
37
|
-
settings={ {
|
|
38
|
-
styles,
|
|
39
|
-
__experimentalFeatures: settings,
|
|
40
|
-
} }
|
|
41
|
-
>
|
|
42
|
-
<BlockList />
|
|
43
|
-
</BlockEditorProvider>
|
|
44
|
-
);
|
|
45
|
-
}
|
|
46
|
-
```
|
|
47
|
-
|
|
48
|
-
## useGlobalStyle
|
|
49
|
-
|
|
50
|
-
A react hook used to retrieve the style applied to a given context.
|
|
51
|
-
|
|
52
|
-
```js
|
|
53
|
-
import { useGlobalStyle } from '@wordpress/block-editor';
|
|
54
|
-
|
|
55
|
-
function MyComponent() {
|
|
56
|
-
// Text color for the site root.
|
|
57
|
-
const [ color, setColor ] = useGlobalStyle( 'color.text' );
|
|
58
|
-
|
|
59
|
-
// The user modified color for the core paragraph block.
|
|
60
|
-
const [ pColor, setPColor ] = useGlobalStyle(
|
|
61
|
-
'color.text',
|
|
62
|
-
'core/paragraph',
|
|
63
|
-
'user'
|
|
64
|
-
);
|
|
65
|
-
|
|
66
|
-
return 'Something';
|
|
67
|
-
}
|
|
68
|
-
```
|
|
69
|
-
|
|
70
|
-
## useGlobalSetting
|
|
71
|
-
|
|
72
|
-
A react hook used to retrieve the setting applied to a given context.
|
|
73
|
-
|
|
74
|
-
```js
|
|
75
|
-
import { useGlobalSetting } from '@wordpress/block-editor';
|
|
76
|
-
|
|
77
|
-
function MyComponent() {
|
|
78
|
-
// The theme color palette.
|
|
79
|
-
const [ colorPalette, setColorPalette ] = useGlobalSetting(
|
|
80
|
-
'color.palette.theme'
|
|
81
|
-
);
|
|
82
|
-
|
|
83
|
-
// The theme color palette for the paragraph block, ignoring user changes.
|
|
84
|
-
// If the palette is not defined for the paragraph block, the root one is returned.
|
|
85
|
-
const [ pColor, setPColor ] = useGlobalSetting(
|
|
86
|
-
'color.palette.theme',
|
|
87
|
-
'core/paragraph',
|
|
88
|
-
'base'
|
|
89
|
-
);
|
|
90
|
-
|
|
91
|
-
return 'Something';
|
|
92
|
-
}
|
|
93
|
-
```
|
|
94
|
-
|
|
95
|
-
## UI Components
|
|
96
|
-
|
|
97
|
-
The global styles folder also offers a set of reusable UI components. These components follow a strict set of guidelines:
|
|
98
|
-
|
|
99
|
-
- They are independent of any context or any store dependency. They only rely on the props passed to them.
|
|
100
|
-
- They receive a similar set of props:
|
|
101
|
-
|
|
102
|
-
- `value`: The value is a style object that maps closely the format used in `theme.json` and is also very close to the format of the `style` attributes for blocks. There are some differences with the block attributes due to the iteration process and the fact that we need to maintain compatibility with the existing blocks even if they predate the introduction of Global Styles and these UI components. An example value for a style object is:
|
|
103
|
-
|
|
104
|
-
```js
|
|
105
|
-
{
|
|
106
|
-
color: {
|
|
107
|
-
text: 'var:preset|color|blue',
|
|
108
|
-
background: '#FF0000',
|
|
109
|
-
},
|
|
110
|
-
typography: {
|
|
111
|
-
fontFamily: 'var:preset|font-family|base',
|
|
112
|
-
fontSize: '10px',
|
|
113
|
-
lineHeight: 1.5,
|
|
114
|
-
},
|
|
115
|
-
spacing: {
|
|
116
|
-
padding: 'var:preset|spacing|medium',
|
|
117
|
-
},
|
|
118
|
-
elements: {
|
|
119
|
-
link: {
|
|
120
|
-
color: {
|
|
121
|
-
text: 'var:preset|color|green',
|
|
122
|
-
},
|
|
123
|
-
},
|
|
124
|
-
},
|
|
125
|
-
}
|
|
126
|
-
```
|
|
127
|
-
|
|
128
|
-
- `onChange`: A callback that receives the new value and is called when the user changes the value of the UI component.
|
|
129
|
-
- `inheritedValue`: The inherited value is a style object that represents the combined value of the style inherited from the parent context in addition to the style applied to the current context. The format is the same as the `value` prop.
|
|
130
|
-
- `settings`: The settings are the theme.json settings. They are used to provide the UI components with the necessary information to render the UI. An example value for the settings is:
|
|
131
|
-
|
|
132
|
-
```js
|
|
133
|
-
{
|
|
134
|
-
color: {
|
|
135
|
-
palette: {
|
|
136
|
-
custom: [
|
|
137
|
-
{
|
|
138
|
-
slug: 'black',
|
|
139
|
-
color: '#000000',
|
|
140
|
-
},
|
|
141
|
-
{
|
|
142
|
-
slug: 'white',
|
|
143
|
-
color: '#FFFFFF',
|
|
144
|
-
},
|
|
145
|
-
{
|
|
146
|
-
slug: 'blue',
|
|
147
|
-
color: '#0000FF',
|
|
148
|
-
},
|
|
149
|
-
]
|
|
150
|
-
},
|
|
151
|
-
gradients: {
|
|
152
|
-
custom: [
|
|
153
|
-
{
|
|
154
|
-
slug: 'gradient-1',
|
|
155
|
-
gradient: 'linear-gradient(135deg,rgba(6,147,227,1) 0%,rgb(155,81,224) 100%)',
|
|
156
|
-
},
|
|
157
|
-
{
|
|
158
|
-
slug: 'gradient-2',
|
|
159
|
-
gradient: 'linear-gradient(135deg,rgba(6,147,227,1) 0%,rgb(155,81,224) 100%)',
|
|
160
|
-
},
|
|
161
|
-
]
|
|
162
|
-
},
|
|
163
|
-
},
|
|
164
|
-
typography: {
|
|
165
|
-
fontSizes: [
|
|
166
|
-
{
|
|
167
|
-
slug: 'small',
|
|
168
|
-
size: '12px',
|
|
169
|
-
},
|
|
170
|
-
{
|
|
171
|
-
slug: 'medium',
|
|
172
|
-
size: '16px',
|
|
173
|
-
},
|
|
174
|
-
{
|
|
175
|
-
slug: 'large',
|
|
176
|
-
size: '24px',
|
|
177
|
-
},
|
|
178
|
-
],
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
```
|
|
182
|
-
- `defaultControls`: The default controls are the controls that are used by default to render the UI. They are used to provide the UI components with the necessary information to render the UI. An example value for the default controls for the `ColorPanel` component is:
|
|
183
|
-
|
|
184
|
-
```js
|
|
185
|
-
{
|
|
186
|
-
background: true,
|
|
187
|
-
text: true,
|
|
188
|
-
link: true,
|
|
189
|
-
},
|
|
190
|
-
```
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* WordPress dependencies
|
|
3
|
-
*/
|
|
4
|
-
import { createContext } from '@wordpress/element';
|
|
5
|
-
|
|
6
|
-
export const DEFAULT_GLOBAL_STYLES_CONTEXT = {
|
|
7
|
-
user: {},
|
|
8
|
-
base: {},
|
|
9
|
-
merged: {},
|
|
10
|
-
setUserConfig: () => {},
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
export const GlobalStylesContext = createContext(
|
|
14
|
-
DEFAULT_GLOBAL_STYLES_CONTEXT
|
|
15
|
-
);
|
|
16
|
-
GlobalStylesContext.displayName = 'GlobalStylesContext';
|
|
@@ -1,252 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* External dependencies
|
|
3
|
-
*/
|
|
4
|
-
import memoize from 'memize';
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* WordPress dependencies
|
|
8
|
-
*/
|
|
9
|
-
import { __, _n, sprintf } from '@wordpress/i18n';
|
|
10
|
-
import { getBlockTypes } from '@wordpress/blocks';
|
|
11
|
-
|
|
12
|
-
const globalStylesChangesCache = new Map();
|
|
13
|
-
const EMPTY_ARRAY = [];
|
|
14
|
-
const translationMap = {
|
|
15
|
-
caption: __( 'Caption' ),
|
|
16
|
-
link: __( 'Link' ),
|
|
17
|
-
button: __( 'Button' ),
|
|
18
|
-
heading: __( 'Heading' ),
|
|
19
|
-
h1: __( 'H1' ),
|
|
20
|
-
h2: __( 'H2' ),
|
|
21
|
-
h3: __( 'H3' ),
|
|
22
|
-
h4: __( 'H4' ),
|
|
23
|
-
h5: __( 'H5' ),
|
|
24
|
-
h6: __( 'H6' ),
|
|
25
|
-
'settings.color': __( 'Color' ),
|
|
26
|
-
'settings.typography': __( 'Typography' ),
|
|
27
|
-
'settings.shadow': __( 'Shadow' ),
|
|
28
|
-
'settings.layout': __( 'Layout' ),
|
|
29
|
-
'styles.color': __( 'Colors' ),
|
|
30
|
-
'styles.spacing': __( 'Spacing' ),
|
|
31
|
-
'styles.background': __( 'Background' ),
|
|
32
|
-
'styles.typography': __( 'Typography' ),
|
|
33
|
-
};
|
|
34
|
-
const getBlockNames = memoize( () =>
|
|
35
|
-
getBlockTypes().reduce( ( accumulator, { name, title } ) => {
|
|
36
|
-
accumulator[ name ] = title;
|
|
37
|
-
return accumulator;
|
|
38
|
-
}, {} )
|
|
39
|
-
);
|
|
40
|
-
const isObject = ( obj ) => obj !== null && typeof obj === 'object';
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* Get the translation for a given global styles key.
|
|
44
|
-
* @param {string} key A key representing a path to a global style property or setting.
|
|
45
|
-
* @return {string|undefined} A translated key or undefined if no translation exists.
|
|
46
|
-
*/
|
|
47
|
-
function getTranslation( key ) {
|
|
48
|
-
if ( translationMap[ key ] ) {
|
|
49
|
-
return translationMap[ key ];
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
const keyArray = key.split( '.' );
|
|
53
|
-
|
|
54
|
-
if ( keyArray?.[ 0 ] === 'blocks' ) {
|
|
55
|
-
const blockName = getBlockNames()?.[ keyArray[ 1 ] ];
|
|
56
|
-
return blockName || keyArray[ 1 ];
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
if ( keyArray?.[ 0 ] === 'elements' ) {
|
|
60
|
-
return translationMap[ keyArray[ 1 ] ] || keyArray[ 1 ];
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
return undefined;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
/**
|
|
67
|
-
* A deep comparison of two objects, optimized for comparing global styles.
|
|
68
|
-
* @param {Object} changedObject The changed object to compare.
|
|
69
|
-
* @param {Object} originalObject The original object to compare against.
|
|
70
|
-
* @param {string} parentPath A key/value pair object of block names and their rendered titles.
|
|
71
|
-
* @return {string[]} An array of paths whose values have changed.
|
|
72
|
-
*/
|
|
73
|
-
function deepCompare( changedObject, originalObject, parentPath = '' ) {
|
|
74
|
-
// We have two non-object values to compare.
|
|
75
|
-
if ( ! isObject( changedObject ) && ! isObject( originalObject ) ) {
|
|
76
|
-
/*
|
|
77
|
-
* Only return a path if the value has changed.
|
|
78
|
-
* And then only the path name up to 2 levels deep.
|
|
79
|
-
*/
|
|
80
|
-
return changedObject !== originalObject
|
|
81
|
-
? parentPath.split( '.' ).slice( 0, 2 ).join( '.' )
|
|
82
|
-
: undefined;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
// Enable comparison when an object doesn't have a corresponding property to compare.
|
|
86
|
-
changedObject = isObject( changedObject ) ? changedObject : {};
|
|
87
|
-
originalObject = isObject( originalObject ) ? originalObject : {};
|
|
88
|
-
|
|
89
|
-
const allKeys = new Set( [
|
|
90
|
-
...Object.keys( changedObject ),
|
|
91
|
-
...Object.keys( originalObject ),
|
|
92
|
-
] );
|
|
93
|
-
|
|
94
|
-
let diffs = [];
|
|
95
|
-
for ( const key of allKeys ) {
|
|
96
|
-
const path = parentPath ? parentPath + '.' + key : key;
|
|
97
|
-
const changedPath = deepCompare(
|
|
98
|
-
changedObject[ key ],
|
|
99
|
-
originalObject[ key ],
|
|
100
|
-
path
|
|
101
|
-
);
|
|
102
|
-
if ( changedPath ) {
|
|
103
|
-
diffs = diffs.concat( changedPath );
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
return diffs;
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
/**
|
|
110
|
-
* Returns an array of translated summarized global styles changes.
|
|
111
|
-
* Results are cached using a Map() key of `JSON.stringify( { next, previous } )`.
|
|
112
|
-
*
|
|
113
|
-
* @param {Object} next The changed object to compare.
|
|
114
|
-
* @param {Object} previous The original object to compare against.
|
|
115
|
-
* @return {Array[]} A 2-dimensional array of tuples: [ "group", "translated change" ].
|
|
116
|
-
*/
|
|
117
|
-
export function getGlobalStylesChangelist( next, previous ) {
|
|
118
|
-
const cacheKey = JSON.stringify( { next, previous } );
|
|
119
|
-
|
|
120
|
-
if ( globalStylesChangesCache.has( cacheKey ) ) {
|
|
121
|
-
return globalStylesChangesCache.get( cacheKey );
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
/*
|
|
125
|
-
* Compare the two changesets with normalized keys.
|
|
126
|
-
* The order of these keys determines the order in which
|
|
127
|
-
* they'll appear in the results.
|
|
128
|
-
*/
|
|
129
|
-
const changedValueTree = deepCompare(
|
|
130
|
-
{
|
|
131
|
-
styles: {
|
|
132
|
-
background: next?.styles?.background,
|
|
133
|
-
color: next?.styles?.color,
|
|
134
|
-
typography: next?.styles?.typography,
|
|
135
|
-
spacing: next?.styles?.spacing,
|
|
136
|
-
},
|
|
137
|
-
blocks: next?.styles?.blocks,
|
|
138
|
-
elements: next?.styles?.elements,
|
|
139
|
-
settings: next?.settings,
|
|
140
|
-
},
|
|
141
|
-
{
|
|
142
|
-
styles: {
|
|
143
|
-
background: previous?.styles?.background,
|
|
144
|
-
color: previous?.styles?.color,
|
|
145
|
-
typography: previous?.styles?.typography,
|
|
146
|
-
spacing: previous?.styles?.spacing,
|
|
147
|
-
},
|
|
148
|
-
blocks: previous?.styles?.blocks,
|
|
149
|
-
elements: previous?.styles?.elements,
|
|
150
|
-
settings: previous?.settings,
|
|
151
|
-
}
|
|
152
|
-
);
|
|
153
|
-
|
|
154
|
-
if ( ! changedValueTree.length ) {
|
|
155
|
-
globalStylesChangesCache.set( cacheKey, EMPTY_ARRAY );
|
|
156
|
-
return EMPTY_ARRAY;
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
// Remove duplicate results.
|
|
160
|
-
const result = [ ...new Set( changedValueTree ) ]
|
|
161
|
-
/*
|
|
162
|
-
* Translate the keys.
|
|
163
|
-
* Remove empty translations.
|
|
164
|
-
*/
|
|
165
|
-
.reduce( ( acc, curr ) => {
|
|
166
|
-
const translation = getTranslation( curr );
|
|
167
|
-
if ( translation ) {
|
|
168
|
-
acc.push( [ curr.split( '.' )[ 0 ], translation ] );
|
|
169
|
-
}
|
|
170
|
-
return acc;
|
|
171
|
-
}, [] );
|
|
172
|
-
|
|
173
|
-
globalStylesChangesCache.set( cacheKey, result );
|
|
174
|
-
|
|
175
|
-
return result;
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
/**
|
|
179
|
-
* From a getGlobalStylesChangelist() result, returns an array of translated global styles changes, grouped by type.
|
|
180
|
-
* The types are 'blocks', 'elements', 'settings', and 'styles'.
|
|
181
|
-
*
|
|
182
|
-
* @param {Object} next The changed object to compare.
|
|
183
|
-
* @param {Object} previous The original object to compare against.
|
|
184
|
-
* @param {{maxResults:number}} options Options. maxResults: results to return before truncating.
|
|
185
|
-
* @return {string[]} An array of translated changes.
|
|
186
|
-
*/
|
|
187
|
-
export default function getGlobalStylesChanges( next, previous, options = {} ) {
|
|
188
|
-
let changeList = getGlobalStylesChangelist( next, previous );
|
|
189
|
-
const changesLength = changeList.length;
|
|
190
|
-
const { maxResults } = options;
|
|
191
|
-
|
|
192
|
-
if ( changesLength ) {
|
|
193
|
-
// Truncate to `n` results if necessary.
|
|
194
|
-
if ( !! maxResults && changesLength > maxResults ) {
|
|
195
|
-
changeList = changeList.slice( 0, maxResults );
|
|
196
|
-
}
|
|
197
|
-
return Object.entries(
|
|
198
|
-
changeList.reduce( ( acc, curr ) => {
|
|
199
|
-
const group = acc[ curr[ 0 ] ] || [];
|
|
200
|
-
if ( ! group.includes( curr[ 1 ] ) ) {
|
|
201
|
-
acc[ curr[ 0 ] ] = [ ...group, curr[ 1 ] ];
|
|
202
|
-
}
|
|
203
|
-
return acc;
|
|
204
|
-
}, {} )
|
|
205
|
-
).map( ( [ key, changeValues ] ) => {
|
|
206
|
-
const changeValuesLength = changeValues.length;
|
|
207
|
-
const joinedChangesValue = changeValues.join(
|
|
208
|
-
/* translators: Used between list items, there is a space after the comma. */
|
|
209
|
-
__( ', ' ) // eslint-disable-line @wordpress/i18n-no-flanking-whitespace
|
|
210
|
-
);
|
|
211
|
-
switch ( key ) {
|
|
212
|
-
case 'blocks': {
|
|
213
|
-
return sprintf(
|
|
214
|
-
// translators: %s: a list of block names separated by a comma.
|
|
215
|
-
_n( '%s block.', '%s blocks.', changeValuesLength ),
|
|
216
|
-
joinedChangesValue
|
|
217
|
-
);
|
|
218
|
-
}
|
|
219
|
-
case 'elements': {
|
|
220
|
-
return sprintf(
|
|
221
|
-
// translators: %s: a list of element names separated by a comma.
|
|
222
|
-
_n( '%s element.', '%s elements.', changeValuesLength ),
|
|
223
|
-
joinedChangesValue
|
|
224
|
-
);
|
|
225
|
-
}
|
|
226
|
-
case 'settings': {
|
|
227
|
-
return sprintf(
|
|
228
|
-
// translators: %s: a list of theme.json setting labels separated by a comma.
|
|
229
|
-
__( '%s settings.' ),
|
|
230
|
-
joinedChangesValue
|
|
231
|
-
);
|
|
232
|
-
}
|
|
233
|
-
case 'styles': {
|
|
234
|
-
return sprintf(
|
|
235
|
-
// translators: %s: a list of theme.json top-level styles labels separated by a comma.
|
|
236
|
-
__( '%s styles.' ),
|
|
237
|
-
joinedChangesValue
|
|
238
|
-
);
|
|
239
|
-
}
|
|
240
|
-
default: {
|
|
241
|
-
return sprintf(
|
|
242
|
-
// translators: %s: a list of global styles changes separated by a comma.
|
|
243
|
-
__( '%s.' ),
|
|
244
|
-
joinedChangesValue
|
|
245
|
-
);
|
|
246
|
-
}
|
|
247
|
-
}
|
|
248
|
-
} );
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
return EMPTY_ARRAY;
|
|
252
|
-
}
|