@@ -184,7 +184,7 @@ export default function Canvas() {
184
184
case ObjectType . NOTE :
185
185
return notes [ element . id ] ;
186
186
default :
187
- return { x : 0 , y : 0 } ;
187
+ return { x : 0 , y : 0 , locked : false } ;
188
188
}
189
189
} ;
190
190
@@ -198,67 +198,45 @@ export default function Canvas() {
198
198
199
199
if ( ! e . isPrimary ) return ;
200
200
201
- let locked = false ;
202
- let prevCoords = { prevX : 0 , prevY : 0 } ;
201
+ const element = getElement ( { id, type } ) ;
203
202
204
- if ( type === ObjectType . TABLE ) {
205
- const table = tables . find ( ( t ) => t . id === id ) ;
206
- locked = table . locked ;
203
+ setSelectedElement ( ( prev ) => ( {
204
+ ...prev ,
205
+ element : type ,
206
+ id : id ,
207
+ open : false ,
208
+ } ) ) ;
207
209
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 } ;
210
+ if ( element . locked ) {
211
+ setBulkSelectedElements ( [ ] ) ;
212
+ return ;
231
213
}
232
214
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
- }
215
+ let prevCoords = { prevX : element . x , prevY : element . y } ;
216
+ setGrabOffset ( {
217
+ x : element . x - pointer . spaces . diagram . x ,
218
+ y : element . y - pointer . spaces . diagram . y ,
219
+ } ) ;
247
220
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
- } ) ) ;
221
+ let newBulkSelectedElements ;
222
+ if ( bulkSelectedElements . some ( ( el ) => el . id === id ) ) {
223
+ newBulkSelectedElements = bulkSelectedElements ;
224
+ } else {
225
+ newBulkSelectedElements = [ { id, type } ] ;
226
+ setBulkSelectedElements ( newBulkSelectedElements ) ;
259
227
}
260
- } ;
261
228
229
+ setDragging ( ( prev ) => ( {
230
+ ...prev ,
231
+ id,
232
+ element : type ,
233
+ ...prevCoords ,
234
+ initialPositions : newBulkSelectedElements . map ( ( el ) => {
235
+ const { x, y } = getElement ( el ) ;
236
+ return { ...el , undo : { x, y } } ;
237
+ } ) ,
238
+ } ) ) ;
239
+ } ;
262
240
/**
263
241
* @param {PointerEvent } e
264
242
*/
@@ -305,39 +283,33 @@ export default function Canvas() {
305
283
endX : pointer . spaces . diagram . x ,
306
284
endY : pointer . spaces . diagram . y ,
307
285
} ) ;
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 , {
286
+ return ;
287
+ }
288
+
289
+ if ( isDragging ) {
290
+ if ( ! bulkSelectedElements . length ) {
291
+ throw "Something went wrong" ;
292
+ }
293
+ for ( const el of bulkSelectedElements ) {
294
+ const element = getElement ( el ) ;
295
+ const { type } = el ;
296
+ if ( element . locked ) continue ;
297
+ const { x , y } = element ;
298
+
299
+ if ( type === ObjectType . TABLE ) {
300
+ updateTable ( el . id , {
323
301
x : x + deltaX ,
324
302
y : y + deltaY ,
325
303
} ) ;
326
304
}
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 , {
305
+ if ( type === ObjectType . AREA ) {
306
+ updateArea ( el . id , {
332
307
x : x + deltaX ,
333
308
y : y + deltaY ,
334
309
} ) ;
335
310
}
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 , {
311
+ if ( type === ObjectType . NOTE ) {
312
+ updateNote ( el . id , {
341
313
x : x + deltaX ,
342
314
y : y + deltaY ,
343
315
} ) ;
@@ -349,35 +321,10 @@ export default function Canvas() {
349
321
prevX : finalX ,
350
322
prevY : finalY ,
351
323
} ) ) ;
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 ;
324
+ return ;
325
+ }
375
326
376
- updateNote ( dragging . id , {
377
- x : finalX ,
378
- y : finalY ,
379
- } ) ;
380
- } else if ( areaResize . id !== - 1 ) {
327
+ if ( areaResize . id !== - 1 ) {
381
328
if ( areaResize . dir === "none" ) return ;
382
329
let newDims = { ...initCoords } ;
383
330
delete newDims . pointerX ;
@@ -408,7 +355,10 @@ export default function Canvas() {
408
355
}
409
356
410
357
updateArea ( areaResize . id , { ...newDims } ) ;
411
- } else if ( bulkSelectRectPts . show ) {
358
+ return ;
359
+ }
360
+
361
+ if ( bulkSelectRectPts . show ) {
412
362
setBulkSelectRectPts ( ( prev ) => ( {
413
363
...prev ,
414
364
x2 : finalX ,
@@ -455,11 +405,13 @@ export default function Canvas() {
455
405
}
456
406
} ;
457
407
458
- const coordsDidUpdate = ( element ) => {
408
+ const coordsDidUpdate = ( ) => {
409
+ const element = { id : dragging . id , type : dragging . element } ;
459
410
const elementData = getElement ( element ) ;
460
411
const updated = ! (
461
412
dragging . prevX === elementData . x && dragging . prevY === elementData . y
462
413
) ;
414
+ console . log ( updated ) ;
463
415
464
416
return (
465
417
updated ||
@@ -492,51 +444,21 @@ export default function Canvas() {
492
444
493
445
if ( ! e . isPrimary ) return ;
494
446
495
- let bulkMoved = false ;
447
+ const coordinatesDidUpdate = coordsDidUpdate ( ) ;
496
448
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
- }
449
+ if ( coordinatesDidUpdate ) {
450
+ setUndoStack ( ( prev ) => [
451
+ ...prev ,
452
+ {
453
+ action : Action . MOVE ,
454
+ bulk : true ,
455
+ message : t ( "bulk_update" ) ,
456
+ elements : dragging . initialPositions . map ( ( element ) => {
457
+ const { x, y } = getElement ( element ) ;
458
+ return { ...element , redo : { x, y } } ;
459
+ } ) ,
460
+ } ,
461
+ ] ) ;
540
462
setRedoStack ( [ ] ) ;
541
463
}
542
464
setDragging ( { ...notDragging } ) ;
@@ -548,7 +470,7 @@ export default function Canvas() {
548
470
y2 : pointer . spaces . diagram . y ,
549
471
show : false ,
550
472
} ) ) ;
551
- if ( ! bulkMoved ) {
473
+ if ( ! coordinatesDidUpdate ) {
552
474
collectSelectedElements ( ) ;
553
475
}
554
476
}
0 commit comments