@wordpress/block-library
Version:
Block library for the WordPress editor.
135 lines (134 loc) • 4.27 kB
JavaScript
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
import clsx from "clsx";
import {
InnerBlocks,
BlockControls,
BlockVerticalAlignmentToolbar,
InspectorControls,
useBlockProps,
useSettings,
useInnerBlocksProps,
store as blockEditorStore
} from "@wordpress/block-editor";
import {
__experimentalUseCustomUnits as useCustomUnits,
__experimentalUnitControl as UnitControl,
__experimentalToolsPanel as ToolsPanel,
__experimentalToolsPanelItem as ToolsPanelItem
} from "@wordpress/components";
import { useSelect, useDispatch } from "@wordpress/data";
import { sprintf, __ } from "@wordpress/i18n";
import { useToolsPanelDropdownMenuProps } from "../utils/hooks";
function ColumnInspectorControls({ width, setAttributes }) {
const [availableUnits] = useSettings("spacing.units");
const units = useCustomUnits({
availableUnits: availableUnits || ["%", "px", "em", "rem", "vw"]
});
const dropdownMenuProps = useToolsPanelDropdownMenuProps();
return /* @__PURE__ */ jsx(
ToolsPanel,
{
label: __("Settings"),
resetAll: () => {
setAttributes({ width: void 0 });
},
dropdownMenuProps,
children: /* @__PURE__ */ jsx(
ToolsPanelItem,
{
hasValue: () => width !== void 0,
label: __("Width"),
onDeselect: () => setAttributes({ width: void 0 }),
isShownByDefault: true,
children: /* @__PURE__ */ jsx(
UnitControl,
{
label: __("Width"),
__unstableInputWidth: "calc(50% - 8px)",
__next40pxDefaultSize: true,
value: width || "",
onChange: (nextWidth) => {
nextWidth = 0 > parseFloat(nextWidth) ? "0" : nextWidth;
setAttributes({ width: nextWidth });
},
units
}
)
}
)
}
);
}
function ColumnEdit({
attributes: { verticalAlignment, width, templateLock, allowedBlocks },
setAttributes,
clientId
}) {
const classes = clsx("block-core-columns", {
[`is-vertically-aligned-${verticalAlignment}`]: verticalAlignment
});
const { columnsIds, hasChildBlocks, rootClientId } = useSelect(
(select) => {
const { getBlockOrder, getBlockRootClientId } = select(blockEditorStore);
const rootId = getBlockRootClientId(clientId);
return {
hasChildBlocks: getBlockOrder(clientId).length > 0,
rootClientId: rootId,
columnsIds: getBlockOrder(rootId)
};
},
[clientId]
);
const { updateBlockAttributes } = useDispatch(blockEditorStore);
const updateAlignment = (value) => {
setAttributes({ verticalAlignment: value });
updateBlockAttributes(rootClientId, {
verticalAlignment: null
});
};
const widthWithUnit = Number.isFinite(width) ? width + "%" : width;
const blockProps = useBlockProps({
className: classes,
style: widthWithUnit ? { flexBasis: widthWithUnit } : void 0
});
const columnsCount = columnsIds.length;
const currentColumnPosition = columnsIds.indexOf(clientId) + 1;
const label = sprintf(
/* translators: 1: Block label (i.e. "Block: Column"), 2: Position of the selected block, 3: Total number of sibling blocks of the same type */
__("%1$s (%2$d of %3$d)"),
blockProps["aria-label"],
currentColumnPosition,
columnsCount
);
const innerBlocksProps = useInnerBlocksProps(
{ ...blockProps, "aria-label": label },
{
templateLock,
allowedBlocks,
renderAppender: hasChildBlocks ? void 0 : InnerBlocks.ButtonBlockAppender
}
);
return /* @__PURE__ */ jsxs(Fragment, { children: [
/* @__PURE__ */ jsx(BlockControls, { children: /* @__PURE__ */ jsx(
BlockVerticalAlignmentToolbar,
{
onChange: updateAlignment,
value: verticalAlignment,
controls: ["top", "center", "bottom", "stretch"]
}
) }),
/* @__PURE__ */ jsx(InspectorControls, { children: /* @__PURE__ */ jsx(
ColumnInspectorControls,
{
width,
setAttributes
}
) }),
/* @__PURE__ */ jsx("div", { ...innerBlocksProps })
] });
}
var edit_default = ColumnEdit;
export {
edit_default as default
};
//# sourceMappingURL=edit.js.map