@wordpress/format-library
Version:
Format library for the WordPress editor.
174 lines (171 loc) • 5.12 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = InlineColorUI;
exports.getActiveColors = getActiveColors;
exports.parseClassName = parseClassName;
var _element = require("@wordpress/element");
var _data = require("@wordpress/data");
var _richText = require("@wordpress/rich-text");
var _blockEditor = require("@wordpress/block-editor");
var _components = require("@wordpress/components");
var _i18n = require("@wordpress/i18n");
var _index = require("./index");
var _lockUnlock = require("../lock-unlock");
var _jsxRuntime = require("react/jsx-runtime");
/**
* WordPress dependencies
*/
/**
* Internal dependencies
*/
const {
Tabs
} = (0, _lockUnlock.unlock)(_components.privateApis);
const TABS = [{
name: 'color',
title: (0, _i18n.__)('Text')
}, {
name: 'backgroundColor',
title: (0, _i18n.__)('Background')
}];
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 !== _index.transparentValue) {
accumulator.backgroundColor = value;
}
}
return accumulator;
}, {});
}
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 = (0, _blockEditor.getColorObjectByAttributeValues)(colorSettings, colorSlug);
accumulator.color = colorObject.color;
}
return accumulator;
}, {});
}
function getActiveColors(value, name, colorSettings) {
const activeColorFormat = (0, _richText.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 (0, _richText.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', _index.transparentValue].join(':'));
}
if (color) {
const colorObject = (0, _blockEditor.getColorObjectByColorValue)(colorSettings, color);
if (colorObject) {
classNames.push((0, _blockEditor.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 (0, _richText.applyFormat)(value, {
type: name,
attributes
});
}
function ColorPicker({
name,
property,
value,
onChange
}) {
const colors = (0, _data.useSelect)(select => {
var _getSettings$colors;
const {
getSettings
} = select(_blockEditor.store);
return (_getSettings$colors = getSettings().colors) !== null && _getSettings$colors !== void 0 ? _getSettings$colors : [];
}, []);
const activeColors = (0, _element.useMemo)(() => getActiveColors(value, name, colors), [name, value, colors]);
return /*#__PURE__*/(0, _jsxRuntime.jsx)(_blockEditor.ColorPalette, {
value: activeColors[property],
onChange: color => {
onChange(setColors(value, name, colors, {
[property]: color
}));
}
// Prevent the text and color picker from overlapping.
,
__experimentalIsRenderedInSidebar: true
});
}
function InlineColorUI({
name,
value,
onChange,
onClose,
contentRef,
isActive
}) {
const popoverAnchor = (0, _richText.useAnchor)({
editableContentElement: contentRef.current,
settings: {
..._index.textColor,
isActive
}
});
return /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.Popover, {
onClose: onClose,
className: "format-library__inline-color-popover",
anchor: popoverAnchor,
children: /*#__PURE__*/(0, _jsxRuntime.jsxs)(Tabs, {
children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(Tabs.TabList, {
children: TABS.map(tab => /*#__PURE__*/(0, _jsxRuntime.jsx)(Tabs.Tab, {
tabId: tab.name,
children: tab.title
}, tab.name))
}), TABS.map(tab => /*#__PURE__*/(0, _jsxRuntime.jsx)(Tabs.TabPanel, {
tabId: tab.name,
focusable: false,
children: /*#__PURE__*/(0, _jsxRuntime.jsx)(ColorPicker, {
name: name,
property: tab.name,
value: value,
onChange: onChange
})
}, tab.name))]
})
});
}
//# sourceMappingURL=inline.js.map
;