Skip to content

Commit d44de80

Browse files
perf: Assistant Entry Icon Drag-and-Drop Feature
1 parent a7a71c4 commit d44de80

File tree

1 file changed

+51
-2
lines changed

1 file changed

+51
-2
lines changed

frontend/public/assistant.js

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,55 @@
157157
closeviewport.classList.remove('sqlbot-assistant-viewportnone')
158158
}
159159
}
160-
const drag = (e) => {
160+
if (data.float_icon_drag) {
161+
chat_button.setAttribute('draggable', 'true')
162+
163+
let startX = 0
164+
let startY = 0
165+
const img = new Image()
166+
img.src = 'data:image/gif;base64,R0lGODlhAQABAIAAAAUEBAAAACwAAAAAAQABAAACAkQBADs='
167+
chat_button.addEventListener('dragstart', (e) => {
168+
startX = e.clientX - chat_button.offsetLeft
169+
startY = e.clientY - chat_button.offsetTop
170+
e.dataTransfer.setDragImage(img, 0, 0)
171+
})
172+
173+
chat_button.addEventListener('drag', (e) => {
174+
if (e.clientX && e.clientY) {
175+
const left = e.clientX - startX
176+
const top = e.clientY - startY
177+
178+
const maxX = window.innerWidth - chat_button.offsetWidth
179+
const maxY = window.innerHeight - chat_button.offsetHeight
180+
181+
chat_button.style.left = Math.min(Math.max(0, left), maxX) + 'px'
182+
chat_button.style.top = Math.min(Math.max(0, top), maxY) + 'px'
183+
}
184+
})
185+
186+
let touchStartX = 0
187+
let touchStartY = 0
188+
189+
chat_button.addEventListener('touchstart', (e) => {
190+
touchStartX = e.touches[0].clientX - chat_button.offsetLeft
191+
touchStartY = e.touches[0].clientY - chat_button.offsetTop
192+
e.preventDefault()
193+
})
194+
195+
chat_button.addEventListener('touchmove', (e) => {
196+
const left = e.touches[0].clientX - touchStartX
197+
const top = e.touches[0].clientY - touchStartY
198+
199+
const maxX = window.innerWidth - chat_button.offsetWidth
200+
const maxY = window.innerHeight - chat_button.offsetHeight
201+
202+
chat_button.style.left = Math.min(Math.max(0, left), maxX) + 'px'
203+
chat_button.style.top = Math.min(Math.max(0, top), maxY) + 'px'
204+
205+
e.preventDefault()
206+
})
207+
}
208+
/* const drag = (e) => {
161209
if (['touchmove', 'touchstart'].includes(e.type)) {
162210
chat_button.style.top = e.touches[0].clientY - chat_button_img.clientHeight / 2 + 'px'
163211
chat_button.style.left = e.touches[0].clientX - chat_button_img.clientHeight / 2 + 'px'
@@ -169,14 +217,15 @@
169217
chat_button.style.height = chat_button_img.clientHeight + 'px'
170218
}
171219
if (data.float_icon_drag) {
220+
chat_button.setAttribute('draggable', 'true')
172221
chat_button.addEventListener('drag', drag)
173222
chat_button.addEventListener('dragover', (e) => {
174223
e.preventDefault()
175224
})
176225
chat_button.addEventListener('dragend', drag)
177226
chat_button.addEventListener('touchstart', drag)
178227
chat_button.addEventListener('touchmove', drag)
179-
}
228+
} */
180229
viewport.onclick = viewport_func
181230
closeviewport.onclick = viewport_func
182231
}

0 commit comments

Comments
 (0)