@bagelink/vue 0.0.753 → 0.0.755
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/components/Zoomer.vue.d.ts +40 -0
- package/dist/components/Zoomer.vue.d.ts.map +1 -0
- package/dist/components/index.d.ts +1 -0
- package/dist/components/index.d.ts.map +1 -1
- package/dist/components/lightbox/Lightbox.vue.d.ts.map +1 -1
- package/dist/components/lightbox/lightbox.types.d.ts +2 -0
- package/dist/components/lightbox/lightbox.types.d.ts.map +1 -1
- package/dist/index.cjs +716 -241
- package/dist/index.mjs +716 -241
- package/dist/style.css +33 -14
- package/dist/utils/index.d.ts +1 -1
- package/dist/utils/index.d.ts.map +1 -1
- package/dist/utils/tapDetector.d.ts +30 -0
- package/dist/utils/tapDetector.d.ts.map +1 -0
- package/package.json +1 -1
- package/src/components/Zoomer.vue +377 -0
- package/src/components/index.ts +1 -0
- package/src/components/lightbox/Lightbox.vue +28 -4
- package/src/components/lightbox/index.ts +2 -2
- package/src/components/lightbox/lightbox.types.ts +2 -0
- package/src/utils/index.ts +12 -4
- package/src/utils/tapDetector.ts +119 -0
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
type TapCallback = (event: { clientX: number, clientY: number }) => void
|
|
2
|
+
type CallbackType = 'single' | 'double'
|
|
3
|
+
|
|
4
|
+
export default class TapDetector {
|
|
5
|
+
private singleTapCallbacks: TapCallback[] = []
|
|
6
|
+
private doubleTapCallbacks: TapCallback[] = []
|
|
7
|
+
private isTouchMode = false
|
|
8
|
+
private lastTapTimestamp = 0
|
|
9
|
+
private tappedCount = 0
|
|
10
|
+
private touchMovedLength = 0
|
|
11
|
+
private lastPointerX = 0
|
|
12
|
+
private lastPointerY = 0
|
|
13
|
+
|
|
14
|
+
attach(dom: HTMLElement): void {
|
|
15
|
+
if (!(dom instanceof Element)) {
|
|
16
|
+
console.error('TapDetector.attach: arg must be an Element')
|
|
17
|
+
return
|
|
18
|
+
}
|
|
19
|
+
dom.addEventListener('touchstart', this.onTouchStart)
|
|
20
|
+
dom.addEventListener('touchmove', this.onTouchMove)
|
|
21
|
+
dom.addEventListener('touchend', this.onTouchEnd)
|
|
22
|
+
dom.addEventListener('mousedown', this.onMouseDown)
|
|
23
|
+
dom.addEventListener('mouseup', this.onMouseUp)
|
|
24
|
+
dom.addEventListener('mousemove', this.onMouseMove)
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
detach(dom: HTMLElement): void {
|
|
28
|
+
dom.removeEventListener('touchstart', this.onTouchStart)
|
|
29
|
+
dom.removeEventListener('touchmove', this.onTouchMove)
|
|
30
|
+
dom.removeEventListener('touchend', this.onTouchEnd)
|
|
31
|
+
dom.removeEventListener('mousedown', this.onMouseDown)
|
|
32
|
+
dom.removeEventListener('mouseup', this.onMouseUp)
|
|
33
|
+
dom.removeEventListener('mousemove', this.onMouseMove)
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
onSingleTap(callback: TapCallback): void {
|
|
37
|
+
if (typeof callback === 'function' && !this.singleTapCallbacks.includes(callback)) {
|
|
38
|
+
this.singleTapCallbacks.push(callback)
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
onDoubleTap(callback: TapCallback): void {
|
|
43
|
+
if (typeof callback === 'function' && !this.doubleTapCallbacks.includes(callback)) {
|
|
44
|
+
this.doubleTapCallbacks.push(callback)
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
private triggerCallbacks(callbackType: CallbackType, event: { clientX: number, clientY: number }): void {
|
|
49
|
+
const callbacks = callbackType === 'single' ? this.singleTapCallbacks : this.doubleTapCallbacks
|
|
50
|
+
callbacks.forEach((callback) => { callback(event) })
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
private onTouchStart = (event: TouchEvent): void => {
|
|
54
|
+
this.isTouchMode = true
|
|
55
|
+
if (event.touches.length === 1) {
|
|
56
|
+
this.onPointerDown(event.touches[0].clientX, event.touches[0].clientY)
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
private onTouchMove = (event: TouchEvent): void => {
|
|
61
|
+
if (event.touches.length === 1) {
|
|
62
|
+
this.onPointerMove(event.touches[0].clientX, event.touches[0].clientY)
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
private onTouchEnd = (): void => {
|
|
67
|
+
if (this.isTouchMode) this.onPointerUp()
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
private onMouseDown = (event: MouseEvent): void => {
|
|
71
|
+
if (!this.isTouchMode) {
|
|
72
|
+
this.onPointerDown(event.clientX, event.clientY)
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
private onMouseMove = (event: MouseEvent): void => {
|
|
77
|
+
if (!this.isTouchMode && event.button === 0) {
|
|
78
|
+
this.onPointerMove(event.clientX, event.clientY)
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
private onMouseUp = (): void => {
|
|
83
|
+
if (!this.isTouchMode) this.onPointerUp()
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
private onPointerDown(x: number, y: number): void {
|
|
87
|
+
this.lastPointerX = x
|
|
88
|
+
this.lastPointerY = y
|
|
89
|
+
this.touchMovedLength = 0
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
private onPointerUp(): void {
|
|
93
|
+
const currTimeStamp = Date.now()
|
|
94
|
+
if (this.touchMovedLength < 10) {
|
|
95
|
+
if (currTimeStamp - this.lastTapTimestamp < 300) {
|
|
96
|
+
this.tappedCount++
|
|
97
|
+
} else {
|
|
98
|
+
this.tappedCount = 1
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
this.lastTapTimestamp = currTimeStamp
|
|
102
|
+
this.triggerCallbacks('single', { clientX: this.lastPointerX, clientY: this.lastPointerY })
|
|
103
|
+
|
|
104
|
+
if (this.tappedCount === 2) {
|
|
105
|
+
this.triggerCallbacks('double', { clientX: this.lastPointerX, clientY: this.lastPointerY })
|
|
106
|
+
this.tappedCount = 0
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
this.touchMovedLength = 0
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
private onPointerMove(x: number, y: number): void {
|
|
113
|
+
const deltaX = this.lastPointerX - x
|
|
114
|
+
const deltaY = this.lastPointerY - y
|
|
115
|
+
this.touchMovedLength += Math.sqrt(deltaX * deltaX + deltaY * deltaY)
|
|
116
|
+
this.lastPointerX = x
|
|
117
|
+
this.lastPointerY = y
|
|
118
|
+
}
|
|
119
|
+
}
|