@wordpress/block-editor
Version:
208 lines (206 loc) • 7.83 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// packages/block-editor/src/components/writing-flow/use-clipboard-handler.js
var use_clipboard_handler_exports = {};
__export(use_clipboard_handler_exports, {
default: () => useClipboardHandler
});
module.exports = __toCommonJS(use_clipboard_handler_exports);
var import_blocks = require("@wordpress/blocks");
var import_dom = require("@wordpress/dom");
var import_data = require("@wordpress/data");
var import_compose = require("@wordpress/compose");
var import_store = require("../../store");
var import_use_notify_copy = require("../../utils/use-notify-copy");
var import_utils = require("./utils");
var import_pasting = require("../../utils/pasting");
function useClipboardHandler() {
const registry = (0, import_data.useRegistry)();
const {
getBlocksByClientId,
getSelectedBlockClientIds,
hasMultiSelection,
getSettings,
getBlockName,
__unstableIsFullySelected,
__unstableIsSelectionCollapsed,
__unstableIsSelectionMergeable,
__unstableGetSelectedBlocksWithPartialSelection,
canInsertBlockType,
getBlockRootClientId
} = (0, import_data.useSelect)(import_store.store);
const {
flashBlock,
removeBlocks,
replaceBlocks,
__unstableDeleteSelection,
__unstableExpandSelection,
__unstableSplitSelection
} = (0, import_data.useDispatch)(import_store.store);
const notifyCopy = (0, import_use_notify_copy.useNotifyCopy)();
return (0, import_compose.useRefEffect)((node) => {
function handler(event) {
if (event.defaultPrevented) {
return;
}
const selectedBlockClientIds = getSelectedBlockClientIds();
if (selectedBlockClientIds.length === 0) {
return;
}
if (!hasMultiSelection()) {
const { target } = event;
const { ownerDocument } = target;
const hasSelection = event.type === "copy" || event.type === "cut" ? (0, import_dom.documentHasUncollapsedSelection)(ownerDocument) : (0, import_dom.documentHasSelection)(ownerDocument) && !ownerDocument.activeElement.isContentEditable;
if (hasSelection) {
return;
}
}
const { activeElement } = event.target.ownerDocument;
if (!node.contains(activeElement)) {
return;
}
const isSelectionMergeable = __unstableIsSelectionMergeable();
const shouldHandleWholeBlocks = __unstableIsSelectionCollapsed() || __unstableIsFullySelected();
const expandSelectionIsNeeded = !shouldHandleWholeBlocks && !isSelectionMergeable;
if (event.type === "copy" || event.type === "cut") {
event.preventDefault();
if (selectedBlockClientIds.length === 1) {
flashBlock(selectedBlockClientIds[0]);
}
if (expandSelectionIsNeeded) {
__unstableExpandSelection();
} else {
notifyCopy(event.type, selectedBlockClientIds);
let blocks;
if (shouldHandleWholeBlocks) {
blocks = getBlocksByClientId(selectedBlockClientIds);
} else {
const [head, tail] = __unstableGetSelectedBlocksWithPartialSelection();
const inBetweenBlocks = getBlocksByClientId(
selectedBlockClientIds.slice(
1,
selectedBlockClientIds.length - 1
)
);
blocks = [head, ...inBetweenBlocks, tail];
}
(0, import_utils.setClipboardBlocks)(event, blocks, registry);
}
}
if (event.type === "cut") {
if (shouldHandleWholeBlocks && !expandSelectionIsNeeded) {
removeBlocks(selectedBlockClientIds);
} else {
event.target.ownerDocument.activeElement.contentEditable = false;
__unstableDeleteSelection();
}
} else if (event.type === "paste") {
const {
__experimentalCanUserUseUnfilteredHTML: canUserUseUnfilteredHTML,
mediaUpload
} = getSettings();
const isInternal = event.clipboardData.getData("rich-text") === "true";
if (isInternal) {
return;
}
const { plainText, html, files } = (0, import_pasting.getPasteEventData)(event);
const isFullySelected = __unstableIsFullySelected();
let blocks = [];
if (files.length) {
if (!mediaUpload) {
event.preventDefault();
return;
}
const fromTransforms = (0, import_blocks.getBlockTransforms)("from");
blocks = files.reduce((accumulator, file) => {
const transformation = (0, import_blocks.findTransform)(
fromTransforms,
(transform) => transform.type === "files" && transform.isMatch([file])
);
if (transformation) {
accumulator.push(
transformation.transform([file])
);
}
return accumulator;
}, []).flat();
} else {
blocks = (0, import_blocks.pasteHandler)({
HTML: html,
plainText,
mode: isFullySelected ? "BLOCKS" : "AUTO",
canUserUseUnfilteredHTML
});
}
if (typeof blocks === "string") {
return;
}
if (isFullySelected) {
replaceBlocks(
selectedBlockClientIds,
blocks,
blocks.length - 1,
-1
);
event.preventDefault();
return;
}
if (!hasMultiSelection() && !(0, import_blocks.hasBlockSupport)(
getBlockName(selectedBlockClientIds[0]),
"splitting",
false
) && !event.__deprecatedOnSplit) {
return;
}
const [firstSelectedClientId] = selectedBlockClientIds;
const rootClientId = getBlockRootClientId(
firstSelectedClientId
);
const newBlocks = [];
for (const block of blocks) {
if (canInsertBlockType(block.name, rootClientId)) {
newBlocks.push(block);
} else {
const rootBlockName = getBlockName(rootClientId);
const switchedBlocks = block.name !== rootBlockName ? (0, import_blocks.switchToBlockType)(block, rootBlockName) : [block];
if (!switchedBlocks) {
return;
}
for (const switchedBlock of switchedBlocks) {
for (const innerBlock of switchedBlock.innerBlocks) {
newBlocks.push(innerBlock);
}
}
}
}
__unstableSplitSelection(newBlocks);
event.preventDefault();
}
}
node.ownerDocument.addEventListener("copy", handler);
node.ownerDocument.addEventListener("cut", handler);
node.ownerDocument.addEventListener("paste", handler);
return () => {
node.ownerDocument.removeEventListener("copy", handler);
node.ownerDocument.removeEventListener("cut", handler);
node.ownerDocument.removeEventListener("paste", handler);
};
}, []);
}
//# sourceMappingURL=use-clipboard-handler.js.map