@gechiui/block-editor
Version:
146 lines (133 loc) • 4.36 kB
JavaScript
import { createElement, Fragment } from "@gechiui/element";
/**
* GeChiUI dependencies
*/
import { __ } from '@gechiui/i18n';
import { Platform } from '@gechiui/element';
import { getBlockSupport } from '@gechiui/blocks';
import { __experimentalUseCustomUnits as useCustomUnits, __experimentalBoxControl as BoxControl } from '@gechiui/components';
/**
* Internal dependencies
*/
import useSetting from '../components/use-setting';
import { AXIAL_SIDES, SPACING_SUPPORT_KEY, useCustomSides, useIsDimensionsSupportValid } from './dimensions';
import { cleanEmptyObject } from './utils';
/**
* Determines if there is margin support.
*
* @param {string|Object} blockType Block name or Block Type object.
*
* @return {boolean} Whether there is support.
*/
export function hasMarginSupport(blockType) {
const support = getBlockSupport(blockType, SPACING_SUPPORT_KEY);
return !!(true === support || support !== null && support !== void 0 && support.margin);
}
/**
* Checks if there is a current value in the margin block support attributes.
*
* @param {Object} props Block props.
* @return {boolean} Whether or not the block has a margin value set.
*/
export function hasMarginValue(props) {
var _props$attributes$sty, _props$attributes$sty2;
return ((_props$attributes$sty = props.attributes.style) === null || _props$attributes$sty === void 0 ? void 0 : (_props$attributes$sty2 = _props$attributes$sty.spacing) === null || _props$attributes$sty2 === void 0 ? void 0 : _props$attributes$sty2.margin) !== undefined;
}
/**
* Resets the margin block support attributes. This can be used when disabling
* the margin support controls for a block via a `ToolsPanel`.
*
* @param {Object} props Block props.
* @param {Object} props.attributes Block's attributes.
* @param {Object} props.setAttributes Function to set block's attributes.
*/
export function resetMargin(_ref) {
let {
attributes = {},
setAttributes
} = _ref;
const {
style
} = attributes;
setAttributes({
style: cleanEmptyObject({ ...style,
spacing: { ...(style === null || style === void 0 ? void 0 : style.spacing),
margin: undefined
}
})
});
}
/**
* Custom hook that checks if margin settings have been disabled.
*
* @param {string} name The name of the block.
*
* @return {boolean} Whether margin setting is disabled.
*/
export function useIsMarginDisabled() {
let {
name: blockName
} = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
const isDisabled = !useSetting('spacing.margin');
const isInvalid = !useIsDimensionsSupportValid(blockName, 'margin');
return !hasMarginSupport(blockName) || isDisabled || isInvalid;
}
/**
* Inspector control panel containing the margin related configuration
*
* @param {Object} props Block props.
*
* @return {GCElement} Margin edit element.
*/
export function MarginEdit(props) {
var _style$spacing;
const {
name: blockName,
attributes: {
style
},
setAttributes
} = props;
const units = useCustomUnits({
availableUnits: useSetting('spacing.units') || ['%', 'px', 'em', 'rem', 'vw']
});
const sides = useCustomSides(blockName, 'margin');
const splitOnAxis = sides && sides.some(side => AXIAL_SIDES.includes(side));
if (useIsMarginDisabled(props)) {
return null;
}
const onChange = next => {
const newStyle = { ...style,
spacing: { ...(style === null || style === void 0 ? void 0 : style.spacing),
margin: next
}
};
setAttributes({
style: cleanEmptyObject(newStyle)
});
};
const onChangeShowVisualizer = next => {
const newStyle = { ...style,
visualizers: {
margin: next
}
};
setAttributes({
style: cleanEmptyObject(newStyle)
});
};
return Platform.select({
web: createElement(Fragment, null, createElement(BoxControl, {
values: style === null || style === void 0 ? void 0 : (_style$spacing = style.spacing) === null || _style$spacing === void 0 ? void 0 : _style$spacing.margin,
onChange: onChange,
onChangeShowVisualizer: onChangeShowVisualizer,
label: __('边距'),
sides: sides,
units: units,
allowReset: false,
splitOnAxis: splitOnAxis
})),
native: null
});
}
//# sourceMappingURL=margin.js.map