1- import { Component , Element , Event , EventEmitter , Method , Prop } from '@stencil/core' ;
1+ import { Component , Element , Event , EventEmitter , Method , Prop , State } from '@stencil/core' ;
22
33import { DeckdeckgoSlide , DeckdeckgoSlideUtils } from '../deckdeckgo-slide' ;
44import { DeckdeckgoUtils } from '../../utils/deckdeckgo-utils' ;
@@ -19,7 +19,7 @@ export class DeckdeckgoSlideCode implements DeckdeckgoSlide {
1919
2020 @Event ( ) slideDidLoad : EventEmitter < void > ;
2121
22- @Event ( ) scrolling : EventEmitter < void > ;
22+ @Event ( ) scrolling : EventEmitter < boolean > ;
2323
2424 @Prop ( ) src : string ;
2525
@@ -29,8 +29,14 @@ export class DeckdeckgoSlideCode implements DeckdeckgoSlide {
2929
3030 @Prop ( ) language : string = 'javascript' ;
3131
32- private startX : number = null ;
33- private action : DeckdeckgoSlideCodeAction = null ;
32+ @State ( )
33+ private mobile : boolean = false ;
34+
35+ private action : DeckdeckgoSlideCodeAction = DeckdeckgoSlideCodeAction . SWIPE ;
36+
37+ componentWillLoad ( ) {
38+ this . mobile = DeckdeckgoSlideUtils . isMobile ( ) ;
39+ }
3440
3541 async componentDidLoad ( ) {
3642 await DeckdeckgoUtils . hideLazyLoadImages ( this . el ) ;
@@ -75,7 +81,7 @@ export class DeckdeckgoSlideCode implements DeckdeckgoSlide {
7581 private scrollToNext ( swipeLeft : boolean ) : Promise < boolean > {
7682 const element : HTMLElement = this . el . shadowRoot . querySelector ( 'deckgo-highlight-code' ) ;
7783
78- if ( element ) {
84+ if ( element && element . hasOwnProperty ( 'scrollToNext' ) ) {
7985 return ( element as any ) . scrollToNext ( swipeLeft ) ;
8086 } else {
8187 return new Promise < boolean > ( ( resolve ) => {
@@ -88,7 +94,7 @@ export class DeckdeckgoSlideCode implements DeckdeckgoSlide {
8894 return new Promise < void > ( async ( resolve ) => {
8995 const element : HTMLElement = this . el . shadowRoot . querySelector ( 'deckgo-highlight-code' ) ;
9096
91- if ( element ) {
97+ if ( element && element . hasOwnProperty ( 'zoomCode' ) ) {
9298 await ( element as any ) . zoomCode ( zoom ) ;
9399 }
94100
@@ -106,34 +112,26 @@ export class DeckdeckgoSlideCode implements DeckdeckgoSlide {
106112 return DeckdeckgoSlideUtils . lazyLoadContent ( this . el ) ;
107113 }
108114
109- private touchScrollStart ( event : TouchEvent ) {
110- this . startX = DeckdeckgoUtils . unifyEvent ( event ) . clientX ;
111- }
112-
113- private touchScrollMove ( event : TouchEvent ) {
114- if ( this . action ) {
115- return ;
116- }
117-
118- const currentX : number = DeckdeckgoUtils . unifyEvent ( event ) . clientX ;
119-
120- const swipeLeft : boolean = this . startX > currentX ;
121- const swipeRight : boolean = this . startX < currentX ;
115+ private switchAction ( ) : Promise < void > {
116+ return new Promise < void > ( ( resolve ) => {
117+ if ( ! this . mobile ) {
118+ // Scrolling is allowed on not mobile devices
119+ resolve ( ) ;
120+ return ;
121+ }
122122
123- this . action = swipeLeft || swipeRight ? DeckdeckgoSlideCodeAction . SWIPE : DeckdeckgoSlideCodeAction . SCROLL ;
123+ this . action = this . action === DeckdeckgoSlideCodeAction . SWIPE ? DeckdeckgoSlideCodeAction . SCROLL : DeckdeckgoSlideCodeAction . SWIPE ;
124124
125- if ( ! swipeLeft && ! swipeRight ) {
126- this . scrolling . emit ( ) ;
127- this . unlockScroll ( ) ;
128- } else {
129- this . lockScroll ( ) ;
130- }
131- }
125+ this . scrolling . emit ( this . action === DeckdeckgoSlideCodeAction . SCROLL ) ;
132126
133- private touchScrollEnd ( ) {
134- this . action = null ;
127+ if ( this . action === DeckdeckgoSlideCodeAction . SCROLL ) {
128+ this . unlockScroll ( ) ;
129+ } else {
130+ this . lockScroll ( ) ;
131+ }
135132
136- this . unlockScroll ( ) ;
133+ resolve ( ) ;
134+ } ) ;
137135 }
138136
139137 private lockScroll ( ) {
@@ -143,23 +141,21 @@ export class DeckdeckgoSlideCode implements DeckdeckgoSlide {
143141
144142 private unlockScroll ( ) {
145143 const container : HTMLElement = this . el . shadowRoot . querySelector ( 'div.deckgo-slide-code-container' ) ;
146- container . style . removeProperty ( 'overflow-y' ) ;
147- }
148-
149- private emitScrolling ( ) {
150- if ( this . action === DeckdeckgoSlideCodeAction . SCROLL ) {
151- this . scrolling . emit ( ) ;
152- }
144+ container . style . setProperty ( 'overflow-y' , 'auto' ) ;
153145 }
154146
155147 // DeckDeckGo
156148 render ( ) {
149+
150+ let containerStyle : string = 'deckgo-slide-code-container' ;
151+ if ( this . mobile ) {
152+ containerStyle += ' deckgo-slide-code-container-mobile' ;
153+ }
154+
157155 return < div class = "deckgo-slide"
158- onTouchStart = { ( event : TouchEvent ) => this . touchScrollStart ( event ) }
159- onTouchMove = { ( event : TouchEvent ) => this . touchScrollMove ( event ) }
160- onTouchEnd = { ( ) => this . touchScrollEnd ( ) } >
156+ onClick = { ( ) => this . switchAction ( ) } >
161157 < slot name = "title" > </ slot >
162- < div class = "deckgo-slide-code-container" onScroll = { ( ) => this . emitScrolling ( ) } >
158+ < div class = { containerStyle } >
163159 < deckgo-highlight-code src = { this . src } anchor = { this . anchor } anchorZoom = { this . anchorZoom } hideAnchor = { this . hideAnchor } language = { this . language } > </ deckgo-highlight-code >
164160 </ div >
165161 < slot name = "code" > </ slot >
0 commit comments