@@ -13,6 +13,9 @@ export class ElementChatComponent implements OnInit, OnDestroy {
1313 element : any ;
1414 iframeURL : string ;
1515 currentView : View ;
16+ private isLongPress : boolean ;
17+ private longPressTimeout : number ;
18+ private isDragging : boolean ;
1619
1720 constructor (
1821 public viewSrv : ViewService ,
@@ -54,52 +57,76 @@ export class ElementChatComponent implements OnInit, OnDestroy {
5457 const clientOffset : any = { } ;
5558 const dragBox = document . getElementById ( 'dragBox' ) ;
5659 const dragButton = document . querySelector ( '.drag-button' ) ;
57- dragBox . addEventListener ( 'mousedown' , ( event ) => {
58- event . stopPropagation ( ) ;
59- const offsetX = dragBox . getBoundingClientRect ( ) . left ;
60- const offsetY = dragBox . getBoundingClientRect ( ) . top ;
61- const innerX = event . clientX - offsetX ;
62- const innerY = event . clientY - offsetY ;
63-
64- clientOffset . clientX = event . clientX ;
65- clientOffset . clientY = event . clientY ;
66- document . onmousemove = function ( ev : any ) {
67- dragBox . style . left = ev . clientX - innerX + 'px' ;
68- dragBox . style . top = ev . clientY - innerY + 'px' ;
69- const dragDivTop = window . innerHeight - dragBox . getBoundingClientRect ( ) . height ;
70- const dragDivLeft = window . innerWidth - dragBox . getBoundingClientRect ( ) . width ;
71- dragBox . style . left = dragDivLeft + 'px' ;
72- dragBox . style . left = '-48px' ;
73- if ( dragBox . getBoundingClientRect ( ) . top <= 0 ) {
74- dragBox . style . top = '0px' ;
75- }
76- if ( dragBox . getBoundingClientRect ( ) . top >= dragDivTop ) {
77- dragBox . style . top = dragDivTop + 'px' ;
60+ const chatImg = dragButton . querySelector ( '.chat-img' ) ;
61+ const elements = [ dragButton , chatImg ] ;
62+
63+ elements . forEach ( element => {
64+ element . addEventListener ( 'mousedown' , ( event : MouseEvent ) => {
65+ event . stopPropagation ( ) ;
66+ event . preventDefault ( ) ;
67+ const offsetY = dragBox . getBoundingClientRect ( ) . top ;
68+ const innerY = event . clientY - offsetY ;
69+
70+ clientOffset . clientY = event . clientY ;
71+ this . isLongPress = false ;
72+
73+ this . longPressTimeout = setTimeout ( ( ) => {
74+ this . isLongPress = true ;
75+ document . onmousemove = ( ev : MouseEvent ) => {
76+ if ( ! this . isLongPress ) { return ; }
77+
78+ let newTop = ev . clientY - innerY ;
79+
80+ // 确保 dragBox 在窗口可视范围内
81+ const dragDivTop = window . innerHeight - dragBox . offsetHeight ;
82+
83+ if ( newTop < 0 ) { newTop = 0 ; }
84+ if ( newTop > dragDivTop ) { newTop = dragDivTop ; }
85+
86+ dragBox . style . top = newTop + 'px' ;
87+ dragBox . style . left = '-48px' ;
88+
89+ ev . preventDefault ( ) ;
90+ ev . stopPropagation ( ) ;
91+ } ;
92+ } , 300 ) ; // 300ms 作为长按检测时间
93+
94+ document . onmouseup = ( ) => {
95+ document . onmousemove = null ;
96+ document . onmouseup = null ;
97+
98+ if ( ! this . isLongPress ) {
99+ clearTimeout ( this . longPressTimeout ) ; // 确保清除长按检测
100+ this . isShow = ! this . isShow ;
101+ }
102+
103+ setTimeout ( ( ) => {
104+ this . isLongPress = false ;
105+ } , 500 ) ;
106+ } ;
107+ } , false ) ;
108+
109+ element . addEventListener ( 'mouseup' , ( event : MouseEvent ) => {
110+ const clientY = event . clientY ;
111+ if (
112+ this . isDifferenceWithinThreshold ( clientY , clientOffset . clientY )
113+ && ( event . target === dragButton || this . isDescendant ( event . target as Element , dragButton ) )
114+ ) {
115+ this . isShow = ! this . isShow ;
78116 }
79- ev . preventDefault ( ) ;
80- ev . stopPropagation ( ) ;
81- } ;
82- document . onmouseup = function ( ) {
83- document . onmousemove = null ;
84- document . onmouseup = null ;
85- } ;
86- } , false ) ;
87- dragBox . addEventListener ( 'mouseup' , ( event ) => {
88- const clientX = event . clientX ;
89- const clientY = event . clientY ;
90- if (
91- this . isDifferenceWithinThreshold ( clientX , clientOffset . clientX )
92- && this . isDifferenceWithinThreshold ( clientY , clientOffset . clientY )
93- && ( event . target === dragButton || this . isDescendant ( event . target , dragButton ) )
94- ) {
95- this . isShow = ! this . isShow ;
96- }
117+ setTimeout ( ( ) => {
118+ this . isDragging = false ;
119+ } , 500 ) ;
120+ } ) ;
97121 } ) ;
98122 }
99123
100- isDescendant ( element , ancestor ) {
101- if ( element . parentNode === ancestor ) {
102- return true ;
124+ isDescendant ( element : Element , ancestor : Element ) {
125+ while ( element ) {
126+ if ( element === ancestor ) {
127+ return true ;
128+ }
129+ element = element . parentElement ;
103130 }
104131 return false ;
105132 }
0 commit comments