/
global-styles-provider.js
185 lines (168 loc) · 4.28 KB
/
global-styles-provider.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
/**
* External dependencies
*/
import {
get,
cloneDeep,
set,
mergeWith,
pickBy,
isEmpty,
isObject,
identity,
mapValues,
} from 'lodash';
/**
* WordPress dependencies
*/
import { useMemo, useCallback } from '@wordpress/element';
import { useSelect, useDispatch } from '@wordpress/data';
import { store as coreStore } from '@wordpress/core-data';
/**
* Internal dependencies
*/
import { PRESET_METADATA } from './utils';
import { GlobalStylesContext } from './context';
function mergeTreesCustomizer( _, srcValue ) {
// We only pass as arrays the presets,
// in which case we want the new array of values
// to override the old array (no merging).
if ( Array.isArray( srcValue ) ) {
return srcValue;
}
}
function mergeBaseAndUserConfigs( base, user ) {
return mergeWith( {}, base, user, mergeTreesCustomizer );
}
function addUserOriginToSettings( settingsToAdd ) {
const newSettings = cloneDeep( settingsToAdd );
PRESET_METADATA.forEach( ( { path } ) => {
const presetData = get( newSettings, path );
if ( presetData ) {
set( newSettings, path, {
user: presetData,
} );
}
} );
return newSettings;
}
function removeUserOriginFromSettings( settingsToRemove ) {
const newSettings = cloneDeep( settingsToRemove );
PRESET_METADATA.forEach( ( { path } ) => {
const presetData = get( newSettings, path );
if ( presetData ) {
set( newSettings, path, ( presetData ?? {} ).user );
}
} );
return newSettings;
}
const cleanEmptyObject = ( object ) => {
if ( ! isObject( object ) || Array.isArray( object ) ) {
return object;
}
const cleanedNestedObjects = pickBy(
mapValues( object, cleanEmptyObject ),
identity
);
return isEmpty( cleanedNestedObjects ) ? undefined : cleanedNestedObjects;
};
function useGlobalStylesUserConfig() {
const { globalStylesId, settings, styles } = useSelect( ( select ) => {
const _globalStylesId = select(
coreStore
).__experimentalGetCurrentGlobalStylesId();
const record = _globalStylesId
? select( coreStore ).getEditedEntityRecord(
'root',
'globalStyles',
_globalStylesId
)
: undefined;
return {
globalStylesId: _globalStylesId,
settings: record?.settings,
styles: record?.styles,
};
}, [] );
const { getEditedEntityRecord } = useSelect( coreStore );
const { editEntityRecord } = useDispatch( coreStore );
const config = useMemo( () => {
return {
settings: addUserOriginToSettings( settings ?? {} ),
styles: styles ?? {},
};
}, [ settings, styles ] );
const setConfig = useCallback(
( callback ) => {
const record = getEditedEntityRecord(
'root',
'globalStyles',
globalStylesId
);
const currentConfig = {
styles: record?.styles ?? {},
settings: addUserOriginToSettings( record?.settings ?? {} ),
};
const updatedConfig = callback( currentConfig );
editEntityRecord( 'root', 'globalStyles', globalStylesId, {
styles: cleanEmptyObject( updatedConfig.styles ) || {},
settings:
cleanEmptyObject(
removeUserOriginFromSettings( updatedConfig.settings )
) || {},
} );
},
[ globalStylesId ]
);
return [ !! settings || !! styles, config, setConfig ];
}
function useGlobalStylesBaseConfig() {
const baseConfig = useSelect( ( select ) => {
return select(
coreStore
).__experimentalGetCurrentThemeBaseGlobalStyles();
}, [] );
return [ !! baseConfig, baseConfig ];
}
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>
);
}