UNPKG

@wordpress/block-editor

Version:
74 lines (73 loc) 2.33 kB
// packages/block-editor/src/components/writing-flow/utils.js import { __unstableStripHTML as stripHTML } from "@wordpress/dom"; import { serialize, createBlock, pasteHandler, findTransform, getBlockTransforms, store as blocksStore } from "@wordpress/blocks"; import { getPasteEventData } from "../../utils/pasting"; import { store as blockEditorStore } from "../../store"; var requiresWrapperOnCopy = Symbol("requiresWrapperOnCopy"); function setClipboardBlocks(event, blocks, registry) { let _blocks = blocks; const [firstBlock] = blocks; if (firstBlock) { const firstBlockType = registry.select(blocksStore).getBlockType(firstBlock.name); if (firstBlockType[requiresWrapperOnCopy]) { const { getBlockRootClientId, getBlockName, getBlockAttributes } = registry.select(blockEditorStore); const wrapperBlockClientId = getBlockRootClientId( firstBlock.clientId ); const wrapperBlockName = getBlockName(wrapperBlockClientId); if (wrapperBlockName) { _blocks = createBlock( wrapperBlockName, getBlockAttributes(wrapperBlockClientId), _blocks ); } } } const serialized = serialize(_blocks); event.clipboardData.setData("text/plain", toPlainText(serialized)); event.clipboardData.setData("text/html", serialized); } function getPasteBlocks(event, canUserUseUnfilteredHTML) { const { plainText, html, files } = getPasteEventData(event); let blocks = []; if (files.length) { const fromTransforms = getBlockTransforms("from"); blocks = files.reduce((accumulator, file) => { const transformation = findTransform( fromTransforms, (transform) => transform.type === "files" && transform.isMatch([file]) ); if (transformation) { accumulator.push(transformation.transform([file])); } return accumulator; }, []).flat(); } else { blocks = pasteHandler({ HTML: html, plainText, mode: "BLOCKS", canUserUseUnfilteredHTML }); } return blocks; } function toPlainText(html) { html = html.replace(/<br>/g, "\n"); const plainText = stripHTML(html).trim(); return plainText.replace(/\n\n+/g, "\n\n"); } export { getPasteBlocks, requiresWrapperOnCopy, setClipboardBlocks }; //# sourceMappingURL=utils.js.map