@@ -2,6 +2,11 @@ import {Component, Element, Event, EventEmitter, Method, Prop, State} from '@ste
22
33import { DeckdeckgoSlide , DeckDeckGoSlideUtils } from '../deckdeckgo-slide' ;
44
5+ enum DeckdeckgoSlideCodeAction {
6+ SWIPE ,
7+ SCROLL
8+ }
9+
510@Component ( {
611 tag : 'deckgo-slide-code' ,
712 styleUrl : 'deckdeckgo-slide-code.scss' ,
@@ -13,6 +18,8 @@ export class DeckdeckgoSlideCode implements DeckdeckgoSlide {
1318
1419 @Event ( ) slideDidLoad : EventEmitter < void > ;
1520
21+ @Event ( ) scrolling : EventEmitter < void > ;
22+
1623 @Prop ( ) srcFile : string ;
1724
1825 @Prop ( ) anchor : string = '// DeckDeckGo' ;
@@ -24,6 +31,10 @@ export class DeckdeckgoSlideCode implements DeckdeckgoSlide {
2431 @State ( )
2532 private code : string [ ] = [ ] ;
2633
34+ private startX : number = null ;
35+ private detectThreshold : number = 10 ;
36+ private action : DeckdeckgoSlideCodeAction = null ;
37+
2738 async componentDidLoad ( ) {
2839 this . slideDidLoad . emit ( ) ;
2940
@@ -134,12 +145,61 @@ export class DeckdeckgoSlideCode implements DeckdeckgoSlide {
134145
135146 // DeckDeckGo
136147 render ( ) {
137- return < div class = "deckgo-slide" >
148+ return < div class = "deckgo-slide"
149+ onTouchStart = { ( event : TouchEvent ) => this . touchScrollStart ( event ) }
150+ onTouchMove = { ( event : TouchEvent ) => this . touchScrollMove ( event ) }
151+ onTouchEnd = { ( ) => this . touchScrollEnd ( ) } >
138152 < slot name = "title" > </ slot >
139- < div class = "deckgo-code-container" > < code > { this . renderCode ( ) } </ code > </ div >
153+ < div class = "deckgo-code-container" onScroll = { ( ) => this . emitScrolling ( ) } > < code > { this . renderCode ( ) } </ code > </ div >
140154 </ div > ;
141155 }
142156
157+ private touchScrollStart ( event : TouchEvent ) {
158+ this . startX = event . changedTouches ? event . changedTouches [ 0 ] . clientX : null ;
159+ }
160+
161+ private touchScrollMove ( event : TouchEvent ) {
162+ if ( this . action ) {
163+ return ;
164+ }
165+
166+ const currentX : number = event . changedTouches ? event . changedTouches [ 0 ] . clientX : null ;
167+
168+ const swipeLeft : boolean = this . startX > currentX + this . detectThreshold ;
169+ const swipeRight : boolean = this . startX < currentX - this . detectThreshold ;
170+
171+ this . action = swipeLeft || swipeRight ? DeckdeckgoSlideCodeAction . SWIPE : DeckdeckgoSlideCodeAction . SCROLL ;
172+
173+ if ( ! swipeLeft && ! swipeRight ) {
174+ this . scrolling . emit ( ) ;
175+ this . unlockScroll ( ) ;
176+ } else {
177+ this . lockScroll ( ) ;
178+ }
179+ }
180+
181+ private touchScrollEnd ( ) {
182+ this . action = null ;
183+
184+ this . unlockScroll ( ) ;
185+ }
186+
187+ private lockScroll ( ) {
188+ const container : HTMLElement = this . el . shadowRoot . querySelector ( 'div.deckgo-code-container' ) ;
189+ container . style . setProperty ( 'overflow-y' , 'hidden' ) ;
190+ }
191+
192+ private unlockScroll ( ) {
193+ const container : HTMLElement = this . el . shadowRoot . querySelector ( 'div.deckgo-code-container' ) ;
194+ container . style . removeProperty ( 'overflow-y' ) ;
195+ }
196+
197+ private emitScrolling ( ) {
198+ if ( this . action === DeckdeckgoSlideCodeAction . SCROLL ) {
199+ this . scrolling . emit ( ) ;
200+ }
201+ }
202+
143203 private renderCode ( ) {
144204 return (
145205 this . code . map ( ( line : string ) => {
0 commit comments