@wordpress/block-editor
Version:
86 lines (84 loc) • 2.75 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/utils/pasting.js
var pasting_exports = {};
__export(pasting_exports, {
getPasteEventData: () => getPasteEventData,
shouldDismissPastedFiles: () => shouldDismissPastedFiles
});
module.exports = __toCommonJS(pasting_exports);
var import_dom = require("@wordpress/dom");
function removeWindowsFragments(html) {
const startStr = "<!--StartFragment-->";
const startIdx = html.indexOf(startStr);
if (startIdx > -1) {
html = html.substring(startIdx + startStr.length);
} else {
return html;
}
const endStr = "<!--EndFragment-->";
const endIdx = html.indexOf(endStr);
if (endIdx > -1) {
html = html.substring(0, endIdx);
}
return html;
}
function removeCharsetMetaTag(html) {
const metaTag = `<meta charset='utf-8'>`;
if (html.startsWith(metaTag)) {
return html.slice(metaTag.length);
}
return html;
}
function getPasteEventData({ clipboardData }) {
let plainText = "";
let html = "";
try {
plainText = clipboardData.getData("text/plain");
html = clipboardData.getData("text/html");
} catch (error) {
return;
}
html = removeWindowsFragments(html);
html = removeCharsetMetaTag(html);
const files = (0, import_dom.getFilesFromDataTransfer)(clipboardData);
if (files.length && !shouldDismissPastedFiles(files, html)) {
return { files };
}
return { html, plainText, files: [] };
}
function shouldDismissPastedFiles(files, html) {
if (html && files?.length === 1 && files[0].type.indexOf("image/") === 0) {
const IMAGE_TAG = /<\s*img\b/gi;
if (html.match(IMAGE_TAG)?.length !== 1) {
return true;
}
const IMG_WITH_LOCAL_SRC = /<\s*img\b[^>]*\bsrc="file:\/\//i;
if (html.match(IMG_WITH_LOCAL_SRC)) {
return true;
}
}
return false;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
getPasteEventData,
shouldDismissPastedFiles
});
//# sourceMappingURL=pasting.js.map