@wordpress/block-editor
Version:
173 lines (171 loc) • 5.94 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/use-paste-styles/index.js
var use_paste_styles_exports = {};
__export(use_paste_styles_exports, {
default: () => usePasteStyles
});
module.exports = __toCommonJS(use_paste_styles_exports);
var import_element = require("@wordpress/element");
var import_blocks = require("@wordpress/blocks");
var import_data = require("@wordpress/data");
var import_notices = require("@wordpress/notices");
var import_i18n = require("@wordpress/i18n");
var import_store = require("../../store/index.cjs");
var import_supports = require("../../hooks/supports.cjs");
function hasSerializedBlocks(text) {
try {
const blocks = (0, import_blocks.parse)(text, {
__unstableSkipMigrationLogs: true,
__unstableSkipAutop: true
});
if (blocks.length === 1 && blocks[0].name === "core/freeform") {
return false;
}
return true;
} catch (err) {
return false;
}
}
var STYLE_ATTRIBUTES = {
align: import_supports.hasAlignSupport,
borderColor: (nameOrType) => (0, import_supports.hasBorderSupport)(nameOrType, "color"),
backgroundColor: import_supports.hasBackgroundColorSupport,
textAlign: import_supports.hasTextAlignSupport,
textColor: import_supports.hasTextColorSupport,
gradient: import_supports.hasGradientSupport,
className: import_supports.hasCustomClassNameSupport,
fontFamily: import_supports.hasFontFamilySupport,
fontSize: import_supports.hasFontSizeSupport,
layout: import_supports.hasLayoutSupport,
style: import_supports.hasStyleSupport
};
function getStyleAttributes(sourceBlock, targetBlock) {
return Object.entries(STYLE_ATTRIBUTES).reduce(
(attributes, [attributeKey, hasSupport]) => {
if (hasSupport(sourceBlock.name) && hasSupport(targetBlock.name)) {
attributes[attributeKey] = sourceBlock.attributes[attributeKey];
}
return attributes;
},
{}
);
}
function recursivelyUpdateBlockAttributes(targetBlocks, sourceBlocks, updateBlockAttributes) {
for (let index = 0; index < Math.min(sourceBlocks.length, targetBlocks.length); index += 1) {
updateBlockAttributes(
targetBlocks[index].clientId,
getStyleAttributes(sourceBlocks[index], targetBlocks[index])
);
recursivelyUpdateBlockAttributes(
targetBlocks[index].innerBlocks,
sourceBlocks[index].innerBlocks,
updateBlockAttributes
);
}
}
function usePasteStyles() {
const registry = (0, import_data.useRegistry)();
const { updateBlockAttributes } = (0, import_data.useDispatch)(import_store.store);
const { createSuccessNotice, createWarningNotice, createErrorNotice } = (0, import_data.useDispatch)(import_notices.store);
return (0, import_element.useCallback)(
async (targetBlocks) => {
let html = "";
try {
if (!window.navigator.clipboard) {
createErrorNotice(
(0, import_i18n.__)(
"Unable to paste styles. This feature is only available on secure (https) sites in supporting browsers."
),
{ type: "snackbar" }
);
return;
}
html = await window.navigator.clipboard.readText();
} catch (error) {
createErrorNotice(
(0, import_i18n.__)(
"Unable to paste styles. Please allow browser clipboard permissions before continuing."
),
{
type: "snackbar"
}
);
return;
}
if (!html || !hasSerializedBlocks(html)) {
createWarningNotice(
(0, import_i18n.__)(
"Unable to paste styles. Block styles couldn't be found within the copied content."
),
{
type: "snackbar"
}
);
return;
}
const copiedBlocks = (0, import_blocks.parse)(html);
if (copiedBlocks.length === 1) {
registry.batch(() => {
recursivelyUpdateBlockAttributes(
targetBlocks,
targetBlocks.map(() => copiedBlocks[0]),
updateBlockAttributes
);
});
} else {
registry.batch(() => {
recursivelyUpdateBlockAttributes(
targetBlocks,
copiedBlocks,
updateBlockAttributes
);
});
}
if (targetBlocks.length === 1) {
const title = (0, import_blocks.getBlockType)(targetBlocks[0].name)?.title;
createSuccessNotice(
(0, import_i18n.sprintf)(
// Translators: %s: Name of the block being pasted, e.g. "Paragraph".
(0, import_i18n.__)("Pasted styles to %s."),
title
),
{ type: "snackbar" }
);
} else {
createSuccessNotice(
(0, import_i18n.sprintf)(
// Translators: %d: The number of the blocks.
(0, import_i18n.__)("Pasted styles to %d blocks."),
targetBlocks.length
),
{ type: "snackbar" }
);
}
},
[
registry.batch,
updateBlockAttributes,
createSuccessNotice,
createWarningNotice,
createErrorNotice
]
);
}
//# sourceMappingURL=index.cjs.map