@wordpress/block-library
Version:
Block library for the WordPress editor.
98 lines (97 loc) • 3.1 kB
JavaScript
import { useSelect } from "@wordpress/data";
import { useEffect, useRef } from "@wordpress/element";
import { getBlobByURL, isBlobURL, revokeBlobURL } from "@wordpress/blob";
import { store } from "@wordpress/block-editor";
import { store as store$1, useEntityRecords } from "@wordpress/core-data";
import { useEvent, useViewportMatch } from "@wordpress/compose";
// packages/block-library/src/utils/hooks.js
/**
* Returns whether the current user can edit the given entity.
*
* @param {string} kind Entity kind.
* @param {string} name Entity name.
* @param {string} recordId Record's id.
*/
function useCanEditEntity(kind, name, recordId) {
return useSelect((select) => select(store$1).canUser("update", {
kind,
name,
id: recordId
}), [
kind,
name,
recordId
]);
}
/**
* Returns the comments query of a post: `totalItems` is its comment count once
* known, and stays null when the request fails.
*
* @param {number} postId Post ID. Nothing is fetched without one.
*/
function usePostCommentsCount(postId) {
return useEntityRecords("root", "comment", {
post: postId,
per_page: 1,
_fields: "id",
context: "view"
}, { enabled: !!postId });
}
/**
* Handles uploading a media file from a blob URL on mount.
*
* @param {Object} args Upload media arguments.
* @param {string} args.url Blob URL.
* @param {?Array} args.allowedTypes Array of allowed media types.
* @param {Function} args.onChange Function called when the media is uploaded.
* @param {Function} args.onError Function called when an error happens.
*/
function useUploadMediaFromBlobURL(args = {}) {
const hasUploadStartedRef = useRef(false);
const { getSettings } = useSelect(store);
const onChange = useEvent(args.onChange);
const onError = useEvent(args.onError);
const startUpload = useEvent(() => {
if (hasUploadStartedRef.current) return;
if (!args.url || !isBlobURL(args.url)) return;
const file = getBlobByURL(args.url);
if (!file) return;
const { url, allowedTypes } = args;
const { mediaUpload } = getSettings();
if (!mediaUpload) return;
hasUploadStartedRef.current = true;
mediaUpload({
filesList: [file],
allowedTypes,
onFileChange: ([media]) => {
if (isBlobURL(media?.url)) return;
revokeBlobURL(url);
onChange(media);
hasUploadStartedRef.current = false;
},
onError: (message) => {
revokeBlobURL(url);
onError(message);
hasUploadStartedRef.current = false;
}
});
});
useEffect(() => {
startUpload();
}, [startUpload]);
}
function useDefaultAvatar() {
return useSelect((select) => {
const { getSettings } = select(store);
const { __experimentalDiscussionSettings } = getSettings();
return __experimentalDiscussionSettings?.avatarURL ?? "";
}, []);
}
function useToolsPanelDropdownMenuProps() {
return !useViewportMatch("medium", "<") ? { popoverProps: {
placement: "left-start",
offset: 259
} } : {};
}
export { useCanEditEntity, useDefaultAvatar, usePostCommentsCount, useToolsPanelDropdownMenuProps, useUploadMediaFromBlobURL };
//# sourceMappingURL=hooks.mjs.map