@@ -14,6 +14,7 @@ import { cancelRaf, createRaf } from '../../util/raf';
14
14
import { EventArgs } from '../../type' ;
15
15
import RotateControlPoint from '../Rotate' ;
16
16
import { TranslateMatrix , RotateMatrix } from '../../util' ;
17
+ import { reaction , IReactionDisposer } from '../../util/mobx' ;
17
18
18
19
type IProps = {
19
20
model : BaseNodeModel ;
@@ -36,6 +37,7 @@ export default abstract class BaseNode extends Component<IProps, IState> {
36
37
contextMenuTime : number ;
37
38
startTime : number ;
38
39
clickTimer : number ;
40
+ modelDisposer : IReactionDisposer ;
39
41
constructor ( props ) {
40
42
super ( ) ;
41
43
const {
@@ -52,6 +54,21 @@ export default abstract class BaseNode extends Component<IProps, IState> {
52
54
eventCenter,
53
55
model,
54
56
} ) ;
57
+ // https://github.com/didi/LogicFlow/issues/1370
58
+ // 当使用撤销功能:LogicFlow.undo()时,会重新初始化所有model数据,即LogicFlow.undo()时会新构建一个model对象
59
+ // 但是this.stepDrag并不会重新创建
60
+ // 导致this.stepDrag持有的model并没有重新赋值,因为之前的做法是构造函数中传入一个model对象
61
+ // 使用mobx的reaction监听能力,如果this.props.model发生变化,则进行this.stepDrag.setModel()操作
62
+ this . modelDisposer = reaction ( ( ) => this . props , ( newProps ) => {
63
+ if ( newProps && newProps . model ) {
64
+ this . stepDrag . setModel ( newProps . model ) ;
65
+ }
66
+ } ) ;
67
+ }
68
+ componentWillUnmount ( ) {
69
+ if ( this . modelDisposer ) {
70
+ this . modelDisposer ( ) ;
71
+ }
55
72
}
56
73
abstract getShape ( ) ;
57
74
getAnchorShape ( anchorData ) : h . JSX . Element {
@@ -324,13 +341,6 @@ export default abstract class BaseNode extends Component<IProps, IState> {
324
341
this . startTime = new Date ( ) . getTime ( ) ;
325
342
const { editConfigModel } = graphModel ;
326
343
if ( editConfigModel . adjustNodePosition && model . draggable ) {
327
- // https://github.com/didi/LogicFlow/issues/1370
328
- // 当使用撤销功能:LogicFlow.undo()时,会重新初始化所有model数据,即LogicFlow.undo()时会新构建一个model对象
329
- // 但是this.stepDrag并不会重新创建
330
- // 导致this.stepDrag持有的model并没有重新赋值,因为之前的做法是构造函数中传入一个model对象
331
- // 在StepDrag.ts中只有handleMouseDown、handleMouseMove、handleMouseUp使用到this.model
332
- // 因此在handleMouseDown()进行setModel重新将this.props.model的值设置进去,刷新this.model.getData()
333
- this . stepDrag && this . stepDrag . setModel ( model ) ;
334
344
this . stepDrag && this . stepDrag . handleMouseDown ( ev ) ;
335
345
}
336
346
} ;
0 commit comments