UNPKG

@gechiui/block-editor

Version:
142 lines (126 loc) 4.66 kB
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, __experimentalUnitControl as UnitControl } from '@gechiui/components'; /** * Internal dependencies */ import { __unstableUseBlockRef as useBlockRef } from '../components/block-list/use-block-props/use-block-refs'; import useSetting from '../components/use-setting'; import { SPACING_SUPPORT_KEY } from './dimensions'; import { cleanEmptyObject } from './utils'; /** * Determines if there is gap support. * * @param {string|Object} blockType Block name or Block Type object. * @return {boolean} Whether there is support. */ export function hasGapSupport(blockType) { const support = getBlockSupport(blockType, 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. */ export 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. */ export 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. */ export function useIsGapDisabled() { let { name: blockName } = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; const isDisabled = !useSetting('spacing.blockGap'); return !hasGapSupport(blockName) || isDisabled; } /** * Inspector control panel containing the gap related configuration * * @param {Object} props * * @return {GCElement} Gap edit element. */ export function GapEdit(props) { var _style$spacing; const { clientId, attributes: { style }, setAttributes } = props; const units = useCustomUnits({ availableUnits: useSetting('spacing.units') || ['%', 'px', 'em', 'rem', 'vw'] }); const ref = useBlockRef(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: 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 Platform.select({ web: createElement(Fragment, null, createElement(UnitControl, { label: __('块间​​距'), __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