@wordpress/block-editor
Version:
135 lines (134 loc) • 4.97 kB
JavaScript
// packages/block-editor/src/components/list-view/use-clipboard-handler.js
import { useDispatch, useRegistry, useSelect } from "@wordpress/data";
import { useRefEffect } from "@wordpress/compose";
import { store as blockEditorStore } from "../../store";
import { useNotifyCopy } from "../../utils/use-notify-copy";
import { focusListItem } from "./utils";
import { getPasteBlocks, setClipboardBlocks } from "../writing-flow/utils";
function useClipboardHandler({ selectBlock }) {
const registry = useRegistry();
const {
getBlockOrder,
getBlockRootClientId,
getBlocksByClientId,
getPreviousBlockClientId,
getSelectedBlockClientIds,
getSettings,
canInsertBlockType,
canRemoveBlocks
} = useSelect(blockEditorStore);
const { flashBlock, removeBlocks, replaceBlocks, insertBlocks } = useDispatch(blockEditorStore);
const notifyCopy = useNotifyCopy();
return useRefEffect((node) => {
function updateFocusAndSelection(focusClientId, shouldSelectBlock) {
if (shouldSelectBlock) {
selectBlock(void 0, focusClientId, null, null);
}
focusListItem(focusClientId, node);
}
function getBlocksToUpdate(clientId) {
const selectedBlockClientIds = getSelectedBlockClientIds();
const isUpdatingSelectedBlocks = selectedBlockClientIds.includes(clientId);
const firstBlockClientId = isUpdatingSelectedBlocks ? selectedBlockClientIds[0] : clientId;
const firstBlockRootClientId = getBlockRootClientId(firstBlockClientId);
const blocksToUpdate = isUpdatingSelectedBlocks ? selectedBlockClientIds : [clientId];
return {
blocksToUpdate,
firstBlockClientId,
firstBlockRootClientId,
originallySelectedBlockClientIds: selectedBlockClientIds
};
}
function handler(event) {
if (event.defaultPrevented) {
return;
}
if (!node.contains(event.target.ownerDocument.activeElement)) {
return;
}
const listViewRow = event.target.ownerDocument.activeElement?.closest(
"[role=row]"
);
const clientId = listViewRow?.dataset?.block;
if (!clientId) {
return;
}
const {
blocksToUpdate: selectedBlockClientIds,
firstBlockClientId,
firstBlockRootClientId,
originallySelectedBlockClientIds
} = getBlocksToUpdate(clientId);
if (selectedBlockClientIds.length === 0) {
return;
}
event.preventDefault();
if (event.type === "copy" || event.type === "cut") {
if (selectedBlockClientIds.length === 1) {
flashBlock(selectedBlockClientIds[0]);
}
notifyCopy(event.type, selectedBlockClientIds);
const blocks = getBlocksByClientId(selectedBlockClientIds);
setClipboardBlocks(event, blocks, registry);
}
if (event.type === "cut") {
if (!canRemoveBlocks(selectedBlockClientIds)) {
return;
}
let blockToFocus = getPreviousBlockClientId(firstBlockClientId) ?? // If the previous block is not found (when the first block is deleted),
// fallback to focus the parent block.
firstBlockRootClientId;
removeBlocks(selectedBlockClientIds, false);
const shouldUpdateSelection = originallySelectedBlockClientIds.length > 0 && getSelectedBlockClientIds().length === 0;
if (!blockToFocus) {
blockToFocus = getBlockOrder()[0];
}
updateFocusAndSelection(blockToFocus, shouldUpdateSelection);
} else if (event.type === "paste") {
const {
__experimentalCanUserUseUnfilteredHTML: canUserUseUnfilteredHTML
} = getSettings();
const blocks = getPasteBlocks(
event,
canUserUseUnfilteredHTML
);
if (selectedBlockClientIds.length === 1) {
const [selectedBlockClientId] = selectedBlockClientIds;
if (blocks.every(
(block) => canInsertBlockType(
block.name,
selectedBlockClientId
)
)) {
insertBlocks(
blocks,
void 0,
selectedBlockClientId
);
updateFocusAndSelection(blocks[0]?.clientId, false);
return;
}
}
replaceBlocks(
selectedBlockClientIds,
blocks,
blocks.length - 1,
-1
);
updateFocusAndSelection(blocks[0]?.clientId, false);
}
}
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);
};
}, []);
}
export {
useClipboardHandler as default
};
//# sourceMappingURL=use-clipboard-handler.js.map