/
App.tsx
302 lines (285 loc) · 10.3 KB
/
App.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*
* Copyright (C) 1998-2023 by Northwoods Software Corporation. All Rights Reserved.
*/
import * as go from 'gojs';
import { produce } from 'immer';
import * as React from 'react';
import { DiagramWrapper } from './components/DiagramWrapper';
import { SelectionInspector } from './components/SelectionInspector';
import './App.css';
/**
* Use a linkDataArray since we'll be using a GraphLinksModel,
* and modelData for demonstration purposes. Note, though, that
* both are optional props in ReactDiagram.
*/
interface AppState {
nodeDataArray: Array<go.ObjectData>;
linkDataArray: Array<go.ObjectData>;
modelData: go.ObjectData;
selectedData: go.ObjectData | null;
skipsDiagramUpdate: boolean;
}
class App extends React.Component<{}, AppState> {
// Maps to store key -> arr index for quick lookups
private mapNodeKeyIdx: Map<go.Key, number>;
private mapLinkKeyIdx: Map<go.Key, number>;
constructor(props: object) {
super(props);
this.state = {
nodeDataArray: [
{ key: 0, text: 'Alpha', color: 'lightblue', loc: '0 0' },
{ key: 1, text: 'Beta', color: 'orange', loc: '150 0' },
{ key: 2, text: 'Gamma', color: 'lightgreen', loc: '0 150' },
{ key: 3, text: 'Delta', color: 'pink', loc: '150 150' }
],
linkDataArray: [
{ key: -1, from: 0, to: 1 },
{ key: -2, from: 0, to: 2 },
{ key: -3, from: 1, to: 1 },
{ key: -4, from: 2, to: 3 },
{ key: -5, from: 3, to: 0 }
],
modelData: {
canRelink: true
},
selectedData: null,
skipsDiagramUpdate: false
};
// init maps
this.mapNodeKeyIdx = new Map<go.Key, number>();
this.mapLinkKeyIdx = new Map<go.Key, number>();
this.refreshNodeIndex(this.state.nodeDataArray);
this.refreshLinkIndex(this.state.linkDataArray);
// bind handler methods
this.handleDiagramEvent = this.handleDiagramEvent.bind(this);
this.handleModelChange = this.handleModelChange.bind(this);
this.handleInputChange = this.handleInputChange.bind(this);
this.handleRelinkChange = this.handleRelinkChange.bind(this);
}
/**
* Update map of node keys to their index in the array.
*/
private refreshNodeIndex(nodeArr: Array<go.ObjectData>) {
this.mapNodeKeyIdx.clear();
nodeArr.forEach((n: go.ObjectData, idx: number) => {
this.mapNodeKeyIdx.set(n.key, idx);
});
}
/**
* Update map of link keys to their index in the array.
*/
private refreshLinkIndex(linkArr: Array<go.ObjectData>) {
this.mapLinkKeyIdx.clear();
linkArr.forEach((l: go.ObjectData, idx: number) => {
this.mapLinkKeyIdx.set(l.key, idx);
});
}
/**
* Handle any relevant DiagramEvents, in this case just selection changes.
* On ChangedSelection, find the corresponding data and set the selectedData state.
* @param e a GoJS DiagramEvent
*/
public handleDiagramEvent(e: go.DiagramEvent) {
const name = e.name;
switch (name) {
case 'ChangedSelection': {
const sel = e.subject.first();
this.setState(
produce((draft: AppState) => {
if (sel) {
if (sel instanceof go.Node) {
const idx = this.mapNodeKeyIdx.get(sel.key);
if (idx !== undefined && idx >= 0) {
const nd = draft.nodeDataArray[idx];
draft.selectedData = nd;
}
} else if (sel instanceof go.Link) {
const idx = this.mapLinkKeyIdx.get(sel.key);
if (idx !== undefined && idx >= 0) {
const ld = draft.linkDataArray[idx];
draft.selectedData = ld;
}
}
} else {
draft.selectedData = null;
}
})
);
break;
}
default: break;
}
}
/**
* Handle GoJS model changes, which output an object of data changes via Model.toIncrementalData.
* This method iterates over those changes and updates state to keep in sync with the GoJS model.
* @param obj a JSON-formatted string
*/
public handleModelChange(obj: go.IncrementalData) {
const insertedNodeKeys = obj.insertedNodeKeys;
const modifiedNodeData = obj.modifiedNodeData;
const removedNodeKeys = obj.removedNodeKeys;
const insertedLinkKeys = obj.insertedLinkKeys;
const modifiedLinkData = obj.modifiedLinkData;
const removedLinkKeys = obj.removedLinkKeys;
const modifiedModelData = obj.modelData;
// maintain maps of modified data so insertions don't need slow lookups
const modifiedNodeMap = new Map<go.Key, go.ObjectData>();
const modifiedLinkMap = new Map<go.Key, go.ObjectData>();
this.setState(
produce((draft: AppState) => {
let narr = draft.nodeDataArray;
if (modifiedNodeData) {
modifiedNodeData.forEach((nd: go.ObjectData) => {
modifiedNodeMap.set(nd.key, nd);
const idx = this.mapNodeKeyIdx.get(nd.key);
if (idx !== undefined && idx >= 0) {
narr[idx] = nd;
if (draft.selectedData && draft.selectedData.key === nd.key) {
draft.selectedData = nd;
}
}
});
}
if (insertedNodeKeys) {
insertedNodeKeys.forEach((key: go.Key) => {
const nd = modifiedNodeMap.get(key);
const idx = this.mapNodeKeyIdx.get(key);
if (nd && idx === undefined) { // nodes won't be added if they already exist
this.mapNodeKeyIdx.set(nd.key, narr.length);
narr.push(nd);
}
});
}
if (removedNodeKeys) {
narr = narr.filter((nd: go.ObjectData) => {
if (removedNodeKeys.includes(nd.key)) {
return false;
}
return true;
});
draft.nodeDataArray = narr;
this.refreshNodeIndex(narr);
}
let larr = draft.linkDataArray;
if (modifiedLinkData) {
modifiedLinkData.forEach((ld: go.ObjectData) => {
modifiedLinkMap.set(ld.key, ld);
const idx = this.mapLinkKeyIdx.get(ld.key);
if (idx !== undefined && idx >= 0) {
larr[idx] = ld;
if (draft.selectedData && draft.selectedData.key === ld.key) {
draft.selectedData = ld;
}
}
});
}
if (insertedLinkKeys) {
insertedLinkKeys.forEach((key: go.Key) => {
const ld = modifiedLinkMap.get(key);
const idx = this.mapLinkKeyIdx.get(key);
if (ld && idx === undefined) { // links won't be added if they already exist
this.mapLinkKeyIdx.set(ld.key, larr.length);
larr.push(ld);
}
});
}
if (removedLinkKeys) {
larr = larr.filter((ld: go.ObjectData) => {
if (removedLinkKeys.includes(ld.key)) {
return false;
}
return true;
});
draft.linkDataArray = larr;
this.refreshLinkIndex(larr);
}
// handle model data changes, for now just replacing with the supplied object
if (modifiedModelData) {
draft.modelData = modifiedModelData;
}
draft.skipsDiagramUpdate = true; // the GoJS model already knows about these updates
})
);
}
/**
* Handle inspector changes, and on input field blurs, update node/link data state.
* @param path the path to the property being modified
* @param value the new value of that property
* @param isBlur whether the input event was a blur, indicating the edit is complete
*/
public handleInputChange(path: string, value: string, isBlur: boolean) {
this.setState(
produce((draft: AppState) => {
const data = draft.selectedData as go.ObjectData; // only reached if selectedData isn't null
data[path] = value;
if (isBlur) {
const key = data.key;
if (key < 0) { // negative keys are links
const idx = this.mapLinkKeyIdx.get(key);
if (idx !== undefined && idx >= 0) {
draft.linkDataArray[idx] = data;
draft.skipsDiagramUpdate = false;
}
} else {
const idx = this.mapNodeKeyIdx.get(key);
if (idx !== undefined && idx >= 0) {
draft.nodeDataArray[idx] = data;
draft.skipsDiagramUpdate = false;
}
}
}
})
);
}
/**
* Handle changes to the checkbox on whether to allow relinking.
* @param e a change event from the checkbox
*/
public handleRelinkChange(e: any) {
const target = e.target;
const value = target.checked;
this.setState({ modelData: { canRelink: value }, skipsDiagramUpdate: false });
}
public render() {
const selectedData = this.state.selectedData;
let inspector;
if (selectedData !== null) {
inspector = <SelectionInspector
selectedData={this.state.selectedData}
onInputChange={this.handleInputChange}
/>;
}
return (
<div>
<p>
Try moving around nodes, editing text, relinking, undoing (Ctrl-Z), etc. within the diagram
and you'll notice the changes are reflected in the inspector area. You'll also notice that changes
made in the inspector are reflected in the diagram. If you use the React dev tools,
you can inspect the React state and see it updated as changes happen.
</p>
<p>
Check out the <a href='https://gojs.net/latest/intro/react.html' target='_blank' rel='noopener noreferrer'>Intro page on using GoJS with React</a> for more information.
</p>
<DiagramWrapper
nodeDataArray={this.state.nodeDataArray}
linkDataArray={this.state.linkDataArray}
modelData={this.state.modelData}
skipsDiagramUpdate={this.state.skipsDiagramUpdate}
onDiagramEvent={this.handleDiagramEvent}
onModelChange={this.handleModelChange}
/>
<label>
Allow Relinking?
<input
type='checkbox'
id='relink'
checked={this.state.modelData.canRelink}
onChange={this.handleRelinkChange} />
</label>
{inspector}
</div>
);
}
}
export default App;