mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-04-06 03:57:59 +08:00
80 lines
1.7 KiB
TypeScript
80 lines
1.7 KiB
TypeScript
import { extend, inBrowser, withInstall, ComponentInstance } from '../utils';
|
|
import { mountComponent, usePopupState } from '../utils/mount-component';
|
|
import VanImagePreview from './ImagePreview';
|
|
import type { App } from 'vue';
|
|
import type { ImagePreviewOptions } from './types';
|
|
|
|
let instance: ComponentInstance;
|
|
|
|
const defaultConfig: ImagePreviewOptions = {
|
|
loop: true,
|
|
images: [],
|
|
maxZoom: 3,
|
|
minZoom: 1 / 3,
|
|
onScale: undefined,
|
|
onClose: undefined,
|
|
onChange: undefined,
|
|
teleport: 'body',
|
|
className: '',
|
|
showIndex: true,
|
|
closeable: false,
|
|
closeIcon: 'clear',
|
|
transition: undefined,
|
|
beforeClose: undefined,
|
|
overlayStyle: undefined,
|
|
startPosition: 0,
|
|
swipeDuration: 300,
|
|
showIndicators: false,
|
|
closeOnPopstate: true,
|
|
closeIconPosition: 'top-right',
|
|
};
|
|
|
|
function initInstance() {
|
|
({ instance } = mountComponent({
|
|
setup() {
|
|
const { state, toggle } = usePopupState();
|
|
const onClosed = () => {
|
|
(state as any).images = [];
|
|
};
|
|
|
|
return () => (
|
|
<VanImagePreview
|
|
{...state}
|
|
{...{
|
|
onClosed,
|
|
'onUpdate:show': toggle,
|
|
}}
|
|
/>
|
|
);
|
|
},
|
|
}));
|
|
}
|
|
|
|
const ImagePreview = (
|
|
images: string[] | ImagePreviewOptions,
|
|
startPosition = 0
|
|
) => {
|
|
/* istanbul ignore if */
|
|
if (!inBrowser) {
|
|
return;
|
|
}
|
|
|
|
if (!instance) {
|
|
initInstance();
|
|
}
|
|
|
|
const options = Array.isArray(images) ? { images, startPosition } : images;
|
|
|
|
instance.open(extend({}, defaultConfig, options));
|
|
|
|
return instance;
|
|
};
|
|
|
|
ImagePreview.Component = withInstall(VanImagePreview);
|
|
|
|
ImagePreview.install = (app: App) => {
|
|
app.use(ImagePreview.Component);
|
|
};
|
|
|
|
export { ImagePreview };
|