@gechiui/block-editor
Version:
165 lines (135 loc) • 5.03 kB
JavaScript
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.GapEdit = GapEdit;
exports.hasGapSupport = hasGapSupport;
exports.hasGapValue = hasGapValue;
exports.resetGap = resetGap;
exports.useIsGapDisabled = useIsGapDisabled;
var _element = require("@gechiui/element");
var _i18n = require("@gechiui/i18n");
var _blocks = require("@gechiui/blocks");
var _components = require("@gechiui/components");
var _useBlockRefs = require("../components/block-list/use-block-props/use-block-refs");
var _useSetting = _interopRequireDefault(require("../components/use-setting"));
var _dimensions = require("./dimensions");
var _utils = require("./utils");
/**
* GeChiUI dependencies
*/
/**
* Internal dependencies
*/
/**
* Determines if there is gap support.
*
* @param {string|Object} blockType Block name or Block Type object.
* @return {boolean} Whether there is support.
*/
function hasGapSupport(blockType) {
const support = (0, _blocks.getBlockSupport)(blockType, _dimensions.SPACING_SUPPORT_KEY);
return !!(true === support || support !== null && support !== void 0 && support.blockGap);
}
/**
* Checks if there is a current value in the gap block support attributes.
*
* @param {Object} props Block props.
* @return {boolean} Whether or not the block has a gap value set.
*/
function hasGapValue(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.blockGap) !== undefined;
}
/**
* Resets the gap block support attribute. This can be used when disabling
* the gap 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 resetGap(_ref) {
let {
attributes = {},
setAttributes
} = _ref;
const {
style
} = attributes;
setAttributes({
style: { ...style,
spacing: { ...(style === null || style === void 0 ? void 0 : style.spacing),
blockGap: undefined
}
}
});
}
/**
* Custom hook that checks if gap settings have been disabled.
*
* @param {string} name The name of the block.
* @return {boolean} Whether the gap setting is disabled.
*/
function useIsGapDisabled() {
let {
name: blockName
} = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
const isDisabled = !(0, _useSetting.default)('spacing.blockGap');
return !hasGapSupport(blockName) || isDisabled;
}
/**
* Inspector control panel containing the gap related configuration
*
* @param {Object} props
*
* @return {GCElement} Gap edit element.
*/
function GapEdit(props) {
var _style$spacing;
const {
clientId,
attributes: {
style
},
setAttributes
} = props;
const units = (0, _components.__experimentalUseCustomUnits)({
availableUnits: (0, _useSetting.default)('spacing.units') || ['%', 'px', 'em', 'rem', 'vw']
});
const ref = (0, _useBlockRefs.__unstableUseBlockRef)(clientId);
if (useIsGapDisabled(props)) {
return null;
}
const onChange = next => {
var _window;
const newStyle = { ...style,
spacing: { ...(style === null || style === void 0 ? void 0 : style.spacing),
blockGap: next
}
};
setAttributes({
style: (0, _utils.cleanEmptyObject)(newStyle)
}); // In Safari, changing the `gap` CSS value on its own will not trigger the layout
// to be recalculated / re-rendered. To force the updated gap to re-render, here
// we replace the block's node with itself.
const isSafari = ((_window = window) === null || _window === void 0 ? void 0 : _window.navigator.userAgent) && window.navigator.userAgent.includes('Safari') && !window.navigator.userAgent.includes('Chrome ') && !window.navigator.userAgent.includes('Chromium ');
if (ref.current && isSafari) {
var _ref$current$parentNo;
(_ref$current$parentNo = ref.current.parentNode) === null || _ref$current$parentNo === void 0 ? void 0 : _ref$current$parentNo.replaceChild(ref.current, ref.current);
}
};
return _element.Platform.select({
web: (0, _element.createElement)(_element.Fragment, null, (0, _element.createElement)(_components.__experimentalUnitControl, {
label: (0, _i18n.__)('块间距'),
__unstableInputWidth: "80px",
min: 0,
onChange: onChange,
units: units,
value: style === null || style === void 0 ? void 0 : (_style$spacing = style.spacing) === null || _style$spacing === void 0 ? void 0 : _style$spacing.blockGap
})),
native: null
});
}
//# sourceMappingURL=gap.js.map