@wordpress/block-editor
Version:
94 lines (92 loc) • 3.47 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/utils.js
var utils_exports = {};
__export(utils_exports, {
getPasteBlocks: () => getPasteBlocks,
requiresWrapperOnCopy: () => requiresWrapperOnCopy,
setClipboardBlocks: () => setClipboardBlocks
});
module.exports = __toCommonJS(utils_exports);
var import_dom = require("@wordpress/dom");
var import_blocks = require("@wordpress/blocks");
var import_pasting = require("../../utils/pasting");
var import_store = require("../../store");
var requiresWrapperOnCopy = Symbol("requiresWrapperOnCopy");
function setClipboardBlocks(event, blocks, registry) {
let _blocks = blocks;
const [firstBlock] = blocks;
if (firstBlock) {
const firstBlockType = registry.select(import_blocks.store).getBlockType(firstBlock.name);
if (firstBlockType[requiresWrapperOnCopy]) {
const { getBlockRootClientId, getBlockName, getBlockAttributes } = registry.select(import_store.store);
const wrapperBlockClientId = getBlockRootClientId(
firstBlock.clientId
);
const wrapperBlockName = getBlockName(wrapperBlockClientId);
if (wrapperBlockName) {
_blocks = (0, import_blocks.createBlock)(
wrapperBlockName,
getBlockAttributes(wrapperBlockClientId),
_blocks
);
}
}
}
const serialized = (0, import_blocks.serialize)(_blocks);
event.clipboardData.setData("text/plain", toPlainText(serialized));
event.clipboardData.setData("text/html", serialized);
}
function getPasteBlocks(event, canUserUseUnfilteredHTML) {
const { plainText, html, files } = (0, import_pasting.getPasteEventData)(event);
let blocks = [];
if (files.length) {
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: "BLOCKS",
canUserUseUnfilteredHTML
});
}
return blocks;
}
function toPlainText(html) {
html = html.replace(/<br>/g, "\n");
const plainText = (0, import_dom.__unstableStripHTML)(html).trim();
return plainText.replace(/\n\n+/g, "\n\n");
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
getPasteBlocks,
requiresWrapperOnCopy,
setClipboardBlocks
});
//# sourceMappingURL=utils.js.map