UNPKG

@wordpress/block-library

Version:
270 lines (269 loc) 8.45 kB
// packages/block-library/src/columns/edit.js import clsx from "clsx"; import { __ } from "@wordpress/i18n"; import { Notice, RangeControl, ToggleControl, __experimentalToolsPanel as ToolsPanel, __experimentalToolsPanelItem as ToolsPanelItem, __experimentalVStack as VStack } from "@wordpress/components"; import { InspectorControls, useInnerBlocksProps, BlockControls, BlockVerticalAlignmentToolbar, __experimentalBlockVariationPicker, useBlockProps, store as blockEditorStore } from "@wordpress/block-editor"; import { useDispatch, useSelect, useRegistry } from "@wordpress/data"; import { createBlock, createBlocksFromInnerBlocksTemplate, store as blocksStore } from "@wordpress/blocks"; import { hasExplicitPercentColumnWidths, getMappedColumnWidths, getRedistributedColumnWidths, toWidthPrecision } from "./utils"; import { useToolsPanelDropdownMenuProps } from "../utils/hooks"; import { Fragment, jsx, jsxs } from "react/jsx-runtime"; var DEFAULT_BLOCK = { name: "core/column" }; function ColumnInspectorControls({ clientId, setAttributes, isStackedOnMobile }) { const { count, canInsertColumnBlock, minCount } = useSelect( (select) => { const { canInsertBlockType, canRemoveBlock, getBlockOrder } = select(blockEditorStore); const blockOrder = getBlockOrder(clientId); const preventRemovalBlockIndexes = blockOrder.reduce( (acc, blockId, index) => { if (!canRemoveBlock(blockId)) { acc.push(index); } return acc; }, [] ); return { count: blockOrder.length, canInsertColumnBlock: canInsertBlockType( "core/column", clientId ), minCount: Math.max(...preventRemovalBlockIndexes) + 1 }; }, [clientId] ); const { getBlocks } = useSelect(blockEditorStore); const { replaceInnerBlocks } = useDispatch(blockEditorStore); function updateColumns(previousColumns, newColumns) { let innerBlocks = getBlocks(clientId); const hasExplicitWidths = hasExplicitPercentColumnWidths(innerBlocks); const isAddingColumn = newColumns > previousColumns; if (isAddingColumn && hasExplicitWidths) { const newColumnWidth = toWidthPrecision(100 / newColumns); const newlyAddedColumns = newColumns - previousColumns; const widths = getRedistributedColumnWidths( innerBlocks, 100 - newColumnWidth * newlyAddedColumns ); innerBlocks = [ ...getMappedColumnWidths(innerBlocks, widths), ...Array.from({ length: newlyAddedColumns }).map(() => { return createBlock("core/column", { width: `${newColumnWidth}%` }); }) ]; } else if (isAddingColumn) { innerBlocks = [ ...innerBlocks, ...Array.from({ length: newColumns - previousColumns }).map(() => { return createBlock("core/column"); }) ]; } else if (newColumns < previousColumns) { innerBlocks = innerBlocks.slice( 0, -(previousColumns - newColumns) ); if (hasExplicitWidths) { const widths = getRedistributedColumnWidths(innerBlocks, 100); innerBlocks = getMappedColumnWidths(innerBlocks, widths); } } replaceInnerBlocks(clientId, innerBlocks); } const dropdownMenuProps = useToolsPanelDropdownMenuProps(); return /* @__PURE__ */ jsxs( ToolsPanel, { label: __("Settings"), resetAll: () => { setAttributes({ isStackedOnMobile: true }); }, dropdownMenuProps, children: [ canInsertColumnBlock && /* @__PURE__ */ jsxs(VStack, { spacing: 4, style: { gridColumn: "1 / -1" }, children: [ /* @__PURE__ */ jsx( RangeControl, { __nextHasNoMarginBottom: true, __next40pxDefaultSize: true, label: __("Columns"), value: count, onChange: (value) => updateColumns(count, Math.max(minCount, value)), min: Math.max(1, minCount), max: Math.max(6, count) } ), count > 6 && /* @__PURE__ */ jsx(Notice, { status: "warning", isDismissible: false, children: __( "This column count exceeds the recommended amount and may cause visual breakage." ) }) ] }), /* @__PURE__ */ jsx( ToolsPanelItem, { label: __("Stack on mobile"), isShownByDefault: true, hasValue: () => isStackedOnMobile !== true, onDeselect: () => setAttributes({ isStackedOnMobile: true }), children: /* @__PURE__ */ jsx( ToggleControl, { __nextHasNoMarginBottom: true, label: __("Stack on mobile"), checked: isStackedOnMobile, onChange: () => setAttributes({ isStackedOnMobile: !isStackedOnMobile }) } ) } ) ] } ); } function ColumnsEditContainer({ attributes, setAttributes, clientId }) { const { isStackedOnMobile, verticalAlignment, templateLock } = attributes; const registry = useRegistry(); const { getBlockOrder } = useSelect(blockEditorStore); const { updateBlockAttributes } = useDispatch(blockEditorStore); const classes = clsx({ [`are-vertically-aligned-${verticalAlignment}`]: verticalAlignment, [`is-not-stacked-on-mobile`]: !isStackedOnMobile }); const blockProps = useBlockProps({ className: classes }); const innerBlocksProps = useInnerBlocksProps(blockProps, { defaultBlock: DEFAULT_BLOCK, directInsert: true, orientation: "horizontal", renderAppender: false, templateLock }); function updateAlignment(newVerticalAlignment) { const innerBlockClientIds = getBlockOrder(clientId); registry.batch(() => { setAttributes({ verticalAlignment: newVerticalAlignment }); updateBlockAttributes(innerBlockClientIds, { verticalAlignment: newVerticalAlignment }); }); } return /* @__PURE__ */ jsxs(Fragment, { children: [ /* @__PURE__ */ jsx(BlockControls, { children: /* @__PURE__ */ jsx( BlockVerticalAlignmentToolbar, { onChange: updateAlignment, value: verticalAlignment } ) }), /* @__PURE__ */ jsx(InspectorControls, { children: /* @__PURE__ */ jsx( ColumnInspectorControls, { clientId, setAttributes, isStackedOnMobile } ) }), /* @__PURE__ */ jsx("div", { ...innerBlocksProps }) ] }); } function Placeholder({ clientId, name, setAttributes }) { const { blockType, defaultVariation, variations } = useSelect( (select) => { const { getBlockVariations, getBlockType, getDefaultBlockVariation } = select(blocksStore); return { blockType: getBlockType(name), defaultVariation: getDefaultBlockVariation(name, "block"), variations: getBlockVariations(name, "block") }; }, [name] ); const { replaceInnerBlocks } = useDispatch(blockEditorStore); const blockProps = useBlockProps(); return /* @__PURE__ */ jsx("div", { ...blockProps, children: /* @__PURE__ */ jsx( __experimentalBlockVariationPicker, { icon: blockType?.icon?.src, label: blockType?.title, variations, instructions: __("Divide into columns. Select a layout:"), onSelect: (nextVariation = defaultVariation) => { if (nextVariation.attributes) { setAttributes(nextVariation.attributes); } if (nextVariation.innerBlocks) { replaceInnerBlocks( clientId, createBlocksFromInnerBlocksTemplate( nextVariation.innerBlocks ), true ); } }, allowSkip: true } ) }); } var ColumnsEdit = (props) => { const { clientId } = props; const hasInnerBlocks = useSelect( (select) => select(blockEditorStore).getBlocks(clientId).length > 0, [clientId] ); const Component = hasInnerBlocks ? ColumnsEditContainer : Placeholder; return /* @__PURE__ */ jsx(Component, { ...props }); }; var edit_default = ColumnsEdit; export { edit_default as default }; //# sourceMappingURL=edit.js.map