@wordpress/block-library
Version:
Block library for the WordPress editor.
161 lines (160 loc) • 4.9 kB
JavaScript
// packages/block-library/src/columns/edit.jsx
import clsx from "clsx";
import { __ } from "@wordpress/i18n";
import {
ToggleControl,
__experimentalToolsPanel as ToolsPanel,
__experimentalToolsPanelItem as ToolsPanelItem
} 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 {
createBlocksFromInnerBlocksTemplate,
store as blocksStore
} from "@wordpress/blocks";
import { useToolsPanelDropdownMenuProps } from "../utils/hooks.mjs";
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
var DEFAULT_BLOCK = {
name: "core/column"
};
function ColumnInspectorControls({ setAttributes, isStackedOnMobile }) {
const dropdownMenuProps = useToolsPanelDropdownMenuProps();
return /* @__PURE__ */ jsx(
ToolsPanel,
{
label: __("Settings"),
resetAll: () => {
setAttributes({
isStackedOnMobile: true
});
},
dropdownMenuProps,
children: /* @__PURE__ */ jsx(
ToolsPanelItem,
{
label: __("Stack on mobile"),
isShownByDefault: true,
hasValue: () => isStackedOnMobile !== true,
onDeselect: () => setAttributes({
isStackedOnMobile: true
}),
children: /* @__PURE__ */ jsx(
ToggleControl,
{
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,
{
setAttributes,
isStackedOnMobile
}
) }),
/* @__PURE__ */ jsx("div", { ...innerBlocksProps })
] });
}
function Placeholder({ clientId, name, setAttributes }) {
const { blockType, variations } = useSelect(
(select) => {
const { getBlockVariations, getBlockType } = select(blocksStore);
return {
blockType: getBlockType(name),
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) => {
if (nextVariation.attributes) {
setAttributes(nextVariation.attributes);
}
if (nextVariation.innerBlocks) {
replaceInnerBlocks(
clientId,
createBlocksFromInnerBlocksTemplate(
nextVariation.innerBlocks
),
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.mjs.map