@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,171 +0,0 @@
1
- import { createElement, Fragment } from "@wordpress/element";
2
-
3
- /**
4
- * External dependencies
5
- */
6
- import { isEmpty } from 'lodash';
7
- import { View } from 'react-native';
8
- /**
9
- * WordPress dependencies
10
- */
11
-
12
- import { __ } from '@wordpress/i18n';
13
- import { useCallback, useMemo, useState } from '@wordpress/element';
14
- import { BlockControls, useSetting } from '@wordpress/block-editor';
15
- import { ToolbarGroup, ToolbarButton } from '@wordpress/components';
16
- import { Icon, textColor as textColorIcon } from '@wordpress/icons';
17
- import { removeFormat } from '@wordpress/rich-text';
18
- import { usePreferredColorSchemeStyle } from '@wordpress/compose';
19
- /**
20
- * Internal dependencies
21
- */
22
-
23
- import { getActiveColors } from './inline.js';
24
- import { transparentValue } from './index.js';
25
- import { default as InlineColorUI } from './inline';
26
- import styles from './style.scss';
27
- const name = 'core/text-color';
28
-
29
- const title = __('Text color');
30
-
31
- const EMPTY_ARRAY = [];
32
-
33
- function getComputedStyleProperty(element, property) {
34
- const {
35
- props: {
36
- style = {}
37
- }
38
- } = element;
39
-
40
- if (property === 'background-color') {
41
- var _baseColors$color;
42
-
43
- const {
44
- backgroundColor,
45
- baseColors
46
- } = style;
47
-
48
- if (backgroundColor !== 'transparent') {
49
- return backgroundColor;
50
- } else if (baseColors && baseColors !== null && baseColors !== void 0 && (_baseColors$color = baseColors.color) !== null && _baseColors$color !== void 0 && _baseColors$color.background) {
51
- var _baseColors$color2;
52
-
53
- return baseColors === null || baseColors === void 0 ? void 0 : (_baseColors$color2 = baseColors.color) === null || _baseColors$color2 === void 0 ? void 0 : _baseColors$color2.background;
54
- }
55
-
56
- return 'transparent';
57
- }
58
- }
59
-
60
- function fillComputedColors(element, _ref) {
61
- let {
62
- color,
63
- backgroundColor
64
- } = _ref;
65
-
66
- if (!color && !backgroundColor) {
67
- return;
68
- }
69
-
70
- return {
71
- color: color || getComputedStyleProperty(element, 'color'),
72
- backgroundColor: getComputedStyleProperty(element, 'background-color')
73
- };
74
- }
75
-
76
- function TextColorEdit(_ref2) {
77
- let {
78
- value,
79
- onChange,
80
- isActive,
81
- activeAttributes,
82
- contentRef
83
- } = _ref2;
84
- const allowCustomControl = useSetting('color.custom');
85
- const colors = useSetting('color.palette') || EMPTY_ARRAY;
86
- const [isAddingColor, setIsAddingColor] = useState(false);
87
- const enableIsAddingColor = useCallback(() => setIsAddingColor(true), [setIsAddingColor]);
88
- const disableIsAddingColor = useCallback(() => setIsAddingColor(false), [setIsAddingColor]);
89
- const colorIndicatorStyle = useMemo(() => fillComputedColors(contentRef, getActiveColors(value, name, colors)), [value, colors]);
90
- const hasColorsToChoose = !isEmpty(colors) || !allowCustomControl;
91
- const onPressButton = useCallback(() => {
92
- if (hasColorsToChoose) {
93
- enableIsAddingColor();
94
- } else {
95
- onChange(removeFormat(value, name));
96
- }
97
- }, [hasColorsToChoose, value]);
98
- const outlineStyle = usePreferredColorSchemeStyle(styles['components-inline-color__outline'], styles['components-inline-color__outline--dark']);
99
-
100
- if (!hasColorsToChoose && !isActive) {
101
- return null;
102
- }
103
-
104
- const isActiveStyle = { ...colorIndicatorStyle,
105
- ...(!(colorIndicatorStyle !== null && colorIndicatorStyle !== void 0 && colorIndicatorStyle.backgroundColor) ? {
106
- backgroundColor: 'transparent'
107
- } : {}),
108
- ...styles['components-inline-color--is-active']
109
- };
110
- const customContainerStyles = styles['components-inline-color__button-container'];
111
- return createElement(Fragment, null, createElement(BlockControls, null, createElement(ToolbarGroup, null, isActive && createElement(View, {
112
- style: outlineStyle,
113
- pointerEvents: "none"
114
- }), createElement(ToolbarButton, {
115
- name: "text-color",
116
- isActive: isActive,
117
- icon: createElement(Icon, {
118
- icon: textColorIcon,
119
- style: (colorIndicatorStyle === null || colorIndicatorStyle === void 0 ? void 0 : colorIndicatorStyle.color) && {
120
- color: colorIndicatorStyle.color
121
- }
122
- }),
123
- title: title,
124
- extraProps: {
125
- isActiveStyle,
126
- customContainerStyles
127
- } // If has no colors to choose but a color is active remove the color onClick
128
- ,
129
- onClick: onPressButton
130
- }))), isAddingColor && createElement(InlineColorUI, {
131
- name: name,
132
- onClose: disableIsAddingColor,
133
- activeAttributes: activeAttributes,
134
- value: value,
135
- onChange: onChange,
136
- contentRef: contentRef
137
- }));
138
- }
139
-
140
- export const textColor = {
141
- name,
142
- title,
143
- tagName: 'mark',
144
- className: 'has-inline-color',
145
- attributes: {
146
- style: 'style',
147
- class: 'class'
148
- },
149
-
150
- /*
151
- * Since this format relies on the <mark> tag, it's important to
152
- * prevent the default yellow background color applied by most
153
- * browsers. The solution is to detect when this format is used with a
154
- * text color but no background color, and in such cases to override
155
- * the default styling with a transparent background.
156
- *
157
- * @see https://github.com/WordPress/gutenberg/pull/35516
158
- */
159
- __unstableFilterAttributeValue(key, value) {
160
- if (key !== 'style') return value; // We should not add a background-color if it's already set
161
-
162
- if (value && value.includes('background-color')) return value;
163
- const addedCSS = ['background-color', transparentValue].join(':'); // Prepend `addedCSS` to avoid a double `;;` as any the existing CSS
164
- // rules will already include a `;`.
165
-
166
- return value ? [addedCSS, value].join(';') : addedCSS;
167
- },
168
-
169
- edit: TextColorEdit
170
- };
171
- //# sourceMappingURL=index.native.js.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["@wordpress/format-library/src/text-color/index.native.js"],"names":["isEmpty","View","__","useCallback","useMemo","useState","BlockControls","useSetting","ToolbarGroup","ToolbarButton","Icon","textColor","textColorIcon","removeFormat","usePreferredColorSchemeStyle","getActiveColors","transparentValue","default","InlineColorUI","styles","name","title","EMPTY_ARRAY","getComputedStyleProperty","element","property","props","style","backgroundColor","baseColors","color","background","fillComputedColors","TextColorEdit","value","onChange","isActive","activeAttributes","contentRef","allowCustomControl","colors","isAddingColor","setIsAddingColor","enableIsAddingColor","disableIsAddingColor","colorIndicatorStyle","hasColorsToChoose","onPressButton","outlineStyle","isActiveStyle","customContainerStyles","tagName","className","attributes","class","__unstableFilterAttributeValue","key","includes","addedCSS","join","edit"],"mappings":";;AAAA;AACA;AACA;AACA,SAASA,OAAT,QAAwB,QAAxB;AACA,SAASC,IAAT,QAAqB,cAArB;AAEA;AACA;AACA;;AACA,SAASC,EAAT,QAAmB,iBAAnB;AACA,SAASC,WAAT,EAAsBC,OAAtB,EAA+BC,QAA/B,QAA+C,oBAA/C;AACA,SAASC,aAAT,EAAwBC,UAAxB,QAA0C,yBAA1C;AACA,SAASC,YAAT,EAAuBC,aAAvB,QAA4C,uBAA5C;AACA,SAASC,IAAT,EAAeC,SAAS,IAAIC,aAA5B,QAAiD,kBAAjD;AACA,SAASC,YAAT,QAA6B,sBAA7B;AACA,SAASC,4BAAT,QAA6C,oBAA7C;AAEA;AACA;AACA;;AACA,SAASC,eAAT,QAAgC,aAAhC;AACA,SAASC,gBAAT,QAAiC,YAAjC;AACA,SAASC,OAAO,IAAIC,aAApB,QAAyC,UAAzC;AACA,OAAOC,MAAP,MAAmB,cAAnB;AAEA,MAAMC,IAAI,GAAG,iBAAb;;AACA,MAAMC,KAAK,GAAGnB,EAAE,CAAE,YAAF,CAAhB;;AAEA,MAAMoB,WAAW,GAAG,EAApB;;AAEA,SAASC,wBAAT,CAAmCC,OAAnC,EAA4CC,QAA5C,EAAuD;AACtD,QAAM;AACLC,IAAAA,KAAK,EAAE;AAAEC,MAAAA,KAAK,GAAG;AAAV;AADF,MAEFH,OAFJ;;AAIA,MAAKC,QAAQ,KAAK,kBAAlB,EAAuC;AAAA;;AACtC,UAAM;AAAEG,MAAAA,eAAF;AAAmBC,MAAAA;AAAnB,QAAkCF,KAAxC;;AAEA,QAAKC,eAAe,KAAK,aAAzB,EAAyC;AACxC,aAAOA,eAAP;AACA,KAFD,MAEO,IAAKC,UAAU,IAAIA,UAAJ,aAAIA,UAAJ,oCAAIA,UAAU,CAAEC,KAAhB,8CAAI,kBAAmBC,UAAtC,EA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;AA3BkB,CAAlB","sourcesContent":["/**\n * External dependencies\n */\nimport { isEmpty } from 'lodash';\nimport { View } from 'react-native';\n\n/**\n * WordPress dependencies\n */\nimport { __ } from '@wordpress/i18n';\nimport { useCallback, useMemo, useState } from '@wordpress/element';\nimport { BlockControls, useSetting } from '@wordpress/block-editor';\nimport { ToolbarGroup, ToolbarButton } from '@wordpress/components';\nimport { Icon, textColor as textColorIcon } from '@wordpress/icons';\nimport { removeFormat } from '@wordpress/rich-text';\nimport { usePreferredColorSchemeStyle } from '@wordpress/compose';\n\n/**\n * Internal dependencies\n */\nimport { getActiveColors } from './inline.js';\nimport { transparentValue } from './index.js';\nimport { default as InlineColorUI } from './inline';\nimport styles from './style.scss';\n\nconst name = 'core/text-color';\nconst title = __( 'Text color' );\n\nconst EMPTY_ARRAY = [];\n\nfunction getComputedStyleProperty( element, property ) {\n\tconst {\n\t\tprops: { style = {} },\n\t} = element;\n\n\tif ( property === 'background-color' ) {\n\t\tconst { backgroundColor, baseColors } = style;\n\n\t\tif ( backgroundColor !== 'transparent' ) {\n\t\t\treturn backgroundColor;\n\t\t} else if ( baseColors && baseColors?.color?.background ) {\n\t\t\treturn baseColors?.color?.background;\n\t\t}\n\n\t\treturn 'transparent';\n\t}\n}\n\nfunction fillComputedColors( element, { color, backgroundColor } ) {\n\tif ( ! color && ! backgroundColor ) {\n\t\treturn;\n\t}\n\n\treturn {\n\t\tcolor: color || getComputedStyleProperty( element, 'color' ),\n\t\tbackgroundColor: getComputedStyleProperty(\n\t\t\telement,\n\t\t\t'background-color'\n\t\t),\n\t};\n}\n\nfunction TextColorEdit( {\n\tvalue,\n\tonChange,\n\tisActive,\n\tactiveAttributes,\n\tcontentRef,\n} ) {\n\tconst allowCustomControl = useSetting( 'color.custom' );\n\tconst colors = useSetting( 'color.palette' ) || EMPTY_ARRAY;\n\tconst [ isAddingColor, setIsAddingColor ] = useState( false );\n\tconst enableIsAddingColor = useCallback( () => setIsAddingColor( true ), [\n\t\tsetIsAddingColor,\n\t] );\n\tconst disableIsAddingColor = useCallback( () => setIsAddingColor( false ), [\n\t\tsetIsAddingColor,\n\t] );\n\tconst colorIndicatorStyle = useMemo(\n\t\t() =>\n\t\t\tfillComputedColors(\n\t\t\t\tcontentRef,\n\t\t\t\tgetActiveColors( value, name, colors )\n\t\t\t),\n\t\t[ value, colors ]\n\t);\n\n\tconst hasColorsToChoose = ! isEmpty( colors ) || ! allowCustomControl;\n\n\tconst onPressButton = useCallback( () => {\n\t\tif ( hasColorsToChoose ) {\n\t\t\tenableIsAddingColor();\n\t\t} else {\n\t\t\tonChange( removeFormat( value, name ) );\n\t\t}\n\t}, [ hasColorsToChoose, value ] );\n\n\tconst outlineStyle = usePreferredColorSchemeStyle(\n\t\tstyles[ 'components-inline-color__outline' ],\n\t\tstyles[ 'components-inline-color__outline--dark' ]\n\t);\n\n\tif ( ! hasColorsToChoose && ! isActive ) {\n\t\treturn null;\n\t}\n\n\tconst isActiveStyle = {\n\t\t...colorIndicatorStyle,\n\t\t...( ! colorIndicatorStyle?.backgroundColor\n\t\t\t? { backgroundColor: 'transparent' }\n\t\t\t: {} ),\n\t\t...styles[ 'components-inline-color--is-active' ],\n\t};\n\n\tconst customContainerStyles =\n\t\tstyles[ 'components-inline-color__button-container' ];\n\n\treturn (\n\t\t<>\n\t\t\t<BlockControls>\n\t\t\t\t<ToolbarGroup>\n\t\t\t\t\t{ isActive && (\n\t\t\t\t\t\t<View style={ outlineStyle } pointerEvents=\"none\" />\n\t\t\t\t\t) }\n\n\t\t\t\t\t<ToolbarButton\n\t\t\t\t\t\tname=\"text-color\"\n\t\t\t\t\t\tisActive={ isActive }\n\t\t\t\t\t\ticon={\n\t\t\t\t\t\t\t<Icon\n\t\t\t\t\t\t\t\ticon={ textColorIcon }\n\t\t\t\t\t\t\t\tstyle={\n\t\t\t\t\t\t\t\t\tcolorIndicatorStyle?.color && {\n\t\t\t\t\t\t\t\t\t\tcolor: colorIndicatorStyle.color,\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t/>\n\t\t\t\t\t\t}\n\t\t\t\t\t\ttitle={ title }\n\t\t\t\t\t\textraProps={ {\n\t\t\t\t\t\t\tisActiveStyle,\n\t\t\t\t\t\t\tcustomContainerStyles,\n\t\t\t\t\t\t} }\n\t\t\t\t\t\t// If has no colors to choose but a color is active remove the color onClick\n\t\t\t\t\t\tonClick={ onPressButton }\n\t\t\t\t\t/>\n\t\t\t\t</ToolbarGroup>\n\t\t\t</BlockControls>\n\t\t\t{ isAddingColor && (\n\t\t\t\t<InlineColorUI\n\t\t\t\t\tname={ name }\n\t\t\t\t\tonClose={ disableIsAddingColor }\n\t\t\t\t\tactiveAttributes={ activeAttributes }\n\t\t\t\t\tvalue={ value }\n\t\t\t\t\tonChange={ onChange }\n\t\t\t\t\tcontentRef={ contentRef }\n\t\t\t\t/>\n\t\t\t) }\n\t\t</>\n\t);\n}\n\nexport const textColor = {\n\tname,\n\ttitle,\n\ttagName: 'mark',\n\tclassName: 'has-inline-color',\n\tattributes: {\n\t\tstyle: 'style',\n\t\tclass: 'class',\n\t},\n\t/*\n\t * Since this format relies on the <mark> tag, it's important to\n\t * prevent the default yellow background color applied by most\n\t * browsers. The solution is to detect when this format is used with a\n\t * text color but no background color, and in such cases to override\n\t * the default styling with a transparent background.\n\t *\n\t * @see https://github.com/WordPress/gutenberg/pull/35516\n\t */\n\t__unstableFilterAttributeValue( key, value ) {\n\t\tif ( key !== 'style' ) return value;\n\t\t// We should not add a background-color if it's already set\n\t\tif ( value && value.includes( 'background-color' ) ) return value;\n\t\tconst addedCSS = [ 'background-color', transparentValue ].join( ':' );\n\t\t// Prepend `addedCSS` to avoid a double `;;` as any the existing CSS\n\t\t// rules will already include a `;`.\n\t\treturn value ? [ addedCSS, value ].join( ';' ) : addedCSS;\n\t},\n\tedit: TextColorEdit,\n};\n"]}
@@ -1,156 +0,0 @@
1
- import { createElement } from "@wordpress/element";
2
-
3
- /**
4
- * WordPress dependencies
5
- */
6
- import { useCallback, useMemo } from '@wordpress/element';
7
- import { applyFormat, removeFormat, getActiveFormat } from '@wordpress/rich-text';
8
- import { useSetting, getColorClassName, getColorObjectByColorValue } from '@wordpress/block-editor';
9
- import { BottomSheet, ColorSettings } from '@wordpress/components';
10
- /**
11
- * Internal dependencies
12
- */
13
-
14
- import { textColor as settings } from './index';
15
- import { transparentValue } from './index.js';
16
- import { parseClassName } from './inline.js';
17
-
18
- function parseCSS() {
19
- let css = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
20
- return css.split(';').reduce((accumulator, rule) => {
21
- if (rule) {
22
- const [property, value] = rule.replace(/ /g, '').split(':');
23
- if (property === 'color') accumulator.color = value;
24
- if (property === 'background-color' && value !== transparentValue) accumulator.backgroundColor = value;
25
- }
26
-
27
- return accumulator;
28
- }, {});
29
- }
30
-
31
- function getActiveColors(value, name, colorSettings) {
32
- const activeColorFormat = getActiveFormat(value, name);
33
-
34
- if (!activeColorFormat) {
35
- return {};
36
- }
37
-
38
- return { ...parseCSS(activeColorFormat.attributes.style),
39
- ...parseClassName(activeColorFormat.attributes.class, colorSettings)
40
- };
41
- }
42
-
43
- function setColors(value, name, colorSettings, colors) {
44
- var _value$text;
45
-
46
- const {
47
- color,
48
- backgroundColor
49
- } = { ...getActiveColors(value, name, colorSettings),
50
- ...colors
51
- };
52
-
53
- if (!color && !backgroundColor) {
54
- return removeFormat(value, name);
55
- }
56
-
57
- const styles = [];
58
- const classNames = [];
59
- const attributes = {};
60
-
61
- if (backgroundColor) {
62
- styles.push(['background-color', backgroundColor].join(':'));
63
- } else {
64
- // Override default browser color for mark element.
65
- styles.push(['background-color', transparentValue].join(':'));
66
- }
67
-
68
- if (color) {
69
- const colorObject = getColorObjectByColorValue(colorSettings, color);
70
-
71
- if (colorObject) {
72
- classNames.push(getColorClassName('color', colorObject.slug));
73
- styles.push(['color', colorObject.color].join(':'));
74
- } else {
75
- styles.push(['color', color].join(':'));
76
- }
77
- }
78
-
79
- if (styles.length) attributes.style = styles.join(';');
80
- if (classNames.length) attributes.class = classNames.join(' ');
81
- const format = {
82
- type: name,
83
- attributes
84
- }; // For cases when there is no text selected, formatting is forced
85
- // for the first empty character
86
-
87
- if ((value === null || value === void 0 ? void 0 : value.start) === (value === null || value === void 0 ? void 0 : value.end) && ((value === null || value === void 0 ? void 0 : value.text.length) === 0 || ((_value$text = value.text) === null || _value$text === void 0 ? void 0 : _value$text.charAt(value.end - 1)) === ' ')) {
88
- return applyFormat(value, format, (value === null || value === void 0 ? void 0 : value.start) - 1, (value === null || value === void 0 ? void 0 : value.end) + 1);
89
- }
90
-
91
- return applyFormat(value, format);
92
- }
93
-
94
- function ColorPicker(_ref) {
95
- let {
96
- name,
97
- value,
98
- onChange
99
- } = _ref;
100
- const property = 'color';
101
- const colors = useSetting('color.palette') || settings.colors;
102
- const colorSettings = {
103
- colors
104
- };
105
- const onColorChange = useCallback(color => {
106
- var _value$text2;
107
-
108
- if (color !== '') {
109
- onChange(setColors(value, name, colors, {
110
- [property]: color
111
- })); // Remove formatting if the color was reset, there's no
112
- // current selection and the previous character is a space
113
- } else if ((value === null || value === void 0 ? void 0 : value.start) === (value === null || value === void 0 ? void 0 : value.end) && ((_value$text2 = value.text) === null || _value$text2 === void 0 ? void 0 : _value$text2.charAt((value === null || value === void 0 ? void 0 : value.end) - 1)) === ' ') {
114
- onChange(removeFormat(value, name, value.end - 1, value.end));
115
- } else {
116
- onChange(removeFormat(value, name));
117
- }
118
- }, [colors, onChange, property]);
119
- const activeColors = useMemo(() => getActiveColors(value, name, colors), [name, value, colors]);
120
- return createElement(ColorSettings, {
121
- colorValue: activeColors[property],
122
- onColorChange: onColorChange,
123
- defaultSettings: colorSettings,
124
- hideNavigation: true
125
- });
126
- }
127
-
128
- export default function InlineColorUI(_ref2) {
129
- let {
130
- name,
131
- value,
132
- onChange,
133
- onClose
134
- } = _ref2;
135
- return createElement(BottomSheet, {
136
- isVisible: true,
137
- onClose: onClose,
138
- hideHeader: true,
139
- contentStyle: {
140
- paddingLeft: 0,
141
- paddingRight: 0
142
- },
143
- hasNavigation: true,
144
- leftButton: null
145
- }, createElement(BottomSheet.NavigationContainer, {
146
- animate: true,
147
- main: true
148
- }, createElement(BottomSheet.NavigationScreen, {
149
- name: "text-color"
150
- }, createElement(ColorPicker, {
151
- name: name,
152
- value: value,
153
- onChange: onChange
154
- }))));
155
- }
156
- //# sourceMappingURL=inline.native.js.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["@wordpress/format-library/src/text-color/inline.native.js"],"names":["useCallback","useMemo","applyFormat","removeFormat","getActiveFormat","useSetting","getColorClassName","getColorObjectByColorValue","BottomSheet","ColorSettings","textColor","settings","transparentValue","parseClassName","parseCSS","css","split","reduce","accumulator","rule","property","value","replace","color","backgroundColor","getActiveColors","name","colorSettings","activeColorFormat","attributes","style","class","setColors","colors","styles","classNames","push","join","colorObject","slug","length","format","type","start","end","text","charAt","ColorPicker","onChange","onColorChange","activeColors","InlineColorUI","onClose","paddingLeft","paddingRight"],"mappings":";;AAAA;AACA;AACA;AACA,SAASA,WAAT,EAAsBC,OAAtB,QAAqC,oBAArC;AACA,SACCC,WADD,EAECC,YAFD,EAGCC,eAHD,QAIO,sBAJP;AAKA,SACCC,UADD,EAECC,iBAFD,EAGCC,0BAHD,QAIO,yBAJP;AAKA,SAASC,WAAT,EAAsBC,aAAtB,QAA2C,uBAA3C;AAEA;AACA;AACA;;AACA,SAASC,SAAS,IAAIC,QAAtB,QAAsC,SAAtC;AACA,SAASC,gBAAT,QAAiC,YAAjC;AACA,SAASC,cAAT,QAA+B,aAA/B;;AAEA,SAASC,QAAT,GAA8B;AAAA,MAAXC,GAAW,uEAAL,EAAK;AAC7B,SAAOA,GAAG,CAACC,KAAJ,CAAW,GAAX,EAAiBC,MAAjB,CAAyB,CAAEC,WAAF,EAAeC,IAAf,KAAyB;AACxD,QAAKA,IAAL,EAAY;AACX,YAAM,CAAEC,QAAF,EAAYC,KAAZ,IAAsBF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R;AAEC,IAAA,KAAK,EAAGL,KAFT;AAGC,IAAA,QAAQ,EAAG2B;AAHZ,IADD,CADD,CARD,CADD;AAoBA","sourcesContent":["/**\n * WordPress dependencies\n */\nimport { useCallback, useMemo } from '@wordpress/element';\nimport {\n\tapplyFormat,\n\tremoveFormat,\n\tgetActiveFormat,\n} from '@wordpress/rich-text';\nimport {\n\tuseSetting,\n\tgetColorClassName,\n\tgetColorObjectByColorValue,\n} from '@wordpress/block-editor';\nimport { BottomSheet, ColorSettings } from '@wordpress/components';\n\n/**\n * Internal dependencies\n */\nimport { textColor as settings } from './index';\nimport { transparentValue } from './index.js';\nimport { parseClassName } from './inline.js';\n\nfunction parseCSS( css = '' ) {\n\treturn css.split( ';' ).reduce( ( accumulator, rule ) => {\n\t\tif ( rule ) {\n\t\t\tconst [ property, value ] = rule.replace( / /g, '' ).split( ':' );\n\t\t\tif ( property === 'color' ) accumulator.color = value;\n\t\t\tif ( property === 'background-color' && value !== transparentValue )\n\t\t\t\taccumulator.backgroundColor = value;\n\t\t}\n\t\treturn accumulator;\n\t}, {} );\n}\n\nfunction getActiveColors( value, name, colorSettings ) {\n\tconst activeColorFormat = getActiveFormat( value, name );\n\n\tif ( ! activeColorFormat ) {\n\t\treturn {};\n\t}\n\n\treturn {\n\t\t...parseCSS( activeColorFormat.attributes.style ),\n\t\t...parseClassName( activeColorFormat.attributes.class, colorSettings ),\n\t};\n}\n\nfunction setColors( value, name, colorSettings, colors ) {\n\tconst { color, backgroundColor } = {\n\t\t...getActiveColors( value, name, colorSettings ),\n\t\t...colors,\n\t};\n\n\tif ( ! color && ! backgroundColor ) {\n\t\treturn removeFormat( value, name );\n\t}\n\n\tconst styles = [];\n\tconst classNames = [];\n\tconst attributes = {};\n\n\tif ( backgroundColor ) {\n\t\tstyles.push( [ 'background-color', backgroundColor ].join( ':' ) );\n\t} else {\n\t\t// Override default browser color for mark element.\n\t\tstyles.push( [ 'background-color', transparentValue ].join( ':' ) );\n\t}\n\n\tif ( color ) {\n\t\tconst colorObject = getColorObjectByColorValue( colorSettings, color );\n\n\t\tif ( colorObject ) {\n\t\t\tclassNames.push( getColorClassName( 'color', colorObject.slug ) );\n\t\t\tstyles.push( [ 'color', colorObject.color ].join( ':' ) );\n\t\t} else {\n\t\t\tstyles.push( [ 'color', color ].join( ':' ) );\n\t\t}\n\t}\n\n\tif ( styles.length ) attributes.style = styles.join( ';' );\n\tif ( classNames.length ) attributes.class = classNames.join( ' ' );\n\n\tconst format = { type: name, attributes };\n\n\t// For cases when there is no text selected, formatting is forced\n\t// for the first empty character\n\tif (\n\t\tvalue?.start === value?.end &&\n\t\t( value?.text.length === 0 ||\n\t\t\tvalue.text?.charAt( value.end - 1 ) === ' ' )\n\t) {\n\t\treturn applyFormat( value, format, value?.start - 1, value?.end + 1 );\n\t}\n\n\treturn applyFormat( value, format );\n}\n\nfunction ColorPicker( { name, value, onChange } ) {\n\tconst property = 'color';\n\tconst colors = useSetting( 'color.palette' ) || settings.colors;\n\tconst colorSettings = {\n\t\tcolors,\n\t};\n\n\tconst onColorChange = useCallback(\n\t\t( color ) => {\n\t\t\tif ( color !== '' ) {\n\t\t\t\tonChange(\n\t\t\t\t\tsetColors( value, name, colors, { [ property ]: color } )\n\t\t\t\t);\n\t\t\t\t// Remove formatting if the color was reset, there's no\n\t\t\t\t// current selection and the previous character is a space\n\t\t\t} else if (\n\t\t\t\tvalue?.start === value?.end &&\n\t\t\t\tvalue.text?.charAt( value?.end - 1 ) === ' '\n\t\t\t) {\n\t\t\t\tonChange(\n\t\t\t\t\tremoveFormat( value, name, value.end - 1, value.end )\n\t\t\t\t);\n\t\t\t} else {\n\t\t\t\tonChange( removeFormat( value, name ) );\n\t\t\t}\n\t\t},\n\t\t[ colors, onChange, property ]\n\t);\n\tconst activeColors = useMemo(\n\t\t() => getActiveColors( value, name, colors ),\n\t\t[ name, value, colors ]\n\t);\n\n\treturn (\n\t\t<ColorSettings\n\t\t\tcolorValue={ activeColors[ property ] }\n\t\t\tonColorChange={ onColorChange }\n\t\t\tdefaultSettings={ colorSettings }\n\t\t\thideNavigation\n\t\t/>\n\t);\n}\n\nexport default function InlineColorUI( { name, value, onChange, onClose } ) {\n\treturn (\n\t\t<BottomSheet\n\t\t\tisVisible\n\t\t\tonClose={ onClose }\n\t\t\thideHeader\n\t\t\tcontentStyle={ { paddingLeft: 0, paddingRight: 0 } }\n\t\t\thasNavigation\n\t\t\tleftButton={ null }\n\t\t>\n\t\t\t<BottomSheet.NavigationContainer animate main>\n\t\t\t\t<BottomSheet.NavigationScreen name=\"text-color\">\n\t\t\t\t\t<ColorPicker\n\t\t\t\t\t\tname={ name }\n\t\t\t\t\t\tvalue={ value }\n\t\t\t\t\t\tonChange={ onChange }\n\t\t\t\t\t/>\n\t\t\t\t</BottomSheet.NavigationScreen>\n\t\t\t</BottomSheet.NavigationContainer>\n\t\t</BottomSheet>\n\t);\n}\n"]}
@@ -1,191 +0,0 @@
1
- /**
2
- * External dependencies
3
- */
4
- import { isEmpty } from 'lodash';
5
- import { View } from 'react-native';
6
-
7
- /**
8
- * WordPress dependencies
9
- */
10
- import { __ } from '@wordpress/i18n';
11
- import { useCallback, useMemo, useState } from '@wordpress/element';
12
- import { BlockControls, useSetting } from '@wordpress/block-editor';
13
- import { ToolbarGroup, ToolbarButton } from '@wordpress/components';
14
- import { Icon, textColor as textColorIcon } from '@wordpress/icons';
15
- import { removeFormat } from '@wordpress/rich-text';
16
- import { usePreferredColorSchemeStyle } from '@wordpress/compose';
17
-
18
- /**
19
- * Internal dependencies
20
- */
21
- import { getActiveColors } from './inline.js';
22
- import { transparentValue } from './index.js';
23
- import { default as InlineColorUI } from './inline';
24
- import styles from './style.scss';
25
-
26
- const name = 'core/text-color';
27
- const title = __( 'Text color' );
28
-
29
- const EMPTY_ARRAY = [];
30
-
31
- function getComputedStyleProperty( element, property ) {
32
- const {
33
- props: { style = {} },
34
- } = element;
35
-
36
- if ( property === 'background-color' ) {
37
- const { backgroundColor, baseColors } = style;
38
-
39
- if ( backgroundColor !== 'transparent' ) {
40
- return backgroundColor;
41
- } else if ( baseColors && baseColors?.color?.background ) {
42
- return baseColors?.color?.background;
43
- }
44
-
45
- return 'transparent';
46
- }
47
- }
48
-
49
- function fillComputedColors( element, { color, backgroundColor } ) {
50
- if ( ! color && ! backgroundColor ) {
51
- return;
52
- }
53
-
54
- return {
55
- color: color || getComputedStyleProperty( element, 'color' ),
56
- backgroundColor: getComputedStyleProperty(
57
- element,
58
- 'background-color'
59
- ),
60
- };
61
- }
62
-
63
- function TextColorEdit( {
64
- value,
65
- onChange,
66
- isActive,
67
- activeAttributes,
68
- contentRef,
69
- } ) {
70
- const allowCustomControl = useSetting( 'color.custom' );
71
- const colors = useSetting( 'color.palette' ) || EMPTY_ARRAY;
72
- const [ isAddingColor, setIsAddingColor ] = useState( false );
73
- const enableIsAddingColor = useCallback( () => setIsAddingColor( true ), [
74
- setIsAddingColor,
75
- ] );
76
- const disableIsAddingColor = useCallback( () => setIsAddingColor( false ), [
77
- setIsAddingColor,
78
- ] );
79
- const colorIndicatorStyle = useMemo(
80
- () =>
81
- fillComputedColors(
82
- contentRef,
83
- getActiveColors( value, name, colors )
84
- ),
85
- [ value, colors ]
86
- );
87
-
88
- const hasColorsToChoose = ! isEmpty( colors ) || ! allowCustomControl;
89
-
90
- const onPressButton = useCallback( () => {
91
- if ( hasColorsToChoose ) {
92
- enableIsAddingColor();
93
- } else {
94
- onChange( removeFormat( value, name ) );
95
- }
96
- }, [ hasColorsToChoose, value ] );
97
-
98
- const outlineStyle = usePreferredColorSchemeStyle(
99
- styles[ 'components-inline-color__outline' ],
100
- styles[ 'components-inline-color__outline--dark' ]
101
- );
102
-
103
- if ( ! hasColorsToChoose && ! isActive ) {
104
- return null;
105
- }
106
-
107
- const isActiveStyle = {
108
- ...colorIndicatorStyle,
109
- ...( ! colorIndicatorStyle?.backgroundColor
110
- ? { backgroundColor: 'transparent' }
111
- : {} ),
112
- ...styles[ 'components-inline-color--is-active' ],
113
- };
114
-
115
- const customContainerStyles =
116
- styles[ 'components-inline-color__button-container' ];
117
-
118
- return (
119
- <>
120
- <BlockControls>
121
- <ToolbarGroup>
122
- { isActive && (
123
- <View style={ outlineStyle } pointerEvents="none" />
124
- ) }
125
-
126
- <ToolbarButton
127
- name="text-color"
128
- isActive={ isActive }
129
- icon={
130
- <Icon
131
- icon={ textColorIcon }
132
- style={
133
- colorIndicatorStyle?.color && {
134
- color: colorIndicatorStyle.color,
135
- }
136
- }
137
- />
138
- }
139
- title={ title }
140
- extraProps={ {
141
- isActiveStyle,
142
- customContainerStyles,
143
- } }
144
- // If has no colors to choose but a color is active remove the color onClick
145
- onClick={ onPressButton }
146
- />
147
- </ToolbarGroup>
148
- </BlockControls>
149
- { isAddingColor && (
150
- <InlineColorUI
151
- name={ name }
152
- onClose={ disableIsAddingColor }
153
- activeAttributes={ activeAttributes }
154
- value={ value }
155
- onChange={ onChange }
156
- contentRef={ contentRef }
157
- />
158
- ) }
159
- </>
160
- );
161
- }
162
-
163
- export const textColor = {
164
- name,
165
- title,
166
- tagName: 'mark',
167
- className: 'has-inline-color',
168
- attributes: {
169
- style: 'style',
170
- class: 'class',
171
- },
172
- /*
173
- * Since this format relies on the <mark> tag, it's important to
174
- * prevent the default yellow background color applied by most
175
- * browsers. The solution is to detect when this format is used with a
176
- * text color but no background color, and in such cases to override
177
- * the default styling with a transparent background.
178
- *
179
- * @see https://github.com/WordPress/gutenberg/pull/35516
180
- */
181
- __unstableFilterAttributeValue( key, value ) {
182
- if ( key !== 'style' ) return value;
183
- // We should not add a background-color if it's already set
184
- if ( value && value.includes( 'background-color' ) ) return value;
185
- const addedCSS = [ 'background-color', transparentValue ].join( ':' );
186
- // Prepend `addedCSS` to avoid a double `;;` as any the existing CSS
187
- // rules will already include a `;`.
188
- return value ? [ addedCSS, value ].join( ';' ) : addedCSS;
189
- },
190
- edit: TextColorEdit,
191
- };