@wordpress/editor
Version:
Enhanced block editor for WordPress posts.
151 lines (146 loc) • 4.7 kB
JavaScript
;
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 PostActions({
postType,
postId,
onActionPerformed
}) {
const [activeModalAction, setActiveModalAction] = (0, _element.useState)(null);
const {
item,
permissions
} = (0, _data.useSelect)(select => {
const {
getEditedEntityRecord,
getEntityRecordPermissions
} = (0, _lockUnlock.unlock)(select(_coreData.store));
return {
item: getEditedEntityRecord('postType', postType, postId),
permissions: getEntityRecordPermissions('postType', postType, postId)
};
}, [postId, postType]);
const itemWithPermissions = (0, _element.useMemo)(() => {
return {
...item,
permissions
};
}, [item, permissions]);
const allActions = (0, _actions.usePostActions)({
postType,
onActionPerformed
});
const actions = (0, _element.useMemo)(() => {
return allActions.filter(action => {
return !action.isEligible || action.isEligible(itemWithPermissions);
});
}, [allActions, itemWithPermissions]);
return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
children: [/*#__PURE__*/(0, _jsxRuntime.jsxs)(Menu, {
placement: "bottom-end",
children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(Menu.TriggerButton, {
render: /*#__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"
})
}), /*#__PURE__*/(0, _jsxRuntime.jsx)(Menu.Popover, {
children: /*#__PURE__*/(0, _jsxRuntime.jsx)(ActionsDropdownMenuGroup, {
actions: actions,
items: [itemWithPermissions],
setActiveModalAction: setActiveModalAction
})
})]
}), !!activeModalAction && /*#__PURE__*/(0, _jsxRuntime.jsx)(ActionModal, {
action: activeModalAction,
items: [itemWithPermissions],
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