@wordpress/block-editor
Version:
150 lines (148 loc) • 5.46 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/spacing-visualizer.js
var spacing_visualizer_exports = {};
__export(spacing_visualizer_exports, {
MarginVisualizer: () => MarginVisualizer,
PaddingVisualizer: () => PaddingVisualizer
});
module.exports = __toCommonJS(spacing_visualizer_exports);
var import_element = require("@wordpress/element");
var import_is_shallow_equal = __toESM(require("@wordpress/is-shallow-equal"));
var import_cover = __toESM(require("../components/block-popover/cover"));
var import_use_block_refs = require("../components/block-list/use-block-props/use-block-refs");
var import_jsx_runtime = require("react/jsx-runtime");
function SpacingVisualizer({ clientId, value, computeStyle, forceShow }) {
const blockElement = (0, import_use_block_refs.useBlockElement)(clientId);
const [style, updateStyle] = (0, import_element.useReducer)(
() => computeStyle(blockElement)
);
(0, import_element.useEffect)(() => {
if (blockElement && forceShow) {
updateStyle();
}
}, [blockElement, forceShow]);
(0, import_element.useEffect)(() => {
if (!blockElement) {
return;
}
const observer = new window.MutationObserver(updateStyle);
observer.observe(blockElement, {
attributes: true,
attributeFilter: ["style", "class"]
});
return () => {
observer.disconnect();
};
}, [blockElement]);
const previousValueRef = (0, import_element.useRef)(value);
const [isActive, setIsActive] = (0, import_element.useState)(false);
(0, import_element.useEffect)(() => {
if ((0, import_is_shallow_equal.default)(value, previousValueRef.current) || forceShow) {
return;
}
setIsActive(true);
previousValueRef.current = value;
const timeout = setTimeout(() => {
setIsActive(false);
}, 400);
return () => {
setIsActive(false);
clearTimeout(timeout);
};
}, [value, forceShow]);
if (!isActive && !forceShow) {
return null;
}
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
import_cover.default,
{
clientId,
__unstablePopoverSlot: "block-toolbar",
children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: "block-editor__spacing-visualizer", style })
}
);
}
function getComputedCSS(element, property) {
return element.ownerDocument.defaultView.getComputedStyle(element).getPropertyValue(property);
}
function MarginVisualizer({ clientId, value, forceShow }) {
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
SpacingVisualizer,
{
clientId,
value: value?.spacing?.margin,
computeStyle: (blockElement) => {
const top = getComputedCSS(blockElement, "margin-top");
const right = getComputedCSS(blockElement, "margin-right");
const bottom = getComputedCSS(blockElement, "margin-bottom");
const left = getComputedCSS(blockElement, "margin-left");
return {
borderTopWidth: top,
borderRightWidth: right,
borderBottomWidth: bottom,
borderLeftWidth: left,
top: top ? `-${top}` : 0,
right: right ? `-${right}` : 0,
bottom: bottom ? `-${bottom}` : 0,
left: left ? `-${left}` : 0
};
},
forceShow
}
);
}
function PaddingVisualizer({ clientId, value, forceShow }) {
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
SpacingVisualizer,
{
clientId,
value: value?.spacing?.padding,
computeStyle: (blockElement) => ({
borderTopWidth: getComputedCSS(blockElement, "padding-top"),
borderRightWidth: getComputedCSS(
blockElement,
"padding-right"
),
borderBottomWidth: getComputedCSS(
blockElement,
"padding-bottom"
),
borderLeftWidth: getComputedCSS(blockElement, "padding-left")
}),
forceShow
}
);
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
MarginVisualizer,
PaddingVisualizer
});
//# sourceMappingURL=spacing-visualizer.js.map