Skip to content
This repository was archived by the owner on Feb 6, 2024. It is now read-only.

Commit 390bc99

Browse files
feat(#41): slide code, on mobile, active scrolling-y on click
1 parent 6c746ff commit 390bc99

File tree

6 files changed

+49
-47
lines changed

6 files changed

+49
-47
lines changed

src/components.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ export namespace Components {
165165
'anchorZoom'?: string;
166166
'hideAnchor'?: boolean;
167167
'language'?: string;
168-
'onScrolling'?: (event: CustomEvent<void>) => void;
168+
'onScrolling'?: (event: CustomEvent<boolean>) => void;
169169
'onSlideDidLoad'?: (event: CustomEvent<void>) => void;
170170
'src'?: string;
171171
}

src/components/deck/deckdeckgo-deck/deckdeckgo-deck.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,8 @@ export class DeckdeckgoDeck {
121121
}
122122

123123
@Listen('scrolling')
124-
scrolling() {
125-
this.blockSlide = true;
124+
scrolling($event: CustomEvent) {
125+
this.blockSlide = $event ? $event.detail : false;
126126
}
127127

128128
private start(e: Event) {
@@ -152,7 +152,6 @@ export class DeckdeckgoDeck {
152152

153153
private async stop(e: Event) {
154154
if (this.blockSlide) {
155-
this.blockSlide = false;
156155
return;
157156
}
158157

src/components/slides/deckdeckgo-slide-code/deckdeckgo-slide-code.scss

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,12 @@ div.deckgo-slide {
77
}
88

99
div.deckgo-slide-code-container {
10-
overflow-y: auto;
1110
width: 100%;
11+
overflow-y: auto;
12+
13+
&.deckgo-slide-code-container-mobile {
14+
overflow-y: hidden;
15+
}
1216
}
1317

1418
::slotted([slot="title"]) {

src/components/slides/deckdeckgo-slide-code/deckdeckgo-slide-code.tsx

Lines changed: 37 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {Component, Element, Event, EventEmitter, Method, Prop} from '@stencil/core';
1+
import {Component, Element, Event, EventEmitter, Method, Prop, State} from '@stencil/core';
22

33
import {DeckdeckgoSlide, DeckdeckgoSlideUtils} from '../deckdeckgo-slide';
44
import {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>

src/components/slides/deckdeckgo-slide.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ export class DeckdeckgoSlideUtils {
9999
}
100100

101101
const a: string = navigator.userAgent || navigator.vendor || (window as any).opera;
102+
102103
return (/(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows ce|xda|xiino/i.test(a) || /1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp( i|ip)|hs\-c|ht(c(\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac( |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc\-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas\-|your|zeto|zte\-/i.test(a.substr(0, 4)));
103104
}
104105

src/index.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77

88
<script src="/build/deckdeckgo.js"></script>
99

10+
<script src="https://unpkg.com/deckdeckgo-highlight-code@latest/dist/deckdeckgo-highlight-code.js"></script>
11+
1012
</head>
11-
<body style="margin: 0;">
13+
<body style="margin: 0; overflow: hidden;">
1214

1315
<deckgo-deck id="slider">
1416
<deckgo-slide-title>

0 commit comments

Comments
 (0)