-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
index.js
208 lines (182 loc) · 5.32 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/**
* External dependencies
*/
import deepmerge from 'deepmerge';
import { isPlainObject } from 'is-plain-object';
/**
* WordPress dependencies
*/
import { privateApis as blockEditorPrivateApis } from '@wordpress/block-editor';
import { store as coreStore } from '@wordpress/core-data';
import { useSelect, useDispatch } from '@wordpress/data';
import { useMemo, useCallback } from '@wordpress/element';
/**
* Internal dependencies
*/
import { unlock } from '../../lock-unlock';
const { GlobalStylesContext, cleanEmptyObject } = unlock(
blockEditorPrivateApis
);
export function mergeBaseAndUserConfigs( base, user ) {
return deepmerge( base, user, {
// We only pass as arrays the presets,
// in which case we want the new array of values
// to override the old array (no merging).
isMergeableObject: isPlainObject,
} );
}
function useGlobalStylesUserConfig() {
const { globalStylesId, isReady, settings, styles, _links } = useSelect(
( select ) => {
const {
getEditedEntityRecord,
hasFinishedResolution,
getUser,
getCurrentUser,
} = select( coreStore );
const _globalStylesId =
select( coreStore ).__experimentalGetCurrentGlobalStylesId();
// Doing canUser( 'read', 'global_styles' ) returns false even for users with the capability.
// See: https://github.com/WordPress/gutenberg/issues/63438
// So we need to check the user capabilities directly.
const userId = getCurrentUser()?.id;
const canEditThemeOptions =
userId && getUser( userId )?.capabilities?.edit_theme_options;
const record =
_globalStylesId && canEditThemeOptions
? getEditedEntityRecord(
'root',
'globalStyles',
_globalStylesId
)
: undefined;
let hasResolved = false;
if (
hasFinishedResolution(
'__experimentalGetCurrentGlobalStylesId'
)
) {
hasResolved = _globalStylesId
? hasFinishedResolution( 'getEditedEntityRecord', [
'root',
'globalStyles',
_globalStylesId,
] )
: true;
}
return {
globalStylesId: _globalStylesId,
isReady: hasResolved,
settings: record?.settings,
styles: record?.styles,
_links: record?._links,
};
},
[]
);
const { getEditedEntityRecord } = useSelect( coreStore );
const { editEntityRecord } = useDispatch( coreStore );
const config = useMemo( () => {
return {
settings: settings ?? {},
styles: styles ?? {},
_links: _links ?? {},
};
}, [ settings, styles, _links ] );
const setConfig = useCallback(
/**
* Set the global styles config.
* @param {Function|Object} callbackOrObject If the callbackOrObject is a function, pass the current config to the callback so the consumer can merge values.
* Otherwise, overwrite the current config with the incoming object.
* @param {Object} options Options for editEntityRecord Core selector.
*/
( callbackOrObject, options = {} ) => {
const record = getEditedEntityRecord(
'root',
'globalStyles',
globalStylesId
);
const currentConfig = {
styles: record?.styles ?? {},
settings: record?.settings ?? {},
_links: record?._links ?? {},
};
const updatedConfig =
typeof callbackOrObject === 'function'
? callbackOrObject( currentConfig )
: callbackOrObject;
editEntityRecord(
'root',
'globalStyles',
globalStylesId,
{
styles: cleanEmptyObject( updatedConfig.styles ) || {},
settings: cleanEmptyObject( updatedConfig.settings ) || {},
_links: cleanEmptyObject( updatedConfig._links ) || {},
},
options
);
},
[ globalStylesId, editEntityRecord, getEditedEntityRecord ]
);
return [ isReady, config, setConfig ];
}
function useGlobalStylesBaseConfig() {
const baseConfig = useSelect( ( select ) => {
const {
getCurrentUser,
getUser,
__experimentalGetCurrentThemeBaseGlobalStyles,
} = select( coreStore );
// Doing canUser( 'read', 'global_styles' ) returns false even for users with the capability.
// See: https://github.com/WordPress/gutenberg/issues/63438
// So we need to check the user capabilities directly.
const userId = getCurrentUser()?.id;
const canEditThemeOptions =
userId && getUser( userId )?.capabilities?.edit_theme_options;
return (
canEditThemeOptions &&
__experimentalGetCurrentThemeBaseGlobalStyles()
);
}, [] );
return [ !! baseConfig, baseConfig ];
}
export function useGlobalStylesContext() {
const [ isUserConfigReady, userConfig, setUserConfig ] =
useGlobalStylesUserConfig();
const [ isBaseConfigReady, baseConfig ] = useGlobalStylesBaseConfig();
const mergedConfig = useMemo( () => {
if ( ! baseConfig || ! userConfig ) {
return {};
}
return mergeBaseAndUserConfigs( baseConfig, userConfig );
}, [ userConfig, baseConfig ] );
const context = useMemo( () => {
return {
isReady: isUserConfigReady && isBaseConfigReady,
user: userConfig,
base: baseConfig,
merged: mergedConfig,
setUserConfig,
};
}, [
mergedConfig,
userConfig,
baseConfig,
setUserConfig,
isUserConfigReady,
isBaseConfigReady,
] );
return context;
}
export function GlobalStylesProvider( { children } ) {
const context = useGlobalStylesContext();
if ( ! context.isReady ) {
return null;
}
return (
<GlobalStylesContext.Provider value={ context }>
{ children }
</GlobalStylesContext.Provider>
);
}