-
Notifications
You must be signed in to change notification settings - Fork 1
/
draggable-iframe.js
59 lines (48 loc) · 1.58 KB
/
draggable-iframe.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
var baseMouseX, baseMouseY
var dragging = false;
document.addEventListener('mousedown', handleMousedown)
document.addEventListener('touchstart', handleTouchstart, { passive: false })
function handleMousedown (evt) {
return handleDragStart(evt)
}
function handleTouchstart (evt) {
evt.preventDefault()
return handleDragStart(evt.touches[0])
}
function handleDragStart({ clientX, clientY }) {
dragging = true;
baseMouseX = clientX
baseMouseY = clientY
window.parent.postMessage({
msg: 'NOTEPAD_DRAG_START',
mouseX: clientX,
mouseY: clientY
}, '*')
document.addEventListener('mouseup', handleDragEnd)
document.addEventListener('touchend', handleDragEnd)
document.addEventListener('mousemove', handleMousemove)
document.addEventListener('touchmove', handleTouchmove, { passive: false })
}
function handleMousemove (evt) {
return handleDragging(evt)
}
function handleTouchmove(evt) {
evt.preventDefault()
return handleDragging(evt.touches[0])
}
function handleDragging({ clientX, clientY }) {
window.parent.postMessage({
msg: 'NOTEPAD_DRAG_MOUSEMOVE',
offsetX: clientX - baseMouseX,
offsetY: clientY - baseMouseY
}, '*')
}
function handleDragEnd () {
window.parent.postMessage({
msg: 'NOTEPAD_DRAG_END'
}, '*')
document.removeEventListener('mouseup', handleDragEnd)
document.removeEventListener('touchend', handleDragEnd)
document.removeEventListener('mousemove', handleMousemove)
document.removeEventListener('mousemove', handleTouchmove)
}