/
color-edit.js
213 lines (191 loc) · 5.74 KB
/
color-edit.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
209
210
211
212
213
// The code in this file is copied entirely from https://github.com/WordPress/gutenberg/blob/HEAD/packages/block-library/src/button/color-edit.js
/* eslint-disable */
/**
* External dependencies
*/
import { pickBy, isEqual, isObject, identity, mapValues } from 'lodash';
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { useState, useEffect, useRef, Platform } from '@wordpress/element';
import { useSelect } from '@wordpress/data';
/**
* Internal dependencies
*/
import {
getColorObjectByColorValue,
getColorObjectByAttributeValues,
getGradientValueBySlug,
getGradientSlugByValue,
__experimentalPanelColorGradientSettings as PanelColorGradientSettings,
ContrastChecker,
InspectorControls,
} from '@wordpress/block-editor';
const isWebPlatform = Platform.OS === 'web';
function getBlockDOMNode( clientId ) {
return document.getElementById( 'block-' + clientId );
}
/**
* Removed undefined values from nested object.
*
* @param {*} object
* @return {*} Object cleaned from undefined values
*/
const cleanEmptyObject = ( object ) => {
if ( ! isObject( object ) ) {
return object;
}
const cleanedNestedObjects = pickBy( mapValues( object, cleanEmptyObject ), identity );
return isEqual( cleanedNestedObjects, {} ) ? undefined : cleanedNestedObjects;
};
function ColorPanel( { settings, clientId, enableContrastChecking = true } ) {
const { getComputedStyle, Node } = window;
const [ detectedBackgroundColor, setDetectedBackgroundColor ] = useState();
const [ detectedColor, setDetectedColor ] = useState();
const title = isWebPlatform
? __( 'Color settings', 'full-site-editing' )
: __( 'Color Settings', 'full-site-editing' );
useEffect( () => {
if ( isWebPlatform && ! enableContrastChecking ) {
return;
}
const colorsDetectionElement = getBlockDOMNode( clientId );
if ( ! colorsDetectionElement ) {
return;
}
setDetectedColor( getComputedStyle( colorsDetectionElement ).color );
let backgroundColorNode = colorsDetectionElement;
let backgroundColor = getComputedStyle( backgroundColorNode ).backgroundColor;
while (
backgroundColor === 'rgba(0, 0, 0, 0)' &&
backgroundColorNode.parentNode &&
backgroundColorNode.parentNode.nodeType === Node.ELEMENT_NODE
) {
backgroundColorNode = backgroundColorNode.parentNode;
backgroundColor = getComputedStyle( backgroundColorNode ).backgroundColor;
}
setDetectedBackgroundColor( backgroundColor );
} );
return (
<InspectorControls>
<PanelColorGradientSettings title={ title } initialOpen={ false } settings={ settings }>
{ isWebPlatform && enableContrastChecking && (
<ContrastChecker
backgroundColor={ detectedBackgroundColor }
textColor={ detectedColor }
/>
) }
</PanelColorGradientSettings>
</InspectorControls>
);
}
/**
* Inspector control panel containing the color related configuration
*
* @param {Object} props
*
* @return {WPElement} Color edit element.
*/
function ColorEdit( props ) {
const { attributes } = props;
const { colors, gradients } = useSelect( ( select ) => {
return select( 'core/block-editor' ).getSettings();
}, [] );
// Shouldn't be needed but right now the ColorGradientsPanel
// can trigger both onChangeColor and onChangeBackground
// synchronously causing our two callbacks to override changes
// from each other.
const localAttributes = useRef( attributes );
useEffect( () => {
localAttributes.current = attributes;
}, [ attributes ] );
const { style, textColor, backgroundColor, gradient } = attributes;
let gradientValue;
if ( gradient ) {
gradientValue = getGradientValueBySlug( gradients, gradient );
} else {
gradientValue = style?.color?.gradient;
}
const onChangeColor = ( name ) => ( value ) => {
const colorObject = getColorObjectByColorValue( colors, value );
const attributeName = name + 'Color';
const newStyle = {
...localAttributes.current.style,
color: {
...localAttributes.current?.style?.color,
[ name ]: colorObject?.slug ? undefined : value,
},
};
const newNamedColor = colorObject?.slug ? colorObject.slug : undefined;
const newAttributes = {
style: cleanEmptyObject( newStyle ),
[ attributeName ]: newNamedColor,
};
props.setAttributes( newAttributes );
localAttributes.current = {
...localAttributes.current,
...newAttributes,
};
};
const onChangeGradient = ( value ) => {
const slug = getGradientSlugByValue( gradients, value );
let newAttributes;
if ( slug ) {
const newStyle = {
...localAttributes.current?.style,
color: {
...localAttributes.current?.style?.color,
gradient: undefined,
},
};
newAttributes = {
style: cleanEmptyObject( newStyle ),
gradient: slug,
};
} else {
const newStyle = {
...localAttributes.current?.style,
color: {
...localAttributes.current?.style?.color,
gradient: value,
},
};
newAttributes = {
style: cleanEmptyObject( newStyle ),
gradient: undefined,
};
}
props.setAttributes( newAttributes );
localAttributes.current = {
...localAttributes.current,
...newAttributes,
};
};
return (
<ColorPanel
enableContrastChecking={ ! gradient && ! style?.color?.gradient }
clientId={ props.clientId }
settings={ [
{
label: __( 'Text Color', 'full-site-editing' ),
onColorChange: onChangeColor( 'text' ),
colorValue: getColorObjectByAttributeValues( colors, textColor, style?.color?.text )
.color,
},
{
label: __( 'Background Color', 'full-site-editing' ),
onColorChange: onChangeColor( 'background' ),
colorValue: getColorObjectByAttributeValues(
colors,
backgroundColor,
style?.color?.background
).color,
gradientValue,
onGradientChange: onChangeGradient,
},
] }
/>
);
}
export default ColorEdit;