@wordpress/block-editor
Version:
177 lines (175 loc) • 7.2 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/components/block-mover/button.js
var button_exports = {};
__export(button_exports, {
BlockMoverDownButton: () => BlockMoverDownButton,
BlockMoverUpButton: () => BlockMoverUpButton
});
module.exports = __toCommonJS(button_exports);
var import_clsx = __toESM(require("clsx"));
var import_blocks = require("@wordpress/blocks");
var import_components = require("@wordpress/components");
var import_compose = require("@wordpress/compose");
var import_data = require("@wordpress/data");
var import_element = require("@wordpress/element");
var import_i18n = require("@wordpress/i18n");
var import_icons = require("@wordpress/icons");
var import_mover_description = require("./mover-description");
var import_store = require("../../store");
var import_jsx_runtime = require("react/jsx-runtime");
var getArrowIcon = (direction, orientation) => {
if (direction === "up") {
if (orientation === "horizontal") {
return (0, import_i18n.isRTL)() ? import_icons.chevronRight : import_icons.chevronLeft;
}
return import_icons.chevronUp;
} else if (direction === "down") {
if (orientation === "horizontal") {
return (0, import_i18n.isRTL)() ? import_icons.chevronLeft : import_icons.chevronRight;
}
return import_icons.chevronDown;
}
return null;
};
var getMovementDirectionLabel = (moveDirection, orientation) => {
if (moveDirection === "up") {
if (orientation === "horizontal") {
return (0, import_i18n.isRTL)() ? (0, import_i18n.__)("Move right") : (0, import_i18n.__)("Move left");
}
return (0, import_i18n.__)("Move up");
} else if (moveDirection === "down") {
if (orientation === "horizontal") {
return (0, import_i18n.isRTL)() ? (0, import_i18n.__)("Move left") : (0, import_i18n.__)("Move right");
}
return (0, import_i18n.__)("Move down");
}
return null;
};
var BlockMoverButton = (0, import_element.forwardRef)(
({ clientIds, direction, orientation: moverOrientation, ...props }, ref) => {
const instanceId = (0, import_compose.useInstanceId)(BlockMoverButton);
const normalizedClientIds = Array.isArray(clientIds) ? clientIds : [clientIds];
const blocksCount = normalizedClientIds.length;
const { disabled } = props;
const {
blockType,
isDisabled,
rootClientId,
isFirst,
isLast,
firstIndex,
orientation = "vertical"
} = (0, import_data.useSelect)(
(select) => {
const {
getBlockIndex,
getBlockRootClientId,
getBlockOrder,
getBlock,
getBlockListSettings
} = select(import_store.store);
const firstClientId = normalizedClientIds[0];
const blockRootClientId = getBlockRootClientId(firstClientId);
const firstBlockIndex = getBlockIndex(firstClientId);
const lastBlockIndex = getBlockIndex(
normalizedClientIds[normalizedClientIds.length - 1]
);
const blockOrder = getBlockOrder(blockRootClientId);
const block = getBlock(firstClientId);
const isFirstBlock = firstBlockIndex === 0;
const isLastBlock = lastBlockIndex === blockOrder.length - 1;
const { orientation: blockListOrientation } = getBlockListSettings(blockRootClientId) || {};
return {
blockType: block ? (0, import_blocks.getBlockType)(block.name) : null,
isDisabled: disabled || (direction === "up" ? isFirstBlock : isLastBlock),
rootClientId: blockRootClientId,
firstIndex: firstBlockIndex,
isFirst: isFirstBlock,
isLast: isLastBlock,
orientation: moverOrientation || blockListOrientation
};
},
[clientIds, direction]
);
const { moveBlocksDown, moveBlocksUp } = (0, import_data.useDispatch)(import_store.store);
const moverFunction = direction === "up" ? moveBlocksUp : moveBlocksDown;
const onClick = (event) => {
moverFunction(clientIds, rootClientId);
if (props.onClick) {
props.onClick(event);
}
};
const descriptionId = `block-editor-block-mover-button__description-${instanceId}`;
return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, { children: [
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(
import_components.Button,
{
__next40pxDefaultSize: true,
ref,
className: (0, import_clsx.default)(
"block-editor-block-mover-button",
`is-${direction}-button`
),
icon: getArrowIcon(direction, orientation),
label: getMovementDirectionLabel(
direction,
orientation
),
"aria-describedby": descriptionId,
...props,
onClick: isDisabled ? null : onClick,
disabled: isDisabled,
accessibleWhenDisabled: true
}
),
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_components.VisuallyHidden, { id: descriptionId, children: (0, import_mover_description.getBlockMoverDescription)(
blocksCount,
blockType && blockType.title,
firstIndex,
isFirst,
isLast,
direction === "up" ? -1 : 1,
orientation
) })
] });
}
);
var BlockMoverUpButton = (0, import_element.forwardRef)((props, ref) => {
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(BlockMoverButton, { direction: "up", ref, ...props });
});
var BlockMoverDownButton = (0, import_element.forwardRef)((props, ref) => {
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(BlockMoverButton, { direction: "down", ref, ...props });
});
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
BlockMoverDownButton,
BlockMoverUpButton
});
//# sourceMappingURL=button.js.map