@wordpress/block-library
Version:
Block library for the WordPress editor.
151 lines (150 loc) • 7.4 kB
JavaScript
;
Object.defineProperties(exports, {
__esModule: { value: true },
[Symbol.toStringTag]: { value: "Module" }
});
// \0rolldown/runtime.js
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
key = keys[i];
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
get: ((k) => from[k]).bind(null, key),
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
});
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
value: mod,
enumerable: true
}) : target, mod));
let clsx = require("clsx");
clsx = __toESM(clsx);
let _wordpress_i18n = require("@wordpress/i18n");
let _wordpress_components = require("@wordpress/components");
let _wordpress_block_editor = require("@wordpress/block-editor");
let _wordpress_data = require("@wordpress/data");
let _wordpress_blocks = require("@wordpress/blocks");
let ___utils_hooks_js = require("../utils/hooks.cjs");
let react_jsx_runtime = require("react/jsx-runtime");
// packages/block-library/src/columns/edit.jsx
const DEFAULT_BLOCK = { name: "core/column" };
function ColumnInspectorControls({ setAttributes, isStackedOnMobile }) {
const dropdownMenuProps = (0, ___utils_hooks_js.useToolsPanelDropdownMenuProps)();
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.__experimentalToolsPanel, {
label: (0, _wordpress_i18n.__)("Settings"),
resetAll: () => {
setAttributes({ isStackedOnMobile: true });
},
dropdownMenuProps,
children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.__experimentalToolsPanelItem, {
label: (0, _wordpress_i18n.__)("Stack on mobile"),
isShownByDefault: true,
hasValue: () => isStackedOnMobile !== true,
onDeselect: () => setAttributes({ isStackedOnMobile: true }),
children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.ToggleControl, {
label: (0, _wordpress_i18n.__)("Stack on mobile"),
checked: isStackedOnMobile,
onChange: () => setAttributes({ isStackedOnMobile: !isStackedOnMobile })
})
})
});
}
function ColumnsEditContainer({ attributes, setAttributes, clientId }) {
const { isStackedOnMobile, verticalAlignment, templateLock } = attributes;
const registry = (0, _wordpress_data.useRegistry)();
const { getBlockOrder } = (0, _wordpress_data.useSelect)(_wordpress_block_editor.store);
const { updateBlockAttributes } = (0, _wordpress_data.useDispatch)(_wordpress_block_editor.store);
const classes = (0, clsx.default)({
[`are-vertically-aligned-${verticalAlignment}`]: verticalAlignment,
[`is-not-stacked-on-mobile`]: !isStackedOnMobile
});
const blockProps = (0, _wordpress_block_editor.useBlockProps)({ className: classes });
const innerBlocksProps = (0, _wordpress_block_editor.useInnerBlocksProps)(blockProps, {
defaultBlock: DEFAULT_BLOCK,
directInsert: true,
orientation: "horizontal",
renderAppender: false,
templateLock
});
/**
* Update all child Column blocks with a new vertical alignment setting
* based on whatever alignment is passed in. This allows change to parent
* to override anything set on a individual column basis.
*
* @param {string} newVerticalAlignment The vertical alignment setting.
*/
function updateAlignment(newVerticalAlignment) {
const innerBlockClientIds = getBlockOrder(clientId);
registry.batch(() => {
setAttributes({ verticalAlignment: newVerticalAlignment });
updateBlockAttributes(innerBlockClientIds, { verticalAlignment: newVerticalAlignment });
});
}
return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(react_jsx_runtime.Fragment, { children: [
/* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_block_editor.BlockControls, { children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_block_editor.BlockVerticalAlignmentToolbar, {
onChange: updateAlignment,
value: verticalAlignment
}) }),
/* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_block_editor.InspectorControls, { children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(ColumnInspectorControls, {
setAttributes,
isStackedOnMobile
}) }),
/* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", { ...innerBlocksProps })
] });
}
function Placeholder({ clientId, name, setAttributes }) {
const { blockType, variations } = (0, _wordpress_data.useSelect)((select) => {
const { getBlockVariations, getBlockType } = select(_wordpress_blocks.store);
return {
blockType: getBlockType(name),
variations: getBlockVariations(name, "block")
};
}, [name]);
const { replaceInnerBlocks } = (0, _wordpress_data.useDispatch)(_wordpress_block_editor.store);
const blockProps = (0, _wordpress_block_editor.useBlockProps)();
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
...blockProps,
children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_block_editor.__experimentalBlockVariationPicker, {
icon: blockType?.icon?.src,
label: blockType?.title,
variations,
instructions: (0, _wordpress_i18n.__)("Divide into columns. Select a layout:"),
onSelect: (nextVariation) => {
if (nextVariation.attributes) setAttributes(nextVariation.attributes);
if (nextVariation.innerBlocks) replaceInnerBlocks(clientId, (0, _wordpress_blocks.createBlocksFromInnerBlocksTemplate)(nextVariation.innerBlocks), true);
}
})
});
}
/**
* Renders the `core/columns` block in the editor.
*
* Until the block has inner blocks it renders a variation picker; afterwards it
* renders the columns container. Every prop is forwarded to whichever of the two
* is rendered.
*
* @param {Object} props Component props.
* @param {string} props.clientId Client ID of the block.
* @param {string} props.name Block name, used to look up the variations offered by the picker.
* @param {Object} props.attributes Block attributes.
* @param {string} [props.attributes.verticalAlignment] Vertical alignment applied to the block and to every child Column block.
* @param {boolean} [props.attributes.isStackedOnMobile] Whether the columns stack on small viewports. Defaults to `true`.
* @param {string|boolean} [props.attributes.templateLock] Template lock applied to the inner blocks, one of `all`, `insert`, `contentOnly` or `false`.
* @param {Function} props.setAttributes Callback for updating block attributes.
*
* @return {React.JSX.Element} React element.
*/
const ColumnsEdit = (props) => {
const { clientId } = props;
const Component = (0, _wordpress_data.useSelect)((select) => select(_wordpress_block_editor.store).getBlocks(clientId).length > 0, [clientId]) ? ColumnsEditContainer : Placeholder;
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(Component, { ...props });
};
exports.default = ColumnsEdit;
//# sourceMappingURL=edit.cjs.map