@wordpress/format-library 3.0.18-next.33ec3857e2.0 → 3.0.21

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.
@@ -1,163 +0,0 @@
1
- /**
2
- * WordPress dependencies
3
- */
4
- import { useCallback, useMemo } from '@wordpress/element';
5
- import {
6
- applyFormat,
7
- removeFormat,
8
- getActiveFormat,
9
- } from '@wordpress/rich-text';
10
- import {
11
- useSetting,
12
- getColorClassName,
13
- getColorObjectByColorValue,
14
- } from '@wordpress/block-editor';
15
- import { BottomSheet, ColorSettings } from '@wordpress/components';
16
-
17
- /**
18
- * Internal dependencies
19
- */
20
- import { textColor as settings } from './index';
21
- import { transparentValue } from './index.js';
22
- import { parseClassName } from './inline.js';
23
-
24
- function parseCSS( css = '' ) {
25
- return css.split( ';' ).reduce( ( accumulator, rule ) => {
26
- if ( rule ) {
27
- const [ property, value ] = rule.replace( / /g, '' ).split( ':' );
28
- if ( property === 'color' ) accumulator.color = value;
29
- if ( property === 'background-color' && value !== transparentValue )
30
- accumulator.backgroundColor = value;
31
- }
32
- return accumulator;
33
- }, {} );
34
- }
35
-
36
- function getActiveColors( value, name, colorSettings ) {
37
- const activeColorFormat = getActiveFormat( value, name );
38
-
39
- if ( ! activeColorFormat ) {
40
- return {};
41
- }
42
-
43
- return {
44
- ...parseCSS( activeColorFormat.attributes.style ),
45
- ...parseClassName( activeColorFormat.attributes.class, colorSettings ),
46
- };
47
- }
48
-
49
- function setColors( value, name, colorSettings, colors ) {
50
- const { color, backgroundColor } = {
51
- ...getActiveColors( value, name, colorSettings ),
52
- ...colors,
53
- };
54
-
55
- if ( ! color && ! backgroundColor ) {
56
- return removeFormat( value, name );
57
- }
58
-
59
- const styles = [];
60
- const classNames = [];
61
- const attributes = {};
62
-
63
- if ( backgroundColor ) {
64
- styles.push( [ 'background-color', backgroundColor ].join( ':' ) );
65
- } else {
66
- // Override default browser color for mark element.
67
- styles.push( [ 'background-color', transparentValue ].join( ':' ) );
68
- }
69
-
70
- if ( color ) {
71
- const colorObject = getColorObjectByColorValue( colorSettings, color );
72
-
73
- if ( colorObject ) {
74
- classNames.push( getColorClassName( 'color', colorObject.slug ) );
75
- styles.push( [ 'color', colorObject.color ].join( ':' ) );
76
- } else {
77
- styles.push( [ 'color', color ].join( ':' ) );
78
- }
79
- }
80
-
81
- if ( styles.length ) attributes.style = styles.join( ';' );
82
- if ( classNames.length ) attributes.class = classNames.join( ' ' );
83
-
84
- const format = { type: name, attributes };
85
-
86
- // For cases when there is no text selected, formatting is forced
87
- // for the first empty character
88
- if (
89
- value?.start === value?.end &&
90
- ( value?.text.length === 0 ||
91
- value.text?.charAt( value.end - 1 ) === ' ' )
92
- ) {
93
- return applyFormat( value, format, value?.start - 1, value?.end + 1 );
94
- }
95
-
96
- return applyFormat( value, format );
97
- }
98
-
99
- function ColorPicker( { name, value, onChange } ) {
100
- const property = 'color';
101
- const colors = useSetting( 'color.palette' ) || settings.colors;
102
- const colorSettings = {
103
- colors,
104
- };
105
-
106
- const onColorChange = useCallback(
107
- ( color ) => {
108
- if ( color !== '' ) {
109
- onChange(
110
- setColors( value, name, colors, { [ property ]: color } )
111
- );
112
- // Remove formatting if the color was reset, there's no
113
- // current selection and the previous character is a space
114
- } else if (
115
- value?.start === value?.end &&
116
- value.text?.charAt( value?.end - 1 ) === ' '
117
- ) {
118
- onChange(
119
- removeFormat( value, name, value.end - 1, value.end )
120
- );
121
- } else {
122
- onChange( removeFormat( value, name ) );
123
- }
124
- },
125
- [ colors, onChange, property ]
126
- );
127
- const activeColors = useMemo(
128
- () => getActiveColors( value, name, colors ),
129
- [ name, value, colors ]
130
- );
131
-
132
- return (
133
- <ColorSettings
134
- colorValue={ activeColors[ property ] }
135
- onColorChange={ onColorChange }
136
- defaultSettings={ colorSettings }
137
- hideNavigation
138
- />
139
- );
140
- }
141
-
142
- export default function InlineColorUI( { name, value, onChange, onClose } ) {
143
- return (
144
- <BottomSheet
145
- isVisible
146
- onClose={ onClose }
147
- hideHeader
148
- contentStyle={ { paddingLeft: 0, paddingRight: 0 } }
149
- hasNavigation
150
- leftButton={ null }
151
- >
152
- <BottomSheet.NavigationContainer animate main>
153
- <BottomSheet.NavigationScreen name="text-color">
154
- <ColorPicker
155
- name={ name }
156
- value={ value }
157
- onChange={ onChange }
158
- />
159
- </BottomSheet.NavigationScreen>
160
- </BottomSheet.NavigationContainer>
161
- </BottomSheet>
162
- );
163
- }
@@ -1,23 +0,0 @@
1
- .components-inline-color--is-active {
2
- border-radius: 18px;
3
- }
4
-
5
- .components-inline-color__outline {
6
- border-color: $light-dim;
7
- top: 6px;
8
- bottom: 6px;
9
- left: 11px;
10
- right: 11px;
11
- border-radius: 24px;
12
- border-width: $border-width;
13
- position: absolute;
14
- z-index: 2;
15
- }
16
-
17
- .components-inline-color__outline--dark {
18
- border-color: $dark-ultra-dim;
19
- }
20
-
21
- .components-inline-color__button-container {
22
- padding: 6px;
23
- }