-
Notifications
You must be signed in to change notification settings - Fork 45
/
ImageNode.tsx
98 lines (92 loc) · 3.7 KB
/
ImageNode.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
/*******************************************************************************
* 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 } from '@eclipse-sirius/sirius-components-core';
import { Theme, useTheme } from '@material-ui/core/styles';
import { memo, useContext } from 'react';
import { NodeProps, NodeResizer } from 'reactflow';
import { BorderNodePositon } from '../DiagramRenderer.types';
import { Label } from '../Label';
import { useConnector } from '../connector/useConnector';
import { useDropNode } from '../dropNode/useDropNode';
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 { ImageNodeData } from './ImageNode.types';
const imageNodeStyle = (
theme: Theme,
style: React.CSSProperties,
selected: boolean,
faded: boolean,
rotation: string | undefined
): React.CSSProperties => {
const imageNodeStyle: React.CSSProperties = {
width: '100%',
height: '100%',
opacity: faded ? '0.4' : '',
transform: rotation,
...style,
};
if (selected) {
imageNodeStyle.outline = `${theme.palette.primary.main} solid 1px`;
}
return imageNodeStyle;
};
const computeBorderRotation = (data: ImageNodeData): string | undefined => {
if (data?.isBorderNode && data.positionDependentRotation) {
switch (data.borderNodePosition) {
case BorderNodePositon.NORTH:
return 'rotate(90deg)';
case BorderNodePositon.EAST:
return 'rotate(180deg)';
case BorderNodePositon.SOUTH:
return 'rotate(270deg)';
default:
return undefined;
}
}
return undefined;
};
export const ImageNode = memo(({ data, id, selected }: NodeProps<ImageNodeData>) => {
const theme = useTheme();
const { dropFeedbackStyleProvider } = useDropNode();
const { httpOrigin } = useContext<ServerContextValue>(ServerContext);
const { newConnectionStyleProvider } = useConnector();
const rotation = computeBorderRotation(data);
useRefreshConnectionHandles(id, data.connectionHandles);
return (
<>
<NodeResizer
color={theme.palette.primary.main}
isVisible={selected && !data.isBorderNode}
shouldResize={() => !data.isBorderNode}
keepAspectRatio={data.nodeDescription?.keepAspectRatio}
/>
<img
src={httpOrigin + data.imageURL}
style={{
...imageNodeStyle(theme, data.style, selected, data.faded, rotation),
...newConnectionStyleProvider.getNodeStyle(id, data.descriptionId),
...dropFeedbackStyleProvider.getNodeStyle(id),
}}
data-testid={`Image - ${data?.targetObjectLabel}`}
/>
{data.label ? <Label diagramElementId={id} label={data.label} faded={data.faded} transform="" /> : null}
{selected ? <DiagramElementPalette diagramElementId={id} labelId={data.label ? data.label.id : null} /> : null}
{selected ? <ConnectionCreationHandles nodeId={id} /> : null}
<ConnectionTargetHandle nodeId={id} />
<ConnectionHandles nodeId={id} connectionHandles={data.connectionHandles} />
</>
);
});