@@ -3,9 +3,8 @@ import { Observer } from '../../pcui-binding-external';
33import { diff } from 'json-diff' ;
44import { deepCopyFunction } from './util' ;
55import GraphView from './graph-view' ;
6- import { Vec2 } from 'playcanvas' ;
76import './style.scss' ;
8- // import './style-story.scss';
7+ import './style-story.scss' ;
98
109
1110export var GRAPH_ACTIONS = {
@@ -25,7 +24,8 @@ export var GRAPH_ACTIONS = {
2524var defaultConfig = {
2625 edgeHoverEffect : true ,
2726 passiveUIEvents : false ,
28- readOnly : false
27+ readOnly : false ,
28+ restrictTranslate : false
2929} ;
3030
3131class SelectedItem {
@@ -157,10 +157,21 @@ class Graph extends Element {
157157 }
158158 } ) ;
159159 }
160+ let element = e . target ;
161+ while ( ! element . classList . contains ( 'pcui-contextmenu' ) ) {
162+ element = element . parentElement ;
163+ }
164+ var pos = {
165+ x : Number ( element . style . left . replace ( 'px' , '' ) ) ,
166+ y : Number ( element . style . top . replace ( 'px' , '' ) )
167+ } ;
168+ pos = this . getWindowToGraphPosition ( pos ) ;
169+ node . posX = pos . x ;
170+ node . posY = pos . y ;
160171 if ( this . _config . passiveUIEvents ) {
161172 this . dom . dispatchEvent ( new CustomEvent ( GRAPH_ACTIONS . ADD_NODE , { detail : node } ) ) ;
162173 } else {
163- this . createNode ( node , e ) ;
174+ this . createNode ( node ) ;
164175 this . _graphData . set ( `data.nodes.${ node . id } ` , node ) ;
165176 this . selectNode ( node ) ;
166177 }
@@ -312,7 +323,7 @@ class Graph extends Element {
312323 var node = this . _graphData . get ( `data.nodes.${ nodeId } ` ) ;
313324 var prevPosX = node . posX ;
314325 var prevPosY = node . posY ;
315- if ( new Vec2 ( pos . x , pos . y ) . sub ( new Vec2 ( node . posX , node . posY ) ) . length ( ) > 5 ) {
326+ if ( pos . x !== node . posX || pos . y !== node . posY ) {
316327 node . posX = pos . x ;
317328 node . posY = pos . y ;
318329 if ( ! this . _config . passiveUIEvents ) {
@@ -324,35 +335,43 @@ class Graph extends Element {
324335 }
325336 }
326337
327- onNodeAttributeUpdated ( nodeId , attribute , value ) {
338+ _onNodeAttributeUpdated ( nodeId , attribute , value ) {
328339 var node = this . _graphData . get ( `data.nodes.${ nodeId } ` ) ;
329- if ( value === node . attributes [ attribute . name ] ) return ;
330340 var prevAttributeValue ;
331- if ( Number . isFinite ( node . attributes [ attribute . name ] . x ) ) {
332- prevAttributeValue = { ...node . attributes [ attribute . name ] } ;
341+ let attributeKey = node . attributes [ attribute . name ] ? attribute . name : undefined ;
342+ if ( ! attributeKey ) {
343+ Object . keys ( node . attributes ) . forEach ( k => {
344+ const item = node . attributes [ k ] ;
345+ if ( item . name === attribute . name ) attributeKey = k ;
346+ } ) ;
347+ }
348+ if ( Number . isFinite ( node . attributes [ attributeKey ] . x ) ) {
349+ prevAttributeValue = { ...node . attributes [ attributeKey ] } ;
333350 } else {
334- prevAttributeValue = node . attributes [ attribute . name ] ;
351+ prevAttributeValue = node . attributes [ attributeKey ] ;
335352 }
336353 if ( Array . isArray ( value ) ) {
337354 var keyMap = [ 'x' , 'y' , 'z' , 'w' ] ;
338355 value . forEach ( ( v , i ) => {
339- node . attributes [ attribute . name ] [ keyMap [ i ] ] = v ;
356+ node . attributes [ attributeKey ] [ keyMap [ i ] ] = v ;
340357 } ) ;
341358 } else {
342- node . attributes [ attribute . name ] = value ;
359+ node . attributes [ attributeKey ] = value ;
343360 }
344- if ( ! this . _config . passiveUIEvents ) {
345- this . _graphData . set ( `data.nodes.${ nodeId } ` , node ) ;
346- } else {
361+ if ( JSON . stringify ( node . attributes [ attributeKey ] ) === JSON . stringify ( prevAttributeValue ) ) return ;
362+ if ( this . _config . passiveUIEvents ) {
347363 this . updateNodeAttribute ( nodeId , attribute . name , prevAttributeValue ) ;
364+ } else {
365+ this . _graphData . set ( `data.nodes.${ nodeId } ` , node ) ;
348366 }
349367 this . dom . dispatchEvent (
350368 new CustomEvent (
351369 GRAPH_ACTIONS . UPDATE_NODE_ATTRIBUTE ,
352370 {
353371 detail : {
354372 node : node ,
355- attribute : attribute . name
373+ attribute : attribute . name ,
374+ attributeKey : attributeKey
356375 }
357376 }
358377 )
@@ -396,7 +415,7 @@ class Graph extends Element {
396415 this . view . addNodeEvent (
397416 node . id ,
398417 `updateAttribute` ,
399- this . onNodeAttributeUpdated . bind ( this ) ,
418+ this . _onNodeAttributeUpdated . bind ( this ) ,
400419 attribute
401420 ) ;
402421 } ) ;
@@ -414,7 +433,7 @@ class Graph extends Element {
414433
415434 updateNodeAttribute ( nodeId , attribute , value ) {
416435 if ( ! this . _graphData . get ( `data.nodes.${ nodeId } ` ) ) return ;
417- this . _graphData . set ( `data.nodes.${ nodeId } .${ attribute } ` , value ) ;
436+ this . _graphData . set ( `data.nodes.${ nodeId } .attributes. ${ attribute } ` , value ) ;
418437 this . view . updateNodeAttribute ( nodeId , attribute , value ) ;
419438 }
420439
@@ -492,6 +511,14 @@ class Graph extends Element {
492511 return this . view . getGraphScale ( ) ;
493512 }
494513
514+ getWindowToGraphPosition ( pos ) {
515+ return this . view . getWindowToGraphPosition ( pos ) ;
516+ }
517+
518+ getGraphPosition ( pos ) {
519+ return this . view . getGraphPosition ( pos ) ;
520+ }
521+
495522 on ( eventName , callback ) {
496523 if ( this . _config . readOnly && ( ! eventName . includes ( 'EVENT_SELECT_' ) && ! eventName . includes ( 'EVENT_DESELECT' ) ) ) return ;
497524 this . dom . addEventListener ( eventName , ( e ) => {
0 commit comments