@wordpress/block-library
Version:
Block library for the WordPress editor.
99 lines (92 loc) • 2.99 kB
JavaScript
/**
* WordPress dependencies
*/
import { store as blockEditorStore } from '@wordpress/block-editor';
import { store as coreStore, useEntityProp } from '@wordpress/core-data';
import { __, sprintf } from '@wordpress/i18n';
import { useSelect } from '@wordpress/data';
function getAvatarSizes(sizes) {
const minSize = sizes ? sizes[0] : 24;
const maxSize = sizes ? sizes[sizes.length - 1] : 96;
const maxSizeBuffer = Math.floor(maxSize * 2.5);
return {
minSize,
maxSize: maxSizeBuffer
};
}
function useDefaultAvatar() {
const {
avatarURL: defaultAvatarUrl
} = useSelect(select => {
const {
getSettings
} = select(blockEditorStore);
const {
__experimentalDiscussionSettings
} = getSettings();
return __experimentalDiscussionSettings;
});
return defaultAvatarUrl;
}
export function useCommentAvatar(_ref) {
let {
commentId
} = _ref;
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,
maxSize
} = getAvatarSizes(sizes);
const defaultAvatar = useDefaultAvatar();
return {
src: avatarUrls ? avatarUrls[avatarUrls.length - 1] : defaultAvatar,
minSize,
maxSize,
// translators: %s is the Author name.
alt: authorName ? // translators: %s is the Author name.
sprintf(__('%s Avatar'), authorName) : __('Default Avatar')
};
}
export function useUserAvatar(_ref2) {
let {
userId,
postId,
postType
} = _ref2;
const {
authorDetails
} = useSelect(select => {
var _getEditedEntityRecor;
const {
getEditedEntityRecord,
getUser
} = select(coreStore);
if (userId) {
return {
authorDetails: getUser(userId)
};
}
const _authorId = (_getEditedEntityRecor = getEditedEntityRecord('postType', postType, postId)) === null || _getEditedEntityRecor === void 0 ? void 0 : _getEditedEntityRecor.author;
return {
authorDetails: _authorId ? getUser(_authorId) : null
};
}, [postType, postId, userId]);
const avatarUrls = authorDetails !== null && authorDetails !== void 0 && authorDetails.avatar_urls ? Object.values(authorDetails.avatar_urls) : null;
const sizes = authorDetails !== null && authorDetails !== void 0 && authorDetails.avatar_urls ? Object.keys(authorDetails.avatar_urls) : null;
const {
minSize,
maxSize
} = getAvatarSizes(sizes);
const defaultAvatar = useDefaultAvatar();
return {
src: avatarUrls ? avatarUrls[avatarUrls.length - 1] : defaultAvatar,
minSize,
maxSize,
alt: authorDetails ? // translators: %s is the Author name.
sprintf(__('%s Avatar'), authorDetails === null || authorDetails === void 0 ? void 0 : authorDetails.name) : __('Default Avatar')
};
}
//# sourceMappingURL=hooks.js.map