@wordpress/format-library
Version:
Format library for the WordPress editor.
103 lines (100 loc) • 4.09 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.transparentValue = exports.textColor = void 0;
var _i18n = require("@wordpress/i18n");
var _element = require("@wordpress/element");
var _blockEditor = require("@wordpress/block-editor");
var _icons = require("@wordpress/icons");
var _richText = require("@wordpress/rich-text");
var _inline = _interopRequireWildcard(require("./inline"));
var _jsxRuntime = require("react/jsx-runtime");
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
/**
* WordPress dependencies
*/
/**
* Internal dependencies
*/
const transparentValue = exports.transparentValue = 'rgba(0, 0, 0, 0)';
const name = 'core/text-color';
const title = (0, _i18n.__)('Highlight');
const EMPTY_ARRAY = [];
function getComputedStyleProperty(element, property) {
const {
ownerDocument
} = element;
const {
defaultView
} = ownerDocument;
const style = defaultView.getComputedStyle(element);
const value = style.getPropertyValue(property);
if (property === 'background-color' && value === transparentValue && element.parentElement) {
return getComputedStyleProperty(element.parentElement, property);
}
return value;
}
function fillComputedColors(element, {
color,
backgroundColor
}) {
if (!color && !backgroundColor) {
return;
}
return {
color: color || getComputedStyleProperty(element, 'color'),
backgroundColor: backgroundColor === transparentValue ? getComputedStyleProperty(element, 'background-color') : backgroundColor
};
}
function TextColorEdit({
value,
onChange,
isActive,
activeAttributes,
contentRef
}) {
const [allowCustomControl, colors = EMPTY_ARRAY] = (0, _blockEditor.useSettings)('color.custom', 'color.palette');
const [isAddingColor, setIsAddingColor] = (0, _element.useState)(false);
const colorIndicatorStyle = (0, _element.useMemo)(() => fillComputedColors(contentRef.current, (0, _inline.getActiveColors)(value, name, colors)), [contentRef, value, colors]);
const hasColorsToChoose = !!colors.length || allowCustomControl;
if (!hasColorsToChoose && !isActive) {
return null;
}
return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_blockEditor.RichTextToolbarButton, {
className: "format-library-text-color-button",
isActive: isActive,
icon: /*#__PURE__*/(0, _jsxRuntime.jsx)(_icons.Icon, {
icon: Object.keys(activeAttributes).length ? _icons.textColor : _icons.color,
style: colorIndicatorStyle
}),
title: title
// If has no colors to choose but a color is active remove the color onClick.
,
onClick: hasColorsToChoose ? () => setIsAddingColor(true) : () => onChange((0, _richText.removeFormat)(value, name)),
role: "menuitemcheckbox"
}), isAddingColor && /*#__PURE__*/(0, _jsxRuntime.jsx)(_inline.default, {
name: name,
onClose: () => setIsAddingColor(false),
activeAttributes: activeAttributes,
value: value,
onChange: onChange,
contentRef: contentRef,
isActive: isActive
})]
});
}
const textColor = exports.textColor = {
name,
title,
tagName: 'mark',
className: 'has-inline-color',
attributes: {
style: 'style',
class: 'class'
},
edit: TextColorEdit
};
//# sourceMappingURL=index.js.map
;