-
Notifications
You must be signed in to change notification settings - Fork 1
/
view.js
132 lines (130 loc) · 2.87 KB
/
view.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/**
* WordPress dependencies
*/
import {
store,
getElement,
getContext,
withScope,
} from '@wordpress/interactivity';
const { state, actions } = store( 'iapi-gallery', {
state: {
get noPrevSlide() {
const ctx = getContext();
if ( ctx.continuous ) {
return false;
}
return ctx.currentSlide === 1;
},
get noNextSlide() {
const ctx = getContext();
if ( ctx.continuous ) {
return false;
}
return ctx.currentSlide === ctx.totalSlides;
},
get currentPos() {
const ctx = getContext();
return `translateX(-${ ( ctx.currentSlide - 1 ) * 100 }%)`;
},
get imageIndex() {
const ctx = getContext();
return `${ ctx.currentSlide }/${ ctx.totalSlides }`;
},
get transitionsSpeed() {
const ctx = getContext();
return Number( ctx.speed ) * 1000;
},
},
actions: {
prevImage: () => {
const ctx = getContext();
console.log( ctx.currentSlide );
if ( ctx.continuous && ctx.currentSlide === 1 ) {
ctx.currentSlide = ctx.totalSlides;
return;
}
ctx.currentSlide--;
},
nextImage: () => {
const ctx = getContext();
if (
( ctx.continuous || ctx.autoplay ) &&
ctx.currentSlide === ctx.totalSlides
) {
ctx.currentSlide = 1;
return;
}
ctx.currentSlide++;
},
onKeyDown: ( e ) => {
switch ( e.key ) {
case 'ArrowLeft': {
if ( ! state.noPrevSlide ) {
actions.prevImage();
}
break;
}
case 'ArrowRight': {
if ( ! state.noNextSlide ) {
actions.nextImage();
}
break;
}
}
},
onTouchStart: ( e ) => {
const ctx = getContext();
ctx.swipe = e.changedTouches[ 0 ].clientX;
},
onTouchEnd: ( e ) => {
const { swipe } = getContext();
if ( e.changedTouches[ 0 ].clientX < swipe ) {
if ( ! state.noNextSlide ) {
actions.nextImage();
}
} else {
if ( ! state.noPrevSlide ) {
actions.prevImage();
}
}
},
},
callbacks: {
initSlideShow: () => {
const ctx = getContext();
if ( ctx.autoplay ) {
const int = setInterval(
withScope( () => {
actions.nextImage();
} ),
state.transitionsSpeed
);
// The returned function executes when the element is removed from the DOM.
return () => clearInterval( int );
}
},
initSlide: () => {
const ctx = getContext();
// This is called by the core/cover blocks inside this block.
// Adds the element reference to the `slides` array.
const { ref } = getElement();
ctx.slides.push( ref );
// The returned function executes when the element is removed from
// the DOM.
return () => {
ctx.slides = ctx.slides.filter( ( s ) => s !== ref );
};
},
},
} );
/**
* Helper to log the data in a readable format. Useful for debugging parts of the store.
*
* Use console.log for non-store values.
*
* @param {*} data
* @returns
*/
const debugLog = ( data ) =>
console.log( JSON.parse( JSON.stringify( data ) ) );