@@ -63,9 +63,7 @@ export default function Canvas() {
63
63
prevY : 0 ,
64
64
initialPositions : [ ] ,
65
65
} ;
66
- const [ dragging , setDragging ] = useState ( {
67
- ...notDragging ,
68
- } ) ;
66
+ const [ dragging , setDragging ] = useState ( notDragging ) ;
69
67
const [ linking , setLinking ] = useState ( false ) ;
70
68
const [ linkingLine , setLinkingLine ] = useState ( {
71
69
startTableId : - 1 ,
@@ -184,7 +182,7 @@ export default function Canvas() {
184
182
case ObjectType . NOTE :
185
183
return notes [ element . id ] ;
186
184
default :
187
- return { x : 0 , y : 0 } ;
185
+ return { x : 0 , y : 0 , locked : false } ;
188
186
}
189
187
} ;
190
188
@@ -198,67 +196,45 @@ export default function Canvas() {
198
196
199
197
if ( ! e . isPrimary ) return ;
200
198
201
- let locked = false ;
202
- let prevCoords = { prevX : 0 , prevY : 0 } ;
199
+ const element = getElement ( { id, type } ) ;
203
200
204
- if ( type === ObjectType . TABLE ) {
205
- const table = tables . find ( ( t ) => t . id === id ) ;
206
- locked = table . locked ;
201
+ setSelectedElement ( ( prev ) => ( {
202
+ ...prev ,
203
+ element : type ,
204
+ id : id ,
205
+ open : false ,
206
+ } ) ) ;
207
207
208
- setGrabOffset ( {
209
- x : table . x - pointer . spaces . diagram . x ,
210
- y : table . y - pointer . spaces . diagram . y ,
211
- } ) ;
212
- prevCoords = { prevX : table . x , prevY : table . y } ;
213
- } else if ( type === ObjectType . AREA ) {
214
- const area = areas . find ( ( t ) => t . id === id ) ;
215
- locked = area . locked ;
216
-
217
- setGrabOffset ( {
218
- x : area . x - pointer . spaces . diagram . x ,
219
- y : area . y - pointer . spaces . diagram . y ,
220
- } ) ;
221
- prevCoords = { prevX : area . x , prevY : area . y } ;
222
- } else if ( type === ObjectType . NOTE ) {
223
- const note = notes . find ( ( t ) => t . id === id ) ;
224
- locked = note . locked ;
225
-
226
- setGrabOffset ( {
227
- x : note . x - pointer . spaces . diagram . x ,
228
- y : note . y - pointer . spaces . diagram . y ,
229
- } ) ;
230
- prevCoords = { prevX : note . x , prevY : note . y } ;
208
+ if ( element . locked ) {
209
+ setBulkSelectedElements ( [ ] ) ;
210
+ return ;
231
211
}
232
212
233
- if ( ! locked ) {
234
- setDragging ( ( prev ) => ( {
235
- ...prev ,
236
- id,
237
- element : type ,
238
- ...prevCoords ,
239
- } ) ) ;
240
- setSelectedElement ( ( prev ) => ( {
241
- ...prev ,
242
- element : type ,
243
- id : id ,
244
- open : false ,
245
- } ) ) ;
246
- }
213
+ let prevCoords = { prevX : element . x , prevY : element . y } ;
214
+ setGrabOffset ( {
215
+ x : element . x - pointer . spaces . diagram . x ,
216
+ y : element . y - pointer . spaces . diagram . y ,
217
+ } ) ;
247
218
248
- if ( bulkSelectedElements . length ) {
249
- setDragging ( ( prev ) => ( {
250
- ...prev ,
251
- initialPositions : bulkSelectedElements . map ( ( element ) => ( {
252
- ...element ,
253
- undo : {
254
- x : getElement ( element ) . x ,
255
- y : getElement ( element ) . y ,
256
- } ,
257
- } ) ) ,
258
- } ) ) ;
219
+ let newBulkSelectedElements ;
220
+ if ( bulkSelectedElements . some ( ( el ) => el . id === id && el . type === type ) ) {
221
+ newBulkSelectedElements = bulkSelectedElements ;
222
+ } else {
223
+ newBulkSelectedElements = [ { id, type } ] ;
224
+ setBulkSelectedElements ( newBulkSelectedElements ) ;
259
225
}
260
- } ;
261
226
227
+ setDragging ( ( prev ) => ( {
228
+ ...prev ,
229
+ id,
230
+ element : type ,
231
+ ...prevCoords ,
232
+ initialPositions : newBulkSelectedElements . map ( ( el ) => {
233
+ const { x, y } = getElement ( el ) ;
234
+ return { ...el , undo : { x, y } } ;
235
+ } ) ,
236
+ } ) ) ;
237
+ } ;
262
238
/**
263
239
* @param {PointerEvent } e
264
240
*/
@@ -305,39 +281,30 @@ export default function Canvas() {
305
281
endX : pointer . spaces . diagram . x ,
306
282
endY : pointer . spaces . diagram . y ,
307
283
} ) ;
308
- } else if (
309
- dragging . element !== ObjectType . NONE &&
310
- dragging . id !== null &&
311
- bulkSelectedElements . length &&
312
- bulkSelectedElements . some (
313
- ( element ) =>
314
- element . id === dragging . id && element . type === dragging . element ,
315
- )
316
- ) {
317
- for ( const element of bulkSelectedElements ) {
318
- if ( element . type === ObjectType . TABLE ) {
319
- const table = tables . find ( ( e ) => e . id === element . id ) ;
320
- if ( table . locked ) continue ;
321
- const { x, y } = table ;
322
- updateTable ( element . id , {
284
+ return ;
285
+ }
286
+
287
+ if ( isDragging ) {
288
+ for ( const el of bulkSelectedElements ) {
289
+ const element = getElement ( el ) ;
290
+ const { type } = el ;
291
+ if ( element . locked ) continue ;
292
+ const { x, y } = element ;
293
+
294
+ if ( type === ObjectType . TABLE ) {
295
+ updateTable ( el . id , {
323
296
x : x + deltaX ,
324
297
y : y + deltaY ,
325
298
} ) ;
326
299
}
327
- if ( element . type === ObjectType . AREA ) {
328
- const area = areas [ element . id ] ;
329
- if ( area . locked ) continue ;
330
- const { x, y } = area ;
331
- updateArea ( element . id , {
300
+ if ( type === ObjectType . AREA ) {
301
+ updateArea ( el . id , {
332
302
x : x + deltaX ,
333
303
y : y + deltaY ,
334
304
} ) ;
335
305
}
336
- if ( element . type === ObjectType . NOTE ) {
337
- const note = notes [ element . id ] ;
338
- if ( note . locked ) continue ;
339
- const { x, y } = note ;
340
- updateNote ( element . id , {
306
+ if ( type === ObjectType . NOTE ) {
307
+ updateNote ( el . id , {
341
308
x : x + deltaX ,
342
309
y : y + deltaY ,
343
310
} ) ;
@@ -349,35 +316,10 @@ export default function Canvas() {
349
316
prevX : finalX ,
350
317
prevY : finalY ,
351
318
} ) ) ;
352
- } else if ( dragging . element === ObjectType . TABLE && dragging . id !== null ) {
353
- const table = tables . find ( ( t ) => t . id === dragging . id ) ;
354
- if ( table . locked ) return ;
355
-
356
- updateTable ( dragging . id , {
357
- x : finalX ,
358
- y : finalY ,
359
- } ) ;
360
- } else if (
361
- dragging . element === ObjectType . AREA &&
362
- dragging . id !== null &&
363
- areaResize . id === - 1
364
- ) {
365
- const area = areas . find ( ( t ) => t . id === dragging . id ) ;
366
- if ( area . locked ) return ;
367
-
368
- updateArea ( dragging . id , {
369
- x : finalX ,
370
- y : finalY ,
371
- } ) ;
372
- } else if ( dragging . element === ObjectType . NOTE && dragging . id !== null ) {
373
- const note = notes . find ( ( t ) => t . id === dragging . id ) ;
374
- if ( note . locked ) return ;
319
+ return ;
320
+ }
375
321
376
- updateNote ( dragging . id , {
377
- x : finalX ,
378
- y : finalY ,
379
- } ) ;
380
- } else if ( areaResize . id !== - 1 ) {
322
+ if ( areaResize . id !== - 1 ) {
381
323
if ( areaResize . dir === "none" ) return ;
382
324
let newDims = { ...initCoords } ;
383
325
delete newDims . pointerX ;
@@ -408,7 +350,10 @@ export default function Canvas() {
408
350
}
409
351
410
352
updateArea ( areaResize . id , { ...newDims } ) ;
411
- } else if ( bulkSelectRectPts . show ) {
353
+ return ;
354
+ }
355
+
356
+ if ( bulkSelectRectPts . show ) {
412
357
setBulkSelectRectPts ( ( prev ) => ( {
413
358
...prev ,
414
359
x2 : finalX ,
@@ -455,7 +400,8 @@ export default function Canvas() {
455
400
}
456
401
} ;
457
402
458
- const coordsDidUpdate = ( element ) => {
403
+ const coordsDidUpdate = ( ) => {
404
+ const element = { id : dragging . id , type : dragging . element } ;
459
405
const elementData = getElement ( element ) ;
460
406
const updated = ! (
461
407
dragging . prevX === elementData . x && dragging . prevY === elementData . y
@@ -492,54 +438,24 @@ export default function Canvas() {
492
438
493
439
if ( ! e . isPrimary ) return ;
494
440
495
- let bulkMoved = false ;
441
+ const coordinatesDidUpdate = coordsDidUpdate ( ) ;
496
442
497
- if ( coordsDidUpdate ( { id : dragging . id , type : dragging . element } ) ) {
498
- if ( bulkSelectedElements . length ) {
499
- setUndoStack ( ( prev ) => [
500
- ...prev ,
501
- {
502
- action : Action . MOVE ,
503
- bulk : true ,
504
- message : t ( "bulk_update" ) ,
505
- elements : dragging . initialPositions . map ( ( element ) => ( {
506
- ...element ,
507
- redo : {
508
- x : getElement ( element ) . x ,
509
- y : getElement ( element ) . y ,
510
- } ,
511
- } ) ) ,
512
- } ,
513
- ] ) ;
514
- bulkMoved = true ;
515
- } else {
516
- const element = getElement ( {
517
- id : dragging . id ,
518
- type : dragging . element ,
519
- } ) ;
520
- setUndoStack ( ( prev ) => [
521
- ...prev ,
522
- {
523
- action : Action . MOVE ,
524
- element : dragging . element ,
525
- x : dragging . prevX ,
526
- y : dragging . prevY ,
527
- toX : element . x ,
528
- toY : element . y ,
529
- id : dragging . id ,
530
- message : t ( "move_element" , {
531
- coords : `(${ element . x } , ${ element . y } )` ,
532
- name : getElement ( {
533
- id : dragging . id ,
534
- type : dragging . element ,
535
- } ) . name ,
536
- } ) ,
537
- } ,
538
- ] ) ;
539
- }
443
+ if ( coordinatesDidUpdate ) {
444
+ setUndoStack ( ( prev ) => [
445
+ ...prev ,
446
+ {
447
+ action : Action . MOVE ,
448
+ bulk : true ,
449
+ message : t ( "bulk_update" ) ,
450
+ elements : dragging . initialPositions . map ( ( element ) => {
451
+ const { x, y } = getElement ( element ) ;
452
+ return { ...element , redo : { x, y } } ;
453
+ } ) ,
454
+ } ,
455
+ ] ) ;
540
456
setRedoStack ( [ ] ) ;
541
457
}
542
- setDragging ( { ... notDragging } ) ;
458
+ setDragging ( notDragging ) ;
543
459
544
460
if ( bulkSelectRectPts . show ) {
545
461
setBulkSelectRectPts ( ( prev ) => ( {
@@ -548,7 +464,7 @@ export default function Canvas() {
548
464
y2 : pointer . spaces . diagram . y ,
549
465
show : false ,
550
466
} ) ) ;
551
- if ( ! bulkMoved ) {
467
+ if ( ! coordinatesDidUpdate ) {
552
468
collectSelectedElements ( ) ;
553
469
}
554
470
}
@@ -598,7 +514,7 @@ export default function Canvas() {
598
514
599
515
const handleGripField = ( ) => {
600
516
setPanning ( ( old ) => ( { ...old , isPanning : false } ) ) ;
601
- setDragging ( { ... notDragging } ) ;
517
+ setDragging ( notDragging ) ;
602
518
setLinking ( true ) ;
603
519
} ;
604
520
0 commit comments