-
Notifications
You must be signed in to change notification settings - Fork 4.1k
/
inline.js
185 lines (166 loc) · 4.48 KB
/
inline.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 } from 'lodash';
/**
* WordPress dependencies
*/
import { useCallback, useMemo } from '@wordpress/element';
import { useSelect } from '@wordpress/data';
import {
applyFormat,
removeFormat,
getActiveFormat,
useAnchorRef,
} from '@wordpress/rich-text';
import {
ColorPalette,
getColorClassName,
getColorObjectByColorValue,
getColorObjectByAttributeValues,
store as blockEditorStore,
useCachedTruthy,
} from '@wordpress/block-editor';
import { Popover, TabPanel } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
/**
* Internal dependencies
*/
import { textColor as settings, transparentValue } from './index';
function parseCSS( css = '' ) {
return css.split( ';' ).reduce( ( accumulator, rule ) => {
if ( rule ) {
const [ property, value ] = rule.split( ':' );
if ( property === 'color' ) accumulator.color = value;
if ( property === 'background-color' && value !== transparentValue )
accumulator.backgroundColor = value;
}
return accumulator;
}, {} );
}
export function parseClassName( className = '', colorSettings ) {
return className.split( ' ' ).reduce( ( accumulator, name ) => {
// `colorSlug` could contain dashes, so simply match the start and end.
if ( name.startsWith( 'has-' ) && name.endsWith( '-color' ) ) {
const colorSlug = name
.replace( /^has-/, '' )
.replace( /-color$/, '' );
const colorObject = getColorObjectByAttributeValues(
colorSettings,
colorSlug
);
accumulator.color = colorObject.color;
}
return accumulator;
}, {} );
}
export function getActiveColors( value, name, colorSettings ) {
const activeColorFormat = getActiveFormat( value, name );
if ( ! activeColorFormat ) {
return {};
}
return {
...parseCSS( activeColorFormat.attributes.style ),
...parseClassName( activeColorFormat.attributes.class, colorSettings ),
};
}
function setColors( value, name, colorSettings, colors ) {
const { color, backgroundColor } = {
...getActiveColors( value, name, colorSettings ),
...colors,
};
if ( ! color && ! backgroundColor ) {
return removeFormat( value, name );
}
const styles = [];
const classNames = [];
const attributes = {};
if ( backgroundColor ) {
styles.push( [ 'background-color', backgroundColor ].join( ':' ) );
} else {
// Override default browser color for mark element.
styles.push( [ 'background-color', transparentValue ].join( ':' ) );
}
if ( color ) {
const colorObject = getColorObjectByColorValue( colorSettings, color );
if ( colorObject ) {
classNames.push( getColorClassName( 'color', colorObject.slug ) );
} else {
styles.push( [ 'color', color ].join( ':' ) );
}
}
if ( styles.length ) attributes.style = styles.join( ';' );
if ( classNames.length ) attributes.class = classNames.join( ' ' );
return applyFormat( value, { type: name, attributes } );
}
function ColorPicker( { name, property, value, onChange } ) {
const colors = useSelect( ( select ) => {
const { getSettings } = select( blockEditorStore );
return get( getSettings(), [ 'colors' ], [] );
}, [] );
const onColorChange = useCallback(
( color ) => {
onChange(
setColors( value, name, colors, { [ property ]: color } )
);
},
[ colors, onChange, property ]
);
const activeColors = useMemo(
() => getActiveColors( value, name, colors ),
[ name, value, colors ]
);
return (
<ColorPalette
value={ activeColors[ property ] }
onChange={ onColorChange }
/>
);
}
export default function InlineColorUI( {
name,
value,
onChange,
onClose,
contentRef,
} ) {
/*
As you change the text color by typing a HEX value into a field,
the return value of document.getSelection jumps to the field you're editing,
not the highlighted text. Given that useAnchorRef uses document.getSelection,
it will return null, since it can't find the <mark> element within the HEX input.
This caches the last truthy value of the selection anchor reference.
*/
const anchorRef = useCachedTruthy(
useAnchorRef( { ref: contentRef, value, settings } )
);
return (
<Popover
onClose={ onClose }
className="components-inline-color-popover"
anchorRef={ anchorRef }
>
<TabPanel
tabs={ [
{
name: 'color',
title: __( 'Text' ),
},
{
name: 'backgroundColor',
title: __( 'Background' ),
},
] }
>
{ ( tab ) => (
<ColorPicker
name={ name }
property={ tab.name }
value={ value }
onChange={ onChange }
/>
) }
</TabPanel>
</Popover>
);
}