-
Notifications
You must be signed in to change notification settings - Fork 45
/
ListNodeConverterHandler.ts
144 lines (133 loc) · 4.73 KB
/
ListNodeConverterHandler.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
/*******************************************************************************
* 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 { Node, XYPosition } from 'reactflow';
import { GQLNodeDescription } from '../graphql/query/nodeDescriptionFragment.types';
import { GQLEdge } from '../graphql/subscription/edgeFragment.types';
import {
GQLNode,
GQLNodeStyle,
GQLRectangularNodeStyle,
GQLViewModifier,
} from '../graphql/subscription/nodeFragment.types';
import { BorderNodePositon } from '../renderer/DiagramRenderer.types';
import { ConnectionHandle } from '../renderer/handles/ConnectionHandles.types';
import { ListNodeData } from '../renderer/node/ListNode.types';
import { IConvertEngine, INodeConverterHandler } from './ConvertEngine.types';
import { convertLabelStyle } from './convertDiagram';
import { AlignmentMap } from './convertDiagram.types';
import { convertHandles } from './convertHandles';
const defaultPosition: XYPosition = { x: 0, y: 0 };
const toListNode = (
gqlNode: GQLNode<GQLRectangularNodeStyle>,
gqlParentNode: GQLNode<GQLNodeStyle> | null,
nodeDescription: GQLNodeDescription | undefined,
isBorderNode: boolean,
gqlEdges: GQLEdge[]
): Node<ListNodeData> => {
const {
targetObjectId,
targetObjectLabel,
targetObjectKind,
descriptionId,
insideLabel,
id,
state,
style,
labelEditable,
} = gqlNode;
const connectionHandles: ConnectionHandle[] = convertHandles(gqlNode, gqlEdges);
const data: ListNodeData = {
targetObjectId,
targetObjectLabel,
targetObjectKind,
descriptionId,
style: {
backgroundColor: style.color,
borderColor: style.borderColor,
borderRadius: style.borderRadius,
borderWidth: style.borderSize,
borderStyle: style.borderStyle,
},
label: undefined,
isBorderNode: isBorderNode,
borderNodePosition: isBorderNode ? BorderNodePositon.WEST : null,
faded: state === GQLViewModifier.Faded,
labelEditable,
nodeDescription,
connectionHandles,
defaultWidth: gqlNode.defaultWidth,
defaultHeight: gqlNode.defaultHeight,
};
if (insideLabel) {
const labelStyle = insideLabel.style;
data.label = {
id: insideLabel.id,
text: insideLabel.text,
iconURL: labelStyle.iconURL,
isHeader: insideLabel.isHeader,
displayHeaderSeparator: insideLabel.displayHeaderSeparator,
style: {
display: 'flex',
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'center',
padding: '8px 16px',
textAlign: 'center',
...convertLabelStyle(labelStyle),
},
};
const alignement = AlignmentMap[insideLabel.insideLabelLocation];
if (alignement.isPrimaryVerticalAlignment) {
if (alignement.primaryAlignment === 'TOP') {
if (data.label.displayHeaderSeparator) {
data.label.style.borderBottom = `${style.borderSize}px ${style.borderStyle} ${style.borderColor}`;
}
data.style = { ...data.style, display: 'flex', flexDirection: 'column', justifyContent: 'flex-start' };
}
if (alignement.secondaryAlignment === 'CENTER') {
data.style = { ...data.style, alignItems: 'stretch' };
data.label.style = { ...data.label.style, justifyContent: 'center' };
}
}
}
const node: Node<ListNodeData> = {
id,
type: 'listNode',
data,
position: defaultPosition,
hidden: state === GQLViewModifier.Hidden,
};
if (gqlParentNode) {
node.parentNode = gqlParentNode.id;
}
return node;
};
export class ListNodeConverterHandler implements INodeConverterHandler {
canHandle(gqlNode: GQLNode<GQLNodeStyle>) {
return gqlNode.style.__typename === 'RectangularNodeStyle' && gqlNode.childrenLayoutStrategy?.kind === 'List';
}
handle(
convertEngine: IConvertEngine,
gqlNode: GQLNode<GQLRectangularNodeStyle>,
gqlEdges: GQLEdge[],
parentNode: GQLNode<GQLNodeStyle> | null,
isBorderNode: boolean,
nodes: Node[],
nodeDescriptions: GQLNodeDescription[]
) {
const nodeDescription = nodeDescriptions.find((description) => description.id === gqlNode.descriptionId);
nodes.push(toListNode(gqlNode, parentNode, nodeDescription, isBorderNode, gqlEdges));
convertEngine.convertNodes(gqlNode.borderNodes ?? [], gqlNode, nodes, nodeDescriptions);
convertEngine.convertNodes(gqlNode.childNodes ?? [], gqlNode, nodes, nodeDescriptions);
}
}