@wordpress/block-editor
Version:
92 lines (78 loc) • 2.4 kB
JavaScript
import { createElement } from "@wordpress/element";
/**
* External dependencies
*/
import { find } from 'lodash';
/**
* WordPress dependencies
*/
import { hasBlockSupport } from '@wordpress/blocks';
/**
* Internal dependencies
*/
import { cleanEmptyObject } from './utils';
import useEditorFeature from '../components/use-editor-feature';
import FontFamilyControl from '../components/font-family';
export const FONT_FAMILY_SUPPORT_KEY = '__experimentalFontFamily';
const getFontFamilyFromAttributeValue = (fontFamilies, value) => {
const attributeParsed = /var:preset\|font-family\|(.+)/.exec(value);
if (attributeParsed && attributeParsed[1]) {
const fontFamilyObject = find(fontFamilies, ({
slug
}) => {
return slug === attributeParsed[1];
});
if (fontFamilyObject) {
return fontFamilyObject.fontFamily;
}
}
return value;
};
export function FontFamilyEdit({
name,
setAttributes,
attributes: {
style = {}
}
}) {
var _style$typography;
const fontFamilies = useEditorFeature('typography.fontFamilies');
const isDisable = useIsFontFamilyDisabled({
name
});
if (isDisable) {
return null;
}
const value = getFontFamilyFromAttributeValue(fontFamilies, (_style$typography = style.typography) === null || _style$typography === void 0 ? void 0 : _style$typography.fontFamily);
function onChange(newValue) {
const predefinedFontFamily = find(fontFamilies, ({
fontFamily
}) => fontFamily === newValue);
setAttributes({
style: cleanEmptyObject({ ...style,
typography: { ...(style.typography || {}),
fontFamily: predefinedFontFamily ? `var:preset|font-family|${predefinedFontFamily.slug}` : newValue || undefined
}
})
});
}
return createElement(FontFamilyControl, {
className: "block-editor-hooks-font-family-control",
fontFamilies: fontFamilies,
value: value,
onChange: onChange
});
}
/**
* Custom hook that checks if font-family functionality is disabled.
*
* @param {string} name The name of the block.
* @return {boolean} Whether setting is disabled.
*/
export function useIsFontFamilyDisabled({
name
}) {
const fontFamilies = useEditorFeature('typography.fontFamilies');
return !fontFamilies || fontFamilies.length === 0 || !hasBlockSupport(name, FONT_FAMILY_SUPPORT_KEY);
}
//# sourceMappingURL=font-family.js.map