astro-swiper 1.0.2 → 1.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/components/Swiper.astro +36 -16
- package/src/components/SwiperButtonNext.astro +1 -1
- package/src/components/SwiperButtonPrev.astro +1 -1
- package/src/components/SwiperPagination.astro +1 -1
- package/src/components/SwiperScrollbar.astro +1 -1
- package/src/components/SwiperSlide.astro +1 -1
- package/src/components/SwiperWrapper.astro +1 -1
- package/src/index.ts +8 -10
package/package.json
CHANGED
|
@@ -21,7 +21,7 @@ const {
|
|
|
21
21
|
|
|
22
22
|
<!-- Slider main container -->
|
|
23
23
|
<astro-swiper
|
|
24
|
-
class={
|
|
24
|
+
class:list={['swiper', uniqueClass, className]}
|
|
25
25
|
{...props}
|
|
26
26
|
data-options={JSON.stringify(options)}
|
|
27
27
|
data-linktothumbuniqueclass={linkToThumbUniqueClass}
|
|
@@ -33,11 +33,28 @@ const {
|
|
|
33
33
|
|
|
34
34
|
<script>
|
|
35
35
|
import Swiper from 'swiper/bundle'
|
|
36
|
-
import {
|
|
36
|
+
import type { SwiperOptions } from 'swiper/types';
|
|
37
|
+
|
|
38
|
+
/** contains the swiper json object once created, for each uniqueClass */
|
|
39
|
+
const _useSwiper: { [uniqueClass: string] : Swiper; } = {};
|
|
40
|
+
|
|
41
|
+
/** contains the option used when creating the swiper object for each uniqueClass */
|
|
42
|
+
const _useOptions: { [uniqueClass: string] : SwiperOptions; } = {};
|
|
43
|
+
|
|
44
|
+
/** contains all the uniqueClass that have their swiper object delayed because the
|
|
45
|
+
* related thumbnail swiper is not created yet
|
|
46
|
+
* For each uniqueClass, is equal to the thumbnail slider uniqueClass
|
|
47
|
+
*/
|
|
48
|
+
const _useDelaySwiper: { [uniqueClass: string] : string; } = {};
|
|
37
49
|
|
|
38
50
|
class AstroSwiper extends HTMLElement {
|
|
51
|
+
/** pointer to the swiper structure that was created using "new",
|
|
52
|
+
* even when not initialized */
|
|
53
|
+
astroSwiper: Swiper | undefined
|
|
54
|
+
|
|
39
55
|
constructor() {
|
|
40
56
|
super();
|
|
57
|
+
this.astroSwiper = undefined
|
|
41
58
|
|
|
42
59
|
// Read the message from the data attribute.
|
|
43
60
|
const options = JSON.parse(this.dataset.options || '{}')
|
|
@@ -47,34 +64,37 @@ const {
|
|
|
47
64
|
if (linkToThumbUniqueClass === '') {
|
|
48
65
|
// no thumbnail link - the swiper can be created immediatly
|
|
49
66
|
const swiper = new Swiper(`.${uniqueClass}`, options)
|
|
50
|
-
|
|
67
|
+
_useSwiper[uniqueClass] = swiper
|
|
68
|
+
this.astroSwiper = swiper
|
|
51
69
|
} else {
|
|
52
70
|
// a thumbnail link is provided
|
|
53
71
|
// if the thumbnail swiper is already created, the main slider can be created also
|
|
54
72
|
// otherwise, it will be added in the delayed swiper list
|
|
55
|
-
if (
|
|
56
|
-
options.thumbs = { swiper:
|
|
73
|
+
if (_useSwiper[linkToThumbUniqueClass] !== undefined) {
|
|
74
|
+
options.thumbs = { swiper: _useSwiper[linkToThumbUniqueClass], ...options.thumbs }
|
|
57
75
|
const swiper = new Swiper(`.${uniqueClass}`, options)
|
|
58
|
-
|
|
76
|
+
_useSwiper[uniqueClass] = swiper
|
|
77
|
+
this.astroSwiper = swiper
|
|
59
78
|
} else {
|
|
60
79
|
// cannot create the swiper as the thumbnail swiper not created yet
|
|
61
80
|
// will be done later
|
|
62
|
-
|
|
81
|
+
_useDelaySwiper[uniqueClass] = linkToThumbUniqueClass
|
|
63
82
|
}
|
|
64
83
|
}
|
|
65
84
|
|
|
66
|
-
|
|
85
|
+
_useOptions[uniqueClass] = options
|
|
67
86
|
|
|
68
87
|
// look if any delayed swiper can be created, that is its related thumbnail swiper is now created
|
|
69
|
-
Object.keys(
|
|
70
|
-
const delayedThumbClass =
|
|
71
|
-
if (
|
|
72
|
-
const delayedOptions =
|
|
73
|
-
delayedOptions.thumbs = { swiper:
|
|
88
|
+
Object.keys(_useDelaySwiper).forEach(delayedClass => {
|
|
89
|
+
const delayedThumbClass = _useDelaySwiper[delayedClass]
|
|
90
|
+
if (_useSwiper[delayedThumbClass] !== undefined) {
|
|
91
|
+
const delayedOptions = _useOptions[delayedClass]
|
|
92
|
+
delayedOptions.thumbs = { swiper: _useSwiper[delayedThumbClass], ...delayedOptions.thumbs }
|
|
74
93
|
const swiper = new Swiper(`.${delayedClass}`, delayedOptions)
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
94
|
+
_useSwiper[delayedClass] = swiper;
|
|
95
|
+
(document.querySelector(`.${delayedClass}`) as AstroSwiper).astroSwiper = swiper
|
|
96
|
+
_useOptions[delayedClass] = delayedOptions
|
|
97
|
+
delete _useDelaySwiper[delayedClass]
|
|
78
98
|
}
|
|
79
99
|
})
|
|
80
100
|
}
|
package/src/index.ts
CHANGED
|
@@ -36,14 +36,12 @@ export { default as SwiperScrollbar } from './components/SwiperScrollbar.astro'
|
|
|
36
36
|
export { default as SwiperSlide } from './components/SwiperSlide.astro'
|
|
37
37
|
export { default as SwiperWrapper } from './components/SwiperWrapper.astro'
|
|
38
38
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
39
|
+
declare class AstroSwiper extends HTMLElement {
|
|
40
|
+
/** pointer to the swiper structure that was created using "new",
|
|
41
|
+
* even when not initialized */
|
|
42
|
+
astroSwiper: Swiper | undefined
|
|
43
|
+
}
|
|
44
44
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
*/
|
|
49
|
-
export const useDelaySwiper: { [uniqueClass: string] : string; } = {};
|
|
45
|
+
export function getSwiperFromUniqueClass(uniqueClass: string): Swiper | undefined {
|
|
46
|
+
return (document.querySelector(`.${uniqueClass}`) as AstroSwiper)?.astroSwiper
|
|
47
|
+
}
|