Skip to content

Commit

Permalink
Feat(DND): Enable Simple DND
Browse files Browse the repository at this point in the history
  • Loading branch information
shivamG640 committed Dec 4, 2024
1 parent 4d8d775 commit 99a12be
Show file tree
Hide file tree
Showing 12 changed files with 410 additions and 124 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { isNode, observer } from '@patternfly/react-topology';
import { FunctionComponent } from 'react';
import { useCollapseStep } from '../hooks/collapse-step.hook';
import { CustomNodeWithSelection } from '../Node/CustomNode';
import { CustomGroupExpanded } from './CustomGroupExpanded';
import { CustomGroupExpendedWithDndDrop } from './CustomGroupExpanded';
import { CustomGroupProps } from './Group.models';

export const CustomGroupCollapsible: FunctionComponent<CustomGroupProps> = observer(
Expand All @@ -26,7 +26,7 @@ export const CustomGroupCollapsible: FunctionComponent<CustomGroupProps> = obser
}

return (
<CustomGroupExpanded
<CustomGroupExpendedWithDndDrop
{...rest}
element={element}
onCollapseToggle={() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
background-color: var(--custom-node-BackgroundColor);
text-align: left;

&__dropTarget {
border: 4px dashed var(--custom-node-BorderColor);
}

&__title {
display: flex;
position: relative;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ import { Icon } from '@patternfly/react-core';
import { BanIcon } from '@patternfly/react-icons';
import {
AnchorEnd,
DropTargetSpec,
GROUPS_LAYER,
GraphElement,
GraphElementProps,
Layer,
Node,
Rect,
Expand All @@ -12,6 +15,7 @@ import {
useAnchor,
useHover,
useSelection,
withDndDrop,
} from '@patternfly/react-topology';
import { FunctionComponent, useContext, useRef } from 'react';
import { IVisualizationNode, NodeToolbarTrigger } from '../../../../models';
Expand All @@ -21,9 +25,10 @@ import { CanvasDefaults } from '../../Canvas/canvas.defaults';
import { TargetAnchor } from '../target-anchor';
import './CustomGroupExpanded.scss';
import { CustomGroupProps } from './Group.models';
import clsx from 'clsx';

export const CustomGroupExpanded: FunctionComponent<CustomGroupProps> = observer(
({ element, onContextMenu, onCollapseToggle }) => {
const CustomGroupExpanded: FunctionComponent<CustomGroupProps> = observer(
({ element, onContextMenu, onCollapseToggle, dndDropRef, hover, droppable, canDrop }) => {
if (!isNode(element)) {
throw new Error('CustomGroupExpanded must be used only on Node elements');
}
Expand All @@ -39,7 +44,7 @@ export const CustomGroupExpanded: FunctionComponent<CustomGroupProps> = observer
CanvasDefaults.HOVER_DELAY_IN,
CanvasDefaults.HOVER_DELAY_OUT,
);
const boxRef = useRef<Rect>(element.getBounds());
const boxRef = useRef<Rect | null>(null);
const shouldShowToolbar =
settingsAdapter.getSettings().nodeToolbarTrigger === NodeToolbarTrigger.onHover
? isGHover || isToolbarHover || isSelected
Expand All @@ -53,7 +58,9 @@ export const CustomGroupExpanded: FunctionComponent<CustomGroupProps> = observer
return null;
}

boxRef.current = element.getBounds();
if (!droppable || !boxRef.current) {
boxRef.current = element.getBounds();
}
const toolbarWidth = Math.max(CanvasDefaults.STEP_TOOLBAR_WIDTH, boxRef.current.width);
const toolbarX = boxRef.current.x + (boxRef.current.width - toolbarWidth) / 2;
const toolbarY = boxRef.current.y - CanvasDefaults.STEP_TOOLBAR_HEIGHT;
Expand All @@ -72,13 +79,18 @@ export const CustomGroupExpanded: FunctionComponent<CustomGroupProps> = observer
onContextMenu={onContextMenu}
>
<foreignObject
ref={dndDropRef}
data-nodelabel={label}
x={boxRef.current.x}
y={boxRef.current.y}
width={boxRef.current.width}
height={boxRef.current.height}
>
<div className="custom-group__container">
<div
className={clsx('custom-group__container', {
'custom-group__container__dropTarget': canDrop && hover,
})}
>
<div className="custom-group__container__title" title={tooltipContent}>
<img src={vizNode.data.icon} />
<span title={label}>{label}</span>
Expand Down Expand Up @@ -116,3 +128,23 @@ export const CustomGroupExpanded: FunctionComponent<CustomGroupProps> = observer
);
},
);

const groupDropTargetSpec: DropTargetSpec<GraphElement, unknown, object, GraphElementProps> = {
accept: ['#node#'],
canDrop: (item, _monitor, props) => {
if (!!props && (item as Node).getData().vizNode.parentNode.id !== (props.element as Node).getData().vizNode.id) {
return (
(item as Node).getData().vizNode.parentNode.id === (props.element as Node).getData().vizNode.parentNode?.id
);
} else {
return false;
}
},
collect: (monitor) => ({
droppable: monitor.isDragging(),
hover: monitor.isOver(),
canDrop: monitor.canDrop(),
}),
};

export const CustomGroupExpendedWithDndDrop = withDndDrop(groupDropTargetSpec)(CustomGroupExpanded);
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
import type { ElementModel, GraphElement, DefaultGroup as TopologyDefaultGroup } from '@patternfly/react-topology';
import type {
ElementModel,
GraphElement,
DefaultGroup as TopologyDefaultGroup,
WithDndDropProps,
} from '@patternfly/react-topology';
import { CanvasNode } from '../../Canvas';

type DefaultGroupProps = Parameters<typeof TopologyDefaultGroup>[0];
export interface CustomGroupProps extends DefaultGroupProps {
export interface CustomGroupProps extends DefaultGroupProps, WithDndDropProps {
element: GraphElement<ElementModel, CanvasNode['data']>;
/** Toggle node collapse / expand */
onCollapseToggle?: () => void;
hover?: boolean;
droppable?: boolean;
canDrop?: boolean;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
flex-flow: column nowrap;
justify-content: space-around;

&__dropTarget {
border: 3px dashed var(--pf-v5-global--BorderColor--100);
border-radius: 5px;
}

&__image {
position: relative;
display: flex;
Expand Down
Loading

0 comments on commit 99a12be

Please sign in to comment.