UNPKG

@wordpress/editor

Version:
129 lines (124 loc) 4.28 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.usePostActions = usePostActions; var _data = require("@wordpress/data"); var _element = require("@wordpress/element"); var _coreData = require("@wordpress/core-data"); var _store = require("../../store"); var _lockUnlock = require("../../lock-unlock"); var _setAsHomepage = require("./set-as-homepage"); var _setAsPostsPage = require("./set-as-posts-page"); var _jsxRuntime = require("react/jsx-runtime"); /** * WordPress dependencies */ /** * Internal dependencies */ function usePostActions({ postType, onActionPerformed, context }) { const { defaultActions } = (0, _data.useSelect)(select => { const { getEntityActions } = (0, _lockUnlock.unlock)(select(_store.store)); return { defaultActions: getEntityActions('postType', postType) }; }, [postType]); const { canManageOptions, hasFrontPageTemplate } = (0, _data.useSelect)(select => { const { getEntityRecords, canUser } = select(_coreData.store); const canUpdateSettings = canUser('update', { kind: 'root', name: 'site' }); const templates = 'page' === postType && canUpdateSettings ? getEntityRecords('postType', 'wp_template', { per_page: -1 }) : []; return { canManageOptions: canUpdateSettings, hasFrontPageTemplate: !!templates?.find(template => template?.slug === 'front-page') }; }, [postType]); const setAsHomepageAction = (0, _setAsHomepage.useSetAsHomepageAction)(); const setAsPostsPageAction = (0, _setAsPostsPage.useSetAsPostsPageAction)(); const shouldShowHomepageActions = canManageOptions && !hasFrontPageTemplate; const { registerPostTypeSchema } = (0, _lockUnlock.unlock)((0, _data.useDispatch)(_store.store)); (0, _element.useEffect)(() => { registerPostTypeSchema(postType); }, [registerPostTypeSchema, postType]); return (0, _element.useMemo)(() => { let actions = [...defaultActions]; if (shouldShowHomepageActions) { actions.push(setAsHomepageAction, setAsPostsPageAction); } // Ensure "Move to trash" is always the last action. actions = actions.sort((a, b) => b.id === 'move-to-trash' ? -1 : 0); // Filter actions based on provided context. If not provided // all actions are returned. We'll have a single entry for getting the actions // and the consumer should provide the context to filter the actions, if needed. // Actions should also provide the `context` they support, if it's specific, to // compare with the provided context to get all the actions. // Right now the only supported context is `list`. actions = actions.filter(action => { if (!action.context) { return true; } return action.context === context; }); if (onActionPerformed) { for (let i = 0; i < actions.length; ++i) { if (actions[i].callback) { const existingCallback = actions[i].callback; actions[i] = { ...actions[i], callback: (items, argsObject) => { existingCallback(items, { ...argsObject, onActionPerformed: _items => { if (argsObject?.onActionPerformed) { argsObject.onActionPerformed(_items); } onActionPerformed(actions[i].id, _items); } }); } }; } if (actions[i].RenderModal) { const ExistingRenderModal = actions[i].RenderModal; actions[i] = { ...actions[i], RenderModal: props => { return /*#__PURE__*/(0, _jsxRuntime.jsx)(ExistingRenderModal, { ...props, onActionPerformed: _items => { if (props.onActionPerformed) { props.onActionPerformed(_items); } onActionPerformed(actions[i].id, _items); } }); } }; } } } return actions; }, [context, defaultActions, onActionPerformed, setAsHomepageAction, setAsPostsPageAction, shouldShowHomepageActions]); } //# sourceMappingURL=actions.js.map