UNPKG

@wordpress/editor

Version:
158 lines (153 loc) 5.06 kB
/* wp:polyfill */ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ActionModal = ActionModal; exports.default = PostActions; var _data = require("@wordpress/data"); var _element = require("@wordpress/element"); var _i18n = require("@wordpress/i18n"); var _components = require("@wordpress/components"); var _icons = require("@wordpress/icons"); var _coreData = require("@wordpress/core-data"); var _lockUnlock = require("../../lock-unlock"); var _actions = require("./actions"); var _jsxRuntime = require("react/jsx-runtime"); /** * WordPress dependencies */ /** * Internal dependencies */ const { Menu, kebabCase } = (0, _lockUnlock.unlock)(_components.privateApis); function useEditedEntityRecordsWithPermissions(postType, postIds) { const { items, permissions } = (0, _data.useSelect)(select => { const { getEditedEntityRecord, getEntityRecordPermissions } = (0, _lockUnlock.unlock)(select(_coreData.store)); return { items: postIds.map(postId => getEditedEntityRecord('postType', postType, postId)), permissions: postIds.map(postId => getEntityRecordPermissions('postType', postType, postId)) }; }, [postIds, postType]); return (0, _element.useMemo)(() => { return items.map((item, index) => ({ ...item, permissions: permissions[index] })); }, [items, permissions]); } function PostActions({ postType, postId, onActionPerformed }) { const [activeModalAction, setActiveModalAction] = (0, _element.useState)(null); const _postIds = (0, _element.useMemo)(() => { if (Array.isArray(postId)) { return postId; } return postId ? [postId] : []; }, [postId]); const itemsWithPermissions = useEditedEntityRecordsWithPermissions(postType, _postIds); const allActions = (0, _actions.usePostActions)({ postType, onActionPerformed }); const actions = (0, _element.useMemo)(() => { return allActions.filter(action => { return (!action.isEligible || itemsWithPermissions.some(itemWithPermissions => action.isEligible(itemWithPermissions))) && (itemsWithPermissions.length < 2 || action.supportsBulk); }); }, [allActions, itemsWithPermissions]); return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, { children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(Menu, { trigger: /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.Button, { size: "small", icon: _icons.moreVertical, label: (0, _i18n.__)('Actions'), disabled: !actions.length, accessibleWhenDisabled: true, className: "editor-all-actions-button" }), placement: "bottom-end", children: /*#__PURE__*/(0, _jsxRuntime.jsx)(ActionsDropdownMenuGroup, { actions: actions, items: itemsWithPermissions, setActiveModalAction: setActiveModalAction }) }), !!activeModalAction && /*#__PURE__*/(0, _jsxRuntime.jsx)(ActionModal, { action: activeModalAction, items: itemsWithPermissions, closeModal: () => setActiveModalAction(null) })] }); } // From now on all the functions on this file are copied as from the dataviews packages, // The editor packages should not be using the dataviews packages directly, // and the dataviews package should not be using the editor packages directly, // so duplicating the code here seems like the least bad option. function DropdownMenuItemTrigger({ action, onClick, items }) { const label = typeof action.label === 'string' ? action.label : action.label(items); return /*#__PURE__*/(0, _jsxRuntime.jsx)(Menu.Item, { onClick: onClick, children: /*#__PURE__*/(0, _jsxRuntime.jsx)(Menu.ItemLabel, { children: label }) }); } function ActionModal({ action, items, closeModal }) { const label = typeof action.label === 'string' ? action.label : action.label(items); return /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.Modal, { title: action.modalHeader || label, __experimentalHideHeader: !!action.hideModalHeader, onRequestClose: closeModal !== null && closeModal !== void 0 ? closeModal : () => {}, focusOnMount: "firstContentElement", size: "medium", overlayClassName: `editor-action-modal editor-action-modal__${kebabCase(action.id)}`, children: /*#__PURE__*/(0, _jsxRuntime.jsx)(action.RenderModal, { items: items, closeModal: closeModal }) }); } function ActionsDropdownMenuGroup({ actions, items, setActiveModalAction }) { const registry = (0, _data.useRegistry)(); return /*#__PURE__*/(0, _jsxRuntime.jsx)(Menu.Group, { children: actions.map(action => { return /*#__PURE__*/(0, _jsxRuntime.jsx)(DropdownMenuItemTrigger, { action: action, onClick: () => { if ('RenderModal' in action) { setActiveModalAction(action); return; } action.callback(items, { registry }); }, items: items }, action.id); }) }); } //# sourceMappingURL=index.js.map