Skip to content

Commit ccedb52

Browse files
authored
feat(photo-swipe)!: rebuild photo-swipe plugin (#303)
`registerPhotoSwipe` is removed and `delay` option is no longer needed
1 parent 487d8c4 commit ccedb52

File tree

19 files changed

+184
-283
lines changed

19 files changed

+184
-283
lines changed

docs/plugins/features/photo-swipe.md

-40
Original file line numberDiff line numberDiff line change
@@ -64,20 +64,6 @@ In preview mode, you can:
6464
- Default: `true`
6565
- Details: Whether close the current image when scrolling.
6666

67-
### delay
68-
69-
- Type: `number`
70-
- Default: `800`
71-
- Details:
72-
73-
The delay of operating dom, in ms.
74-
75-
::: tip
76-
77-
If the theme you are using has a switching animation, it is recommended to configure this option to `Switch animation duration + 200`.
78-
79-
:::
80-
8167
### locales
8268

8369
- Type: `PhotoSwipePluginLocaleConfig`
@@ -254,32 +240,6 @@ onUnmounted(() => {
254240
</template>
255241
```
256242

257-
`registerPhotoSwipe` allows you to register photoswipe for the given image elements:
258-
259-
```vue
260-
<script setup lang="ts">
261-
import { registerPhotoSwipe } from '@vuepress/plugin-photo-swipe/client'
262-
import { onMounted, onUnmounted } from 'vue'
263-
264-
let destroy: () => null | void = null
265-
266-
onMounted(async () => {
267-
await nextTick()
268-
269-
const images = Array.from(document.querySelectorAll('img'))
270-
271-
// create a new photoswipe instance on image elements
272-
destroy = await registerPhotoSwipe(images, {
273-
// photoswipe options
274-
})
275-
})
276-
277-
onUnmounted(() => {
278-
destroy?.()
279-
})
280-
</script>
281-
```
282-
283243
## Styles
284244

285245
You can customize the style via CSS variables:

docs/zh/plugins/features/photo-swipe.md

-40
Original file line numberDiff line numberDiff line change
@@ -64,20 +64,6 @@ export default {
6464
- 默认值:`true`
6565
- 详情:是否在滚动时关闭当前图片。
6666

67-
### delay
68-
69-
- 类型:`number`
70-
- 默认值:`800`
71-
- 详情:
72-
73-
操作页面 DOM 的延时,单位 ms。
74-
75-
::: tip
76-
77-
如果你使用的主题有切换动画,建议配置此选项为 `切换动画时长 + 200`
78-
79-
:::
80-
8167
### locales
8268

8369
- 类型:`PhotoSwipePluginLocaleConfig`
@@ -254,32 +240,6 @@ onUnmounted(() => {
254240
</template>
255241
```
256242

257-
`registerPhotoSwipe` 允许你为给定的图片元素注册 photoswipe:
258-
259-
```vue
260-
<script setup lang="ts">
261-
import { registerPhotoSwipe } from '@vuepress/plugin-photo-swipe/client'
262-
import { onMounted, onUnmounted } from 'vue'
263-
264-
let destroy: () => null | void = null
265-
266-
onMounted(async () => {
267-
await nextTick()
268-
269-
const images = Array.from(document.querySelectorAll('img'))
270-
271-
// 通过图片元素创建一个新的 photoswipe 实例
272-
destroy = await registerPhotoSwipe(images, {
273-
// photoswipe 选项
274-
})
275-
})
276-
277-
onUnmounted(() => {
278-
destroy?.()
279-
})
280-
</script>
281-
```
282-
283243
## 样式
284244

285245
你可以通过 CSS 变量来自定义部分样式:

e2e/docs/.vuepress/config.ts

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { catalogPlugin } from '@vuepress/plugin-catalog'
66
import { copyrightPlugin } from '@vuepress/plugin-copyright'
77
import { feedPlugin } from '@vuepress/plugin-feed'
88
import { noticePlugin } from '@vuepress/plugin-notice'
9+
import { photoSwipePlugin } from '@vuepress/plugin-photo-swipe'
910
import { pwaPlugin } from '@vuepress/plugin-pwa'
1011
import { redirectPlugin } from '@vuepress/plugin-redirect'
1112
import { registerComponentsPlugin } from '@vuepress/plugin-register-components'
@@ -98,6 +99,7 @@ export default defineUserConfig({
9899
},
99100

100101
themePlugins: {
102+
mediumZoom: false,
101103
sitemap: {
102104
devServer: true,
103105
devHostname: 'https://ecosystem-e2e-test.com',
@@ -256,6 +258,7 @@ export default defineUserConfig({
256258
},
257259
],
258260
}),
261+
photoSwipePlugin(),
259262
pwaPlugin({
260263
manifest: {
261264
icons: [

e2e/docs/photo-swipe/disabled.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
photoSwipe: false
3+
---
4+
5+
![](/logo.png)
6+
![](/favicon.ico)

e2e/docs/photo-swipe/enabled.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
![](/logo.png)
2+
![](/favicon.ico)

e2e/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"@vuepress/plugin-copyright": "workspace:*",
2323
"@vuepress/plugin-feed": "workspace:*",
2424
"@vuepress/plugin-notice": "workspace:*",
25+
"@vuepress/plugin-photo-swipe": "workspace:*",
2526
"@vuepress/plugin-pwa": "workspace:*",
2627
"@vuepress/plugin-redirect": "workspace:*",
2728
"@vuepress/plugin-register-components": "workspace:*",
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
1-
import { isString, useLocaleConfig, wait } from '@vuepress/helper/client'
2-
import { nextTick, onMounted, onUnmounted, watch } from 'vue'
3-
import { usePageData, usePageFrontmatter } from 'vuepress/client'
1+
import { isArray, isString, useLocaleConfig } from '@vuepress/helper/client'
2+
import { useEventListener } from '@vueuse/core'
3+
import type PhotoSwipe from 'photoswipe'
4+
import type { SlideData } from 'photoswipe'
5+
import { computed, onMounted, onUnmounted } from 'vue'
6+
import { usePageFrontmatter } from 'vuepress/client'
47
import type { PhotoSwipePluginLocaleData } from '../../shared/index.js'
58
import { usePhotoSwipeOptions } from '../helpers/index.js'
6-
import type { PhotoSwipeBehaviorOptions } from '../utils/index.js'
7-
import { getImages, registerPhotoSwipe } from '../utils/index.js'
9+
import type { PhotoSwipeBehaviorOptions } from '../typings.js'
10+
import {
11+
LOADING_ICON,
12+
resolveImageInfoFromElement,
13+
setupPhotoSwipe,
14+
} from '../utils/index.js'
815

916
import 'photoswipe/dist/photoswipe.css'
1017
import '../styles/photo-swipe.css'
@@ -15,57 +22,107 @@ export interface UsePhotoSwipeOptions extends PhotoSwipeBehaviorOptions {
1522
string,
1623
Record<`${keyof PhotoSwipePluginLocaleData}Title`, string>
1724
>
18-
/** @default 500 */
19-
delay?: number
2025
}
2126

2227
export const usePhotoSwipe = ({
2328
selector,
2429
locales,
25-
delay = 500,
2630
download = true,
2731
fullscreen = true,
2832
scrollToClose = true,
2933
}: UsePhotoSwipeOptions): void => {
3034
const photoSwipeOptions = usePhotoSwipeOptions()
3135
const locale = useLocaleConfig(locales)
32-
const page = usePageData()
3336
const frontmatter = usePageFrontmatter<{ photoSwipe: boolean | string }>()
3437

35-
let destroy: (() => void) | null = null
36-
37-
const setupPhotoSwipe = (): void => {
38+
const imageSelector = computed(() => {
3839
const { photoSwipe } = frontmatter.value
3940

40-
if (photoSwipe !== false)
41-
void nextTick()
42-
.then(() => wait(delay))
43-
.then(async () => {
44-
const imageSelector = isString(photoSwipe) ? photoSwipe : selector
45-
46-
destroy = await registerPhotoSwipe(getImages(imageSelector), {
47-
...photoSwipeOptions.value,
48-
...locale.value,
49-
download,
50-
fullscreen,
51-
scrollToClose,
52-
})
41+
return photoSwipe === false
42+
? null
43+
: isString(photoSwipe)
44+
? photoSwipe
45+
: isArray(selector)
46+
? selector.join(', ')
47+
: selector
48+
})
49+
50+
const options = computed(() => ({
51+
...photoSwipeOptions.value,
52+
...locale.value,
53+
download,
54+
fullscreen,
55+
scrollToClose,
56+
}))
57+
58+
let photoSwipeId = 0
59+
let photoSwipe: PhotoSwipe | null = null
60+
61+
const handlePhotoSwipe = async (event: MouseEvent): Promise<void> => {
62+
const el = event.target as HTMLImageElement
63+
64+
if (imageSelector.value && el.matches(imageSelector.value)) {
65+
photoSwipe?.destroy()
66+
67+
const { default: PhotoSwipe } = await import(
68+
/* webpackChunkName: "photo-swipe" */ 'photoswipe'
69+
)
70+
71+
const images = Array.from(
72+
document.querySelectorAll<HTMLImageElement>(imageSelector.value),
73+
)
74+
const currentIndex = images.findIndex((image) => image === el)
75+
76+
const dataSource = images.map<SlideData>((image) => ({
77+
html: LOADING_ICON,
78+
element: image,
79+
msrc: image.src,
80+
}))
81+
82+
dataSource.splice(currentIndex, 1, await resolveImageInfoFromElement(el))
83+
84+
const id = Date.now()
85+
86+
photoSwipeId = id
87+
photoSwipe = new PhotoSwipe({
88+
preloaderDelay: 0,
89+
showHideAnimationType: 'zoom',
90+
...options,
91+
dataSource,
92+
index: currentIndex,
93+
...(scrollToClose
94+
? { closeOnVerticalDrag: true, wheelToZoom: false }
95+
: {}),
96+
})
97+
98+
setupPhotoSwipe(photoSwipe, { download, fullscreen })
99+
100+
photoSwipe.init()
101+
102+
photoSwipe.on('destroy', () => {
103+
photoSwipe = null
104+
photoSwipeId = 0
105+
})
106+
107+
images.forEach((image, index) => {
108+
if (index === currentIndex || photoSwipeId !== id) return
109+
110+
void resolveImageInfoFromElement(image).then((data) => {
111+
dataSource.splice(index, 1, data)
112+
photoSwipe?.refreshSlideContent(index)
53113
})
114+
})
115+
}
54116
}
55117

56118
onMounted(() => {
57-
setupPhotoSwipe()
58-
59-
watch(
60-
() => [page.value.path, photoSwipeOptions.value],
61-
() => {
62-
destroy?.()
63-
setupPhotoSwipe()
64-
},
65-
)
119+
useEventListener('click', handlePhotoSwipe)
120+
useEventListener('wheel', () => {
121+
if (options.value.scrollToClose) photoSwipe?.close()
122+
})
66123
})
67124

68125
onUnmounted(() => {
69-
destroy?.()
126+
photoSwipe?.destroy()
70127
})
71128
}

plugins/features/plugin-photo-swipe/src/client/config.ts

-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import { injectPhotoSwipeConfig } from './helpers/index.js'
66
import './styles/vars.css'
77

88
declare const __PS_SELECTOR__: string[] | string
9-
declare const __PS_DELAY__: number
109
declare const __PS_LOCALES__: Record<
1110
string,
1211
Record<`${keyof PhotoSwipePluginLocaleData}Title`, string>
@@ -17,7 +16,6 @@ declare const __PS_SCROLL_TO_CLOSE__: boolean
1716

1817
const selector = __PS_SELECTOR__
1918
const locales = __PS_LOCALES__
20-
const delay = __PS_DELAY__
2119
const download = __PS_DOWNLOAD__
2220
const fullscreen = __PS_FULLSCREEN__
2321
const scrollToClose = __PS_SCROLL_TO_CLOSE__
@@ -29,7 +27,6 @@ export default defineClientConfig({
2927
setup: () => {
3028
usePhotoSwipe({
3129
selector,
32-
delay,
3330
locales,
3431
download,
3532
fullscreen,
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export * from './helpers/index.js'
22
export * from './composables/index.js'
3-
export * from './utils/index.js'
3+
export type * from './typings.js'
4+
export { createPhotoSwipe } from './utils/index.js'
45
export type * from '../shared/index.js'

0 commit comments

Comments
 (0)