@wordpress/block-library
Version:
Block library for the WordPress editor.
71 lines (70 loc) • 2.57 kB
JavaScript
import { InspectorControls, __experimentalGetSpacingClassesAndStyles, store, useBlockProps } from "@wordpress/block-editor";
import { PanelBody, RangeControl, ResizableBox } from "@wordpress/components";
import { useEntityProp } from "@wordpress/core-data";
import { useSelect } from "@wordpress/data";
import { __, isRTL } from "@wordpress/i18n";
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
// packages/block-library/src/comment-author-avatar/edit.jsx
function Edit({ attributes, context: { commentId }, setAttributes, isSelected }) {
const { height, width } = attributes;
const [avatars] = useEntityProp("root", "comment", "author_avatar_urls", commentId);
const [authorName] = useEntityProp("root", "comment", "author_name", commentId);
const avatarUrls = avatars ? Object.values(avatars) : null;
const sizes = avatars ? Object.keys(avatars) : null;
const minSize = sizes ? sizes[0] : 24;
const maxSize = sizes ? sizes[sizes.length - 1] : 96;
const blockProps = useBlockProps();
const spacingProps = __experimentalGetSpacingClassesAndStyles(attributes);
const maxSizeBuffer = Math.floor(maxSize * 2.5);
const avatarURL = useSelect((select) => {
const { __experimentalDiscussionSettings } = select(store).getSettings();
return __experimentalDiscussionSettings?.avatarURL;
}, []);
const inspectorControls = /* @__PURE__ */ jsx(InspectorControls, { children: /* @__PURE__ */ jsx(PanelBody, {
title: __("Settings"),
children: /* @__PURE__ */ jsx(RangeControl, {
label: __("Image size"),
onChange: (newWidth) => setAttributes({
width: newWidth,
height: newWidth
}),
min: minSize,
max: maxSizeBuffer,
initialPosition: width,
value: width
})
}) });
const resizableAvatar = /* @__PURE__ */ jsx(ResizableBox, {
size: {
width,
height
},
showHandle: isSelected,
onResizeStop: (event, direction, elt, delta) => {
setAttributes({
height: parseInt(height + delta.height, 10),
width: parseInt(width + delta.width, 10)
});
},
lockAspectRatio: true,
enable: {
top: false,
right: !isRTL(),
bottom: true,
left: isRTL()
},
minWidth: minSize,
maxWidth: maxSizeBuffer,
children: /* @__PURE__ */ jsx("img", {
src: avatarUrls ? avatarUrls[avatarUrls.length - 1] : avatarURL,
alt: `${authorName} ${__("Avatar")}`,
...blockProps
})
});
return /* @__PURE__ */ jsxs(Fragment, { children: [inspectorControls, /* @__PURE__ */ jsx("div", {
...spacingProps,
children: resizableAvatar
})] });
}
export { Edit as default };
//# sourceMappingURL=edit.mjs.map