@gechiui/block-editor
Version:
187 lines (160 loc) • 5.82 kB
JavaScript
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.FONT_WEIGHT_SUPPORT_KEY = exports.FONT_STYLE_SUPPORT_KEY = void 0;
exports.FontAppearanceEdit = FontAppearanceEdit;
exports.hasFontAppearanceValue = hasFontAppearanceValue;
exports.resetFontAppearance = resetFontAppearance;
exports.useIsFontAppearanceDisabled = useIsFontAppearanceDisabled;
exports.useIsFontStyleDisabled = useIsFontStyleDisabled;
exports.useIsFontWeightDisabled = useIsFontWeightDisabled;
var _element = require("@gechiui/element");
var _blocks = require("@gechiui/blocks");
var _fontAppearanceControl = _interopRequireDefault(require("../components/font-appearance-control"));
var _useSetting = _interopRequireDefault(require("../components/use-setting"));
var _utils = require("./utils");
/**
* GeChiUI dependencies
*/
/**
* Internal dependencies
*/
/**
* Key within block settings' support array indicating support for font style.
*/
const FONT_STYLE_SUPPORT_KEY = 'typography.__experimentalFontStyle';
/**
* Key within block settings' support array indicating support for font weight.
*/
exports.FONT_STYLE_SUPPORT_KEY = FONT_STYLE_SUPPORT_KEY;
const FONT_WEIGHT_SUPPORT_KEY = 'typography.__experimentalFontWeight';
/**
* Inspector control panel containing the font appearance options.
*
* @param {Object} props Block properties.
*
* @return {GCElement} Font appearance edit element.
*/
exports.FONT_WEIGHT_SUPPORT_KEY = FONT_WEIGHT_SUPPORT_KEY;
function FontAppearanceEdit(props) {
var _style$typography, _style$typography2;
const {
attributes: {
style
},
setAttributes
} = props;
const hasFontStyles = !useIsFontStyleDisabled(props);
const hasFontWeights = !useIsFontWeightDisabled(props);
const onChange = newStyles => {
setAttributes({
style: (0, _utils.cleanEmptyObject)({ ...style,
typography: { ...(style === null || style === void 0 ? void 0 : style.typography),
fontStyle: newStyles.fontStyle,
fontWeight: newStyles.fontWeight
}
})
});
};
const fontStyle = style === null || style === void 0 ? void 0 : (_style$typography = style.typography) === null || _style$typography === void 0 ? void 0 : _style$typography.fontStyle;
const fontWeight = style === null || style === void 0 ? void 0 : (_style$typography2 = style.typography) === null || _style$typography2 === void 0 ? void 0 : _style$typography2.fontWeight;
return (0, _element.createElement)(_fontAppearanceControl.default, {
onChange: onChange,
hasFontStyles: hasFontStyles,
hasFontWeights: hasFontWeights,
value: {
fontStyle,
fontWeight
}
});
}
/**
* Checks if font style support has been disabled either by not opting in for
* support or by failing to provide preset styles.
*
* @param {Object} props Block properties.
* @param {string} props.name Name for the block type.
*
* @return {boolean} Whether font style support has been disabled.
*/
function useIsFontStyleDisabled() {
let {
name: blockName
} = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
const styleSupport = (0, _blocks.hasBlockSupport)(blockName, FONT_STYLE_SUPPORT_KEY);
const hasFontStyles = (0, _useSetting.default)('typography.fontStyle');
return !styleSupport || !hasFontStyles;
}
/**
* Checks if font weight support has been disabled either by not opting in for
* support or by failing to provide preset weights.
*
* @param {Object} props Block properties.
* @param {string} props.name Name for the block type.
*
* @return {boolean} Whether font weight support has been disabled.
*/
function useIsFontWeightDisabled() {
let {
name: blockName
} = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
const weightSupport = (0, _blocks.hasBlockSupport)(blockName, FONT_WEIGHT_SUPPORT_KEY);
const hasFontWeights = (0, _useSetting.default)('typography.fontWeight');
return !weightSupport || !hasFontWeights;
}
/**
* Checks if font appearance support has been disabled.
*
* @param {Object} props Block properties.
*
* @return {boolean} Whether font appearance support has been disabled.
*/
function useIsFontAppearanceDisabled(props) {
const stylesDisabled = useIsFontStyleDisabled(props);
const weightsDisabled = useIsFontWeightDisabled(props);
return stylesDisabled && weightsDisabled;
}
/**
* Checks if there is either a font style or weight value set within the
* typography styles.
*
* @param {Object} props Block props.
* @return {boolean} Whether or not the block has a font style or weight.
*/
function hasFontAppearanceValue(props) {
var _props$attributes$sty;
const {
fontStyle,
fontWeight
} = ((_props$attributes$sty = props.attributes.style) === null || _props$attributes$sty === void 0 ? void 0 : _props$attributes$sty.typography) || {};
return !!fontStyle || !!fontWeight;
}
/**
* Resets the font style and weight block support attributes. This can be used
* when disabling the font appearance support controls for a block via a
* progressive discovery panel.
*
* @param {Object} props Block props.
* @param {Object} props.attributes Block's attributes.
* @param {Object} props.setAttributes Function to set block's attributes.
*/
function resetFontAppearance(_ref) {
let {
attributes = {},
setAttributes
} = _ref;
const {
style
} = attributes;
setAttributes({
style: (0, _utils.cleanEmptyObject)({ ...style,
typography: { ...(style === null || style === void 0 ? void 0 : style.typography),
fontStyle: undefined,
fontWeight: undefined
}
})
});
}
//# sourceMappingURL=font-appearance.js.map