@wordpress/block-library
Version:
Block library for the WordPress editor.
83 lines (82 loc) • 3.21 kB
JavaScript
import clsx from "clsx";
import { __ } from "@wordpress/i18n";
import { AlignmentControl, BlockControls, store, useBlockProps, useInnerBlocksProps } from "@wordpress/block-editor";
import { BlockQuotation } from "@wordpress/components";
import { useDispatch, useRegistry, useSelect } from "@wordpress/data";
import { useEffect } from "@wordpress/element";
import deprecated from "@wordpress/deprecated";
import { verse } from "@wordpress/icons";
import { migrateToQuoteV2 } from "./deprecated.mjs";
import { Caption } from "../utils/caption.mjs";
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
// packages/block-library/src/quote/edit.jsx
/**
* At the moment, deprecations don't handle create blocks from attributes
* (like when using CPT templates). For this reason, this hook is necessary
* to avoid breaking templates using the old quote block format.
*
* @param {Object} attributes Block attributes.
* @param {string} clientId Block client ID.
*/
const useMigrateOnLoad = (attributes, clientId) => {
const registry = useRegistry();
const { updateBlockAttributes, replaceInnerBlocks, selectBlock } = useDispatch(store);
useEffect(() => {
if (!attributes.value) return;
const [newAttributes, newInnerBlocks] = migrateToQuoteV2(attributes);
deprecated("Value attribute on the quote block", {
since: "6.0",
version: "6.5",
alternative: "inner blocks"
});
const shouldReselectBlock = registry.select(store).hasSelectedInnerBlock(clientId, true);
registry.batch(() => {
updateBlockAttributes(clientId, newAttributes);
replaceInnerBlocks(clientId, newInnerBlocks);
if (shouldReselectBlock) selectBlock(clientId);
});
}, [attributes.value]);
};
function QuoteEdit({ attributes, setAttributes, insertBlocksAfter, clientId, className, isSelected }) {
const { textAlign, allowedBlocks } = attributes;
const { hasInnerBlocks } = useSelect((select) => {
const { getBlockCount } = select(store);
return { hasInnerBlocks: getBlockCount(clientId) > 0 };
}, [clientId]);
useMigrateOnLoad(attributes, clientId);
const blockProps = useBlockProps({ className: clsx(className, { [`has-text-align-${textAlign}`]: textAlign }) });
const innerBlocksProps = useInnerBlocksProps(blockProps, {
__experimentalCaptureToolbars: true,
renderAppender: hasInnerBlocks ? false : void 0,
allowedBlocks
});
return /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsx(BlockControls, {
group: "block",
children: /* @__PURE__ */ jsx(AlignmentControl, {
value: textAlign,
onChange: (nextAlign) => {
setAttributes({ textAlign: nextAlign });
}
})
}), /* @__PURE__ */ jsxs(BlockQuotation, {
...innerBlocksProps,
children: [innerBlocksProps.children, /* @__PURE__ */ jsx(Caption, {
attributeKey: "citation",
tagName: "cite",
style: { display: "block" },
isSelected,
attributes,
setAttributes,
icon: verse,
label: __("Quote citation"),
placeholder: __("Add citation"),
addLabel: __("Add citation"),
removeLabel: __("Remove citation"),
excludeElementClassName: true,
className: "wp-block-quote__citation",
insertBlocksAfter
})]
})] });
}
export { QuoteEdit as default };
//# sourceMappingURL=edit.mjs.map