@wordpress/block-editor
Version:
171 lines (170 loc) • 5.02 kB
JavaScript
// packages/block-editor/src/hooks/dimensions.js
import clsx from "clsx";
import { Platform, useState, useEffect, useCallback } from "@wordpress/element";
import { useDispatch, useSelect } from "@wordpress/data";
import { getBlockSupport } from "@wordpress/blocks";
import deprecated from "@wordpress/deprecated";
import InspectorControls from "../components/inspector-controls";
import {
DimensionsPanel as StylesDimensionsPanel,
useHasDimensionsPanel
} from "../components/global-styles";
import { MarginVisualizer, PaddingVisualizer } from "./spacing-visualizer";
import { store as blockEditorStore } from "../store";
import { unlock } from "../lock-unlock";
import { cleanEmptyObject, shouldSkipSerialization } from "./utils";
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
var DIMENSIONS_SUPPORT_KEY = "dimensions";
var SPACING_SUPPORT_KEY = "spacing";
var ALL_SIDES = ["top", "right", "bottom", "left"];
var AXIAL_SIDES = ["vertical", "horizontal"];
function useVisualizer() {
const [property, setProperty] = useState(false);
const { hideBlockInterface, showBlockInterface } = unlock(
useDispatch(blockEditorStore)
);
useEffect(() => {
if (!property) {
showBlockInterface();
} else {
hideBlockInterface();
}
}, [property, showBlockInterface, hideBlockInterface]);
return [property, setProperty];
}
function DimensionsInspectorControl({ children, resetAllFilter }) {
const attributesResetAllFilter = useCallback(
(attributes) => {
const existingStyle = attributes.style;
const updatedStyle = resetAllFilter(existingStyle);
return {
...attributes,
style: updatedStyle
};
},
[resetAllFilter]
);
return /* @__PURE__ */ jsx(
InspectorControls,
{
group: "dimensions",
resetAllFilter: attributesResetAllFilter,
children
}
);
}
function DimensionsPanel({ clientId, name, setAttributes, settings }) {
const isEnabled = useHasDimensionsPanel(settings);
const value = useSelect(
(select) => {
if (!isEnabled) {
return void 0;
}
return select(blockEditorStore).getBlockAttributes(clientId)?.style;
},
[clientId, isEnabled]
);
const [visualizedProperty, setVisualizedProperty] = useVisualizer();
const onChange = (newStyle) => {
setAttributes({
style: cleanEmptyObject(newStyle)
});
};
if (!isEnabled) {
return null;
}
const defaultDimensionsControls = getBlockSupport(name, [
DIMENSIONS_SUPPORT_KEY,
"__experimentalDefaultControls"
]);
const defaultSpacingControls = getBlockSupport(name, [
SPACING_SUPPORT_KEY,
"__experimentalDefaultControls"
]);
const defaultControls = {
...defaultDimensionsControls,
...defaultSpacingControls
};
return /* @__PURE__ */ jsxs(Fragment, { children: [
/* @__PURE__ */ jsx(
StylesDimensionsPanel,
{
as: DimensionsInspectorControl,
panelId: clientId,
settings,
value,
onChange,
defaultControls,
onVisualize: setVisualizedProperty
}
),
!!settings?.spacing?.padding && visualizedProperty === "padding" && /* @__PURE__ */ jsx(
PaddingVisualizer,
{
forceShow: visualizedProperty === "padding",
clientId,
value
}
),
!!settings?.spacing?.margin && visualizedProperty === "margin" && /* @__PURE__ */ jsx(
MarginVisualizer,
{
forceShow: visualizedProperty === "margin",
clientId,
value
}
)
] });
}
function hasDimensionsSupport(blockName, feature = "any") {
if (Platform.OS !== "web") {
return false;
}
const support = getBlockSupport(blockName, DIMENSIONS_SUPPORT_KEY);
if (support === true) {
return true;
}
if (feature === "any") {
return !!(support?.aspectRatio || !!support?.minHeight || !!support?.width);
}
return !!support?.[feature];
}
var dimensions_default = {
useBlockProps,
attributeKeys: ["minHeight", "width", "style"],
hasSupport(name) {
return hasDimensionsSupport(name);
}
};
function useBlockProps({ name, minHeight, style }) {
if (!hasDimensionsSupport(name, "aspectRatio") || shouldSkipSerialization(name, DIMENSIONS_SUPPORT_KEY, "aspectRatio")) {
return {};
}
const className = clsx({
"has-aspect-ratio": !!style?.dimensions?.aspectRatio
});
const inlineStyleOverrides = {};
if (style?.dimensions?.aspectRatio) {
inlineStyleOverrides.minHeight = "unset";
} else if (minHeight || style?.dimensions?.minHeight) {
inlineStyleOverrides.aspectRatio = "unset";
}
return { className, style: inlineStyleOverrides };
}
function useCustomSides() {
deprecated("wp.blockEditor.__experimentalUseCustomSides", {
since: "6.3",
version: "6.4"
});
}
export {
ALL_SIDES,
AXIAL_SIDES,
DIMENSIONS_SUPPORT_KEY,
DimensionsPanel,
SPACING_SUPPORT_KEY,
dimensions_default as default,
hasDimensionsSupport,
useCustomSides
};
//# sourceMappingURL=dimensions.js.map