forked from dalibo/pev2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdragscroll.ts
More file actions
70 lines (59 loc) · 1.83 KB
/
Copy pathdragscroll.ts
File metadata and controls
70 lines (59 loc) · 1.83 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
export default class Dragscroll {
private element: Element
private listener: boolean
private start: boolean
private startMousePositionX: number
private startMousePositionY: number
private startScrollPositionX: number
private startScrollPositionY: number
constructor(element: Element) {
this.element = element
this.listener = true
this.start = false
this.startMousePositionX = 0
this.startMousePositionY = 0
this.startScrollPositionX = 0
this.startScrollPositionY = 0
this.element.addEventListener("mousedown", (e: Event) => {
const event = e as MouseEvent
const target = e.target as Element
if (target.closest(".plan-node-body")) {
return
}
event.preventDefault()
this.clearSelection()
this.startMousePositionX = event.screenX
this.startMousePositionY = event.screenY
this.startScrollPositionX = this.element.scrollLeft
this.startScrollPositionY = this.element.scrollTop
this.start = true
})
document.documentElement.addEventListener("mouseup", (e: Event) => {
e.preventDefault()
this.startMousePositionX = 0
this.startMousePositionY = 0
this.startScrollPositionX = 0
this.startScrollPositionY = 0
this.start = false
})
this.element.addEventListener("mousemove", (e: Event) => {
const event = e as MouseEvent
if (this.listener && this.start) {
event.preventDefault()
this.element.scrollTo(
this.startScrollPositionX +
(this.startMousePositionX - event.screenX),
this.startScrollPositionY + (this.startMousePositionY - event.screenY)
)
}
})
}
private clearSelection() {
if (window.getSelection) {
const sel = window.getSelection()
if (sel) {
sel.removeAllRanges()
}
}
}
}