|
1 |
| -import { nextTick } from 'vue' |
2 | 1 | import type {
|
3 | 2 | EdgeAddChange,
|
4 | 3 | EdgeChange,
|
| 4 | + EdgeLookup, |
5 | 5 | EdgeRemoveChange,
|
6 | 6 | EdgeSelectionChange,
|
7 |
| - ElementChange, |
8 |
| - FlowElement, |
9 | 7 | GraphEdge,
|
10 | 8 | GraphNode,
|
11 | 9 | NodeAddChange,
|
12 | 10 | NodeChange,
|
| 11 | + NodeLookup, |
13 | 12 | NodeRemoveChange,
|
14 | 13 | NodeSelectionChange,
|
15 |
| - StyleFunc, |
16 | 14 | Styles,
|
17 | 15 | } from '../types'
|
18 |
| -import { isGraphNode } from '.' |
19 | 16 |
|
20 | 17 | function handleParentExpand(updateItem: GraphNode, parent: GraphNode) {
|
21 | 18 | if (parent) {
|
22 |
| - const extendWidth = updateItem.position.x + updateItem.dimensions.width - parent.dimensions.width |
23 |
| - const extendHeight = updateItem.position.y + updateItem.dimensions.height - parent.dimensions.height |
| 19 | + const itemWidth = updateItem.measured.width || 0 |
| 20 | + const itemHeight = updateItem.measured.height || 0 |
| 21 | + const parentWidth = updateItem.measured.width || 0 |
| 22 | + const parentHeight = updateItem.measured.height || 0 |
| 23 | + |
| 24 | + const extendWidth = updateItem.position.x + itemWidth - parentWidth |
| 25 | + const extendHeight = updateItem.position.y + itemHeight - parentHeight |
24 | 26 |
|
25 | 27 | if (extendWidth > 0 || extendHeight > 0 || updateItem.position.x < 0 || updateItem.position.y < 0) {
|
26 | 28 | let parentStyles: Styles = {}
|
27 | 29 |
|
28 |
| - if (typeof parent.style === 'function') { |
29 |
| - parentStyles = { ...parent.style(parent) } |
30 |
| - } else if (parent.style) { |
| 30 | + if (parent.style) { |
31 | 31 | parentStyles = { ...parent.style }
|
32 | 32 | }
|
33 | 33 |
|
34 |
| - parentStyles.width = parentStyles.width ?? `${parent.dimensions.width}px` |
35 |
| - parentStyles.height = parentStyles.height ?? `${parent.dimensions.height}px` |
| 34 | + parentStyles.width = parentStyles.width ?? `${parent.measured.width}px` |
| 35 | + parentStyles.height = parentStyles.height ?? `${parent.measured.height}px` |
36 | 36 |
|
37 | 37 | if (extendWidth > 0) {
|
38 | 38 | if (typeof parentStyles.width === 'string') {
|
@@ -80,136 +80,80 @@ function handleParentExpand(updateItem: GraphNode, parent: GraphNode) {
|
80 | 80 | updateItem.position.y = 0
|
81 | 81 | }
|
82 | 82 |
|
83 |
| - parent.dimensions.width = Number(parentStyles.width.toString().replace('px', '')) |
84 |
| - parent.dimensions.height = Number(parentStyles.height.toString().replace('px', '')) |
85 |
| - |
86 |
| - if (typeof parent.style === 'function') { |
87 |
| - parent.style = (p) => { |
88 |
| - const styleFunc = parent.style as StyleFunc |
89 |
| - |
90 |
| - return { |
91 |
| - ...styleFunc(p), |
92 |
| - ...parentStyles, |
93 |
| - } |
94 |
| - } |
95 |
| - } else { |
96 |
| - parent.style = { |
97 |
| - ...parent.style, |
98 |
| - ...parentStyles, |
99 |
| - } |
| 83 | + parent.width = Number(parentStyles.width.toString().replace('px', '')) |
| 84 | + parent.height = Number(parentStyles.height.toString().replace('px', '')) |
| 85 | + parent.style = { |
| 86 | + ...parent.style, |
| 87 | + ...parentStyles, |
100 | 88 | }
|
101 | 89 | }
|
102 | 90 | }
|
103 | 91 | }
|
104 | 92 |
|
105 |
| -export function applyChanges< |
106 |
| - T extends FlowElement = FlowElement, |
107 |
| - C extends ElementChange = T extends GraphNode ? NodeChange : EdgeChange, |
108 |
| ->(changes: C[], elements: T[]): T[] { |
109 |
| - const addRemoveChanges = changes.filter((c) => c.type === 'add' || c.type === 'remove') as ( |
110 |
| - | NodeAddChange |
111 |
| - | EdgeAddChange |
112 |
| - | NodeRemoveChange |
113 |
| - | EdgeRemoveChange |
114 |
| - )[] |
115 |
| - |
116 |
| - for (const change of addRemoveChanges) { |
| 93 | +export function applyEdgeChanges(changes: EdgeChange[], edgeLookup: EdgeLookup) { |
| 94 | + for (const change of changes) { |
117 | 95 | if (change.type === 'add') {
|
118 |
| - const index = elements.findIndex((el) => el.id === change.item.id) |
| 96 | + edgeLookup.set(change.item.id, change.item) |
| 97 | + } |
119 | 98 |
|
120 |
| - if (index === -1) { |
121 |
| - elements.push(<T>change.item) |
122 |
| - } |
123 |
| - } else if (change.type === 'remove') { |
124 |
| - const index = elements.findIndex((el) => el.id === change.id) |
| 99 | + if (change.type === 'remove') { |
| 100 | + edgeLookup.delete(change.id) |
| 101 | + } |
| 102 | + |
| 103 | + if (change.type === 'select') { |
| 104 | + const edge = edgeLookup.get(change.id) |
125 | 105 |
|
126 |
| - if (index !== -1) { |
127 |
| - elements.splice(index, 1) |
| 106 | + if (edge) { |
| 107 | + edge.selected = change.selected |
128 | 108 | }
|
129 | 109 | }
|
130 | 110 | }
|
131 | 111 |
|
132 |
| - const elementIds = elements.map((el) => el.id) |
| 112 | + return Array.from(edgeLookup.values()) |
| 113 | +} |
| 114 | + |
| 115 | +export function applyNodeChanges(changes: NodeChange[], nodeLookup: NodeLookup) { |
| 116 | + for (const change of changes) { |
| 117 | + if (change.type === 'add') { |
| 118 | + nodeLookup.set(change.item.id, change.item) |
| 119 | + } |
| 120 | + |
| 121 | + if (change.type === 'remove') { |
| 122 | + nodeLookup.delete(change.id) |
| 123 | + } |
| 124 | + |
| 125 | + if (change.type === 'select') { |
| 126 | + const node = nodeLookup.get(change.id) |
133 | 127 |
|
134 |
| - for (const element of elements) { |
135 |
| - for (const currentChange of changes) { |
136 |
| - if ((<any>currentChange).id !== element.id) { |
137 |
| - continue |
| 128 | + if (node) { |
| 129 | + node.selected = change.selected |
138 | 130 | }
|
| 131 | + } |
139 | 132 |
|
140 |
| - switch (currentChange.type) { |
141 |
| - case 'select': |
142 |
| - element.selected = currentChange.selected |
143 |
| - break |
144 |
| - case 'position': |
145 |
| - if (isGraphNode(element)) { |
146 |
| - if (typeof currentChange.position !== 'undefined') { |
147 |
| - element.position = currentChange.position |
148 |
| - } |
149 |
| - |
150 |
| - if (typeof currentChange.dragging !== 'undefined') { |
151 |
| - element.dragging = currentChange.dragging |
152 |
| - } |
153 |
| - |
154 |
| - if (element.expandParent && element.parentNode) { |
155 |
| - const parent = elements[elementIds.indexOf(element.parentNode)] |
156 |
| - |
157 |
| - if (parent && isGraphNode(parent)) { |
158 |
| - handleParentExpand(element, parent) |
159 |
| - } |
160 |
| - } |
161 |
| - } |
162 |
| - break |
163 |
| - case 'dimensions': |
164 |
| - if (isGraphNode(element)) { |
165 |
| - if (typeof currentChange.dimensions !== 'undefined') { |
166 |
| - element.dimensions = currentChange.dimensions |
167 |
| - } |
168 |
| - |
169 |
| - if (typeof currentChange.updateStyle !== 'undefined') { |
170 |
| - element.style = { |
171 |
| - ...(element.style || {}), |
172 |
| - width: `${currentChange.dimensions?.width}px`, |
173 |
| - height: `${currentChange.dimensions?.height}px`, |
174 |
| - } |
175 |
| - } |
176 |
| - |
177 |
| - if (typeof currentChange.resizing !== 'undefined') { |
178 |
| - element.resizing = currentChange.resizing |
179 |
| - } |
180 |
| - |
181 |
| - if (element.expandParent && element.parentNode) { |
182 |
| - const parent = elements[elementIds.indexOf(element.parentNode)] |
183 |
| - |
184 |
| - if (parent && isGraphNode(parent)) { |
185 |
| - const parentInit = !!parent.dimensions.width && !!parent.dimensions.height |
186 |
| - |
187 |
| - if (!parentInit) { |
188 |
| - nextTick(() => { |
189 |
| - handleParentExpand(element, parent) |
190 |
| - }) |
191 |
| - } else { |
192 |
| - handleParentExpand(element, parent) |
193 |
| - } |
194 |
| - } |
195 |
| - } |
| 133 | + if (change.type === 'position') { |
| 134 | + const node = nodeLookup.get(change.id) |
| 135 | + |
| 136 | + if (node) { |
| 137 | + if (typeof change.position !== 'undefined') { |
| 138 | + node.position = change.position |
| 139 | + } |
| 140 | + |
| 141 | + if (typeof change.dragging !== 'undefined') { |
| 142 | + node.dragging = change.dragging |
| 143 | + } |
| 144 | + |
| 145 | + if (node.expandParent && node.parentId) { |
| 146 | + const parent = nodeLookup.get(node.parentId) |
| 147 | + |
| 148 | + if (parent) { |
| 149 | + handleParentExpand(node, parent) |
196 | 150 | }
|
197 |
| - break |
| 151 | + } |
198 | 152 | }
|
199 | 153 | }
|
200 | 154 | }
|
201 | 155 |
|
202 |
| - return elements |
203 |
| -} |
204 |
| - |
205 |
| -/** @deprecated Use store instance and call `applyChanges` with template-ref or the one received by `onPaneReady` instead */ |
206 |
| -export function applyEdgeChanges(changes: EdgeChange[], edges: GraphEdge[]) { |
207 |
| - return applyChanges(changes, edges) |
208 |
| -} |
209 |
| - |
210 |
| -/** @deprecated Use store instance and call `applyChanges` with template-ref or the one received by `onPaneReady` instead */ |
211 |
| -export function applyNodeChanges(changes: NodeChange[], nodes: GraphNode[]) { |
212 |
| - return applyChanges(changes, nodes) |
| 156 | + return Array.from(nodeLookup.values()) |
213 | 157 | }
|
214 | 158 |
|
215 | 159 | export function createSelectionChange(id: string, selected: boolean): NodeSelectionChange | EdgeSelectionChange {
|
|
0 commit comments