@wordpress/block-editor
Version:
377 lines (375 loc) • 14.1 kB
JavaScript
;
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 __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// packages/block-editor/src/hooks/layout.js
var layout_exports = {};
__export(layout_exports, {
addAttribute: () => addAttribute,
default: () => layout_default,
useLayoutClasses: () => useLayoutClasses,
useLayoutStyles: () => useLayoutStyles,
withLayoutStyles: () => withLayoutStyles
});
module.exports = __toCommonJS(layout_exports);
var import_clsx = __toESM(require("clsx"));
var import_compose = require("@wordpress/compose");
var import_hooks = require("@wordpress/hooks");
var import_blocks = require("@wordpress/blocks");
var import_data = require("@wordpress/data");
var import_components = require("@wordpress/components");
var import_i18n = require("@wordpress/i18n");
var import_store = require("../store");
var import_components2 = require("../components");
var import_use_settings = require("../components/use-settings");
var import_layouts = require("../layouts");
var import_block_editing_mode = require("../components/block-editing-mode");
var import_definitions = require("../layouts/definitions");
var import_utils = require("./utils");
var import_lock_unlock = require("../lock-unlock");
var import_jsx_runtime = require("react/jsx-runtime");
var layoutBlockSupportKey = "layout";
var { kebabCase } = (0, import_lock_unlock.unlock)(import_components.privateApis);
function hasLayoutBlockSupport(blockName) {
return (0, import_blocks.hasBlockSupport)(blockName, "layout") || (0, import_blocks.hasBlockSupport)(blockName, "__experimentalLayout");
}
function useLayoutClasses(blockAttributes = {}, blockName = "") {
const { layout } = blockAttributes;
const { default: defaultBlockLayout } = (0, import_blocks.getBlockSupport)(blockName, layoutBlockSupportKey) || {};
const usedLayout = layout?.inherit || layout?.contentSize || layout?.wideSize ? { ...layout, type: "constrained" } : layout || defaultBlockLayout || {};
const layoutClassnames = [];
if (import_definitions.LAYOUT_DEFINITIONS[usedLayout?.type || "default"]?.className) {
const baseClassName = import_definitions.LAYOUT_DEFINITIONS[usedLayout?.type || "default"]?.className;
const splitBlockName = blockName.split("/");
const fullBlockName = splitBlockName[0] === "core" ? splitBlockName.pop() : splitBlockName.join("-");
const compoundClassName = `wp-block-${fullBlockName}-${baseClassName}`;
layoutClassnames.push(baseClassName, compoundClassName);
}
const hasGlobalPadding = (0, import_data.useSelect)(
(select) => {
if (!usedLayout?.inherit && !usedLayout?.contentSize && usedLayout?.type !== "constrained") {
return false;
}
return select(import_store.store).getSettings().__experimentalFeatures?.useRootPaddingAwareAlignments;
},
[usedLayout?.contentSize, usedLayout?.inherit, usedLayout?.type]
);
if (hasGlobalPadding) {
layoutClassnames.push("has-global-padding");
}
if (usedLayout?.orientation) {
layoutClassnames.push(`is-${kebabCase(usedLayout.orientation)}`);
}
if (usedLayout?.justifyContent) {
layoutClassnames.push(
`is-content-justification-${kebabCase(
usedLayout.justifyContent
)}`
);
}
if (usedLayout?.flexWrap && usedLayout.flexWrap === "nowrap") {
layoutClassnames.push("is-nowrap");
}
return layoutClassnames;
}
function useLayoutStyles(blockAttributes = {}, blockName, selector) {
const { layout = {}, style = {} } = blockAttributes;
const usedLayout = layout?.inherit || layout?.contentSize || layout?.wideSize ? { ...layout, type: "constrained" } : layout || {};
const fullLayoutType = (0, import_layouts.getLayoutType)(usedLayout?.type || "default");
const [blockGapSupport] = (0, import_use_settings.useSettings)("spacing.blockGap");
const hasBlockGapSupport = blockGapSupport !== null;
return fullLayoutType?.getLayoutStyle?.({
blockName,
selector,
layout,
style,
hasBlockGapSupport
});
}
function LayoutPanelPure({
layout,
setAttributes,
name: blockName,
clientId
}) {
const settings = (0, import_utils.useBlockSettings)(blockName);
const { layout: layoutSettings } = settings;
const { themeSupportsLayout } = (0, import_data.useSelect)((select) => {
const { getSettings } = select(import_store.store);
return {
themeSupportsLayout: getSettings().supportsLayout
};
}, []);
const blockEditingMode = (0, import_block_editing_mode.useBlockEditingMode)();
if (blockEditingMode !== "default") {
return null;
}
const layoutBlockSupport = (0, import_blocks.getBlockSupport)(
blockName,
layoutBlockSupportKey,
{}
);
const blockSupportAndThemeSettings = {
...layoutSettings,
...layoutBlockSupport
};
const {
allowSwitching,
allowEditing = true,
allowInheriting = true,
default: defaultBlockLayout
} = blockSupportAndThemeSettings;
if (!allowEditing) {
return null;
}
const blockSupportAndLayout = {
...layoutBlockSupport,
...layout
};
const { type, default: { type: defaultType = "default" } = {} } = blockSupportAndLayout;
const blockLayoutType = type || defaultType;
const showInheritToggle = !!(allowInheriting && (!blockLayoutType || blockLayoutType === "default" || blockLayoutType === "constrained" || blockSupportAndLayout.inherit));
const usedLayout = layout || defaultBlockLayout || {};
const { inherit = false, contentSize = null } = usedLayout;
if ((blockLayoutType === "default" || blockLayoutType === "constrained") && !themeSupportsLayout) {
return null;
}
const layoutType = (0, import_layouts.getLayoutType)(blockLayoutType);
const constrainedType = (0, import_layouts.getLayoutType)("constrained");
const displayControlsForLegacyLayouts = !usedLayout.type && (contentSize || inherit);
const hasContentSizeOrLegacySettings = !!inherit || !!contentSize;
const onChangeType = (newType) => setAttributes({ layout: { type: newType } });
const onChangeLayout = (newLayout) => setAttributes({ layout: newLayout });
return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, { children: [
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_components2.InspectorControls, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_components.PanelBody, { title: (0, import_i18n.__)("Layout"), children: [
showInheritToggle && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_jsx_runtime.Fragment, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
import_components.ToggleControl,
{
__nextHasNoMarginBottom: true,
label: (0, import_i18n.__)("Inner blocks use content width"),
checked: layoutType?.name === "constrained" || hasContentSizeOrLegacySettings,
onChange: () => setAttributes({
layout: {
type: layoutType?.name === "constrained" || hasContentSizeOrLegacySettings ? "default" : "constrained"
}
}),
help: layoutType?.name === "constrained" || hasContentSizeOrLegacySettings ? (0, import_i18n.__)(
"Nested blocks use content width with options for full and wide widths."
) : (0, import_i18n.__)(
"Nested blocks will fill the width of this container."
)
}
) }),
!inherit && allowSwitching && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
LayoutTypeSwitcher,
{
type: blockLayoutType,
onChange: onChangeType
}
),
layoutType && layoutType.name !== "default" && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
layoutType.inspectorControls,
{
layout: usedLayout,
onChange: onChangeLayout,
layoutBlockSupport: blockSupportAndThemeSettings,
name: blockName,
clientId
}
),
constrainedType && displayControlsForLegacyLayouts && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
constrainedType.inspectorControls,
{
layout: usedLayout,
onChange: onChangeLayout,
layoutBlockSupport: blockSupportAndThemeSettings,
name: blockName,
clientId
}
)
] }) }),
!inherit && layoutType && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
layoutType.toolBarControls,
{
layout: usedLayout,
onChange: onChangeLayout,
layoutBlockSupport,
name: blockName,
clientId
}
)
] });
}
var layout_default = {
shareWithChildBlocks: true,
edit: LayoutPanelPure,
attributeKeys: ["layout"],
hasSupport(name) {
return hasLayoutBlockSupport(name);
}
};
function LayoutTypeSwitcher({ type, onChange }) {
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
import_components.__experimentalToggleGroupControl,
{
__next40pxDefaultSize: true,
isBlock: true,
label: (0, import_i18n.__)("Layout type"),
__nextHasNoMarginBottom: true,
hideLabelFromVision: true,
isAdaptiveWidth: true,
value: type,
onChange,
children: (0, import_layouts.getLayoutTypes)().map(({ name, label }) => {
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
import_components.__experimentalToggleGroupControlOption,
{
value: name,
label
},
name
);
})
}
);
}
function addAttribute(settings) {
if ("type" in (settings.attributes?.layout ?? {})) {
return settings;
}
if (hasLayoutBlockSupport(settings)) {
settings.attributes = {
...settings.attributes,
layout: {
type: "object"
}
};
}
return settings;
}
function BlockWithLayoutStyles({
block: BlockListBlock,
props,
blockGapSupport,
layoutClasses
}) {
const { name, attributes } = props;
const id = (0, import_compose.useInstanceId)(BlockListBlock);
const { layout } = attributes;
const { default: defaultBlockLayout } = (0, import_blocks.getBlockSupport)(name, layoutBlockSupportKey) || {};
const usedLayout = layout?.inherit || layout?.contentSize || layout?.wideSize ? { ...layout, type: "constrained" } : layout || defaultBlockLayout || {};
const selectorPrefix = `wp-container-${kebabCase(name)}-is-layout-`;
const selector = `.${selectorPrefix}${id}`;
const hasBlockGapSupport = blockGapSupport !== null;
const fullLayoutType = (0, import_layouts.getLayoutType)(usedLayout?.type || "default");
const css = fullLayoutType?.getLayoutStyle?.({
blockName: name,
selector,
layout: usedLayout,
style: attributes?.style,
hasBlockGapSupport
});
const layoutClassNames = (0, import_clsx.default)(
{
[`${selectorPrefix}${id}`]: !!css
// Only attach a container class if there is generated CSS to be attached.
},
layoutClasses
);
(0, import_utils.useStyleOverride)({ css });
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
BlockListBlock,
{
...props,
__unstableLayoutClassNames: layoutClassNames
}
);
}
var withLayoutStyles = (0, import_compose.createHigherOrderComponent)(
(BlockListBlock) => (props) => {
const { clientId, name, attributes } = props;
const blockSupportsLayout = hasLayoutBlockSupport(name);
const layoutClasses = useLayoutClasses(attributes, name);
const extraProps = (0, import_data.useSelect)(
(select) => {
if (!blockSupportsLayout) {
return;
}
const { getSettings, getBlockSettings } = (0, import_lock_unlock.unlock)(
select(import_store.store)
);
const { disableLayoutStyles } = getSettings();
if (disableLayoutStyles) {
return;
}
const [blockGapSupport] = getBlockSettings(
clientId,
"spacing.blockGap"
);
return { blockGapSupport };
},
[blockSupportsLayout, clientId]
);
if (!extraProps) {
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
BlockListBlock,
{
...props,
__unstableLayoutClassNames: blockSupportsLayout ? layoutClasses : void 0
}
);
}
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
BlockWithLayoutStyles,
{
block: BlockListBlock,
props,
layoutClasses,
...extraProps
}
);
},
"withLayoutStyles"
);
(0, import_hooks.addFilter)(
"blocks.registerBlockType",
"core/layout/addAttribute",
addAttribute
);
(0, import_hooks.addFilter)(
"editor.BlockListBlock",
"core/editor/layout/with-layout-styles",
withLayoutStyles
);
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
addAttribute,
useLayoutClasses,
useLayoutStyles,
withLayoutStyles
});
//# sourceMappingURL=layout.js.map