-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
92 lines (76 loc) · 2.3 KB
/
index.js
File metadata and controls
92 lines (76 loc) · 2.3 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import Hammer from 'hammerjs'
const CONTAINER_STYLE = {
border: '2px red solid',
position: 'absolute',
display: 'flex',
'z-index': 9999999,
width: '200px',
height: '200px',
left: '0px',
top: '0px',
overflow: 'scroll',
'box-sizing': 'border-box'
}
const TARGET_STYLE = {
flex: 1,
width: '100%',
'box-sizing': 'border-box',
padding: '30px',
overflow: 'auto'
}
const DRAGGER_STYLE = {
width: '40px',
height: '40px'
}
class WebDebugBox {
init() {
console.log('There is a web debug box running on your website')
this.container = document.createElement('div')
this.target = document.createElement('pre')
this.dragger = document.createElement('img')
this.dragger.setAttribute('src', require('./dragger.png'))
this.container.appendChild(this.target)
this.container.appendChild(this.dragger)
this.setStyle(this.container, CONTAINER_STYLE)
this.setStyle(this.target, TARGET_STYLE)
this.setStyle(this.dragger, DRAGGER_STYLE)
this._addBoxToPage()
this._bindEvent()
this.currentPointerCenterX = 0
this.currentPointerCenterY = 0
}
showData(data) {
this.target.innerHTML = JSON.stringify(data, Object.keys(data), 2)
}
setStyle(target, style) {
Object.keys(style).forEach(key => target.style[key] = style[key])
}
setClass(className) {
this.container.className = className
}
_addBoxToPage() {
document.body.appendChild(this.container)
}
_bindEvent() {
const hammer = new Hammer(this.dragger)
const pan = new Hammer.Pan({threshold: 1})
hammer.add(pan)
hammer.on('panstart', (event) => {
this.currentPointerCenterX = event.center.x
this.currentPointerCenterY = event.center.y
})
hammer.on('panmove', (event) => {
const movedX = event.center.x - this.currentPointerCenterX
const movedY = event.center.y - this.currentPointerCenterY
this._panBox(movedX, movedY)
this.currentPointerCenterX = event.center.x
this.currentPointerCenterY = event.center.y
})
}
_panBox(movedX, movedY) {
const left = `${Number.parseFloat(this.container.style.left) + Number.parseFloat(movedX)}px`
const top = `${Number.parseFloat(this.container.style.top) + Number.parseFloat(movedY)}px`
this.setStyle(this.container, { left, top })
}
}
export default new WebDebugBox()