UNPKG

@wordpress/block-library

Version:
253 lines (252 loc) • 9.44 kB
"use strict"; 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_block_editor = require("@wordpress/block-editor"); let _wordpress_components = require("@wordpress/components"); let _wordpress_element = require("@wordpress/element"); let _wordpress_data = require("@wordpress/data"); let ___lock_unlock_ts = require("../lock-unlock.cjs"); let __controls_jsx = require("./controls.cjs"); __controls_jsx = __toESM(__controls_jsx); let __constants_js = require("./constants.cjs"); let react_jsx_runtime = require("react/jsx-runtime"); // packages/block-library/src/spacer/edit.jsx const { useSpacingSizes } = (0, ___lock_unlock_ts.unlock)(_wordpress_block_editor.privateApis); const ResizableSpacer = ({ orientation, onResizeStart, onResize, onResizeStop, isSelected, isResizing, setIsResizing, ...props }) => { const getCurrentSize = (elt) => { return orientation === "horizontal" ? elt.clientWidth : elt.clientHeight; }; const getNextVal = (elt) => { return `${getCurrentSize(elt)}px`; }; return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.ResizableBox, { className: (0, clsx.default)("block-library-spacer__resize-container", { "resize-horizontal": orientation === "horizontal", "is-resizing": isResizing, "is-selected": isSelected }), onResizeStart: (_event, _direction, elt) => { const nextVal = getNextVal(elt); onResizeStart(nextVal); onResize(nextVal); }, onResize: (_event, _direction, elt) => { onResize(getNextVal(elt)); if (!isResizing) setIsResizing(true); }, onResizeStop: (_event, _direction, elt) => { onResizeStop(`${getCurrentSize(elt)}px`); setIsResizing(false); }, __experimentalShowTooltip: true, __experimentalTooltipProps: { axis: orientation === "horizontal" ? "x" : "y", position: "corner", isVisible: isResizing }, showHandle: isSelected, ...props }); }; const SpacerEdit = ({ attributes, isSelected, setAttributes, toggleSelection, context, __unstableParentLayout: parentLayout, className }) => { const disableCustomSpacingSizes = (0, _wordpress_data.useSelect)((select) => { return select(_wordpress_block_editor.store).getSettings()?.disableCustomSpacingSizes; }); const { orientation } = context; const { orientation: parentOrientation, type, default: { type: defaultType } = {} } = parentLayout || {}; const isFlexLayout = type === "flex" || !type && defaultType === "flex"; const inheritedOrientation = !parentOrientation && isFlexLayout ? "horizontal" : parentOrientation || orientation; const { height, width, style: blockStyle = {} } = attributes; const { layout = {} } = blockStyle; const { selfStretch, flexSize } = layout; const spacingSizes = useSpacingSizes(); const [isResizing, setIsResizing] = (0, _wordpress_element.useState)(false); const [temporaryHeight, setTemporaryHeight] = (0, _wordpress_element.useState)(null); const [temporaryWidth, setTemporaryWidth] = (0, _wordpress_element.useState)(null); const onResizeStart = () => toggleSelection(false); const onResizeStop = () => toggleSelection(true); const { __unstableMarkNextChangeAsNotPersistent } = (0, _wordpress_data.useDispatch)(_wordpress_block_editor.store); const handleOnVerticalResizeStop = (newHeight) => { onResizeStop(); if (isFlexLayout) setAttributes({ style: { ...blockStyle, layout: { ...layout, flexSize: newHeight, selfStretch: "fixed" } } }); setAttributes({ height: newHeight }); setTemporaryHeight(null); }; const handleOnHorizontalResizeStop = (newWidth) => { onResizeStop(); if (isFlexLayout) setAttributes({ style: { ...blockStyle, layout: { ...layout, flexSize: newWidth, selfStretch: "fixed" } } }); setAttributes({ width: newWidth }); setTemporaryWidth(null); }; const getHeightForVerticalBlocks = () => { if (isFlexLayout) return; return temporaryHeight || (0, _wordpress_block_editor.getSpacingPresetCssVar)(height) || void 0; }; const getWidthForHorizontalBlocks = () => { if (isFlexLayout) return; return temporaryWidth || (0, _wordpress_block_editor.getSpacingPresetCssVar)(width) || void 0; }; const sizeConditionalOnOrientation = inheritedOrientation === "horizontal" ? temporaryWidth || flexSize : temporaryHeight || flexSize; const style = { height: inheritedOrientation === "horizontal" ? 24 : getHeightForVerticalBlocks(), width: inheritedOrientation === "horizontal" ? getWidthForHorizontalBlocks() : void 0, minWidth: inheritedOrientation === "vertical" && isFlexLayout ? 48 : void 0, flexBasis: isFlexLayout ? sizeConditionalOnOrientation : void 0, flexGrow: isFlexLayout && isResizing ? 0 : void 0 }; const resizableBoxWithOrientation = (blockOrientation) => { if (blockOrientation === "horizontal") return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(ResizableSpacer, { minWidth: __constants_js.MIN_SPACER_SIZE, enable: { top: false, right: true, bottom: false, left: false, topRight: false, bottomRight: false, bottomLeft: false, topLeft: false }, orientation: blockOrientation, onResizeStart, onResize: setTemporaryWidth, onResizeStop: handleOnHorizontalResizeStop, isSelected, isResizing, setIsResizing }); return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(react_jsx_runtime.Fragment, { children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(ResizableSpacer, { minHeight: __constants_js.MIN_SPACER_SIZE, enable: { top: false, right: false, bottom: true, left: false, topRight: false, bottomRight: false, bottomLeft: false, topLeft: false }, orientation: blockOrientation, onResizeStart, onResize: setTemporaryHeight, onResizeStop: handleOnVerticalResizeStop, isSelected, isResizing, setIsResizing }) }); }; (0, _wordpress_element.useEffect)(() => { const setAttributesCovertly = (nextAttributes) => { __unstableMarkNextChangeAsNotPersistent(); setAttributes(nextAttributes); }; if (isFlexLayout && selfStretch !== "fill" && selfStretch !== "fit" && flexSize === void 0) { if (inheritedOrientation === "horizontal") { const newSize = (0, _wordpress_block_editor.getCustomValueFromPreset)(width, spacingSizes) || (0, _wordpress_block_editor.getCustomValueFromPreset)(height, spacingSizes) || "100px"; setAttributesCovertly({ width: "0px", style: { ...blockStyle, layout: { ...layout, flexSize: newSize, selfStretch: "fixed" } } }); } else { const newSize = (0, _wordpress_block_editor.getCustomValueFromPreset)(height, spacingSizes) || (0, _wordpress_block_editor.getCustomValueFromPreset)(width, spacingSizes) || "100px"; setAttributesCovertly({ height: "0px", style: { ...blockStyle, layout: { ...layout, flexSize: newSize, selfStretch: "fixed" } } }); } } else if (isFlexLayout && (selfStretch === "fill" || selfStretch === "fit")) setAttributesCovertly(inheritedOrientation === "horizontal" ? { width: void 0 } : { height: void 0 }); else if (!isFlexLayout && (selfStretch || flexSize)) setAttributesCovertly({ ...inheritedOrientation === "horizontal" ? { width: flexSize } : { height: flexSize }, style: { ...blockStyle, layout: { ...layout, flexSize: void 0, selfStretch: void 0 } } }); }, [ blockStyle, flexSize, height, inheritedOrientation, isFlexLayout, layout, selfStretch, setAttributes, spacingSizes, width, __unstableMarkNextChangeAsNotPersistent ]); const blockEditingMode = (0, _wordpress_block_editor.useBlockEditingMode)(); return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(react_jsx_runtime.Fragment, { children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", { ...(0, _wordpress_block_editor.useBlockProps)({ style, className: (0, clsx.default)(className, { "custom-sizes-disabled": disableCustomSpacingSizes }) }), children: blockEditingMode === "default" && resizableBoxWithOrientation(inheritedOrientation) }), !isFlexLayout && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(__controls_jsx.default, { setAttributes, height: temporaryHeight || height, width: temporaryWidth || width, orientation: inheritedOrientation, isResizing })] }); }; exports.default = SpacerEdit; //# sourceMappingURL=edit.cjs.map