Skip to content

Commit a1e3bc3

Browse files
wbccbwumail
authored andcommitted
fix(core): use mobx reaction to track the value of stepDrag.model(#1370)
1 parent 1cb45dd commit a1e3bc3

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

packages/core/src/util/mobx.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { action, observable, computed, toJS, isObservable, configure } from 'mobx';
1+
import { action, observable, computed, toJS, isObservable, configure, reaction, IReactionDisposer } from 'mobx';
22

33
configure({ isolateGlobalState: true });
44

@@ -9,4 +9,6 @@ export {
99
isObservable,
1010
toJS,
1111
configure,
12+
reaction,
13+
IReactionDisposer,
1214
};

packages/core/src/view/node/BaseNode.tsx

+17-7
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { cancelRaf, createRaf } from '../../util/raf';
1414
import { EventArgs } from '../../type';
1515
import RotateControlPoint from '../Rotate';
1616
import { TranslateMatrix, RotateMatrix } from '../../util';
17+
import { reaction, IReactionDisposer } from '../../util/mobx';
1718

1819
type IProps = {
1920
model: BaseNodeModel;
@@ -36,6 +37,7 @@ export default abstract class BaseNode extends Component<IProps, IState> {
3637
contextMenuTime: number;
3738
startTime: number;
3839
clickTimer: number;
40+
modelDisposer: IReactionDisposer;
3941
constructor(props) {
4042
super();
4143
const {
@@ -52,6 +54,21 @@ export default abstract class BaseNode extends Component<IProps, IState> {
5254
eventCenter,
5355
model,
5456
});
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+
}
5572
}
5673
abstract getShape();
5774
getAnchorShape(anchorData): h.JSX.Element {
@@ -324,13 +341,6 @@ export default abstract class BaseNode extends Component<IProps, IState> {
324341
this.startTime = new Date().getTime();
325342
const { editConfigModel } = graphModel;
326343
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);
334344
this.stepDrag && this.stepDrag.handleMouseDown(ev);
335345
}
336346
};

0 commit comments

Comments
 (0)