Skip to content

Commit

Permalink
fixed: Fixed right icon drag issue
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhaoJiSen committed Jul 25, 2024
1 parent 466ccb7 commit 6651768
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 43 deletions.
2 changes: 1 addition & 1 deletion src/app/elements/chat/chat.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<i class="fa fa-cog"></i>
</div>
<div [hidden]="!chatAiEnabled" class="drag-button">
<img alt="" src="assets/icons/chat-ai.png"/>
<img alt="" class="chat-img" src="assets/icons/chat-ai.png"/>
</div>
</div>
<div *ngIf="chatAiEnabled" class="content">
Expand Down
1 change: 1 addition & 0 deletions src/app/elements/chat/chat.component.scss
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
position: absolute;
bottom: 20%;
left: -48px;
z-index: 999 !important;
}

.drag-button {
Expand Down
111 changes: 69 additions & 42 deletions src/app/elements/chat/chat.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ export class ElementChatComponent implements OnInit, OnDestroy {
element: any;
iframeURL: string;
currentView: View;
private isLongPress: boolean;
private longPressTimeout: number;
private isDragging: boolean;

constructor(
public viewSrv: ViewService,
Expand Down Expand Up @@ -54,52 +57,76 @@ export class ElementChatComponent implements OnInit, OnDestroy {
const clientOffset: any = {};
const dragBox = document.getElementById('dragBox');
const dragButton = document.querySelector('.drag-button');
dragBox.addEventListener('mousedown', (event) => {
event.stopPropagation();
const offsetX = dragBox.getBoundingClientRect().left;
const offsetY = dragBox.getBoundingClientRect().top;
const innerX = event.clientX - offsetX;
const innerY = event.clientY - offsetY;

clientOffset.clientX = event.clientX;
clientOffset.clientY = event.clientY;
document.onmousemove = function (ev: any) {
dragBox.style.left = ev.clientX - innerX + 'px';
dragBox.style.top = ev.clientY - innerY + 'px';
const dragDivTop = window.innerHeight - dragBox.getBoundingClientRect().height;
const dragDivLeft = window.innerWidth - dragBox.getBoundingClientRect().width;
dragBox.style.left = dragDivLeft + 'px';
dragBox.style.left = '-48px';
if (dragBox.getBoundingClientRect().top <= 0) {
dragBox.style.top = '0px';
}
if (dragBox.getBoundingClientRect().top >= dragDivTop) {
dragBox.style.top = dragDivTop + 'px';
const chatImg = dragButton.querySelector('.chat-img');
const elements = [dragButton, chatImg];

elements.forEach(element => {
element.addEventListener('mousedown', (event: MouseEvent) => {
event.stopPropagation();
event.preventDefault();
const offsetY = dragBox.getBoundingClientRect().top;
const innerY = event.clientY - offsetY;

clientOffset.clientY = event.clientY;
this.isLongPress = false;

this.longPressTimeout = setTimeout(() => {
this.isLongPress = true;
document.onmousemove = (ev: MouseEvent) => {
if (!this.isLongPress) { return; }

let newTop = ev.clientY - innerY;

// 确保 dragBox 在窗口可视范围内
const dragDivTop = window.innerHeight - dragBox.offsetHeight;

if (newTop < 0) { newTop = 0; }
if (newTop > dragDivTop) { newTop = dragDivTop; }

dragBox.style.top = newTop + 'px';
dragBox.style.left = '-48px';

ev.preventDefault();
ev.stopPropagation();
};
}, 300); // 300ms 作为长按检测时间

document.onmouseup = () => {
document.onmousemove = null;
document.onmouseup = null;

if (!this.isLongPress) {
clearTimeout(this.longPressTimeout); // 确保清除长按检测
this.isShow = !this.isShow;
}

setTimeout(() => {
this.isLongPress = false;
}, 500);
};
}, false);

element.addEventListener('mouseup', (event: MouseEvent) => {
const clientY = event.clientY;
if (
this.isDifferenceWithinThreshold(clientY, clientOffset.clientY)
&& (event.target === dragButton || this.isDescendant(event.target as Element, dragButton))
) {
this.isShow = !this.isShow;
}
ev.preventDefault();
ev.stopPropagation();
};
document.onmouseup = function () {
document.onmousemove = null;
document.onmouseup = null;
};
}, false);
dragBox.addEventListener('mouseup', (event) => {
const clientX = event.clientX;
const clientY = event.clientY;
if (
this.isDifferenceWithinThreshold(clientX, clientOffset.clientX)
&& this.isDifferenceWithinThreshold(clientY, clientOffset.clientY)
&& (event.target === dragButton || this.isDescendant(event.target, dragButton))
) {
this.isShow = !this.isShow;
}
setTimeout(() => {
this.isDragging = false;
}, 500);
});
});
}

isDescendant(element, ancestor) {
if (element.parentNode === ancestor) {
return true;
isDescendant(element: Element, ancestor: Element) {
while (element) {
if (element === ancestor) {
return true;
}
element = element.parentElement;
}
return false;
}
Expand Down

0 comments on commit 6651768

Please sign in to comment.