@wordpress/block-editor
Version:
140 lines (138 loc) • 5 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-actions/index.js
var block_actions_exports = {};
__export(block_actions_exports, {
default: () => BlockActions
});
module.exports = __toCommonJS(block_actions_exports);
var import_data = require("@wordpress/data");
var import_blocks = require("@wordpress/blocks");
var import_use_paste_styles = __toESM(require("../use-paste-styles"));
var import_store = require("../../store");
function BlockActions({
clientIds,
children,
__experimentalUpdateSelection: updateSelection
}) {
const { getDefaultBlockName, getGroupingBlockName } = (0, import_data.useSelect)(import_blocks.store);
const selected = (0, import_data.useSelect)(
(select) => {
const {
canInsertBlockType,
getBlockRootClientId,
getBlocksByClientId: getBlocksByClientId2,
getDirectInsertBlock,
canRemoveBlocks
} = select(import_store.store);
const blocks = getBlocksByClientId2(clientIds);
const rootClientId = getBlockRootClientId(clientIds[0]);
const canInsertDefaultBlock = canInsertBlockType(
getDefaultBlockName(),
rootClientId
);
const directInsertBlock = rootClientId ? getDirectInsertBlock(rootClientId) : null;
return {
canRemove: canRemoveBlocks(clientIds),
canInsertBlock: blocks.every((block) => {
return (canInsertDefaultBlock || !!directInsertBlock) && canInsertBlockType(block.name, rootClientId);
}),
canCopyStyles: blocks.every((block) => {
return !!block && ((0, import_blocks.hasBlockSupport)(block.name, "color") || (0, import_blocks.hasBlockSupport)(block.name, "typography"));
}),
canDuplicate: blocks.every((block) => {
return !!block && (0, import_blocks.hasBlockSupport)(block.name, "multiple", true) && canInsertBlockType(block.name, rootClientId);
})
};
},
[clientIds, getDefaultBlockName]
);
const { getBlocksByClientId, getBlocks } = (0, import_data.useSelect)(import_store.store);
const { canRemove, canInsertBlock, canCopyStyles, canDuplicate } = selected;
const {
removeBlocks,
replaceBlocks,
duplicateBlocks,
insertAfterBlock,
insertBeforeBlock,
flashBlock
} = (0, import_data.useDispatch)(import_store.store);
const pasteStyles = (0, import_use_paste_styles.default)();
return children({
canCopyStyles,
canDuplicate,
canInsertBlock,
canRemove,
onDuplicate() {
return duplicateBlocks(clientIds, updateSelection);
},
onRemove() {
return removeBlocks(clientIds, updateSelection);
},
onInsertBefore() {
insertBeforeBlock(clientIds[0]);
},
onInsertAfter() {
insertAfterBlock(clientIds[clientIds.length - 1]);
},
onGroup() {
if (!clientIds.length) {
return;
}
const groupingBlockName = getGroupingBlockName();
const newBlocks = (0, import_blocks.switchToBlockType)(
getBlocksByClientId(clientIds),
groupingBlockName
);
if (!newBlocks) {
return;
}
replaceBlocks(clientIds, newBlocks);
},
onUngroup() {
if (!clientIds.length) {
return;
}
const innerBlocks = getBlocks(clientIds[0]);
if (!innerBlocks.length) {
return;
}
replaceBlocks(clientIds, innerBlocks);
},
onCopy() {
if (clientIds.length === 1) {
flashBlock(clientIds[0]);
}
},
async onPasteStyles() {
await pasteStyles(getBlocksByClientId(clientIds));
}
});
}
//# sourceMappingURL=index.js.map