Skip to content

Commit 0a6109c

Browse files
committed
Add clear method, make skipsDiagramUpdate optional, update packages
1 parent 39080ec commit 0a6109c

File tree

7 files changed

+493
-304
lines changed

7 files changed

+493
-304
lines changed

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# gojs-react Change Log
22

3+
## 1.1.0
4+
- [ReactDiagram.skipsDiagramUpdate](https://github.com/NorthwoodsSoftware/gojs-react#optional---skipsdiagramupdate-reactdiagram-only) is now an optional prop.
5+
We still encourage using it in most cases.
6+
- Added [clear](https://github.com/NorthwoodsSoftware/gojs-react#clear-reactdiagram-and-reactpalette-only) method to allow clearing of the diagram and treating the next state update as a Diagram reinitialization.
7+
38
## 1.0.10
49
- Model data is now merged into the GoJS model before node and link data in case any ofModel bindings depend upon it.
510

@@ -15,5 +20,5 @@ new data as necessary.
1520
- The GoJS model change listener is now active during model initialization and any merges of data.
1621
This means all changes that take place in GoJS - including side effects like laying out newly added nodes -
1722
will call the [onModelChange](https://github.com/NorthwoodsSoftware/gojs-react#optional---onmodelchange-reactdiagram-only) handler.
18-
Make sure to set [skipsDiagramUpdate](https://github.com/NorthwoodsSoftware/gojs-react#skipsdiagramupdate-reactdiagram-only)
23+
Make sure to set [skipsDiagramUpdate](https://github.com/NorthwoodsSoftware/gojs-react#optional---skipsdiagramupdate-reactdiagram-only)
1924
to true when making state updates in that handler.

IMPLEMENTATION.md

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -37,24 +37,12 @@ public componentDidMount() {
3737

3838
// delay initialization of the diagram so all initial model data is merged before any animations/layouts
3939
diagram.delayInitialization(() => {
40-
const model = diagram.model;
41-
model.commit((m: go.Model) => {
42-
if (this.props.modelData !== undefined) {
43-
m.assignAllDataProperties(m.modelData, this.props.modelData);
44-
}
45-
m.mergeNodeDataArray(this.props.nodeDataArray);
46-
if (this.props.linkDataArray !== undefined && m instanceof go.GraphLinksModel) {
47-
m.mergeLinkDataArray(this.props.linkDataArray);
48-
}
49-
}, 'gojs-react init merge');
40+
this.mergeData(diagram, true);
5041
});
5142
}
5243
```
5344

5445
### Updating the GoJS Model based on React state changes
55-
#### componentDidUpdate
56-
The componentDidUpdate method is where any changes to React state are merged into the GoJS model.
57-
5846
When state is updated in React, it is important to keep the GoJS model up-to-date.
5947
The methods used to do this are [Model.mergeNodeDataArray](https://gojs.net/latest/api/symbols/Model.html#mergeNodeDataArray) and
6048
[GraphLinksModel.mergeLinkDataArray](https://gojs.net/latest/api/symbols/GraphLinksModel.html#mergeLinkDataArray).
@@ -64,6 +52,9 @@ using [Model.cloneDeep](https://gojs.net/latest/api/symbols/Model.html#cloneDeep
6452

6553
_Properties should not be removed, but rather set to undefined if they are no longer needed; GoJS avoids destructive merging._
6654

55+
#### componentDidUpdate
56+
The componentDidUpdate method calls mergeData where any changes to React state are merged into the GoJS model.
57+
6758
```ts
6859
/**
6960
* When the component updates, merge all data changes into the GoJS model to ensure everything stays in sync.
@@ -73,16 +64,15 @@ _Properties should not be removed, but rather set to undefined if they are no lo
7364
public componentDidUpdate(prevProps: DiagramProps, prevState: any) {
7465
const diagram = this.getDiagram();
7566
if (diagram !== null) {
76-
const model = diagram.model;
77-
model.startTransaction('update data');
78-
if (this.props.modelData !== undefined) {
79-
model.assignAllDataProperties(model.modelData, this.props.modelData);
80-
}
81-
model.mergeNodeDataArray(this.props.nodeDataArray);
82-
if (this.props.linkDataArray !== undefined && model instanceof go.GraphLinksModel) {
83-
model.mergeLinkDataArray(this.props.linkDataArray);
67+
// if clear was just called, treat this as initial
68+
if (this.wasCleared) {
69+
diagram.delayInitialization(() => {
70+
this.mergeData(diagram, true);
71+
this.wasCleared = false;
72+
});
73+
} else {
74+
this.mergeData(diagram, false);
8475
}
85-
model.commitTransaction('update data');
8676
}
8777
}
8878
```
@@ -108,6 +98,24 @@ public shouldComponentUpdate(nextProps: DiagramProps, nextState: any) {
10898
}
10999
```
110100

101+
#### mergeData
102+
The mergeData method is where any changes to React state are merged into the GoJS model. It is called by componentDidMount and componentDidUpdate.
103+
104+
```ts
105+
private mergeData(diagram: go.Diagram, isInit: boolean) {
106+
const model = diagram.model;
107+
model.commit((m: go.Model) => {
108+
if (this.props.modelData !== undefined) {
109+
m.assignAllDataProperties(m.modelData, this.props.modelData);
110+
}
111+
m.mergeNodeDataArray(this.props.nodeDataArray);
112+
if (this.props.linkDataArray !== undefined && m instanceof go.GraphLinksModel) {
113+
m.mergeLinkDataArray(this.props.linkDataArray);
114+
}
115+
}, isInit ? null : 'merge data');
116+
}
117+
```
118+
111119
### Tearing down the GoJS Diagram
112120
#### componentWillUnmount
113121
The componentWillUnmount method is responsible for tearing down the diagram and all listeners.

README.md

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,8 @@ Specifies a modelData object for the Diagram's model, only necessary when using
142142
that will be shared by the model as a whole.
143143
See [Model.modelData](https://gojs.net/latest/api/symbols/Model.html#modelData).
144144

145-
#### skipsDiagramUpdate (ReactDiagram only)
146-
Specifies whether the component should skip updating, often set to true when updating state from a GoJS model change.
145+
#### Optional - skipsDiagramUpdate (ReactDiagram only)
146+
Specifies whether the component should skip updating, often set to true when updating state originating from a GoJS model change.
147147
This flag is checked during shouldComponentUpdate. Because GoJS Palettes are read-only by default,
148148
this prop is not present on ReactPalette.
149149

@@ -177,6 +177,42 @@ function handleModelChange(data) {
177177
#### observedDiagram (ReactOverview only)
178178
Specifies the go.Diagram which the Overview will observe.
179179

180+
### Component Methods
181+
182+
#### getDiagram/getPalette/getOverview
183+
Gets a reference to the GoJS Diagram/Palette/Overview.
184+
185+
```ts
186+
const diagram = this.diagramRef.current.getDiagram();
187+
if (diagram instanceof go.Diagram) {
188+
// ...
189+
}
190+
```
191+
192+
#### clear (ReactDiagram and ReactPalette only)
193+
Clears the diagram and allows the next update to be treated as an initial load of the model.
194+
195+
```ts
196+
// clear out the diagram
197+
this.diagramRef.current.clear();
198+
// provide new diagram data, which will be treated as initial data
199+
this.setState({
200+
nodeDataArray: [
201+
{ key: 'Epsilon', color: 'lightblue' },
202+
{ key: 'Zeta', color: 'orange' },
203+
{ key: 'Eta', color: 'lightgreen' },
204+
{ key: 'Theta', color: 'pink' }
205+
],
206+
linkDataArray: [
207+
{ key: -1, from: 'Epsilon', to: 'Zeta' },
208+
{ key: -2, from: 'Epsilon', to: 'Eta' },
209+
{ key: -3, from: 'Zeta', to: 'Zeta' },
210+
{ key: -4, from: 'Zeta', to: 'Theta' },
211+
{ key: -5, from: 'Theta', to: 'Epsilon' }
212+
]
213+
});
214+
```
215+
180216
## License
181217

182218
This project is intended to be used alongside [GoJS](https://gojs.net/latest/index.html),

0 commit comments

Comments
 (0)