@@ -53,6 +53,7 @@ export class BoardComponent implements AfterViewChecked {
53
53
currentBoardId : string = UUID ( ) ;
54
54
isAllClientsSendingData = false ;
55
55
canToggleClientsSendingData = true ;
56
+ lastTouch : Touch ;
56
57
57
58
@Input ( ) isReadOnly = false ;
58
59
@Input ( ) loadedSave : any ;
@@ -119,7 +120,7 @@ export class BoardComponent implements AfterViewChecked {
119
120
else
120
121
this . zoomOut ( ) ;
121
122
}
122
-
123
+
123
124
ngOnInit ( ) : void {
124
125
this . board = document . getElementById ( "board" ) ;
125
126
this . board . style . width = `${ this . placingService . boardWidth } px` ;
@@ -128,8 +129,8 @@ export class BoardComponent implements AfterViewChecked {
128
129
129
130
this . updateBoardTransform ( ) ;
130
131
131
- window . addEventListener ( "resize" , event => {
132
- event . preventDefault ( ) ;
132
+ this . board . addEventListener ( "resize" , e => {
133
+ e . preventDefault ( ) ;
133
134
} ) ;
134
135
135
136
if ( ! this . isReadOnly ) { // These events will not be used in readonly board
@@ -153,7 +154,23 @@ export class BoardComponent implements AfterViewChecked {
153
154
this . placingService . stopCreating ( ) ;
154
155
this . componentChanged ( ) ;
155
156
}
156
- } )
157
+ } ) ;
158
+ window . addEventListener ( "touchstart" , ( e ) => { this . lastTouch = e . touches [ 0 ] ; } ) ;
159
+ window . addEventListener ( "touchmove" , ( e ) => { this . lastTouch = e . touches [ 0 ] ; } ) ;
160
+
161
+ window . addEventListener ( "touchend" , ( e : any ) => {
162
+ if ( this . placingService . isCreating ) {
163
+ let posX = ( this . posX - this . placingService . boardWidth * ( this . placingService . boardScale - 1 ) / 2 ) ;
164
+ let posY = ( this . posY - this . placingService . boardHeight * ( this . placingService . boardScale - 1 ) / 2 ) ;
165
+ var x = Math . max ( Math . min ( ( this . lastTouch . pageX - posX ) / this . placingService . boardScale - 20 , this . placingService . boardWidth ) , 0 ) ;
166
+ var y = Math . max ( Math . min ( ( this . lastTouch . pageY - posY ) / this . placingService . boardScale - 80 / this . placingService . boardScale , this . placingService . boardWidth ) , 0 ) ;
167
+ let component = this . placingService . createComponent ( this . placingService . creatingItem , x , y , this . placingService . creatingItemOptions ) ;
168
+ this . pushComponent ( component ) ;
169
+ this . placingService . stopCreating ( ) ;
170
+ this . componentChanged ( ) ;
171
+ }
172
+ return true ;
173
+ } ) ;
157
174
this . placingService . componentChanged . subscribe ( ( ) => {
158
175
// Some component just got changed, change will be added for undo
159
176
this . componentChanged ( ) ;
@@ -352,73 +369,108 @@ export class BoardComponent implements AfterViewChecked {
352
369
} , 1000 ) ;
353
370
}
354
371
355
- public handleMousedown ( event : Event ) {
356
- let e = event as MouseEvent ;
357
- if ( e . button == 0 && ! this . isReadOnly ) {
358
- // Start selecting
359
- this . showContextMenu = false ;
360
- this . showComponentContextMenu = false ;
361
- this . selectionService . startSelecting ( e , this . placingService . boardScale )
372
+ public handleMousedown ( e : Event ) {
373
+ if ( e instanceof MouseEvent ) {
374
+ if ( e . button == 0 && ! this . isReadOnly ) {
375
+ // Start selecting
376
+ this . showContextMenu = false ;
377
+ this . showComponentContextMenu = false ;
378
+ this . selectionService . startSelecting ( e , this . placingService . boardScale )
379
+ }
380
+ else if ( this . isReadOnly || e . button == 1 || e . button == 2 ) {
381
+ e . preventDefault ( ) ;
382
+ if ( ! this . placingService . canDrag ( ) )
383
+ return ;
384
+ this . board . classList . add ( "moving" )
385
+ this . board . addEventListener ( "mousemove" , this . handleMousemove ) ;
386
+ window . addEventListener ( "mouseup" , this . handleMouseup ) ;
387
+ }
362
388
}
363
- else if ( this . isReadOnly || e . button == 1 || e . button == 2 ) {
389
+ else if ( e instanceof TouchEvent ) {
364
390
e . preventDefault ( ) ;
365
391
if ( ! this . placingService . canDrag ( ) )
366
392
return ;
393
+ this . lastTouchMoveX = e . touches [ 0 ] . clientX ;
394
+ this . lastTouchMoveY = e . touches [ 0 ] . clientY ;
367
395
this . board . classList . add ( "moving" )
368
- this . board . addEventListener ( "mousemove " , this . handleMousemove ) ;
369
- window . addEventListener ( "mouseup " , this . handleMouseup ) ;
396
+ this . board . addEventListener ( "touchmove " , this . handleMousemove ) ;
397
+ window . addEventListener ( "touchend " , this . handleMouseup ) ;
370
398
}
399
+
371
400
}
372
401
373
- public handleMousemove = ( event : MouseEvent ) : void => {
374
- this . boardMoved = true ;
375
- this . showContextMenu = false ;
376
- this . showComponentContextMenu = false ;
377
- this . posX += event . movementX ;
378
- this . posY += event . movementY ;
402
+ lastTouchMoveX : number = 0 ;
403
+ lastTouchMoveY : number = 0 ;
404
+
405
+ public handleMousemove = ( e : Event ) : void => {
406
+ if ( e instanceof MouseEvent ) {
407
+ this . boardMoved = true ;
408
+ this . showContextMenu = false ;
409
+ this . showComponentContextMenu = false ;
410
+ this . posX += e . movementX ;
411
+ this . posY += e . movementY ;
412
+ }
413
+ else if ( e instanceof TouchEvent ) {
414
+ this . boardMoved = true ;
415
+ this . showContextMenu = false ;
416
+ this . showComponentContextMenu = false ;
417
+ this . posX += e . touches [ 0 ] . clientX - this . lastTouchMoveX ;
418
+ this . posY += e . touches [ 0 ] . clientY - this . lastTouchMoveY ;
419
+ this . lastTouchMoveX = e . touches [ 0 ] . clientX ;
420
+ this . lastTouchMoveY = e . touches [ 0 ] . clientY ;
421
+ }
379
422
380
423
this . updateBoardTransform ( ) ;
381
424
}
382
425
383
- public handleMouseup = ( e ) : void => {
384
- if ( e . button === 2 && ! this . boardMoved && ! this . isReadOnly ) {
385
- this . showContextMenu = true ;
386
- this . showComponentContextMenu = false ;
387
- this . contextMenuX = e . offsetX ;
388
- this . contextMenuY = e . offsetY ;
426
+ public handleMouseup = ( e : Event ) : void => {
427
+ if ( e instanceof MouseEvent ) {
428
+ if ( e . button === 2 && ! this . boardMoved && ! this . isReadOnly ) {
429
+ this . showContextMenu = true ;
430
+ this . showComponentContextMenu = false ;
431
+ this . contextMenuX = e . offsetX ;
432
+ this . contextMenuY = e . offsetY ;
433
+ }
434
+ this . boardMoved = false ;
435
+ this . board . classList . remove ( "moving" )
436
+ this . board . removeEventListener ( "mousemove" , this . handleMousemove ) ;
437
+ window . removeEventListener ( "mouseup" , this . handleMouseup ) ;
438
+ }
439
+ else if ( e instanceof TouchEvent ) {
440
+ this . boardMoved = false ;
441
+ this . board . classList . remove ( "moving" )
442
+ this . board . removeEventListener ( "touchmove" , this . handleMousemove ) ;
443
+ window . removeEventListener ( "touchend" , this . handleMouseup ) ;
389
444
}
390
- this . boardMoved = false ;
391
- this . board . classList . remove ( "moving" )
392
- this . board . removeEventListener ( "mousemove" , this . handleMousemove ) ;
393
- window . removeEventListener ( "mouseup" , this . handleMouseup ) ;
445
+
394
446
}
395
447
396
448
public updateBoardTransform ( ) {
397
449
this . board . style . transform = `translateX(${ this . posX } px) translateY(${ this . posY } px) scale(${ this . placingService . boardScale } )` ;
398
450
}
399
451
400
- public handleClick = ( event : MouseEvent ) : void => {
452
+ public handleClick = ( ) : void => {
401
453
if ( this . placingService . isConnecting ) {
402
454
this . placingService . stopConnecting ( ) ;
403
455
this . board . onmousemove = null ;
404
456
document . getElementsByClassName ( "svg-canvas" ) [ 0 ] . innerHTML = "" ;
405
457
}
406
458
}
407
459
408
- public handleSelfClick ( event : Event ) {
460
+ public handleSelfClick ( ) {
409
461
this . selectionService . clearSelection ( ) ;
410
462
this . selectionService . clearConnectionSelection ( ) ;
411
463
this . selectionService . clearCurrentConnectionSelections ( ) ;
412
464
this . selectionService . clearLineBreakSelection ( ) ;
413
465
}
414
466
415
- zoomOut ( ) {
416
- this . placingService . boardScale = Math . max ( this . placingService . boardScale - 0.1 , 0.1 ) ;
467
+ zoomOut ( modifier : number = 1 ) {
468
+ this . placingService . boardScale = Math . max ( this . placingService . boardScale - ( 0.1 / modifier ) , 0.1 ) ;
417
469
this . updateBoardTransform ( ) ;
418
470
}
419
471
420
- zoomIn ( ) {
421
- this . placingService . boardScale = Math . min ( this . placingService . boardScale + 0.1 , 2 ) ;
472
+ zoomIn ( modifier : number = 1 ) {
473
+ this . placingService . boardScale = Math . min ( this . placingService . boardScale + ( 0.1 / modifier ) , 2 ) ;
422
474
this . updateBoardTransform ( ) ;
423
475
}
424
476
0 commit comments