@@ -174,13 +174,27 @@ class ObjectTreeReact extends React.Component<IProps, IStates> {
174
174
this . _onClientSharedOptionsChanged
175
175
) ;
176
176
177
- this . setState ( old => ( {
178
- ...old ,
179
- filePath : document . context . localPath ,
180
- jcadObject : this . props . cpModel . jcadModel ?. getAllObject ( ) ,
181
- options : this . props . cpModel . sharedModel ?. options ,
182
- clientId : document . context . model . getClientId ( )
183
- } ) ) ;
177
+ const currentSelection = this . _getCurrentSelection ( ) ;
178
+
179
+ if ( ! currentSelection ) {
180
+ this . setState ( old => ( {
181
+ ...old ,
182
+ filePath : document . context . localPath ,
183
+ jcadObject : this . props . cpModel . jcadModel ?. getAllObject ( ) ,
184
+ options : this . props . cpModel . sharedModel ?. options ,
185
+ clientId : document . context . model . getClientId ( )
186
+ } ) ) ;
187
+ } else {
188
+ this . setState ( old => ( {
189
+ ...old ,
190
+ selectedNodes : currentSelection . newSelectedNodes ,
191
+ openNodes : currentSelection . newOpenNodes ,
192
+ filePath : document . context . localPath ,
193
+ jcadObject : this . props . cpModel . jcadModel ?. getAllObject ( ) ,
194
+ options : this . props . cpModel . sharedModel ?. options ,
195
+ clientId : document . context . model . getClientId ( )
196
+ } ) ) ;
197
+ }
184
198
} else {
185
199
this . setState ( {
186
200
filePath : undefined ,
@@ -257,11 +271,23 @@ class ObjectTreeReact extends React.Component<IProps, IStates> {
257
271
change : IJcadObjectDocChange
258
272
) : void => {
259
273
if ( change . objectChange ) {
260
- this . setState ( old => ( {
261
- ...old ,
262
- jcadObject : this . props . cpModel . jcadModel ?. getAllObject ( ) ,
263
- options : this . props . cpModel . sharedModel ?. options
264
- } ) ) ;
274
+ const currentSelection = this . _getCurrentSelection ( ) ;
275
+
276
+ if ( ! currentSelection ) {
277
+ this . setState ( old => ( {
278
+ ...old ,
279
+ jcadObject : this . props . cpModel . jcadModel ?. getAllObject ( ) ,
280
+ options : this . props . cpModel . sharedModel ?. options
281
+ } ) ) ;
282
+ } else {
283
+ this . setState ( old => ( {
284
+ ...old ,
285
+ selectedNodes : currentSelection . newSelectedNodes ,
286
+ openNodes : currentSelection . newOpenNodes ,
287
+ jcadObject : this . props . cpModel . jcadModel ?. getAllObject ( ) ,
288
+ options : this . props . cpModel . sharedModel ?. options
289
+ } ) ) ;
290
+ }
265
291
}
266
292
} ;
267
293
@@ -284,35 +310,59 @@ class ObjectTreeReact extends React.Component<IProps, IStates> {
284
310
sender : IJupyterCadModel ,
285
311
clients : Map < number , IJupyterCadClientState >
286
312
) : void => {
313
+ const currentSelection = this . _getCurrentSelection ( clients ) ;
314
+
315
+ if ( ! currentSelection ) {
316
+ return ;
317
+ }
318
+
319
+ const { newSelectedNodes, newOpenNodes } = currentSelection ;
320
+ const { selectedNodes, openNodes } = this . state ;
321
+ if (
322
+ JSON . stringify ( selectedNodes ) !== JSON . stringify ( newSelectedNodes ) ||
323
+ JSON . stringify ( openNodes ) !== JSON . stringify ( newOpenNodes )
324
+ ) {
325
+ this . setState ( old => ( {
326
+ ...old ,
327
+ openNodes : newOpenNodes ,
328
+ selectedNodes : newSelectedNodes
329
+ } ) ) ;
330
+ }
331
+ } ;
332
+
333
+ private _getCurrentSelection (
334
+ clients ?: Map < number , IJupyterCadClientState >
335
+ ) :
336
+ | { newSelectedNodes : string [ ] ; newOpenNodes : ( string | number ) [ ] }
337
+ | undefined {
287
338
const localState = this . props . cpModel . jcadModel ?. localState ;
288
339
289
340
if ( ! localState ) {
290
341
return ;
291
342
}
292
343
293
- let selectedNodes : string [ ] = [ ] ;
294
- if ( localState . remoteUser ) {
344
+ let newSelectedNodes : string [ ] = [ ] ;
345
+ if ( clients && localState . remoteUser ) {
295
346
// We are in following mode.
296
347
// Sync selections from a remote user
297
348
const remoteState = clients . get ( localState . remoteUser ) ;
298
349
299
350
if ( remoteState ?. selected ?. value ) {
300
- selectedNodes = this . _selectedNodes ( remoteState . selected . value ) ;
351
+ newSelectedNodes = this . _selectedNodes ( remoteState . selected . value ) ;
301
352
}
302
353
} else if ( localState . selected ?. value ) {
303
- selectedNodes = this . _selectedNodes ( localState . selected . value ) ;
354
+ newSelectedNodes = this . _selectedNodes ( localState . selected . value ) ;
304
355
}
305
356
306
- const openNodes = [ ...this . state . openNodes ] ;
307
-
308
- for ( const selectedNode of selectedNodes ) {
309
- if ( selectedNode && openNodes . indexOf ( selectedNode ) === - 1 ) {
310
- openNodes . push ( selectedNode ) ;
357
+ const newOpenNodes = [ ...this . state . openNodes ] ;
358
+ for ( const selectedNode of newSelectedNodes ) {
359
+ if ( selectedNode && newOpenNodes . indexOf ( selectedNode ) === - 1 ) {
360
+ newOpenNodes . push ( selectedNode ) ;
311
361
}
312
362
}
313
363
314
- this . setState ( old => ( { ... old , openNodes , selectedNodes } ) ) ;
315
- } ;
364
+ return { newSelectedNodes , newOpenNodes } ;
365
+ }
316
366
317
367
private _onClientSharedOptionsChanged = (
318
368
sender : IJupyterCadDoc ,
0 commit comments