@leafer-ui/interaction-miniapp 1.0.0-rc.10

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023-present, Chao (Leafer) Wan
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1 @@
1
+ # @leafer-ui/interaction-miniapp
package/package.json ADDED
@@ -0,0 +1,30 @@
1
+ {
2
+ "name": "@leafer-ui/interaction-miniapp",
3
+ "version": "1.0.0-rc.10",
4
+ "description": "@leafer-ui/interaction-miniapp",
5
+ "author": "Chao (Leafer) Wan",
6
+ "license": "MIT",
7
+ "main": "src/index.ts",
8
+ "types": "types/index.d.ts",
9
+ "files": [
10
+ "src",
11
+ "types",
12
+ "dist"
13
+ ],
14
+ "repository": {
15
+ "type": "git",
16
+ "url": "https://github.com/leaferjs/ui.git"
17
+ },
18
+ "homepage": "https://github.com/leaferjs/ui/tree/main/packages/interaction/interaction-miniapp",
19
+ "bugs": "https://github.com/leaferjs/ui/issues",
20
+ "keywords": [
21
+ "leafer-ui",
22
+ "leaferjs"
23
+ ],
24
+ "dependencies": {
25
+ "@leafer-ui/core": "1.0.0-rc.10"
26
+ },
27
+ "devDependencies": {
28
+ "@leafer/interface": "1.0.0-rc.10"
29
+ }
30
+ }
@@ -0,0 +1,140 @@
1
+ import { IKeepTouchData, IPointData } from '@leafer/interface'
2
+ import { InteractionBase, InteractionHelper } from '@leafer-ui/core'
3
+
4
+ import { PointerEventHelper } from './PointerEventHelper'
5
+
6
+
7
+ interface IClientPoint {
8
+ clientX: number
9
+ clientY: number
10
+ x?: number
11
+ y?: number
12
+ }
13
+
14
+ interface ITouch {
15
+ readonly clientX: number
16
+ readonly clientY: number
17
+ readonly identifier: number
18
+ readonly pageX: number
19
+ readonly pageY: number
20
+ readonly x?: number
21
+ readonly y?: number
22
+ }
23
+
24
+
25
+ export class Interaction extends InteractionBase {
26
+
27
+ protected useMultiTouch: boolean
28
+ protected useTouch: boolean
29
+ protected touches?: ITouch[]
30
+
31
+ protected __listenEvents(): void {
32
+ super.__listenEvents()
33
+ if (this.config.eventer) this.config.eventer.receiveEvent = this.receive.bind(this)
34
+
35
+ }
36
+
37
+ public receive(e: any): void {
38
+ switch (e.type) {
39
+ case 'touchstart':
40
+ this.onTouchStart(e)
41
+ break
42
+ case 'touchmove':
43
+ this.onTouchMove(e)
44
+ break
45
+ case 'touchend':
46
+ this.onTouchEnd(e)
47
+ break
48
+ case 'touchcancel':
49
+ this.onTouchCancel()
50
+ }
51
+ }
52
+
53
+ protected getLocal(p: IClientPoint, updateClient?: boolean): IPointData {
54
+ if (updateClient) this.canvas.updateClientBounds()
55
+ if (p.x !== undefined) {
56
+ return { x: p.x, y: p.y } // Canvas
57
+ } else {
58
+ const { clientBounds } = this.canvas
59
+ return { x: p.clientX - clientBounds.x, y: p.clientY - clientBounds.y }
60
+ }
61
+ }
62
+
63
+ protected getTouches(touches: any): ITouch[] {
64
+ return touches
65
+ }
66
+
67
+
68
+ // touch
69
+ protected onTouchStart(e: TouchEvent): void {
70
+ this.multiTouchStart(e)
71
+
72
+ const touch = PointerEventHelper.getTouch(e)
73
+ this.pointerDown(PointerEventHelper.convertTouch(e, this.getLocal(touch, true)))
74
+ }
75
+
76
+ protected onTouchMove(e: TouchEvent): void {
77
+ this.multiTouchMove(e)
78
+ if (this.useMultiTouch) return
79
+ const touch = PointerEventHelper.getTouch(e)
80
+ this.pointerMove(PointerEventHelper.convertTouch(e, this.getLocal(touch)))
81
+ }
82
+
83
+ protected onTouchEnd(e: TouchEvent): void {
84
+ this.multiTouchEnd()
85
+
86
+ const touch = PointerEventHelper.getTouch(e)
87
+ this.pointerUp(PointerEventHelper.convertTouch(e, this.getLocal(touch)))
88
+ }
89
+
90
+ protected onTouchCancel(): void {
91
+ this.pointerCancel()
92
+ }
93
+
94
+
95
+ // multiTouch
96
+ protected multiTouchStart(e: TouchEvent): void {
97
+ this.useMultiTouch = (e.touches.length >= 2)
98
+ this.touches = this.useMultiTouch ? this.getTouches(e.touches) : undefined
99
+ if (this.useMultiTouch) this.pointerCancel()
100
+ }
101
+
102
+ protected multiTouchMove(e: TouchEvent): void {
103
+ if (!this.useMultiTouch) return
104
+ if (e.touches.length > 1) {
105
+ const touches = this.getTouches(e.touches)
106
+ const list = this.getKeepTouchList(this.touches, touches)
107
+ if (list.length > 1) {
108
+ this.multiTouch(InteractionHelper.getBase(e), list)
109
+ this.touches = touches
110
+ }
111
+ }
112
+ }
113
+
114
+ protected multiTouchEnd(): void {
115
+ this.touches = null
116
+ this.useMultiTouch = false
117
+ this.transformEnd()
118
+ }
119
+
120
+ protected getKeepTouchList(old: ITouch[], touches: ITouch[]): IKeepTouchData[] {
121
+ let to: ITouch
122
+ const list: IKeepTouchData[] = []
123
+ old.forEach(from => {
124
+ to = touches.find(touch => touch.identifier === from.identifier)
125
+ if (to) list.push({ from: this.getLocal(from), to: this.getLocal(to) })
126
+ })
127
+ return list
128
+ }
129
+
130
+ protected getLocalTouchs(points: ITouch[]): IPointData[] {
131
+ return points.map(point => this.getLocal(point))
132
+ }
133
+
134
+
135
+ public destroy(): void {
136
+ super.destroy()
137
+ this.touches = null
138
+ }
139
+
140
+ }
@@ -0,0 +1,25 @@
1
+ import { IPointData, IPointerEvent } from '@leafer/interface'
2
+ import { InteractionHelper } from '@leafer-ui/core'
3
+
4
+
5
+ export const PointerEventHelper = {
6
+
7
+ convertTouch(e: TouchEvent, local: IPointData): IPointerEvent {
8
+ const touch = PointerEventHelper.getTouch(e)
9
+ const base = InteractionHelper.getBase(e)
10
+ return {
11
+ ...base,
12
+ x: local.x,
13
+ y: local.y,
14
+ width: 1,
15
+ height: 1,
16
+ pointerType: 'touch',
17
+ pressure: touch.force || 1,
18
+ }
19
+ },
20
+
21
+ getTouch(e: TouchEvent): Touch {
22
+ return e.touches[0] || e.changedTouches[0]
23
+ }
24
+
25
+ }
package/src/index.ts ADDED
@@ -0,0 +1 @@
1
+ export { Interaction } from './Interaction'
@@ -0,0 +1,39 @@
1
+ import { IPointData, IKeepTouchData } from '@leafer/interface';
2
+ import { InteractionBase } from '@leafer-ui/core';
3
+
4
+ interface IClientPoint {
5
+ clientX: number;
6
+ clientY: number;
7
+ x?: number;
8
+ y?: number;
9
+ }
10
+ interface ITouch {
11
+ readonly clientX: number;
12
+ readonly clientY: number;
13
+ readonly identifier: number;
14
+ readonly pageX: number;
15
+ readonly pageY: number;
16
+ readonly x?: number;
17
+ readonly y?: number;
18
+ }
19
+ declare class Interaction extends InteractionBase {
20
+ protected useMultiTouch: boolean;
21
+ protected useTouch: boolean;
22
+ protected touches?: ITouch[];
23
+ protected __listenEvents(): void;
24
+ receive(e: any): void;
25
+ protected getLocal(p: IClientPoint, updateClient?: boolean): IPointData;
26
+ protected getTouches(touches: any): ITouch[];
27
+ protected onTouchStart(e: TouchEvent): void;
28
+ protected onTouchMove(e: TouchEvent): void;
29
+ protected onTouchEnd(e: TouchEvent): void;
30
+ protected onTouchCancel(): void;
31
+ protected multiTouchStart(e: TouchEvent): void;
32
+ protected multiTouchMove(e: TouchEvent): void;
33
+ protected multiTouchEnd(): void;
34
+ protected getKeepTouchList(old: ITouch[], touches: ITouch[]): IKeepTouchData[];
35
+ protected getLocalTouchs(points: ITouch[]): IPointData[];
36
+ destroy(): void;
37
+ }
38
+
39
+ export { Interaction };