UNPKG

@wordpress/block-editor

Version:
156 lines (154 loc) 6.02 kB
"use strict"; 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/list-view/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_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_utils2 = require("../writing-flow/utils"); function useClipboardHandler({ selectBlock }) { const registry = (0, import_data.useRegistry)(); const { getBlockOrder, getBlockRootClientId, getBlocksByClientId, getPreviousBlockClientId, getSelectedBlockClientIds, getSettings, canInsertBlockType, canRemoveBlocks } = (0, import_data.useSelect)(import_store.store); const { flashBlock, removeBlocks, replaceBlocks, insertBlocks } = (0, import_data.useDispatch)(import_store.store); const notifyCopy = (0, import_use_notify_copy.useNotifyCopy)(); return (0, import_compose.useRefEffect)((node) => { function updateFocusAndSelection(focusClientId, shouldSelectBlock) { if (shouldSelectBlock) { selectBlock(void 0, focusClientId, null, null); } (0, import_utils.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); (0, import_utils2.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 = (0, import_utils2.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); }; }, []); } //# sourceMappingURL=use-clipboard-handler.js.map