Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(runtime-web): 1. swiper item数据更新异常 2. 单个swiper-item场景禁用autoplay #74

Merged
merged 1 commit into from
Jul 11, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
fix(runtime-web): 解决swiper item数据更新异常 && 单个swiper-item 场景无需autoplay的问题
  • Loading branch information
shujian-cao committed Jul 11, 2023
commit 22334e5430bb25ebdda1c0415bbf0e0102be3e6d
64 changes: 59 additions & 5 deletions packages/runtime-web/src/components/src/views/swiper/swiper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ export default class TigaSwiper extends LitElement {

@internalProperty() updating = false

@internalProperty() slideWrapperObserver: MutationObserver

constructor() {
super()

Expand Down Expand Up @@ -185,7 +187,31 @@ export default class TigaSwiper extends LitElement {
})
this.dispatchEvent(event)
},
slideChangeTransitionStart: () => {
if (!this.swiper || !this.swiper.initialized) return

const $wrapperEl = this.swiper.$wrapperEl
const params = this.swiper.params
// 替换duplicate slide,避免duplicate slide不更新
$wrapperEl
.children(
'.' + params.slideClass + '.' + params.slideDuplicateClass
)
.each(function () {
const idx = this.getAttribute('data-swiper-slide-index')
this.innerHTML = $wrapperEl
.children(
'.' +
params.slideClass +
'[data-swiper-slide-index="' +
idx +
'"]:not(.' +
params.slideDuplicateClass +
')'
)
.html()
})
},
slideChangeTransitionEnd: () => {
if (!this.swiper || !this.swiper.initialized) return

Expand Down Expand Up @@ -228,6 +254,33 @@ export default class TigaSwiper extends LitElement {
}

this.swiper = new window.Swiper(container, options)
this.observeSlideWrapper()
}

observeSlideWrapper = () => {
const wrapperEl = this.swiper.$wrapperEl?.[0]
if (!wrapperEl) return

this.slideWrapperObserver = new MutationObserver(this.handleAutoPlayListen)
this.slideWrapperObserver.observe(wrapperEl, { childList: true })
}

handleAutoPlayListen = () => {
const slides = this.swiperWrapper?.querySelectorAll('.swiper-slide') || []
// loop状态下,有3个silde时表示仅一个有效slide,无需autoplay
if (
this[properties.CIRCULAR] &&
slides.length === 3 &&
this[properties.AUTOPLAY]
) {
this.swiper.autoplay && this.swiper.autoplay.stop()
} else if (this[properties.AUTOPLAY]) {
if (this.swiper.params.autoplay.disableOnInteraction === true) {
this.swiper.params.autoplay.disableOnInteraction = false
}
this.swiper.params.autoplay.delay = this[properties.INTERVAL]
this.swiper.autoplay && this.swiper.autoplay.start()
}
}

loadScript = () =>
Expand Down Expand Up @@ -256,15 +309,15 @@ export default class TigaSwiper extends LitElement {
}

overwriteDomOperation() {
const swiperWrapper = this.querySelector(
this.swiperWrapper = this.querySelector(
`.tiga-swiper-${this.swiperId} > .swiper-container > .swiper-wrapper`
) as any

;(this as any).appendChild = (newChild: HTMLElement) => {
if (!this.isElementNode(newChild)) return

newChild.classList.add('swiper-slide')
return swiperWrapper.appendChild(newChild)
return this.swiperWrapper.appendChild(newChild)
}
;(this as any).insertBefore = (
newChild: HTMLElement,
Expand All @@ -273,7 +326,7 @@ export default class TigaSwiper extends LitElement {
if (!this.isElementNode(newChild) || !this.isElementNode(refChild)) return

newChild.classList.add('swiper-slide')
return swiperWrapper.insertBefore(newChild, refChild)
return this.swiperWrapper.insertBefore(newChild, refChild)
}
;(this as any).replaceChild = (
newChild: HTMLElement,
Expand All @@ -282,12 +335,12 @@ export default class TigaSwiper extends LitElement {
if (!this.isElementNode(newChild) || !this.isElementNode(oldChild)) return

newChild.classList.add('swiper-slide')
return swiperWrapper.replaceChild(newChild, oldChild)
return this.swiperWrapper.replaceChild(newChild, oldChild)
}
;(this as any).removeChild = (oldChild: HTMLElement) => {
if (!this.isElementNode(oldChild)) return

return swiperWrapper.removeChild(oldChild)
return this.swiperWrapper.removeChild(oldChild)
}
}

Expand Down Expand Up @@ -347,6 +400,7 @@ export default class TigaSwiper extends LitElement {
disconnectedCallback() {
super.disconnectedCallback()

this.slideWrapperObserver?.disconnect()
this.swiper.destroy()
this.swiper = null
}
Expand Down