@@ -380,23 +380,7 @@ class GraphModel {
380
380
return ;
381
381
}
382
382
if ( graphData . nodes ) {
383
- this . nodes = map ( graphData . nodes , node => {
384
- const Model = this . getModel ( node . type ) ;
385
- if ( ! Model ) {
386
- throw new Error ( `找不到${ node . type } 对应的节点。` ) ;
387
- }
388
- const { x : nodeX , y : nodeY } = node ;
389
- // 根据 grid 修正节点的 x, y
390
- if ( nodeX && nodeY ) {
391
- node . x = snapToGrid ( nodeX , this . gridSize ) ;
392
- node . y = snapToGrid ( nodeY , this . gridSize ) ;
393
- if ( typeof node . text === 'object' ) {
394
- node . text . x -= getGridOffset ( nodeX , this . gridSize ) ;
395
- node . text . y -= getGridOffset ( nodeY , this . gridSize ) ;
396
- }
397
- }
398
- return new Model ( node , this ) ;
399
- } ) ;
383
+ this . nodes = map ( graphData . nodes , ( node : NodeConfig ) => this . getModelAfterSnapToGrid ( node ) ) ;
400
384
} else {
401
385
this . nodes = [ ] ;
402
386
}
@@ -681,13 +665,7 @@ class GraphModel {
681
665
if ( nodeOriginData . id && this . nodesMap [ nodeConfig . id ] ) {
682
666
delete nodeOriginData . id ;
683
667
}
684
- const Model = this . getModel ( nodeOriginData . type ) ;
685
- if ( ! Model ) {
686
- throw new Error ( `找不到${ nodeOriginData . type } 对应的节点,请确认是否已注册此类型节点。` ) ;
687
- }
688
- nodeOriginData . x = snapToGrid ( nodeOriginData . x , this . gridSize ) ;
689
- nodeOriginData . y = snapToGrid ( nodeOriginData . y , this . gridSize ) ;
690
- const nodeModel = new Model ( nodeOriginData , this ) ;
668
+ const nodeModel = this . getModelAfterSnapToGrid ( nodeOriginData ) ;
691
669
this . nodes . push ( nodeModel ) ;
692
670
const nodeData = nodeModel . getData ( ) ;
693
671
const eventData : Record < string , any > = { data : nodeData } ;
@@ -697,7 +675,35 @@ class GraphModel {
697
675
this . eventCenter . emit ( eventType , eventData ) ;
698
676
return nodeModel ;
699
677
}
700
-
678
+ /**
679
+ * 将node节点位置进行grid修正
680
+ * 同时处理node内文字的偏移量
681
+ * 返回一个位置修正过的复制节点NodeModel
682
+ * @param node
683
+ */
684
+ getModelAfterSnapToGrid ( node : NodeConfig ) {
685
+ const Model = this . getModel ( node . type ) ;
686
+ if ( ! Model ) {
687
+ throw new Error ( `找不到${ node . type } 对应的节点,请确认是否已注册此类型节点。` ) ;
688
+ }
689
+ const { x : nodeX , y : nodeY } = node ;
690
+ // 根据 grid 修正节点的 x, y
691
+ if ( nodeX && nodeY ) {
692
+ node . x = snapToGrid ( nodeX , this . gridSize ) ;
693
+ node . y = snapToGrid ( nodeY , this . gridSize ) ;
694
+ if ( typeof node . text === 'object' ) {
695
+ // 原来的处理是:node.text.x -= getGridOffset(nodeX, this.gridSize);
696
+ // 由于snapToGrid()使用了Math.round()四舍五入的做法,因此无法判断需要执行
697
+ // node.text.x = node.text.x + getGridOffset()
698
+ // 还是
699
+ // node.text.x = node.text.x - getGridOffset()
700
+ // 直接改为node.x - nodeX就可以满足上面的要求
701
+ node . text . x += ( node . x - nodeX ) ;
702
+ node . text . y += ( node . y - nodeY ) ;
703
+ }
704
+ }
705
+ return new Model ( node , this ) ;
706
+ }
701
707
/**
702
708
* 克隆节点
703
709
* @param nodeId 节点Id
0 commit comments