UNPKG

@wordpress/editor

Version:
83 lines (80 loc) 2.8 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.default = PostTrash; var _i18n = require("@wordpress/i18n"); var _components = require("@wordpress/components"); var _data = require("@wordpress/data"); var _element = require("@wordpress/element"); var _store = require("../../store"); var _check = _interopRequireDefault(require("./check")); var _jsxRuntime = require("react/jsx-runtime"); /** * WordPress dependencies */ /** * Internal dependencies */ /** * Displays the Post Trash Button and Confirm Dialog in the Editor. * * @param {?{onActionPerformed: Object}} An object containing the onActionPerformed function. * @return {JSX.Element|null} The rendered PostTrash component. */function PostTrash({ onActionPerformed }) { const registry = (0, _data.useRegistry)(); const { isNew, isDeleting, postId, title } = (0, _data.useSelect)(select => { const store = select(_store.store); return { isNew: store.isEditedPostNew(), isDeleting: store.isDeletingPost(), postId: store.getCurrentPostId(), title: store.getCurrentPostAttribute('title') }; }, []); const { trashPost } = (0, _data.useDispatch)(_store.store); const [showConfirmDialog, setShowConfirmDialog] = (0, _element.useState)(false); if (isNew || !postId) { return null; } const handleConfirm = async () => { setShowConfirmDialog(false); await trashPost(); const item = await registry.resolveSelect(_store.store).getCurrentPost(); // After the post is trashed, we want to trigger the onActionPerformed callback, so the user is redirect // to the post view depending on if the user is on post editor or site editor. onActionPerformed?.('move-to-trash', [item]); }; return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_check.default, { children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_components.Button, { __next40pxDefaultSize: true, className: "editor-post-trash", isDestructive: true, variant: "secondary", isBusy: isDeleting, "aria-disabled": isDeleting, onClick: isDeleting ? undefined : () => setShowConfirmDialog(true), children: (0, _i18n.__)('Move to trash') }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.__experimentalConfirmDialog, { isOpen: showConfirmDialog, onConfirm: handleConfirm, onCancel: () => setShowConfirmDialog(false), confirmButtonText: (0, _i18n.__)('Move to trash'), size: "small", children: (0, _i18n.sprintf)( // translators: %s: The item's title. (0, _i18n.__)('Are you sure you want to move "%s" to the trash?'), title) })] }); } //# sourceMappingURL=index.js.map