1
1
// import {MetaNodeModel} from "../react-diagrams/MetaNodeModel";
2
2
import { MetaLink , MetaNodeModel , MetaLinkModel } from "@metacell/meta-diagram"
3
+ import { PNLClasses } from "../../constants" ;
3
4
4
5
class Graph {
5
6
private readonly node : MetaNodeModel ;
@@ -26,6 +27,10 @@ class Graph {
26
27
this . children . set ( graph . getID ( ) , graph )
27
28
}
28
29
30
+ deleteChild ( id : string ) : void {
31
+ this . children . delete ( id ) ;
32
+ }
33
+
29
34
getChildren ( ) : MetaNodeModel [ ] {
30
35
return Array . from ( this . children . values ( ) ) . map ( g => g . getNode ( ) )
31
36
}
@@ -60,10 +65,12 @@ class Graph {
60
65
export class MetaGraph {
61
66
private readonly roots : Map < string , Graph > ;
62
67
private readonly links : MetaLinkModel [ ] ;
68
+ private parentUpdating : boolean ;
63
69
64
70
constructor ( ) {
65
71
this . roots = new Map < string , Graph > ( )
66
72
this . links = [ ] ;
73
+ this . parentUpdating = false ;
67
74
}
68
75
69
76
addLinks ( links : MetaLink [ ] ) {
@@ -170,28 +177,82 @@ export class MetaGraph {
170
177
return parent
171
178
}
172
179
173
- handleNodePositionChanged ( metaNodeModel : MetaNodeModel , cursorX : number , cursorY : number ) {
180
+ private findParentNodeGraph ( path : string [ ] ) : Graph {
181
+ const newPath = [ ...path ] ;
182
+ newPath . pop ( ) ;
183
+ return this . findNodeGraph ( newPath ) ;
184
+ }
185
+
186
+ updateGraph ( metaNodeModel : MetaNodeModel , cursorX : number , cursorY : number ) {
187
+ this . updateNodeContainerBoundingBox ( metaNodeModel ) ;
188
+ let parent : MetaNodeModel | undefined = this . rootContainsNode ( metaNodeModel , cursorX , cursorY ) ;
189
+ let newPath = this . findNewPath ( metaNodeModel , parent , cursorX , cursorY ) ;
190
+ if ( metaNodeModel . getGraphPath ( ) . join ( ) . toString ( ) !== newPath . join ( ) . toString ( ) ) {
191
+ this . updateNodeInGraph ( metaNodeModel , newPath ) ;
192
+ }
193
+ this . handleNodePositionChanged ( metaNodeModel ) ;
194
+ }
195
+
196
+ handleNodePositionChanged ( metaNodeModel : MetaNodeModel ) {
174
197
// TODO: Update node parent (add or remove parent)
175
198
// update node graph path,
176
199
// bounding boxes of parents
177
200
201
+ // update the graph for right parent children relationship
202
+ this . updateNodeContainerBoundingBox ( metaNodeModel ) ;
178
203
// Update children position (children should move the same delta as node)
179
204
this . updateChildrenPosition ( metaNodeModel )
180
205
// Update local position / relative position to the parent
181
206
this . updateNodeLocalPosition ( metaNodeModel )
182
- // update the graph for right parent children relationship
183
- this . updateGraph ( metaNodeModel , cursorX , cursorY ) ;
184
207
}
185
208
186
- updateGraph ( metaNodeModel : MetaNodeModel , cursorX : number , cursorY : number ) {
187
- let parent = undefined ;
188
- let search = true ;
189
- this . roots . forEach ( ( node , id ) => {
190
- if ( node . getContainerBoundingBox ( ) . containsPoint ( cursorX , cursorY ) ) {
209
+ rootContainsNode ( metaNodeModel : MetaNodeModel , cursorX : number , cursorY : number ) : MetaNodeModel | undefined {
210
+ let parent = undefined
211
+ this . roots . forEach ( ( graph , id ) => {
212
+ const node = graph . getNode ( ) ;
213
+ if ( node . getID ( ) !== metaNodeModel . getID ( )
214
+ && node . getOption ( 'shape' ) === PNLClasses . COMPOSITION
215
+ && node . getNodeBoundingBox ( ) . containsPoint ( cursorX , cursorY ) )
216
+ {
191
217
parent = node ;
192
218
}
193
219
} ) ;
194
- // TODO add the new child to the graph and update graphPath for the metaNodeModel instance
220
+ return parent ;
221
+ }
222
+
223
+ findNewPath ( metaNodeModel : MetaNodeModel , parent : MetaNodeModel | undefined , cursorX : number , cursorY : number ) {
224
+ let search : boolean = true ;
225
+ let newPath : string [ ] = [ ] ;
226
+ while ( search && parent ) {
227
+ search = false ;
228
+ const children = this . getChildren ( parent ) ;
229
+ // eslint-disable-next-line no-loop-func
230
+ children . forEach ( ( child : MetaNodeModel ) => {
231
+ if ( ! search
232
+ && child . getID ( ) !== metaNodeModel . getID ( )
233
+ && child . getOption ( 'shape' ) === PNLClasses . COMPOSITION
234
+ && child . getNodeBoundingBox ( ) . containsPoint ( cursorX , cursorY ) )
235
+ {
236
+ search = true ;
237
+ parent = child ;
238
+ }
239
+ } ) ;
240
+ // @ts -ignore
241
+ newPath = parent . getGraphPath ( ) ;
242
+ }
243
+ return [ ...newPath , metaNodeModel . getID ( ) ] ;
244
+ }
245
+
246
+ updateNodeInGraph ( metaNodeModel : MetaNodeModel , newPath : string [ ] ) {
247
+ const oldPath = metaNodeModel . getGraphPath ( ) ;
248
+ if ( oldPath . length === 1 ) {
249
+ this . roots . delete ( oldPath [ 0 ] ) ;
250
+ } else {
251
+ let parentGraph = this . findParentNodeGraph ( oldPath ) ;
252
+ parentGraph . deleteChild ( metaNodeModel . getID ( ) ) ;
253
+ }
254
+ metaNodeModel . setOption ( 'graphPath' , newPath ) ;
255
+ this . addNode ( metaNodeModel ) ;
195
256
}
196
257
197
258
private updateChildrenPosition ( metaNodeModel : MetaNodeModel ) {
@@ -204,7 +265,7 @@ export class MetaGraph {
204
265
*/
205
266
// @ts -ignore
206
267
const localPosition = n . getLocalPosition ( )
207
- n . setPosition ( metaNodeModel . getX ( ) + localPosition . x , metaNodeModel . getY ( ) + localPosition . y )
268
+ n . setNodePosition ( metaNodeModel . getX ( ) + localPosition . x , metaNodeModel . getY ( ) + localPosition . y )
208
269
209
270
} )
210
271
}
@@ -214,7 +275,12 @@ export class MetaGraph {
214
275
metaNodeModel . updateLocalPosition ( parent )
215
276
}
216
277
217
- updateNodesContainerBoundingBoxes ( nodes : MetaNodeModel [ ] ) : void {
218
- nodes . forEach ( n => n . setContainerBoundingBox ( this . getNodeContainerBoundingBox ( n ) ) )
278
+ updateNodeContainerBoundingBox ( node : MetaNodeModel ) : void {
279
+ node . setContainerBoundingBox ( {
280
+ left : node . getX ( ) ,
281
+ top : node . getY ( ) ,
282
+ bottom : node . getY ( ) + node . height ,
283
+ right : node . getX ( ) + node . width
284
+ } ) ;
219
285
}
220
286
}
0 commit comments