@@ -13,12 +13,22 @@ import { Config } from '../interfaces';
13
13
import { MouseButton } from '../../handlers/gestureHandlerCommon' ;
14
14
import KeyboardEventManager from './KeyboardEventManager' ;
15
15
16
+ interface DefaultViewStyles {
17
+ userSelect : string ;
18
+ touchAction : string ;
19
+ }
20
+
16
21
export class GestureHandlerWebDelegate
17
22
implements GestureHandlerDelegate < HTMLElement , IGestureHandler >
18
23
{
24
+ private isInitialized = false ;
19
25
private view ! : HTMLElement ;
20
26
private gestureHandler ! : IGestureHandler ;
21
27
private eventManagers : EventManager < unknown > [ ] = [ ] ;
28
+ private defaultViewStyles : DefaultViewStyles = {
29
+ userSelect : '' ,
30
+ touchAction : '' ,
31
+ } ;
22
32
23
33
getView ( ) : HTMLElement {
24
34
return this . view ;
@@ -31,19 +41,21 @@ export class GestureHandlerWebDelegate
31
41
) ;
32
42
}
33
43
44
+ this . isInitialized = true ;
45
+
34
46
this . gestureHandler = handler ;
35
47
this . view = findNodeHandle ( viewRef ) as unknown as HTMLElement ;
36
48
37
- const config = handler . getConfig ( ) ;
49
+ this . defaultViewStyles = {
50
+ userSelect : this . view . style . userSelect ,
51
+ touchAction : this . view . style . touchAction ,
52
+ } ;
38
53
39
- this . addContextMenuListeners ( config ) ;
54
+ const config = handler . getConfig ( ) ;
40
55
41
- this . view . style [ 'userSelect' ] = config . userSelect ?? 'none' ;
42
- this . view . style [ 'webkitUserSelect' ] = config . userSelect ?? 'none' ;
43
-
44
- this . view . style [ 'touchAction' ] = config . touchAction ?? 'none' ;
45
- // @ts -ignore This one disables default events on Safari
46
- this . view . style [ 'WebkitTouchCallout' ] = 'none' ;
56
+ this . setUserSelect ( config . enabled ) ;
57
+ this . setTouchAction ( config . enabled ) ;
58
+ this . setContextMenu ( config . enabled ) ;
47
59
48
60
this . eventManagers . push ( new PointerEventManager ( this . view ) ) ;
49
61
this . eventManagers . push ( new TouchEventManager ( this . view ) ) ;
@@ -119,6 +131,51 @@ export class GestureHandlerWebDelegate
119
131
e . stopPropagation ( ) ;
120
132
}
121
133
134
+ private setUserSelect ( isHandlerEnabled : boolean ) {
135
+ const { userSelect } = this . gestureHandler . getConfig ( ) ;
136
+
137
+ this . view . style [ 'userSelect' ] = isHandlerEnabled
138
+ ? userSelect ?? 'none'
139
+ : this . defaultViewStyles . userSelect ;
140
+
141
+ this . view . style [ 'webkitUserSelect' ] = isHandlerEnabled
142
+ ? userSelect ?? 'none'
143
+ : this . defaultViewStyles . userSelect ;
144
+ }
145
+
146
+ private setTouchAction ( isHandlerEnabled : boolean ) {
147
+ const { touchAction } = this . gestureHandler . getConfig ( ) ;
148
+
149
+ this . view . style [ 'touchAction' ] = isHandlerEnabled
150
+ ? touchAction ?? 'none'
151
+ : this . defaultViewStyles . touchAction ;
152
+
153
+ // @ts -ignore This one disables default events on Safari
154
+ this . view . style [ 'WebkitTouchCallout' ] = isHandlerEnabled
155
+ ? touchAction ?? 'none'
156
+ : this . defaultViewStyles . touchAction ;
157
+ }
158
+
159
+ private setContextMenu ( isHandlerEnabled : boolean ) {
160
+ const config = this . gestureHandler . getConfig ( ) ;
161
+
162
+ if ( isHandlerEnabled ) {
163
+ this . addContextMenuListeners ( config ) ;
164
+ } else {
165
+ this . removeContextMenuListeners ( config ) ;
166
+ }
167
+ }
168
+
169
+ onEnabledChange ( enabled : boolean ) : void {
170
+ if ( ! this . isInitialized ) {
171
+ return ;
172
+ }
173
+
174
+ this . setUserSelect ( enabled ) ;
175
+ this . setTouchAction ( enabled ) ;
176
+ this . setContextMenu ( enabled ) ;
177
+ }
178
+
122
179
onBegin ( ) : void {
123
180
// no-op for now
124
181
}
0 commit comments