@craftercms/studio-ui
Version:
Services, components, models & utils to build CrafterCMS authoring extensions.
89 lines (87 loc) • 3.43 kB
JavaScript
/*
* Copyright (C) 2007-2022 Crafter Software Corporation. All Rights Reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version 3 as published by
* the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* Copyright (C) 2007-2022 Crafter Software Corporation. All Rights Reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as published by
* the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import React from 'react';
import ContextMenu from '../ContextMenu';
import { useDispatch } from 'react-redux';
import { useIntl } from 'react-intl';
import { generateSingleItemOptions, itemActionDispatcher } from '../../utils/itemActions';
import { getRootPath, isValidCopyPastePath, isValidCutPastePath } from '../../utils/path';
import { useSelection } from '../../hooks/useSelection';
import { useActiveSiteId } from '../../hooks/useActiveSiteId';
import { useEnv } from '../../hooks/useEnv';
import { useItemsByPath } from '../../hooks/useItemsByPath';
export function ItemActionsMenu(props) {
const {
open,
path,
onClose,
numOfLoaderItems = 8,
classes,
anchorEl,
anchorOrigin,
anchorReference = 'anchorEl',
anchorPosition
} = props;
const site = useActiveSiteId();
const items = useItemsByPath();
const clipboard = useSelection((state) => state.content.clipboard);
const item = items === null || items === void 0 ? void 0 : items[path];
const { authoringBase } = useEnv();
const dispatch = useDispatch();
const { formatMessage } = useIntl();
const onMenuItemClicked = (option, event) => {
itemActionDispatcher({ site, item, option, authoringBase, dispatch, formatMessage, clipboard, event });
onClose();
};
const hasClipboard =
item &&
clipboard &&
clipboard.paths.length &&
getRootPath(clipboard.sourcePath) === getRootPath(item.path) &&
(clipboard.type === 'CUT'
? isValidCutPastePath(item.path, clipboard.sourcePath)
: isValidCopyPastePath(item.path, clipboard.sourcePath));
const options = generateSingleItemOptions(item, formatMessage, { hasClipboard });
return React.createElement(ContextMenu, {
open: open,
onClose: onClose,
classes: classes,
options: options,
onMenuItemClicked: onMenuItemClicked,
isLoading: !item,
numOfLoaderItems: numOfLoaderItems,
anchorEl: anchorEl,
anchorOrigin: anchorOrigin,
anchorReference: anchorReference,
anchorPosition: anchorPosition
});
}
export default ItemActionsMenu;