-
Notifications
You must be signed in to change notification settings - Fork 45
/
FreeFormNode.tsx
179 lines (170 loc) · 6.03 KB
/
FreeFormNode.tsx
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
/*******************************************************************************
* 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 { ServerContext, ServerContextValue, getCSSColor } from '@eclipse-sirius/sirius-components-core';
import { Theme, useTheme } from '@material-ui/core/styles';
import React, { memo, useContext } from 'react';
import { NodeProps, NodeResizer } from 'reactflow';
import { BorderNodePosition } from '../DiagramRenderer.types';
import { Label } from '../Label';
import { useConnector } from '../connector/useConnector';
import { useDrop } from '../drop/useDrop';
import { useDropNodeStyle } from '../dropNode/useDropNodeStyle';
import { ConnectionCreationHandles } from '../handles/ConnectionCreationHandles';
import { ConnectionHandles } from '../handles/ConnectionHandles';
import { ConnectionTargetHandle } from '../handles/ConnectionTargetHandle';
import { useRefreshConnectionHandles } from '../handles/useRefreshConnectionHandles';
import { DiagramElementPalette } from '../palette/DiagramElementPalette';
import { FreeFormNodeData } from './FreeFormNode.types';
import { NodeContext } from './NodeContext';
import { NodeContextValue } from './NodeContext.types';
const freeFormNodeStyle = (
theme: Theme,
style: React.CSSProperties,
selected: boolean,
hovered: boolean,
faded: boolean,
rotation: string | undefined,
imageURL: string | undefined
): React.CSSProperties => {
const freeFormNodeStyle: React.CSSProperties = {
width: '100%',
height: '100%',
opacity: faded ? '0.4' : '',
transform: rotation,
...style,
backgroundColor: getCSSColor(String(style.backgroundColor), theme),
};
if (selected || hovered) {
freeFormNodeStyle.outline = `${theme.palette.selected} solid 1px`;
}
if (imageURL) {
freeFormNodeStyle.backgroundImage = `url(${imageURL})`;
freeFormNodeStyle.backgroundRepeat = 'no-repeat';
freeFormNodeStyle.backgroundSize = '100% 100%';
}
return freeFormNodeStyle;
};
const computeBorderRotation = (data: FreeFormNodeData): string | undefined => {
if (data?.isBorderNode && data.positionDependentRotation) {
switch (data.borderNodePosition) {
case BorderNodePosition.NORTH:
return 'rotate(90deg)';
case BorderNodePosition.EAST:
return 'rotate(180deg)';
case BorderNodePosition.SOUTH:
return 'rotate(270deg)';
default:
return undefined;
}
}
return undefined;
};
const outsideBottomLabelAreaStyle = (): React.CSSProperties => {
return {
display: 'grid',
gridTemplateColumns: `1fr 1fr 1fr`,
gridTemplateRows: `min-content`,
gridTemplateAreas: `
'BOTTOM_BEGIN BOTTOM_MIDDLE BOTTOM_END'
`,
};
};
export const FreeFormNode = memo(({ data, id, selected }: NodeProps<FreeFormNodeData>) => {
const { httpOrigin } = useContext<ServerContextValue>(ServerContext);
const theme = useTheme();
const { onDrop, onDragOver } = useDrop();
const { newConnectionStyleProvider } = useConnector();
const { style: dropFeedbackStyle } = useDropNodeStyle(id);
const { hoveredNode } = useContext<NodeContextValue>(NodeContext);
const rotation = computeBorderRotation(data);
let imageURL: string | undefined = undefined;
if (data.imageURL) {
imageURL = httpOrigin + data.imageURL;
}
const handleOnDrop = (event: React.DragEvent) => {
onDrop(event, id);
};
useRefreshConnectionHandles(id, data.connectionHandles);
const outsideBottomlabels: JSX.Element[] = [];
if (data.outsideLabels.BOTTOM_BEGIN) {
const outsideLabel = data.outsideLabels.BOTTOM_BEGIN;
outsideBottomlabels.push(
<Label
diagramElementId={id}
label={outsideLabel}
faded={data.faded}
transform=""
gridArea="BOTTOM_BEGIN"
key={outsideLabel.id}
/>
);
}
if (data.outsideLabels.BOTTOM_MIDDLE) {
const outsideLabel = data.outsideLabels.BOTTOM_MIDDLE;
outsideBottomlabels.push(
<Label
diagramElementId={id}
label={outsideLabel}
faded={data.faded}
transform=""
gridArea="BOTTOM_MIDDLE"
key={outsideLabel.id}
/>
);
}
if (data.outsideLabels.BOTTOM_END) {
const outsideLabel = data.outsideLabels.BOTTOM_END;
outsideBottomlabels.push(
<Label
diagramElementId={id}
label={outsideLabel}
faded={data.faded}
transform=""
gridArea="BOTTOM_END"
key={outsideLabel.id}
/>
);
}
return (
<>
{data.nodeDescription?.userResizable && (
<NodeResizer
color={theme.palette.selected}
isVisible={selected}
shouldResize={() => !data.isBorderNode}
keepAspectRatio={data.nodeDescription?.keepAspectRatio}
/>
)}
<div
style={{
...freeFormNodeStyle(theme, data.style, selected, hoveredNode?.id === id, data.faded, rotation, imageURL),
...newConnectionStyleProvider.getNodeStyle(id, data.descriptionId),
...dropFeedbackStyle,
}}
onDragOver={onDragOver}
onDrop={handleOnDrop}
data-testid={`FreeForm - ${data?.targetObjectLabel}`}>
{data.insideLabel ? (
<Label diagramElementId={id} label={data.insideLabel} faded={data.faded} transform="" gridArea="" />
) : null}
{selected ? (
<DiagramElementPalette diagramElementId={id} labelId={data.insideLabel ? data.insideLabel.id : null} />
) : null}
{selected ? <ConnectionCreationHandles nodeId={id} /> : null}
<ConnectionTargetHandle nodeId={id} />
<ConnectionHandles connectionHandles={data.connectionHandles} />
</div>
<div style={{ ...outsideBottomLabelAreaStyle() }}>{outsideBottomlabels}</div>
</>
);
});