-
Notifications
You must be signed in to change notification settings - Fork 45
/
useDropNode.ts
240 lines (219 loc) · 8.54 KB
/
useDropNode.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*******************************************************************************
* Copyright (c) 2023 Obeo.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Obeo - initial API and implementation
*******************************************************************************/
import { gql, useMutation } from '@apollo/client';
import { useMultiToast } from '@eclipse-sirius/sirius-components-core';
import { useTheme } from '@material-ui/core/styles';
import { useCallback, useContext, useEffect } from 'react';
import { Node, NodeDragHandler, Viewport, XYPosition, useReactFlow, useStoreApi, useViewport } from 'reactflow';
import { DiagramContext } from '../../contexts/DiagramContext';
import { DiagramContextValue } from '../../contexts/DiagramContext.types';
import { useDiagramDescription } from '../../contexts/useDiagramDescription';
import { GQLDropNodeCompatibility } from '../../representation/DiagramRepresentation.types';
import { EdgeData, NodeData } from '../DiagramRenderer.types';
import { useLayout } from '../layout/useLayout';
import { DropNodeContext } from './DropNodeContext';
import { DropNodeContextValue } from './DropNodeContext.types';
import {
DiagramBackgroundStyle,
GQLDropNodeData,
GQLDropNodeInput,
GQLDropNodePayload,
GQLDropNodeVariables,
GQLErrorPayload,
GQLSuccessPayload,
UseDropNodeValue,
} from './useDropNode.types';
const dropNodeMutation = gql`
mutation dropNode($input: DropNodeInput!) {
dropNode(input: $input) {
__typename
... on ErrorPayload {
messages {
body
level
}
}
... on SuccessPayload {
messages {
body
level
}
}
}
}
`;
const isErrorPayload = (payload: GQLDropNodePayload): payload is GQLErrorPayload =>
payload.__typename === 'ErrorPayload';
const isSuccessPayload = (payload: GQLDropNodePayload): payload is GQLSuccessPayload =>
payload.__typename === 'SuccessPayload';
const getNodeDepth = (node: Node<NodeData>, intersections: Node<NodeData>[]): number => {
let nodeDepth = 0;
let nodeHierarchy: Node<NodeData> | undefined = node;
while (nodeHierarchy) {
nodeDepth++;
nodeHierarchy = intersections.find((node) => node.id === nodeHierarchy?.parentNode);
}
return nodeDepth;
};
const useDropNodeMutation = () => {
const { diagramId, editingContextId } = useContext<DiagramContextValue>(DiagramContext);
const { addErrorMessage, addMessages } = useMultiToast();
const [dropMutation, { data: dropNodeData, error: dropNodeError }] = useMutation<
GQLDropNodeData,
GQLDropNodeVariables
>(dropNodeMutation);
const { setReferencePosition, resetReferencePosition } = useLayout();
useEffect(() => {
if (dropNodeError) {
addErrorMessage('An unexpected error has occurred, please refresh the page');
resetReferencePosition();
}
if (dropNodeData) {
const { dropNode } = dropNodeData;
if (isSuccessPayload(dropNode)) {
addMessages(dropNode.messages);
}
if (isErrorPayload(dropNode)) {
addMessages(dropNode.messages);
resetReferencePosition();
}
}
}, [dropNodeData, dropNodeError]);
const invokeMutation = (droppedElementId: string, targetElementId: string | null, dropPosition: XYPosition): void => {
const input: GQLDropNodeInput = {
id: crypto.randomUUID(),
editingContextId,
representationId: diagramId,
droppedElementId,
targetElementId,
};
setReferencePosition(dropPosition, targetElementId);
dropMutation({ variables: { input } });
};
return invokeMutation;
};
const isDescendantOf = (parent: Node, candidate: Node, nodeById: (string) => Node | undefined): boolean => {
if (parent.id === candidate.id) {
return true;
} else {
const candidateParent = nodeById(candidate.parentNode);
return candidateParent !== undefined && isDescendantOf(parent, candidateParent, nodeById);
}
};
const computeDropPosition = (
event: MouseEvent | React.MouseEvent,
{ x: viewportX, y: viewportY, zoom: viewportZoom }: Viewport,
bounds?: DOMRect
): XYPosition => {
return {
x: (event.clientX - (bounds?.left ?? 0) - viewportX) / viewportZoom,
y: (event.clientY - (bounds?.top ?? 0) - viewportY) / viewportZoom,
};
};
export const useDropNode = (): UseDropNodeValue => {
const {
initialParentId,
draggedNode,
targetNodeId,
compatibleNodeIds,
droppableOnDiagram,
initializeDrop,
setTargetNodeId,
resetDrop,
} = useContext<DropNodeContextValue>(DropNodeContext);
const { diagramDescription } = useDiagramDescription();
const onDropNode = useDropNodeMutation();
const { getNodes, getIntersectingNodes } = useReactFlow<NodeData, EdgeData>();
const viewport = useViewport();
const { domNode } = useStoreApi().getState();
const element = domNode?.getBoundingClientRect();
const getNodeById: (string) => Node | undefined = (id: string) => getNodes().find((n) => n.id === id);
const onNodeDragStart: NodeDragHandler = (_event, node) => {
const dropDataEntry: GQLDropNodeCompatibility | undefined = diagramDescription.dropNodeCompatibility.find(
(entry) => entry.droppedNodeDescriptionId === (node as Node<NodeData>).data.descriptionId
);
const compatibleNodes = getNodes()
.filter((candidate) => !candidate.hidden && !isDescendantOf(node, candidate, getNodeById))
.filter((candidate) =>
dropDataEntry?.droppableOnNodeTypes.includes((candidate as Node<NodeData>).data.descriptionId)
)
.map((candidate) => candidate.id);
initializeDrop({
initialParentId: node.parentNode || null,
draggedNode: node,
targetNodeId: node.parentNode || null,
compatibleNodeIds: compatibleNodes,
droppableOnDiagram: dropDataEntry?.droppableOnDiagram || false,
});
};
const onNodeDrag: NodeDragHandler = (_event, node) => {
if (draggedNode && !draggedNode.data.isBorderNode) {
const intersections = getIntersectingNodes(node).filter((intersectingNode) => !intersectingNode.hidden);
const newParentId =
[...intersections]
.filter((intersectingNode) => !isDescendantOf(draggedNode, intersectingNode, getNodeById))
.sort((n1, n2) => getNodeDepth(n2, intersections) - getNodeDepth(n1, intersections))[0]?.id || null;
setTargetNodeId(newParentId);
}
};
const onNodeDragStop: (onDragCancelled: (node: Node) => void) => NodeDragHandler = useCallback(
(onDragCancelled) => {
return (event, _node) => {
const dropPosition = computeDropPosition(event, viewport, element);
if (draggedNode) {
const oldParentId: string | null = draggedNode.parentNode || null;
const newParentId: string | null = targetNodeId;
const validNewParent =
(newParentId === null && droppableOnDiagram) ||
(newParentId !== null && compatibleNodeIds.includes(newParentId));
if (oldParentId !== newParentId) {
if (validNewParent) {
onDropNode(draggedNode.id, newParentId, dropPosition);
} else {
onDragCancelled(draggedNode);
}
} else if (draggedNode.type === 'iconLabelNode') {
// For list items, any move which did not change the parent (and potentially
// trigger a DnD operation) should be reverted as their layout is fixed.
onDragCancelled(draggedNode);
}
}
resetDrop();
};
},
[element?.top, element?.left, viewport, draggedNode, targetNodeId, droppableOnDiagram, compatibleNodeIds]
);
const hasDroppedNodeParentChanged = (): boolean => {
return draggedNode?.parentNode !== targetNodeId;
};
const theme = useTheme();
const diagramForbidden = draggedNode?.id !== null && !droppableOnDiagram;
const diagramTargeted = targetNodeId === null && initialParentId !== null;
const backgroundColor =
diagramTargeted && diagramForbidden ? theme.palette.action.disabledBackground : theme.palette.background.default;
const diagramBackgroundStyle: DiagramBackgroundStyle = {
backgroundColor,
smallGridColor: diagramForbidden ? backgroundColor : '#f1f1f1',
largeGridColor: diagramForbidden ? backgroundColor : '#cccccc',
};
return {
onNodeDragStart,
onNodeDrag,
onNodeDragStop,
hasDroppedNodeParentChanged,
compatibleNodeIds,
draggedNode,
targetNodeId,
diagramBackgroundStyle,
};
};