2023-01-07 18:16:19 -08:00
|
|
|
import { CellContextMenuEvent } from '@ag-grid-community/core';
|
2023-01-07 18:25:36 -08:00
|
|
|
import sortBy from 'lodash/sortBy';
|
2023-01-07 18:16:19 -08:00
|
|
|
import { LibraryItem } from '/@/renderer/api/types';
|
|
|
|
|
import { openContextMenu, SetContextMenuItems } from '/@/renderer/features/context-menu/events';
|
|
|
|
|
|
|
|
|
|
export const useHandleTableContextMenu = (
|
|
|
|
|
itemType: LibraryItem,
|
|
|
|
|
contextMenuItems: SetContextMenuItems,
|
|
|
|
|
) => {
|
|
|
|
|
const handleContextMenu = (e: CellContextMenuEvent) => {
|
|
|
|
|
if (!e.event) return;
|
|
|
|
|
const clickEvent = e.event as MouseEvent;
|
|
|
|
|
clickEvent.preventDefault();
|
|
|
|
|
|
|
|
|
|
let selectedNodes = sortBy(e.api.getSelectedNodes(), ['rowIndex']);
|
|
|
|
|
let selectedRows = selectedNodes.map((node) => node.data);
|
|
|
|
|
|
|
|
|
|
const shouldReplaceSelected = !selectedNodes.map((node) => node.data.id).includes(e.data.id);
|
|
|
|
|
|
|
|
|
|
if (shouldReplaceSelected) {
|
|
|
|
|
e.api.deselectAll();
|
|
|
|
|
e.node.setSelected(true);
|
|
|
|
|
selectedRows = [e.data];
|
|
|
|
|
selectedNodes = e.api.getSelectedNodes();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
openContextMenu({
|
|
|
|
|
data: selectedRows,
|
|
|
|
|
dataNodes: selectedNodes,
|
|
|
|
|
menuItems: contextMenuItems,
|
|
|
|
|
type: itemType,
|
|
|
|
|
xPos: clickEvent.clientX,
|
|
|
|
|
yPos: clickEvent.clientY,
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return handleContextMenu;
|
|
|
|
|
};
|