@wordpress/block-library
Version:
Block library for the WordPress editor.
58 lines (57 loc) • 2.25 kB
JavaScript
import { Button } from "@wordpress/components";
import { Stack } from "@wordpress/ui";
import { __ } from "@wordpress/i18n";
import { useSelect } from "@wordpress/data";
import { privateApis, store } from "@wordpress/block-editor";
import { store as store$1 } from "@wordpress/core-data";
import { unlock } from "../lock-unlock.mjs";
import { jsx } from "react/jsx-runtime";
// packages/block-library/src/utils/edit-original-inspector-button.tsx
const { PrivateBlockInspectorEditOriginal } = unlock(privateApis);
/**
* The block inspector's "Edit original" button for a block that renders an
* entity, such as a synced pattern or a template part. It opens the entity
* for editing, and only shows for users who can edit it, on a block that
* isn't locked against editing.
*
* @param props Component props.
* @param props.clientId Client ID of the block that renders the entity.
* @param props.postType The entity's post type.
* @param props.postId The entity's ID.
*/
function EditOriginalInspectorButton({ clientId, postType, postId }) {
const { canUserEdit, onNavigateToEntityRecord } = useSelect((select) => {
const { canEditBlock, getSettings } = select(store);
return {
canUserEdit: canEditBlock(clientId) && (!postId || !!select(store$1).canUser("update", {
kind: "postType",
name: postType,
id: postId
})),
onNavigateToEntityRecord: getSettings().onNavigateToEntityRecord
};
}, [
clientId,
postType,
postId
]);
if (!canUserEdit || !onNavigateToEntityRecord) return null;
return /* @__PURE__ */ jsx(PrivateBlockInspectorEditOriginal.Fill, { children: ({ clientId: inspectedClientId }) => inspectedClientId === clientId && /* @__PURE__ */ jsx(Stack, {
direction: "column",
className: "block-editor-block-inspector-edit-contents",
children: /* @__PURE__ */ jsx(Button, {
className: "block-editor-block-inspector-edit-contents__button",
__next40pxDefaultSize: true,
variant: "secondary",
onClick: () => onNavigateToEntityRecord({
postId,
postType
}),
accessibleWhenDisabled: true,
disabled: !postId,
children: __("Edit original")
})
}) });
}
export { EditOriginalInspectorButton as default };
//# sourceMappingURL=edit-original-inspector-button.mjs.map